Renderer.h

00001 // El config file se lee en el metodo Startup de esta clase
00002 
00003 #ifndef _Renderer_H_
00004 #define _Renderer_H_
00005 
00006 #include "dxtypes.h"
00007 
00008 #ifndef USING_DIRECTX81
00009 
00010 #pragma comment(lib, "d3d9.lib")
00011 #pragma comment(lib, "d3dx9.lib")
00012 #pragma comment(lib, "dxguid.lib")
00013 
00014 #else
00015 
00016 #pragma comment(lib, "d3d8.lib")
00017 #pragma comment(lib, "d3dx8.lib")
00018 #pragma comment(lib, "dxguid.lib")
00019 
00020 #endif
00021 
00022 #include <vector>
00023 
00024 #include "Defines.h"
00025 #include "MathUtil.h"
00026 #include "Window.h"
00027 #include "ConfigFile.h"
00028 #include "FileLog.h"
00029 #include "VertexBufferManager.h"
00030 
00031 using namespace std;
00032 
00033 #define D3DFVF_ZAKVERTEX    (D3DFVF_XYZ|D3DFVF_DIFFUSE)
00034 #define D3DFVF_ZAKTEXVERTEX (D3DFVF_XYZ|D3DFVF_TEX1|D3DFVF_TEXCOORDSIZE2(0))
00035 
00036 #define ZAK_TEXT_BOTTOM          DT_BOTTOM
00037 #define ZAK_TEXT_CALCRECT        DT_CALCRECT
00038 #define ZAK_TEXT_CENTER          DT_CENTER
00039 #define ZAK_TEXT_EXPANDTABS      DT_EXPANDTABS
00040 #define ZAK_TEXT_LEFT            DT_LEFT
00041 #define ZAK_TEXT_NOCLIP          DT_NOCLIP
00042 #define ZAK_TEXT_RIGHT           DT_RIGHT
00043 #define ZAK_TEXT_RTLREADING      DT_RTLREADING
00044 #define ZAK_TEXT_SINGLELINE  DT_SINGLELINE
00045 #define ZAK_TEXT_TOP             DT_TOP
00046 #define ZAK_TEXT_VCENTER         DT_VCENTER
00047 #define ZAK_TEXT_WORDBREAK       DT_WORDBREAK
00048 
00049 namespace zak {
00050 
00051 struct Formats
00052 {
00053         char szFormat[32];
00054         D3DDEVTYPE d3dType;
00055         D3DFORMAT d3dFormat;
00056         BOOL bWindowed;
00057 };
00058 
00059 class TextureInfo{
00060         public:
00061                 char * szFileName;
00062                 char * pData;
00063                 unsigned int texColorKey;
00064                 int iMipLevels;
00065                 unsigned int uiTexWidth;
00066                 unsigned int uiTexHeight;
00067                 unsigned int size;
00068                 bool persist;
00069 
00070         TextureInfo()
00071         {
00072                 pData = NULL;
00073                 szFileName = NULL;
00074                 texColorKey = 0;
00075                 iMipLevels = 0;
00076                 uiTexWidth = 0;
00077                 uiTexHeight = 0;
00078                 size = 0;
00079                 persist = false;
00080         }
00081 };
00082 
00083 enum RenderStates {
00084         ZAK_RENDER_STATE_BLEND = 0,
00085         ZAK_RENDER_STATE_BLENDFUNCBRIGHT
00086 };
00087 
00088 
00089 class ZAKENGINE_API TexVertex {
00090         private:
00091                 float _fX,_fY,_fZ;
00092                 float _fU, _fV;
00093 
00094         public:
00095                 void SetVal(float fX, float fY, float fZ, float fU, float fV);
00096 
00097                 float GetValX() {
00098                         return _fX;
00099                 }
00100 
00101                 float GetValY() {
00102                         return _fY;
00103                 }
00104 
00105         TexVertex();
00106 };
00107 
00108 class ZAKENGINE_API ColorVertex {
00109 
00110         private:
00111                 float _fX,_fY,_fZ;
00112                 DWORD _dwColor;
00113 
00114         public:
00115                 void SetVal(float fX, float fY, float fZ, DWORD dwColor);
00116 
00117                 float GetValX() {
00118                         return _fX;
00119                 }
00120 
00121                 float GetValY() {
00122                         return _fY;
00123                 }
00124 
00125         ColorVertex();
00126 };
00127 
00128 enum DRAWPRIMITIVES
00129 {
00130         ZAK_PRI_POINTLIST=0,
00131         ZAK_PRI_LINELIST,
00132         ZAK_PRI_LINESTRIP,
00133         ZAK_PRI_TRIANGLELIST,
00134         ZAK_PRI_TRIANGLESTRIP,
00135         ZAK_PRI_TRIANGLEFAN
00136 };
00137 
00138 enum MATRIX_MODES {
00139         ZAK_WORLD_MATRIX=0,
00140         ZAK_VIEW_MATRIX,
00141         ZAK_PROJECTION_MATRIX,
00142         ZAK_TEXTURE_MATRIX
00143 };
00144 
00145 enum ZAK_FONT_TYPES {
00146         FT_ARIAL = 0,
00147         FT_COURIER,
00148         FT_TIMES,
00149         FT_VERDANA,
00150         FT_TAHOMA
00151 };
00152 
00153 class ENTITY;
00154 
00159 class ZAKENGINE_API Renderer {
00160         public:
00161 
00162                 void BindTexture(DX_LPTEXTURE pBitmapTexture);
00163 
00164                 DX_LPTEXTURE LoadTexture(TextureInfo * pTexInfo, DWORD filter=2, DWORD mipFilter=2);
00165                 int GetViewPortHeight();
00166                 int GetViewPortWidth();
00167 
00172                 int      GetBits() {
00173                         return _iBits;
00174                 }
00175 
00180                 bool GetFullscreen() { return _fullScreen; }
00181 
00185                 void LoadIdentity();
00186 
00192                 void Scale(float fX, float fY);
00193 
00200                 void Translate(float fX, float fY, float fZ);
00201 
00206                 void RotationZ(float fAngle);
00207 
00211                 void PushMatrix();
00212 
00216                 void PopMatrix();
00217 
00222                 void SetMatrixMode(int matType) {
00223                         _CurrentMatrixMode=(MATRIX_MODES)matType;
00224                 }
00225 
00230                 bool SetFont(int ft, int size=15);
00231 
00236                 void SetFontColor(unsigned int color) { _color = color; }
00237 
00242                 int GetFont();
00243 
00281                 void DrawString(wstring &text, float x, float y, float w, float h, unsigned int align=ZAK_TEXT_LEFT);
00282 
00320                 void DrawString(string &text, float x, float y, float w, float h, unsigned int align=ZAK_TEXT_LEFT);
00321 
00359                 void DrawString(char* text, float x, float y, float w, float h, unsigned int align=ZAK_TEXT_LEFT);
00360 
00361 
00374                 void EnableModulate() {
00375                         _pD3DDevice->SetTextureStageState(0,D3DTSS_COLOROP,D3DTOP_MODULATE);
00376                         _pD3DDevice->SetTextureStageState(0,D3DTSS_COLORARG1,D3DTA_TEXTURE);
00377                         _pD3DDevice->SetTextureStageState(0,D3DTSS_COLORARG2,D3DTA_TFACTOR);
00378 
00379                         _pD3DDevice->SetTextureStageState(0,D3DTSS_ALPHAOP,D3DTOP_MODULATE);
00380                         _pD3DDevice->SetTextureStageState(0,D3DTSS_ALPHAARG1,D3DTA_TEXTURE);
00381                         _pD3DDevice->SetTextureStageState(0,D3DTSS_ALPHAARG2,D3DTA_TFACTOR);
00382                 }
00383 
00396                 void SetModulationColor(DWORD dwColor) {
00397                         _pD3DDevice->SetRenderState(D3DRS_TEXTUREFACTOR, dwColor);
00398                 }
00399 
00412                 void DisableModulate() {
00413                         _pD3DDevice->SetTextureStageState(0,D3DTSS_COLOROP,D3DTOP_MODULATE);
00414                         _pD3DDevice->SetTextureStageState(0,D3DTSS_COLORARG1,D3DTA_TEXTURE);
00415                         _pD3DDevice->SetTextureStageState(0,D3DTSS_COLORARG2,D3DTA_CURRENT);
00416 
00417                         _pD3DDevice->SetTextureStageState(0,D3DTSS_ALPHAOP,D3DTOP_SELECTARG1);
00418                         _pD3DDevice->SetTextureStageState(0,D3DTSS_ALPHAARG1,D3DTA_TEXTURE);
00419                         _pD3DDevice->SetTextureStageState(0,D3DTSS_ALPHAARG2,D3DTA_CURRENT);
00420                 }
00421 
00422                 void SetMinFilter(unsigned int stage, DWORD filter)
00423                 {
00424                         #ifdef USING_DIRECTX81
00425                                 _pD3DDevice->SetTextureStageState(stage, D3DTSS_MINFILTER, filter);
00426                         #else 
00427                                 _pD3DDevice->SetSamplerState(stage, D3DSAMP_MINFILTER, filter);
00428                         #endif
00429                 }
00430                 
00431                 void SetMagFilter(unsigned int stage, DWORD filter)
00432                 {
00433                         #ifdef USING_DIRECTX81
00434                                 _pD3DDevice->SetTextureStageState(stage, D3DTSS_MAGFILTER, filter);
00435                         #else 
00436                                 _pD3DDevice->SetSamplerState(stage, D3DSAMP_MAGFILTER, filter);
00437                         #endif
00438                 }
00439 
00440 
00446                 void SetViewPosition(float fPosX, float fPosY) {
00447                         _fViewPosX = fPosX;
00448                         _fViewPosY = fPosY;
00449 
00450                         D3DXVECTOR3 d3dvEyePos   (MathUtil::Round(fPosX), MathUtil::Round(fPosY), -5.0f);
00451                         D3DXVECTOR3 d3dvLookPos  (MathUtil::Round(fPosX), MathUtil::Round(fPosY),  0.0f);
00452                         D3DXVECTOR3 d3dvUpVector (0.0f,  1.0f,   0.0f);
00453 
00454                         SetMatrixMode(ZAK_VIEW_MATRIX);
00455 
00456                         D3DXMatrixLookAtLH(&_theMatrix,&d3dvEyePos,&d3dvLookPos, &d3dvUpVector);
00457 
00458                         _pD3DDevice->SetTransform(D3DTS_VIEW,&_theMatrix);
00459                 }
00460 
00466                 void GetViewPosition(float &fPosX, float &fPosY) {
00467                         fPosX = _fViewPosX;
00468                         fPosY = _fViewPosY;
00469                 }
00470 
00471 #ifndef USING_DIRECTX81
00472 
00475                 void EnableScissor() {
00476                         _pD3DDevice->SetRenderState(D3DRS_SCISSORTESTENABLE, TRUE);
00477                 }
00478 
00482                 void DisableScissor() {
00483                         _pD3DDevice->SetRenderState(D3DRS_SCISSORTESTENABLE, FALSE);
00484                 }
00485 
00492                 bool SetScissor(CONST RECT &rect) {
00493                         if (_pD3DDevice->SetScissorRect(&rect) != D3D_OK)
00494                                 return false;
00495 
00496                         return true;
00497                 }
00498 #endif 
00499                 void GetWorldRect(RECT &Rect) {
00500                         D3DXMATRIX mProj;
00501                         _pD3DDevice->GetTransform(D3DTS_VIEW, &_theMatrix);
00502                         _pD3DDevice->GetTransform(D3DTS_PROJECTION, &mProj);
00503 
00504                         float fx = _theMatrix(3,0);
00505                         float fy = _theMatrix(3,1);
00506 
00507                         Rect.left   = (LONG) (-fx - 1/mProj(0,0));
00508                         Rect.right  = (LONG) (-fx + 1/mProj(0,0));
00509                         Rect.top    = (LONG) (-fy + 1/mProj(1,1));
00510                         Rect.bottom = (LONG) (-fy - 1/mProj(1,1));
00511                 }
00512 
00517                 void EnableState(int eRenderState) {
00518                         if ((RenderStates)eRenderState == ZAK_RENDER_STATE_BLEND) {
00519                                 _pD3DDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
00520                         } else if ((RenderStates)eRenderState == ZAK_RENDER_STATE_BLENDFUNCBRIGHT) {
00521                                 _pD3DDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);
00522                         }
00523                 }
00524 
00529                 void DisableState(int eRenderState) {
00530                         if ((RenderStates)eRenderState == ZAK_RENDER_STATE_BLEND) {
00531                                 _pD3DDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, false);
00532                         } else if ((RenderStates)eRenderState == ZAK_RENDER_STATE_BLENDFUNCBRIGHT) {
00533 //                              _pD3DDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
00534                                 _pD3DDevice->SetRenderState(D3DRS_SRCBLEND,D3DBLEND_SRCALPHA);
00535                                 _pD3DDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
00536 
00537                         }
00538                 }
00539 
00546                 void Draw(ColorVertex * VertexCollection, DRAWPRIMITIVES ePrim, unsigned int uiVertexCount);
00547 
00554                 void Draw(TexVertex * vertexCollection, DRAWPRIMITIVES prim, unsigned int uiVertexCount);
00555 
00556                 void DrawCircle(float x, float y, float radius, DWORD color);
00557                 void DrawLine(float x1, float y1, DWORD c1, float x2, float y2, DWORD c2);
00558 
00568                 bool Startup (unsigned int iAncho, unsigned int iAlto, int iBits, bool bFullScreen, int refreshRate, bool vsync);
00569 
00573                 void ShutDown();
00574 
00578                 void BeginScene() {
00579                         HRESULT hr = _pD3DDevice->BeginScene();
00580                 }
00581 
00585                 void EndEscene() {
00586                         _pD3DDevice->EndScene();
00587                 }
00588 
00592                 void ClearTextures();
00593 
00597                 void SetClearColor(D3DCOLOR ClearColor);
00598 
00602                 void Clear() {
00603                         _pD3DDevice->Clear(0,NULL,D3DCLEAR_TARGET, _ClearColor,1.0f,0);
00604                 }
00605 
00609                 void Present(void) {
00610                         if (_pD3DDevice->Present(NULL,NULL,NULL,NULL) != D3D_OK) {
00611                                 _deviceLost = true;
00612                         }
00613                 }
00614 
00618                 bool ChangeResolution(unsigned int uiAncho, unsigned int uiAlto, int iBits, bool bFullScreen, int refreshRate, bool vsync);
00619 
00620                 DX_LPDIRECT3DDEVICE GetDevice() { return _pD3DDevice; }
00621 
00622                 void Register(ENTITY* entity) { _entities.push_back(entity); }
00623                 void Unregister(ENTITY* entity);
00624 
00625                 bool RegisterScript();
00626 
00627         Renderer();
00628         ~Renderer();
00629 
00630         private:
00631                 friend class Game;
00632 
00633                 ColorVertex             _VertexColor[17];
00634 
00635                 ID3DXFont*              _pDefDXFont;
00636                 ZAK_FONT_TYPES  _pDefDXFontType;
00637                 D3DCOLOR                _color;
00638 
00639                 vector<Formats*> _validFormats;
00640 
00641                 vector<ENTITY*> _entities;
00642 
00643                 int             _width;
00644                 int             _height;
00645                 int             _bits;
00646                 bool    _fullScreen;
00647                 int             _refreshRate;
00648                 bool    _vsync;
00649 
00650                 D3DPRESENT_PARAMETERS   D3DPresentParameter;
00651                 bool                                    _deviceLost;
00652                 //LPDIRECT3DSURFACE9            _BackSurface;
00653 
00654                 D3DCOLOR                                _ClearColor;
00655                 float                                   _fViewPosX;
00656                 float                                   _fViewPosY;
00657 
00658                 int                                             _iBits;
00659 
00660                 D3DFORMAT                               _d3DFormat;
00661 
00662                 DX_VIEWPORT                             vp;//objeto tipo viewport
00663 
00664                 DX_LPDIRECT3D                   _pD3D;
00665                 DX_LPDIRECT3DDEVICE             _pD3DDevice;
00666                 DX_LPVERTEXBUFFER               _pD3DVertexBuffer;
00667 
00668 
00669                 MATRIX_MODES                    _CurrentMatrixMode;
00670 
00671                 D3DXMATRIX _theMatrix;
00672 
00673                 //Pila de matrices
00674                 LPD3DXMATRIXSTACK               _pWorldMatrixStack;
00675                 LPD3DXMATRIXSTACK               _pProjectionMatrixStack;
00676                 LPD3DXMATRIXSTACK               _pViewMatrixStack;
00677                 LPD3DXMATRIXSTACK               _pTextureMatrixStack;
00678 
00679                 D3DPRIMITIVETYPE                _PrimitiveVector[5];
00680 
00681                 ConfigFile                      *_ConfigFile;
00682 
00683                 VertexBufferManager<ColorVertex,D3DFVF_ZAKVERTEX>               _VertexBufferManager;
00684                 VertexBufferManager<TexVertex,D3DFVF_ZAKTEXVERTEX>      _VertexBufferTexManager;
00685 
00686                 DX_LPTEXTURE                    _pCurrentBitmapTexture;
00687 
00688                 bool IniciarDX          (unsigned int uiAncho, unsigned int uiAlto, int iBits, bool bFullScreen, int refreshRate, bool vsync);
00689                 void ApagarDX           ();
00690                 bool SetupDX            ();
00691                 void CheckModes();
00692 
00697                 bool IsDeviceLost() {
00698                         return _deviceLost;
00699                 }
00700 
00705                 bool CheckForLostDevice();
00706 
00711                 bool Reset() {
00712                         if (_pD3DDevice->Reset(&D3DPresentParameter) != D3D_OK)
00713                                 return false;
00714 
00715                         return true;
00716                 }
00717 
00718                 void DrawStr(string &text, float x, float y, float w, float h, unsigned int align=ZAK_TEXT_LEFT) { DrawString(text, x, y, w, h, align); }
00719 };
00720 
00721 extern ZAKENGINE_API Renderer g_renderer;
00722 
00723 }
00724 
00725 #endif //_Renderer_H_

Generado el Tue May 29 14:46:15 2007 para Zak Engine v1.1.0 por  doxygen 1.5.1-p1