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