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