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

Latest commit

 

History

History
1723 lines (1579 loc) · 44.3 KB

SDL_video.c

File metadata and controls

1723 lines (1579 loc) · 44.3 KB
 
Apr 26, 2001
Apr 26, 2001
1
2
/*
SDL - Simple DirectMedia Layer
Feb 1, 2006
Feb 1, 2006
3
Copyright (C) 1997-2006 Sam Lantinga
Apr 26, 2001
Apr 26, 2001
4
5
This library is free software; you can redistribute it and/or
Feb 1, 2006
Feb 1, 2006
6
modify it under the terms of the GNU Lesser General Public
Apr 26, 2001
Apr 26, 2001
7
License as published by the Free Software Foundation; either
Feb 1, 2006
Feb 1, 2006
8
version 2.1 of the License, or (at your option) any later version.
Apr 26, 2001
Apr 26, 2001
9
10
11
12
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
Feb 1, 2006
Feb 1, 2006
13
Lesser General Public License for more details.
Apr 26, 2001
Apr 26, 2001
14
Feb 1, 2006
Feb 1, 2006
15
16
17
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
Apr 26, 2001
Apr 26, 2001
18
19
Sam Lantinga
Dec 14, 2001
Dec 14, 2001
20
slouken@libsdl.org
Apr 26, 2001
Apr 26, 2001
21
*/
Feb 21, 2006
Feb 21, 2006
22
#include "SDL_config.h"
Apr 26, 2001
Apr 26, 2001
23
24
25
26
27
28
29
30
/* The high-level video driver subsystem */
#include "SDL.h"
#include "SDL_sysvideo.h"
#include "SDL_blit.h"
#include "SDL_pixels_c.h"
#include "SDL_cursor_c.h"
Feb 16, 2006
Feb 16, 2006
31
32
#include "../events/SDL_sysevents.h"
#include "../events/SDL_events_c.h"
Apr 26, 2001
Apr 26, 2001
33
34
35
/* Available video drivers */
static VideoBootStrap *bootstrap[] = {
Feb 16, 2006
Feb 16, 2006
36
#if SDL_VIDEO_DRIVER_QUARTZ
Sep 8, 2005
Sep 8, 2005
37
38
&QZ_bootstrap,
#endif
Feb 16, 2006
Feb 16, 2006
39
#if SDL_VIDEO_DRIVER_X11
Apr 26, 2001
Apr 26, 2001
40
41
&X11_bootstrap,
#endif
Feb 16, 2006
Feb 16, 2006
42
#if SDL_VIDEO_DRIVER_DGA
Apr 26, 2001
Apr 26, 2001
43
44
&DGA_bootstrap,
#endif
Feb 16, 2006
Feb 16, 2006
45
#if SDL_VIDEO_DRIVER_NANOX
May 11, 2001
May 11, 2001
46
47
&NX_bootstrap,
#endif
Feb 16, 2006
Feb 16, 2006
48
#if SDL_VIDEO_DRIVER_IPOD
Sep 8, 2005
Sep 8, 2005
49
50
&iPod_bootstrap,
#endif
Feb 16, 2006
Feb 16, 2006
51
#if SDL_VIDEO_DRIVER_QTOPIA
Jan 20, 2003
Jan 20, 2003
52
53
&Qtopia_bootstrap,
#endif
Feb 16, 2006
Feb 16, 2006
54
#if SDL_VIDEO_DRIVER_WSCONS
Nov 22, 2005
Nov 22, 2005
55
56
&WSCONS_bootstrap,
#endif
Feb 16, 2006
Feb 16, 2006
57
#if SDL_VIDEO_DRIVER_FBCON
Apr 26, 2001
Apr 26, 2001
58
59
&FBCON_bootstrap,
#endif
Feb 16, 2006
Feb 16, 2006
60
#if SDL_VIDEO_DRIVER_DIRECTFB
Sep 4, 2001
Sep 4, 2001
61
62
&DirectFB_bootstrap,
#endif
Feb 16, 2006
Feb 16, 2006
63
#if SDL_VIDEO_DRIVER_PS2GS
Apr 26, 2001
Apr 26, 2001
64
65
&PS2GS_bootstrap,
#endif
Feb 16, 2006
Feb 16, 2006
66
#if SDL_VIDEO_DRIVER_GGI
Apr 26, 2001
Apr 26, 2001
67
68
&GGI_bootstrap,
#endif
Feb 16, 2006
Feb 16, 2006
69
#if SDL_VIDEO_DRIVER_VGL
Jun 19, 2001
Jun 19, 2001
70
71
&VGL_bootstrap,
#endif
Feb 16, 2006
Feb 16, 2006
72
#if SDL_VIDEO_DRIVER_SVGALIB
Apr 26, 2001
Apr 26, 2001
73
74
&SVGALIB_bootstrap,
#endif
Feb 16, 2006
Feb 16, 2006
75
#if SDL_VIDEO_DRIVER_GAPI
Jan 19, 2006
Jan 19, 2006
76
77
&GAPI_bootstrap,
#endif
Feb 16, 2006
Feb 16, 2006
78
#if SDL_VIDEO_DRIVER_WINDIB
Apr 26, 2001
Apr 26, 2001
79
80
&WINDIB_bootstrap,
#endif
Feb 16, 2006
Feb 16, 2006
81
#if SDL_VIDEO_DRIVER_DDRAW
Jan 1, 2006
Jan 1, 2006
82
83
&DIRECTX_bootstrap,
#endif
Feb 16, 2006
Feb 16, 2006
84
#if SDL_VIDEO_DRIVER_BWINDOW
Apr 26, 2001
Apr 26, 2001
85
86
&BWINDOW_bootstrap,
#endif
Feb 16, 2006
Feb 16, 2006
87
#if SDL_VIDEO_DRIVER_TOOLBOX
Apr 26, 2001
Apr 26, 2001
88
89
&TOOLBOX_bootstrap,
#endif
Feb 16, 2006
Feb 16, 2006
90
#if SDL_VIDEO_DRIVER_DRAWSPROCKET
Apr 26, 2001
Apr 26, 2001
91
92
&DSp_bootstrap,
#endif
Feb 16, 2006
Feb 16, 2006
93
#if SDL_VIDEO_DRIVER_CYBERGRAPHICS
Apr 26, 2001
Apr 26, 2001
94
&CGX_bootstrap,
Apr 26, 2001
Apr 26, 2001
95
#endif
Feb 16, 2006
Feb 16, 2006
96
#if SDL_VIDEO_DRIVER_PHOTON
May 10, 2001
May 10, 2001
97
&ph_bootstrap,
Sep 11, 2001
Sep 11, 2001
98
#endif
Feb 16, 2006
Feb 16, 2006
99
#if SDL_VIDEO_DRIVER_EPOC
Sep 11, 2001
Sep 11, 2001
100
101
&EPOC_bootstrap,
#endif
Feb 16, 2006
Feb 16, 2006
102
#if SDL_VIDEO_DRIVER_XBIOS
Feb 17, 2002
Feb 17, 2002
103
104
&XBIOS_bootstrap,
#endif
Feb 16, 2006
Feb 16, 2006
105
#if SDL_VIDEO_DRIVER_GEM
Feb 17, 2002
Feb 17, 2002
106
&GEM_bootstrap,
May 19, 2002
May 19, 2002
107
#endif
Feb 16, 2006
Feb 16, 2006
108
#if SDL_VIDEO_DRIVER_PICOGUI
Aug 1, 2002
Aug 1, 2002
109
&PG_bootstrap,
Oct 5, 2002
Oct 5, 2002
110
#endif
Feb 16, 2006
Feb 16, 2006
111
#if SDL_VIDEO_DRIVER_DC
Oct 5, 2002
Oct 5, 2002
112
&DC_bootstrap,
Apr 15, 2003
Apr 15, 2003
113
#endif
Feb 16, 2006
Feb 16, 2006
114
#if SDL_VIDEO_DRIVER_RISCOS
Jan 19, 2006
Jan 19, 2006
115
&RISCOS_bootstrap,
May 29, 2003
May 29, 2003
116
#endif
Feb 16, 2006
Feb 16, 2006
117
#if SDL_VIDEO_DRIVER_OS2FS
Nov 23, 2005
Nov 23, 2005
118
119
&OS2FSLib_bootstrap,
#endif
Feb 16, 2006
Feb 16, 2006
120
121
122
123
#if SDL_VIDEO_DRIVER_AALIB
&AALIB_bootstrap,
#endif
#if SDL_VIDEO_DRIVER_DUMMY
Apr 15, 2003
Apr 15, 2003
124
&DUMMY_bootstrap,
May 1, 2006
May 1, 2006
125
126
127
#endif
#if SDL_VIDEO_DRIVER_GLSDL
&glSDL_bootstrap,
Apr 26, 2001
Apr 26, 2001
128
129
130
#endif
NULL
};
Sep 11, 2001
Sep 11, 2001
131
Apr 26, 2001
Apr 26, 2001
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
SDL_VideoDevice *current_video = NULL;
/* Various local functions */
int SDL_VideoInit(const char *driver_name, Uint32 flags);
void SDL_VideoQuit(void);
static SDL_GrabMode SDL_WM_GrabInputOff(void);
/*
* Initialize the video and event subsystems -- determine native pixel format
*/
int SDL_VideoInit (const char *driver_name, Uint32 flags)
{
SDL_VideoDevice *video;
int index;
int i;
SDL_PixelFormat vformat;
Uint32 video_flags;
/* Toggle the event thread flags, based on OS requirements */
#if defined(MUST_THREAD_EVENTS)
flags |= SDL_INIT_EVENTTHREAD;
#elif defined(CANT_THREAD_EVENTS)
if ( (flags & SDL_INIT_EVENTTHREAD) == SDL_INIT_EVENTTHREAD ) {
SDL_SetError("OS doesn't support threaded events");
return(-1);
}
#endif
/* Check to make sure we don't overwrite 'current_video' */
if ( current_video != NULL ) {
SDL_VideoQuit();
}
/* Select the proper video driver */
index = 0;
video = NULL;
if ( driver_name != NULL ) {
#if 0 /* This will be replaced with a better driver selection API */
Feb 7, 2006
Feb 7, 2006
172
173
if ( SDL_strrchr(driver_name, ':') != NULL ) {
index = atoi(SDL_strrchr(driver_name, ':')+1);
Apr 26, 2001
Apr 26, 2001
174
175
176
}
#endif
for ( i=0; bootstrap[i]; ++i ) {
Feb 7, 2006
Feb 7, 2006
177
178
if ( SDL_strncmp(bootstrap[i]->name, driver_name,
SDL_strlen(bootstrap[i]->name)) == 0 ) {
Apr 26, 2001
Apr 26, 2001
179
180
181
182
183
184
185
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
if ( bootstrap[i]->available() ) {
video = bootstrap[i]->create(index);
break;
}
}
}
} else {
for ( i=0; bootstrap[i]; ++i ) {
if ( bootstrap[i]->available() ) {
video = bootstrap[i]->create(index);
if ( video != NULL ) {
break;
}
}
}
}
if ( video == NULL ) {
SDL_SetError("No available video device");
return(-1);
}
current_video = video;
current_video->name = bootstrap[i]->name;
/* Do some basic variable initialization */
video->screen = NULL;
video->shadow = NULL;
video->visible = NULL;
video->physpal = NULL;
video->gammacols = NULL;
video->gamma = NULL;
video->wm_title = NULL;
video->wm_icon = NULL;
video->offset_x = 0;
video->offset_y = 0;
Feb 7, 2006
Feb 7, 2006
213
SDL_memset(&video->info, 0, (sizeof video->info));
Apr 17, 2005
Apr 17, 2005
214
215
video->displayformatalphapixel = NULL;
Apr 26, 2001
Apr 26, 2001
216
217
218
219
/* Set some very sane GL defaults */
video->gl_config.driver_loaded = 0;
video->gl_config.dll_handle = NULL;
Feb 20, 2006
Feb 20, 2006
220
221
222
video->gl_config.red_size = 3;
video->gl_config.green_size = 3;
video->gl_config.blue_size = 2;
Apr 26, 2001
Apr 26, 2001
223
224
225
226
227
228
229
230
231
video->gl_config.alpha_size = 0;
video->gl_config.buffer_size = 0;
video->gl_config.depth_size = 16;
video->gl_config.stencil_size = 0;
video->gl_config.double_buffer = 1;
video->gl_config.accum_red_size = 0;
video->gl_config.accum_green_size = 0;
video->gl_config.accum_blue_size = 0;
video->gl_config.accum_alpha_size = 0;
Aug 19, 2002
Aug 19, 2002
232
video->gl_config.stereo = 0;
Jul 22, 2003
Jul 22, 2003
233
234
video->gl_config.multisamplebuffers = 0;
video->gl_config.multisamplesamples = 0;
Apr 27, 2006
Apr 27, 2006
235
236
video->gl_config.accelerated = -1; /* not known, don't set */
video->gl_config.swap_control = -1; /* not known, don't set */
Apr 26, 2001
Apr 26, 2001
237
238
/* Initialize the video subsystem */
Feb 7, 2006
Feb 7, 2006
239
SDL_memset(&vformat, 0, sizeof(vformat));
Apr 26, 2001
Apr 26, 2001
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
if ( video->VideoInit(video, &vformat) < 0 ) {
SDL_VideoQuit();
return(-1);
}
/* Create a zero sized video surface of the appropriate format */
video_flags = SDL_SWSURFACE;
SDL_VideoSurface = SDL_CreateRGBSurface(video_flags, 0, 0,
vformat.BitsPerPixel,
vformat.Rmask, vformat.Gmask, vformat.Bmask, 0);
if ( SDL_VideoSurface == NULL ) {
SDL_VideoQuit();
return(-1);
}
SDL_PublicSurface = NULL; /* Until SDL_SetVideoMode() */
#if 0 /* Don't change the current palette - may be used by other programs.
* The application can't do anything with the display surface until
* a video mode has been set anyway. :)
*/
/* If we have a palettized surface, create a default palette */
if ( SDL_VideoSurface->format->palette ) {
Jul 28, 2003
Jul 28, 2003
262
SDL_PixelFormat *vf = SDL_VideoSurface->format;
Apr 26, 2001
Apr 26, 2001
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
SDL_DitherColors(vf->palette->colors, vf->BitsPerPixel);
video->SetColors(video,
0, vf->palette->ncolors, vf->palette->colors);
}
#endif
video->info.vfmt = SDL_VideoSurface->format;
/* Start the event loop */
if ( SDL_StartEventLoop(flags) < 0 ) {
SDL_VideoQuit();
return(-1);
}
SDL_CursorInit(flags & SDL_INIT_EVENTTHREAD);
/* We're ready to go! */
return(0);
}
char *SDL_VideoDriverName(char *namebuf, int maxlen)
{
if ( current_video != NULL ) {
Feb 19, 2006
Feb 19, 2006
284
SDL_strlcpy(namebuf, current_video->name, maxlen);
Apr 26, 2001
Apr 26, 2001
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
return(namebuf);
}
return(NULL);
}
/*
* Get the current display surface
*/
SDL_Surface *SDL_GetVideoSurface(void)
{
SDL_Surface *visible;
visible = NULL;
if ( current_video ) {
visible = current_video->visible;
}
return(visible);
}
/*
* Get the current information about the video hardware
*/
const SDL_VideoInfo *SDL_GetVideoInfo(void)
{
const SDL_VideoInfo *info;
info = NULL;
if ( current_video ) {
info = &current_video->info;
}
return(info);
}
/*
* Return a pointer to an array of available screen dimensions for the
* given format, sorted largest to smallest. Returns NULL if there are
* no dimensions available for a particular format, or (SDL_Rect **)-1
* if any dimension is okay for the given format. If 'format' is NULL,
* the mode list will be for the format given by SDL_GetVideoInfo()->vfmt
*/
SDL_Rect ** SDL_ListModes (SDL_PixelFormat *format, Uint32 flags)
{
SDL_VideoDevice *video = current_video;
SDL_VideoDevice *this = current_video;
SDL_Rect **modes;
modes = NULL;
if ( SDL_VideoSurface ) {
if ( format == NULL ) {
format = SDL_VideoSurface->format;
}
modes = video->ListModes(this, format, flags);
}
return(modes);
}
/*
* Check to see if a particular video mode is supported.
* It returns 0 if the requested mode is not supported under any bit depth,
* or returns the bits-per-pixel of the closest available mode with the
* given width and height. If this bits-per-pixel is different from the
* one used when setting the video mode, SDL_SetVideoMode() will succeed,
* but will emulate the requested bits-per-pixel with a shadow surface.
*/
static Uint8 SDL_closest_depths[4][8] = {
/* 8 bit closest depth ordering */
{ 0, 8, 16, 15, 32, 24, 0, 0 },
/* 15,16 bit closest depth ordering */
{ 0, 16, 15, 32, 24, 8, 0, 0 },
/* 24 bit closest depth ordering */
{ 0, 24, 32, 16, 15, 8, 0, 0 },
/* 32 bit closest depth ordering */
{ 0, 32, 16, 15, 24, 8, 0, 0 }
};
Feb 24, 2004
Feb 24, 2004
360
Feb 21, 2006
Feb 21, 2006
361
#ifdef __MACOS__ /* MPW optimization bug? */
Feb 24, 2004
Feb 24, 2004
362
363
364
365
366
367
#define NEGATIVE_ONE 0xFFFFFFFF
#else
#define NEGATIVE_ONE -1
#endif
int SDL_VideoModeOK (int width, int height, int bpp, Uint32 flags)
Apr 26, 2001
Apr 26, 2001
368
369
370
371
372
373
374
375
376
377
{
int table, b, i;
int supported;
SDL_PixelFormat format;
SDL_Rect **sizes;
/* Currently 1 and 4 bpp are not supported */
if ( bpp < 8 || bpp > 32 ) {
return(0);
}
Aug 20, 2002
Aug 20, 2002
378
if ( (width <= 0) || (height <= 0) ) {
Apr 26, 2001
Apr 26, 2001
379
380
381
382
return(0);
}
/* Search through the list valid of modes */
Feb 7, 2006
Feb 7, 2006
383
SDL_memset(&format, 0, sizeof(format));
Apr 26, 2001
Apr 26, 2001
384
385
386
387
388
389
390
391
392
393
394
supported = 0;
table = ((bpp+7)/8)-1;
SDL_closest_depths[table][0] = bpp;
SDL_closest_depths[table][7] = 0;
for ( b = 0; !supported && SDL_closest_depths[table][b]; ++b ) {
format.BitsPerPixel = SDL_closest_depths[table][b];
sizes = SDL_ListModes(&format, flags);
if ( sizes == (SDL_Rect **)0 ) {
/* No sizes supported at this bit-depth */
continue;
} else
Feb 24, 2004
Feb 24, 2004
395
if (sizes == (SDL_Rect **)NEGATIVE_ONE) {
Apr 26, 2001
Apr 26, 2001
396
397
398
/* Any size supported at this bit-depth */
supported = 1;
continue;
Feb 24, 2004
Feb 24, 2004
399
400
401
402
403
404
405
406
} else if (current_video->handles_any_size) {
/* Driver can center a smaller surface to simulate fullscreen */
for ( i=0; sizes[i]; ++i ) {
if ((sizes[i]->w >= width) && (sizes[i]->h >= height)) {
supported = 1; /* this mode can fit the centered window. */
break;
}
}
Apr 26, 2001
Apr 26, 2001
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
} else
for ( i=0; sizes[i]; ++i ) {
if ((sizes[i]->w == width) && (sizes[i]->h == height)) {
supported = 1;
break;
}
}
}
if ( supported ) {
--b;
return(SDL_closest_depths[table][b]);
} else {
return(0);
}
}
/*
* Get the closest non-emulated video mode to the one requested
*/
static int SDL_GetVideoMode (int *w, int *h, int *BitsPerPixel, Uint32 flags)
{
int table, b, i;
int supported;
int native_bpp;
SDL_PixelFormat format;
SDL_Rect **sizes;
Aug 20, 2002
Aug 20, 2002
434
435
436
437
438
/* Check parameters */
if ( *BitsPerPixel < 8 || *BitsPerPixel > 32 ) {
SDL_SetError("Invalid bits per pixel (range is {8...32})");
return(0);
}
Aug 1, 2002
Aug 1, 2002
439
if ((*w <= 0) || (*h <= 0)) {
Aug 20, 2002
Aug 20, 2002
440
SDL_SetError("Invalid width or height");
Aug 1, 2002
Aug 1, 2002
441
442
443
return(0);
}
Apr 26, 2001
Apr 26, 2001
444
445
446
447
448
449
450
451
452
453
454
/* Try the original video mode, get the closest depth */
native_bpp = SDL_VideoModeOK(*w, *h, *BitsPerPixel, flags);
if ( native_bpp == *BitsPerPixel ) {
return(1);
}
if ( native_bpp > 0 ) {
*BitsPerPixel = native_bpp;
return(1);
}
/* No exact size match at any depth, look for closest match */
Feb 7, 2006
Feb 7, 2006
455
SDL_memset(&format, 0, sizeof(format));
Apr 26, 2001
Apr 26, 2001
456
457
458
459
460
supported = 0;
table = ((*BitsPerPixel+7)/8)-1;
SDL_closest_depths[table][0] = *BitsPerPixel;
SDL_closest_depths[table][7] = SDL_VideoSurface->format->BitsPerPixel;
for ( b = 0; !supported && SDL_closest_depths[table][b]; ++b ) {
Jun 12, 2005
Jun 12, 2005
461
462
int best;
Apr 26, 2001
Apr 26, 2001
463
464
465
466
467
468
format.BitsPerPixel = SDL_closest_depths[table][b];
sizes = SDL_ListModes(&format, flags);
if ( sizes == (SDL_Rect **)0 ) {
/* No sizes supported at this bit-depth */
continue;
}
Jun 12, 2005
Jun 12, 2005
469
best=0;
Apr 26, 2001
Apr 26, 2001
470
for ( i=0; sizes[i]; ++i ) {
Jun 12, 2005
Jun 12, 2005
471
472
473
474
/* Mode with both dimensions bigger or equal than asked ? */
if ((sizes[i]->w >= *w) && (sizes[i]->h >= *h)) {
/* Mode with any dimension smaller or equal than current best ? */
if ((sizes[i]->w <= sizes[best]->w) || (sizes[i]->h <= sizes[best]->h)) {
Jan 19, 2006
Jan 19, 2006
475
/* Now choose the mode that has less pixels */
Jan 25, 2006
Jan 25, 2006
476
if ((sizes[i]->w * sizes[i]->h) <= (sizes[best]->w * sizes[best]->h)) {
Jan 19, 2006
Jan 19, 2006
477
478
479
best=i;
supported = 1;
}
Apr 26, 2001
Apr 26, 2001
480
481
482
}
}
}
Jun 12, 2005
Jun 12, 2005
483
484
485
if (supported) {
*w=sizes[best]->w;
*h=sizes[best]->h;
Apr 26, 2001
Apr 26, 2001
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
*BitsPerPixel = SDL_closest_depths[table][b];
}
}
if ( ! supported ) {
SDL_SetError("No video mode large enough for %dx%d", *w, *h);
}
return(supported);
}
/* This should probably go somewhere else -- like SDL_surface.c */
static void SDL_ClearSurface(SDL_Surface *surface)
{
Uint32 black;
black = SDL_MapRGB(surface->format, 0, 0, 0);
SDL_FillRect(surface, NULL, black);
if ((surface->flags&SDL_HWSURFACE) && (surface->flags&SDL_DOUBLEBUF)) {
SDL_Flip(surface);
SDL_FillRect(surface, NULL, black);
}
SDL_Flip(surface);
}
/*
* Create a shadow surface suitable for fooling the app. :-)
*/
static void SDL_CreateShadowSurface(int depth)
{
Uint32 Rmask, Gmask, Bmask;
/* Allocate the shadow surface */
if ( depth == (SDL_VideoSurface->format)->BitsPerPixel ) {
Rmask = (SDL_VideoSurface->format)->Rmask;
Gmask = (SDL_VideoSurface->format)->Gmask;
Bmask = (SDL_VideoSurface->format)->Bmask;
} else {
Rmask = Gmask = Bmask = 0;
}
SDL_ShadowSurface = SDL_CreateRGBSurface(SDL_SWSURFACE,
SDL_VideoSurface->w, SDL_VideoSurface->h,
depth, Rmask, Gmask, Bmask, 0);
if ( SDL_ShadowSurface == NULL ) {
return;
}
/* 8-bit shadow surfaces report that they have exclusive palette */
if ( SDL_ShadowSurface->format->palette ) {
SDL_ShadowSurface->flags |= SDL_HWPALETTE;
if ( depth == (SDL_VideoSurface->format)->BitsPerPixel ) {
Feb 7, 2006
Feb 7, 2006
535
SDL_memcpy(SDL_ShadowSurface->format->palette->colors,
Apr 26, 2001
Apr 26, 2001
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
SDL_VideoSurface->format->palette->colors,
SDL_VideoSurface->format->palette->ncolors*
sizeof(SDL_Color));
} else {
SDL_DitherColors(
SDL_ShadowSurface->format->palette->colors, depth);
}
}
/* If the video surface is resizable, the shadow should say so */
if ( (SDL_VideoSurface->flags & SDL_RESIZABLE) == SDL_RESIZABLE ) {
SDL_ShadowSurface->flags |= SDL_RESIZABLE;
}
/* If the video surface has no frame, the shadow should say so */
if ( (SDL_VideoSurface->flags & SDL_NOFRAME) == SDL_NOFRAME ) {
SDL_ShadowSurface->flags |= SDL_NOFRAME;
}
/* If the video surface is fullscreen, the shadow should say so */
if ( (SDL_VideoSurface->flags & SDL_FULLSCREEN) == SDL_FULLSCREEN ) {
SDL_ShadowSurface->flags |= SDL_FULLSCREEN;
}
/* If the video surface is flippable, the shadow should say so */
if ( (SDL_VideoSurface->flags & SDL_DOUBLEBUF) == SDL_DOUBLEBUF ) {
SDL_ShadowSurface->flags |= SDL_DOUBLEBUF;
}
return;
}
Jul 18, 2004
Jul 18, 2004
564
565
566
567
#ifdef __QNXNTO__
#include <sys/neutrino.h>
#endif /* __QNXNTO__ */
Apr 26, 2001
Apr 26, 2001
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
/*
* Set the requested video mode, allocating a shadow buffer if necessary.
*/
SDL_Surface * SDL_SetVideoMode (int width, int height, int bpp, Uint32 flags)
{
SDL_VideoDevice *video, *this;
SDL_Surface *prev_mode, *mode;
int video_w;
int video_h;
int video_bpp;
int is_opengl;
SDL_GrabMode saved_grab;
/* Start up the video driver, if necessary..
WARNING: This is the only function protected this way!
*/
if ( ! current_video ) {
if ( SDL_Init(SDL_INIT_VIDEO|SDL_INIT_NOPARACHUTE) < 0 ) {
return(NULL);
}
}
this = video = current_video;
Mar 15, 2006
Mar 15, 2006
591
592
593
594
595
596
597
/* Default to the current width and height */
if ( width == 0 ) {
width = video->info.current_w;
}
if ( height == 0 ) {
height = video->info.current_h;
}
Apr 26, 2001
Apr 26, 2001
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
/* Default to the current video bpp */
if ( bpp == 0 ) {
flags |= SDL_ANYFORMAT;
bpp = SDL_VideoSurface->format->BitsPerPixel;
}
/* Get a good video mode, the closest one possible */
video_w = width;
video_h = height;
video_bpp = bpp;
if ( ! SDL_GetVideoMode(&video_w, &video_h, &video_bpp, flags) ) {
return(NULL);
}
/* Check the requested flags */
May 1, 2006
May 1, 2006
613
614
615
616
if ( flags & SDL_INTERNALOPENGL ) {
SDL_SetError("SDL_INTERNALOPENGL is for internal use only");
return(NULL);
}
Apr 26, 2001
Apr 26, 2001
617
if ( video_bpp > 8 ) {
May 1, 2006
May 1, 2006
618
/* There's no palette in > 8 bits-per-pixel mode */
Apr 26, 2001
Apr 26, 2001
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
flags &= ~SDL_HWPALETTE;
}
#if 0
if ( (flags&SDL_FULLSCREEN) != SDL_FULLSCREEN ) {
/* There's no windowed double-buffering */
flags &= ~SDL_DOUBLEBUF;
}
#endif
if ( (flags&SDL_DOUBLEBUF) == SDL_DOUBLEBUF ) {
/* Use hardware surfaces when double-buffering */
flags |= SDL_HWSURFACE;
}
is_opengl = ( ( flags & SDL_OPENGL ) == SDL_OPENGL );
if ( is_opengl ) {
/* These flags are for 2D video modes only */
flags &= ~(SDL_HWSURFACE|SDL_DOUBLEBUF);
May 1, 2006
May 1, 2006
636
637
/* This flag tells the backends to treat the surface accordingly */
flags |= SDL_INTERNALOPENGL;
Apr 26, 2001
Apr 26, 2001
638
639
}
May 1, 2001
May 1, 2001
640
641
/* Reset the keyboard here so event callbacks can run */
SDL_ResetKeyboard();
Aug 20, 2002
Aug 20, 2002
642
SDL_ResetMouse();
Jan 30, 2006
Jan 30, 2006
643
SDL_cursorstate &= ~CURSOR_USINGSW;
May 1, 2001
May 1, 2001
644
Apr 26, 2001
Apr 26, 2001
645
646
647
648
649
650
651
652
653
654
655
/* Clean up any previous video mode */
if ( SDL_PublicSurface != NULL ) {
SDL_PublicSurface = NULL;
}
if ( SDL_ShadowSurface != NULL ) {
SDL_Surface *ready_to_go;
ready_to_go = SDL_ShadowSurface;
SDL_ShadowSurface = NULL;
SDL_FreeSurface(ready_to_go);
}
if ( video->physpal ) {
Feb 7, 2006
Feb 7, 2006
656
657
SDL_free(video->physpal->colors);
SDL_free(video->physpal);
Apr 26, 2001
Apr 26, 2001
658
659
660
video->physpal = NULL;
}
if( video->gammacols) {
Feb 7, 2006
Feb 7, 2006
661
SDL_free(video->gammacols);
Apr 26, 2001
Apr 26, 2001
662
663
664
665
666
667
668
669
670
671
672
673
video->gammacols = NULL;
}
/* Save the previous grab state and turn off grab for mode switch */
saved_grab = SDL_WM_GrabInputOff();
/* Try to set the video mode, along with offset and clipping */
prev_mode = SDL_VideoSurface;
SDL_LockCursor();
SDL_VideoSurface = NULL; /* In case it's freed by driver */
mode = video->SetVideoMode(this, prev_mode,video_w,video_h,video_bpp,flags);
if ( mode ) { /* Prevent resize events from mode change */
Nov 23, 2005
Nov 23, 2005
674
675
/* But not on OS/2 */
#ifndef __OS2__
Apr 26, 2001
Apr 26, 2001
676
SDL_PrivateResize(mode->w, mode->h);
Nov 23, 2005
Nov 23, 2005
677
#endif
Nov 4, 2001
Nov 4, 2001
678
679
/* Sam - If we asked for OpenGL mode, and didn't get it, fail */
May 1, 2006
May 1, 2006
680
if ( is_opengl && !(mode->flags & SDL_INTERNALOPENGL) ) {
Oct 25, 2001
Oct 25, 2001
681
mode = NULL;
Apr 11, 2004
Apr 11, 2004
682
SDL_SetError("OpenGL not available");
Nov 4, 2001
Nov 4, 2001
683
}
Jul 28, 2003
Jul 28, 2003
684
}
Apr 26, 2001
Apr 26, 2001
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
/*
* rcg11292000
* If you try to set an SDL_OPENGL surface, and fail to find a
* matching visual, then the next call to SDL_SetVideoMode()
* will segfault, since we no longer point to a dummy surface,
* but rather NULL.
* Sam 11/29/00
* WARNING, we need to make sure that the previous mode hasn't
* already been freed by the video driver. What do we do in
* that case? Should we call SDL_VideoInit() again?
*/
SDL_VideoSurface = (mode != NULL) ? mode : prev_mode;
if ( (mode != NULL) && (!is_opengl) ) {
/* Sanity check */
if ( (mode->w < width) || (mode->h < height) ) {
SDL_SetError("Video mode smaller than requested");
return(NULL);
}
/* If we have a palettized surface, create a default palette */
if ( mode->format->palette ) {
Jul 28, 2003
Jul 28, 2003
707
SDL_PixelFormat *vf = mode->format;
Apr 26, 2001
Apr 26, 2001
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
SDL_DitherColors(vf->palette->colors, vf->BitsPerPixel);
video->SetColors(this, 0, vf->palette->ncolors,
vf->palette->colors);
}
/* Clear the surface to black */
video->offset_x = 0;
video->offset_y = 0;
mode->offset = 0;
SDL_SetClipRect(mode, NULL);
SDL_ClearSurface(mode);
/* Now adjust the offsets to match the desired mode */
video->offset_x = (mode->w-width)/2;
video->offset_y = (mode->h-height)/2;
mode->offset = video->offset_y*mode->pitch +
video->offset_x*mode->format->BytesPerPixel;
#ifdef DEBUG_VIDEO
fprintf(stderr,
"Requested mode: %dx%dx%d, obtained mode %dx%dx%d (offset %d)\n",
width, height, bpp,
mode->w, mode->h, mode->format->BitsPerPixel, mode->offset);
#endif
mode->w = width;
mode->h = height;
SDL_SetClipRect(mode, NULL);
}
SDL_ResetCursor();
SDL_UnlockCursor();
/* If we failed setting a video mode, return NULL... (Uh Oh!) */
if ( mode == NULL ) {
return(NULL);
}
/* If there is no window manager, set the SDL_NOFRAME flag */
if ( ! video->info.wm_available ) {
mode->flags |= SDL_NOFRAME;
}
/* Reset the mouse cursor and grab for new video mode */
SDL_SetCursor(NULL);
if ( video->UpdateMouse ) {
video->UpdateMouse(this);
}
SDL_WM_GrabInput(saved_grab);
SDL_GetRelativeMouseState(NULL, NULL); /* Clear first large delta */
Feb 16, 2006
Feb 16, 2006
756
#if SDL_VIDEO_OPENGL
Jul 12, 2003
Jul 12, 2003
757
/* Load GL symbols (before MakeCurrent, where we need glGetString). */
May 1, 2006
May 1, 2006
758
if ( flags & SDL_INTERNALOPENGL ) {
Jul 18, 2004
Jul 18, 2004
759
Jan 25, 2005
Jan 25, 2005
760
#if defined(__QNXNTO__) && (_NTO_VERSION < 630)
Jul 25, 2004
Jul 25, 2004
761
#define __SDL_NOGETPROCADDR__
Nov 25, 2004
Nov 25, 2004
762
763
#elif defined(__MINT__)
#define __SDL_NOGETPROCADDR__
Jul 25, 2004
Jul 25, 2004
764
#endif
Jul 18, 2004
Jul 18, 2004
765
766
767
768
769
770
771
#ifdef __SDL_NOGETPROCADDR__
#define SDL_PROC(ret,func,params) video->func=func;
#else
#define SDL_PROC(ret,func,params) \
do { \
video->func = SDL_GL_GetProcAddress(#func); \
if ( ! video->func ) { \
Sep 8, 2005
Sep 8, 2005
772
SDL_SetError("Couldn't load GL function %s: %s\n", #func, SDL_GetError()); \
Jul 18, 2004
Jul 18, 2004
773
774
775
776
777
778
return(NULL); \
} \
} while ( 0 );
#endif /* __SDL_NOGETPROCADDR__ */
Jul 12, 2003
Jul 12, 2003
779
780
781
#include "SDL_glfuncs.h"
#undef SDL_PROC
}
Feb 16, 2006
Feb 16, 2006
782
#endif /* SDL_VIDEO_OPENGL */
Jul 12, 2003
Jul 12, 2003
783
Apr 26, 2001
Apr 26, 2001
784
/* If we're running OpenGL, make the context current */
May 1, 2006
May 1, 2006
785
if ( (video->screen->flags & SDL_INTERNALOPENGL) &&
Apr 26, 2001
Apr 26, 2001
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
video->GL_MakeCurrent ) {
if ( video->GL_MakeCurrent(this) < 0 ) {
return(NULL);
}
}
/* Create a shadow surface if necessary */
/* There are three conditions under which we create a shadow surface:
1. We need a particular bits-per-pixel that we didn't get.
2. We need a hardware palette and didn't get one.
3. We need a software surface and got a hardware surface.
*/
if ( !(SDL_VideoSurface->flags & SDL_OPENGL) &&
(
( !(flags&SDL_ANYFORMAT) &&
(SDL_VideoSurface->format->BitsPerPixel != bpp)) ||
( (flags&SDL_HWPALETTE) &&
!(SDL_VideoSurface->flags&SDL_HWPALETTE)) ||
/* If the surface is in hardware, video writes are visible
as soon as they are performed, so we need to buffer them
*/
( ((flags&SDL_HWSURFACE) == SDL_SWSURFACE) &&
Jan 26, 2003
Jan 26, 2003
808
809
810
811
(SDL_VideoSurface->flags&SDL_HWSURFACE)) ||
( (flags&SDL_DOUBLEBUF) &&
(SDL_VideoSurface->flags&SDL_HWSURFACE) &&
!(SDL_VideoSurface->flags&SDL_DOUBLEBUF))
Apr 26, 2001
Apr 26, 2001
812
813
814
815
816
817
818
819
820
821
822
) ) {
SDL_CreateShadowSurface(bpp);
if ( SDL_ShadowSurface == NULL ) {
SDL_SetError("Couldn't create shadow surface");
return(NULL);
}
SDL_PublicSurface = SDL_ShadowSurface;
} else {
SDL_PublicSurface = SDL_VideoSurface;
}
video->info.vfmt = SDL_VideoSurface->format;
Mar 15, 2006
Mar 15, 2006
823
824
video->info.current_w = SDL_VideoSurface->w;
video->info.current_h = SDL_VideoSurface->h;
Apr 26, 2001
Apr 26, 2001
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
/* We're done! */
return(SDL_PublicSurface);
}
/*
* Convert a surface into the video pixel format.
*/
SDL_Surface * SDL_DisplayFormat (SDL_Surface *surface)
{
Uint32 flags;
if ( ! SDL_PublicSurface ) {
SDL_SetError("No video mode has been set");
return(NULL);
}
/* Set the flags appropriate for copying to display surface */
Mar 20, 2002
Mar 20, 2002
842
if (((SDL_PublicSurface->flags&SDL_HWSURFACE) == SDL_HWSURFACE) && current_video->info.blit_hw)
Mar 10, 2002
Mar 10, 2002
843
844
845
flags = SDL_HWSURFACE;
else
flags = SDL_SWSURFACE;
Apr 26, 2001
Apr 26, 2001
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
#ifdef AUTORLE_DISPLAYFORMAT
flags |= (surface->flags & (SDL_SRCCOLORKEY|SDL_SRCALPHA));
flags |= SDL_RLEACCELOK;
#else
flags |= surface->flags & (SDL_SRCCOLORKEY|SDL_SRCALPHA|SDL_RLEACCELOK);
#endif
return(SDL_ConvertSurface(surface, SDL_PublicSurface->format, flags));
}
/*
* Convert a surface into a format that's suitable for blitting to
* the screen, but including an alpha channel.
*/
SDL_Surface *SDL_DisplayFormatAlpha(SDL_Surface *surface)
{
SDL_PixelFormat *vf;
SDL_PixelFormat *format;
SDL_Surface *converted;
Uint32 flags;
/* 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);
flags = SDL_PublicSurface->flags & SDL_HWSURFACE;
flags |= surface->flags & (SDL_SRCALPHA | SDL_RLEACCELOK);
converted = SDL_ConvertSurface(surface, format, flags);
SDL_FreeFormat(format);
return(converted);
}
/*
* Update a specific portion of the physical screen
*/
void SDL_UpdateRect(SDL_Surface *screen, Sint32 x, Sint32 y, Uint32 w, Uint32 h)
{
if ( screen ) {
SDL_Rect rect;
/* Perform some checking */
if ( w == 0 )
w = screen->w;
if ( h == 0 )
h = screen->h;
if ( (int)(x+w) > screen->w )
return;
if ( (int)(y+h) > screen->h )
return;
/* Fill the rectangle */
Feb 24, 2006
Feb 24, 2006
931
932
933
934
rect.x = (Sint16)x;
rect.y = (Sint16)y;
rect.w = (Uint16)w;
rect.h = (Uint16)h;
Apr 26, 2001
Apr 26, 2001
935
936
937
938
939
940
941
942
943
SDL_UpdateRects(screen, 1, &rect);
}
}
void SDL_UpdateRects (SDL_Surface *screen, int numrects, SDL_Rect *rects)
{
int i;
SDL_VideoDevice *video = current_video;
SDL_VideoDevice *this = current_video;
Apr 27, 2006
Apr 27, 2006
944
if ( screen->flags & SDL_OPENGL ) {
Mar 9, 2006
Mar 9, 2006
945
946
947
SDL_SetError("OpenGL active, use SDL_GL_SwapBuffers()");
return;
}
Apr 26, 2001
Apr 26, 2001
948
949
if ( screen == SDL_ShadowSurface ) {
/* Blit the shadow surface using saved mapping */
Sep 9, 2002
Sep 9, 2002
950
SDL_Palette *pal = screen->format->palette;
Apr 26, 2001
Apr 26, 2001
951
SDL_Color *saved_colors = NULL;
Sep 9, 2002
Sep 9, 2002
952
if ( pal && !(SDL_VideoSurface->flags & SDL_HWPALETTE) ) {
Apr 26, 2001
Apr 26, 2001
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
/* simulated 8bpp, use correct physical palette */
saved_colors = pal->colors;
if ( video->gammacols ) {
/* gamma-corrected palette */
pal->colors = video->gammacols;
} else if ( video->physpal ) {
/* physical palette different from logical */
pal->colors = video->physpal->colors;
}
}
if ( SHOULD_DRAWCURSOR(SDL_cursorstate) ) {
SDL_LockCursor();
SDL_DrawCursor(SDL_ShadowSurface);
for ( i=0; i<numrects; ++i ) {
SDL_LowerBlit(SDL_ShadowSurface, &rects[i],
SDL_VideoSurface, &rects[i]);
}
SDL_EraseCursor(SDL_ShadowSurface);
SDL_UnlockCursor();
} else {
for ( i=0; i<numrects; ++i ) {
SDL_LowerBlit(SDL_ShadowSurface, &rects[i],
SDL_VideoSurface, &rects[i]);
}
}
Sep 9, 2002
Sep 9, 2002
978
if ( saved_colors ) {
Apr 26, 2001
Apr 26, 2001
979
pal->colors = saved_colors;
Sep 9, 2002
Sep 9, 2002
980
}
Apr 26, 2001
Apr 26, 2001
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Fall through to video surface update */
screen = SDL_VideoSurface;
}
if ( screen == SDL_VideoSurface ) {
/* Update the video surface */
if ( screen->offset ) {
for ( i=0; i<numrects; ++i ) {
rects[i].x += video->offset_x;
rects[i].y += video->offset_y;
}
video->UpdateRects(this, numrects, rects);
for ( i=0; i<numrects; ++i ) {
rects[i].x -= video->offset_x;
rects[i].y -= video->offset_y;
}
} else {
video->UpdateRects(this, numrects, rects);
}
}