]> git.cworth.org Git - apitrace-tests/blob - apps/d3d8/tri.cpp
156d0c5e800d8ded9f252a0dd1a769870d06a887
[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     ZeroMemory(&g_PresentationParameters, sizeof g_PresentationParameters);
104     g_PresentationParameters.Windowed = Windowed;
105     if (!Windowed) {
106         g_PresentationParameters.BackBufferWidth = WindowWidth;
107         g_PresentationParameters.BackBufferHeight = WindowHeight;
108     }
109     g_PresentationParameters.BackBufferCount = 1;
110     g_PresentationParameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
111     if (Windowed) {
112         // Must matche the format of the current display mode
113         D3DDISPLAYMODE Mode;
114         hr = g_pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &Mode);
115         if (FAILED(hr)) {
116             g_pD3D->Release();
117             g_pD3D = NULL;
118             return 1;
119         }
120         g_PresentationParameters.BackBufferFormat = Mode.Format;
121     } else {
122         g_PresentationParameters.BackBufferFormat = D3DFMT_X8R8G8B8;
123         g_PresentationParameters.FullScreen_PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
124     }
125     g_PresentationParameters.hDeviceWindow = hWnd;
126
127     g_PresentationParameters.EnableAutoDepthStencil = FALSE;
128
129     hr = g_pD3D->CreateDevice(D3DADAPTER_DEFAULT,
130                               D3DDEVTYPE_HAL,
131                               hWnd,
132                               dwBehaviorFlags,
133                               &g_PresentationParameters,
134                               &g_pDevice);
135     if (FAILED(hr)) {
136         g_pD3D->Release();
137         g_pD3D = NULL;
138         return 1;
139     }
140
141     struct Vertex {
142         float x, y, z;
143         DWORD color;
144     };
145
146
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();
150
151     g_pDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
152     g_pDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
153
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) },
158     };
159
160     g_pDevice->SetVertexShader(D3DFVF_XYZ | D3DFVF_DIFFUSE);
161     g_pDevice->DrawPrimitiveUP(D3DPT_TRIANGLELIST, 1, vertices, sizeof(Vertex));
162
163     g_pDevice->EndScene();
164
165     g_pDevice->Present(NULL, NULL, NULL, NULL);
166
167     g_pDevice->Release();
168     g_pDevice = NULL;
169     g_pD3D->Release();
170     g_pD3D = NULL;
171
172     DestroyWindow(hWnd);
173
174     return 0;
175 }
176