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 main(int argc, char *argv[])
42 HINSTANCE hInstance = GetModuleHandle(NULL);
60 const int WindowWidth = 250;
61 const int WindowHeight = 250;
64 DWORD dwStyle = WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_OVERLAPPEDWINDOW;
66 RECT rect = {0, 0, WindowWidth, WindowHeight};
67 AdjustWindowRect(&rect, dwStyle, FALSE);
69 HWND hWnd = CreateWindow(wc.lpszClassName,
70 "Simple example using DirectX9",
72 CW_USEDEFAULT, CW_USEDEFAULT,
73 rect.right - rect.left,
74 rect.bottom - rect.top,
83 ShowWindow(hWnd, SW_SHOW);
85 g_pD3D = Direct3DCreate9(D3D_SDK_VERSION);
91 hr = g_pD3D->GetDeviceCaps(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &caps);
96 DWORD dwBehaviorFlags;
97 if ((caps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT) == 0 ||
98 caps.VertexShaderVersion < D3DVS_VERSION(1, 1)) {
99 dwBehaviorFlags = D3DCREATE_SOFTWARE_VERTEXPROCESSING;
101 dwBehaviorFlags = D3DCREATE_HARDWARE_VERTEXPROCESSING;
104 ZeroMemory(&g_PresentationParameters, sizeof g_PresentationParameters);
105 g_PresentationParameters.Windowed = Windowed;
107 g_PresentationParameters.BackBufferWidth = WindowWidth;
108 g_PresentationParameters.BackBufferHeight = WindowHeight;
110 g_PresentationParameters.BackBufferCount = 1;
111 g_PresentationParameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
113 g_PresentationParameters.BackBufferFormat = D3DFMT_X8R8G8B8;
115 g_PresentationParameters.BackBufferFormat = D3DFMT_UNKNOWN;
117 g_PresentationParameters.hDeviceWindow = hWnd;
119 g_PresentationParameters.EnableAutoDepthStencil = FALSE;
120 g_PresentationParameters.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
122 hr = g_pD3D->CreateDevice(D3DADAPTER_DEFAULT,
126 &g_PresentationParameters,
140 D3DCOLOR clearColor = D3DCOLOR_COLORVALUE(0.3f, 0.1f, 0.3f, 1.0f);
141 g_pDevice->Clear(0, NULL, D3DCLEAR_TARGET, clearColor, 1.0f, 0);
142 g_pDevice->BeginScene();
144 g_pDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
145 g_pDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
147 static const Vertex vertices[] = {
148 { -0.9f, -0.9f, 0.5f, D3DCOLOR_COLORVALUE(0.8f, 0.0f, 0.0f, 0.1f) },
149 { 0.9f, -0.9f, 0.5f, D3DCOLOR_COLORVALUE(0.0f, 0.9f, 0.0f, 0.1f) },
150 { 0.0f, 0.9f, 0.5f, D3DCOLOR_COLORVALUE(0.0f, 0.0f, 0.7f, 0.1f) },
153 g_pDevice->SetFVF(D3DFVF_XYZ | D3DFVF_DIFFUSE);
154 g_pDevice->DrawPrimitiveUP(D3DPT_TRIANGLELIST, 1, vertices, sizeof(Vertex));
156 g_pDevice->EndScene();
158 g_pDevice->Present(NULL, NULL, NULL, NULL);
161 g_pDevice->Release();