3
LPDIRECT3D8 g_pD3D = NULL;
4
LPDIRECT3DDEVICE8 g_pD3DDevice = NULL;
6
HRESULT InitialiseD3D(HWND hWnd)
8
// First of all, create the main D3D object. If it is created successfully
9
// we should get a pointer to an IDirect3D8 interface.
10
g_pD3D = Direct3DCreate8(D3D_SDK_VERSION);
17
//Get the current display mode
19
if(FAILED(g_pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &d3ddm)))
24
// Create a structure to hold the settings for our device
25
D3DPRESENT_PARAMETERS d3dpp;
26
ZeroMemory(&d3dpp, sizeof(d3dpp));
28
// Fill the structure: Program shall be windowed,
29
// back buffer format matches current display mode
30
d3dpp.Windowed = TRUE;
31
d3dpp.SwapEffect = D3DSWAPEFFECT_COPY_VSYNC;
32
d3dpp.BackBufferFormat = d3ddm.Format;
34
//Create a Direct3D device.
35
if(FAILED(g_pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
36
D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &g_pD3DDevice)))
46
if(g_pD3DDevice == NULL)
51
// Clear the backbuffer to blue
52
g_pD3DDevice->Clear(0, NULL, D3DCLEAR_TARGET,
53
D3DCOLOR_XRGB(0, 0, 255), 1.0f, 0);
56
g_pD3DDevice->BeginScene();
58
// Fill in here the rendering of other objects
61
g_pD3DDevice->EndScene();
63
// Fill back and front buffers so that back buffer will be visible on screen
64
g_pD3DDevice->Present(NULL, NULL, NULL, NULL);
69
if(g_pD3DDevice != NULL)
71
g_pD3DDevice->Release();
84
// Enter the main loop
88
PeekMessage(&msg, NULL, 0U, 0U, PM_NOREMOVE);
90
while(msg.message != WM_QUIT)
92
bMessage = PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE);
97
TranslateMessage(&msg);
98
DispatchMessage(&msg);
102
Render(); // No message to process -> render the scene
107
// The windows message handler
108
LRESULT WINAPI WinProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
121
// Escape key pressed -> exit
129
return DefWindowProc(hWnd, msg, wParam, lParam);
132
// Application main entry point
133
INT WINAPI WinMain(HINSTANCE hInst, HINSTANCE, LPSTR, INT)
135
//Register the window class
136
WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, WinProc, 0L, 0L,
137
GetModuleHandle(NULL), NULL, NULL, NULL, NULL,
138
"DirectX Project", NULL };
139
RegisterClassEx(&wc);
141
// Create the application's main window
142
HWND hWnd = CreateWindow("DirectX Project", "Code::Blocks Template",
143
WS_OVERLAPPEDWINDOW, 50, 50, 500, 500,
144
GetDesktopWindow(), NULL, wc.hInstance, NULL);
146
// Initialize Direct3D
147
if(SUCCEEDED(InitialiseD3D(hWnd)))
150
ShowWindow(hWnd, SW_SHOWDEFAULT);
153
//Start game running: Enter the game loop
159
UnregisterClass("DirectX Project", wc.hInstance);