Fix more -Wstrict-prototypes warnings.
[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 createdirectdraw(void)
35 {
36     HRESULT rc;
37     
38     wc.style = CS_HREDRAW | CS_VREDRAW;
39     wc.lpfnWndProc = DefWindowProcA;
40     wc.cbClsExtra = 0;
41     wc.cbWndExtra = 0;
42     wc.hInstance = GetModuleHandleA(0);
43     wc.hIcon = LoadIconA(wc.hInstance, IDI_APPLICATION);
44     wc.hCursor = LoadCursorA(NULL, IDC_ARROW);
45     wc.hbrBackground = (HBRUSH) GetStockObject(BLACK_BRUSH);
46     wc.lpszMenuName = NULL;
47     wc.lpszClassName = "TestWindowClass";
48     if(!RegisterClassA(&wc))
49         assert(0);
50     
51     hwnd = CreateWindowExA(0, "TestWindowClass", "TestWindowClass",
52         WS_POPUP, 0, 0,
53         GetSystemMetrics(SM_CXSCREEN),
54         GetSystemMetrics(SM_CYSCREEN),
55         NULL, NULL, GetModuleHandleA(0), NULL);
56     assert(hwnd != NULL);
57     
58     ShowWindow(hwnd, SW_HIDE);
59     UpdateWindow(hwnd);
60     SetFocus(hwnd);
61     
62     rc = DirectDrawCreate(NULL, &lpDD, NULL);
63     ok(rc==DD_OK,"DirectDrawCreate returned: %lx\n",rc);
64 }
65
66
67 static void releasedirectdraw(void)
68 {
69         if( lpDD != NULL )
70         {
71                 IDirectDraw_Release(lpDD);
72                 lpDD = NULL;
73         }
74 }
75
76 static void adddisplaymode(LPDDSURFACEDESC lpddsd)
77 {
78     if (!modes) 
79         modes = malloc((modes_size = 2) * sizeof(DDSURFACEDESC));
80     if (modes_cnt == modes_size) 
81             modes = realloc(modes, (modes_size *= 2) * sizeof(DDSURFACEDESC));
82     assert(modes);
83     modes[modes_cnt++] = *lpddsd;
84 }
85
86 static void flushdisplaymodes(void)
87 {
88     free(modes);
89     modes = 0;
90     modes_cnt = modes_size = 0;
91 }
92
93 HRESULT WINAPI enummodescallback(LPDDSURFACEDESC lpddsd, LPVOID lpContext)
94 {
95     trace("Width = %li, Height = %li, Refresh Rate = %li\r\n",
96         lpddsd->dwWidth, lpddsd->dwHeight,
97         U2(*lpddsd).dwRefreshRate);
98     adddisplaymode(lpddsd);
99
100     return DDENUMRET_OK;
101 }
102
103 void enumdisplaymodes(void)
104 {
105     DDSURFACEDESC ddsd;
106     HRESULT rc;
107
108     ZeroMemory(&ddsd, sizeof(DDSURFACEDESC));
109     ddsd.dwSize = sizeof(DDSURFACEDESC);
110     ddsd.dwFlags = DDSD_CAPS;
111     ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
112
113     rc = IDirectDraw_EnumDisplayModes(lpDD,
114         DDEDM_STANDARDVGAMODES, &ddsd, 0, enummodescallback);
115     ok(rc==DD_OK || rc==E_INVALIDARG,"EnumDisplayModes returned: %lx\n",rc);
116 }
117
118 static void setdisplaymode(int i)
119 {
120     HRESULT rc;
121
122     rc = IDirectDraw_SetCooperativeLevel(lpDD,
123         hwnd, DDSCL_ALLOWMODEX | DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
124     ok(rc==DD_OK,"SetCooperativeLevel returned: %lx\n",rc);
125     if (modes[i].dwFlags & DDSD_PIXELFORMAT)
126     {
127         if (modes[i].ddpfPixelFormat.dwFlags & DDPF_RGB)
128         {
129             rc = IDirectDraw_SetDisplayMode(lpDD,
130                 modes[i].dwWidth, modes[i].dwHeight,
131                 U1(modes[i].ddpfPixelFormat).dwRGBBitCount);
132             ok(DD_OK==rc || DDERR_UNSUPPORTED==rc,"SetDisplayMode returned: %lx\n",rc);
133             if (DD_OK==rc) {
134                 rc = IDirectDraw_RestoreDisplayMode(lpDD);
135                 ok(DD_OK==rc,"RestoreDisplayMode returned: %lx\n",rc);
136             }
137         }
138     }
139 }
140
141 static void createsurface(void)
142 {
143     DDSURFACEDESC ddsd;
144     DDSCAPS ddscaps;
145     HRESULT rc;
146     
147     ddsd.dwSize = sizeof(ddsd);
148     ddsd.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
149     ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE |
150         DDSCAPS_FLIP |
151         DDSCAPS_COMPLEX;
152     ddsd.dwBackBufferCount = 1;
153     rc = IDirectDraw_CreateSurface(lpDD, &ddsd, &lpDDSPrimary, NULL );
154     ok(rc==DD_OK,"CreateSurface returned: %lx\n",rc);
155     ddscaps.dwCaps = DDSCAPS_BACKBUFFER;
156     rc = IDirectDrawSurface_GetAttachedSurface(lpDDSPrimary, &ddscaps, &lpDDSBack);
157     ok(rc==DD_OK,"GetAttachedSurface returned: %lx\n",rc);
158 }
159
160 static void destroysurface(void)
161 {
162     if( lpDDSPrimary != NULL )
163     {
164         IDirectDrawSurface_Release(lpDDSPrimary);
165         lpDDSPrimary = NULL;
166     }
167 }
168
169 static void testsurface(void)
170 {
171     const char* testMsg = "ddraw device context test";
172     HDC hdc;
173     HRESULT rc;
174     
175     rc = IDirectDrawSurface_GetDC(lpDDSBack, &hdc);
176     ok(rc==DD_OK, "IDirectDrawSurface_GetDC returned: %lx\n",rc);
177     SetBkColor(hdc, RGB(0, 0, 255));
178     SetTextColor(hdc, RGB(255, 255, 0));
179     TextOut(hdc, 0, 0, testMsg, lstrlen(testMsg));
180     IDirectDrawSurface_ReleaseDC(lpDDSBack, hdc);
181     ok(rc==DD_OK, "IDirectDrawSurface_ReleaseDC returned: %lx\n",rc);
182     
183     while (1)
184     {
185         rc = IDirectDrawSurface_Flip(lpDDSPrimary, NULL, DDFLIP_WAIT);
186         ok(rc==DD_OK || rc==DDERR_SURFACELOST, "IDirectDrawSurface_BltFast returned: %lx\n",rc);
187
188         if (rc == DD_OK)
189         {
190             break;
191         }
192         else if (rc == DDERR_SURFACELOST)
193         {
194             rc = IDirectDrawSurface_Restore(lpDDSPrimary);
195             ok(rc==DD_OK, "IDirectDrawSurface_Restore returned: %lx\n",rc);
196         }
197     }
198 }
199
200 static void testdisplaymodes(void)
201 {
202     int i;
203
204     for (i = 0; i < modes_cnt; ++i)
205     {
206         setdisplaymode(i);
207         createsurface();
208         testsurface();
209         destroysurface();
210     }
211 }
212
213 START_TEST(ddrawmodes)
214 {
215     createdirectdraw();
216     enumdisplaymodes();
217     testdisplaymodes();
218     flushdisplaymodes();
219     releasedirectdraw();
220 }