Hardwarie.h

///----------------------------------------------------------------------
//  Hardwarie.h
//
//  written by Anders F Björklund <afb@algonet.se>
//  copyright ©2000-2002 afb. All Rights Reserved
//
//  Description: Defines a common API for Hardware Graphics Acceleration.
///----------------------------------------------------------------------
 
#ifndef __Hardwarie__
#define __Hardwarie__
 
#ifndef __QUICKDRAW__
#include <Quickdraw.h>
#endif
 
#ifndef __QDOFFSCREEN__
#include <QDOffscreen.h>
#endif
 
#ifdef __cplusplus
extern "C" {
#endif
 
#define kHardwarieHeaderVersion     0x01006001UL // 1.0b1
 
#define kHardwarieInvisibleAlpha    0.0f
#define kHardwarieOpaqueAlpha       1.0f
 
#define kHardwarieOriginalScale     1.0f
#define kHardwarieCalculateScale    0.0f
 
#define kHardwarieNoRotation        0.0f
#define kHardwarieRightRotation     90.0f
 
///----------------------------------------------------------------------
// Types (private/opaque)
 
typedef struct HardwarieContext     *HardwarieContextPtr;
typedef struct HardwarieFrame       *HardwarieFramePtr;
typedef struct HardwarieTexture     *HardwarieTexturePtr;
 
///----------------------------------------------------------------------
// Types (public/transparent)
 
typedef struct HardwariePoint       HardwariePoint, *HardwariePointPtr;
typedef struct HardwarieRect        HardwarieRect,  *HardwarieRectPtr;
typedef struct HardwarieColor       HardwarieColor, *HardwarieColorPtr;
 
struct HardwariePoint
{
    float   h;
    float   v;
};
 
struct HardwarieRect
{
    float   left;
    float   top;
    float   right;
    float   bottom;
};
 
struct HardwarieColor
{
    float   red;
    float   green;
    float   blue;
    float   alpha;
};
 
///----------------------------------------------------------------------
// Utility functions
 
void HardwarieSetPoint(HardwariePoint *point, PointPtr p);
 
void HardwarieSetRect(HardwarieRect *rect, RectPtr r);
 
void HardwarieSetColor(HardwarieColor *color, RGBColorPtr c);
 
OSErr HardwarieCreateGWorldFromPict(GWorldPtr *newGWorld,
    Rect *newBounds, short pictID, Boolean grayscale );
 
 
int HardwarieRoundUp(int x);
 
void HardwarieSetMaxTextureSize(int size);
 
int HardwarieGetMaxTextureSize(void);
 
void HardwarieSetDrawOutlines(Boolean draw);
 
Boolean HardwarieGetDrawOutlines(void);
 
UInt32 HardwarieGetVersion(void);
 
StringPtr HardwarieGetRenderName(void);
 
StringPtr HardwarieGetEngineName(void);
 
Size HardwarieGetTextureMemoryAvailable(void);
 
Size HardwarieGetTotalTextureMemory(void);
 
///----------------------------------------------------------------------
// Setup functions
 
OSErr HardwarieInit(
    HardwarieContextPtr *newContext,
    GDHandle            device,
    CWindowPtr          window,
    RectPtr             bounds,
    Boolean             fullScreen);
 
void HardwarieExit(
    HardwarieContextPtr *oldContext);
 
OSErr HardwarieLoadFrame(
    HardwarieFramePtr    *newFrameP,
    HardwarieContextPtr context,
    GWorldPtr           pictureGWorld,
    RectPtr             pictureRect,
    GWorldPtr           maskGWorld,
    RectPtr             maskRect,
    GWorldPtr           alphaGWorld,
    RectPtr             alphaRect);
 
OSErr HardwarieReloadFrame(
    HardwarieFramePtr   oldFrameP,
    HardwarieContextPtr context,
    GWorldPtr           pictureGWorld,
    RectPtr             pictureRect,
    GWorldPtr           maskGWorld,
    RectPtr             maskRect,
    GWorldPtr           alphaGWorld,
    RectPtr             alphaRect);
 
void HardwarieUnloadFrame(
    HardwarieFramePtr   *oldFrameP);
 
void HardwariePause(
    HardwarieContextPtr context );
 
void HardwarieResume(
    HardwarieContextPtr context );
 
///----------------------------------------------------------------------
// Drawing functions
 
void HardwarieBegin(
    HardwarieContextPtr context );
 
void HardwarieEnd(
    HardwarieContextPtr context );
 
void HardwarieClearBackground(
    HardwarieContextPtr context,
    HardwarieColorPtr   color);
 
void HardwariePutPixel(
    HardwarieContextPtr context,
    HardwariePointPtr   loc,
    HardwarieColorPtr   color);
 
void HardwarieDrawLine(
    HardwarieContextPtr context,
    HardwariePointPtr   from,
    HardwariePointPtr   to,
    HardwarieColorPtr   color);
 
void HardwariePaintRect(
    HardwarieContextPtr context,
    HardwarieRectPtr    rect,
    HardwarieColorPtr   color);
 
void HardwarieDrawFrame(
    HardwarieFramePtr   srcFrameP,
    HardwarieRectPtr    dstRect,
    float               alpha);
 
void HardwarieScaledDrawFrame(
    HardwarieFramePtr   srcFrameP,
    HardwarieRectPtr    dstRect,
    float               alpha,
    float               horizScale,
    float               vertScale);
 
void HardwarieRotatedDrawFrame(
    HardwarieFramePtr   srcFrameP,
    HardwarieRectPtr    dstRect,
    float               alpha,
    float               degreesRotation);
 
///----------------------------------------------------------------------
// C++ wrapper classes
 
#ifdef __cplusplus
 
} // extern
 
class THardwarieException;
class THardwarieContext;
class THardwarieFrame;
 
class THardwariePoint;
class THardwarieRect;
class THardwarieColor;
 
#include <stdexcept>
 
class THardwarieException : public std::runtime_error
{
    protected:
        OSErr err;
    public:
        THardwarieException(const char* what, OSErr error) : std::runtime_error(what)
        {
            err = error;
        }
        OSErr error()
        {
            return err;
        }
};
 
class THardwarieContext
{
    protected:
        HardwarieContextPtr context;
 
        operator HardwarieContextPtr() { return context; }
    
    public:
        THardwarieContext(GDHandle device, CWindowPtr window,
            RectPtr bounds = NULL, bool fullscreen = false) throw (THardwarieException)
        {
            context = NULL;
            OSErr err = HardwarieInit(&context, device, window, bounds, fullscreen);
            if (err != noErr)
                throw THardwarieException("THardwarieContext::ctor",err);
        }
        
        ~THardwarieContext()
        {
            HardwarieExit(&context);
        }
 
        void pause()
        {
            HardwariePause(context);
        }
 
        void resume()
        {
            HardwarieResume(context);
        }
 
        void begin()
        {
            HardwarieBegin(context);
        }
 
        void end()
        {
            HardwarieEnd(context);
        }
 
        void clearBackground(HardwarieColorPtr color)
        {
            HardwarieClearBackground(context, color);
        }
 
        void putPixel(HardwariePointPtr loc, HardwarieColorPtr color)
        {
            HardwariePutPixel(context, loc, color);
        }
 
        void drawLine(HardwariePointPtr from, HardwariePointPtr to, HardwarieColorPtr color)
        {
            HardwarieDrawLine(context, from, to, color);
        }
};
 
class THardwarieFrame
{
    protected:
        HardwarieFramePtr frame;
 
        operator HardwarieFramePtr() { return frame; }
 
    public:
        THardwarieFrame(THardwarieContext &context,
            GWorldPtr imageGWorld, RectPtr imageRect,
            GWorldPtr maskGWorld = NULL, RectPtr maskRect = NULL,
            GWorldPtr alphaGWorld = NULL, RectPtr alphaRect = NULL) throw (THardwarieException)
        {
            frame = NULL;
            OSErr err = HardwarieLoadFrame( &frame, context,
                imageGWorld, imageRect, maskGWorld, maskRect, alphaGWorld, alphaRect);
            if (err != noErr)
                throw THardwarieException("THardwarieFrame::ctor",err);
        }
        
        ~THardwarieFrame() throw()
        {
            HardwarieUnloadFrame(&frame);
        }
    
        void reload(THardwarieContext &context,
            GWorldPtr imageGWorld, RectPtr imageRect,
            GWorldPtr maskGWorld = NULL, RectPtr maskRect = NULL,
            GWorldPtr alphaGWorld = NULL, RectPtr alphaRect = NULL) throw (THardwarieException)
        {
            OSErr err = HardwarieReloadFrame( frame, context,
                imageGWorld, imageRect, maskGWorld, maskRect, alphaGWorld, alphaRect);
            if (err != noErr)
                throw THardwarieException("THardwarieFrame::reload",err);
        }
        
        void draw(HardwarieRectPtr rect, float alpha = kHardwarieOpaqueAlpha)
        {
            HardwarieDrawFrame(frame, rect, alpha);
        }
 
        void drawScaled(HardwarieRectPtr rect, float alpha = kHardwarieOpaqueAlpha,
            float horizScale = kHardwarieCalculateScale, float vertScale = kHardwarieCalculateScale)
        {
            HardwarieScaledDrawFrame(frame, rect, alpha, horizScale, vertScale);
        }
    
        void drawRotated(HardwarieRectPtr rect, float alpha = kHardwarieOpaqueAlpha,
            float degreesRotation = kHardwarieNoRotation)
        {
            HardwarieRotatedDrawFrame(frame, rect, alpha, degreesRotation);
        }
};
 
class THardwariePoint
{
    private:
        HardwariePoint point;
 
        operator HardwariePointPtr() { return &point; }
 
    public:
        THardwariePoint(float h = 0.0f, float v = 0.0f) throw()
        {
            point.h = h; point.v = v;
        }
        
        THardwariePoint(Point &p) throw()
        {
            HardwarieSetPoint(&point,&p);
        }
};
 
class THardwarieRect
{
    private:
        HardwarieRect  rect;
 
        operator HardwarieRectPtr() { return &rect; }
 
    public:
        THardwarieRect(float left = 0.0f, float top = 0.0f, float right = 0.0f, float bottom = 0.0f) throw()
        {
            rect.left = left; rect.top = top; rect.right = right; rect.bottom = bottom;
        }
        
        THardwarieRect(Rect &r) throw()
        {
            HardwarieSetRect(&rect,&r);
        }
};
 
class THardwarieColor
{
    private:
        HardwarieColor color;
 
        operator HardwarieColorPtr() { return &color; }
 
    public:
        THardwarieColor(float red = 0.0f, float green = 0.0f, float blue = 0.0f, float alpha = kHardwarieOpaqueAlpha) throw ()
        {
            color.red = red; color.green = green; color.blue = blue; color.alpha = alpha;
        }
        
        THardwarieColor(RGBColor &c) throw()
        {
            HardwarieSetColor(&color,&c);
        }
};
 
#endif // __cplusplus
 
#endif //__Hardwarie__