keybode

Renderer.cpp

Jan 23rd, 2014
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.85 KB | None | 0 0
  1. #include "Renderer.h"
  2. #include "Vector.h"
  3.  
  4. CRenderer* g_pRenderer = new CRenderer( );
  5.  
  6. CRenderer::CRenderer( void )
  7. {
  8.     m_pDevice = NULL;
  9.     m_pFont = NULL;
  10.     m_pLine = NULL;
  11. }
  12.  
  13. CRenderer::~CRenderer( void )
  14. {
  15.     m_pDevice = NULL;
  16.     m_pFont = NULL;
  17.     m_pLine = NULL;
  18. }
  19.  
  20. void CRenderer::Update( IDirect3DDevice9* pDevice )
  21. {
  22.     if( !m_pDevice )
  23.     {
  24.         m_pDevice = pDevice;
  25.     }
  26.  
  27.     if( !m_pFont )
  28.     {
  29.         if( FAILED( D3DXCreateFont( m_pDevice, 14, NULL, NULL, NULL, NULL, DEFAULT_CHARSET, OUT_CHARACTER_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH, "Verdana", &m_pFont ) ) )
  30.         {
  31.             return;
  32.         }
  33.     }
  34.  
  35.     if( !m_pLine )
  36.     {
  37.         if( FAILED( D3DXCreateLine( m_pDevice, &m_pLine ) ) )
  38.         {
  39.             return;
  40.         }
  41.     }
  42. }
  43.  
  44. void CRenderer::Release( void )
  45. {
  46.     if( m_pFont )
  47.     {
  48.         m_pFont->OnLostDevice( );
  49.         m_pFont->OnResetDevice( );
  50.  
  51.         m_pFont = NULL;
  52.     }
  53.  
  54.     if( m_pLine )
  55.     {
  56.         m_pLine->OnLostDevice( );
  57.         m_pLine->OnResetDevice( );
  58.  
  59.         m_pLine = NULL;
  60.     }
  61. }
  62.  
  63. void CRenderer::SetFont( ID3DXFont* pFont )
  64. {
  65.     m_pFont = pFont;
  66. }
  67.  
  68. void CRenderer::SetLine( ID3DXLine* pLine )
  69. {
  70.     m_pLine = pLine;
  71. }
  72.  
  73. void CRenderer::SetDevice( IDirect3DDevice9* pDevice )
  74. {
  75.     m_pDevice = pDevice;
  76. }
  77.  
  78. ID3DXFont* CRenderer::GetFont( void )
  79. {
  80.     return m_pFont;
  81. }
  82.  
  83. ID3DXLine* CRenderer::GetLine( void )
  84. {
  85.     return m_pLine;
  86. }
  87.  
  88. IDirect3DDevice9* CRenderer::GetDevice( void )
  89. {
  90.     return m_pDevice;
  91. }
  92.  
  93. void CRenderer::DrawRect( int x, int y, int w, int h, DWORD_PTR color )
  94. {
  95.     if( !IsReady( ) )
  96.         return;
  97.  
  98.     D3DXVECTOR2 vecLine[ 2 ];
  99.  
  100.     m_pLine->SetWidth( w );
  101.     m_pLine->SetAntialias( false );
  102.     m_pLine->SetGLLines( false );
  103.  
  104.     vecLine[ 0 ].x = x + w / 2;
  105.     vecLine[ 0 ].y = y;
  106.  
  107.     vecLine[ 1 ].x = x + w / 2;
  108.     vecLine[ 1 ].y = y + h;
  109.  
  110.     m_pLine->Begin( );
  111.     m_pLine->Draw( vecLine, 2, color );
  112.     m_pLine->End( );
  113. }
  114.  
  115. void CRenderer::DrawRectOutlined( int x, int y, int w, int h, DWORD_PTR color, DWORD_PTR outlined, int thickness )
  116. {
  117.     if( !IsReady( ) )
  118.         return;
  119.  
  120.     DrawRect( x, y, w, h, color );
  121.     DrawBorderBox( x - 1, y - 1, w + 1, h + 1, outlined, thickness );
  122. }
  123.  
  124. void CRenderer::DrawBorderBox( int x, int y, int w, int h, DWORD_PTR color, int thickness )
  125. {
  126.     if( !IsReady( ) )
  127.         return;
  128.  
  129.     DrawRect( x, y, w, thickness, color );
  130.     DrawRect( x, y, thickness, h, color );
  131.     DrawRect( x + w, y, thickness, h, color );
  132.     DrawRect( x, y + h, w + thickness, thickness, color );
  133. }
  134.  
  135. void CRenderer::DrawBorderBoxOutlined( int x, int y, int w, int h, DWORD_PTR color, DWORD_PTR outlined, int thickness )
  136. {
  137.     if( !IsReady( ) )
  138.         return;
  139.  
  140.     DrawBorderBox( x, y, w, h, color, thickness );
  141.     DrawBorderBox( x - 1, y - 1, w + 2, h + 2, outlined, 1 );
  142.     DrawBorderBox( x + 1, y + 1, w - 2, h - 2, outlined, 1 );
  143. }
  144.  
  145. void CRenderer::DrawLine( int x0, int y0, int x1, int y1, DWORD_PTR color )
  146. {
  147.     if( !IsReady( ) )
  148.         return;
  149.  
  150.     D3DXVECTOR2 vecLine[ 2 ];
  151.  
  152.     m_pLine->SetWidth( 1 );
  153.  
  154.     vecLine[ 0 ].x = x0;
  155.     vecLine[ 0 ].y = y0;
  156.  
  157.     vecLine[ 1 ].x = x1;
  158.     vecLine[ 1 ].y = y1;
  159.  
  160.     m_pLine->Draw( vecLine, 2, color );
  161. }
  162.  
  163. void CRenderer::DrawLine( Vector x, Vector y, DWORD_PTR color )
  164. {
  165.     if( !IsReady( ) )
  166.         return;
  167.  
  168.     D3DXVECTOR2 vecLine[ 2 ];
  169.  
  170.     m_pLine->SetWidth( 1 );
  171.  
  172.     vecLine[ 0 ].x = x.x;
  173.     vecLine[ 0 ].y = x.y;
  174.  
  175.     vecLine[ 1 ].x = y.x;
  176.     vecLine[ 1 ].y = y.y;
  177.  
  178.     m_pLine->Draw( vecLine, 2, color );
  179. }
  180.  
  181. void CRenderer::DrawString( int x, int y, DWORD_PTR color, const char* text, ... )
  182. {
  183.     if( !IsReady( ) )
  184.         return;
  185.  
  186.     char lpBuffer[ 1024 ];
  187.  
  188.     va_list va_alist;
  189.     va_start( va_alist, text );
  190.     vsprintf( lpBuffer, text, va_alist );
  191.     va_end( va_alist );
  192.  
  193.     RECT rect = { x, y, x + 50, y + 50 };
  194.     m_pFont->DrawTextA( NULL, lpBuffer, -1, &rect, DT_NOCLIP, color );
  195. }
  196.  
  197. void CRenderer::DrawStringOutlined( int x, int y, DWORD_PTR color, const char* text, ... )
  198. {
  199.     if( !IsReady( ) )
  200.         return;
  201. }
  202.  
  203. bool CRenderer::IsReady( void )
  204. {
  205.     return ( m_pDevice && m_pFont && m_pLine );
  206. }
Advertisement
Add Comment
Please, Sign In to add comment