Make it a compile time choice, whether you want to use a clipper or not, because...
[reactos.git] / rosapps / demos / ddraw / main.cpp
1 #include <windows.h>
2 #include <ddraw.h>
3
4 LPDIRECTDRAW7 DirectDraw = NULL;
5 LPDIRECTDRAWSURFACE7 FrontBuffer = NULL;
6 LPDIRECTDRAWCLIPPER Clipper = NULL;
7
8 PCHAR DDErrorString (HRESULT hResult);
9 LONG WINAPI WndProc (HWND hwnd, UINT message, UINT wParam, LONG lParam);
10
11 bool Fullscreen, Running;
12 //#define USE_CLIPPER
13
14 bool Init (HWND hwnd)
15 {
16 DDSURFACEDESC2 ddsd;
17 HRESULT hResult;
18
19 // Create the main object
20 OutputDebugString("=> DirectDrawCreateEx\n");
21 hResult = DirectDrawCreateEx(NULL, (VOID**)&DirectDraw, IID_IDirectDraw7, NULL);
22
23 if (hResult != DD_OK)
24 {
25 MessageBox(0,DDErrorString(hResult), "DirectDrawCreateEx", 0);
26 return 0;
27 }
28
29 // Set Fullscreen or windowed mode
30 OutputDebugString("=> DDraw->SetCooperativeLevel\n");
31
32 if(Fullscreen)
33 hResult = DirectDraw->SetCooperativeLevel (hwnd, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
34 else
35 hResult = DirectDraw->SetCooperativeLevel (hwnd, DDSCL_NORMAL);
36
37 if (hResult != DD_OK)
38 {
39 MessageBox(0,DDErrorString(hResult), "DDraw->SetCooperativeLevel", 0);
40 return 0;
41 }
42
43 // Set the new resolution
44 if(Fullscreen)
45 {
46 OutputDebugString("=> DDraw->SetDisplayMode\n");
47 hResult = DirectDraw->SetDisplayMode (800, 600, 32, 0, 0);
48
49 if (hResult != DD_OK)
50 {
51 MessageBox(0,DDErrorString(hResult), "DDraw->SetDisplayMode", 0);
52 return 0;
53 }
54 }
55
56 // Create the primary surface
57 memset(&ddsd, 0, sizeof(DDSURFACEDESC2));
58
59 ddsd.dwSize = sizeof(DDSURFACEDESC2);
60 ddsd.dwFlags = DDSD_CAPS;
61 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
62
63 OutputDebugString("=> DDraw->CreateSurface\n");
64 hResult = DirectDraw->CreateSurface(&ddsd, &FrontBuffer, NULL);
65
66 if (hResult != DD_OK)
67 {
68 MessageBox(0,DDErrorString(hResult), "DDraw->CreateSurface", 0);
69 return 0;
70 }
71
72 #ifdef USE_CLIPPER
73 // Set up the clipper
74 OutputDebugString("=> DDraw->CreateClipper\n");
75
76 hResult = DirectDraw->CreateClipper(0,&Clipper,NULL);
77 if (hResult != DD_OK)
78 {
79 MessageBox(0,DDErrorString(hResult), "DDraw->CreateSurface", 0);
80 return 0;
81 }
82
83 OutputDebugString("=> Clipper->SetHWnd\n");
84 hResult = Clipper->SetHWnd(0,hwnd);
85 if (hResult != DD_OK)
86 {
87 MessageBox(0,DDErrorString(hResult), "Clipper->SetHWnd", 0);
88 return 0;
89 }
90
91 OutputDebugString("=> Suface->SetClipper\n");
92 hResult = FrontBuffer->SetClipper(Clipper);
93 if (hResult != DD_OK)
94 {
95 MessageBox(0,DDErrorString(hResult), "FrontBuffer->SetClipper", 0);
96 return 0;
97 }
98 #endif
99
100 return true;
101 }
102
103 void Draw (HWND hwnd)
104 {
105 // Make the frontbuffer pink
106 RECT rect;
107 GetWindowRect(hwnd, &rect); // this is not necessary when clippper is used
108
109 DDBLTFX ddbltfx;
110 ddbltfx.dwSize = sizeof(DDBLTFX);
111 ddbltfx.dwFillColor = RGB(255, 0, 255);
112
113 OutputDebugString("=> Surface->Blt (DDBLT_COLORFILL)\n");
114
115 FrontBuffer->Blt(&rect, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &ddbltfx);
116 }
117
118 void CleanUp (void)
119 {
120 if (Clipper != NULL)
121 {
122 OutputDebugString("=> Clipper->Release\n");
123 Clipper->Release();
124 Clipper = NULL;
125 }
126
127 if (FrontBuffer != NULL)
128 {
129 OutputDebugString("=> Surface->Release\n");
130 FrontBuffer->Release();
131 FrontBuffer = NULL;
132 }
133 if (DirectDraw != NULL)
134 {
135 OutputDebugString("=> DDraw->Release\n");
136 DirectDraw->Release();
137 DirectDraw = NULL;
138 }
139 }
140
141 int WINAPI WinMain (HINSTANCE hInst, HINSTANCE hPrevInst,
142 LPSTR lpCmdLine, int nCmdShow)
143 {
144 MSG msg;
145 WNDCLASS wndclass;
146
147 Fullscreen = MessageBox(0, "Do you want to me to run in Fullscreen ?", 0, MB_YESNO) == IDYES;
148
149 // Create windnow
150 wndclass.style = CS_HREDRAW | CS_VREDRAW;
151 wndclass.lpfnWndProc = WndProc;
152 wndclass.cbClsExtra = 0;
153 wndclass.cbWndExtra = 0;
154 wndclass.hInstance = hInst;
155 wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION);
156 wndclass.hCursor = LoadCursor (NULL, IDC_ARROW);
157 wndclass.hbrBackground = (HBRUSH)GetStockObject (LTGRAY_BRUSH);
158 wndclass.lpszMenuName = NULL;
159 wndclass.lpszClassName = "DDrawDemo";
160
161 RegisterClass(&wndclass);
162
163 HWND hwnd = CreateWindow("DDrawDemo",
164 "ReactOS DirectDraw Demo",
165 #ifdef USE_CLIPPER
166 Fullscreen ? WS_POPUP : WS_OVERLAPPEDWINDOW,
167 #else
168 WS_POPUP,
169 #endif
170 CW_USEDEFAULT,
171 CW_USEDEFAULT,
172 800,
173 600,
174 NULL, NULL,
175 hInst, NULL);
176
177 // Inizalize Ddraw
178 if(Init(hwnd))
179 {
180 Running = true;
181
182 ShowWindow(hwnd, nCmdShow);
183 UpdateWindow(hwnd);
184 }
185
186 // Main loop
187 while (Running)
188 {
189 if(Fullscreen)
190 Draw(hwnd);
191
192 if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
193 {
194 if (msg.message == WM_QUIT) break;
195 TranslateMessage(&msg);
196 DispatchMessage(&msg);
197 }
198 }
199
200 CleanUp();
201
202 return 0;
203 }
204
205
206 LONG WINAPI WndProc (HWND hwnd, UINT message,
207 UINT wParam, LONG lParam)
208 {
209 switch (message)
210 {
211 case WM_PAINT:
212 {
213 if(!Fullscreen)
214 Draw(hwnd);
215 } break;
216
217 case WM_KEYDOWN:
218 {
219 switch (wParam)
220 {
221 case VK_ESCAPE:
222 Running=false;
223 return 0;
224 } break;
225 }
226
227 case WM_DESTROY:
228 {
229 PostQuitMessage (0);
230 return 0;
231 } break;
232 }
233
234 return DefWindowProc (hwnd, message, wParam, lParam);
235 }
236
237 PCHAR DDErrorString (HRESULT hResult)
238 {
239 switch (hResult)
240 {
241 case DD_OK: return "DD_OK";
242 case DDERR_ALREADYINITIALIZED: return "DDERR_ALREADYINITIALIZED";
243 case DDERR_CANNOTATTACHSURFACE: return "DDERR_CANNOTATTACHSURFACE";
244 case DDERR_CANNOTDETACHSURFACE: return "DDERR_CANNOTDETACHSURFACE";
245 case DDERR_CURRENTLYNOTAVAIL: return "DDERR_CURRENTLYNOTAVAIL";
246 case DDERR_EXCEPTION: return "DDERR_EXCEPTION";
247 case DDERR_GENERIC: return "DDERR_GENERIC";
248 case DDERR_HEIGHTALIGN: return "DDERR_HEIGHTALIGN";
249 case DDERR_INCOMPATIBLEPRIMARY: return "DDERR_INCOMPATIBLEPRIMARY";
250 case DDERR_INVALIDCAPS: return "DDERR_INVALIDCAPS";
251 case DDERR_INVALIDCLIPLIST: return "DDERR_INVALIDCLIPLIST";
252 case DDERR_INVALIDMODE: return "DDERR_INVALIDMODE";
253 case DDERR_INVALIDOBJECT: return "DDERR_INVALIDOBJECT";
254 case DDERR_INVALIDPARAMS: return "DDERR_INVALIDPARAMS";
255 case DDERR_INVALIDPIXELFORMAT: return "DDERR_INVALIDPIXELFORMAT";
256 case DDERR_INVALIDRECT: return "DDERR_INVALIDRECT";
257 case DDERR_LOCKEDSURFACES: return "DDERR_LOCKEDSURFACES";
258 case DDERR_NO3D: return "DDERR_NO3D";
259 case DDERR_NOALPHAHW: return "DDERR_NOALPHAHW";
260 case DDERR_NOCLIPLIST: return "DDERR_NOCLIPLIST";
261 case DDERR_NOCOLORCONVHW: return "DDERR_NOCOLORCONVHW";
262 case DDERR_NOCOOPERATIVELEVELSET: return "DDERR_NOCOOPERATIVELEVELSET";
263 case DDERR_NOCOLORKEY: return "DDERR_NOCOLORKEY";
264 case DDERR_NOCOLORKEYHW: return "DDERR_NOCOLORKEYHW";
265 case DDERR_NODIRECTDRAWSUPPORT: return "DDERR_NODIRECTDRAWSUPPORT";
266 case DDERR_NOEXCLUSIVEMODE: return "DDERR_NOEXCLUSIVEMODE";
267 case DDERR_NOFLIPHW: return "DDERR_NOFLIPHW";
268 case DDERR_NOGDI: return "DDERR_NOGDI";
269 case DDERR_NOMIRRORHW: return "DDERR_NOMIRRORHW";
270 case DDERR_NOTFOUND: return "DDERR_NOTFOUND";
271 case DDERR_NOOVERLAYHW: return "DDERR_NOOVERLAYHW";
272 case DDERR_NORASTEROPHW: return "DDERR_NORASTEROPHW";
273 case DDERR_NOROTATIONHW: return "DDERR_NOROTATIONHW";
274 case DDERR_NOSTRETCHHW: return "DDERR_NOSTRETCHHW";
275 case DDERR_NOT4BITCOLOR: return "DDERR_NOT4BITCOLOR";
276 case DDERR_NOT4BITCOLORINDEX: return "DDERR_NOT4BITCOLORINDEX";
277 case DDERR_NOT8BITCOLOR: return "DDERR_NOT8BITCOLOR";
278 case DDERR_NOTEXTUREHW: return "DDERR_NOTEXTUREHW";
279 case DDERR_NOVSYNCHW: return "DDERR_NOVSYNCHW";
280 case DDERR_NOZBUFFERHW: return "DDERR_NOZBUFFERHW";
281 case DDERR_NOZOVERLAYHW: return "DDERR_NOZOVERLAYHW";
282 case DDERR_OUTOFCAPS: return "DDERR_OUTOFCAPS";
283 case DDERR_OUTOFMEMORY: return "DDERR_OUTOFMEMORY";
284 case DDERR_OUTOFVIDEOMEMORY: return "DDERR_OUTOFVIDEOMEMORY";
285 case DDERR_OVERLAYCANTCLIP: return "DDERR_OVERLAYCANTCLIP";
286 case DDERR_OVERLAYCOLORKEYONLYONEACTIVE: return "DDERR_OVERLAYCOLORKEYONLYONEACTIVE";
287 case DDERR_PALETTEBUSY: return "DDERR_PALETTEBUSY";
288 case DDERR_COLORKEYNOTSET: return "DDERR_COLORKEYNOTSET";
289 case DDERR_SURFACEALREADYATTACHED: return "DDERR_SURFACEALREADYATTACHED";
290 case DDERR_SURFACEALREADYDEPENDENT: return "DDERR_SURFACEALREADYDEPENDENT";
291 case DDERR_SURFACEBUSY: return "DDERR_SURFACEBUSY";
292 case DDERR_CANTLOCKSURFACE: return "DDERR_CANTLOCKSURFACE";
293 case DDERR_SURFACEISOBSCURED: return "DDERR_SURFACEISOBSCURED";
294 case DDERR_SURFACELOST: return "DDERR_SURFACELOST";
295 case DDERR_SURFACENOTATTACHED: return "DDERR_SURFACENOTATTACHED";
296 case DDERR_TOOBIGHEIGHT: return "DDERR_TOOBIGHEIGHT";
297 case DDERR_TOOBIGSIZE: return "DDERR_TOOBIGSIZE";
298 case DDERR_TOOBIGWIDTH: return "DDERR_TOOBIGWIDTH";
299 case DDERR_UNSUPPORTED: return "DDERR_UNSUPPORTED";
300 case DDERR_UNSUPPORTEDFORMAT: return "DDERR_UNSUPPORTEDFORMAT";
301 case DDERR_UNSUPPORTEDMASK: return "DDERR_UNSUPPORTEDMASK";
302 case DDERR_VERTICALBLANKINPROGRESS: return "DDERR_VERTICALBLANKINPROGRESS";
303 case DDERR_WASSTILLDRAWING: return "DDERR_WASSTILLDRAWING";
304 case DDERR_XALIGN: return "DDERR_XALIGN";
305 case DDERR_INVALIDDIRECTDRAWGUID: return "DDERR_INVALIDDIRECTDRAWGUID";
306 case DDERR_DIRECTDRAWALREADYCREATED: return "DDERR_DIRECTDRAWALREADYCREATED";
307 case DDERR_NODIRECTDRAWHW: return "DDERR_NODIRECTDRAWHW";
308 case DDERR_PRIMARYSURFACEALREADYEXISTS: return "DDERR_PRIMARYSURFACEALREADYEXISTS";
309 case DDERR_NOEMULATION: return "DDERR_NOEMULATION";
310 case DDERR_REGIONTOOSMALL: return "DDERR_REGIONTOOSMALL";
311 case DDERR_CLIPPERISUSINGHWND: return "DDERR_CLIPPERISUSINGHWND";
312 case DDERR_NOCLIPPERATTACHED: return "DDERR_NOCLIPPERATTACHED";
313 case DDERR_NOHWND: return "DDERR_NOHWND";
314 case DDERR_HWNDSUBCLASSED: return "DDERR_HWNDSUBCLASSED";
315 case DDERR_HWNDALREADYSET: return "DDERR_HWNDALREADYSET";
316 case DDERR_NOPALETTEATTACHED: return "DDERR_NOPALETTEATTACHED";
317 case DDERR_NOPALETTEHW: return "DDERR_NOPALETTEHW";
318 case DDERR_BLTFASTCANTCLIP: return "DDERR_BLTFASTCANTCLIP";
319 case DDERR_NOBLTHW: return "DDERR_NOBLTHW";
320 case DDERR_NODDROPSHW: return "DDERR_NODDROPSHW";
321 case DDERR_OVERLAYNOTVISIBLE: return "DDERR_OVERLAYNOTVISIBLE";
322 case DDERR_NOOVERLAYDEST: return "DDERR_NOOVERLAYDEST";
323 case DDERR_INVALIDPOSITION: return "DDERR_INVALIDPOSITION";
324 case DDERR_NOTAOVERLAYSURFACE: return "DDERR_NOTAOVERLAYSURFACE";
325 case DDERR_EXCLUSIVEMODEALREADYSET: return "DDERR_EXCLUSIVEMODEALREADYSET";
326 case DDERR_NOTFLIPPABLE: return "DDERR_NOTFLIPPABLE";
327 case DDERR_CANTDUPLICATE: return "DDERR_CANTDUPLICATE";
328 case DDERR_NOTLOCKED: return "DDERR_NOTLOCKED";
329 case DDERR_CANTCREATEDC: return "DDERR_CANTCREATEDC";
330 case DDERR_NODC: return "DDERR_NODC";
331 case DDERR_WRONGMODE: return "DDERR_WRONGMODE";
332 case DDERR_IMPLICITLYCREATED: return "DDERR_IMPLICITLYCREATED";
333 case DDERR_NOTPALETTIZED: return "DDERR_NOTPALETTIZED";
334 case DDERR_UNSUPPORTEDMODE: return "DDERR_UNSUPPORTEDMODE";
335 case DDERR_NOMIPMAPHW: return "DDERR_NOMIPMAPHW";
336 case DDERR_INVALIDSURFACETYPE: return "DDERR_INVALIDSURFACETYPE";
337 case DDERR_DCALREADYCREATED: return "DDERR_DCALREADYCREATED";
338 case DDERR_CANTPAGELOCK: return "DDERR_CANTPAGELOCK";
339 case DDERR_CANTPAGEUNLOCK: return "DDERR_CANTPAGEUNLOCK";
340 case DDERR_NOTPAGELOCKED: return "DDERR_NOTPAGELOCKED";
341 case DDERR_NOTINITIALIZED: return "DDERR_NOTINITIALIZED";
342 }
343 return "Unknown Error";
344 }
345