You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
136 lines
3.2 KiB
136 lines
3.2 KiB
5 years ago
|
#include "DxRender.h"
|
||
|
|
||
|
DxRender::DxRender()
|
||
|
{
|
||
|
g_pD3D = NULL;
|
||
|
g_pD3DDevice = NULL;
|
||
|
pDxFont = NULL;
|
||
|
}
|
||
|
|
||
|
DxRender::~DxRender()
|
||
|
{
|
||
|
this->Release();
|
||
|
}
|
||
|
|
||
|
bool DxRender::Init(HWND hWnd, MapManager* mMapManager)
|
||
|
{
|
||
|
HRESULT hr;
|
||
|
D3DDISPLAYMODE disp_mode;
|
||
|
D3DPRESENT_PARAMETERS pp;
|
||
|
if(!g_pD3D)
|
||
|
g_pD3D = Direct3DCreate9(D3D_SDK_VERSION);
|
||
|
if(!g_pD3D) return false;
|
||
|
|
||
|
hr = g_pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &disp_mode);
|
||
|
if (FAILED(hr))
|
||
|
{
|
||
|
g_pD3D->Release();
|
||
|
g_pD3D = NULL;
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
ZeroMemory(&pp, sizeof(D3DPRESENT_PARAMETERS));
|
||
|
pp.Windowed = true;
|
||
|
pp.SwapEffect = D3DSWAPEFFECT_DISCARD;
|
||
|
pp.BackBufferFormat = disp_mode.Format;
|
||
|
pp.EnableAutoDepthStencil = TRUE;
|
||
|
pp.AutoDepthStencilFormat = D3DFMT_D16;
|
||
|
pp.BackBufferCount = 1;
|
||
|
|
||
|
hr = g_pD3D->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,hWnd,D3DCREATE_HARDWARE_VERTEXPROCESSING,&pp,&g_pD3DDevice);
|
||
|
if (FAILED(hr))
|
||
|
{
|
||
|
hr = g_pD3D->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,hWnd,D3DCREATE_SOFTWARE_VERTEXPROCESSING,&pp,&g_pD3DDevice);
|
||
|
if (FAILED(hr))
|
||
|
{
|
||
|
hr = g_pD3D->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_REF,hWnd,D3DCREATE_SOFTWARE_VERTEXPROCESSING,&pp,&g_pD3DDevice);
|
||
|
if (FAILED(hr))
|
||
|
{
|
||
|
g_pD3D->Release();
|
||
|
g_pD3D = NULL;
|
||
|
return FALSE;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
D3DCAPS9 DX9DCaps;
|
||
|
g_pD3DDevice->GetDeviceCaps(&DX9DCaps);
|
||
|
|
||
|
ZeroMemory( &light, sizeof(light) );
|
||
|
light.Type = D3DLIGHT_DIRECTIONAL;
|
||
|
light.Diffuse.r = 1.0f;
|
||
|
light.Diffuse.g = 1.0f;
|
||
|
light.Diffuse.b = 1.0f;
|
||
|
light.Ambient.r = 1.0f;
|
||
|
light.Ambient.g = 1.0f;
|
||
|
light.Ambient.b = 1.0f;
|
||
|
|
||
|
D3DXVECTOR3 vector = D3DXVECTOR3(0.0f,1.0f,2.0f);
|
||
|
D3DXVec3Normalize((D3DXVECTOR3*)&light.Direction,&vector);
|
||
|
|
||
|
g_pD3DDevice->SetLight(0, &light);
|
||
|
|
||
|
QueryPerformanceFrequency(&m_PerformanceFrequency);
|
||
|
QueryPerformanceCounter(&m_LastFPSUpdateTick);
|
||
|
m_FramesRendered = 0;
|
||
|
|
||
|
D3DXCreateFontA(g_pD3DDevice, 30, 10, FW_NORMAL, 0, false, 1, 0, 0, DEFAULT_PITCH|FF_MODERN, "Arial", &pDxFont);
|
||
|
|
||
|
m_MapManager = mMapManager;
|
||
|
m_MapManager->SetDevice(g_pD3DDevice);
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
void DxRender::Release()
|
||
|
{
|
||
|
if(pDxFont)
|
||
|
{
|
||
|
pDxFont->Release();
|
||
|
pDxFont = NULL;
|
||
|
}
|
||
|
if (g_pD3DDevice)
|
||
|
{
|
||
|
g_pD3DDevice->Release();
|
||
|
g_pD3DDevice = NULL;
|
||
|
}
|
||
|
if (g_pD3D)
|
||
|
{
|
||
|
g_pD3D->Release();
|
||
|
g_pD3D = NULL;
|
||
|
}
|
||
|
//TODO: Clear buffers
|
||
|
}
|
||
|
|
||
|
void DxRender::onTick()
|
||
|
{
|
||
|
if(g_pD3DDevice)
|
||
|
{
|
||
|
g_pD3DDevice->Clear( 0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(128,0,0), 1.0f, 0L );
|
||
|
|
||
|
DWORD CurrentTick = GetTickCount();
|
||
|
LARGE_INTEGER CurrentPerformanceTick;
|
||
|
QueryPerformanceCounter(&CurrentPerformanceTick);
|
||
|
double TimeElapsedSeconds = (double)(CurrentPerformanceTick.QuadPart - m_LastFPSUpdateTick.QuadPart) / (double)(m_PerformanceFrequency.QuadPart);
|
||
|
if(TimeElapsedSeconds >= 1)
|
||
|
{
|
||
|
m_CurrentFPS = ((double)m_FramesRendered) / TimeElapsedSeconds;
|
||
|
m_FramesRendered = 0;
|
||
|
m_LastFPSUpdateTick = CurrentPerformanceTick;
|
||
|
}
|
||
|
|
||
|
g_pD3DDevice->BeginScene();
|
||
|
m_MapManager->Render();
|
||
|
|
||
|
if(pDxFont)
|
||
|
{
|
||
|
char buffer[128];
|
||
|
RECT rc={0,0,100,100};
|
||
|
sprintf(buffer, "FPS: %.1lf", m_CurrentFPS);
|
||
|
pDxFont->DrawTextA(NULL, buffer, lstrlenA(buffer), &rc, DT_TOP | DT_LEFT, D3DCOLOR_XRGB(255,255,0));
|
||
|
}
|
||
|
g_pD3DDevice->EndScene();
|
||
|
|
||
|
g_pD3DDevice->Present( NULL, NULL, NULL, NULL );
|
||
|
m_FramesRendered++;
|
||
|
}
|
||
|
}
|