Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Latest commit

 

History

History
228 lines (202 loc) · 6.67 KB

SDL_win32modes.c

File metadata and controls

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