Made some functions static.
[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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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 void createdirectdraw(void)
63 {
64     HRESULT rc;
65
66     rc = DirectDrawCreate(NULL, &lpDD, NULL);
67     ok(rc==DD_OK,"DirectDrawCreate returned: %lx\n",rc);
68 }
69
70
71 static void releasedirectdraw(void)
72 {
73         if( lpDD != NULL )
74         {
75                 IDirectDraw_Release(lpDD);
76                 lpDD = NULL;
77         }
78 }
79
80 static void adddisplaymode(LPDDSURFACEDESC lpddsd)
81 {
82     if (!modes) 
83         modes = malloc((modes_size = 2) * sizeof(DDSURFACEDESC));
84     if (modes_cnt == modes_size) 
85             modes = realloc(modes, (modes_size *= 2) * sizeof(DDSURFACEDESC));
86     assert(modes);
87     modes[modes_cnt++] = *lpddsd;
88 }
89
90 static void flushdisplaymodes(void)
91 {
92     free(modes);
93     modes = 0;
94     modes_cnt = modes_size = 0;
95 }
96
97 static HRESULT WINAPI enummodescallback(LPDDSURFACEDESC lpddsd, LPVOID lpContext)
98 {
99     trace("Width = %li, Height = %li, Refresh Rate = %li\r\n",
100         lpddsd->dwWidth, lpddsd->dwHeight,
101         U2(*lpddsd).dwRefreshRate);
102     adddisplaymode(lpddsd);
103
104     return DDENUMRET_OK;
105 }
106
107 static void enumdisplaymodes(void)
108 {
109     DDSURFACEDESC ddsd;
110     HRESULT rc;
111
112     ZeroMemory(&ddsd, sizeof(DDSURFACEDESC));
113     ddsd.dwSize = sizeof(DDSURFACEDESC);
114     ddsd.dwFlags = DDSD_CAPS;
115     ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
116
117     rc = IDirectDraw_EnumDisplayModes(lpDD,
118         DDEDM_STANDARDVGAMODES, &ddsd, 0, enummodescallback);
119     ok(rc==DD_OK || rc==E_INVALIDARG,"EnumDisplayModes returned: %lx\n",rc);
120 }
121
122 static void setdisplaymode(int i)
123 {
124     HRESULT rc;
125
126     rc = IDirectDraw_SetCooperativeLevel(lpDD,
127         hwnd, DDSCL_ALLOWMODEX | DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
128     ok(rc==DD_OK,"SetCooperativeLevel returned: %lx\n",rc);
129     if (modes[i].dwFlags & DDSD_PIXELFORMAT)
130     {
131         if (modes[i].ddpfPixelFormat.dwFlags & DDPF_RGB)
132         {
133             rc = IDirectDraw_SetDisplayMode(lpDD,
134                 modes[i].dwWidth, modes[i].dwHeight,
135                 U1(modes[i].ddpfPixelFormat).dwRGBBitCount);
136             ok(DD_OK==rc || DDERR_UNSUPPORTED==rc,"SetDisplayMode returned: %lx\n",rc);
137             if (DD_OK==rc) {
138                 rc = IDirectDraw_RestoreDisplayMode(lpDD);
139                 ok(DD_OK==rc,"RestoreDisplayMode returned: %lx\n",rc);
140             }
141         }
142     }
143 }
144
145 static void createsurface(void)
146 {
147     DDSURFACEDESC ddsd;
148     DDSCAPS ddscaps;
149     HRESULT rc;
150     
151     ddsd.dwSize = sizeof(ddsd);
152     ddsd.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
153     ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE |
154         DDSCAPS_FLIP |
155         DDSCAPS_COMPLEX;
156     ddsd.dwBackBufferCount = 1;
157     rc = IDirectDraw_CreateSurface(lpDD, &ddsd, &lpDDSPrimary, NULL );
158     ok(rc==DD_OK,"CreateSurface returned: %lx\n",rc);
159     ddscaps.dwCaps = DDSCAPS_BACKBUFFER;
160     rc = IDirectDrawSurface_GetAttachedSurface(lpDDSPrimary, &ddscaps, &lpDDSBack);
161     ok(rc==DD_OK,"GetAttachedSurface returned: %lx\n",rc);
162 }
163
164 static void destroysurface(void)
165 {
166     if( lpDDSPrimary != NULL )
167     {
168         IDirectDrawSurface_Release(lpDDSPrimary);
169         lpDDSPrimary = NULL;
170     }
171 }
172
173 static void testsurface(void)
174 {
175     const char* testMsg = "ddraw device context test";
176     HDC hdc;
177     HRESULT rc;
178     
179     rc = IDirectDrawSurface_GetDC(lpDDSBack, &hdc);
180     ok(rc==DD_OK, "IDirectDrawSurface_GetDC returned: %lx\n",rc);
181     SetBkColor(hdc, RGB(0, 0, 255));
182     SetTextColor(hdc, RGB(255, 255, 0));
183     TextOut(hdc, 0, 0, testMsg, lstrlen(testMsg));
184     IDirectDrawSurface_ReleaseDC(lpDDSBack, hdc);
185     ok(rc==DD_OK, "IDirectDrawSurface_ReleaseDC returned: %lx\n",rc);
186     
187     while (1)
188     {
189         rc = IDirectDrawSurface_Flip(lpDDSPrimary, NULL, DDFLIP_WAIT);
190         ok(rc==DD_OK || rc==DDERR_SURFACELOST, "IDirectDrawSurface_BltFast returned: %lx\n",rc);
191
192         if (rc == DD_OK)
193         {
194             break;
195         }
196         else if (rc == DDERR_SURFACELOST)
197         {
198             rc = IDirectDrawSurface_Restore(lpDDSPrimary);
199             ok(rc==DD_OK, "IDirectDrawSurface_Restore returned: %lx\n",rc);
200         }
201     }
202 }
203
204 static void testdisplaymodes(void)
205 {
206     int i;
207
208     for (i = 0; i < modes_cnt; ++i)
209     {
210         setdisplaymode(i);
211         createsurface();
212         testsurface();
213         destroysurface();
214     }
215 }
216
217 static void testcooperativelevels_normal(void)
218 {
219     HRESULT rc;
220
221     /* Do some tests with DDSCL_NORMAL mode */
222
223     rc = IDirectDraw_SetCooperativeLevel(lpDD,
224         hwnd, DDSCL_NORMAL);
225     ok(rc==DD_OK,"SetCooperativeLevel(DDSCL_NORMAL) returned: %lx\n",rc);
226
227     /* Set the focus window */
228     rc = IDirectDraw_SetCooperativeLevel(lpDD,
229         hwnd, DDSCL_SETFOCUSWINDOW);
230     ok(rc==DD_OK,"SetCooperativeLevel(DDSCL_SETFOCUSWINDOW) returned: %lx\n",rc);
231
232     /* Set the focus window a secound time*/
233     rc = IDirectDraw_SetCooperativeLevel(lpDD,
234         hwnd, DDSCL_SETFOCUSWINDOW);
235     ok(rc==DD_OK,"SetCooperativeLevel(DDSCL_SETFOCUSWINDOW) the secound time returned: %lx\n",rc);
236
237     /* Test DDSCL_SETFOCUSWINDOW with the other flags. They should all fail, except of DDSCL_NOWINDOWCHANGES */
238     rc = IDirectDraw_SetCooperativeLevel(lpDD,
239         hwnd, DDSCL_NORMAL | DDSCL_SETFOCUSWINDOW);
240     ok(rc==DDERR_INVALIDPARAMS,"SetCooperativeLevel(DDSCL_NORMAL | DDSCL_SETFOCUSWINDOW) returned: %lx\n",rc);
241
242     rc = IDirectDraw_SetCooperativeLevel(lpDD,
243         hwnd, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN | DDSCL_SETFOCUSWINDOW);
244     ok(rc==DDERR_INVALIDPARAMS,"SetCooperativeLevel(DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN | DDSCL_SETFOCUSWINDOW) returned: %lx\n",rc);
245
246     /* This one succeeds */
247     rc = IDirectDraw_SetCooperativeLevel(lpDD,
248         hwnd, DDSCL_NOWINDOWCHANGES | DDSCL_SETFOCUSWINDOW);
249     ok(rc==DD_OK,"SetCooperativeLevel(DDSCL_NOWINDOWCHANGES | DDSCL_SETFOCUSWINDOW) returned: %lx\n",rc);
250
251     rc = IDirectDraw_SetCooperativeLevel(lpDD,
252         hwnd, DDSCL_MULTITHREADED | DDSCL_SETFOCUSWINDOW);
253     ok(rc==DDERR_INVALIDPARAMS,"SetCooperativeLevel(DDSCL_MULTITHREADED | DDSCL_SETFOCUSWINDOW) returned: %lx\n",rc);
254
255     rc = IDirectDraw_SetCooperativeLevel(lpDD,
256         hwnd, DDSCL_FPUSETUP | DDSCL_SETFOCUSWINDOW);
257     ok(rc==DDERR_INVALIDPARAMS,"SetCooperativeLevel(DDSCL_FPUSETUP | DDSCL_SETFOCUSWINDOW) returned: %lx\n",rc);
258
259     rc = IDirectDraw_SetCooperativeLevel(lpDD,
260         hwnd, DDSCL_FPUPRESERVE | DDSCL_SETFOCUSWINDOW);
261     ok(rc==DDERR_INVALIDPARAMS,"SetCooperativeLevel(DDSCL_FPUPRESERVE | DDSCL_SETFOCUSWINDOW) returned: %lx\n",rc);
262
263     rc = IDirectDraw_SetCooperativeLevel(lpDD,
264         hwnd, DDSCL_ALLOWREBOOT | DDSCL_SETFOCUSWINDOW);
265     ok(rc==DDERR_INVALIDPARAMS,"SetCooperativeLevel(DDSCL_ALLOWREBOOT | DDSCL_SETFOCUSWINDOW) returned: %lx\n",rc);
266
267     rc = IDirectDraw_SetCooperativeLevel(lpDD,
268         hwnd, DDSCL_ALLOWMODEX | DDSCL_SETFOCUSWINDOW);
269     ok(rc==DDERR_INVALIDPARAMS,"SetCooperativeLevel(DDSCL_ALLOWMODEX | DDSCL_SETFOCUSWINDOW) returned: %lx\n",rc);
270
271     /* Set the device window without any other flags. Should give an error */
272     rc = IDirectDraw_SetCooperativeLevel(lpDD,
273         hwnd, DDSCL_SETDEVICEWINDOW);
274     ok(rc==DDERR_INVALIDPARAMS,"SetCooperativeLevel(DDSCL_SETDEVICEWINDOW) returned: %lx\n",rc);
275
276     /* Set device window with DDSCL_NORMAL */
277     rc = IDirectDraw_SetCooperativeLevel(lpDD,
278         hwnd, DDSCL_NORMAL | DDSCL_SETDEVICEWINDOW);
279     ok(rc==DD_OK,"SetCooperativeLevel(DDSCL_NORMAL | DDSCL_SETDEVICEWINDOW) returned: %lx\n",rc);
280  
281     /* Also set the focus window. Should give an error */
282     rc = IDirectDraw_SetCooperativeLevel(lpDD,
283         hwnd, DDSCL_ALLOWMODEX | DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN | DDSCL_SETDEVICEWINDOW | DDSCL_SETFOCUSWINDOW);
284     ok(rc==DDERR_INVALIDPARAMS,"SetCooperativeLevel(DDSCL_NORMAL | DDSCL_SETDEVICEWINDOW | DDSCL_SETFOCUSWINDOW) returned: %lx\n",rc);
285
286     /* All done */
287 }
288
289 static void testcooperativelevels_exclusive(void)
290 {
291     HRESULT rc;
292
293     /* Do some tests with DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN mode */
294
295     /* Try to set exclusive mode only */
296     rc = IDirectDraw_SetCooperativeLevel(lpDD,
297         hwnd, DDSCL_EXCLUSIVE);
298     ok(rc==DDERR_INVALIDPARAMS,"SetCooperativeLevel(DDSCL_EXCLUSIVE) returned: %lx\n",rc);
299
300     /* Full screen mode only */
301     rc = IDirectDraw_SetCooperativeLevel(lpDD,
302         hwnd, DDSCL_FULLSCREEN);
303     ok(rc==DDERR_INVALIDPARAMS,"SetCooperativeLevel(DDSCL_FULLSCREEN) returned: %lx\n",rc);
304
305     /* Full screen mode + exclusive mode */
306     rc = IDirectDraw_SetCooperativeLevel(lpDD,
307         hwnd, DDSCL_FULLSCREEN | DDSCL_EXCLUSIVE);
308     ok(rc==DD_OK,"SetCooperativeLevel(DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN) returned: %lx\n",rc);
309
310     /* Set the focus window. Should fail */
311     rc = IDirectDraw_SetCooperativeLevel(lpDD,
312         hwnd, DDSCL_SETFOCUSWINDOW);
313     ok(rc==DDERR_HWNDALREADYSET,"SetCooperativeLevel(DDSCL_SETFOCUSWINDOW) returned: %lx\n",rc);
314
315
316     /* All done */
317 }
318
319 START_TEST(ddrawmodes)
320 {
321     createwindow();
322     createdirectdraw();
323     enumdisplaymodes();
324     testdisplaymodes();
325     flushdisplaymodes();
326     releasedirectdraw();
327
328     createdirectdraw();
329     testcooperativelevels_normal();
330     releasedirectdraw();
331
332     createdirectdraw();
333     testcooperativelevels_exclusive();
334     releasedirectdraw();
335 }