]> git.cworth.org Git - apitrace-tests/blob - apps/d3d8/tri.cpp
Add --exact, --no-deps, or --no-prune to trim tests as needed
[apitrace-tests] / apps / d3d8 / 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 <d3d8.h>
30
31
32 static IDirect3D8 * g_pD3D = NULL;
33 static IDirect3DDevice8 * g_pDevice = NULL;
34 static D3DPRESENT_PARAMETERS g_PresentationParameters;
35
36
37 int main(int argc, char *argv[])
38 {
39     HRESULT hr;
40
41     HINSTANCE hInstance = GetModuleHandle(NULL);
42
43     WNDCLASSEX wc = {
44         sizeof(WNDCLASSEX),
45         CS_CLASSDC,
46         DefWindowProc,
47         0,
48         0,
49         hInstance,
50         NULL,
51         NULL,
52         NULL,
53         NULL,
54         "SimpleDX8",
55         NULL
56     };
57     RegisterClassEx(&wc);
58
59     const int WindowWidth = 250;
60     const int WindowHeight = 250;
61     BOOL Windowed = TRUE;
62
63     DWORD dwStyle = WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_OVERLAPPEDWINDOW;
64
65     RECT rect = {0, 0, WindowWidth, WindowHeight};
66     AdjustWindowRect(&rect, dwStyle, FALSE);
67
68     HWND hWnd = CreateWindow(wc.lpszClassName,
69                              "Simple example using DirectX8",
70                              dwStyle,
71                              CW_USEDEFAULT, CW_USEDEFAULT,
72                              rect.right - rect.left,
73                              rect.bottom - rect.top,
74                              NULL,
75                              NULL,
76                              hInstance,
77                              NULL);
78     if (!hWnd) {
79         return 1;
80     }
81
82     ShowWindow(hWnd, SW_SHOW);
83
84     g_pD3D = Direct3DCreate8(D3D_SDK_VERSION);
85     if (!g_pD3D) {
86         return 1;
87     }
88
89     D3DCAPS8 caps;
90     hr = g_pD3D->GetDeviceCaps(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &caps);
91     if (FAILED(hr)) {
92        return 1;
93     }
94
95     DWORD dwBehaviorFlags;
96     if ((caps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT) == 0 ||
97         caps.VertexShaderVersion < D3DVS_VERSION(1, 1)) {
98        dwBehaviorFlags = D3DCREATE_SOFTWARE_VERTEXPROCESSING;
99     } else {
100        dwBehaviorFlags = D3DCREATE_HARDWARE_VERTEXPROCESSING;
101     }
102
103     D3DDISPLAYMODE Mode;
104     hr = g_pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &Mode);
105     if (FAILED(hr)) {
106         return 1;
107     }
108
109     ZeroMemory(&g_PresentationParameters, sizeof g_PresentationParameters);
110     g_PresentationParameters.Windowed = Windowed;
111     if (Windowed) {
112         g_PresentationParameters.BackBufferWidth = WindowWidth;
113         g_PresentationParameters.BackBufferHeight = WindowHeight;
114     } else {
115         g_PresentationParameters.BackBufferWidth = Mode.Width;
116         g_PresentationParameters.BackBufferHeight = Mode.Height;
117     }
118     g_PresentationParameters.BackBufferFormat = Mode.Format;
119     g_PresentationParameters.BackBufferCount = 1;
120     g_PresentationParameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
121     if (!Windowed) {
122         g_PresentationParameters.FullScreen_PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
123     }
124     g_PresentationParameters.hDeviceWindow = hWnd;
125
126     g_PresentationParameters.EnableAutoDepthStencil = FALSE;
127
128     hr = g_pD3D->CreateDevice(D3DADAPTER_DEFAULT,
129                               D3DDEVTYPE_HAL,
130                               hWnd,
131                               dwBehaviorFlags,
132                               &g_PresentationParameters,
133                               &g_pDevice);
134     if (FAILED(hr)) {
135         g_pD3D->Release();
136         g_pD3D = NULL;
137         return 1;
138     }
139
140     struct Vertex {
141         float x, y, z;
142         DWORD color;
143     };
144
145
146     D3DCOLOR clearColor = D3DCOLOR_COLORVALUE(0.3f, 0.1f, 0.3f, 1.0f);
147     g_pDevice->Clear(0, NULL, D3DCLEAR_TARGET, clearColor, 1.0f, 0);
148     g_pDevice->BeginScene();
149
150     g_pDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
151     g_pDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
152
153     static const Vertex vertices[] = {
154         { -0.9f, -0.9f, 0.5f, D3DCOLOR_COLORVALUE(0.8f, 0.0f, 0.0f, 0.1f) },
155         {  0.9f, -0.9f, 0.5f, D3DCOLOR_COLORVALUE(0.0f, 0.9f, 0.0f, 0.1f) },
156         {  0.0f,  0.9f, 0.5f, D3DCOLOR_COLORVALUE(0.0f, 0.0f, 0.7f, 0.1f) },
157     };
158
159     g_pDevice->SetVertexShader(D3DFVF_XYZ | D3DFVF_DIFFUSE);
160     g_pDevice->DrawPrimitiveUP(D3DPT_TRIANGLELIST, 1, vertices, sizeof(Vertex));
161
162     g_pDevice->EndScene();
163
164     g_pDevice->Present(NULL, NULL, NULL, NULL);
165
166     g_pDevice->Release();
167     g_pDevice = NULL;
168     g_pD3D->Release();
169     g_pD3D = NULL;
170
171     DestroyWindow(hWnd);
172
173     return 0;
174 }
175