Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "Renderer.h"
- #include "FontManager.h"
- CRenderer gRenderer;
- CRenderer::CRenderer ( void )
- {
- m_pSprite = 0;
- m_pStateBlock = 0;
- m_pDevice = 0;
- m_dwFVF = 0;
- }
- CRenderer::~CRenderer ( void )
- {
- }
- void CRenderer::Update ( IDirect3DDevice9* pDevice )
- {
- if ( !m_pDevice )
- {
- m_pDevice = pDevice;
- }
- if ( !m_pStateBlock )
- {
- m_pDevice->CreateStateBlock ( D3DSBT_ALL, &m_pStateBlock );
- }
- if ( !m_pSprite )
- {
- D3DXCreateSprite ( m_pDevice, &m_pSprite );
- }
- }
- void CRenderer::PreFrame ( void )
- {
- m_pStateBlock->Capture ();
- m_pSprite->Begin ( D3DXSPRITE_ALPHABLEND );
- m_pDevice->GetFVF ( &m_dwFVF );
- m_pDevice->SetFVF ( D3DFVF_XYZRHW | D3DFVF_DIFFUSE | D3DFVF_TEX1 );
- m_pDevice->SetTexture ( 0, 0 );
- }
- void CRenderer::PostFrame ( void )
- {
- m_pDevice->SetFVF ( m_dwFVF );
- m_pSprite->End ();
- m_pStateBlock->Apply ();
- }
- void CRenderer::Release ( void )
- {
- if ( m_pSprite )
- {
- m_pSprite->OnLostDevice ();
- m_pSprite->OnResetDevice ();
- m_pSprite = 0;
- }
- if ( m_pStateBlock )
- {
- m_pStateBlock->Release ();
- m_pStateBlock = 0;
- }
- }
- void CRenderer::SetDevice ( IDirect3DDevice9* pDevice )
- {
- m_pDevice = pDevice;
- }
- IDirect3DDevice9* CRenderer::GetDevice ( void )
- {
- return m_pDevice;
- }
- struct D3DTLVERTEX
- {
- float x, y, z, rhw;
- DWORD color;
- };
- void CRenderer::Rect ( int x, int y, int w, int h, Color color )
- {
- if ( !IsReady () )
- return;
- D3DTLVERTEX vertex[ 4 ] =
- {
- { x, y + h, 0, 1, color.GetCode () },
- { x, y, 0, 1, color.GetCode () },
- { x + w, y + h, 0, 1, color.GetCode () },
- { x + w, y, 0, 1, color.GetCode () }
- };
- m_pDevice->SetTexture ( 0, 0 );
- m_pDevice->DrawPrimitiveUP ( D3DPT_TRIANGLESTRIP, 2, vertex, sizeof(D3DTLVERTEX) );
- }
- void CRenderer::RectOutlined ( int x, int y, int w, int h, Color color, Color outlined, int t )
- {
- if ( !IsReady () )
- return;
- Rect ( x, y, w, h, color );
- BorderBox ( x - 1, y - 1, w + 1, h + 1, outlined, t );
- }
- void CRenderer::BorderBox ( int x, int y, int w, int h, Color color, int t )
- {
- if ( !IsReady () )
- return;
- Rect ( x, y, w, t, color );
- Rect ( x, y, t, h, color );
- Rect ( x + w, y, t, h, color );
- Rect ( x, y + h, w + t, t, color );
- }
- void CRenderer::BorderBoxOutlined ( int x, int y, int w, int h, Color color, Color outlined, int t )
- {
- if ( !IsReady () )
- return;
- BorderBox ( x, y, w, h, color, t );
- BorderBox ( x - 1, y - 1, w + 2, h + 2, outlined, 1 );
- BorderBox ( x + 1, y + 1, w - 2, h - 2, outlined, 1 );
- }
- void __stdcall BuildVertex ( int x, int y, Color color, D3DTLVERTEX* vertex, int index )
- {
- vertex[index].x = x;
- vertex[index].y = y;
- vertex[index].z = 0;
- vertex[index].rhw = 0;
- vertex[index].color = color.GetCode ();
- }
- void CRenderer::Circle ( int x, int y, int radius, Color color )
- {
- if ( !IsReady () )
- return;
- const int vertCount = 30;
- D3DTLVERTEX vertex[ vertCount + 1 ];
- float angle = (2.0f * D3DX_PI) / vertCount;
- for ( int i = 0; i <= vertCount; i++ )
- {
- float curAngle = (float)( i * angle );
- float posX = ( x + radius * cos ( curAngle ) );
- float posY = ( y - radius * sin ( curAngle ) );
- vertex[ i ].x = posX;
- vertex[ i ].y = posY;
- vertex[ i ].z = 0.0f;
- vertex[ i ].rhw = 0.0f;
- vertex[ i ].color = color.GetCode ();
- }
- m_pDevice->DrawPrimitiveUP ( D3DPT_LINESTRIP, vertCount, vertex, sizeof(D3DTLVERTEX) );
- }
- void CRenderer::Line ( int x0, int y0, int x1, int y1, int w, Color color )
- {
- if ( !IsReady () )
- return;
- D3DTLVERTEX vertex[ 2 ];
- BuildVertex ( x0, y0, color, vertex, 0 );
- BuildVertex ( x1, y1, color, vertex, 1 );
- m_pDevice->DrawPrimitiveUP ( D3DPT_LINELIST, 2, &vertex, sizeof(D3DTLVERTEX) );
- }
- void CRenderer::String ( const char* fontName, int x, int y, Color color, const char* text, ... )
- {
- if ( !IsReady () )
- return;
- ID3DXFont* font = gFontManager.GetFont ( fontName );
- if ( !font )
- return;
- char buf[ 1024 ];
- va_list va_alist;
- va_start(va_alist, text);
- vsprintf ( buf, text, va_alist );
- va_end ( va_alist );
- RECT rect = { x, y, x + 50, y + 50 };
- font->DrawTextA ( m_pSprite, buf, -1, &rect, DT_NOCLIP, color.GetCode () );
- }
- void CRenderer::StringOutlined ( const char* fontName, int x, int y, Color color, const char* text, ... )
- {
- if ( !IsReady () )
- return;
- ID3DXFont* font = gFontManager.GetFont ( fontName );
- if ( !font )
- return;
- char buf[ 1024 ];
- va_list va_alist;
- va_start(va_alist, text);
- vsprintf ( buf, text, va_alist );
- va_end ( va_alist );
- RECT FontPos0 = { x, y, x + 120, y + 120 };
- RECT FontPos1 = { x - 1, y, x + 120, y + 120 };
- RECT FontPos2 = { x, y - 1, x + 120, y + 120 };
- RECT FontPos3 = { x + 1, y, x + 120, y + 120 };
- RECT FontPos4 = { x, y + 1, x + 120, y + 120 };
- font->DrawTextA ( m_pSprite, buf, -1, &FontPos1, DT_NOCLIP, COLOR_BLACK.GetCode () );
- font->DrawTextA ( m_pSprite, buf, -1, &FontPos2, DT_NOCLIP, COLOR_BLACK.GetCode () );
- font->DrawTextA ( m_pSprite, buf, -1, &FontPos3, DT_NOCLIP, COLOR_BLACK.GetCode () );
- font->DrawTextA ( m_pSprite, buf, -1, &FontPos4, DT_NOCLIP, COLOR_BLACK.GetCode () );
- font->DrawTextA ( m_pSprite, buf, -1, &FontPos0, DT_NOCLIP, color.GetCode () );
- }
- bool CRenderer::IsReady ( void )
- {
- return ( m_pDevice && m_pSprite && m_pStateBlock );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement