comparison src/video/win32/SDL_win32modes.c @ 1725:98a3207ddde8 SDL-1.3

Implemented Win32 video mode support
author Sam Lantinga <slouken@libsdl.org>
date Fri, 07 Jul 2006 08:05:39 +0000
parents
children 0b1070f2f94d
comparison
equal deleted inserted replaced
1724:6c63fc2bd986 1725:98a3207ddde8
1 /*
2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997-2006 Sam Lantinga
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
19 Sam Lantinga
20 slouken@libsdl.org
21 */
22 #include "SDL_config.h"
23
24 #include "SDL_win32video.h"
25
26
27 typedef struct
28 {
29 TCHAR DeviceName[32];
30 DEVMODE DeviceMode;
31 } SDL_DisplayModeData;
32
33 /* FIXME: Each call to EnumDisplaySettings() takes about 6 ms on my laptop.
34 With 500 or so modes, this takes almost 3 seconds to run!
35 */
36
37 static SDL_bool
38 WIN_GetDisplayMode(LPCTSTR deviceName, DWORD index, SDL_DisplayMode * mode)
39 {
40 SDL_DisplayModeData *data;
41 DEVMODE devmode;
42 HDC hdc;
43
44 devmode.dmSize = sizeof(devmode);
45 devmode.dmDriverExtra = 0;
46 if (!EnumDisplaySettings(deviceName, index, &devmode)) {
47 return SDL_FALSE;
48 }
49
50 data = (SDL_DisplayModeData *) SDL_malloc(sizeof(*data));
51 if (!data) {
52 return SDL_FALSE;
53 }
54 SDL_memcpy(data->DeviceName, deviceName, sizeof(data->DeviceName));
55 data->DeviceMode = devmode;
56 data->DeviceMode.dmFields =
57 (DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT | DM_DISPLAYFREQUENCY |
58 DM_DISPLAYFLAGS);
59
60 /* Fill in the mode information */
61 mode->format = SDL_PixelFormat_Unknown;
62 mode->w = devmode.dmPelsWidth;
63 mode->h = devmode.dmPelsHeight;
64 mode->refresh_rate = devmode.dmDisplayFrequency;
65 mode->driverdata = data;
66
67 hdc = CreateDC(deviceName, NULL, NULL, &devmode);
68 if (hdc) {
69 char bmi_data[sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD)];
70 LPBITMAPINFO bmi;
71 HBITMAP hbm;
72
73 SDL_zero(bmi_data);
74 bmi = (LPBITMAPINFO) bmi_data;
75 bmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
76
77 hbm = CreateCompatibleBitmap(hdc, 1, 1);
78 GetDIBits(hdc, hbm, 0, 1, NULL, bmi, DIB_RGB_COLORS);
79 GetDIBits(hdc, hbm, 0, 1, NULL, bmi, DIB_RGB_COLORS);
80 DeleteObject(hbm);
81 DeleteDC(hdc);
82 if (bmi->bmiHeader.biCompression == BI_BITFIELDS) {
83 switch (*(Uint32 *) bmi->bmiColors) {
84 case 0x00FF0000:
85 mode->format = SDL_PixelFormat_RGB888;
86 break;
87 case 0x000000FF:
88 mode->format = SDL_PixelFormat_BGR888;
89 break;
90 case 0xF800:
91 mode->format = SDL_PixelFormat_RGB565;
92 break;
93 case 0x7C00:
94 mode->format = SDL_PixelFormat_RGB555;
95 break;
96 }
97 } else if (bmi->bmiHeader.biBitCount == 8) {
98 mode->format = SDL_PixelFormat_Index8;
99 }
100 } else {
101 switch (devmode.dmBitsPerPel) {
102 case 32:
103 mode->format = SDL_PixelFormat_RGB888;
104 break;
105 case 24:
106 mode->format = SDL_PixelFormat_RGB24;
107 break;
108 case 16:
109 mode->format = SDL_PixelFormat_RGB565;
110 break;
111 case 15:
112 mode->format = SDL_PixelFormat_RGB555;
113 break;
114 case 8:
115 mode->format = SDL_PixelFormat_Index8;
116 break;
117 }
118 }
119 return SDL_TRUE;
120 }
121
122 void
123 WIN_InitModes(_THIS)
124 {
125 SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
126 DWORD i, j, k;
127 DISPLAY_DEVICE device;
128
129 device.cb = sizeof(device);
130 for (i = 0;; ++i) {
131 TCHAR DeviceName[32];
132
133 if (!EnumDisplayDevices(NULL, i, &device, 0)) {
134 break;
135 }
136 if (!(device.StateFlags & DISPLAY_DEVICE_ATTACHED_TO_DESKTOP)) {
137 continue;
138 }
139 SDL_memcpy(DeviceName, device.DeviceName, sizeof(DeviceName));
140 #ifdef DEBUG_MODES
141 printf("Device: %s\n", WIN_StringToUTF8(DeviceName));
142 #endif
143 for (j = 0;; ++j) {
144 int index;
145 SDL_VideoDisplay display;
146 SDL_DisplayMode mode;
147
148 if (!EnumDisplayDevices(DeviceName, j, &device, 0)) {
149 break;
150 }
151 if (!(device.StateFlags & DISPLAY_DEVICE_ATTACHED_TO_DESKTOP)) {
152 continue;
153 }
154 #ifdef DEBUG_MODES
155 printf("Monitor: %s\n", WIN_StringToUTF8(device.DeviceName));
156 #endif
157 if (!WIN_GetDisplayMode(DeviceName, ENUM_CURRENT_SETTINGS, &mode)) {
158 break;
159 }
160 SDL_zero(display);
161 display.desktop_mode = mode;
162 display.current_mode = mode;
163 index = SDL_AddVideoDisplay(&display);
164
165 for (k = 0;; ++k) {
166 if (!WIN_GetDisplayMode(DeviceName, k, &mode)) {
167 break;
168 }
169 if (!SDL_AddDisplayMode(index, &mode)) {
170 SDL_free(mode.driverdata);
171 }
172 }
173 }
174 }
175 }
176
177 int
178 WIN_SetDisplayMode(_THIS, SDL_DisplayMode * mode)
179 {
180 SDL_DisplayModeData *data = (SDL_DisplayModeData *) mode->driverdata;
181 LONG status;
182
183 status =
184 ChangeDisplaySettingsEx(data->DeviceName, &data->DeviceMode, NULL,
185 CDS_FULLSCREEN, NULL);
186 if (status == DISP_CHANGE_SUCCESSFUL) {
187 return 0;
188 } else {
189 const char *reason = "Unknown reason";
190 switch (status) {
191 case DISP_CHANGE_BADFLAGS:
192 reason = "DISP_CHANGE_BADFLAGS";
193 break;
194 case DISP_CHANGE_BADMODE:
195 reason = "DISP_CHANGE_BADMODE";
196 break;
197 case DISP_CHANGE_BADPARAM:
198 reason = "DISP_CHANGE_BADPARAM";
199 break;
200 case DISP_CHANGE_FAILED:
201 reason = "DISP_CHANGE_FAILED";
202 break;
203 }
204 SDL_SetError("ChangeDisplaySettingsEx() failed: %s", reason);
205 return -1;
206 }
207 }
208
209 void
210 WIN_QuitModes(_THIS)
211 {
212 ChangeDisplaySettingsEx(NULL, NULL, NULL, 0, NULL);
213 }
214
215 /* vi: set ts=4 sw=4 expandtab: */