]> git.cworth.org Git - apitrace-tests/blob - apps/d3d7/tri.cpp
D3D7 sample.
[apitrace-tests] / apps / d3d7 / 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 <initguid.h>
28
29 #include <windows.h>
30
31 #include <d3d.h>
32
33
34 static LPDIRECTDRAW7 g_pDD = NULL;
35 static LPDIRECT3D7 g_pD3D = NULL;
36 static LPDIRECT3DDEVICE7 g_pDevice = NULL;
37 static LPDIRECTDRAWSURFACE7 g_pddsPrimary;
38 static LPDIRECTDRAWSURFACE7 g_pddsBackBuffer;
39 static RECT g_rcScreenRect;
40 static RECT g_rcViewportRect;
41
42
43 int main(int argc, char *argv[])
44 {
45     HRESULT hr;
46
47     HINSTANCE hInstance = GetModuleHandle(NULL);
48
49     WNDCLASSEX wc = {
50         sizeof(WNDCLASSEX),
51         CS_CLASSDC,
52         DefWindowProc,
53         0,
54         0,
55         hInstance,
56         NULL,
57         NULL,
58         NULL,
59         NULL,
60         "SimpleDX7",
61         NULL
62     };
63     RegisterClassEx(&wc);
64
65     const int WindowWidth = 250;
66     const int WindowHeight = 250;
67     const int WindowDepth = 4;
68     BOOL Windowed = TRUE;
69
70     DWORD dwStyle = WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_OVERLAPPEDWINDOW;
71
72     RECT rect = {0, 0, WindowWidth, WindowHeight};
73     AdjustWindowRect(&rect, dwStyle, FALSE);
74
75     HWND hWnd = CreateWindow(wc.lpszClassName,
76                              "Simple example using DirectX7",
77                              dwStyle,
78                              CW_USEDEFAULT, CW_USEDEFAULT,
79                              rect.right - rect.left,
80                              rect.bottom - rect.top,
81                              NULL,
82                              NULL,
83                              hInstance,
84                              NULL);
85     if (!hWnd) {
86         return 1;
87     }
88
89     ShowWindow(hWnd, SW_SHOW);
90
91     hr = DirectDrawCreateEx(NULL, (void **)&g_pDD, IID_IDirectDraw7, NULL);
92     if (FAILED(hr)) {
93         return 1;
94     }
95
96     hr = g_pDD->SetCooperativeLevel(hWnd, DDSCL_NORMAL);
97     if (FAILED(hr)) {
98         return 1;
99     }
100
101     /*
102      * Create primary
103      */
104
105     DDSURFACEDESC2 ddsd;
106     ZeroMemory(&ddsd, sizeof ddsd);
107     ddsd.dwSize         = sizeof ddsd;
108     ddsd.dwFlags        = DDSD_CAPS;
109     ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
110
111     hr = g_pDD->CreateSurface(&ddsd, &g_pddsPrimary, NULL);
112     if (FAILED(hr)) {
113         return 1;
114     }
115
116     /*
117      * Create backbuffer
118      */
119
120     ddsd.dwFlags        = DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS;
121     ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_3DDEVICE;
122
123     GetClientRect(hWnd, &g_rcScreenRect);
124     GetClientRect(hWnd, &g_rcViewportRect);
125     ClientToScreen(hWnd, (POINT*)&g_rcScreenRect.left);
126     ClientToScreen(hWnd, (POINT*)&g_rcScreenRect.right);
127     ddsd.dwWidth  = g_rcScreenRect.right - g_rcScreenRect.left;
128     ddsd.dwHeight = g_rcScreenRect.bottom - g_rcScreenRect.top;
129
130     hr = g_pDD->CreateSurface(&ddsd, &g_pddsBackBuffer, NULL);
131     if (FAILED(hr)) {
132         return 1;
133     }
134
135     LPDIRECTDRAWCLIPPER pcClipper;
136     hr = g_pDD->CreateClipper(0, &pcClipper, NULL);
137     if (FAILED(hr)) {
138         return 1;
139     }
140
141     pcClipper->SetHWnd(0, hWnd);
142     g_pddsPrimary->SetClipper(pcClipper);
143     pcClipper->Release();
144
145     /*
146      * Initialize D3D
147      */
148
149     hr = g_pDD->QueryInterface(IID_IDirect3D7, (void **)&g_pD3D);
150     if (FAILED(hr)) {
151         return 1;
152     }
153
154     hr = g_pD3D->CreateDevice(IID_IDirect3DHALDevice, g_pddsBackBuffer, &g_pDevice);
155     if (FAILED(hr)) {
156         g_pD3D->Release();
157         g_pD3D = NULL;
158         return 1;
159     }
160
161     struct Vertex {
162         float x, y, z;
163         DWORD color;
164     };
165
166
167     D3DCOLOR clearColor = D3DRGBA(0.3f, 0.1f, 0.3f, 1.0f);
168     g_pDevice->Clear(0, NULL, D3DCLEAR_TARGET, clearColor, 1.0f, 0);
169     g_pDevice->BeginScene();
170
171     g_pDevice->SetRenderState(D3DRENDERSTATE_LIGHTING, FALSE);
172     g_pDevice->SetRenderState(D3DRENDERSTATE_CULLMODE, D3DCULL_NONE);
173
174     static Vertex vertices[] = {
175         { -0.9f, -0.9f, 0.5f, D3DRGBA(0.8f, 0.0f, 0.0f, 0.1f) },
176         {  0.9f, -0.9f, 0.5f, D3DRGBA(0.0f, 0.9f, 0.0f, 0.1f) },
177         {  0.0f,  0.9f, 0.5f, D3DRGBA(0.0f, 0.0f, 0.7f, 0.1f) },
178     };
179
180     g_pDevice->DrawPrimitive(D3DPT_TRIANGLELIST, D3DFVF_XYZ | D3DFVF_DIFFUSE, vertices, 3, 0);
181
182     g_pDevice->EndScene();
183
184     /*
185      * Present
186      */
187     hr = g_pddsPrimary->Blt(&g_rcScreenRect, g_pddsBackBuffer, &g_rcViewportRect, DDBLT_WAIT, NULL);
188     if (FAILED(hr)) {
189         return 1;
190     }
191
192     g_pDevice->Release();
193     g_pDevice = NULL;
194     g_pD3D->Release();
195     g_pD3D = NULL;
196
197     DestroyWindow(hWnd);
198
199     return 0;
200 }
201