GB2D/include/gb2d.h

00001 /*------------------------------------*
00002  * ghulbus2D                          *
00003  *  A 2D graphics framework           *
00004  *-----------------------------V-1.1--*/
00005 /***********************************************+
00006 | Optional modules:                             |
00007 |  - GB2D__USE_GBUI   (requires DXUT)           |
00008 |  - GB2D__USE_GBTEXT (requires DXUT)           |
00009 |                                               |
00010 | (flags for modules have to be set in          |
00011 |   the build environment!)                     |
00012 +***********************************************/
00013 
00014 #ifndef _WINDOWS_
00015 #include "windows.h"
00016 #endif
00017 #ifndef _D3D9_H_
00018 #include "d3d9.h"
00019 #endif
00020 #ifndef __D3DX9_H__
00021 #include "d3dx9.h"
00022 #endif
00023 #ifndef _VECTOR_
00024 #include <vector>
00025 #endif
00026 #ifndef _GHULBUS2D_
00027 #define _GHULBUS2D_
00028   //TYPEDEFS:
00029   class gb2DManager;                //Main engine object
00030   class gb2DSprite;                 //Used for storing sprite-data
00031   class gb2DImageLoader;            //Used for loading TGA image files
00032   class gb2DFont;                   //Used for dx3d-based text-output
00033   typedef gb2DManager *LPGB2D;
00034   typedef gb2DSprite  *LPGBSPRITE;
00035   typedef gb2DFont    *LPGBFONT;
00036 # ifdef GB2D__USE_GBTEXT
00037       class gb2DText;                   //custom textengine
00038       typedef gb2DText    *LPGBTEXT;
00039 # else
00040       typedef void        *LPGBTEXT;
00041 # endif
00042 #  ifdef GB2D__USE_GBUI
00043       class gbUIManager;
00044       typedef gbUIManager *LPGBUI;
00045 #  else
00046       typedef void        *LPGBUI;
00047 #  endif
00048 
00049 
00050   enum GB2DERROR {              //describes the errorstate of any GB2D-object
00051     GB2D_OK,                        //No errors
00052     GB2D_FAILED,                    //General failure, check hres for further detail
00053     GB2D_OUTOFMEMORY,               //memory allocation failed
00054     GB2D_NOTIMPLEMENTED,            //indicates that a requested feature is not yet implemented
00055     GB2D_D3DCREATEFAILED,           //Failure while calling Direct3DCreate9()
00056     GB2D_D3DCREATEDEVICEFAILED,     //Failure while calling IDirect3D9::CreateDevice()
00057     GB2D_D3DCREATETEXTUREFAILED,    //Failure while calling IDirect3DDevice9::CreateTexture() e.g. while initializing framebuffer
00058     GB2D_D3DCREATESPRITEFAILED,     //Failure while calling D3DXCreateSprite() e.g. while initializing framebuffer
00059     GB2D_D3DCREATEFONTFAILED,       //Failure while calling D3DXCreateFont()
00060     GB2D_RENDEROUTOFBOUNDARIES,     //tried to render to an area out of buffer
00061     GB2D_FILEOPENFAILED,            //a file could not be opened (doesn't exist?)
00062     GB2D_FILEREADERROR,             //reading from a file failed
00063     GB2D_DEVICEACCESSFAILED,        //usually triggered when trying to render to an unlocked buffer
00064     GB2D_ENUMERATIONFAILED,         //an error occured while enumerating
00065     GB2D_ILLEGALPARAMETER,          //one ore more specified parameters are invalid
00066     GB2D_NOTACTIVATED,              //tried to use a feature that was disabled via preprocessor flag
00067   };
00068 
00069 
00070   //GLOBALS:
00071   const wchar_t* TranslateErrorStateW(GB2DERROR);
00072   const char* TranslateErrorStateA(GB2DERROR);
00073 # ifdef UNICODE
00074 #   define TranslateErrorState TranslateErrorStateW 
00075 # else
00076 #   define TranslateErrorState TranslateErrorStateA
00077 # endif
00078 
00079   LPGB2D CreateGB2D(HWND);      //Creates a gb2DManager-object
00080   LPGB2D CreateGB2D(HWND hWnd, int resX, int resY, bool fullscreen=false);
00081   LPGB2D CreateGB2DExternal(HWND hWnd, LPDIRECT3DDEVICE9 dev, int resX, int resY, bool fullscreen);
00082 
00083 
00084   //CLASSES:
00085   class gb2DInterface {                     //Interface Class (abstract)
00086   protected:
00087       GB2DERROR errorstate;                 //global errorstate; check hres if this is != GB2D_OK
00088   public:
00089       gb2DInterface(): errorstate(GB2D_OK) {}
00090       virtual GB2DERROR GetErrorState()=0;  //Return and clear the current errorstate of the object
00091   };
00092 
00093 
00094   class gb2DFontList {  //TODO
00095       friend gb2DManager;
00096   private:
00097       std::vector<LPGBFONT> v;
00098       void Render();
00099       void AddFont(LPGBFONT font);
00100       void RemoveFont(LPGBFONT font);
00101   public:
00102       gb2DFontList();
00103       ~gb2DFontList();
00104   };
00105 
00106 
00107   class gb2DManager: public gb2DInterface {
00108   private:
00109     LPDIRECT3D9              lpD3D;             //D3D main object
00110     LPDIRECT3DDEVICE9        lpDev;             //D3D device
00111     LPDIRECT3DTEXTURE9       lpBuffer;          //Texture holding buffer
00112     LPD3DXSPRITE             lpBufferSprite;    //Sprite used for rendering buffer
00113     D3DLOCKED_RECT           rct;               //used for direct buffer manipulation
00114     D3DPRESENT_PARAMETERS    d3dpp;             //d3dpp
00115     HRESULT                  hres;              //global result handle, may be used for detailed debugging
00116     HWND                     hWnd;              //hWnd
00117     HINSTANCE                hInst;             //hInst (required for DInput)
00118     D3DCOLOR                 d3dClearColor;     //color used for framebuffer-clearing
00119     bool                     rendering;         //global rendering flag
00120     bool                     locked;            //flags if the framebuffer is locked for rendering
00121     int                      scrWidth;          //X-resolution of engine viewport
00122     int                      scrHeight;         //Y-resolution of engine viewport
00123     bool                     scrWindowed;       //indicates whether GB2D runs windowed or fullscreen
00124     gb2DFontList             fontList;          //stores all fonts created by the engine
00125     ::std::vector<LPGBTEXT>  textList;          //stores all texts created by the engine
00126     ::std::vector<LPGBUI>    uiList;            //stores all ui manager objects created by the engine
00127   public:
00128     gb2DManager(HWND hWnd, int resX, int resY, bool fullscreen);
00129                                 //Initializes Direct3D and Manager-objects
00130     gb2DManager(LPDIRECT3DDEVICE9 devive, HWND hWnd, int resX, int resY, bool fullscreen);
00131                                 //starts gb2D on top of an already initialized device
00132     ~gb2DManager();             //Releases all Direct3D-objects and deletes dynamic data
00133     GB2DERROR GetErrorState();  //Return the current errorstate of the manager
00134     HRESULT GetHRes();          //Return the global result handle
00135     LPGBFONT CreateConsoleFont(int fontSize, int fontBold, int fontItalic, const wchar_t* fontName);
00136     LPGBTEXT CreateText();
00137     LPGBUI CreateUIManager();
00138     void BeginScene();          //Open framebuffer for rendering
00139     void EndScene();            //Unlock framebuffer
00140     void Render();              //Opens D3D-device for drawing; sets global rendering flag
00141     void Flip();                //Renders framebuffer and fonts, closes the D3D-device and presents the backbuffer; clears global rendering flag
00142     void SetClearColor(unsigned char, unsigned char, unsigned char);
00143                                 //Select the Clear color used for Backbuffer clear
00144     void PutPixel(int, int, D3DCOLOR);
00145                                 //Set a single pixel in the framebuffer (slow!)
00146     bool IsRendering();         //Returns status of the global rendering flag
00147     bool IsLocked();            //Returns true if called between BeginScene() and EndScene()
00148     void ClrScr();              //Clear the framebuffer with black
00149     void Blt(DWORD* data, int width, int height, int posX, int posY);
00150                                 //Use for blitting 32 bit image data
00151     void Blt(LPGBSPRITE sprite);//Use for blitting Sprite
00152     void BltRotate(LPGBSPRITE sprite, double angle);
00153                                 //Use for blitting rotated Sprite
00154     void Blt24(DWORD* data, int width, int height, int posX, int posY);
00155                                 //Use for non-alpha blitting of 32 bit image data
00156     void Line(int startX, int startY, int endX, int endY, D3DCOLOR color);
00157                                 //Line-drawing (Bresenham)
00158     void HLine(int startX, int endX, int y, D3DCOLOR color);
00159                                 //Fast horizontal line drawing
00160     void HLineGradient(int startX, int endX, int y, D3DCOLOR c1, D3DCOLOR c2);
00161                                 //Draws a horizontal line with a coursed color gradient
00162     void Circle(int posX, int posY, int radius, D3DCOLOR color);
00163                                 //Circle drawing (Bresenham)
00164     void Rectangle(int left, int top, int right, int bottom, D3DCOLOR color);
00165                                 //Draws a filled rectangle
00166     void RectangleGradient(int left, int top, int right, int bottom, 
00167                            D3DCOLOR ul, D3DCOLOR ur, D3DCOLOR ll, D3DCOLOR lr);
00168                                 //Draws a filled rectangle with color gradient
00169     int  GetScreenWidth();
00170     int  GetScreenHeight();
00171     bool IsScreenFullscreen();
00172   };
00173 
00174 
00175   class gb2DSprite: public gb2DInterface {
00176       //todo: integrate this with the manager
00177   friend gb2DImageLoader; friend gb2DManager;
00178   public:
00179       enum {
00180           BLEND_DEACTIVATED,    //use no blending at all
00181           BLEND_USEKEY,         //use the specified color-key (only one color key can be active at a time)
00182           BLEND_USEALPHA        //use the image's alpha values for blending
00183       } blendingMode;                   //Blending mode - this is public so it can be set directly
00184   private:
00185       DWORD alphaKey;                   //used for color-keyed blending if blendingMode is set to BLEND_USEKEY
00186       DWORD* data;                      //pointer to the sprite's data
00187       short width, height;              //specifies the sprites dimensions
00188       int posX;                         //the sprite's position in screen-coordinates
00189       int posY;                         //the sprite's position in screen-coordinates
00190       double angle;                     //Rotation angle
00191   public:
00192       gb2DSprite(short Width, short Height);
00193       ~gb2DSprite();
00194       GB2DERROR GetErrorState();
00195       void StoreImageData(DWORD* pData);//fill the data structure; pData must be at least width*height!
00196       void SetPosition(int x, int y);   //specify position of the sprite in screen coordinates
00197       void SetColorKey(D3DCOLOR key);   //set a color key and activate color-keyed-blending
00198       void SetRotation(double angle);   //set the rotation angle
00199       double GetRotation();             //return the current angle
00200       void ApplyColorKey(D3DCOLOR key); //Apply a color key to the image data itself and activate alpha-blending (required for multiple color keys)
00201       D3DCOLOR GetColorKey();           //returns the most recently set color key
00202   };
00203 
00204 
00205   class gb2DImageLoader: public gb2DInterface {
00206   public:
00207       enum GB2D_IMAGETYPE {
00208           NONE, TGA, BMP, RAW, PCX, PNG, PPM, TIFF /*todo: autodetect*/
00209       };
00210   private:
00211       unsigned char* data;      //pointer to image data
00212       D3DCOLOR* palette;        //pointer to color palette
00213       unsigned int file_offset; //offset of file pointer (if multiple images are stored in the same file)
00214       int width;
00215       int height;
00216       int bpp;
00217       bool data_is_valid;       //flag for internal use; indicates whether the other member variables are sane
00218       GB2D_IMAGETYPE type;
00219   public:
00220       gb2DImageLoader(const char* fname, GB2D_IMAGETYPE img_type);
00221       gb2DImageLoader();
00222       ~gb2DImageLoader();
00223       GB2DERROR GetErrorState();    //Return the current errorstate
00224       void LoadFile(const char* fname, GB2D_IMAGETYPE img_type);
00225                                     //clears existing image data and reads a new file
00226       int GetWidth();
00227       int GetHeight();
00228       int GetBpp();
00229       void GetImageData(DWORD* pData);
00230                                     //copy image data to pData (pData must be at least width*height*4 bytes!)
00231       void GetRawData(DWORD* pRaw); //copy raw image data (pRaw must be at least width*height*4 bytes!)
00232       void GetPaletteData(DWORD* pPal);
00233                                     //copy palette data (pPal must be at least 2^Bpp bytes)
00234       LPGBSPRITE CreateSprite();    //Build a new sprite object and copy image data
00235   };
00236 
00237 
00238   class gb2DFont: public gb2DInterface {
00239     friend gb2DFontList;
00240   private:
00241       LPD3DXFONT lpFont;                //Font-object
00242       LPD3DXSPRITE lpBuffer;            //pointer to gb2DManager's buffer-sprite
00243       wchar_t* cBuffer;                 //character-buffer
00244       int iBufferSize;                  //size of cBuffer in characters
00245       int iBufferOffset;                //currently occupied characters in buffer
00246       RECT rct;                         //rct structure determining position and size of text-field
00247       D3DCOLOR color;                   //text-color
00248       HRESULT hres;                     //global result handle, may be used for additional debugging
00249       void Render();                    //Render()-method calles by gb2DFontList (implicitly clears buffer)
00250   public:
00251       gb2DFont(LPDIRECT3DDEVICE9 lpDev, LPD3DXSPRITE lpSpriteBuffer, int fontSize, int fontBold, int fontItalic, const wchar_t* fontName);
00252       ~gb2DFont();
00253       GB2DERROR GetErrorState();
00254       void SetPosition(int x, int y);
00255       void SetColor(D3DCOLOR d3dColor);
00256       void SetSize(int width, int height);
00257       void ClrScr();                    //explicit buffer clear
00258       void print(const wchar_t* str, ...);
00259                                         //append characters to buffer
00260   };
00261 
00262 
00263 #  ifdef GB2D__USE_GBTEXT
00264       class gb2DText: public gb2DInterface {
00265                                             //renders text on D3DTextures
00266           friend gb2DManager;
00267       private:
00268           int       res_x;                  //texture resolution
00269           int       res_y;                  //texture resolution
00270           wchar_t*  cbuffer;                //character buffer
00271           int       cursor_pos_x;           //cursor position
00272           int       cursor_pos_y;           //cursor position
00273           int       rows;                   //number of character rows
00274           int       chars_per_row;          //number of character columns
00275           int       font_size_x;            //font width
00276           int       font_size_y;            //font height
00277           D3DCOLOR* colorbuffer;            //buffer for color values
00278           D3DCOLOR  color;                  //cursor color
00279           D3DCOLOR  bgcolor;                //buffer background color
00280           D3DCOLOR  fontbgcolor;            //cursor background color
00281           LPDIRECT3DTEXTURE9 texture;       //texture that is rendered on
00282           void*     sprite;                 //reserved pointer (used by gb2DManager)
00283           void RenderSingleChar(wchar_t const& c, int const& x, int const& y, D3DCOLOR const& color, D3DLOCKED_RECT const& target);
00284                                             //helper function: renders a single character
00285           void FillSingleChar(int const& x, int const& y, D3DLOCKED_RECT const& target);
00286                                             //helper function: fill a character-field with bgcolor
00287       public:
00288           gb2DText(int resolution_x, int resolution_y, LPDIRECT3DTEXTURE9 lpTex);
00289           ~gb2DText();
00290           void SetColor(D3DCOLOR d3dcolor); //changes cursor color (print()-time)
00291           void SetBGColor(D3DCOLOR d3dcolor);
00292                                             //changes background color (render()-time)
00293           void SetFontBGColor(D3DCOLOR d3dcolor);
00294                                             //changes font background color (render()-time)
00295           void SetPosition(int x, int y);   //changes cursor position
00296           int GetPositionX() const;         //get current cursor position in X
00297           int GetPositionY() const;         //get current cursor position in Y
00298           void ClrScr();                    //clears char and colorbuffer; resets cursor
00299           void print(const wchar_t* str, ...);
00300                                             //write text to the buffer
00301           void Render();                    //render texture
00302           void RenderExternalBuffer(wchar_t* ext_buffer, D3DCOLOR* ext_color, int size);
00303                                             //render texture based on external data
00304           void DrawWindow(int x, int y, int width, int height);
00305           GB2DERROR GetErrorState();
00306       };
00307 #  endif
00308 
00309 #  ifdef GB2D__USE_GBUI
00310 #     include "../src/gbUI/gbUI.hpp"
00311 
00312       class gbUIManager: public gb2DInterface {
00313       private:
00314           LPDIRECT3DDEVICE9        lpDev;               //D3D device
00315           HWND                     hWnd;
00316           struct {
00317               int pos_x, pos_y;
00318               bool lbutton, rbutton, mbutton;
00319               int ldownx, ldowny;
00320               int lupx, lupy;
00321           } mouse;
00322           LPGBUIFRAME root;
00323           LPGBUIFRAME ldrag_src;
00324           LPGBUIFRAME ldrag_dst;
00325           void MouseMoved();
00326           void LMouseDown();
00327           void LMouseUp();
00328           void KeyPressed();
00329       public:
00330           gbUIManager(LPDIRECT3DDEVICE9 device, gb2DManager* gb2D, HWND hwnd);
00331           ~gbUIManager();
00332           LRESULT MessageCallback(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
00333           void Render();
00334           GB2DERROR GetErrorState();
00335           LPGBUIFRAME GetRootFrame();
00336           int GetMouseX();
00337           int GetMouseY();
00338       };
00339 #  endif
00340 #endif

Generated on Sat Jan 19 18:58:21 2008 for PS2 IconSys Library by  doxygen 1.5.4