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 **************************************************************************/
33 #include "tri_vs_2_0.h"
34 #include "tri_ps_2_0.h"
37 static IDirect3D9 * g_pD3D = NULL;
38 static IDirect3DDevice9 * g_pDevice = NULL;
39 static D3DPRESENT_PARAMETERS g_PresentationParameters;
43 main(int argc, char *argv[])
47 HINSTANCE hInstance = GetModuleHandle(NULL);
65 const int WindowWidth = 250;
66 const int WindowHeight = 250;
69 DWORD dwStyle = WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_OVERLAPPEDWINDOW;
71 RECT rect = {0, 0, WindowWidth, WindowHeight};
72 AdjustWindowRect(&rect, dwStyle, FALSE);
74 HWND hWnd = CreateWindow(wc.lpszClassName,
75 "Simple example using DirectX9",
77 CW_USEDEFAULT, CW_USEDEFAULT,
78 rect.right - rect.left,
79 rect.bottom - rect.top,
88 ShowWindow(hWnd, SW_SHOW);
90 g_pD3D = Direct3DCreate9(D3D_SDK_VERSION);
96 hr = g_pD3D->GetDeviceCaps(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &caps);
101 DWORD dwBehaviorFlags;
102 if ((caps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT) == 0 ||
103 caps.VertexShaderVersion < D3DVS_VERSION(1, 1)) {
104 dwBehaviorFlags = D3DCREATE_SOFTWARE_VERTEXPROCESSING;
106 dwBehaviorFlags = D3DCREATE_HARDWARE_VERTEXPROCESSING;
109 ZeroMemory(&g_PresentationParameters, sizeof g_PresentationParameters);
110 g_PresentationParameters.Windowed = Windowed;
112 g_PresentationParameters.BackBufferWidth = WindowWidth;
113 g_PresentationParameters.BackBufferHeight = WindowHeight;
115 g_PresentationParameters.BackBufferCount = 1;
116 g_PresentationParameters.SwapEffect = D3DSWAPEFFECT_FLIP;
118 g_PresentationParameters.BackBufferFormat = D3DFMT_X8R8G8B8;
120 g_PresentationParameters.BackBufferFormat = D3DFMT_UNKNOWN;
122 g_PresentationParameters.hDeviceWindow = hWnd;
124 g_PresentationParameters.EnableAutoDepthStencil = FALSE;
125 g_PresentationParameters.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
127 hr = g_pD3D->CreateDevice(D3DADAPTER_DEFAULT,
131 &g_PresentationParameters,
137 D3DCOLOR clearColor = D3DCOLOR_COLORVALUE(0.3f, 0.1f, 0.3f, 1.0f);
138 g_pDevice->Clear(0, NULL, D3DCLEAR_TARGET, clearColor, 1.0f, 0);
145 static const Vertex vertices[] = {
146 { { -0.9f, -0.9f, 0.5f, 1.0f}, { 0.8f, 0.0f, 0.0f, 0.1f } },
147 { { 0.9f, -0.9f, 0.5f, 1.0f}, { 0.0f, 0.9f, 0.0f, 0.1f } },
148 { { 0.0f, 0.9f, 0.5f, 1.0f}, { 0.0f, 0.0f, 0.7f, 0.1f } },
151 static const D3DVERTEXELEMENT9 VertexElements[] = {
152 { 0, offsetof(Vertex, position), D3DDECLTYPE_FLOAT4, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 },
153 { 0, offsetof(Vertex, color), D3DDECLTYPE_FLOAT4, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 0 },
157 LPDIRECT3DVERTEXDECLARATION9 pVertexDeclaration = NULL;
158 hr = g_pDevice->CreateVertexDeclaration(VertexElements, &pVertexDeclaration);
162 g_pDevice->SetVertexDeclaration(pVertexDeclaration);
164 LPDIRECT3DVERTEXSHADER9 pVertexShader = NULL;
165 hr = g_pDevice->CreateVertexShader((CONST DWORD *)g_vs20_VS, &pVertexShader);
169 g_pDevice->SetVertexShader(pVertexShader);
171 LPDIRECT3DPIXELSHADER9 pPixelShader = NULL;
172 hr = g_pDevice->CreatePixelShader((CONST DWORD *)g_ps20_PS, &pPixelShader);
176 g_pDevice->SetPixelShader(pPixelShader);
178 g_pDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
180 g_pDevice->BeginScene();
182 g_pDevice->DrawPrimitiveUP(D3DPT_TRIANGLELIST, 1, vertices, sizeof(Vertex));
184 g_pDevice->EndScene();
186 g_pDevice->Present(NULL, NULL, NULL, NULL);
188 pPixelShader->Release();
191 pVertexShader->Release();
192 pVertexShader = NULL;
194 pVertexDeclaration->Release();
195 pVertexDeclaration = NULL;
197 g_pDevice->Release();