Advertisement
keybode

Renderer.cpp

May 21st, 2014
377
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.28 KB | None | 0 0
  1. #include "Renderer.h"
  2. #include "FontManager.h"
  3.  
  4. CRenderer gRenderer;
  5.  
  6. CRenderer::CRenderer ( void )
  7. {
  8.     m_pSprite = 0;
  9.     m_pStateBlock = 0;
  10.     m_pDevice = 0;
  11.     m_dwFVF = 0;
  12. }
  13.  
  14. CRenderer::~CRenderer ( void )
  15. {
  16.  
  17. }
  18.  
  19. void CRenderer::Update ( IDirect3DDevice9* pDevice )
  20. {
  21.     if ( !m_pDevice )
  22.     {
  23.         m_pDevice = pDevice;
  24.     }
  25.  
  26.     if ( !m_pStateBlock )
  27.     {
  28.         m_pDevice->CreateStateBlock ( D3DSBT_ALL, &m_pStateBlock );
  29.     }
  30.  
  31.     if ( !m_pSprite )
  32.     {
  33.         D3DXCreateSprite ( m_pDevice, &m_pSprite );
  34.     }
  35. }
  36.  
  37. void CRenderer::PreFrame ( void )
  38. {
  39.     m_pStateBlock->Capture ();
  40.  
  41.     m_pSprite->Begin ( D3DXSPRITE_ALPHABLEND );
  42.  
  43.     m_pDevice->GetFVF ( &m_dwFVF );
  44.     m_pDevice->SetFVF ( D3DFVF_XYZRHW | D3DFVF_DIFFUSE | D3DFVF_TEX1 );
  45.  
  46.     m_pDevice->SetTexture ( 0, 0 );
  47. }
  48.  
  49. void CRenderer::PostFrame ( void )
  50. {
  51.     m_pDevice->SetFVF ( m_dwFVF );
  52.    
  53.     m_pSprite->End ();
  54.  
  55.     m_pStateBlock->Apply ();
  56. }
  57.  
  58. void CRenderer::Release ( void )
  59. {
  60.     if ( m_pSprite )
  61.     {
  62.         m_pSprite->OnLostDevice ();
  63.         m_pSprite->OnResetDevice ();
  64.  
  65.         m_pSprite = 0;
  66.     }
  67.  
  68.     if ( m_pStateBlock )
  69.     {
  70.         m_pStateBlock->Release ();
  71.  
  72.         m_pStateBlock = 0;
  73.     }
  74. }
  75.  
  76. void CRenderer::SetDevice ( IDirect3DDevice9* pDevice )
  77. {
  78.     m_pDevice = pDevice;
  79. }
  80.  
  81. IDirect3DDevice9* CRenderer::GetDevice ( void )
  82. {
  83.     return m_pDevice;
  84. }
  85.  
  86. struct D3DTLVERTEX
  87. {
  88.     float x, y, z, rhw;
  89.     DWORD color;
  90. };
  91.  
  92. void CRenderer::Rect ( int x, int y, int w, int h, Color color )
  93. {
  94.     if ( !IsReady () )
  95.         return;
  96.  
  97.     D3DTLVERTEX vertex[ 4 ] =
  98.     {
  99.         { x, y + h, 0, 1, color.GetCode () },
  100.         { x, y, 0, 1, color.GetCode () },
  101.         { x + w, y + h, 0, 1, color.GetCode () },
  102.         { x + w, y, 0, 1, color.GetCode () }
  103.     };
  104.  
  105.     m_pDevice->SetTexture ( 0, 0 );
  106.     m_pDevice->DrawPrimitiveUP ( D3DPT_TRIANGLESTRIP, 2, vertex, sizeof(D3DTLVERTEX) );
  107. }
  108.  
  109. void CRenderer::RectOutlined ( int x, int y, int w, int h, Color color, Color outlined, int t )
  110. {
  111.     if ( !IsReady () )
  112.         return;
  113.  
  114.     Rect ( x, y, w, h, color );
  115.     BorderBox ( x - 1, y - 1, w + 1, h + 1, outlined, t );
  116. }
  117.  
  118. void CRenderer::BorderBox ( int x, int y, int w, int h, Color color, int t )
  119. {
  120.     if ( !IsReady () )
  121.         return;
  122.  
  123.     Rect ( x, y, w, t, color );
  124.     Rect ( x, y, t, h, color );
  125.     Rect ( x + w, y, t, h, color );
  126.     Rect ( x, y + h, w + t, t, color );
  127. }
  128.  
  129. void CRenderer::BorderBoxOutlined ( int x, int y, int w, int h, Color color, Color outlined, int t )
  130. {
  131.     if ( !IsReady () )
  132.         return;
  133.  
  134.     BorderBox ( x, y, w, h, color, t );
  135.     BorderBox ( x - 1, y - 1, w + 2, h + 2, outlined, 1 );
  136.     BorderBox ( x + 1, y + 1, w - 2, h - 2, outlined, 1 );
  137. }
  138.  
  139. void __stdcall BuildVertex ( int x, int y, Color color, D3DTLVERTEX* vertex, int index )
  140. {
  141.     vertex[index].x = x;
  142.     vertex[index].y = y;
  143.     vertex[index].z = 0;
  144.     vertex[index].rhw = 0;
  145.     vertex[index].color = color.GetCode ();
  146. }
  147.  
  148. void CRenderer::Circle ( int x, int y, int radius, Color color )
  149. {
  150.     if ( !IsReady () )
  151.         return;
  152.  
  153.     const int vertCount = 30;
  154.  
  155.     D3DTLVERTEX vertex[ vertCount + 1 ];
  156.  
  157.     float angle = (2.0f * D3DX_PI) / vertCount;
  158.  
  159.     for ( int i = 0; i <= vertCount; i++ )
  160.     {
  161.         float curAngle = (float)( i * angle );
  162.  
  163.         float posX = ( x + radius * cos ( curAngle ) );
  164.         float posY = ( y - radius * sin ( curAngle ) );
  165.  
  166.         vertex[ i ].x = posX;
  167.         vertex[ i ].y = posY;
  168.         vertex[ i ].z = 0.0f;
  169.         vertex[ i ].rhw = 0.0f;
  170.         vertex[ i ].color = color.GetCode ();
  171.     }
  172.  
  173.     m_pDevice->DrawPrimitiveUP ( D3DPT_LINESTRIP, vertCount, vertex, sizeof(D3DTLVERTEX) );
  174. }
  175.  
  176. void CRenderer::Line ( int x0, int y0, int x1, int y1, int w, Color color )
  177. {
  178.     if ( !IsReady () )
  179.         return;
  180.  
  181.     D3DTLVERTEX vertex[ 2 ];
  182.  
  183.     BuildVertex ( x0, y0, color, vertex, 0 );
  184.     BuildVertex ( x1, y1, color, vertex, 1 );
  185.  
  186.     m_pDevice->DrawPrimitiveUP ( D3DPT_LINELIST, 2, &vertex, sizeof(D3DTLVERTEX) );
  187. }
  188.  
  189. void CRenderer::String ( const char* fontName, int x, int y, Color color, const char* text, ... )
  190. {
  191.     if ( !IsReady () )
  192.         return;
  193.  
  194.     ID3DXFont* font = gFontManager.GetFont ( fontName );
  195.  
  196.     if ( !font )
  197.         return;
  198.  
  199.     char buf[ 1024 ];
  200.  
  201.     va_list va_alist;
  202.     va_start(va_alist, text);
  203.     vsprintf ( buf, text, va_alist );
  204.     va_end ( va_alist );
  205.  
  206.     RECT rect = { x, y, x + 50, y + 50 };
  207.     font->DrawTextA ( m_pSprite, buf, -1, &rect, DT_NOCLIP, color.GetCode () );
  208. }
  209.  
  210. void CRenderer::StringOutlined ( const char* fontName, int x, int y, Color color, const char* text, ... )
  211. {
  212.     if ( !IsReady () )
  213.         return;
  214.  
  215.     ID3DXFont* font = gFontManager.GetFont ( fontName );
  216.  
  217.     if ( !font )
  218.         return;
  219.  
  220.     char buf[ 1024 ];
  221.  
  222.     va_list va_alist;
  223.     va_start(va_alist, text);
  224.     vsprintf ( buf, text, va_alist );
  225.     va_end ( va_alist );
  226.  
  227.     RECT FontPos0 = { x, y, x + 120, y + 120 };
  228.     RECT FontPos1 = { x - 1, y, x + 120, y + 120 };
  229.     RECT FontPos2 = { x, y - 1, x + 120, y + 120 };
  230.     RECT FontPos3 = { x + 1, y, x + 120, y + 120 };
  231.     RECT FontPos4 = { x, y + 1, x + 120, y + 120 };
  232.  
  233.     font->DrawTextA ( m_pSprite, buf, -1, &FontPos1, DT_NOCLIP, COLOR_BLACK.GetCode () );
  234.     font->DrawTextA ( m_pSprite, buf, -1, &FontPos2, DT_NOCLIP, COLOR_BLACK.GetCode () );
  235.     font->DrawTextA ( m_pSprite, buf, -1, &FontPos3, DT_NOCLIP, COLOR_BLACK.GetCode () );
  236.     font->DrawTextA ( m_pSprite, buf, -1, &FontPos4, DT_NOCLIP, COLOR_BLACK.GetCode () );
  237.  
  238.     font->DrawTextA ( m_pSprite, buf, -1, &FontPos0, DT_NOCLIP, color.GetCode () );
  239. }
  240.  
  241. bool CRenderer::IsReady ( void )
  242. {
  243.     return ( m_pDevice && m_pSprite && m_pStateBlock );
  244. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement