]> git.cworth.org Git - apitrace-tests/blob - apps/d3d9/tri.cpp
816673497027b8c9421642139caa8d8047f930d5
[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 WINAPI
38 WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pCmdLine, int nCmdShow)
39 {
40     HRESULT hr;
41
42     WNDCLASSEX wc = {
43         sizeof(WNDCLASSEX),
44         CS_CLASSDC,
45         DefWindowProc,
46         0,
47         0,
48         hInstance,
49         NULL,
50         NULL,
51         NULL,
52         NULL,
53         "SimpleDX9",
54         NULL
55     };
56     RegisterClassEx(&wc);
57
58     const int WindowWidth = 250;
59     const int WindowHeight = 250;
60     BOOL Windowed = TRUE;
61
62     DWORD dwStyle = WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_OVERLAPPEDWINDOW;
63
64     RECT rect = {0, 0, WindowWidth, WindowHeight};
65     AdjustWindowRect(&rect, dwStyle, FALSE);
66
67     HWND hWnd = CreateWindow(wc.lpszClassName,
68                              "Simple example using DirectX9",
69                              dwStyle,
70                              CW_USEDEFAULT, CW_USEDEFAULT,
71                              rect.right - rect.left,
72                              rect.bottom - rect.top,
73                              NULL,
74                              NULL,
75                              hInstance,
76                              NULL);
77     if (!hWnd) {
78         return 1;
79     }
80
81     ShowWindow(hWnd, SW_SHOW);
82
83     g_pD3D = Direct3DCreate9(D3D_SDK_VERSION);
84     if (!g_pD3D) {
85         return 1;
86     }
87
88     D3DCAPS9 caps;
89     hr = g_pD3D->GetDeviceCaps(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &caps);
90     if (FAILED(hr)) {
91        return 1;
92     }
93
94     DWORD dwBehaviorFlags;
95     if ((caps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT) == 0 ||
96         caps.VertexShaderVersion < D3DVS_VERSION(1, 1)) {
97        dwBehaviorFlags = D3DCREATE_SOFTWARE_VERTEXPROCESSING;
98     } else {
99        dwBehaviorFlags = D3DCREATE_HARDWARE_VERTEXPROCESSING;
100     }
101
102     ZeroMemory(&g_PresentationParameters, sizeof g_PresentationParameters);
103     g_PresentationParameters.Windowed = Windowed;
104     if (!Windowed) {
105         g_PresentationParameters.BackBufferWidth = WindowWidth;
106         g_PresentationParameters.BackBufferHeight = WindowHeight;
107     }
108     g_PresentationParameters.BackBufferCount = 1;
109     g_PresentationParameters.SwapEffect = D3DSWAPEFFECT_FLIP;
110     if (!Windowed) {
111         g_PresentationParameters.BackBufferFormat = D3DFMT_X8R8G8B8;
112     } else {
113         g_PresentationParameters.BackBufferFormat = D3DFMT_UNKNOWN;
114     }
115     g_PresentationParameters.hDeviceWindow = hWnd;
116
117     g_PresentationParameters.EnableAutoDepthStencil = FALSE;
118     g_PresentationParameters.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
119
120     hr = g_pD3D->CreateDevice(D3DADAPTER_DEFAULT,
121                               D3DDEVTYPE_HAL,
122                               hWnd,
123                               dwBehaviorFlags,
124                               &g_PresentationParameters,
125                               &g_pDevice);
126     if (FAILED(hr)) {
127         g_pD3D->Release();
128         g_pD3D = NULL;
129         return 1;
130     }
131
132     struct Vertex {
133         float x, y, z;
134         DWORD color;
135     };
136
137
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();
141
142     g_pDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
143     g_pDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
144
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) },
149     };
150
151     g_pDevice->SetFVF(D3DFVF_XYZ | D3DFVF_DIFFUSE);
152     g_pDevice->DrawPrimitiveUP(D3DPT_TRIANGLELIST, 1, vertices, sizeof(Vertex));
153
154     g_pDevice->EndScene();
155
156     g_pDevice->Present(NULL, NULL, NULL, NULL);
157
158
159     g_pDevice->Release();
160     g_pDevice = NULL;
161     g_pD3D->Release();
162     g_pD3D = NULL;
163
164     DestroyWindow(hWnd);
165
166     return 0;
167 }
168