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

Latest commit

 

History

History
1779 lines (1581 loc) · 47.5 KB

SDL_compat.c

File metadata and controls

1779 lines (1581 loc) · 47.5 KB
 
1
2
/*
SDL - Simple DirectMedia Layer
Dec 8, 2008
Dec 8, 2008
3
Copyright (C) 1997-2009 Sam Lantinga
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
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"
/* 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"
Nov 24, 2008
Nov 24, 2008
31
#include "video/SDL_yuv_sw_c.h"
Mar 14, 2008
Mar 14, 2008
33
static SDL_WindowID SDL_VideoWindow = 0;
Jul 15, 2006
Jul 15, 2006
34
static SDL_RendererInfo SDL_VideoRendererInfo;
Mar 14, 2008
Mar 14, 2008
35
36
37
38
39
static SDL_TextureID SDL_VideoTexture = 0;
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
40
static Uint32 SDL_VideoFlags = 0;
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;
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
char *
SDL_AudioDriverName(char *namebuf, int maxlen)
{
const char *name = SDL_GetCurrentAudioDriver();
if (name) {
SDL_strlcpy(namebuf, name, maxlen);
return namebuf;
}
return NULL;
}
char *
SDL_VideoDriverName(char *namebuf, int maxlen)
{
const char *name = SDL_GetCurrentVideoDriver();
if (name) {
SDL_strlcpy(namebuf, name, maxlen);
return namebuf;
}
return NULL;
}
Dec 3, 2009
Dec 3, 2009
67
68
69
70
71
72
73
74
75
76
77
78
static void
SelectVideoDisplay()
{
const char *variable = SDL_getenv("SDL_VIDEO_FULLSCREEN_DISPLAY");
if ( !variable ) {
variable = SDL_getenv("SDL_VIDEO_FULLSCREEN_HEAD");
}
if ( variable ) {
SDL_SelectVideoDisplay(SDL_atoi(variable));
}
}
79
80
81
82
const SDL_VideoInfo *
SDL_GetVideoInfo(void)
{
static SDL_VideoInfo info;
Aug 5, 2006
Aug 5, 2006
83
SDL_DisplayMode mode;
Dec 3, 2009
Dec 3, 2009
85
86
SelectVideoDisplay();
87
/* Memory leak, compatibility code, who cares? */
Aug 5, 2006
Aug 5, 2006
88
if (!info.vfmt && SDL_GetDesktopDisplayMode(&mode) == 0) {
89
90
91
int bpp;
Uint32 Rmask, Gmask, Bmask, Amask;
Aug 5, 2006
Aug 5, 2006
92
93
SDL_PixelFormatEnumToMasks(mode.format, &bpp, &Rmask, &Gmask, &Bmask,
&Amask);
94
info.vfmt = SDL_AllocFormat(bpp, Rmask, Gmask, Bmask, Amask);
Dec 12, 2008
Dec 12, 2008
95
96
info.current_w = mode.w;
info.current_h = mode.h;
97
98
99
100
101
102
103
104
105
106
107
108
109
}
return &info;
}
int
SDL_VideoModeOK(int width, int height, int bpp, Uint32 flags)
{
int i, actual_bpp = 0;
if (!SDL_GetVideoDevice()) {
return 0;
}
Dec 3, 2009
Dec 3, 2009
110
111
SelectVideoDisplay();
112
if (!(flags & SDL_FULLSCREEN)) {
Aug 5, 2006
Aug 5, 2006
113
114
115
SDL_DisplayMode mode;
SDL_GetDesktopDisplayMode(&mode);
return SDL_BITSPERPIXEL(mode.format);
116
117
118
}
for (i = 0; i < SDL_GetNumDisplayModes(); ++i) {
Aug 5, 2006
Aug 5, 2006
119
120
121
122
SDL_DisplayMode mode;
SDL_GetDisplayMode(i, &mode);
if (!mode.w || !mode.h || (width == mode.w && height == mode.h)) {
if (!mode.format) {
123
124
return bpp;
}
Aug 5, 2006
Aug 5, 2006
125
126
if (SDL_BITSPERPIXEL(mode.format) >= (Uint32) bpp) {
actual_bpp = SDL_BITSPERPIXEL(mode.format);
127
128
129
130
131
132
133
}
}
}
return actual_bpp;
}
SDL_Rect **
Jan 13, 2009
Jan 13, 2009
134
SDL_ListModes(const SDL_PixelFormat * format, Uint32 flags)
135
136
137
138
139
140
141
142
{
int i, nmodes;
SDL_Rect **modes;
if (!SDL_GetVideoDevice()) {
return NULL;
}
Dec 3, 2009
Dec 3, 2009
143
144
SelectVideoDisplay();
145
146
147
148
if (!(flags & SDL_FULLSCREEN)) {
return (SDL_Rect **) (-1);
}
Jan 13, 2009
Jan 13, 2009
149
150
151
152
if (!format) {
format = SDL_GetVideoInfo()->vfmt;
}
153
154
155
156
/* Memory leak, but this is a compatibility function, who cares? */
nmodes = 0;
modes = NULL;
for (i = 0; i < SDL_GetNumDisplayModes(); ++i) {
Aug 5, 2006
Aug 5, 2006
157
158
159
SDL_DisplayMode mode;
SDL_GetDisplayMode(i, &mode);
if (!mode.w || !mode.h) {
160
161
return (SDL_Rect **) (-1);
}
Aug 5, 2006
Aug 5, 2006
162
if (SDL_BITSPERPIXEL(mode.format) != format->BitsPerPixel) {
Aug 5, 2006
Aug 5, 2006
165
166
if (nmodes > 0 && modes[nmodes - 1]->w == mode.w
&& modes[nmodes - 1]->h == mode.h) {
167
168
169
170
171
172
173
174
175
176
177
178
179
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
180
181
modes[nmodes]->w = mode.w;
modes[nmodes]->h = mode.h;
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
++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:
if (!SDL_HasEvent(SDL_VIDEOEXPOSEMASK)) {
fake.type = SDL_VIDEOEXPOSE;
SDL_PushEvent(&fake);
}
break;
case SDL_WINDOWEVENT_RESIZED:
Dec 4, 2008
Dec 4, 2008
205
SDL_PeepEvents(&fake, 1, SDL_GETEVENT, SDL_VIDEORESIZEMASK);
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
fake.type = SDL_VIDEORESIZE;
fake.resize.w = event->window.data1;
fake.resize.h = event->window.data2;
SDL_PushEvent(&fake);
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
243
fake.active.gain = 0;
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
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
271
272
273
case SDL_TEXTINPUT:
{
/* FIXME: Generate an old style key repeat event if needed */
Jun 19, 2007
Jun 19, 2007
274
//printf("TEXTINPUT: '%s'\n", event->text.text);
Jun 16, 2007
Jun 16, 2007
275
276
break;
}
277
278
279
280
281
282
case SDL_MOUSEWHEEL:
{
Uint8 button;
int selected;
int x, y;
Jul 6, 2007
Jul 6, 2007
283
284
285
286
if (event->wheel.y == 0) {
break;
}
287
selected = SDL_SelectMouse(event->wheel.which);
Dec 16, 2009
Dec 16, 2009
288
SDL_GetMouseState(&x, &y);
289
290
SDL_SelectMouse(selected);
Jul 6, 2007
Jul 6, 2007
291
292
293
294
295
296
if (event->wheel.y > 0) {
button = SDL_BUTTON_WHEELUP;
} else {
button = SDL_BUTTON_WHEELDOWN;
}
297
fake.button.which = event->wheel.windowID;
Jul 6, 2007
Jul 6, 2007
298
fake.button.button = button;
299
300
301
302
fake.button.x = x;
fake.button.y = y;
fake.button.windowID = event->wheel.windowID;
Jul 6, 2007
Jul 6, 2007
303
304
305
fake.type = SDL_MOUSEBUTTONDOWN;
fake.button.state = SDL_PRESSED;
SDL_PushEvent(&fake);
Jul 6, 2007
Jul 6, 2007
307
308
309
fake.type = SDL_MOUSEBUTTONUP;
fake.button.state = SDL_RELEASED;
SDL_PushEvent(&fake);
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
break;
}
}
return 1;
}
static int
SDL_VideoPaletteChanged(void *userdata, SDL_Palette * palette)
{
if (userdata == SDL_ShadowSurface) {
/* If the shadow palette changed, make the changes visible */
if (!SDL_VideoSurface->format->palette) {
SDL_UpdateRect(SDL_ShadowSurface, 0, 0, 0, 0);
}
}
if (userdata == SDL_VideoSurface) {
Aug 6, 2006
Aug 6, 2006
327
328
329
330
331
332
333
if (SDL_SetDisplayPalette(palette->colors, 0, palette->ncolors) < 0) {
return -1;
}
if (SDL_SetTexturePalette
(SDL_VideoTexture, palette->colors, 0, palette->ncolors) < 0) {
return -1;
}
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
}
return 0;
}
static void
GetEnvironmentWindowPosition(int w, int h, int *x, int *y)
{
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) {
Aug 5, 2006
Aug 5, 2006
352
353
354
355
SDL_DisplayMode mode;
SDL_GetDesktopDisplayMode(&mode);
*x = (mode.w - w) / 2;
*y = (mode.h - h) / 2;
Aug 11, 2007
Aug 11, 2007
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
static SDL_Surface *
CreateVideoSurface(SDL_TextureID textureID)
{
SDL_Surface *surface;
Uint32 format;
int w, h;
int bpp;
Uint32 Rmask, Gmask, Bmask, Amask;
void *pixels;
int pitch;
if (SDL_QueryTexture(textureID, &format, NULL, &w, &h) < 0) {
return NULL;
}
if (!SDL_PixelFormatEnumToMasks
(format, &bpp, &Rmask, &Gmask, &Bmask, &Amask)) {
SDL_SetError("Unknown texture format");
return NULL;
}
if (SDL_QueryTexturePixels(textureID, &pixels, &pitch) == 0) {
surface =
SDL_CreateRGBSurfaceFrom(pixels, w, h, bpp, pitch, Rmask, Gmask,
Bmask, Amask);
} else {
surface =
SDL_CreateRGBSurface(0, w, h, bpp, Rmask, Gmask, Bmask, Amask);
}
return surface;
}
Dec 4, 2008
Dec 4, 2008
391
392
393
394
395
396
397
398
399
400
401
static void
ClearVideoSurface()
{
Uint32 black;
/* Clear the surface for display */
black = SDL_MapRGB(SDL_PublicSurface->format, 0, 0, 0);
SDL_FillRect(SDL_PublicSurface, NULL, black);
SDL_UpdateRect(SDL_PublicSurface, 0, 0, 0, 0);
}
Jan 12, 2009
Jan 12, 2009
402
static void
Jan 12, 2009
Jan 12, 2009
403
SetupScreenSaver(int flags)
Jan 12, 2009
Jan 12, 2009
404
405
406
407
{
const char *env;
SDL_bool allow_screensaver;
Jan 12, 2009
Jan 12, 2009
408
409
410
411
412
413
414
415
416
/* 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
417
418
419
420
421
422
423
if (allow_screensaver) {
SDL_EnableScreenSaver();
} else {
SDL_DisableScreenSaver();
}
}
Dec 4, 2008
Dec 4, 2008
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
int
SDL_ResizeVideoMode(int width, int height, int bpp, Uint32 flags)
{
int w, h;
Uint32 format;
int access;
void *pixels;
int pitch;
/* We can't resize something we don't have... */
if (!SDL_VideoWindow) {
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;
}
/* 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;
}
/* Destroy the screen texture and recreate it */
SDL_QueryTexture(SDL_VideoTexture, &format, &access, &w, &h);
SDL_DestroyTexture(SDL_VideoTexture);
SDL_VideoTexture = SDL_CreateTexture(format, access, width, height);
if (!SDL_VideoTexture) {
return -1;
}
SDL_VideoSurface->w = width;
SDL_VideoSurface->h = height;
if (SDL_QueryTexturePixels(SDL_VideoTexture, &pixels, &pitch) == 0) {
SDL_VideoSurface->pixels = pixels;
SDL_VideoSurface->pitch = pitch;
} else {
SDL_CalculatePitch(SDL_VideoSurface);
SDL_VideoSurface->pixels =
SDL_realloc(SDL_VideoSurface->pixels,
SDL_VideoSurface->h * SDL_VideoSurface->pitch);
}
SDL_SetClipRect(SDL_VideoSurface, NULL);
Dec 5, 2008
Dec 5, 2008
481
SDL_InvalidateMap(SDL_VideoSurface->map);
Dec 4, 2008
Dec 4, 2008
482
483
484
485
if (SDL_ShadowSurface) {
SDL_ShadowSurface->w = width;
SDL_ShadowSurface->h = height;
Dec 5, 2008
Dec 5, 2008
486
SDL_ShadowSurface->pitch = SDL_CalculatePitch(SDL_ShadowSurface);
Dec 4, 2008
Dec 4, 2008
487
488
489
490
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
491
SDL_InvalidateMap(SDL_ShadowSurface->map);
Dec 4, 2008
Dec 4, 2008
492
493
494
495
496
497
498
}
ClearVideoSurface();
return 0;
}
499
500
501
SDL_Surface *
SDL_SetVideoMode(int width, int height, int bpp, Uint32 flags)
{
Aug 5, 2006
Aug 5, 2006
502
SDL_DisplayMode desktop_mode;
503
504
505
506
507
508
509
510
511
512
513
514
int window_x = SDL_WINDOWPOS_UNDEFINED;
int window_y = SDL_WINDOWPOS_UNDEFINED;
Uint32 window_flags;
Uint32 desktop_format;
Uint32 desired_format;
Uint32 surface_flags;
if (!SDL_GetVideoDevice()) {
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE) < 0) {
return NULL;
}
}
May 23, 2009
May 23, 2009
515
Dec 3, 2009
Dec 3, 2009
516
517
SelectVideoDisplay();
Mar 23, 2009
Mar 23, 2009
518
519
520
521
522
523
524
525
SDL_GetDesktopDisplayMode(&desktop_mode);
if (width == 0) {
width = desktop_mode.w;
}
if (height == 0) {
height = desktop_mode.h;
}
Dec 4, 2008
Dec 4, 2008
527
528
529
530
531
/* See if we can simply resize the existing window and surface */
if (SDL_ResizeVideoMode(width, height, bpp, flags) == 0) {
return SDL_PublicSurface;
}
532
533
534
535
536
537
538
539
540
541
542
543
/* Destroy existing window */
SDL_PublicSurface = NULL;
if (SDL_ShadowSurface) {
SDL_FreeSurface(SDL_ShadowSurface);
SDL_ShadowSurface = NULL;
}
if (SDL_VideoSurface) {
SDL_DelPaletteWatch(SDL_VideoSurface->format->palette,
SDL_VideoPaletteChanged, NULL);
SDL_FreeSurface(SDL_VideoSurface);
SDL_VideoSurface = NULL;
}
Jul 16, 2006
Jul 16, 2006
544
if (SDL_VideoContext) {
Mar 14, 2008
Mar 14, 2008
545
/* SDL_GL_MakeCurrent(0, NULL); *//* Doesn't do anything */
Jul 16, 2006
Jul 16, 2006
546
547
548
SDL_GL_DeleteContext(SDL_VideoContext);
SDL_VideoContext = NULL;
}
549
550
if (SDL_VideoWindow) {
SDL_GetWindowPosition(SDL_VideoWindow, &window_x, &window_y);
Mar 6, 2008
Mar 6, 2008
551
SDL_DestroyWindow(SDL_VideoWindow);
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
}
/* 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
580
SDL_SetWindowIcon(SDL_VideoWindow, SDL_VideoIcon);
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
window_flags = SDL_GetWindowFlags(SDL_VideoWindow);
surface_flags = 0;
if (window_flags & SDL_WINDOW_FULLSCREEN) {
surface_flags |= SDL_FULLSCREEN;
}
if (window_flags & SDL_WINDOW_OPENGL) {
surface_flags |= SDL_OPENGL;
}
if (window_flags & SDL_WINDOW_RESIZABLE) {
surface_flags |= SDL_RESIZABLE;
}
if (window_flags & SDL_WINDOW_BORDERLESS) {
surface_flags |= SDL_NOFRAME;
}
/* Set up the desired display mode */
Aug 5, 2006
Aug 5, 2006
598
desktop_format = desktop_mode.format;
599
600
601
602
603
604
605
606
607
if (desktop_format && ((flags & SDL_ANYFORMAT)
|| (bpp == SDL_BITSPERPIXEL(desktop_format)))) {
desired_format = desktop_format;
} else {
switch (bpp) {
case 0:
if (desktop_format) {
desired_format = desktop_format;
} else {
Aug 5, 2006
Aug 5, 2006
608
desired_format = SDL_PIXELFORMAT_RGB888;
Aug 15, 2007
Aug 15, 2007
610
bpp = SDL_BITSPERPIXEL(desired_format);
611
612
break;
case 8:
Aug 5, 2006
Aug 5, 2006
613
desired_format = SDL_PIXELFORMAT_INDEX8;
614
615
break;
case 15:
Aug 5, 2006
Aug 5, 2006
616
desired_format = SDL_PIXELFORMAT_RGB555;
617
618
break;
case 16:
Aug 5, 2006
Aug 5, 2006
619
desired_format = SDL_PIXELFORMAT_RGB565;
620
621
break;
case 24:
Aug 5, 2006
Aug 5, 2006
622
desired_format = SDL_PIXELFORMAT_RGB24;
623
624
break;
case 32:
Aug 5, 2006
Aug 5, 2006
625
desired_format = SDL_PIXELFORMAT_RGB888;
626
627
628
629
630
631
632
break;
default:
SDL_SetError("Unsupported bpp in SDL_SetVideoMode()");
return NULL;
}
}
Dec 1, 2009
Dec 1, 2009
633
/* Set up the desired display mode */
634
if (flags & SDL_FULLSCREEN) {
Dec 1, 2009
Dec 1, 2009
635
636
637
638
639
SDL_DisplayMode mode;
SDL_zero(mode);
mode.format = desired_format;
if (SDL_SetWindowDisplayMode(SDL_VideoWindow, &mode) < 0) {
640
641
642
643
644
645
return NULL;
}
}
/* 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
646
647
648
649
650
651
652
SDL_VideoContext = SDL_GL_CreateContext(SDL_VideoWindow);
if (!SDL_VideoContext) {
return NULL;
}
if (SDL_GL_MakeCurrent(SDL_VideoWindow, SDL_VideoContext) < 0) {
return NULL;
}
653
654
655
656
657
658
659
660
661
662
663
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 a renderer for the window */
Jul 15, 2006
Jul 15, 2006
664
665
if (SDL_CreateRenderer
(SDL_VideoWindow, -1,
Aug 5, 2006
Aug 5, 2006
666
SDL_RENDERER_SINGLEBUFFER | SDL_RENDERER_PRESENTDISCARD) < 0) {
667
668
return NULL;
}
Aug 11, 2007
Aug 11, 2007
669
SDL_GetRendererInfo(&SDL_VideoRendererInfo);
670
671
672
/* Create a texture for the screen surface */
SDL_VideoTexture =
Aug 11, 2007
Aug 11, 2007
673
SDL_CreateTexture(desired_format, SDL_TEXTUREACCESS_STREAMING, width,
Oct 4, 2008
Oct 4, 2008
675
676
677
if (!SDL_VideoTexture) {
SDL_VideoTexture =
Dec 16, 2008
Dec 16, 2008
678
SDL_CreateTexture(desktop_format,
Aug 11, 2007
Aug 11, 2007
679
SDL_TEXTUREACCESS_STREAMING, width, height);
680
681
682
683
684
685
}
if (!SDL_VideoTexture) {
return NULL;
}
/* Create the screen surface */
Aug 11, 2007
Aug 11, 2007
686
SDL_VideoSurface = CreateVideoSurface(SDL_VideoTexture);
687
688
689
690
691
692
693
694
695
696
697
if (!SDL_VideoSurface) {
return NULL;
}
SDL_VideoSurface->flags |= surface_flags;
/* Set a default screen palette */
if (SDL_VideoSurface->format->palette) {
SDL_VideoSurface->flags |= SDL_HWPALETTE;
SDL_DitherColors(SDL_VideoSurface->format->palette->colors,
SDL_VideoSurface->format->BitsPerPixel);
SDL_AddPaletteWatch(SDL_VideoSurface->format->palette,
Dec 7, 2008
Dec 7, 2008
698
SDL_VideoPaletteChanged, SDL_VideoSurface);
699
700
701
702
703
704
SDL_SetPaletteColors(SDL_VideoSurface->format->palette,
SDL_VideoSurface->format->palette->colors, 0,
SDL_VideoSurface->format->palette->ncolors);
}
/* Create a shadow surface if necessary */
Aug 11, 2007
Aug 11, 2007
705
706
707
708
if ((bpp != SDL_VideoSurface->format->BitsPerPixel)
&& !(flags & SDL_ANYFORMAT)) {
SDL_ShadowSurface =
SDL_CreateRGBSurface(0, width, height, bpp, 0, 0, 0, 0);
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
if (!SDL_ShadowSurface) {
return NULL;
}
SDL_ShadowSurface->flags |= surface_flags;
/* 8-bit SDL_ShadowSurface surfaces report that they have exclusive palette */
if (SDL_ShadowSurface->format->palette) {
SDL_ShadowSurface->flags |= SDL_HWPALETTE;
if (SDL_VideoSurface->format->palette) {
SDL_SetSurfacePalette(SDL_ShadowSurface,
SDL_VideoSurface->format->palette);
} else {
SDL_DitherColors(SDL_ShadowSurface->format->palette->colors,
SDL_ShadowSurface->format->BitsPerPixel);
}
Dec 7, 2008
Dec 7, 2008
724
725
SDL_AddPaletteWatch(SDL_ShadowSurface->format->palette,
SDL_VideoPaletteChanged, SDL_ShadowSurface);
726
727
728
729
730
}
}
SDL_PublicSurface =
(SDL_ShadowSurface ? SDL_ShadowSurface : SDL_VideoSurface);
Dec 4, 2008
Dec 4, 2008
731
732
733
SDL_VideoFlags = flags;
ClearVideoSurface();
Jan 12, 2009
Jan 12, 2009
735
SetupScreenSaver(flags);
Jan 12, 2009
Jan 12, 2009
736
737
738
739
740
741
742
743
744
745
746
/* We're finally done! */
return SDL_PublicSurface;
}
SDL_Surface *
SDL_GetVideoSurface(void)
{
return SDL_PublicSurface;
}
Aug 18, 2007
Aug 18, 2007
747
748
749
int
SDL_SetAlpha(SDL_Surface * surface, Uint32 flag, Uint8 value)
{
Dec 6, 2008
Dec 6, 2008
750
751
752
753
754
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
755
SDL_SetSurfaceAlphaMod(surface, value);
Dec 20, 2008
Dec 20, 2008
756
SDL_SetSurfaceBlendMode(surface, SDL_BLENDMODE_BLEND);
Aug 18, 2007
Aug 18, 2007
757
758
} else {
SDL_SetSurfaceAlphaMod(surface, 0xFF);
Dec 20, 2008
Dec 20, 2008
759
SDL_SetSurfaceBlendMode(surface, SDL_BLENDMODE_NONE);
Aug 18, 2007
Aug 18, 2007
760
}
Dec 6, 2008
Dec 6, 2008
761
762
SDL_SetSurfaceRLE(surface, (flag & SDL_RLEACCEL));
Aug 18, 2007
Aug 18, 2007
763
764
765
return 0;
}
766
767
768
SDL_Surface *
SDL_DisplayFormat(SDL_Surface * surface)
{
Nov 29, 2008
Nov 29, 2008
769
SDL_PixelFormat *format;
770
771
772
773
774
if (!SDL_PublicSurface) {
SDL_SetError("No video mode has been set");
return NULL;
}
Nov 29, 2008
Nov 29, 2008
775
format = SDL_PublicSurface->format;
776
777
/* Set the flags appropriate for copying to display surface */
Nov 29, 2008
Nov 29, 2008
778
return SDL_ConvertSurface(surface, format, SDL_RLEACCEL);
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
}
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;
}
format = SDL_AllocFormat(32, rmask, gmask, bmask, amask);
Nov 29, 2008
Nov 29, 2008
827
converted = SDL_ConvertSurface(surface, format, SDL_RLEACCEL);
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
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
846
847
848
849
rect.x = (int) x;
rect.y = (int) y;
rect.w = (int) (w ? w : screen->w);
rect.h = (int) (h ? h : screen->h);
850
851
852
SDL_UpdateRects(screen, 1, &rect);
}
}
Aug 27, 2008
Aug 27, 2008
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
void
SDL_UpdateRects(SDL_Surface * screen, int numrects, SDL_Rect * rects)
{
int i;
if (screen == SDL_ShadowSurface) {
for (i = 0; i < numrects; ++i) {
SDL_LowerBlit(SDL_ShadowSurface, &rects[i], SDL_VideoSurface,
&rects[i]);
}
/* Fall through to video surface update */
screen = SDL_VideoSurface;
}
if (screen == SDL_VideoSurface) {
Aug 11, 2007
Aug 11, 2007
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
if (screen->flags & SDL_PREALLOC) {
/* The surface memory is maintained by the renderer */
SDL_DirtyTexture(SDL_VideoTexture, numrects, rects);
} else {
/* The surface memory needs to be copied to texture */
int pitch = screen->pitch;
int psize = screen->format->BytesPerPixel;
for (i = 0; i < numrects; ++i) {
const SDL_Rect *rect = &rects[i];
void *pixels =
(Uint8 *) screen->pixels + rect->y * pitch +
rect->x * psize;
SDL_UpdateTexture(SDL_VideoTexture, rect, pixels, pitch);
}
}
Aug 5, 2006
Aug 5, 2006
884
if (SDL_VideoRendererInfo.flags & SDL_RENDERER_PRESENTCOPY) {
Jul 15, 2006
Jul 15, 2006
885
for (i = 0; i < numrects; ++i) {
Aug 28, 2006
Aug 28, 2006
886
SDL_RenderCopy(SDL_VideoTexture, &rects[i], &rects[i]);
Jul 15, 2006
Jul 15, 2006
887
888
889
890
891
892
893
}
} else {
SDL_Rect rect;
rect.x = 0;
rect.y = 0;
rect.w = screen->w;
rect.h = screen->h;
Aug 28, 2006
Aug 28, 2006
894
SDL_RenderCopy(SDL_VideoTexture, &rect, &rect);
895
896
897
898
899
900
901
902
903
904
}
SDL_RenderPresent();
}
}
void
SDL_WM_SetCaption(const char *title, const char *icon)
{
if (wm_title) {
SDL_free(wm_title);
Sep 23, 2006
Sep 23, 2006
905
906
}
if (title) {
907
wm_title = SDL_strdup(title);
Sep 23, 2006
Sep 23, 2006
908
909
} else {
wm_title = NULL;
910
911
912
913
914
}
SDL_SetWindowTitle(SDL_VideoWindow, wm_title);
}
void
Sep 23, 2006
Sep 23, 2006
915
SDL_WM_GetCaption(const char **title, const char **icon)
916
917
918
919
920
921
922
923
924
925
926
927
{
if (title) {
*title = wm_title;
}
if (icon) {
*icon = "";
}
}
void
SDL_WM_SetIcon(SDL_Surface * icon, Uint8 * mask)
{
Jan 2, 2009
Jan 2, 2009
928
SDL_VideoIcon = icon;
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
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
}
int
SDL_WM_IconifyWindow(void)
{
SDL_MinimizeWindow(SDL_VideoWindow);
return 0;
}
int
SDL_WM_ToggleFullScreen(SDL_Surface * surface)
{
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;
}
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)
{