Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- __declspec( naked ) HRESULT WINAPI hkClear (LPDIRECT3DDEVICE9 pDevice, DWORD Count, CONST D3DRECT* pRects, DWORD Flags, D3DCOLOR Color, float Z, DWORD Stencil)
- {
- __asm nop
- __asm pushad
- if (g_pD3DRenderer->shouldStopClear()) Flags &= (~D3DCLEAR_ZBUFFER);
- __asm popad
- __asm jmp oClear
- }
- #include "D3DRenderer.h"
- D3DRenderer::D3DRenderer()
- {
- pDevice = NULL;
- initialized = false;
- BeginSceneCallback();
- }
- void D3DRenderer::Init(LPDIRECT3DDEVICE9 device)
- {
- if (initialized) return;
- initialized = true;
- pDevice = device;
- LPD3DXBUFFER adj;
- LPDIRECT3DVERTEXBUFFER9 pTempVertexBuffer;
- LPD3DXMESH pTempSphereMesh = NULL;
- if (D3DXCreateSphere(pDevice, 0.5, 32, 32, &pTempSphereMesh, &adj) == D3D_OK) {
- pTempSphereMesh->CloneMeshFVF(0, D3DFVF_MY_VERTEX, pDevice, &sphereMesh);
- pTempSphereMesh->Release();
- if (sphereMesh->OptimizeInplace(D3DXMESHOPT_ATTRSORT, (DWORD*)adj->GetBufferPointer(), NULL, NULL, NULL) != D3D_OK)
- sphereMesh = NULL;
- }
- D3DXCreateSprite(pDevice, &SptDraw);
- D3DXCreateFont(pDevice, 18, 0, 900, 0, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, ANTIALIASED_QUALITY, DEFAULT_PITCH | FF_DONTCARE, TEXT("Monaco"), &pFont);
- ZeroMemory( &m_material, sizeof(D3DMATERIAL9) );
- m_material.Ambient.r = m_material.Ambient.g = m_material.Ambient.b = 0.6;
- m_material.Ambient.a = .4;
- m_material.Diffuse.r = 1.0;
- m_material.Diffuse.g = 1.0;
- m_material.Diffuse.b = 0.0;
- m_material.Diffuse.a = .7;
- pDevice->SetMaterial( &m_material );
- ZeroMemory( &light, sizeof(light) );
- light.Type = D3DLIGHT_DIRECTIONAL;
- D3DXVECTOR3 vecDir;
- vecDir = D3DXVECTOR3(0, -1, 0.2);
- D3DXVec3Normalize( (D3DXVECTOR3*)&light.Direction, &vecDir );
- // set directional light diffuse color
- light.Diffuse.r = 1.0f;
- light.Diffuse.g = 1.0f;
- light.Diffuse.b = 1.0f;
- light.Diffuse.a = 1.0f;
- BeginSceneCallback();
- }
- void D3DRenderer::BeginSceneCallback()
- {
- stride4 = stride48 = stride64 = 0;
- playerStencilSurface = NULL;
- stopClear = false;
- spheres.clear();
- strings.clear();
- rects.clear();
- textures.clear();
- }
- void D3DRenderer::DrawIndexedPrimitiveCallback(int stride)
- {
- if (stride == 4) stride4++;
- else if (stride == 48) stride48++;
- else if (stride == 64) stride64++;
- }
- void D3DRenderer::SetDepthStencilSurfaceCallback(LPDIRECT3DSURFACE9 pSurface)
- {
- if (!pDevice) return;
- if (stride4 && stride48 && stride64) pDevice->GetDepthStencilSurface(&playerStencilSurface);
- stopClear = (playerStencilSurface == pSurface);
- stride4 = stride48 = stride64 = 0;
- }
- bool D3DRenderer::shouldStopClear()
- {
- return stopClear;
- }
- void D3DRenderer::Render(dice::RenderView *view) {
- DWORD fvf, fillmode;
- LPDIRECT3DBASETEXTURE9 texture;
- LPDIRECT3DPIXELSHADER9 pixelshader;
- LPDIRECT3DVERTEXSHADER9 vertexshader;
- LPDIRECT3DSURFACE9 depthsurface;
- D3DXMATRIX s_world, s_view, s_proj;
- pDevice->GetFVF( &fvf );
- pDevice->GetTexture( 0, &texture );
- pDevice->GetPixelShader( &pixelshader );
- pDevice->GetVertexShader( &vertexshader );
- pDevice->GetRenderState( D3DRS_FILLMODE, &fillmode);
- pDevice->GetTransform(D3DTS_WORLD, &s_world);
- pDevice->GetTransform(D3DTS_VIEW, &s_view);
- pDevice->GetTransform(D3DTS_PROJECTION, &s_proj);
- pDevice->GetDepthStencilSurface(&depthsurface);
- pDevice->SetLight( 0, &light );
- pDevice->LightEnable( 0, TRUE );
- pDevice->SetTexture( 0, NULL );
- pDevice->SetPixelShader( NULL );
- pDevice->SetVertexShader( NULL );
- pDevice->SetDepthStencilSurface(playerStencilSurface);
- pDevice->SetFVF( D3DFVF_MY_VERTEX );
- pDevice->SetRenderState(D3DRS_LIGHTING, TRUE);
- pDevice->SetRenderState(D3DRS_COLORVERTEX, TRUE);
- pDevice->SetRenderState(D3DRS_DIFFUSEMATERIALSOURCE, D3DMCS_MATERIAL);
- pDevice->SetRenderState(D3DRS_AMBIENTMATERIALSOURCE, D3DMCS_MATERIAL);
- pDevice->SetRenderState(D3DRS_AMBIENT, D3DCOLOR_RGBA(1, 1, 1, 1));
- pDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
- pDevice->SetRenderState(D3DRS_SRCBLEND,D3DBLEND_SRCALPHA);
- pDevice->SetRenderState(D3DRS_DESTBLEND,D3DBLEND_INVSRCALPHA);
- pDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE );
- pDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_LESS);
- pDevice->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
- pDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
- pDevice->SetTransform(D3DTS_VIEW, &view->m_viewMatrix);
- pDevice->SetTransform(D3DTS_PROJECTION, &view->m_projectionMatrix);
- D3DXMATRIX worldMatrix;
- D3DXMatrixIdentity(&worldMatrix);
- for (vector<RenderSphere>::iterator it = spheres.begin(); it != spheres.end(); it++)
- {
- D3DXMatrixTranslation(&worldMatrix, it->pos.x, it->pos.y, it->pos.z);
- worldMatrix._11 = worldMatrix._22 = worldMatrix._33 = it->radius;
- pDevice->SetTransform(D3DTS_WORLD, &worldMatrix);
- sphereMesh->DrawSubset(0);
- }
- pDevice->SetRenderState(D3DRS_FILLMODE, fillmode);
- pDevice->SetPixelShader( pixelshader );
- pDevice->SetTexture( 0, texture );
- pDevice->SetFVF( fvf );
- pDevice->SetVertexShader( vertexshader );
- pDevice->SetDepthStencilSurface(depthsurface);
- SptDraw->Begin(D3DXSPRITE_ALPHABLEND);
- for (vector<RenderString>::iterator it = strings.begin(); it != strings.end(); it++) {
- RECT rct;
- rct.left = it->x - 1;
- rct.right = it->x + 1;
- rct.top = it->y - 1;
- rct.bottom = it->y + 1;
- pFont->DrawTextA(SptDraw, it->str, -1, &rct, DT_NOCLIP | DT_CENTER, it->color);
- }
- for (vector<RenderTexture>::iterator it = textures.begin(); it != textures.end(); it++)
- SptDraw->Draw(it->texture,NULL,NULL,&D3DXVECTOR3(it->x,it->x, 0.0f), 0xFFFFFFFF);
- SptDraw->End();
- for (vector<RenderRect>::iterator it = rects.begin(); it != rects.end(); it++)
- pDevice->Clear(1, &it->rect, D3DCLEAR_TARGET, it->color, 0, 0);
- }
- void D3DRenderer::PrintText(long x, long y, D3DCOLOR fontColor, char *text, ...)
- {
- if(!text) { return; }
- RenderString rs;
- va_list va_alist;
- va_start (va_alist, text);
- _vsnprintf (rs.str, 512, text, va_alist);
- va_end (va_alist);
- rs.x = x;
- rs.y = y;
- rs.color = fontColor;
- strings.push_back(rs);
- }
- void D3DRenderer::DrawSphere(D3DXVECTOR3 *pos, float radius, D3DCOLOR col)
- {
- RenderSphere rs;
- rs.pos = *pos;
- rs.col = col;
- rs.radius = radius;
- spheres.push_back(rs);
- }
- void D3DRenderer::DrawRect(int startx, int starty, int endx, int endy, D3DCOLOR col)
- {
- #define RECT_ASSIGN(r, a, b, c, d) r.x1 = a; r.y1 = b; r.x2 = c; r.y2 = d
- RenderRect rr;
- RECT_ASSIGN(rr.rect, startx, starty, endx, endy);
- rr.color = col;
- rects.push_back(rr);
- }
- void D3DRenderer::DrawBox(int startx, int starty, int endx, int endy, int width, D3DCOLOR col)
- {
- DrawRect(startx, starty, startx + width, endy, col);
- DrawRect(endx, starty, endx + width, endy, col);
- DrawRect(startx, starty, endx, starty + width, col);
- DrawRect(startx, endy, endx, endy + width, col);
- }
- void D3DRenderer::DrawTexture(int x, int y, LPDIRECT3DTEXTURE9 dTexture)
- {
- RenderTexture rt;
- rt.x = x;
- rt.y = y;
- rt.texture = dTexture;
- textures.push_back(rt);
- }
- D3DRenderer::~D3DRenderer(void)
- {
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement