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