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 IDirect3D8 * g_pD3D = NULL;
33 static IDirect3DDevice8 * g_pDevice = NULL;
34 static D3DPRESENT_PARAMETERS g_PresentationParameters;
37 int main(int argc, char *argv[])
41 HINSTANCE hInstance = GetModuleHandle(NULL);
59 const int WindowWidth = 250;
60 const int WindowHeight = 250;
63 DWORD dwStyle = WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_OVERLAPPEDWINDOW;
65 RECT rect = {0, 0, WindowWidth, WindowHeight};
66 AdjustWindowRect(&rect, dwStyle, FALSE);
68 HWND hWnd = CreateWindow(wc.lpszClassName,
69 "Simple example using DirectX8",
71 CW_USEDEFAULT, CW_USEDEFAULT,
72 rect.right - rect.left,
73 rect.bottom - rect.top,
82 ShowWindow(hWnd, SW_SHOW);
84 g_pD3D = Direct3DCreate8(D3D_SDK_VERSION);
90 hr = g_pD3D->GetDeviceCaps(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &caps);
95 DWORD dwBehaviorFlags;
96 if ((caps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT) == 0 ||
97 caps.VertexShaderVersion < D3DVS_VERSION(1, 1)) {
98 dwBehaviorFlags = D3DCREATE_SOFTWARE_VERTEXPROCESSING;
100 dwBehaviorFlags = D3DCREATE_HARDWARE_VERTEXPROCESSING;
103 ZeroMemory(&g_PresentationParameters, sizeof g_PresentationParameters);
104 g_PresentationParameters.Windowed = Windowed;
106 g_PresentationParameters.BackBufferWidth = WindowWidth;
107 g_PresentationParameters.BackBufferHeight = WindowHeight;
109 g_PresentationParameters.BackBufferCount = 1;
110 g_PresentationParameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
112 // Must matche the format of the current display mode
114 hr = g_pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &Mode);
120 g_PresentationParameters.BackBufferFormat = Mode.Format;
122 g_PresentationParameters.BackBufferFormat = D3DFMT_X8R8G8B8;
123 g_PresentationParameters.FullScreen_PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
125 g_PresentationParameters.hDeviceWindow = hWnd;
127 g_PresentationParameters.EnableAutoDepthStencil = FALSE;
129 hr = g_pD3D->CreateDevice(D3DADAPTER_DEFAULT,
133 &g_PresentationParameters,
147 D3DCOLOR clearColor = D3DCOLOR_COLORVALUE(0.3f, 0.1f, 0.3f, 1.0f);
148 g_pDevice->Clear(0, NULL, D3DCLEAR_TARGET, clearColor, 1.0f, 0);
149 g_pDevice->BeginScene();
151 g_pDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
152 g_pDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
154 static const Vertex vertices[] = {
155 { -0.9f, -0.9f, 0.5f, D3DCOLOR_COLORVALUE(0.8f, 0.0f, 0.0f, 0.1f) },
156 { 0.9f, -0.9f, 0.5f, D3DCOLOR_COLORVALUE(0.0f, 0.9f, 0.0f, 0.1f) },
157 { 0.0f, 0.9f, 0.5f, D3DCOLOR_COLORVALUE(0.0f, 0.0f, 0.7f, 0.1f) },
160 g_pDevice->SetVertexShader(D3DFVF_XYZ | D3DFVF_DIFFUSE);
161 g_pDevice->DrawPrimitiveUP(D3DPT_TRIANGLELIST, 1, vertices, sizeof(Vertex));
163 g_pDevice->EndScene();
165 g_pDevice->Present(NULL, NULL, NULL, NULL);
167 g_pDevice->Release();