1 /**************************************************************************
3 * Copyright 2012 Jose Fonseca
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 **************************************************************************/
32 static IDirect3D9 * g_pD3D = NULL;
33 static IDirect3DDevice9 * g_pDevice = NULL;
34 static D3DPRESENT_PARAMETERS g_PresentationParameters;
38 WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pCmdLine, int nCmdShow)
58 const int WindowWidth = 250;
59 const int WindowHeight = 250;
62 DWORD dwStyle = WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_OVERLAPPEDWINDOW;
64 RECT rect = {0, 0, WindowWidth, WindowHeight};
65 AdjustWindowRect(&rect, dwStyle, FALSE);
67 HWND hWnd = CreateWindow(wc.lpszClassName,
68 "Simple example using DirectX9",
70 CW_USEDEFAULT, CW_USEDEFAULT,
71 rect.right - rect.left,
72 rect.bottom - rect.top,
81 ShowWindow(hWnd, SW_SHOW);
83 g_pD3D = Direct3DCreate9(D3D_SDK_VERSION);
89 hr = g_pD3D->GetDeviceCaps(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &caps);
94 DWORD dwBehaviorFlags;
95 if ((caps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT) == 0 ||
96 caps.VertexShaderVersion < D3DVS_VERSION(1, 1)) {
97 dwBehaviorFlags = D3DCREATE_SOFTWARE_VERTEXPROCESSING;
99 dwBehaviorFlags = D3DCREATE_HARDWARE_VERTEXPROCESSING;
102 ZeroMemory(&g_PresentationParameters, sizeof g_PresentationParameters);
103 g_PresentationParameters.Windowed = Windowed;
105 g_PresentationParameters.BackBufferWidth = WindowWidth;
106 g_PresentationParameters.BackBufferHeight = WindowHeight;
108 g_PresentationParameters.BackBufferCount = 1;
109 g_PresentationParameters.SwapEffect = D3DSWAPEFFECT_FLIP;
111 g_PresentationParameters.BackBufferFormat = D3DFMT_X8R8G8B8;
113 g_PresentationParameters.BackBufferFormat = D3DFMT_UNKNOWN;
115 g_PresentationParameters.hDeviceWindow = hWnd;
117 g_PresentationParameters.EnableAutoDepthStencil = FALSE;
118 g_PresentationParameters.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
120 hr = g_pD3D->CreateDevice(D3DADAPTER_DEFAULT,
124 &g_PresentationParameters,
138 D3DCOLOR clearColor = D3DCOLOR_COLORVALUE(0.3f, 0.1f, 0.3f, 1.0f);
139 g_pDevice->Clear(0, NULL, D3DCLEAR_TARGET, clearColor, 1.0f, 0);
140 g_pDevice->BeginScene();
142 g_pDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
143 g_pDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
145 static const Vertex vertices[] = {
146 { -0.9f, -0.9f, 0.5f, D3DCOLOR_COLORVALUE(0.8f, 0.0f, 0.0f, 0.1f) },
147 { 0.9f, -0.9f, 0.5f, D3DCOLOR_COLORVALUE(0.0f, 0.9f, 0.0f, 0.1f) },
148 { 0.0f, 0.9f, 0.5f, D3DCOLOR_COLORVALUE(0.0f, 0.0f, 0.7f, 0.1f) },
151 g_pDevice->SetFVF(D3DFVF_XYZ | D3DFVF_DIFFUSE);
152 g_pDevice->DrawPrimitiveUP(D3DPT_TRIANGLELIST, 1, vertices, sizeof(Vertex));
154 g_pDevice->EndScene();
156 g_pDevice->Present(NULL, NULL, NULL, NULL);
159 g_pDevice->Release();