1 /**************************************************************************
3 * Copyright 2012 Jose Fonseca
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
24 **************************************************************************/
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;
43 int main(int argc, char *argv[])
47 HINSTANCE hInstance = GetModuleHandle(NULL);
65 const int WindowWidth = 250;
66 const int WindowHeight = 250;
67 const int WindowDepth = 4;
70 DWORD dwStyle = WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_OVERLAPPEDWINDOW;
72 RECT rect = {0, 0, WindowWidth, WindowHeight};
73 AdjustWindowRect(&rect, dwStyle, FALSE);
75 HWND hWnd = CreateWindow(wc.lpszClassName,
76 "Simple example using DirectX7",
78 CW_USEDEFAULT, CW_USEDEFAULT,
79 rect.right - rect.left,
80 rect.bottom - rect.top,
89 ShowWindow(hWnd, SW_SHOW);
91 hr = DirectDrawCreateEx(NULL, (void **)&g_pDD, IID_IDirectDraw7, NULL);
96 hr = g_pDD->SetCooperativeLevel(hWnd, DDSCL_NORMAL);
106 ZeroMemory(&ddsd, sizeof ddsd);
107 ddsd.dwSize = sizeof ddsd;
108 ddsd.dwFlags = DDSD_CAPS;
109 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
111 hr = g_pDD->CreateSurface(&ddsd, &g_pddsPrimary, NULL);
120 ddsd.dwFlags = DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS;
121 ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_3DDEVICE;
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;
130 hr = g_pDD->CreateSurface(&ddsd, &g_pddsBackBuffer, NULL);
135 LPDIRECTDRAWCLIPPER pcClipper;
136 hr = g_pDD->CreateClipper(0, &pcClipper, NULL);
141 pcClipper->SetHWnd(0, hWnd);
142 g_pddsPrimary->SetClipper(pcClipper);
143 pcClipper->Release();
149 hr = g_pDD->QueryInterface(IID_IDirect3D7, (void **)&g_pD3D);
154 hr = g_pD3D->CreateDevice(IID_IDirect3DHALDevice, g_pddsBackBuffer, &g_pDevice);
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();
171 g_pDevice->SetRenderState(D3DRENDERSTATE_LIGHTING, FALSE);
172 g_pDevice->SetRenderState(D3DRENDERSTATE_CULLMODE, D3DCULL_NONE);
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) },
180 g_pDevice->DrawPrimitive(D3DPT_TRIANGLELIST, D3DFVF_XYZ | D3DFVF_DIFFUSE, vertices, 3, 0);
182 g_pDevice->EndScene();
187 hr = g_pddsPrimary->Blt(&g_rcScreenRect, g_pddsBackBuffer, &g_rcViewportRect, DDBLT_WAIT, NULL);
192 g_pDevice->Release();