Никак не получается инициализировать Direct3D, что я делаю не так?
Game.cpp
Code
#include "Precompiled.h"
#include "DisplayDevice.h"
#include "GraphicsServer.h"
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{
DisplayDevice* displayDevice = new DisplayDevice();
displayDevice->OpenWindow();
GraphicsServer* gfxServer = new GraphicsServer();
gfxServer->OnFrame();
displayDevice->CloseWindow();
return 0;
}
GraphicsServer.h
Code
#ifndef GRAPHICSSERVER_H
#define GRAPHICSSERVER_H
#include "Precompiled.h"
typedef unsigned int IndexT;
class GraphicsServer
{
public:
GraphicsServer();
virtual ~GraphicsServer();
void OnFrame();
private:
IndexT frameCount;
};
#endif
GraphicsServer.cpp
Code
#include "Precompiled.h"
#include "GraphicsServer.h"
#include "RenderDevice.h"
#include "DisplayDevice.h"
#include "ManagedResource.h"
GraphicsServer::GraphicsServer()
{
}
GraphicsServer::~GraphicsServer()
{
}
void GraphicsServer::OnFrame()
{
RenderDevice* renderDevice = RenderDevice::GetSingleton();
DisplayDevice* displayDevice = DisplayDevice::GetSingleton();
displayDevice->ProcessWindowMessages();
if(renderDevice->BeginFrame())
{
renderDevice->EndFrame();
renderDevice->Present();
}
this->frameCount++;
}
Добавлено (28.09.2008, 03:35)
---------------------------------------------
DisplayDevice.cpp
Code
#include "Precompiled.h"
#include "DisplayDevice.h"
DisplayDevice* DisplayDevice::Singleton = NULL;
DisplayDevice::DisplayDevice()
{
Singleton = this;
this->hInstance = GetModuleHandle(0);
}
DisplayDevice::~DisplayDevice()
{
Singleton = NULL;
}
void DisplayDevice::ProcessWindowMessages()
{
if(0 != this->hWnd)
{
MSG msg;
ZeroMemory(&msg, sizeof(MSG));
while(PeekMessage(&msg, this->hWnd, 0, 0, PM_REMOVE))
{
int msgHandled = TranslateAccelerator(this->hWnd, this->hAccel, &msg);
if(0 == msgHandled)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
}
}
bool DisplayDevice::OpenWindow()
{
ACCEL acc[1];
acc[0].cmd = 1001;
acc[0].fVirt = FALT | FNOINVERT | FVIRTKEY;
acc[0].key = VK_RETURN;
this->hAccel = CreateAcceleratorTable((LPACCEL)acc, 1);
WNDCLASSEX wcex;
ZeroMemory(&wcex, sizeof(WNDCLASSEX));
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
wcex.lpfnWndProc = (WNDPROC)WinProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = sizeof(void*);
wcex.hInstance = this->hInstance;
wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)GetStockObject(NULL_BRUSH);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = "WindowClass";
wcex.hIconSm = NULL;
RegisterClassEx(&wcex);
this->hWnd = CreateWindow("WindowClass", "Windows Application", windowedStyle,
CW_USEDEFAULT, CW_USEDEFAULT, screenWidth, screenHeight, NULL, NULL, this->hInstance, NULL);
ShowWindow(this->hWnd, SW_SHOW);
UpdateWindow(this->hWnd);
return true;
}
void DisplayDevice::CloseWindow()
{
if(0 != this->hWnd)
{
DestroyWindow(this->hWnd);
this->hWnd = 0;
}
if (this->hAccel)
{
DestroyAcceleratorTable(this->hAccel);
this->hAccel = 0;
}
ShowCursor(true);
UnregisterClass("WindowClass", this->hInstance);
}
LRESULT CALLBACK DisplayDevice::WinProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
HDC hDc;
PAINTSTRUCT paintStr;
switch(uMsg)
{
case WM_PAINT:
hDc = BeginPaint(hWnd, &paintStr);
EndPaint(hWnd, &paintStr);
break;
case WM_COMMAND:
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
}
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
Добавлено (28.09.2008, 03:36)
---------------------------------------------
DisplayDevice.h
Code
#ifndef DISPLAYDEVICE_H
#define DISPLAYDEVICE_H
#include "Precompiled.h"
class DisplayDevice
{
public:
DisplayDevice();
virtual ~DisplayDevice();
virtual void ProcessWindowMessages();
virtual bool OpenWindow();
virtual void CloseWindow();
HWND GetHwnd() const;
static inline DisplayDevice* GetSingleton() { return Singleton; }
protected:
static LRESULT CALLBACK WinProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
HINSTANCE hInstance;
HWND hWnd;
HACCEL hAccel;
DWORD screenWidth;
DWORD screenHeight;
DWORD windowedStyle;
DWORD fullscreenStyle;
private:
static DisplayDevice* Singleton;
};
inline HWND DisplayDevice::GetHwnd() const
{
return this->hWnd;
}
#endif
Добавлено (28.09.2008, 03:37)
---------------------------------------------
RenderDevice.h
Code
#ifndef RENDERDEVICE_H
#define RENDERDEVICE_H
#include "Precompiled.h"
class RenderDevice
{
public:
RenderDevice();
virtual ~RenderDevice();
void SetupBufferFormats();
void SetupPresentParams();
bool OpenDirect3D();
void CloseDirect3D();
bool TestResetDevice();
bool BeginFrame();
void EndFrame();
void Present();
LPDIRECT3DDEVICE9 GetDevice() { return direct3DDevice; }
static inline RenderDevice* GetSingleton() { return Singleton; }
private:
static IDirect3D9* direct3D;
IDirect3DDevice9* direct3DDevice;
D3DPRESENT_PARAMETERS presentParams;
UINT adapter;
D3DFORMAT displayFormat;
private:
static RenderDevice* Singleton;
};
#endif
Добавлено (28.09.2008, 03:37)
---------------------------------------------
RenderDevice.cpp
Code
#include "Precompiled.h"
#include "RenderDevice.h"
#include "DisplayDevice.h"
RenderDevice* RenderDevice::Singleton = NULL;
IDirect3D9* RenderDevice::direct3D = 0;
RenderDevice::RenderDevice() :
direct3DDevice(0),
adapter(0),
displayFormat(D3DFMT_A8R8G8B8)
{
Singleton = this;
direct3D = Direct3DCreate9(D3D_SDK_VERSION);
}
RenderDevice::~RenderDevice()
{
Singleton = NULL;
}
void RenderDevice::SetupBufferFormats()
{
this->presentParams.Windowed = true;
this->presentParams.BackBufferCount = 1;
this->presentParams.BackBufferWidth = 800;
this->presentParams.BackBufferHeight = 600;
this->presentParams.BackBufferFormat = displayFormat;
this->presentParams.EnableAutoDepthStencil = FALSE; // Выключение буфера глубины
this->presentParams.AutoDepthStencilFormat = D3DFMT_D24S8;
}
void RenderDevice::SetupPresentParams()
{
DisplayDevice* displayDevice = DisplayDevice::GetSingleton();
this->presentParams.SwapEffect = D3DSWAPEFFECT_DISCARD;
this->presentParams.hDeviceWindow = displayDevice->GetHwnd();
this->presentParams.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
this->presentParams.MultiSampleType = D3DMULTISAMPLE_NONE;
this->presentParams.MultiSampleQuality = 0;
}
bool RenderDevice::OpenDirect3D()
{
DisplayDevice* displayDevice = DisplayDevice::GetSingleton();
ZeroMemory(&this->presentParams, sizeof(this->presentParams));
this->SetupBufferFormats();
this->SetupPresentParams();
HRESULT hResult = this->direct3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL,
displayDevice->GetHwnd(), D3DCREATE_SOFTWARE_VERTEXPROCESSING, &(this->presentParams), &(this->direct3DDevice));
ShowCursor(false);
return true;
}
void RenderDevice::CloseDirect3D()
{
ShowCursor(true);
if(0 != this->direct3DDevice)
{
this->direct3DDevice->Release();
this->direct3DDevice = 0;
}
if(0 != this->direct3D)
{
this->direct3D->Release();
this->direct3D = 0;
}
}
bool RenderDevice::BeginFrame()
{
this->direct3DDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
HRESULT hRes = this->direct3DDevice->BeginScene();
return true;
}
void RenderDevice::EndFrame()
{
// Конец отрисовки сцены
HRESULT hRes = this->direct3DDevice->EndScene();
}
void RenderDevice::Present()
{
HRESULT hRes = this->direct3DDevice->Present(NULL, NULL, NULL, NULL);
}