comctl32: Update Polish translation.
[wine] / dlls / ddraw / tests / ddrawmodes.c
1 /*
2  * Unit tests for ddraw functions
3  *
4  * Copyright (C) 2003 Sami Aario
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include <assert.h>
22 #include "wine/test.h"
23 #include "ddraw.h"
24
25 static LPDIRECTDRAW lpDD = NULL;
26 static LPDIRECTDRAWSURFACE lpDDSPrimary = NULL;
27 static LPDIRECTDRAWSURFACE lpDDSBack = NULL;
28 static WNDCLASS wc;
29 static HWND hwnd;
30 static int modes_cnt;
31 static int modes_size;
32 static LPDDSURFACEDESC modes;
33
34 static void createwindow(void)
35 {
36     wc.style = CS_HREDRAW | CS_VREDRAW;
37     wc.lpfnWndProc = DefWindowProcA;
38     wc.cbClsExtra = 0;
39     wc.cbWndExtra = 0;
40     wc.hInstance = GetModuleHandleA(0);
41     wc.hIcon = LoadIconA(wc.hInstance, IDI_APPLICATION);
42     wc.hCursor = LoadCursorA(NULL, IDC_ARROW);
43     wc.hbrBackground = (HBRUSH) GetStockObject(BLACK_BRUSH);
44     wc.lpszMenuName = NULL;
45     wc.lpszClassName = "TestWindowClass";
46     if(!RegisterClassA(&wc))
47         assert(0);
48     
49     hwnd = CreateWindowExA(0, "TestWindowClass", "TestWindowClass",
50         WS_POPUP, 0, 0,
51         GetSystemMetrics(SM_CXSCREEN),
52         GetSystemMetrics(SM_CYSCREEN),
53         NULL, NULL, GetModuleHandleA(0), NULL);
54     assert(hwnd != NULL);
55     
56     ShowWindow(hwnd, SW_HIDE);
57     UpdateWindow(hwnd);
58     SetFocus(hwnd);
59     
60 }
61
62 static BOOL createdirectdraw(void)
63 {
64     HRESULT rc;
65
66     rc = DirectDrawCreate(NULL, &lpDD, NULL);
67     ok(rc==DD_OK || rc==DDERR_NODIRECTDRAWSUPPORT, "DirectDrawCreateEx returned: %lx\n", rc);
68     if (!lpDD) {
69         trace("DirectDrawCreateEx() failed with an error %lx\n", rc);
70         return FALSE;
71     }
72     return TRUE;
73 }
74
75
76 static void releasedirectdraw(void)
77 {
78         if( lpDD != NULL )
79         {
80                 IDirectDraw_Release(lpDD);
81                 lpDD = NULL;
82         }
83 }
84
85 static void adddisplaymode(LPDDSURFACEDESC lpddsd)
86 {
87     if (!modes) 
88         modes = malloc((modes_size = 2) * sizeof(DDSURFACEDESC));
89     if (modes_cnt == modes_size) 
90             modes = realloc(modes, (modes_size *= 2) * sizeof(DDSURFACEDESC));
91     assert(modes);
92     modes[modes_cnt++] = *lpddsd;
93 }
94
95 static void flushdisplaymodes(void)
96 {
97     free(modes);
98     modes = 0;
99     modes_cnt = modes_size = 0;
100 }
101
102 static HRESULT WINAPI enummodescallback(LPDDSURFACEDESC lpddsd, LPVOID lpContext)
103 {
104     trace("Width = %li, Height = %li, Refresh Rate = %li\r\n",
105         lpddsd->dwWidth, lpddsd->dwHeight,
106         U2(*lpddsd).dwRefreshRate);
107     adddisplaymode(lpddsd);
108
109     return DDENUMRET_OK;
110 }
111
112 static void enumdisplaymodes(void)
113 {
114     DDSURFACEDESC ddsd;
115     HRESULT rc;
116
117     ZeroMemory(&ddsd, sizeof(DDSURFACEDESC));
118     ddsd.dwSize = sizeof(DDSURFACEDESC);
119     ddsd.dwFlags = DDSD_CAPS;
120     ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
121
122     rc = IDirectDraw_EnumDisplayModes(lpDD,
123         DDEDM_STANDARDVGAMODES, &ddsd, 0, enummodescallback);
124     ok(rc==DD_OK || rc==E_INVALIDARG,"EnumDisplayModes returned: %lx\n",rc);
125 }
126
127 static void setdisplaymode(int i)
128 {
129     HRESULT rc;
130
131     rc = IDirectDraw_SetCooperativeLevel(lpDD,
132         hwnd, DDSCL_ALLOWMODEX | DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
133     ok(rc==DD_OK,"SetCooperativeLevel returned: %lx\n",rc);
134     if (modes[i].dwFlags & DDSD_PIXELFORMAT)
135     {
136         if (modes[i].ddpfPixelFormat.dwFlags & DDPF_RGB)
137         {
138             rc = IDirectDraw_SetDisplayMode(lpDD,
139                 modes[i].dwWidth, modes[i].dwHeight,
140                 U1(modes[i].ddpfPixelFormat).dwRGBBitCount);
141             ok(DD_OK==rc || DDERR_UNSUPPORTED==rc,"SetDisplayMode returned: %lx\n",rc);
142             if (DD_OK==rc) {
143                 rc = IDirectDraw_RestoreDisplayMode(lpDD);
144                 ok(DD_OK==rc,"RestoreDisplayMode returned: %lx\n",rc);
145             }
146         }
147     }
148 }
149
150 static void createsurface(void)
151 {
152     DDSURFACEDESC ddsd;
153     DDSCAPS ddscaps;
154     HRESULT rc;
155     
156     ddsd.dwSize = sizeof(ddsd);
157     ddsd.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
158     ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE |
159         DDSCAPS_FLIP |
160         DDSCAPS_COMPLEX;
161     ddsd.dwBackBufferCount = 1;
162     rc = IDirectDraw_CreateSurface(lpDD, &ddsd, &lpDDSPrimary, NULL );
163     ok(rc==DD_OK,"CreateSurface returned: %lx\n",rc);
164     ddscaps.dwCaps = DDSCAPS_BACKBUFFER;
165     rc = IDirectDrawSurface_GetAttachedSurface(lpDDSPrimary, &ddscaps, &lpDDSBack);
166     ok(rc==DD_OK,"GetAttachedSurface returned: %lx\n",rc);
167 }
168
169 static void destroysurface(void)
170 {
171     if( lpDDSPrimary != NULL )
172     {
173         IDirectDrawSurface_Release(lpDDSPrimary);
174         lpDDSPrimary = NULL;
175     }
176 }
177
178 static void testsurface(void)
179 {
180     const char* testMsg = "ddraw device context test";
181     HDC hdc;
182     HRESULT rc;
183     
184     rc = IDirectDrawSurface_GetDC(lpDDSBack, &hdc);
185     ok(rc==DD_OK, "IDirectDrawSurface_GetDC returned: %lx\n",rc);
186     SetBkColor(hdc, RGB(0, 0, 255));
187     SetTextColor(hdc, RGB(255, 255, 0));
188     TextOut(hdc, 0, 0, testMsg, lstrlen(testMsg));
189     IDirectDrawSurface_ReleaseDC(lpDDSBack, hdc);
190     ok(rc==DD_OK, "IDirectDrawSurface_ReleaseDC returned: %lx\n",rc);
191     
192     while (1)
193     {
194         rc = IDirectDrawSurface_Flip(lpDDSPrimary, NULL, DDFLIP_WAIT);
195         ok(rc==DD_OK || rc==DDERR_SURFACELOST, "IDirectDrawSurface_BltFast returned: %lx\n",rc);
196
197         if (rc == DD_OK)
198         {
199             break;
200         }
201         else if (rc == DDERR_SURFACELOST)
202         {
203             rc = IDirectDrawSurface_Restore(lpDDSPrimary);
204             ok(rc==DD_OK, "IDirectDrawSurface_Restore returned: %lx\n",rc);
205         }
206     }
207 }
208
209 static void testdisplaymodes(void)
210 {
211     int i;
212
213     for (i = 0; i < modes_cnt; ++i)
214     {
215         setdisplaymode(i);
216         createsurface();
217         testsurface();
218         destroysurface();
219     }
220 }
221
222 static void testcooperativelevels_normal(void)
223 {
224     HRESULT rc;
225     DDSURFACEDESC surfacedesc;
226     IDirectDrawSurface *surface = (IDirectDrawSurface *) 0xdeadbeef;
227
228     memset(&surfacedesc, 0, sizeof(surfacedesc));
229     surfacedesc.dwSize = sizeof(surfacedesc);
230     surfacedesc.ddpfPixelFormat.dwSize = sizeof(surfacedesc.ddpfPixelFormat);
231     surfacedesc.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
232     surfacedesc.dwBackBufferCount = 1;
233     surfacedesc.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP;
234
235     /* Do some tests with DDSCL_NORMAL mode */
236
237     rc = IDirectDraw_SetCooperativeLevel(lpDD,
238         hwnd, DDSCL_NORMAL);
239     ok(rc==DD_OK,"SetCooperativeLevel(DDSCL_NORMAL) returned: %lx\n",rc);
240
241     /* Try creating a double buffered primary in normal mode */
242     rc = IDirectDraw_CreateSurface(lpDD, &surfacedesc, &surface, NULL);
243     ok(rc == DDERR_NOEXCLUSIVEMODE, "IDirectDraw_CreateSurface returned %08lx\n", rc);
244     ok(surface == NULL, "Returned surface pointer is %p\n", surface);
245     if(surface) IDirectDrawSurface_Release(surface);
246
247     /* Set the focus window */
248     rc = IDirectDraw_SetCooperativeLevel(lpDD,
249         hwnd, DDSCL_SETFOCUSWINDOW);
250     ok(rc==DD_OK,"SetCooperativeLevel(DDSCL_SETFOCUSWINDOW) returned: %lx\n",rc);
251
252     /* Set the focus window a second time*/
253     rc = IDirectDraw_SetCooperativeLevel(lpDD,
254         hwnd, DDSCL_SETFOCUSWINDOW);
255     ok(rc==DD_OK,"SetCooperativeLevel(DDSCL_SETFOCUSWINDOW) the second time returned: %lx\n",rc);
256
257     /* Test DDSCL_SETFOCUSWINDOW with the other flags. They should all fail, except of DDSCL_NOWINDOWCHANGES */
258     rc = IDirectDraw_SetCooperativeLevel(lpDD,
259         hwnd, DDSCL_NORMAL | DDSCL_SETFOCUSWINDOW);
260     ok(rc==DDERR_INVALIDPARAMS,"SetCooperativeLevel(DDSCL_NORMAL | DDSCL_SETFOCUSWINDOW) returned: %lx\n",rc);
261
262     rc = IDirectDraw_SetCooperativeLevel(lpDD,
263         hwnd, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN | DDSCL_SETFOCUSWINDOW);
264     ok(rc==DDERR_INVALIDPARAMS,"SetCooperativeLevel(DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN | DDSCL_SETFOCUSWINDOW) returned: %lx\n",rc);
265
266     /* This one succeeds */
267     rc = IDirectDraw_SetCooperativeLevel(lpDD,
268         hwnd, DDSCL_NOWINDOWCHANGES | DDSCL_SETFOCUSWINDOW);
269     ok(rc==DD_OK,"SetCooperativeLevel(DDSCL_NOWINDOWCHANGES | DDSCL_SETFOCUSWINDOW) returned: %lx\n",rc);
270
271     rc = IDirectDraw_SetCooperativeLevel(lpDD,
272         hwnd, DDSCL_MULTITHREADED | DDSCL_SETFOCUSWINDOW);
273     ok(rc==DDERR_INVALIDPARAMS,"SetCooperativeLevel(DDSCL_MULTITHREADED | DDSCL_SETFOCUSWINDOW) returned: %lx\n",rc);
274
275     rc = IDirectDraw_SetCooperativeLevel(lpDD,
276         hwnd, DDSCL_FPUSETUP | DDSCL_SETFOCUSWINDOW);
277     ok(rc==DDERR_INVALIDPARAMS,"SetCooperativeLevel(DDSCL_FPUSETUP | DDSCL_SETFOCUSWINDOW) returned: %lx\n",rc);
278
279     rc = IDirectDraw_SetCooperativeLevel(lpDD,
280         hwnd, DDSCL_FPUPRESERVE | DDSCL_SETFOCUSWINDOW);
281     ok(rc==DDERR_INVALIDPARAMS,"SetCooperativeLevel(DDSCL_FPUPRESERVE | DDSCL_SETFOCUSWINDOW) returned: %lx\n",rc);
282
283     rc = IDirectDraw_SetCooperativeLevel(lpDD,
284         hwnd, DDSCL_ALLOWREBOOT | DDSCL_SETFOCUSWINDOW);
285     ok(rc==DDERR_INVALIDPARAMS,"SetCooperativeLevel(DDSCL_ALLOWREBOOT | DDSCL_SETFOCUSWINDOW) returned: %lx\n",rc);
286
287     rc = IDirectDraw_SetCooperativeLevel(lpDD,
288         hwnd, DDSCL_ALLOWMODEX | DDSCL_SETFOCUSWINDOW);
289     ok(rc==DDERR_INVALIDPARAMS,"SetCooperativeLevel(DDSCL_ALLOWMODEX | DDSCL_SETFOCUSWINDOW) returned: %lx\n",rc);
290
291     /* Set the device window without any other flags. Should give an error */
292     rc = IDirectDraw_SetCooperativeLevel(lpDD,
293         hwnd, DDSCL_SETDEVICEWINDOW);
294     ok(rc==DDERR_INVALIDPARAMS,"SetCooperativeLevel(DDSCL_SETDEVICEWINDOW) returned: %lx\n",rc);
295
296     /* Set device window with DDSCL_NORMAL */
297     rc = IDirectDraw_SetCooperativeLevel(lpDD,
298         hwnd, DDSCL_NORMAL | DDSCL_SETDEVICEWINDOW);
299     ok(rc==DD_OK,"SetCooperativeLevel(DDSCL_NORMAL | DDSCL_SETDEVICEWINDOW) returned: %lx\n",rc);
300  
301     /* Also set the focus window. Should give an error */
302     rc = IDirectDraw_SetCooperativeLevel(lpDD,
303         hwnd, DDSCL_ALLOWMODEX | DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN | DDSCL_SETDEVICEWINDOW | DDSCL_SETFOCUSWINDOW);
304     ok(rc==DDERR_INVALIDPARAMS,"SetCooperativeLevel(DDSCL_NORMAL | DDSCL_SETDEVICEWINDOW | DDSCL_SETFOCUSWINDOW) returned: %lx\n",rc);
305
306     /* All done */
307 }
308
309 static void testcooperativelevels_exclusive(void)
310 {
311     HRESULT rc;
312
313     /* Do some tests with DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN mode */
314
315     /* Try to set exclusive mode only */
316     rc = IDirectDraw_SetCooperativeLevel(lpDD,
317         hwnd, DDSCL_EXCLUSIVE);
318     ok(rc==DDERR_INVALIDPARAMS,"SetCooperativeLevel(DDSCL_EXCLUSIVE) returned: %lx\n",rc);
319
320     /* Full screen mode only */
321     rc = IDirectDraw_SetCooperativeLevel(lpDD,
322         hwnd, DDSCL_FULLSCREEN);
323     ok(rc==DDERR_INVALIDPARAMS,"SetCooperativeLevel(DDSCL_FULLSCREEN) returned: %lx\n",rc);
324
325     /* Full screen mode + exclusive mode */
326     rc = IDirectDraw_SetCooperativeLevel(lpDD,
327         hwnd, DDSCL_FULLSCREEN | DDSCL_EXCLUSIVE);
328     ok(rc==DD_OK,"SetCooperativeLevel(DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN) returned: %lx\n",rc);
329
330     /* Set the focus window. Should fail */
331     rc = IDirectDraw_SetCooperativeLevel(lpDD,
332         hwnd, DDSCL_SETFOCUSWINDOW);
333     ok(rc==DDERR_HWNDALREADYSET,"SetCooperativeLevel(DDSCL_SETFOCUSWINDOW) returned: %lx\n",rc);
334
335
336     /* All done */
337 }
338
339 START_TEST(ddrawmodes)
340 {
341     createwindow();
342     if (!createdirectdraw())
343         return;
344     enumdisplaymodes();
345     testdisplaymodes();
346     flushdisplaymodes();
347     releasedirectdraw();
348
349     createdirectdraw();
350     testcooperativelevels_normal();
351     releasedirectdraw();
352
353     createdirectdraw();
354     testcooperativelevels_exclusive();
355     releasedirectdraw();
356 }