]> git.cworth.org Git - apitrace-tests/blob - apps/d3d9/tri.cpp
8c3fda7baa76e6ae7151837a85903a24ba7afc19
[apitrace-tests] / apps / d3d9 / tri.cpp
1 /**************************************************************************
2  *
3  * Copyright 2012 Jose Fonseca
4  * All Rights Reserved.
5  *
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:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
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
22  * THE SOFTWARE.
23  *
24  **************************************************************************/
25
26
27 #include <windows.h>
28
29 #include <d3d9.h>
30
31
32 static IDirect3D9 * g_pD3D = NULL;
33 static IDirect3DDevice9 * g_pDevice = NULL;
34 static D3DPRESENT_PARAMETERS g_PresentationParameters;
35
36
37 int
38 main(int argc, char *argv[])
39 {
40     HRESULT hr;
41
42     HINSTANCE hInstance = GetModuleHandle(NULL);
43
44     WNDCLASSEX wc = {
45         sizeof(WNDCLASSEX),
46         CS_CLASSDC,
47         DefWindowProc,
48         0,
49         0,
50         hInstance,
51         NULL,
52         NULL,
53         NULL,
54         NULL,
55         "SimpleDX9",
56         NULL
57     };
58     RegisterClassEx(&wc);
59
60     const int WindowWidth = 250;
61     const int WindowHeight = 250;
62     BOOL Windowed = TRUE;
63
64     DWORD dwStyle = WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_OVERLAPPEDWINDOW;
65
66     RECT rect = {0, 0, WindowWidth, WindowHeight};
67     AdjustWindowRect(&rect, dwStyle, FALSE);
68
69     HWND hWnd = CreateWindow(wc.lpszClassName,
70                              "Simple example using DirectX9",
71                              dwStyle,
72                              CW_USEDEFAULT, CW_USEDEFAULT,
73                              rect.right - rect.left,
74                              rect.bottom - rect.top,
75                              NULL,
76                              NULL,
77                              hInstance,
78                              NULL);
79     if (!hWnd) {
80         return 1;
81     }
82
83     ShowWindow(hWnd, SW_SHOW);
84
85     g_pD3D = Direct3DCreate9(D3D_SDK_VERSION);
86     if (!g_pD3D) {
87         return 1;
88     }
89
90     D3DCAPS9 caps;
91     hr = g_pD3D->GetDeviceCaps(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &caps);
92     if (FAILED(hr)) {
93        return 1;
94     }
95
96     DWORD dwBehaviorFlags;
97     if ((caps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT) == 0 ||
98         caps.VertexShaderVersion < D3DVS_VERSION(1, 1)) {
99        dwBehaviorFlags = D3DCREATE_SOFTWARE_VERTEXPROCESSING;
100     } else {
101        dwBehaviorFlags = D3DCREATE_HARDWARE_VERTEXPROCESSING;
102     }
103
104     ZeroMemory(&g_PresentationParameters, sizeof g_PresentationParameters);
105     g_PresentationParameters.Windowed = Windowed;
106     if (!Windowed) {
107         g_PresentationParameters.BackBufferWidth = WindowWidth;
108         g_PresentationParameters.BackBufferHeight = WindowHeight;
109     }
110     g_PresentationParameters.BackBufferCount = 1;
111     g_PresentationParameters.SwapEffect = D3DSWAPEFFECT_FLIP;
112     if (!Windowed) {
113         g_PresentationParameters.BackBufferFormat = D3DFMT_X8R8G8B8;
114     } else {
115         g_PresentationParameters.BackBufferFormat = D3DFMT_UNKNOWN;
116     }
117     g_PresentationParameters.hDeviceWindow = hWnd;
118
119     g_PresentationParameters.EnableAutoDepthStencil = FALSE;
120     g_PresentationParameters.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
121
122     hr = g_pD3D->CreateDevice(D3DADAPTER_DEFAULT,
123                               D3DDEVTYPE_HAL,
124                               hWnd,
125                               dwBehaviorFlags,
126                               &g_PresentationParameters,
127                               &g_pDevice);
128     if (FAILED(hr)) {
129         g_pD3D->Release();
130         g_pD3D = NULL;
131         return 1;
132     }
133
134     struct Vertex {
135         float x, y, z;
136         DWORD color;
137     };
138
139
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();
143
144     g_pDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
145     g_pDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
146
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) },
151     };
152
153     g_pDevice->SetFVF(D3DFVF_XYZ | D3DFVF_DIFFUSE);
154     g_pDevice->DrawPrimitiveUP(D3DPT_TRIANGLELIST, 1, vertices, sizeof(Vertex));
155
156     g_pDevice->EndScene();
157
158     g_pDevice->Present(NULL, NULL, NULL, NULL);
159
160
161     g_pDevice->Release();
162     g_pDevice = NULL;
163     g_pD3D->Release();
164     g_pD3D = NULL;
165
166     DestroyWindow(hWnd);
167
168     return 0;
169 }
170