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