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

Latest commit

 

History

History
1815 lines (1610 loc) · 49.6 KB

SDL_compat.c

File metadata and controls

1815 lines (1610 loc) · 49.6 KB
 
Apr 8, 2011
Apr 8, 2011
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Simple DirectMedia Layer
Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
20
21
22
23
24
25
26
27
28
29
*/
#include "SDL_config.h"
/* This file contains functions for backwards compatibility with SDL 1.2 */
#include "SDL.h"
#include "SDL_syswm.h"
#include "video/SDL_sysvideo.h"
#include "video/SDL_pixels_c.h"
Feb 4, 2011
Feb 4, 2011
30
31
#include "render/SDL_yuv_sw_c.h"
Jan 21, 2010
Jan 21, 2010
33
static SDL_Window *SDL_VideoWindow = NULL;
Feb 14, 2011
Feb 14, 2011
34
static SDL_Surface *SDL_WindowSurface = NULL;
Mar 14, 2008
Mar 14, 2008
35
36
37
38
static SDL_Surface *SDL_VideoSurface = NULL;
static SDL_Surface *SDL_ShadowSurface = NULL;
static SDL_Surface *SDL_PublicSurface = NULL;
static SDL_GLContext *SDL_VideoContext = NULL;
Dec 4, 2008
Dec 4, 2008
39
static Uint32 SDL_VideoFlags = 0;
Feb 14, 2011
Feb 14, 2011
40
static SDL_Rect SDL_VideoViewport;
Mar 14, 2008
Mar 14, 2008
41
static char *wm_title = NULL;
Jan 2, 2009
Jan 2, 2009
42
static SDL_Surface *SDL_VideoIcon;
Sep 19, 2009
Sep 19, 2009
43
static int SDL_enabled_UNICODE = 0;
Feb 4, 2011
Feb 4, 2011
45
const char *
46
47
48
49
SDL_AudioDriverName(char *namebuf, int maxlen)
{
const char *name = SDL_GetCurrentAudioDriver();
if (name) {
Feb 4, 2011
Feb 4, 2011
50
51
52
53
54
55
if (namebuf) {
SDL_strlcpy(namebuf, name, maxlen);
return namebuf;
} else {
return name;
}
56
57
58
59
}
return NULL;
}
Feb 4, 2011
Feb 4, 2011
60
const char *
61
62
63
64
SDL_VideoDriverName(char *namebuf, int maxlen)
{
const char *name = SDL_GetCurrentVideoDriver();
if (name) {
Feb 4, 2011
Feb 4, 2011
65
66
67
68
69
70
if (namebuf) {
SDL_strlcpy(namebuf, name, maxlen);
return namebuf;
} else {
return name;
}
71
72
73
74
}
return NULL;
}
Feb 10, 2011
Feb 10, 2011
75
76
static int
GetVideoDisplay()
Dec 3, 2009
Dec 3, 2009
77
78
79
80
81
82
{
const char *variable = SDL_getenv("SDL_VIDEO_FULLSCREEN_DISPLAY");
if ( !variable ) {
variable = SDL_getenv("SDL_VIDEO_FULLSCREEN_HEAD");
}
if ( variable ) {
Feb 11, 2011
Feb 11, 2011
83
return SDL_atoi(variable);
Feb 10, 2011
Feb 10, 2011
84
85
} else {
return 0;
Dec 3, 2009
Dec 3, 2009
86
87
88
}
}
89
90
91
92
const SDL_VideoInfo *
SDL_GetVideoInfo(void)
{
static SDL_VideoInfo info;
Aug 5, 2006
Aug 5, 2006
93
SDL_DisplayMode mode;
94
95
/* Memory leak, compatibility code, who cares? */
Feb 10, 2011
Feb 10, 2011
96
if (!info.vfmt && SDL_GetDesktopDisplayMode(GetVideoDisplay(), &mode) == 0) {
Feb 13, 2011
Feb 13, 2011
97
info.vfmt = SDL_AllocFormat(mode.format);
Dec 12, 2008
Dec 12, 2008
98
99
info.current_w = mode.w;
info.current_h = mode.h;
100
101
102
103
104
105
106
107
108
109
110
111
112
113
}
return &info;
}
int
SDL_VideoModeOK(int width, int height, int bpp, Uint32 flags)
{
int i, actual_bpp = 0;
if (!SDL_GetVideoDevice()) {
return 0;
}
if (!(flags & SDL_FULLSCREEN)) {
Aug 5, 2006
Aug 5, 2006
114
SDL_DisplayMode mode;
Feb 10, 2011
Feb 10, 2011
115
SDL_GetDesktopDisplayMode(GetVideoDisplay(), &mode);
Aug 5, 2006
Aug 5, 2006
116
return SDL_BITSPERPIXEL(mode.format);
Feb 10, 2011
Feb 10, 2011
119
for (i = 0; i < SDL_GetNumDisplayModes(GetVideoDisplay()); ++i) {
Aug 5, 2006
Aug 5, 2006
120
SDL_DisplayMode mode;
Feb 10, 2011
Feb 10, 2011
121
SDL_GetDisplayMode(GetVideoDisplay(), i, &mode);
Aug 5, 2006
Aug 5, 2006
122
123
if (!mode.w || !mode.h || (width == mode.w && height == mode.h)) {
if (!mode.format) {
124
125
return bpp;
}
Aug 5, 2006
Aug 5, 2006
126
127
if (SDL_BITSPERPIXEL(mode.format) >= (Uint32) bpp) {
actual_bpp = SDL_BITSPERPIXEL(mode.format);
128
129
130
131
132
133
134
}
}
}
return actual_bpp;
}
SDL_Rect **
Jan 13, 2009
Jan 13, 2009
135
SDL_ListModes(const SDL_PixelFormat * format, Uint32 flags)
136
137
138
139
140
141
142
143
144
145
146
147
{
int i, nmodes;
SDL_Rect **modes;
if (!SDL_GetVideoDevice()) {
return NULL;
}
if (!(flags & SDL_FULLSCREEN)) {
return (SDL_Rect **) (-1);
}
Jan 13, 2009
Jan 13, 2009
148
149
150
151
if (!format) {
format = SDL_GetVideoInfo()->vfmt;
}
152
153
154
/* Memory leak, but this is a compatibility function, who cares? */
nmodes = 0;
modes = NULL;
Feb 10, 2011
Feb 10, 2011
155
for (i = 0; i < SDL_GetNumDisplayModes(GetVideoDisplay()); ++i) {
Aug 5, 2006
Aug 5, 2006
156
SDL_DisplayMode mode;
Jan 20, 2011
Jan 20, 2011
157
158
int bpp;
Feb 10, 2011
Feb 10, 2011
159
SDL_GetDisplayMode(GetVideoDisplay(), i, &mode);
Aug 5, 2006
Aug 5, 2006
160
if (!mode.w || !mode.h) {
161
162
return (SDL_Rect **) (-1);
}
Jan 20, 2011
Jan 20, 2011
163
164
165
166
167
168
169
170
171
/* Copied from src/video/SDL_pixels.c:SDL_PixelFormatEnumToMasks */
if (SDL_BYTESPERPIXEL(mode.format) <= 2) {
bpp = SDL_BITSPERPIXEL(mode.format);
} else {
bpp = SDL_BYTESPERPIXEL(mode.format) * 8;
}
if (bpp != format->BitsPerPixel) {
Aug 5, 2006
Aug 5, 2006
174
175
if (nmodes > 0 && modes[nmodes - 1]->w == mode.w
&& modes[nmodes - 1]->h == mode.h) {
176
177
178
179
180
181
182
183
184
185
186
187
188
continue;
}
modes = SDL_realloc(modes, (nmodes + 2) * sizeof(*modes));
if (!modes) {
return NULL;
}
modes[nmodes] = (SDL_Rect *) SDL_malloc(sizeof(SDL_Rect));
if (!modes[nmodes]) {
return NULL;
}
modes[nmodes]->x = 0;
modes[nmodes]->y = 0;
Aug 5, 2006
Aug 5, 2006
189
190
modes[nmodes]->w = mode.w;
modes[nmodes]->h = mode.h;
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
++nmodes;
}
if (modes) {
modes[nmodes] = NULL;
}
return modes;
}
static int
SDL_CompatEventFilter(void *userdata, SDL_Event * event)
{
SDL_Event fake;
switch (event->type) {
case SDL_WINDOWEVENT:
switch (event->window.event) {
case SDL_WINDOWEVENT_EXPOSED:
Mar 25, 2010
Mar 25, 2010
208
if (!SDL_HasEvent(SDL_VIDEOEXPOSE)) {
209
210
211
212
213
fake.type = SDL_VIDEOEXPOSE;
SDL_PushEvent(&fake);
}
break;
case SDL_WINDOWEVENT_RESIZED:
Mar 25, 2010
Mar 25, 2010
214
SDL_FlushEvent(SDL_VIDEORESIZE);
Feb 14, 2011
Feb 14, 2011
215
216
217
218
219
220
221
222
223
/* We don't want to expose that the window width and height will
be different if we don't get the desired fullscreen mode.
*/
if (SDL_VideoWindow && !(SDL_GetWindowFlags(SDL_VideoWindow) & SDL_WINDOW_FULLSCREEN)) {
fake.type = SDL_VIDEORESIZE;
fake.resize.w = event->window.data1;
fake.resize.h = event->window.data2;
SDL_PushEvent(&fake);
}
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
break;
case SDL_WINDOWEVENT_MINIMIZED:
fake.type = SDL_ACTIVEEVENT;
fake.active.gain = 0;
fake.active.state = SDL_APPACTIVE;
SDL_PushEvent(&fake);
break;
case SDL_WINDOWEVENT_RESTORED:
fake.type = SDL_ACTIVEEVENT;
fake.active.gain = 1;
fake.active.state = SDL_APPACTIVE;
SDL_PushEvent(&fake);
break;
case SDL_WINDOWEVENT_ENTER:
fake.type = SDL_ACTIVEEVENT;
fake.active.gain = 1;
fake.active.state = SDL_APPMOUSEFOCUS;
SDL_PushEvent(&fake);
break;
case SDL_WINDOWEVENT_LEAVE:
fake.type = SDL_ACTIVEEVENT;
fake.active.gain = 0;
fake.active.state = SDL_APPMOUSEFOCUS;
SDL_PushEvent(&fake);
break;
case SDL_WINDOWEVENT_FOCUS_GAINED:
fake.type = SDL_ACTIVEEVENT;
fake.active.gain = 1;
fake.active.state = SDL_APPINPUTFOCUS;
SDL_PushEvent(&fake);
break;
case SDL_WINDOWEVENT_FOCUS_LOST:
fake.type = SDL_ACTIVEEVENT;
Oct 28, 2006
Oct 28, 2006
257
fake.active.gain = 0;
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
fake.active.state = SDL_APPINPUTFOCUS;
SDL_PushEvent(&fake);
break;
case SDL_WINDOWEVENT_CLOSE:
fake.type = SDL_QUIT;
SDL_PushEvent(&fake);
break;
}
case SDL_KEYDOWN:
case SDL_KEYUP:
{
Uint32 unicode = 0;
if (event->key.type == SDL_KEYDOWN && event->key.keysym.sym < 256) {
unicode = event->key.keysym.sym;
if (unicode >= 'a' && unicode <= 'z') {
int shifted = !!(event->key.keysym.mod & KMOD_SHIFT);
int capslock = !!(event->key.keysym.mod & KMOD_CAPS);
if ((shifted ^ capslock) != 0) {
unicode = SDL_toupper(unicode);
}
}
}
if (unicode) {
event->key.keysym.unicode = unicode;
}
break;
}
Jun 16, 2007
Jun 16, 2007
285
286
287
case SDL_TEXTINPUT:
{
/* FIXME: Generate an old style key repeat event if needed */
Jun 19, 2007
Jun 19, 2007
288
//printf("TEXTINPUT: '%s'\n", event->text.text);
Jun 16, 2007
Jun 16, 2007
289
290
break;
}
Feb 14, 2011
Feb 14, 2011
291
292
293
294
295
296
297
298
299
300
301
302
303
case SDL_MOUSEMOTION:
{
event->motion.x -= SDL_VideoViewport.x;
event->motion.y -= SDL_VideoViewport.y;
break;
}
case SDL_MOUSEBUTTONDOWN:
case SDL_MOUSEBUTTONUP:
{
event->button.x -= SDL_VideoViewport.x;
event->button.y -= SDL_VideoViewport.y;
break;
}
304
305
306
307
308
case SDL_MOUSEWHEEL:
{
Uint8 button;
int x, y;
Jul 6, 2007
Jul 6, 2007
309
310
311
312
if (event->wheel.y == 0) {
break;
}
Dec 16, 2009
Dec 16, 2009
313
SDL_GetMouseState(&x, &y);
Jul 6, 2007
Jul 6, 2007
315
316
317
318
319
320
321
if (event->wheel.y > 0) {
button = SDL_BUTTON_WHEELUP;
} else {
button = SDL_BUTTON_WHEELDOWN;
}
fake.button.button = button;
322
323
324
325
fake.button.x = x;
fake.button.y = y;
fake.button.windowID = event->wheel.windowID;
Jul 6, 2007
Jul 6, 2007
326
327
328
fake.type = SDL_MOUSEBUTTONDOWN;
fake.button.state = SDL_PRESSED;
SDL_PushEvent(&fake);
Jul 6, 2007
Jul 6, 2007
330
331
332
fake.type = SDL_MOUSEBUTTONUP;
fake.button.state = SDL_RELEASED;
SDL_PushEvent(&fake);
333
334
335
336
337
338
339
340
341
342
break;
}
}
return 1;
}
static void
GetEnvironmentWindowPosition(int w, int h, int *x, int *y)
{
Feb 14, 2011
Feb 14, 2011
343
int display = GetVideoDisplay();
344
345
346
347
348
349
350
351
352
353
354
const char *window = SDL_getenv("SDL_VIDEO_WINDOW_POS");
const char *center = SDL_getenv("SDL_VIDEO_CENTERED");
if (window) {
if (SDL_sscanf(window, "%d,%d", x, y) == 2) {
return;
}
if (SDL_strcmp(window, "center") == 0) {
center = window;
}
}
if (center) {
Feb 14, 2011
Feb 14, 2011
355
356
*x = SDL_WINDOWPOS_CENTERED_DISPLAY(display);
*y = SDL_WINDOWPOS_CENTERED_DISPLAY(display);
Dec 4, 2008
Dec 4, 2008
360
361
362
static void
ClearVideoSurface()
{
Feb 14, 2011
Feb 14, 2011
363
364
365
366
367
368
if (SDL_ShadowSurface) {
SDL_FillRect(SDL_ShadowSurface, NULL,
SDL_MapRGB(SDL_ShadowSurface->format, 0, 0, 0));
}
SDL_FillRect(SDL_WindowSurface, NULL, 0);
SDL_UpdateWindowSurface(SDL_VideoWindow);
Dec 4, 2008
Dec 4, 2008
369
370
}
Jan 12, 2009
Jan 12, 2009
371
static void
Jan 12, 2009
Jan 12, 2009
372
SetupScreenSaver(int flags)
Jan 12, 2009
Jan 12, 2009
373
374
375
376
{
const char *env;
SDL_bool allow_screensaver;
Jan 12, 2009
Jan 12, 2009
377
378
379
380
381
382
383
384
385
/* Allow environment override of screensaver disable */
env = SDL_getenv("SDL_VIDEO_ALLOW_SCREENSAVER");
if (env) {
allow_screensaver = SDL_atoi(env) ? SDL_TRUE : SDL_FALSE;
} else if (flags & SDL_FULLSCREEN) {
allow_screensaver = SDL_FALSE;
} else {
allow_screensaver = SDL_TRUE;
}
Jan 12, 2009
Jan 12, 2009
386
387
388
389
390
391
392
if (allow_screensaver) {
SDL_EnableScreenSaver();
} else {
SDL_DisableScreenSaver();
}
}
Jun 26, 2010
Jun 26, 2010
393
static int
Dec 4, 2008
Dec 4, 2008
394
395
396
397
398
SDL_ResizeVideoMode(int width, int height, int bpp, Uint32 flags)
{
int w, h;
/* We can't resize something we don't have... */
Feb 13, 2011
Feb 13, 2011
399
if (!SDL_VideoSurface) {
Dec 4, 2008
Dec 4, 2008
400
401
402
403
404
405
406
407
408
409
410
411
return -1;
}
/* We probably have to recreate the window in fullscreen mode */
if (flags & SDL_FULLSCREEN) {
return -1;
}
/* I don't think there's any change we can gracefully make in flags */
if (flags != SDL_VideoFlags) {
return -1;
}
Feb 13, 2011
Feb 13, 2011
412
413
414
if (bpp != SDL_VideoSurface->format->BitsPerPixel) {
return -1;
}
Dec 4, 2008
Dec 4, 2008
415
416
417
418
419
420
421
422
423
424
425
426
427
428
/* Resize the window */
SDL_GetWindowSize(SDL_VideoWindow, &w, &h);
if (w != width || h != height) {
SDL_SetWindowSize(SDL_VideoWindow, width, height);
}
/* If we're in OpenGL mode, just resize the stub surface and we're done! */
if (flags & SDL_OPENGL) {
SDL_VideoSurface->w = width;
SDL_VideoSurface->h = height;
return 0;
}
Feb 14, 2011
Feb 14, 2011
429
430
SDL_WindowSurface = SDL_GetWindowSurface(SDL_VideoWindow);
if (!SDL_WindowSurface) {
Dec 4, 2008
Dec 4, 2008
431
432
return -1;
}
Feb 14, 2011
Feb 14, 2011
433
434
435
436
437
438
439
440
if (SDL_VideoSurface->format != SDL_WindowSurface->format) {
return -1;
}
SDL_VideoSurface->w = width;
SDL_VideoSurface->h = height;
SDL_VideoSurface->pixels = SDL_WindowSurface->pixels;
SDL_VideoSurface->pitch = SDL_WindowSurface->pitch;
SDL_SetClipRect(SDL_VideoSurface, NULL);
Dec 4, 2008
Dec 4, 2008
441
442
443
444
if (SDL_ShadowSurface) {
SDL_ShadowSurface->w = width;
SDL_ShadowSurface->h = height;
Dec 5, 2008
Dec 5, 2008
445
SDL_ShadowSurface->pitch = SDL_CalculatePitch(SDL_ShadowSurface);
Dec 4, 2008
Dec 4, 2008
446
447
448
449
SDL_ShadowSurface->pixels =
SDL_realloc(SDL_ShadowSurface->pixels,
SDL_ShadowSurface->h * SDL_ShadowSurface->pitch);
SDL_SetClipRect(SDL_ShadowSurface, NULL);
Dec 5, 2008
Dec 5, 2008
450
SDL_InvalidateMap(SDL_ShadowSurface->map);
Feb 4, 2011
Feb 4, 2011
451
452
} else {
SDL_PublicSurface = SDL_VideoSurface;
Dec 4, 2008
Dec 4, 2008
453
454
455
456
457
458
459
}
ClearVideoSurface();
return 0;
}
460
461
462
SDL_Surface *
SDL_SetVideoMode(int width, int height, int bpp, Uint32 flags)
{
Aug 5, 2006
Aug 5, 2006
463
SDL_DisplayMode desktop_mode;
Feb 14, 2011
Feb 14, 2011
464
465
466
467
468
int display = GetVideoDisplay();
int window_x = SDL_WINDOWPOS_UNDEFINED_DISPLAY(display);
int window_y = SDL_WINDOWPOS_UNDEFINED_DISPLAY(display);
int window_w;
int window_h;
469
470
471
472
473
474
475
476
Uint32 window_flags;
Uint32 surface_flags;
if (!SDL_GetVideoDevice()) {
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE) < 0) {
return NULL;
}
}
May 23, 2009
May 23, 2009
477
Feb 14, 2011
Feb 14, 2011
478
SDL_GetDesktopDisplayMode(display, &desktop_mode);
Mar 23, 2009
Mar 23, 2009
479
480
481
482
483
484
485
if (width == 0) {
width = desktop_mode.w;
}
if (height == 0) {
height = desktop_mode.h;
}
Feb 3, 2011
Feb 3, 2011
486
487
488
if (bpp == 0) {
bpp = SDL_BITSPERPIXEL(desktop_mode.format);
}
Dec 4, 2008
Dec 4, 2008
490
491
492
493
494
/* See if we can simply resize the existing window and surface */
if (SDL_ResizeVideoMode(width, height, bpp, flags) == 0) {
return SDL_PublicSurface;
}
495
496
497
/* Destroy existing window */
SDL_PublicSurface = NULL;
if (SDL_ShadowSurface) {
Feb 13, 2011
Feb 13, 2011
498
SDL_ShadowSurface->flags &= ~SDL_DONTFREE;
499
500
501
502
SDL_FreeSurface(SDL_ShadowSurface);
SDL_ShadowSurface = NULL;
}
if (SDL_VideoSurface) {
Feb 14, 2011
Feb 14, 2011
503
SDL_VideoSurface->flags &= ~SDL_DONTFREE;
504
505
506
SDL_FreeSurface(SDL_VideoSurface);
SDL_VideoSurface = NULL;
}
Jul 16, 2006
Jul 16, 2006
507
if (SDL_VideoContext) {
Mar 14, 2008
Mar 14, 2008
508
/* SDL_GL_MakeCurrent(0, NULL); *//* Doesn't do anything */
Jul 16, 2006
Jul 16, 2006
509
510
511
SDL_GL_DeleteContext(SDL_VideoContext);
SDL_VideoContext = NULL;
}
512
513
if (SDL_VideoWindow) {
SDL_GetWindowPosition(SDL_VideoWindow, &window_x, &window_y);
Mar 6, 2008
Mar 6, 2008
514
SDL_DestroyWindow(SDL_VideoWindow);
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
}
/* Set up the event filter */
if (!SDL_GetEventFilter(NULL, NULL)) {
SDL_SetEventFilter(SDL_CompatEventFilter, NULL);
}
/* Create a new window */
window_flags = SDL_WINDOW_SHOWN;
if (flags & SDL_FULLSCREEN) {
window_flags |= SDL_WINDOW_FULLSCREEN;
}
if (flags & SDL_OPENGL) {
window_flags |= SDL_WINDOW_OPENGL;
}
if (flags & SDL_RESIZABLE) {
window_flags |= SDL_WINDOW_RESIZABLE;
}
if (flags & SDL_NOFRAME) {
window_flags |= SDL_WINDOW_BORDERLESS;
}
GetEnvironmentWindowPosition(width, height, &window_x, &window_y);
SDL_VideoWindow =
SDL_CreateWindow(wm_title, window_x, window_y, width, height,
window_flags);
if (!SDL_VideoWindow) {
return NULL;
}
Jan 2, 2009
Jan 2, 2009
543
SDL_SetWindowIcon(SDL_VideoWindow, SDL_VideoIcon);
Feb 16, 2011
Feb 16, 2011
545
546
SetupScreenSaver(flags);
547
548
549
550
551
window_flags = SDL_GetWindowFlags(SDL_VideoWindow);
surface_flags = 0;
if (window_flags & SDL_WINDOW_FULLSCREEN) {
surface_flags |= SDL_FULLSCREEN;
}
Feb 12, 2011
Feb 12, 2011
552
if ((window_flags & SDL_WINDOW_OPENGL) && (flags & SDL_OPENGL)) {
553
554
555
556
557
558
559
560
561
surface_flags |= SDL_OPENGL;
}
if (window_flags & SDL_WINDOW_RESIZABLE) {
surface_flags |= SDL_RESIZABLE;
}
if (window_flags & SDL_WINDOW_BORDERLESS) {
surface_flags |= SDL_NOFRAME;
}
Feb 16, 2011
Feb 16, 2011
562
563
SDL_VideoFlags = flags;
564
565
/* If we're in OpenGL mode, just create a stub surface and we're done! */
if (flags & SDL_OPENGL) {
Jul 16, 2006
Jul 16, 2006
566
567
568
569
570
571
572
SDL_VideoContext = SDL_GL_CreateContext(SDL_VideoWindow);
if (!SDL_VideoContext) {
return NULL;
}
if (SDL_GL_MakeCurrent(SDL_VideoWindow, SDL_VideoContext) < 0) {
return NULL;
}
573
574
575
576
577
578
579
580
581
582
583
SDL_VideoSurface =
SDL_CreateRGBSurfaceFrom(NULL, width, height, bpp, 0, 0, 0, 0, 0);
if (!SDL_VideoSurface) {
return NULL;
}
SDL_VideoSurface->flags |= surface_flags;
SDL_PublicSurface = SDL_VideoSurface;
return SDL_PublicSurface;
}
/* Create the screen surface */
Feb 14, 2011
Feb 14, 2011
584
585
SDL_WindowSurface = SDL_GetWindowSurface(SDL_VideoWindow);
if (!SDL_WindowSurface) {
586
587
return NULL;
}
Feb 14, 2011
Feb 14, 2011
588
589
590
591
592
593
594
595
596
/* Center the public surface in the window surface */
SDL_GetWindowSize(SDL_VideoWindow, &window_w, &window_h);
SDL_VideoViewport.x = (window_w - width)/2;
SDL_VideoViewport.y = (window_h - height)/2;
SDL_VideoViewport.w = width;
SDL_VideoViewport.h = height;
SDL_VideoSurface = SDL_CreateRGBSurfaceFrom(NULL, 0, 0, 32, 0, 0, 0, 0, 0);
597
SDL_VideoSurface->flags |= surface_flags;
Feb 14, 2011
Feb 14, 2011
598
599
600
601
602
603
604
605
606
607
608
SDL_VideoSurface->flags |= SDL_DONTFREE;
SDL_FreeFormat(SDL_VideoSurface->format);
SDL_VideoSurface->format = SDL_WindowSurface->format;
SDL_VideoSurface->format->refcount++;
SDL_VideoSurface->w = width;
SDL_VideoSurface->h = height;
SDL_VideoSurface->pitch = SDL_WindowSurface->pitch;
SDL_VideoSurface->pixels = (void *)((Uint8 *)SDL_WindowSurface->pixels +
SDL_VideoViewport.y * SDL_VideoSurface->pitch +
SDL_VideoViewport.x * SDL_VideoSurface->format->BytesPerPixel);
SDL_SetClipRect(SDL_VideoSurface, NULL);
609
610
/* Create a shadow surface if necessary */
Aug 11, 2007
Aug 11, 2007
611
612
613
614
if ((bpp != SDL_VideoSurface->format->BitsPerPixel)
&& !(flags & SDL_ANYFORMAT)) {
SDL_ShadowSurface =
SDL_CreateRGBSurface(0, width, height, bpp, 0, 0, 0, 0);
615
616
617
618
if (!SDL_ShadowSurface) {
return NULL;
}
SDL_ShadowSurface->flags |= surface_flags;
Feb 13, 2011
Feb 13, 2011
619
SDL_ShadowSurface->flags |= SDL_DONTFREE;
620
621
622
623
/* 8-bit SDL_ShadowSurface surfaces report that they have exclusive palette */
if (SDL_ShadowSurface->format->palette) {
SDL_ShadowSurface->flags |= SDL_HWPALETTE;
Feb 2, 2011
Feb 2, 2011
624
625
SDL_DitherColors(SDL_ShadowSurface->format->palette->colors,
SDL_ShadowSurface->format->BitsPerPixel);
Feb 14, 2011
Feb 14, 2011
627
628
SDL_FillRect(SDL_ShadowSurface, NULL,
SDL_MapRGB(SDL_ShadowSurface->format, 0, 0, 0));
629
630
631
632
}
SDL_PublicSurface =
(SDL_ShadowSurface ? SDL_ShadowSurface : SDL_VideoSurface);
Dec 4, 2008
Dec 4, 2008
633
ClearVideoSurface();
634
635
636
637
638
639
640
641
642
643
644
/* We're finally done! */
return SDL_PublicSurface;
}
SDL_Surface *
SDL_GetVideoSurface(void)
{
return SDL_PublicSurface;
}
Aug 18, 2007
Aug 18, 2007
645
646
647
int
SDL_SetAlpha(SDL_Surface * surface, Uint32 flag, Uint8 value)
{
Dec 6, 2008
Dec 6, 2008
648
649
650
651
652
if (flag & SDL_SRCALPHA) {
/* According to the docs, value is ignored for alpha surfaces */
if (surface->format->Amask) {
value = 0xFF;
}
Aug 18, 2007
Aug 18, 2007
653
SDL_SetSurfaceAlphaMod(surface, value);
Dec 20, 2008
Dec 20, 2008
654
SDL_SetSurfaceBlendMode(surface, SDL_BLENDMODE_BLEND);
Aug 18, 2007
Aug 18, 2007
655
656
} else {
SDL_SetSurfaceAlphaMod(surface, 0xFF);
Dec 20, 2008
Dec 20, 2008
657
SDL_SetSurfaceBlendMode(surface, SDL_BLENDMODE_NONE);
Aug 18, 2007
Aug 18, 2007
658
}
Dec 6, 2008
Dec 6, 2008
659
660
SDL_SetSurfaceRLE(surface, (flag & SDL_RLEACCEL));
Aug 18, 2007
Aug 18, 2007
661
662
663
return 0;
}
664
665
666
SDL_Surface *
SDL_DisplayFormat(SDL_Surface * surface)
{
Nov 29, 2008
Nov 29, 2008
667
SDL_PixelFormat *format;
668
669
670
671
672
if (!SDL_PublicSurface) {
SDL_SetError("No video mode has been set");
return NULL;
}
Nov 29, 2008
Nov 29, 2008
673
format = SDL_PublicSurface->format;
674
675
/* Set the flags appropriate for copying to display surface */
Nov 29, 2008
Nov 29, 2008
676
return SDL_ConvertSurface(surface, format, SDL_RLEACCEL);
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
}
SDL_Surface *
SDL_DisplayFormatAlpha(SDL_Surface * surface)
{
SDL_PixelFormat *vf;
SDL_PixelFormat *format;
SDL_Surface *converted;
/* default to ARGB8888 */
Uint32 amask = 0xff000000;
Uint32 rmask = 0x00ff0000;
Uint32 gmask = 0x0000ff00;
Uint32 bmask = 0x000000ff;
if (!SDL_PublicSurface) {
SDL_SetError("No video mode has been set");
return NULL;
}
vf = SDL_PublicSurface->format;
switch (vf->BytesPerPixel) {
case 2:
/* For XGY5[56]5, use, AXGY8888, where {X, Y} = {R, B}.
For anything else (like ARGB4444) it doesn't matter
since we have no special code for it anyway */
if ((vf->Rmask == 0x1f) &&
(vf->Bmask == 0xf800 || vf->Bmask == 0x7c00)) {
rmask = 0xff;
bmask = 0xff0000;
}
break;
case 3:
case 4:
/* Keep the video format, as long as the high 8 bits are
unused or alpha */
if ((vf->Rmask == 0xff) && (vf->Bmask == 0xff0000)) {
rmask = 0xff;
bmask = 0xff0000;
}
break;
default:
/* We have no other optimised formats right now. When/if a new
optimised alpha format is written, add the converter here */
break;
}
Feb 13, 2011
Feb 13, 2011
724
725
726
727
728
729
730
format = SDL_AllocFormat(SDL_MasksToPixelFormatEnum(32, rmask,
gmask,
bmask,
amask));
if (!format) {
return NULL;
}
Nov 29, 2008
Nov 29, 2008
731
converted = SDL_ConvertSurface(surface, format, SDL_RLEACCEL);
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
SDL_FreeFormat(format);
return converted;
}
int
SDL_Flip(SDL_Surface * screen)
{
SDL_UpdateRect(screen, 0, 0, 0, 0);
return 0;
}
void
SDL_UpdateRect(SDL_Surface * screen, Sint32 x, Sint32 y, Uint32 w, Uint32 h)
{
if (screen) {
SDL_Rect rect;
/* Fill the rectangle */
Jul 12, 2006
Jul 12, 2006
750
751
752
753
rect.x = (int) x;
rect.y = (int) y;
rect.w = (int) (w ? w : screen->w);
rect.h = (int) (h ? h : screen->h);
754
755
756
SDL_UpdateRects(screen, 1, &rect);
}
}
Aug 27, 2008
Aug 27, 2008
757
758
759
760
761
762
763
764
void
SDL_UpdateRects(SDL_Surface * screen, int numrects, SDL_Rect * rects)
{
int i;
if (screen == SDL_ShadowSurface) {
for (i = 0; i < numrects; ++i) {
Mar 14, 2011
Mar 14, 2011
765
766
SDL_BlitSurface(SDL_ShadowSurface, &rects[i], SDL_VideoSurface,
&rects[i]);
767
768
769
770
771
772
}
/* Fall through to video surface update */
screen = SDL_VideoSurface;
}
if (screen == SDL_VideoSurface) {
Feb 14, 2011
Feb 14, 2011
773
774
775
776
777
778
779
780
781
782
783
784
785
786
if (SDL_VideoViewport.x || SDL_VideoViewport.y) {
SDL_Rect *stackrects = SDL_stack_alloc(SDL_Rect, numrects);
SDL_Rect *stackrect;
const SDL_Rect *rect;
/* Offset all the rectangles before updating */
for (i = 0; i < numrects; ++i) {
rect = &rects[i];
stackrect = &stackrects[i];
stackrect->x = SDL_VideoViewport.x + rect->x;
stackrect->y = SDL_VideoViewport.y + rect->y;
stackrect->w = rect->w;
stackrect->h = rect->h;
}
Feb 15, 2011
Feb 15, 2011
787
SDL_UpdateWindowSurfaceRects(SDL_VideoWindow, stackrects, numrects);
Feb 14, 2011
Feb 14, 2011
788
789
SDL_stack_free(stackrects);
} else {
Feb 15, 2011
Feb 15, 2011
790
SDL_UpdateWindowSurfaceRects(SDL_VideoWindow, rects, numrects);
Feb 14, 2011
Feb 14, 2011
791
}
792
793
794
795
796
797
798
799
}
}
void
SDL_WM_SetCaption(const char *title, const char *icon)
{
if (wm_title) {
SDL_free(wm_title);
Sep 23, 2006
Sep 23, 2006
800
801
}
if (title) {
802
wm_title = SDL_strdup(title);
Sep 23, 2006
Sep 23, 2006
803
804
} else {
wm_title = NULL;
805
806
807
808
809
}
SDL_SetWindowTitle(SDL_VideoWindow, wm_title);
}
void
Sep 23, 2006
Sep 23, 2006
810
SDL_WM_GetCaption(const char **title, const char **icon)
811
812
813
814
815
816
817
818
819
820
821
822
{
if (title) {
*title = wm_title;
}
if (icon) {
*icon = "";
}
}
void
SDL_WM_SetIcon(SDL_Surface * icon, Uint8 * mask)
{
Jan 2, 2009
Jan 2, 2009
823
SDL_VideoIcon = icon;
824
825
826
827
828
829
830
831
832
833
834
835
}
int
SDL_WM_IconifyWindow(void)
{
SDL_MinimizeWindow(SDL_VideoWindow);
return 0;
}
int
SDL_WM_ToggleFullScreen(SDL_Surface * surface)
{
Feb 16, 2011
Feb 16, 2011
836
837
838
839
840
841
842
843
844
845
846
847
848
int length;
void *pixels;
Uint8 *src, *dst;
int row;
int window_w;
int window_h;
if (!SDL_PublicSurface) {
SDL_SetError("SDL_SetVideoMode() hasn't been called");
return 0;
}
/* Copy the old bits out */
Feb 16, 2011
Feb 16, 2011
849
850
length = SDL_PublicSurface->w * SDL_PublicSurface->format->BytesPerPixel;
pixels = SDL_malloc(SDL_PublicSurface->h * length);
Feb 16, 2011
Feb 16, 2011
851
if (pixels) {
Feb 16, 2011
Feb 16, 2011
852
src = (Uint8*)SDL_PublicSurface->pixels;
Feb 16, 2011
Feb 16, 2011
853
dst = (Uint8*)pixels;
Feb 16, 2011
Feb 16, 2011
854
for (row = 0; row < SDL_PublicSurface->h; ++row) {
Feb 16, 2011
Feb 16, 2011
855
SDL_memcpy(dst, src, length);
Feb 16, 2011
Feb 16, 2011
856
src += SDL_PublicSurface->pitch;
Feb 16, 2011
Feb 16, 2011
857
858
859
860
861
dst += length;
}
}
/* Do the physical mode switch */
862
863
864
865
866
867
868
869
870
871
872
if (SDL_GetWindowFlags(SDL_VideoWindow) & SDL_WINDOW_FULLSCREEN) {
if (SDL_SetWindowFullscreen(SDL_VideoWindow, 0) < 0) {
return 0;
}
SDL_PublicSurface->flags &= ~SDL_FULLSCREEN;
} else {
if (SDL_SetWindowFullscreen(SDL_VideoWindow, 1) < 0) {
return 0;
}
SDL_PublicSurface->flags |= SDL_FULLSCREEN;
}
Feb 16, 2011
Feb 16, 2011
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
/* Recreate the screen surface */
SDL_WindowSurface = SDL_GetWindowSurface(SDL_VideoWindow);
if (!SDL_WindowSurface) {
/* We're totally hosed... */
return 0;
}
/* Center the public surface in the window surface */
SDL_GetWindowSize(SDL_VideoWindow, &window_w, &window_h);
SDL_VideoViewport.x = (window_w - SDL_VideoSurface->w)/2;
SDL_VideoViewport.y = (window_h - SDL_VideoSurface->h)/2;
SDL_VideoViewport.w = SDL_VideoSurface->w;
SDL_VideoViewport.h = SDL_VideoSurface->h;
/* Do some shuffling behind the application's back if format changes */
if (SDL_VideoSurface->format->format != SDL_WindowSurface->format->format) {
if (SDL_ShadowSurface) {
if (SDL_ShadowSurface->format->format == SDL_WindowSurface->format->format) {
/* Whee! We don't need a shadow surface anymore! */
SDL_VideoSurface->flags &= ~SDL_DONTFREE;
SDL_FreeSurface(SDL_VideoSurface);
SDL_free(SDL_ShadowSurface->pixels);
SDL_VideoSurface = SDL_ShadowSurface;
Feb 16, 2011
Feb 16, 2011
897
SDL_VideoSurface->flags |= SDL_PREALLOC;
Feb 16, 2011
Feb 16, 2011
898
899
900
901
902
903
904
905
906
907
908
SDL_ShadowSurface = NULL;
} else {
/* No problem, just change the video surface format */
SDL_FreeFormat(SDL_VideoSurface->format);
SDL_VideoSurface->format = SDL_WindowSurface->format;
SDL_VideoSurface->format->refcount++;
SDL_InvalidateMap(SDL_ShadowSurface->map);
}
} else {
/* We can make the video surface the shadow surface */
SDL_ShadowSurface = SDL_VideoSurface;
Feb 16, 2011
Feb 16, 2011
909
910
911
912
913
914
915
916
SDL_ShadowSurface->pitch = SDL_CalculatePitch(SDL_ShadowSurface);
SDL_ShadowSurface->pixels = SDL_malloc(SDL_ShadowSurface->h * SDL_ShadowSurface->pitch);
if (!SDL_ShadowSurface->pixels) {
/* Uh oh, we're hosed */
SDL_ShadowSurface = NULL;
return 0;
}
SDL_ShadowSurface->flags &= ~SDL_PREALLOC;
Feb 16, 2011
Feb 16, 2011
917
918
919
SDL_VideoSurface = SDL_CreateRGBSurfaceFrom(NULL, 0, 0, 32, 0, 0, 0, 0, 0);
SDL_VideoSurface->flags = SDL_ShadowSurface->flags;
Feb 16, 2011
Feb 16, 2011
920
SDL_VideoSurface->flags |= SDL_PREALLOC;
Feb 16, 2011
Feb 16, 2011
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
SDL_FreeFormat(SDL_VideoSurface->format);
SDL_VideoSurface->format = SDL_WindowSurface->format;
SDL_VideoSurface->format->refcount++;
SDL_VideoSurface->w = SDL_ShadowSurface->w;
SDL_VideoSurface->h = SDL_ShadowSurface->h;
}
}
/* Update the video surface */
SDL_VideoSurface->pitch = SDL_WindowSurface->pitch;
SDL_VideoSurface->pixels = (void *)((Uint8 *)SDL_WindowSurface->pixels +
SDL_VideoViewport.y * SDL_VideoSurface->pitch +
SDL_VideoViewport.x * SDL_VideoSurface->format->BytesPerPixel);
SDL_SetClipRect(SDL_VideoSurface, NULL);
/* Copy the old bits back */
if (pixels) {
src = (Uint8*)pixels;
Feb 16, 2011
Feb 16, 2011
939
940
dst = (Uint8*)SDL_PublicSurface->pixels;
for (row = 0; row < SDL_PublicSurface->h; ++row) {
Feb 16, 2011
Feb 16, 2011
941
942
SDL_memcpy(dst, src, length);
src += length;
Feb 16, 2011
Feb 16, 2011
943
dst += SDL_PublicSurface->pitch;
Feb 16, 2011
Feb 16, 2011
944
}
Feb 16, 2011
Feb 16, 2011
945
SDL_Flip(SDL_PublicSurface);
Feb 16, 2011
Feb 16, 2011
946
947
948
949
SDL_free(pixels);
}
/* We're done! */
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
return 1;
}
SDL_GrabMode
SDL_WM_GrabInput(SDL_GrabMode mode)
{
if (mode != SDL_GRAB_QUERY) {
SDL_SetWindowGrab(SDL_VideoWindow, mode);
}
return (SDL_GrabMode) SDL_GetWindowGrab(SDL_VideoWindow);
}
void
SDL_WarpMouse(Uint16 x, Uint16 y)
{
SDL_WarpMouseInWindow(SDL_VideoWindow, x, y);
}
Uint8
SDL_GetAppState(void)
{
Uint8 state = 0;
Uint32 flags = 0;
flags = SDL_GetWindowFlags(SDL_VideoWindow);
if ((flags & SDL_WINDOW_SHOWN) && !(flags & SDL_WINDOW_MINIMIZED)) {
state |= SDL_APPACTIVE;
}
if (flags & SDL_WINDOW_INPUT_FOCUS) {
state |= SDL_APPINPUTFOCUS;
}
if (flags & SDL_WINDOW_MOUSE_FOCUS) {
state |= SDL_APPMOUSEFOCUS;
}
return state;
}
const SDL_version *
SDL_Linked_Version(void)
{
static SDL_version version;
SDL_VERSION(&version);
return &version;
}
int
SDL_SetPalette(SDL_Surface * surface, int flags, const SDL_Color * colors,
int firstcolor, int ncolors)
{
return SDL_SetColors(surface, colors, firstcolor, ncolors);
}