00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
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
00029 class gb2DManager;
00030 class gb2DSprite;
00031 class gb2DImageLoader;
00032 class gb2DFont;
00033 typedef gb2DManager *LPGB2D;
00034 typedef gb2DSprite *LPGBSPRITE;
00035 typedef gb2DFont *LPGBFONT;
00036 # ifdef GB2D__USE_GBTEXT
00037 class gb2DText;
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 {
00051 GB2D_OK,
00052 GB2D_FAILED,
00053 GB2D_OUTOFMEMORY,
00054 GB2D_NOTIMPLEMENTED,
00055 GB2D_D3DCREATEFAILED,
00056 GB2D_D3DCREATEDEVICEFAILED,
00057 GB2D_D3DCREATETEXTUREFAILED,
00058 GB2D_D3DCREATESPRITEFAILED,
00059 GB2D_D3DCREATEFONTFAILED,
00060 GB2D_RENDEROUTOFBOUNDARIES,
00061 GB2D_FILEOPENFAILED,
00062 GB2D_FILEREADERROR,
00063 GB2D_DEVICEACCESSFAILED,
00064 GB2D_ENUMERATIONFAILED,
00065 GB2D_ILLEGALPARAMETER,
00066 GB2D_NOTACTIVATED,
00067 };
00068
00069
00070
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);
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
00085 class gb2DInterface {
00086 protected:
00087 GB2DERROR errorstate;
00088 public:
00089 gb2DInterface(): errorstate(GB2D_OK) {}
00090 virtual GB2DERROR GetErrorState()=0;
00091 };
00092
00093
00094 class gb2DFontList {
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;
00110 LPDIRECT3DDEVICE9 lpDev;
00111 LPDIRECT3DTEXTURE9 lpBuffer;
00112 LPD3DXSPRITE lpBufferSprite;
00113 D3DLOCKED_RECT rct;
00114 D3DPRESENT_PARAMETERS d3dpp;
00115 HRESULT hres;
00116 HWND hWnd;
00117 HINSTANCE hInst;
00118 D3DCOLOR d3dClearColor;
00119 bool rendering;
00120 bool locked;
00121 int scrWidth;
00122 int scrHeight;
00123 bool scrWindowed;
00124 gb2DFontList fontList;
00125 ::std::vector<LPGBTEXT> textList;
00126 ::std::vector<LPGBUI> uiList;
00127 public:
00128 gb2DManager(HWND hWnd, int resX, int resY, bool fullscreen);
00129
00130 gb2DManager(LPDIRECT3DDEVICE9 devive, HWND hWnd, int resX, int resY, bool fullscreen);
00131
00132 ~gb2DManager();
00133 GB2DERROR GetErrorState();
00134 HRESULT GetHRes();
00135 LPGBFONT CreateConsoleFont(int fontSize, int fontBold, int fontItalic, const wchar_t* fontName);
00136 LPGBTEXT CreateText();
00137 LPGBUI CreateUIManager();
00138 void BeginScene();
00139 void EndScene();
00140 void Render();
00141 void Flip();
00142 void SetClearColor(unsigned char, unsigned char, unsigned char);
00143
00144 void PutPixel(int, int, D3DCOLOR);
00145
00146 bool IsRendering();
00147 bool IsLocked();
00148 void ClrScr();
00149 void Blt(DWORD* data, int width, int height, int posX, int posY);
00150
00151 void Blt(LPGBSPRITE sprite);
00152 void BltRotate(LPGBSPRITE sprite, double angle);
00153
00154 void Blt24(DWORD* data, int width, int height, int posX, int posY);
00155
00156 void Line(int startX, int startY, int endX, int endY, D3DCOLOR color);
00157
00158 void HLine(int startX, int endX, int y, D3DCOLOR color);
00159
00160 void HLineGradient(int startX, int endX, int y, D3DCOLOR c1, D3DCOLOR c2);
00161
00162 void Circle(int posX, int posY, int radius, D3DCOLOR color);
00163
00164 void Rectangle(int left, int top, int right, int bottom, D3DCOLOR color);
00165
00166 void RectangleGradient(int left, int top, int right, int bottom,
00167 D3DCOLOR ul, D3DCOLOR ur, D3DCOLOR ll, D3DCOLOR lr);
00168
00169 int GetScreenWidth();
00170 int GetScreenHeight();
00171 bool IsScreenFullscreen();
00172 };
00173
00174
00175 class gb2DSprite: public gb2DInterface {
00176
00177 friend gb2DImageLoader; friend gb2DManager;
00178 public:
00179 enum {
00180 BLEND_DEACTIVATED,
00181 BLEND_USEKEY,
00182 BLEND_USEALPHA
00183 } blendingMode;
00184 private:
00185 DWORD alphaKey;
00186 DWORD* data;
00187 short width, height;
00188 int posX;
00189 int posY;
00190 double angle;
00191 public:
00192 gb2DSprite(short Width, short Height);
00193 ~gb2DSprite();
00194 GB2DERROR GetErrorState();
00195 void StoreImageData(DWORD* pData);
00196 void SetPosition(int x, int y);
00197 void SetColorKey(D3DCOLOR key);
00198 void SetRotation(double angle);
00199 double GetRotation();
00200 void ApplyColorKey(D3DCOLOR key);
00201 D3DCOLOR GetColorKey();
00202 };
00203
00204
00205 class gb2DImageLoader: public gb2DInterface {
00206 public:
00207 enum GB2D_IMAGETYPE {
00208 NONE, TGA, BMP, RAW, PCX, PNG, PPM, TIFF
00209 };
00210 private:
00211 unsigned char* data;
00212 D3DCOLOR* palette;
00213 unsigned int file_offset;
00214 int width;
00215 int height;
00216 int bpp;
00217 bool data_is_valid;
00218 GB2D_IMAGETYPE type;
00219 public:
00220 gb2DImageLoader(const char* fname, GB2D_IMAGETYPE img_type);
00221 gb2DImageLoader();
00222 ~gb2DImageLoader();
00223 GB2DERROR GetErrorState();
00224 void LoadFile(const char* fname, GB2D_IMAGETYPE img_type);
00225
00226 int GetWidth();
00227 int GetHeight();
00228 int GetBpp();
00229 void GetImageData(DWORD* pData);
00230
00231 void GetRawData(DWORD* pRaw);
00232 void GetPaletteData(DWORD* pPal);
00233
00234 LPGBSPRITE CreateSprite();
00235 };
00236
00237
00238 class gb2DFont: public gb2DInterface {
00239 friend gb2DFontList;
00240 private:
00241 LPD3DXFONT lpFont;
00242 LPD3DXSPRITE lpBuffer;
00243 wchar_t* cBuffer;
00244 int iBufferSize;
00245 int iBufferOffset;
00246 RECT rct;
00247 D3DCOLOR color;
00248 HRESULT hres;
00249 void Render();
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();
00258 void print(const wchar_t* str, ...);
00259
00260 };
00261
00262
00263 # ifdef GB2D__USE_GBTEXT
00264 class gb2DText: public gb2DInterface {
00265
00266 friend gb2DManager;
00267 private:
00268 int res_x;
00269 int res_y;
00270 wchar_t* cbuffer;
00271 int cursor_pos_x;
00272 int cursor_pos_y;
00273 int rows;
00274 int chars_per_row;
00275 int font_size_x;
00276 int font_size_y;
00277 D3DCOLOR* colorbuffer;
00278 D3DCOLOR color;
00279 D3DCOLOR bgcolor;
00280 D3DCOLOR fontbgcolor;
00281 LPDIRECT3DTEXTURE9 texture;
00282 void* sprite;
00283 void RenderSingleChar(wchar_t const& c, int const& x, int const& y, D3DCOLOR const& color, D3DLOCKED_RECT const& target);
00284
00285 void FillSingleChar(int const& x, int const& y, D3DLOCKED_RECT const& target);
00286
00287 public:
00288 gb2DText(int resolution_x, int resolution_y, LPDIRECT3DTEXTURE9 lpTex);
00289 ~gb2DText();
00290 void SetColor(D3DCOLOR d3dcolor);
00291 void SetBGColor(D3DCOLOR d3dcolor);
00292
00293 void SetFontBGColor(D3DCOLOR d3dcolor);
00294
00295 void SetPosition(int x, int y);
00296 int GetPositionX() const;
00297 int GetPositionY() const;
00298 void ClrScr();
00299 void print(const wchar_t* str, ...);
00300
00301 void Render();
00302 void RenderExternalBuffer(wchar_t* ext_buffer, D3DCOLOR* ext_color, int size);
00303
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;
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