Code:
#define WIN32_LEAN_AND_MEAN
#include<d3dx9.h>
#define D3DFVF_D3DVertex (D3DFVF_XYZ | D3DFVF_DIFFUSE)
bool InitializeDirect3D(HWND hwnd, bool fullscreen);
bool InitializeObject();
void RenderScence();
void ShutdownDirect3D();
LPDIRECT3D9 Direct3D_Object = NULL;
LPDIRECT3DDEVICE9 D3D_Device = NULL;
LPD3DXFONT Font = NULL;
LPDIRECT3DVERTEXBUFFER9 Vertex_Buffer = NULL;
RECT FontPosition = {0, 0, 0, 0};
HFONT hfont = CreateFont(0, 1, 0, 0, FW_DONTCARE, false, false, false, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_MODERN, "Courier New");
struct Vertex
{
FLOAT x, y, z;
DWORD color;
};
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch(message) {
case WM_KEYUP:
switch(wParam) {
case VK_ESCAPE:
PostQuitMessage(0);
break;
}
break;
case WM_DESTROY:
case WM_CLOSE:
PostQuitMessage(0);
break;
default:
break;
}
return DefWindowProc( hwnd, message, wParam, lParam );
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
MSG msg;
HWND hwnd;
WNDCLASSEX windowClass;
bool done = false;
windowClass.cbSize = sizeof(WNDCLASSEX);
windowClass.style = CS_HREDRAW | CS_VREDRAW;
windowClass.lpfnWndProc = WndProc;
windowClass.cbClsExtra = 0;
windowClass.cbWndExtra = 0;
windowClass.hInstance = hInstance;
windowClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
windowClass.hCursor = LoadCursor(NULL, IDC_ARROW);
windowClass.hbrBackground = NULL;
windowClass.lpszMenuName = NULL;
windowClass.lpszClassName = "XClass";
windowClass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if(!RegisterClassEx(&windowClass)) return 0;
hwnd = CreateWindow("XClass", "Empty DirectX", WS_OVERLAPPEDWINDOW | WS_VISIBLE | WS_SYSMENU |WS_CLIPCHILDREN | WS_CLIPSIBLINGS, 100, 100, 640, 480, NULL, NULL, hInstance, NULL);
if(!hwnd) return 0;
ShowWindow(hwnd, SW_SHOW);
UpdateWindow(hwnd);
done = false;
if(!InitializeDirect3D(hwnd, true)) done = true;
while(!done)
{
if(PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
{
if(msg.message == WM_QUIT)
{
done = true;
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
RenderScence();
}
}
ShutdownDirect3D();
UnregisterClass("XClass", windowClass.hInstance);
return (int)msg.wParam;
}
bool InitializeDirect3D(HWND hwnd, bool fullscreen) {
D3DDISPLAYMODE DisplayMode;
D3DPRESENT_PARAMETERS Present_Parameters;
D3DCAPS9 D3DCaps;
ZeroMemory(&Present_Parameters, sizeof(Present_Parameters));
Direct3D_Object = Direct3DCreate9(D3D_SDK_VERSION);
if(Direct3D_Object == NULL)
{
MessageBox(NULL, "Error, couldn't initialize DirectX", "Error!", MB_ICONSTOP | MB_OK);
return false;
}
if(FAILED(Direct3D_Object->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &DisplayMode))) {
MessageBox(NULL, "Error setting the display mode.", "Error!", MB_ICONSTOP | MB_OK);
return false;
}
if(FAILED(Direct3D_Object->GetDeviceCaps(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &D3DCaps))) {
return false;
}
DWORD VertexProcessing = 0;
if(D3DCaps.VertexProcessingCaps != 0) {
VertexProcessing |= D3DCREATE_HARDWARE_VERTEXPROCESSING;
}
else {
VertexProcessing |= D3DCREATE_SOFTWARE_VERTEXPROCESSING;
}
if(fullscreen) {
Present_Parameters.Windowed = FALSE;
Present_Parameters.BackBufferWidth = 800;
Present_Parameters.BackBufferHeight = 600;
}
else {
Present_Parameters.Windowed = TRUE;
}
Present_Parameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
Present_Parameters.BackBufferFormat = DisplayMode.Format;
Present_Parameters.BackBufferCount = 1;
Present_Parameters.EnableAutoDepthStencil = TRUE;
Present_Parameters.AutoDepthStencilFormat = D3DFMT_D16;
if(FAILED(Direct3D_Object->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd, VertexProcessing, &Present_Parameters, &D3D_Device))) {
MessageBox(NULL, "CreateDevice() failed! Make sure you have DirectX 9.", "Error!", MB_ICONSTOP | MB_OK);
return false;
}
D3D_Device->SetRenderState(D3DRS_LIGHTING, false);
D3D_Device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
if(D3D_Device == NULL) {
MessageBox(NULL, "D3D_Device setup error", "Error!", MB_ICONSTOP | MB_OK);
return false;
}
if(!InitializeObject()) {
return false;
}
if(FAILED(D3DXCreateFont(D3D_Device, hfont, &Font))) {
return false;
}
FontPosition.top = 8;
FontPosition.left = 8;
FontPosition.right = 48;
FontPosition.bottom = 16;
return true;
}
bool InitializeObject()
{
Vertex Triangle_Data[6] =
{
{-0.5f, -0.5f, 0.0f, D3DCOLOR_XRGB(255,255,0)},
{0.5f, -0.5f, 0.0f, D3DCOLOR_XRGB(255,255,0)},
{0.0f, 0.3f, 0.0f, D3DCOLOR_XRGB(0,0,255)},
{-2.5f, -2.5f, 0.0f, D3DCOLOR_XRGB(255,255,0)},
{3.0f, 3.0f, 2.23f, D3DCOLOR_XRGB(100,100,100)},
{2.5f, -2.5f, 0.0f, D3DCOLOR_XRGB(0,0,255)}
};
if(FAILED(D3D_Device->CreateVertexBuffer(3 * sizeof(Vertex), 0, D3DFVF_D3DVertex, D3DPOOL_DEFAULT, &Vertex_Buffer, NULL))) {
return false;
}
Vertex* Vertices;
if(FAILED(Vertex_Buffer->Lock(0, sizeof(Triangle_Data), (void**)&Vertices, 0))) {
return false;
}
memcpy(Vertices, Triangle_Data, sizeof(Triangle_Data));
Vertex_Buffer->Unlock();
return true;
}
void RenderScence() {
D3D_Device->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0,0,0), 1.0f, 0);
D3D_Device->BeginScene();
Font->DrawText("Demo", 0, &FontPosition, DT_CENTER, 0xffffffff);
D3D_Device->SetStreamSource(0, Vertex_Buffer, 0, sizeof(Vertex));
D3D_Device->SetFVF(D3DFVF_D3DVertex);
D3D_Device->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1);
D3D_Device->EndScene();
D3D_Device->Present(NULL, NULL, NULL, NULL);
}
void ShutdownDirect3D() {
if(D3D_Device != NULL) {
D3D_Device->Release();
D3D_Device = NULL;
}
if(Direct3D_Object != NULL) {
Direct3D_Object->Release();
Direct3D_Object = NULL;
}
if(Vertex_Buffer != NULL) {
Vertex_Buffer->Release();
Vertex_Buffer = NULL;
}
if(Font) {
Font->Release();
Font = NULL;
}
}
I am using Dev-C++ 4.9.9.2.