diff options
author | Franklin Wei <git@fwei.tk> | 2017-01-21 15:18:31 -0500 |
---|---|---|
committer | Franklin Wei <git@fwei.tk> | 2017-12-23 21:01:26 -0500 |
commit | a855d6202536ff28e5aae4f22a0f31d8f5b325d0 (patch) | |
tree | 8c75f224dd64ed360505afa8843d016b0d75000b /apps/plugins/sdl/src/video/SDL_video.c | |
parent | 01c6dcf6c7b9bb1ad2fa0450f99bacc5f3d3e04b (diff) | |
download | rockbox-a855d6202536ff28e5aae4f22a0f31d8f5b325d0.tar.gz rockbox-a855d6202536ff28e5aae4f22a0f31d8f5b325d0.zip |
Port of Duke Nukem 3D
This ports Fabien Sanglard's Chocolate Duke to run on a version of SDL
for Rockbox.
Change-Id: I8f2c4c78af19de10c1633ed7bb7a997b43256dd9
Diffstat (limited to 'apps/plugins/sdl/src/video/SDL_video.c')
-rw-r--r-- | apps/plugins/sdl/src/video/SDL_video.c | 1978 |
1 files changed, 1978 insertions, 0 deletions
diff --git a/apps/plugins/sdl/src/video/SDL_video.c b/apps/plugins/sdl/src/video/SDL_video.c new file mode 100644 index 0000000000..2648d1fb3c --- /dev/null +++ b/apps/plugins/sdl/src/video/SDL_video.c | |||
@@ -0,0 +1,1978 @@ | |||
1 | /* | ||
2 | SDL - Simple DirectMedia Layer | ||
3 | Copyright (C) 1997-2012 Sam Lantinga | ||
4 | |||
5 | This library is free software; you can redistribute it and/or | ||
6 | modify it under the terms of the GNU Lesser General Public | ||
7 | License as published by the Free Software Foundation; either | ||
8 | version 2.1 of the License, or (at your option) any later version. | ||
9 | |||
10 | This library is distributed in the hope that it will be useful, | ||
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | Lesser General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU Lesser General Public | ||
16 | License along with this library; if not, write to the Free Software | ||
17 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | ||
18 | |||
19 | Sam Lantinga | ||
20 | slouken@libsdl.org | ||
21 | */ | ||
22 | #include "SDL_config.h" | ||
23 | |||
24 | /* The high-level video driver subsystem */ | ||
25 | |||
26 | #include "SDL.h" | ||
27 | #include "SDL_sysvideo.h" | ||
28 | #include "SDL_blit.h" | ||
29 | #include "SDL_pixels_c.h" | ||
30 | #include "SDL_cursor_c.h" | ||
31 | #include "../events/SDL_sysevents.h" | ||
32 | #include "../events/SDL_events_c.h" | ||
33 | |||
34 | /* Available video drivers */ | ||
35 | static VideoBootStrap *bootstrap[] = { | ||
36 | #if SDL_VIDEO_DRIVER_QUARTZ | ||
37 | &QZ_bootstrap, | ||
38 | #endif | ||
39 | #if SDL_VIDEO_DRIVER_X11 | ||
40 | &X11_bootstrap, | ||
41 | #endif | ||
42 | #if SDL_VIDEO_DRIVER_DGA | ||
43 | &DGA_bootstrap, | ||
44 | #endif | ||
45 | #if SDL_VIDEO_DRIVER_NANOX | ||
46 | &NX_bootstrap, | ||
47 | #endif | ||
48 | #if SDL_VIDEO_DRIVER_IPOD | ||
49 | &iPod_bootstrap, | ||
50 | #endif | ||
51 | #if SDL_VIDEO_DRIVER_QTOPIA | ||
52 | &Qtopia_bootstrap, | ||
53 | #endif | ||
54 | #if SDL_VIDEO_DRIVER_WSCONS | ||
55 | &WSCONS_bootstrap, | ||
56 | #endif | ||
57 | #if SDL_VIDEO_DRIVER_FBCON | ||
58 | &FBCON_bootstrap, | ||
59 | #endif | ||
60 | #if SDL_VIDEO_DRIVER_DIRECTFB | ||
61 | &DirectFB_bootstrap, | ||
62 | #endif | ||
63 | #if SDL_VIDEO_DRIVER_PS2GS | ||
64 | &PS2GS_bootstrap, | ||
65 | #endif | ||
66 | #if SDL_VIDEO_DRIVER_PS3 | ||
67 | &PS3_bootstrap, | ||
68 | #endif | ||
69 | #if SDL_VIDEO_DRIVER_GGI | ||
70 | &GGI_bootstrap, | ||
71 | #endif | ||
72 | #if SDL_VIDEO_DRIVER_VGL | ||
73 | &VGL_bootstrap, | ||
74 | #endif | ||
75 | #if SDL_VIDEO_DRIVER_SVGALIB | ||
76 | &SVGALIB_bootstrap, | ||
77 | #endif | ||
78 | #if SDL_VIDEO_DRIVER_GAPI | ||
79 | &GAPI_bootstrap, | ||
80 | #endif | ||
81 | #if SDL_VIDEO_DRIVER_WINDIB | ||
82 | &WINDIB_bootstrap, | ||
83 | #endif | ||
84 | #if SDL_VIDEO_DRIVER_DDRAW | ||
85 | &DIRECTX_bootstrap, | ||
86 | #endif | ||
87 | #if SDL_VIDEO_DRIVER_BWINDOW | ||
88 | &BWINDOW_bootstrap, | ||
89 | #endif | ||
90 | #if SDL_VIDEO_DRIVER_TOOLBOX | ||
91 | &TOOLBOX_bootstrap, | ||
92 | #endif | ||
93 | #if SDL_VIDEO_DRIVER_DRAWSPROCKET | ||
94 | &DSp_bootstrap, | ||
95 | #endif | ||
96 | #if SDL_VIDEO_DRIVER_PHOTON | ||
97 | &ph_bootstrap, | ||
98 | #endif | ||
99 | #if SDL_VIDEO_DRIVER_EPOC | ||
100 | &EPOC_bootstrap, | ||
101 | #endif | ||
102 | #if SDL_VIDEO_DRIVER_XBIOS | ||
103 | &XBIOS_bootstrap, | ||
104 | #endif | ||
105 | #if SDL_VIDEO_DRIVER_GEM | ||
106 | &GEM_bootstrap, | ||
107 | #endif | ||
108 | #if SDL_VIDEO_DRIVER_PICOGUI | ||
109 | &PG_bootstrap, | ||
110 | #endif | ||
111 | #if SDL_VIDEO_DRIVER_DC | ||
112 | &DC_bootstrap, | ||
113 | #endif | ||
114 | #if SDL_VIDEO_DRIVER_NDS | ||
115 | &NDS_bootstrap, | ||
116 | #endif | ||
117 | #if SDL_VIDEO_DRIVER_RISCOS | ||
118 | &RISCOS_bootstrap, | ||
119 | #endif | ||
120 | #if SDL_VIDEO_DRIVER_OS2FS | ||
121 | &OS2FSLib_bootstrap, | ||
122 | #endif | ||
123 | #if SDL_VIDEO_DRIVER_AALIB | ||
124 | &AALIB_bootstrap, | ||
125 | #endif | ||
126 | #if SDL_VIDEO_DRIVER_CACA | ||
127 | &CACA_bootstrap, | ||
128 | #endif | ||
129 | #if SDL_VIDEO_DRIVER_DUMMY | ||
130 | &DUMMY_bootstrap, | ||
131 | #endif | ||
132 | #if SDL_VIDEO_DRIVER_ROCKBOX | ||
133 | &ROCKBOX_bootstrap, | ||
134 | #endif | ||
135 | NULL | ||
136 | }; | ||
137 | |||
138 | SDL_VideoDevice *current_video = NULL; | ||
139 | |||
140 | /* Various local functions */ | ||
141 | int SDL_VideoInit(const char *driver_name, Uint32 flags); | ||
142 | void SDL_VideoQuit(void); | ||
143 | void SDL_GL_UpdateRectsLock(SDL_VideoDevice* this, int numrects, SDL_Rect* rects); | ||
144 | |||
145 | static SDL_GrabMode SDL_WM_GrabInputOff(void); | ||
146 | #if SDL_VIDEO_OPENGL | ||
147 | static int lock_count = 0; | ||
148 | #endif | ||
149 | |||
150 | |||
151 | /* | ||
152 | * Initialize the video and event subsystems -- determine native pixel format | ||
153 | */ | ||
154 | int SDL_VideoInit (const char *driver_name, Uint32 flags) | ||
155 | { | ||
156 | SDL_VideoDevice *video; | ||
157 | int index; | ||
158 | int i; | ||
159 | SDL_PixelFormat vformat; | ||
160 | Uint32 video_flags; | ||
161 | |||
162 | /* Toggle the event thread flags, based on OS requirements */ | ||
163 | #if defined(MUST_THREAD_EVENTS) | ||
164 | flags |= SDL_INIT_EVENTTHREAD; | ||
165 | #elif defined(CANT_THREAD_EVENTS) | ||
166 | if ( (flags & SDL_INIT_EVENTTHREAD) == SDL_INIT_EVENTTHREAD ) { | ||
167 | SDL_SetError("OS doesn't support threaded events"); | ||
168 | return(-1); | ||
169 | } | ||
170 | #endif | ||
171 | |||
172 | /* Check to make sure we don't overwrite 'current_video' */ | ||
173 | if ( current_video != NULL ) { | ||
174 | SDL_VideoQuit(); | ||
175 | } | ||
176 | |||
177 | /* Select the proper video driver */ | ||
178 | index = 0; | ||
179 | video = NULL; | ||
180 | if ( driver_name != NULL ) { | ||
181 | #if 0 /* This will be replaced with a better driver selection API */ | ||
182 | if ( SDL_strrchr(driver_name, ':') != NULL ) { | ||
183 | index = atoi(SDL_strrchr(driver_name, ':')+1); | ||
184 | } | ||
185 | #endif | ||
186 | for ( i=0; bootstrap[i]; ++i ) { | ||
187 | if ( SDL_strcasecmp(bootstrap[i]->name, driver_name) == 0) { | ||
188 | if ( bootstrap[i]->available() ) { | ||
189 | video = bootstrap[i]->create(index); | ||
190 | break; | ||
191 | } | ||
192 | } | ||
193 | } | ||
194 | } else { | ||
195 | for ( i=0; bootstrap[i]; ++i ) { | ||
196 | if ( bootstrap[i]->available() ) { | ||
197 | video = bootstrap[i]->create(index); | ||
198 | if ( video != NULL ) { | ||
199 | break; | ||
200 | } | ||
201 | } | ||
202 | } | ||
203 | } | ||
204 | if ( video == NULL ) { | ||
205 | SDL_SetError("No available video device"); | ||
206 | return(-1); | ||
207 | } | ||
208 | current_video = video; | ||
209 | current_video->name = bootstrap[i]->name; | ||
210 | |||
211 | /* Do some basic variable initialization */ | ||
212 | video->screen = NULL; | ||
213 | video->shadow = NULL; | ||
214 | video->visible = NULL; | ||
215 | video->physpal = NULL; | ||
216 | video->gammacols = NULL; | ||
217 | video->gamma = NULL; | ||
218 | video->wm_title = NULL; | ||
219 | video->wm_icon = NULL; | ||
220 | video->offset_x = 0; | ||
221 | video->offset_y = 0; | ||
222 | SDL_memset(&video->info, 0, (sizeof video->info)); | ||
223 | |||
224 | video->displayformatalphapixel = NULL; | ||
225 | |||
226 | /* Set some very sane GL defaults */ | ||
227 | video->gl_config.driver_loaded = 0; | ||
228 | video->gl_config.dll_handle = NULL; | ||
229 | video->gl_config.red_size = 3; | ||
230 | video->gl_config.green_size = 3; | ||
231 | video->gl_config.blue_size = 2; | ||
232 | video->gl_config.alpha_size = 0; | ||
233 | video->gl_config.buffer_size = 0; | ||
234 | video->gl_config.depth_size = 16; | ||
235 | video->gl_config.stencil_size = 0; | ||
236 | video->gl_config.double_buffer = 1; | ||
237 | video->gl_config.accum_red_size = 0; | ||
238 | video->gl_config.accum_green_size = 0; | ||
239 | video->gl_config.accum_blue_size = 0; | ||
240 | video->gl_config.accum_alpha_size = 0; | ||
241 | video->gl_config.stereo = 0; | ||
242 | video->gl_config.multisamplebuffers = 0; | ||
243 | video->gl_config.multisamplesamples = 0; | ||
244 | video->gl_config.accelerated = -1; /* not known, don't set */ | ||
245 | video->gl_config.swap_control = -1; /* not known, don't set */ | ||
246 | |||
247 | /* Initialize the video subsystem */ | ||
248 | SDL_memset(&vformat, 0, sizeof(vformat)); | ||
249 | if ( video->VideoInit(video, &vformat) < 0 ) { | ||
250 | SDL_VideoQuit(); | ||
251 | return(-1); | ||
252 | } | ||
253 | |||
254 | /* Create a zero sized video surface of the appropriate format */ | ||
255 | video_flags = SDL_SWSURFACE; | ||
256 | SDL_VideoSurface = SDL_CreateRGBSurface(video_flags, 0, 0, | ||
257 | vformat.BitsPerPixel, | ||
258 | vformat.Rmask, vformat.Gmask, vformat.Bmask, 0); | ||
259 | if ( SDL_VideoSurface == NULL ) { | ||
260 | SDL_VideoQuit(); | ||
261 | return(-1); | ||
262 | } | ||
263 | SDL_PublicSurface = NULL; /* Until SDL_SetVideoMode() */ | ||
264 | |||
265 | #if 0 /* Don't change the current palette - may be used by other programs. | ||
266 | * The application can't do anything with the display surface until | ||
267 | * a video mode has been set anyway. :) | ||
268 | */ | ||
269 | /* If we have a palettized surface, create a default palette */ | ||
270 | if ( SDL_VideoSurface->format->palette ) { | ||
271 | SDL_PixelFormat *vf = SDL_VideoSurface->format; | ||
272 | SDL_DitherColors(vf->palette->colors, vf->BitsPerPixel); | ||
273 | video->SetColors(video, | ||
274 | 0, vf->palette->ncolors, vf->palette->colors); | ||
275 | } | ||
276 | #endif | ||
277 | video->info.vfmt = SDL_VideoSurface->format; | ||
278 | |||
279 | /* Start the event loop */ | ||
280 | if ( SDL_StartEventLoop(flags) < 0 ) { | ||
281 | SDL_VideoQuit(); | ||
282 | return(-1); | ||
283 | } | ||
284 | SDL_CursorInit(flags & SDL_INIT_EVENTTHREAD); | ||
285 | |||
286 | /* We're ready to go! */ | ||
287 | return(0); | ||
288 | } | ||
289 | |||
290 | char *SDL_VideoDriverName(char *namebuf, int maxlen) | ||
291 | { | ||
292 | if ( current_video != NULL ) { | ||
293 | SDL_strlcpy(namebuf, current_video->name, maxlen); | ||
294 | return(namebuf); | ||
295 | } | ||
296 | return(NULL); | ||
297 | } | ||
298 | |||
299 | /* | ||
300 | * Get the current display surface | ||
301 | */ | ||
302 | SDL_Surface *SDL_GetVideoSurface(void) | ||
303 | { | ||
304 | SDL_Surface *visible; | ||
305 | |||
306 | visible = NULL; | ||
307 | if ( current_video ) { | ||
308 | visible = current_video->visible; | ||
309 | } | ||
310 | return(visible); | ||
311 | } | ||
312 | |||
313 | /* | ||
314 | * Get the current information about the video hardware | ||
315 | */ | ||
316 | const SDL_VideoInfo *SDL_GetVideoInfo(void) | ||
317 | { | ||
318 | const SDL_VideoInfo *info; | ||
319 | |||
320 | info = NULL; | ||
321 | if ( current_video ) { | ||
322 | info = ¤t_video->info; | ||
323 | } | ||
324 | return(info); | ||
325 | } | ||
326 | |||
327 | /* | ||
328 | * Return a pointer to an array of available screen dimensions for the | ||
329 | * given format, sorted largest to smallest. Returns NULL if there are | ||
330 | * no dimensions available for a particular format, or (SDL_Rect **)-1 | ||
331 | * if any dimension is okay for the given format. If 'format' is NULL, | ||
332 | * the mode list will be for the format given by SDL_GetVideoInfo()->vfmt | ||
333 | */ | ||
334 | SDL_Rect ** SDL_ListModes (SDL_PixelFormat *format, Uint32 flags) | ||
335 | { | ||
336 | SDL_VideoDevice *video = current_video; | ||
337 | SDL_VideoDevice *this = current_video; | ||
338 | SDL_Rect **modes; | ||
339 | |||
340 | modes = NULL; | ||
341 | if ( SDL_VideoSurface ) { | ||
342 | if ( format == NULL ) { | ||
343 | format = SDL_VideoSurface->format; | ||
344 | } | ||
345 | modes = video->ListModes(this, format, flags); | ||
346 | } | ||
347 | return(modes); | ||
348 | } | ||
349 | |||
350 | /* | ||
351 | * Check to see if a particular video mode is supported. | ||
352 | * It returns 0 if the requested mode is not supported under any bit depth, | ||
353 | * or returns the bits-per-pixel of the closest available mode with the | ||
354 | * given width and height. If this bits-per-pixel is different from the | ||
355 | * one used when setting the video mode, SDL_SetVideoMode() will succeed, | ||
356 | * but will emulate the requested bits-per-pixel with a shadow surface. | ||
357 | */ | ||
358 | static Uint8 SDL_closest_depths[4][8] = { | ||
359 | /* 8 bit closest depth ordering */ | ||
360 | { 0, 8, 16, 15, 32, 24, 0, 0 }, | ||
361 | /* 15,16 bit closest depth ordering */ | ||
362 | { 0, 16, 15, 32, 24, 8, 0, 0 }, | ||
363 | /* 24 bit closest depth ordering */ | ||
364 | { 0, 24, 32, 16, 15, 8, 0, 0 }, | ||
365 | /* 32 bit closest depth ordering */ | ||
366 | { 0, 32, 16, 15, 24, 8, 0, 0 } | ||
367 | }; | ||
368 | |||
369 | |||
370 | #ifdef __MACOS__ /* MPW optimization bug? */ | ||
371 | #define NEGATIVE_ONE 0xFFFFFFFF | ||
372 | #else | ||
373 | #define NEGATIVE_ONE -1 | ||
374 | #endif | ||
375 | |||
376 | int SDL_VideoModeOK (int width, int height, int bpp, Uint32 flags) | ||
377 | { | ||
378 | int table, b, i; | ||
379 | int supported; | ||
380 | SDL_PixelFormat format; | ||
381 | SDL_Rect **sizes; | ||
382 | |||
383 | /* Currently 1 and 4 bpp are not supported */ | ||
384 | if ( bpp < 8 || bpp > 32 ) { | ||
385 | return(0); | ||
386 | } | ||
387 | if ( (width <= 0) || (height <= 0) ) { | ||
388 | return(0); | ||
389 | } | ||
390 | |||
391 | /* Search through the list valid of modes */ | ||
392 | SDL_memset(&format, 0, sizeof(format)); | ||
393 | supported = 0; | ||
394 | table = ((bpp+7)/8)-1; | ||
395 | SDL_closest_depths[table][0] = bpp; | ||
396 | SDL_closest_depths[table][7] = 0; | ||
397 | for ( b = 0; !supported && SDL_closest_depths[table][b]; ++b ) { | ||
398 | format.BitsPerPixel = SDL_closest_depths[table][b]; | ||
399 | sizes = SDL_ListModes(&format, flags); | ||
400 | if ( sizes == (SDL_Rect **)0 ) { | ||
401 | /* No sizes supported at this bit-depth */ | ||
402 | continue; | ||
403 | } else | ||
404 | if (sizes == (SDL_Rect **)NEGATIVE_ONE) { | ||
405 | /* Any size supported at this bit-depth */ | ||
406 | supported = 1; | ||
407 | continue; | ||
408 | } else if (current_video->handles_any_size) { | ||
409 | /* Driver can center a smaller surface to simulate fullscreen */ | ||
410 | for ( i=0; sizes[i]; ++i ) { | ||
411 | if ((sizes[i]->w >= width) && (sizes[i]->h >= height)) { | ||
412 | supported = 1; /* this mode can fit the centered window. */ | ||
413 | break; | ||
414 | } | ||
415 | } | ||
416 | } else | ||
417 | for ( i=0; sizes[i]; ++i ) { | ||
418 | if ((sizes[i]->w == width) && (sizes[i]->h == height)) { | ||
419 | supported = 1; | ||
420 | break; | ||
421 | } | ||
422 | } | ||
423 | } | ||
424 | if ( supported ) { | ||
425 | --b; | ||
426 | return(SDL_closest_depths[table][b]); | ||
427 | } else { | ||
428 | return(0); | ||
429 | } | ||
430 | } | ||
431 | |||
432 | /* | ||
433 | * Get the closest non-emulated video mode to the one requested | ||
434 | */ | ||
435 | static int SDL_GetVideoMode (int *w, int *h, int *BitsPerPixel, Uint32 flags) | ||
436 | { | ||
437 | int table, b, i; | ||
438 | int supported; | ||
439 | int native_bpp; | ||
440 | SDL_PixelFormat format; | ||
441 | SDL_Rect **sizes; | ||
442 | |||
443 | /* Check parameters */ | ||
444 | if ( *BitsPerPixel < 8 || *BitsPerPixel > 32 ) { | ||
445 | SDL_SetError("Invalid bits per pixel (range is {8...32})"); | ||
446 | return(0); | ||
447 | } | ||
448 | if ((*w <= 0) || (*h <= 0)) { | ||
449 | SDL_SetError("Invalid width or height"); | ||
450 | return(0); | ||
451 | } | ||
452 | |||
453 | /* Try the original video mode, get the closest depth */ | ||
454 | native_bpp = SDL_VideoModeOK(*w, *h, *BitsPerPixel, flags); | ||
455 | if ( native_bpp == *BitsPerPixel ) { | ||
456 | return(1); | ||
457 | } | ||
458 | if ( native_bpp > 0 ) { | ||
459 | *BitsPerPixel = native_bpp; | ||
460 | return(1); | ||
461 | } | ||
462 | |||
463 | /* No exact size match at any depth, look for closest match */ | ||
464 | SDL_memset(&format, 0, sizeof(format)); | ||
465 | supported = 0; | ||
466 | table = ((*BitsPerPixel+7)/8)-1; | ||
467 | SDL_closest_depths[table][0] = *BitsPerPixel; | ||
468 | SDL_closest_depths[table][7] = SDL_VideoSurface->format->BitsPerPixel; | ||
469 | for ( b = 0; !supported && SDL_closest_depths[table][b]; ++b ) { | ||
470 | int best; | ||
471 | |||
472 | format.BitsPerPixel = SDL_closest_depths[table][b]; | ||
473 | sizes = SDL_ListModes(&format, flags); | ||
474 | if ( sizes == (SDL_Rect **)0 ) { | ||
475 | /* No sizes supported at this bit-depth */ | ||
476 | continue; | ||
477 | } | ||
478 | best=0; | ||
479 | for ( i=0; sizes[i]; ++i ) { | ||
480 | /* Mode with both dimensions bigger or equal than asked ? */ | ||
481 | if ((sizes[i]->w >= *w) && (sizes[i]->h >= *h)) { | ||
482 | /* Mode with any dimension smaller or equal than current best ? */ | ||
483 | if ((sizes[i]->w <= sizes[best]->w) || (sizes[i]->h <= sizes[best]->h)) { | ||
484 | /* Now choose the mode that has less pixels */ | ||
485 | if ((sizes[i]->w * sizes[i]->h) <= (sizes[best]->w * sizes[best]->h)) { | ||
486 | best=i; | ||
487 | supported = 1; | ||
488 | } | ||
489 | } | ||
490 | } | ||
491 | } | ||
492 | if (supported) { | ||
493 | *w=sizes[best]->w; | ||
494 | *h=sizes[best]->h; | ||
495 | *BitsPerPixel = SDL_closest_depths[table][b]; | ||
496 | } | ||
497 | } | ||
498 | if ( ! supported ) { | ||
499 | SDL_SetError("No video mode large enough for %dx%d", *w, *h); | ||
500 | } | ||
501 | return(supported); | ||
502 | } | ||
503 | |||
504 | /* This should probably go somewhere else -- like SDL_surface.c */ | ||
505 | static void SDL_ClearSurface(SDL_Surface *surface) | ||
506 | { | ||
507 | Uint32 black; | ||
508 | |||
509 | black = SDL_MapRGB(surface->format, 0, 0, 0); | ||
510 | SDL_FillRect(surface, NULL, black); | ||
511 | if ((surface->flags&SDL_HWSURFACE) && (surface->flags&SDL_DOUBLEBUF)) { | ||
512 | SDL_Flip(surface); | ||
513 | SDL_FillRect(surface, NULL, black); | ||
514 | } | ||
515 | if (surface->flags&SDL_FULLSCREEN) { | ||
516 | SDL_Flip(surface); | ||
517 | } | ||
518 | } | ||
519 | |||
520 | /* | ||
521 | * Create a shadow surface suitable for fooling the app. :-) | ||
522 | */ | ||
523 | static void SDL_CreateShadowSurface(int depth) | ||
524 | { | ||
525 | Uint32 Rmask, Gmask, Bmask; | ||
526 | |||
527 | /* Allocate the shadow surface */ | ||
528 | if ( depth == (SDL_VideoSurface->format)->BitsPerPixel ) { | ||
529 | Rmask = (SDL_VideoSurface->format)->Rmask; | ||
530 | Gmask = (SDL_VideoSurface->format)->Gmask; | ||
531 | Bmask = (SDL_VideoSurface->format)->Bmask; | ||
532 | } else { | ||
533 | Rmask = Gmask = Bmask = 0; | ||
534 | } | ||
535 | SDL_ShadowSurface = SDL_CreateRGBSurface(SDL_SWSURFACE, | ||
536 | SDL_VideoSurface->w, SDL_VideoSurface->h, | ||
537 | depth, Rmask, Gmask, Bmask, 0); | ||
538 | if ( SDL_ShadowSurface == NULL ) { | ||
539 | return; | ||
540 | } | ||
541 | |||
542 | /* 8-bit shadow surfaces report that they have exclusive palette */ | ||
543 | if ( SDL_ShadowSurface->format->palette ) { | ||
544 | SDL_ShadowSurface->flags |= SDL_HWPALETTE; | ||
545 | if ( depth == (SDL_VideoSurface->format)->BitsPerPixel ) { | ||
546 | SDL_memcpy(SDL_ShadowSurface->format->palette->colors, | ||
547 | SDL_VideoSurface->format->palette->colors, | ||
548 | SDL_VideoSurface->format->palette->ncolors* | ||
549 | sizeof(SDL_Color)); | ||
550 | } else { | ||
551 | SDL_DitherColors( | ||
552 | SDL_ShadowSurface->format->palette->colors, depth); | ||
553 | } | ||
554 | } | ||
555 | |||
556 | /* If the video surface is resizable, the shadow should say so */ | ||
557 | if ( (SDL_VideoSurface->flags & SDL_RESIZABLE) == SDL_RESIZABLE ) { | ||
558 | SDL_ShadowSurface->flags |= SDL_RESIZABLE; | ||
559 | } | ||
560 | /* If the video surface has no frame, the shadow should say so */ | ||
561 | if ( (SDL_VideoSurface->flags & SDL_NOFRAME) == SDL_NOFRAME ) { | ||
562 | SDL_ShadowSurface->flags |= SDL_NOFRAME; | ||
563 | } | ||
564 | /* If the video surface is fullscreen, the shadow should say so */ | ||
565 | if ( (SDL_VideoSurface->flags & SDL_FULLSCREEN) == SDL_FULLSCREEN ) { | ||
566 | SDL_ShadowSurface->flags |= SDL_FULLSCREEN; | ||
567 | } | ||
568 | /* If the video surface is flippable, the shadow should say so */ | ||
569 | if ( (SDL_VideoSurface->flags & SDL_DOUBLEBUF) == SDL_DOUBLEBUF ) { | ||
570 | SDL_ShadowSurface->flags |= SDL_DOUBLEBUF; | ||
571 | } | ||
572 | return; | ||
573 | } | ||
574 | |||
575 | #ifdef __QNXNTO__ | ||
576 | #include <sys/neutrino.h> | ||
577 | #endif /* __QNXNTO__ */ | ||
578 | |||
579 | #ifdef WIN32 | ||
580 | extern int sysevents_mouse_pressed; | ||
581 | #endif | ||
582 | |||
583 | /* | ||
584 | * Set the requested video mode, allocating a shadow buffer if necessary. | ||
585 | */ | ||
586 | SDL_Surface * SDL_SetVideoMode (int width, int height, int bpp, Uint32 flags) | ||
587 | { | ||
588 | SDL_VideoDevice *video, *this; | ||
589 | SDL_Surface *prev_mode, *mode; | ||
590 | int video_w; | ||
591 | int video_h; | ||
592 | int video_bpp; | ||
593 | int is_opengl; | ||
594 | SDL_GrabMode saved_grab; | ||
595 | |||
596 | #ifdef WIN32 | ||
597 | sysevents_mouse_pressed = 0; | ||
598 | #endif | ||
599 | |||
600 | /* Start up the video driver, if necessary.. | ||
601 | WARNING: This is the only function protected this way! | ||
602 | */ | ||
603 | if ( ! current_video ) { | ||
604 | if ( SDL_Init(SDL_INIT_VIDEO|SDL_INIT_NOPARACHUTE) < 0 ) { | ||
605 | return(NULL); | ||
606 | } | ||
607 | } | ||
608 | this = video = current_video; | ||
609 | |||
610 | /* Default to the current width and height */ | ||
611 | if ( width == 0 ) { | ||
612 | width = video->info.current_w; | ||
613 | } | ||
614 | if ( height == 0 ) { | ||
615 | height = video->info.current_h; | ||
616 | } | ||
617 | /* Default to the current video bpp */ | ||
618 | if ( bpp == 0 ) { | ||
619 | flags |= SDL_ANYFORMAT; | ||
620 | bpp = SDL_VideoSurface->format->BitsPerPixel; | ||
621 | } | ||
622 | |||
623 | /* Get a good video mode, the closest one possible */ | ||
624 | video_w = width; | ||
625 | video_h = height; | ||
626 | video_bpp = bpp; | ||
627 | if ( ! SDL_GetVideoMode(&video_w, &video_h, &video_bpp, flags) ) { | ||
628 | return(NULL); | ||
629 | } | ||
630 | |||
631 | /* Check the requested flags */ | ||
632 | /* There's no palette in > 8 bits-per-pixel mode */ | ||
633 | if ( video_bpp > 8 ) { | ||
634 | flags &= ~SDL_HWPALETTE; | ||
635 | } | ||
636 | #if 0 | ||
637 | if ( (flags&SDL_FULLSCREEN) != SDL_FULLSCREEN ) { | ||
638 | /* There's no windowed double-buffering */ | ||
639 | flags &= ~SDL_DOUBLEBUF; | ||
640 | } | ||
641 | #endif | ||
642 | if ( (flags&SDL_DOUBLEBUF) == SDL_DOUBLEBUF ) { | ||
643 | /* Use hardware surfaces when double-buffering */ | ||
644 | flags |= SDL_HWSURFACE; | ||
645 | } | ||
646 | |||
647 | is_opengl = ( ( flags & SDL_OPENGL ) == SDL_OPENGL ); | ||
648 | if ( is_opengl ) { | ||
649 | /* These flags are for 2D video modes only */ | ||
650 | flags &= ~(SDL_HWSURFACE|SDL_DOUBLEBUF); | ||
651 | } | ||
652 | /* Reset the keyboard here so event callbacks can run */ | ||
653 | SDL_ResetKeyboard(); | ||
654 | SDL_ResetMouse(); | ||
655 | SDL_SetMouseRange(width, height); | ||
656 | SDL_cursorstate &= ~CURSOR_USINGSW; | ||
657 | |||
658 | /* Clean up any previous video mode */ | ||
659 | if ( SDL_PublicSurface != NULL ) { | ||
660 | SDL_PublicSurface = NULL; | ||
661 | } | ||
662 | if ( SDL_ShadowSurface != NULL ) { | ||
663 | SDL_Surface *ready_to_go; | ||
664 | ready_to_go = SDL_ShadowSurface; | ||
665 | SDL_ShadowSurface = NULL; | ||
666 | SDL_FreeSurface(ready_to_go); | ||
667 | } | ||
668 | if ( video->physpal ) { | ||
669 | SDL_free(video->physpal->colors); | ||
670 | SDL_free(video->physpal); | ||
671 | video->physpal = NULL; | ||
672 | } | ||
673 | if( video->gammacols) { | ||
674 | SDL_free(video->gammacols); | ||
675 | video->gammacols = NULL; | ||
676 | } | ||
677 | /* Save the previous grab state and turn off grab for mode switch */ | ||
678 | saved_grab = SDL_WM_GrabInputOff(); | ||
679 | |||
680 | /* Try to set the video mode, along with offset and clipping */ | ||
681 | prev_mode = SDL_VideoSurface; | ||
682 | SDL_LockCursor(); | ||
683 | SDL_VideoSurface = NULL; /* In case it's freed by driver */ | ||
684 | mode = video->SetVideoMode(this, prev_mode,video_w,video_h,video_bpp,flags); | ||
685 | |||
686 | if ( mode ) { /* Prevent resize events from mode change */ | ||
687 | /* But not on OS/2 */ | ||
688 | #ifndef __OS2__ | ||
689 | SDL_PrivateResize(mode->w, mode->h); | ||
690 | #endif | ||
691 | |||
692 | /* Sam - If we asked for OpenGL mode, and didn't get it, fail */ | ||
693 | if ( is_opengl && !(mode->flags & SDL_OPENGL) ) { | ||
694 | mode = NULL; | ||
695 | SDL_SetError("OpenGL not available"); | ||
696 | } | ||
697 | } | ||
698 | /* | ||
699 | * rcg11292000 | ||
700 | * If you try to set an SDL_OPENGL surface, and fail to find a | ||
701 | * matching visual, then the next call to SDL_SetVideoMode() | ||
702 | * will segfault, since we no longer point to a dummy surface, | ||
703 | * but rather NULL. | ||
704 | * Sam 11/29/00 | ||
705 | * WARNING, we need to make sure that the previous mode hasn't | ||
706 | * already been freed by the video driver. What do we do in | ||
707 | * that case? Should we call SDL_VideoInit() again? | ||
708 | */ | ||
709 | SDL_VideoSurface = (mode != NULL) ? mode : prev_mode; | ||
710 | if ( (mode != NULL) && (!is_opengl) ) { | ||
711 | /* Sanity check */ | ||
712 | if ( (mode->w < width) || (mode->h < height) ) { | ||
713 | SDL_SetError("Video mode smaller than requested"); | ||
714 | return(NULL); | ||
715 | } | ||
716 | |||
717 | /* If we have a palettized surface, create a default palette */ | ||
718 | if ( mode->format->palette ) { | ||
719 | SDL_PixelFormat *vf = mode->format; | ||
720 | SDL_DitherColors(vf->palette->colors, vf->BitsPerPixel); | ||
721 | video->SetColors(this, 0, vf->palette->ncolors, | ||
722 | vf->palette->colors); | ||
723 | } | ||
724 | /* Clear the surface to black */ | ||
725 | video->offset_x = 0; | ||
726 | video->offset_y = 0; | ||
727 | mode->offset = 0; | ||
728 | SDL_SetClipRect(mode, NULL); | ||
729 | SDL_ClearSurface(mode); | ||
730 | /* Now adjust the offsets to match the desired mode */ | ||
731 | video->offset_x = (mode->w-width)/2; | ||
732 | video->offset_y = (mode->h-height)/2; | ||
733 | mode->offset = video->offset_y*mode->pitch + | ||
734 | video->offset_x*mode->format->BytesPerPixel; | ||
735 | #ifdef DEBUG_VIDEO | ||
736 | fprintf(stderr, | ||
737 | "Requested mode: %dx%dx%d, obtained mode %dx%dx%d (offset %d)\n", | ||
738 | width, height, bpp, | ||
739 | mode->w, mode->h, mode->format->BitsPerPixel, mode->offset); | ||
740 | #endif | ||
741 | mode->w = width; | ||
742 | mode->h = height; | ||
743 | SDL_SetClipRect(mode, NULL); | ||
744 | } | ||
745 | SDL_ResetCursor(); | ||
746 | SDL_UnlockCursor(); | ||
747 | |||
748 | /* If we failed setting a video mode, return NULL... (Uh Oh!) */ | ||
749 | if ( mode == NULL ) { | ||
750 | return(NULL); | ||
751 | } | ||
752 | |||
753 | /* If there is no window manager, set the SDL_NOFRAME flag */ | ||
754 | if ( ! video->info.wm_available ) { | ||
755 | mode->flags |= SDL_NOFRAME; | ||
756 | } | ||
757 | |||
758 | /* Reset the mouse cursor and grab for new video mode */ | ||
759 | SDL_SetCursor(NULL); | ||
760 | |||
761 | if ( video->UpdateMouse ) { | ||
762 | video->UpdateMouse(this); | ||
763 | } | ||
764 | SDL_WM_GrabInput(saved_grab); | ||
765 | SDL_GetRelativeMouseState(NULL, NULL); /* Clear first large delta */ | ||
766 | |||
767 | #if SDL_VIDEO_OPENGL | ||
768 | /* Load GL symbols (before MakeCurrent, where we need glGetString). */ | ||
769 | if ( flags & (SDL_OPENGL | SDL_OPENGLBLIT) ) { | ||
770 | |||
771 | #if defined(__QNXNTO__) && (_NTO_VERSION < 630) | ||
772 | #define __SDL_NOGETPROCADDR__ | ||
773 | #elif defined(__MINT__) | ||
774 | #define __SDL_NOGETPROCADDR__ | ||
775 | #endif | ||
776 | #ifdef __SDL_NOGETPROCADDR__ | ||
777 | #define SDL_PROC(ret,func,params) video->func=func; | ||
778 | #else | ||
779 | #define SDL_PROC(ret,func,params) \ | ||
780 | do { \ | ||
781 | video->func = SDL_GL_GetProcAddress(#func); \ | ||
782 | if ( ! video->func ) { \ | ||
783 | SDL_SetError("Couldn't load GL function %s: %s\n", #func, SDL_GetError()); \ | ||
784 | return(NULL); \ | ||
785 | } \ | ||
786 | } while ( 0 ); | ||
787 | |||
788 | #endif /* __SDL_NOGETPROCADDR__ */ | ||
789 | |||
790 | #include "SDL_glfuncs.h" | ||
791 | #undef SDL_PROC | ||
792 | } | ||
793 | #endif /* SDL_VIDEO_OPENGL */ | ||
794 | |||
795 | /* If we're running OpenGL, make the context current */ | ||
796 | if ( (video->screen->flags & SDL_OPENGL) && | ||
797 | video->GL_MakeCurrent ) { | ||
798 | if ( video->GL_MakeCurrent(this) < 0 ) { | ||
799 | return(NULL); | ||
800 | } | ||
801 | } | ||
802 | |||
803 | /* Set up a fake SDL surface for OpenGL "blitting" */ | ||
804 | if ( (flags & SDL_OPENGLBLIT) == SDL_OPENGLBLIT ) { | ||
805 | /* Load GL functions for performing the texture updates */ | ||
806 | #if SDL_VIDEO_OPENGL | ||
807 | |||
808 | /* Create a software surface for blitting */ | ||
809 | #ifdef GL_VERSION_1_2 | ||
810 | /* If the implementation either supports the packed pixels | ||
811 | extension, or implements the core OpenGL 1.2 API, it will | ||
812 | support the GL_UNSIGNED_SHORT_5_6_5 texture format. | ||
813 | */ | ||
814 | if ( (bpp == 16) && | ||
815 | (SDL_strstr((const char *)video->glGetString(GL_EXTENSIONS), "GL_EXT_packed_pixels") || | ||
816 | (SDL_atof((const char *)video->glGetString(GL_VERSION)) >= 1.2f)) | ||
817 | ) { | ||
818 | video->is_32bit = 0; | ||
819 | SDL_VideoSurface = SDL_CreateRGBSurface( | ||
820 | flags, | ||
821 | width, | ||
822 | height, | ||
823 | 16, | ||
824 | 31 << 11, | ||
825 | 63 << 5, | ||
826 | 31, | ||
827 | 0 | ||
828 | ); | ||
829 | } | ||
830 | else | ||
831 | #endif /* OpenGL 1.2 */ | ||
832 | { | ||
833 | video->is_32bit = 1; | ||
834 | SDL_VideoSurface = SDL_CreateRGBSurface( | ||
835 | flags, | ||
836 | width, | ||
837 | height, | ||
838 | 32, | ||
839 | #if SDL_BYTEORDER == SDL_LIL_ENDIAN | ||
840 | 0x000000FF, | ||
841 | 0x0000FF00, | ||
842 | 0x00FF0000, | ||
843 | 0xFF000000 | ||
844 | #else | ||
845 | 0xFF000000, | ||
846 | 0x00FF0000, | ||
847 | 0x0000FF00, | ||
848 | 0x000000FF | ||
849 | #endif | ||
850 | ); | ||
851 | } | ||
852 | if ( ! SDL_VideoSurface ) { | ||
853 | return(NULL); | ||
854 | } | ||
855 | SDL_VideoSurface->flags = mode->flags | SDL_OPENGLBLIT; | ||
856 | |||
857 | /* Free the original video mode surface (is this safe?) */ | ||
858 | SDL_FreeSurface(mode); | ||
859 | |||
860 | /* Set the surface completely opaque & white by default */ | ||
861 | SDL_memset( SDL_VideoSurface->pixels, 255, SDL_VideoSurface->h * SDL_VideoSurface->pitch ); | ||
862 | video->glGenTextures( 1, &video->texture ); | ||
863 | video->glBindTexture( GL_TEXTURE_2D, video->texture ); | ||
864 | video->glTexImage2D( | ||
865 | GL_TEXTURE_2D, | ||
866 | 0, | ||
867 | video->is_32bit ? GL_RGBA : GL_RGB, | ||
868 | 256, | ||
869 | 256, | ||
870 | 0, | ||
871 | video->is_32bit ? GL_RGBA : GL_RGB, | ||
872 | #ifdef GL_VERSION_1_2 | ||
873 | video->is_32bit ? GL_UNSIGNED_BYTE : GL_UNSIGNED_SHORT_5_6_5, | ||
874 | #else | ||
875 | GL_UNSIGNED_BYTE, | ||
876 | #endif | ||
877 | NULL); | ||
878 | |||
879 | video->UpdateRects = SDL_GL_UpdateRectsLock; | ||
880 | #else | ||
881 | SDL_SetError("Somebody forgot to #define SDL_VIDEO_OPENGL"); | ||
882 | return(NULL); | ||
883 | #endif | ||
884 | } | ||
885 | |||
886 | /* Create a shadow surface if necessary */ | ||
887 | /* There are three conditions under which we create a shadow surface: | ||
888 | 1. We need a particular bits-per-pixel that we didn't get. | ||
889 | 2. We need a hardware palette and didn't get one. | ||
890 | 3. We need a software surface and got a hardware surface. | ||
891 | */ | ||
892 | if ( !(SDL_VideoSurface->flags & SDL_OPENGL) && | ||
893 | ( | ||
894 | ( !(flags&SDL_ANYFORMAT) && | ||
895 | (SDL_VideoSurface->format->BitsPerPixel != bpp)) || | ||
896 | ( (flags&SDL_HWPALETTE) && | ||
897 | !(SDL_VideoSurface->flags&SDL_HWPALETTE)) || | ||
898 | /* If the surface is in hardware, video writes are visible | ||
899 | as soon as they are performed, so we need to buffer them | ||
900 | */ | ||
901 | ( ((flags&SDL_HWSURFACE) == SDL_SWSURFACE) && | ||
902 | (SDL_VideoSurface->flags&SDL_HWSURFACE)) || | ||
903 | ( (flags&SDL_DOUBLEBUF) && | ||
904 | (SDL_VideoSurface->flags&SDL_HWSURFACE) && | ||
905 | !(SDL_VideoSurface->flags&SDL_DOUBLEBUF)) | ||
906 | ) ) { | ||
907 | SDL_CreateShadowSurface(bpp); | ||
908 | if ( SDL_ShadowSurface == NULL ) { | ||
909 | SDL_SetError("Couldn't create shadow surface"); | ||
910 | return(NULL); | ||
911 | } | ||
912 | SDL_PublicSurface = SDL_ShadowSurface; | ||
913 | } else { | ||
914 | SDL_PublicSurface = SDL_VideoSurface; | ||
915 | } | ||
916 | video->info.vfmt = SDL_VideoSurface->format; | ||
917 | video->info.current_w = SDL_VideoSurface->w; | ||
918 | video->info.current_h = SDL_VideoSurface->h; | ||
919 | |||
920 | /* We're done! */ | ||
921 | return(SDL_PublicSurface); | ||
922 | } | ||
923 | |||
924 | /* | ||
925 | * Convert a surface into the video pixel format. | ||
926 | */ | ||
927 | SDL_Surface * SDL_DisplayFormat (SDL_Surface *surface) | ||
928 | { | ||
929 | Uint32 flags; | ||
930 | |||
931 | if ( ! SDL_PublicSurface ) { | ||
932 | SDL_SetError("No video mode has been set"); | ||
933 | return(NULL); | ||
934 | } | ||
935 | /* Set the flags appropriate for copying to display surface */ | ||
936 | if (((SDL_PublicSurface->flags&SDL_HWSURFACE) == SDL_HWSURFACE) && current_video->info.blit_hw) | ||
937 | flags = SDL_HWSURFACE; | ||
938 | else | ||
939 | flags = SDL_SWSURFACE; | ||
940 | #ifdef AUTORLE_DISPLAYFORMAT | ||
941 | flags |= (surface->flags & (SDL_SRCCOLORKEY|SDL_SRCALPHA)); | ||
942 | flags |= SDL_RLEACCELOK; | ||
943 | #else | ||
944 | flags |= surface->flags & (SDL_SRCCOLORKEY|SDL_SRCALPHA|SDL_RLEACCELOK); | ||
945 | #endif | ||
946 | return(SDL_ConvertSurface(surface, SDL_PublicSurface->format, flags)); | ||
947 | } | ||
948 | |||
949 | /* | ||
950 | * Convert a surface into a format that's suitable for blitting to | ||
951 | * the screen, but including an alpha channel. | ||
952 | */ | ||
953 | SDL_Surface *SDL_DisplayFormatAlpha(SDL_Surface *surface) | ||
954 | { | ||
955 | SDL_PixelFormat *vf; | ||
956 | SDL_PixelFormat *format; | ||
957 | SDL_Surface *converted; | ||
958 | Uint32 flags; | ||
959 | /* default to ARGB8888 */ | ||
960 | Uint32 amask = 0xff000000; | ||
961 | Uint32 rmask = 0x00ff0000; | ||
962 | Uint32 gmask = 0x0000ff00; | ||
963 | Uint32 bmask = 0x000000ff; | ||
964 | |||
965 | if ( ! SDL_PublicSurface ) { | ||
966 | SDL_SetError("No video mode has been set"); | ||
967 | return(NULL); | ||
968 | } | ||
969 | vf = SDL_PublicSurface->format; | ||
970 | |||
971 | switch(vf->BytesPerPixel) { | ||
972 | case 2: | ||
973 | /* For XGY5[56]5, use, AXGY8888, where {X, Y} = {R, B}. | ||
974 | For anything else (like ARGB4444) it doesn't matter | ||
975 | since we have no special code for it anyway */ | ||
976 | if ( (vf->Rmask == 0x1f) && | ||
977 | (vf->Bmask == 0xf800 || vf->Bmask == 0x7c00)) { | ||
978 | rmask = 0xff; | ||
979 | bmask = 0xff0000; | ||
980 | } | ||
981 | break; | ||
982 | |||
983 | case 3: | ||
984 | case 4: | ||
985 | /* Keep the video format, as long as the high 8 bits are | ||
986 | unused or alpha */ | ||
987 | if ( (vf->Rmask == 0xff) && (vf->Bmask == 0xff0000) ) { | ||
988 | rmask = 0xff; | ||
989 | bmask = 0xff0000; | ||
990 | } else if ( vf->Rmask == 0xFF00 && (vf->Bmask == 0xFF000000) ) { | ||
991 | amask = 0x000000FF; | ||
992 | rmask = 0x0000FF00; | ||
993 | gmask = 0x00FF0000; | ||
994 | bmask = 0xFF000000; | ||
995 | } | ||
996 | break; | ||
997 | |||
998 | default: | ||
999 | /* We have no other optimised formats right now. When/if a new | ||
1000 | optimised alpha format is written, add the converter here */ | ||
1001 | break; | ||
1002 | } | ||
1003 | format = SDL_AllocFormat(32, rmask, gmask, bmask, amask); | ||
1004 | flags = SDL_PublicSurface->flags & SDL_HWSURFACE; | ||
1005 | flags |= surface->flags & (SDL_SRCALPHA | SDL_RLEACCELOK); | ||
1006 | converted = SDL_ConvertSurface(surface, format, flags); | ||
1007 | SDL_FreeFormat(format); | ||
1008 | return(converted); | ||
1009 | } | ||
1010 | |||
1011 | /* | ||
1012 | * Update a specific portion of the physical screen | ||
1013 | */ | ||
1014 | void SDL_UpdateRect(SDL_Surface *screen, Sint32 x, Sint32 y, Uint32 w, Uint32 h) | ||
1015 | { | ||
1016 | if ( screen ) { | ||
1017 | SDL_Rect rect; | ||
1018 | |||
1019 | /* Perform some checking */ | ||
1020 | if ( w == 0 ) | ||
1021 | w = screen->w; | ||
1022 | if ( h == 0 ) | ||
1023 | h = screen->h; | ||
1024 | if ( (int)(x+w) > screen->w ) | ||
1025 | return; | ||
1026 | if ( (int)(y+h) > screen->h ) | ||
1027 | return; | ||
1028 | |||
1029 | /* Fill the rectangle */ | ||
1030 | rect.x = (Sint16)x; | ||
1031 | rect.y = (Sint16)y; | ||
1032 | rect.w = (Uint16)w; | ||
1033 | rect.h = (Uint16)h; | ||
1034 | SDL_UpdateRects(screen, 1, &rect); | ||
1035 | } | ||
1036 | } | ||
1037 | void SDL_UpdateRects (SDL_Surface *screen, int numrects, SDL_Rect *rects) | ||
1038 | { | ||
1039 | int i; | ||
1040 | SDL_VideoDevice *video = current_video; | ||
1041 | SDL_VideoDevice *this = current_video; | ||
1042 | |||
1043 | if ( (screen->flags & (SDL_OPENGL | SDL_OPENGLBLIT)) == SDL_OPENGL ) { | ||
1044 | SDL_SetError("OpenGL active, use SDL_GL_SwapBuffers()"); | ||
1045 | return; | ||
1046 | } | ||
1047 | if ( screen == SDL_ShadowSurface ) { | ||
1048 | /* Blit the shadow surface using saved mapping */ | ||
1049 | SDL_Palette *pal = screen->format->palette; | ||
1050 | SDL_Color *saved_colors = NULL; | ||
1051 | if ( pal && !(SDL_VideoSurface->flags & SDL_HWPALETTE) ) { | ||
1052 | /* simulated 8bpp, use correct physical palette */ | ||
1053 | saved_colors = pal->colors; | ||
1054 | if ( video->gammacols ) { | ||
1055 | /* gamma-corrected palette */ | ||
1056 | pal->colors = video->gammacols; | ||
1057 | } else if ( video->physpal ) { | ||
1058 | /* physical palette different from logical */ | ||
1059 | pal->colors = video->physpal->colors; | ||
1060 | } | ||
1061 | } | ||
1062 | if ( SHOULD_DRAWCURSOR(SDL_cursorstate) ) { | ||
1063 | SDL_LockCursor(); | ||
1064 | SDL_DrawCursor(SDL_ShadowSurface); | ||
1065 | for ( i=0; i<numrects; ++i ) { | ||
1066 | SDL_LowerBlit(SDL_ShadowSurface, &rects[i], | ||
1067 | SDL_VideoSurface, &rects[i]); | ||
1068 | } | ||
1069 | SDL_EraseCursor(SDL_ShadowSurface); | ||
1070 | SDL_UnlockCursor(); | ||
1071 | } else { | ||
1072 | for ( i=0; i<numrects; ++i ) { | ||
1073 | SDL_LowerBlit(SDL_ShadowSurface, &rects[i], | ||
1074 | SDL_VideoSurface, &rects[i]); | ||
1075 | } | ||
1076 | } | ||
1077 | if ( saved_colors ) { | ||
1078 | pal->colors = saved_colors; | ||
1079 | } | ||
1080 | |||
1081 | /* Fall through to video surface update */ | ||
1082 | screen = SDL_VideoSurface; | ||
1083 | } | ||
1084 | if ( screen == SDL_VideoSurface ) { | ||
1085 | /* Update the video surface */ | ||
1086 | if ( screen->offset ) { | ||
1087 | for ( i=0; i<numrects; ++i ) { | ||
1088 | rects[i].x += video->offset_x; | ||
1089 | rects[i].y += video->offset_y; | ||
1090 | } | ||
1091 | video->UpdateRects(this, numrects, rects); | ||
1092 | for ( i=0; i<numrects; ++i ) { | ||
1093 | rects[i].x -= video->offset_x; | ||
1094 | rects[i].y -= video->offset_y; | ||
1095 | } | ||
1096 | } else { | ||
1097 | video->UpdateRects(this, numrects, rects); | ||
1098 | } | ||
1099 | } | ||
1100 | } | ||
1101 | |||
1102 | /* | ||
1103 | * Performs hardware double buffering, if possible, or a full update if not. | ||
1104 | */ | ||
1105 | int SDL_Flip(SDL_Surface *screen) | ||
1106 | { | ||
1107 | SDL_VideoDevice *video = current_video; | ||
1108 | /* Copy the shadow surface to the video surface */ | ||
1109 | if ( screen == SDL_ShadowSurface ) { | ||
1110 | SDL_Rect rect; | ||
1111 | SDL_Palette *pal = screen->format->palette; | ||
1112 | SDL_Color *saved_colors = NULL; | ||
1113 | if ( pal && !(SDL_VideoSurface->flags & SDL_HWPALETTE) ) { | ||
1114 | /* simulated 8bpp, use correct physical palette */ | ||
1115 | saved_colors = pal->colors; | ||
1116 | if ( video->gammacols ) { | ||
1117 | /* gamma-corrected palette */ | ||
1118 | pal->colors = video->gammacols; | ||
1119 | } else if ( video->physpal ) { | ||
1120 | /* physical palette different from logical */ | ||
1121 | pal->colors = video->physpal->colors; | ||
1122 | } | ||
1123 | } | ||
1124 | |||
1125 | rect.x = 0; | ||
1126 | rect.y = 0; | ||
1127 | rect.w = screen->w; | ||
1128 | rect.h = screen->h; | ||
1129 | if ( SHOULD_DRAWCURSOR(SDL_cursorstate) ) { | ||
1130 | SDL_LockCursor(); | ||
1131 | SDL_DrawCursor(SDL_ShadowSurface); | ||
1132 | SDL_LowerBlit(SDL_ShadowSurface, &rect, | ||
1133 | SDL_VideoSurface, &rect); | ||
1134 | SDL_EraseCursor(SDL_ShadowSurface); | ||
1135 | SDL_UnlockCursor(); | ||
1136 | } else { | ||
1137 | SDL_LowerBlit(SDL_ShadowSurface, &rect, | ||
1138 | SDL_VideoSurface, &rect); | ||
1139 | } | ||
1140 | if ( saved_colors ) { | ||
1141 | pal->colors = saved_colors; | ||
1142 | } | ||
1143 | |||
1144 | /* Fall through to video surface update */ | ||
1145 | screen = SDL_VideoSurface; | ||
1146 | } | ||
1147 | if ( (screen->flags & SDL_DOUBLEBUF) == SDL_DOUBLEBUF ) { | ||
1148 | SDL_VideoDevice *this = current_video; | ||
1149 | return(video->FlipHWSurface(this, SDL_VideoSurface)); | ||
1150 | } else { | ||
1151 | SDL_UpdateRect(screen, 0, 0, 0, 0); | ||
1152 | } | ||
1153 | return(0); | ||
1154 | } | ||
1155 | |||
1156 | static void SetPalette_logical(SDL_Surface *screen, SDL_Color *colors, | ||
1157 | int firstcolor, int ncolors) | ||
1158 | { | ||
1159 | SDL_Palette *pal = screen->format->palette; | ||
1160 | SDL_Palette *vidpal; | ||
1161 | |||
1162 | if ( colors != (pal->colors + firstcolor) ) { | ||
1163 | SDL_memcpy(pal->colors + firstcolor, colors, | ||
1164 | ncolors * sizeof(*colors)); | ||
1165 | } | ||
1166 | |||
1167 | if ( current_video && SDL_VideoSurface ) { | ||
1168 | vidpal = SDL_VideoSurface->format->palette; | ||
1169 | if ( (screen == SDL_ShadowSurface) && vidpal ) { | ||
1170 | /* | ||
1171 | * This is a shadow surface, and the physical | ||
1172 | * framebuffer is also indexed. Propagate the | ||
1173 | * changes to its logical palette so that | ||
1174 | * updates are always identity blits | ||
1175 | */ | ||
1176 | SDL_memcpy(vidpal->colors + firstcolor, colors, | ||
1177 | ncolors * sizeof(*colors)); | ||
1178 | } | ||
1179 | } | ||
1180 | SDL_FormatChanged(screen); | ||
1181 | } | ||
1182 | |||
1183 | static int SetPalette_physical(SDL_Surface *screen, | ||
1184 | SDL_Color *colors, int firstcolor, int ncolors) | ||
1185 | { | ||
1186 | SDL_VideoDevice *video = current_video; | ||
1187 | int gotall = 1; | ||
1188 | |||
1189 | if ( video->physpal ) { | ||
1190 | /* We need to copy the new colors, since we haven't | ||
1191 | * already done the copy in the logical set above. | ||
1192 | */ | ||
1193 | SDL_memcpy(video->physpal->colors + firstcolor, | ||
1194 | colors, ncolors * sizeof(*colors)); | ||
1195 | } | ||
1196 | if ( screen == SDL_ShadowSurface ) { | ||
1197 | if ( SDL_VideoSurface->flags & SDL_HWPALETTE ) { | ||
1198 | /* | ||
1199 | * The real screen is also indexed - set its physical | ||
1200 | * palette. The physical palette does not include the | ||
1201 | * gamma modification, we apply it directly instead, | ||
1202 | * but this only happens if we have hardware palette. | ||
1203 | */ | ||
1204 | screen = SDL_VideoSurface; | ||
1205 | } else { | ||
1206 | /* | ||
1207 | * The video surface is not indexed - invalidate any | ||
1208 | * active shadow-to-video blit mappings. | ||
1209 | */ | ||
1210 | if ( screen->map->dst == SDL_VideoSurface ) { | ||
1211 | SDL_InvalidateMap(screen->map); | ||
1212 | } | ||
1213 | if ( video->gamma ) { | ||
1214 | if( ! video->gammacols ) { | ||
1215 | SDL_Palette *pp = video->physpal; | ||
1216 | if(!pp) | ||
1217 | pp = screen->format->palette; | ||
1218 | video->gammacols = SDL_malloc(pp->ncolors | ||
1219 | * sizeof(SDL_Color)); | ||
1220 | SDL_ApplyGamma(video->gamma, | ||
1221 | pp->colors, | ||
1222 | video->gammacols, | ||
1223 | pp->ncolors); | ||
1224 | } else { | ||
1225 | SDL_ApplyGamma(video->gamma, colors, | ||
1226 | video->gammacols | ||
1227 | + firstcolor, | ||
1228 | ncolors); | ||
1229 | } | ||
1230 | } | ||
1231 | SDL_UpdateRect(screen, 0, 0, 0, 0); | ||
1232 | } | ||
1233 | } | ||
1234 | |||
1235 | if ( screen == SDL_VideoSurface ) { | ||
1236 | SDL_Color gcolors[256]; | ||
1237 | |||
1238 | if ( video->gamma ) { | ||
1239 | SDL_ApplyGamma(video->gamma, colors, gcolors, ncolors); | ||
1240 | colors = gcolors; | ||
1241 | } | ||
1242 | gotall = video->SetColors(video, firstcolor, ncolors, colors); | ||
1243 | if ( ! gotall ) { | ||
1244 | /* The video flags shouldn't have SDL_HWPALETTE, and | ||
1245 | the video driver is responsible for copying back the | ||
1246 | correct colors into the video surface palette. | ||
1247 | */ | ||
1248 | ; | ||
1249 | } | ||
1250 | SDL_CursorPaletteChanged(); | ||
1251 | } | ||
1252 | return gotall; | ||
1253 | } | ||
1254 | |||
1255 | /* | ||
1256 | * Set the physical and/or logical colormap of a surface: | ||
1257 | * Only the screen has a physical colormap. It determines what is actually | ||
1258 | * sent to the display. | ||
1259 | * The logical colormap is used to map blits to/from the surface. | ||
1260 | * 'which' is one or both of SDL_LOGPAL, SDL_PHYSPAL | ||
1261 | * | ||
1262 | * Return nonzero if all colours were set as requested, or 0 otherwise. | ||
1263 | */ | ||
1264 | int SDL_SetPalette(SDL_Surface *screen, int which, | ||
1265 | SDL_Color *colors, int firstcolor, int ncolors) | ||
1266 | { | ||
1267 | SDL_Palette *pal; | ||
1268 | int gotall; | ||
1269 | int palsize; | ||
1270 | |||
1271 | if ( !screen ) { | ||
1272 | return 0; | ||
1273 | } | ||
1274 | if ( !current_video || screen != SDL_PublicSurface ) { | ||
1275 | /* only screens have physical palettes */ | ||
1276 | which &= ~SDL_PHYSPAL; | ||
1277 | } else if ( (screen->flags & SDL_HWPALETTE) != SDL_HWPALETTE ) { | ||
1278 | /* hardware palettes required for split colormaps */ | ||
1279 | which |= SDL_PHYSPAL | SDL_LOGPAL; | ||
1280 | } | ||
1281 | |||
1282 | /* Verify the parameters */ | ||
1283 | pal = screen->format->palette; | ||
1284 | if( !pal ) { | ||
1285 | return 0; /* not a palettized surface */ | ||
1286 | } | ||
1287 | gotall = 1; | ||
1288 | palsize = 1 << screen->format->BitsPerPixel; | ||
1289 | if ( ncolors > (palsize - firstcolor) ) { | ||
1290 | ncolors = (palsize - firstcolor); | ||
1291 | gotall = 0; | ||
1292 | } | ||
1293 | |||
1294 | if ( which & SDL_LOGPAL ) { | ||
1295 | /* | ||
1296 | * Logical palette change: The actual screen isn't affected, | ||
1297 | * but the internal colormap is altered so that the | ||
1298 | * interpretation of the pixel values (for blits etc) is | ||
1299 | * changed. | ||
1300 | */ | ||
1301 | SetPalette_logical(screen, colors, firstcolor, ncolors); | ||
1302 | } | ||
1303 | if ( which & SDL_PHYSPAL ) { | ||
1304 | SDL_VideoDevice *video = current_video; | ||
1305 | /* | ||
1306 | * Physical palette change: This doesn't affect the | ||
1307 | * program's idea of what the screen looks like, but changes | ||
1308 | * its actual appearance. | ||
1309 | */ | ||
1310 | if ( !video->physpal && !(which & SDL_LOGPAL) ) { | ||
1311 | /* Lazy physical palette allocation */ | ||
1312 | int size; | ||
1313 | SDL_Palette *pp = SDL_malloc(sizeof(*pp)); | ||
1314 | if ( !pp ) { | ||
1315 | return 0; | ||
1316 | } | ||
1317 | video->physpal = pp; | ||
1318 | pp->ncolors = pal->ncolors; | ||
1319 | size = pp->ncolors * sizeof(SDL_Color); | ||
1320 | pp->colors = SDL_malloc(size); | ||
1321 | if ( !pp->colors ) { | ||
1322 | return 0; | ||
1323 | } | ||
1324 | SDL_memcpy(pp->colors, pal->colors, size); | ||
1325 | } | ||
1326 | if ( ! SetPalette_physical(screen, | ||
1327 | colors, firstcolor, ncolors) ) { | ||
1328 | gotall = 0; | ||
1329 | } | ||
1330 | } | ||
1331 | return gotall; | ||
1332 | } | ||
1333 | |||
1334 | int SDL_SetColors(SDL_Surface *screen, SDL_Color *colors, int firstcolor, | ||
1335 | int ncolors) | ||
1336 | { | ||
1337 | return SDL_SetPalette(screen, SDL_LOGPAL | SDL_PHYSPAL, | ||
1338 | colors, firstcolor, ncolors); | ||
1339 | } | ||
1340 | |||
1341 | /* | ||
1342 | * Clean up the video subsystem | ||
1343 | */ | ||
1344 | void SDL_VideoQuit (void) | ||
1345 | { | ||
1346 | SDL_Surface *ready_to_go; | ||
1347 | |||
1348 | if ( current_video ) { | ||
1349 | SDL_VideoDevice *video = current_video; | ||
1350 | SDL_VideoDevice *this = current_video; | ||
1351 | |||
1352 | /* Halt event processing before doing anything else */ | ||
1353 | SDL_StopEventLoop(); | ||
1354 | |||
1355 | /* Clean up allocated window manager items */ | ||
1356 | if ( SDL_PublicSurface ) { | ||
1357 | SDL_PublicSurface = NULL; | ||
1358 | } | ||
1359 | SDL_CursorQuit(); | ||
1360 | |||
1361 | /* Just in case... */ | ||
1362 | SDL_WM_GrabInputOff(); | ||
1363 | |||
1364 | /* Clean up the system video */ | ||
1365 | video->VideoQuit(this); | ||
1366 | |||
1367 | /* Free any lingering surfaces */ | ||
1368 | ready_to_go = SDL_ShadowSurface; | ||
1369 | SDL_ShadowSurface = NULL; | ||
1370 | SDL_FreeSurface(ready_to_go); | ||
1371 | if ( SDL_VideoSurface != NULL ) { | ||
1372 | ready_to_go = SDL_VideoSurface; | ||
1373 | SDL_VideoSurface = NULL; | ||
1374 | SDL_FreeSurface(ready_to_go); | ||
1375 | } | ||
1376 | SDL_PublicSurface = NULL; | ||
1377 | |||
1378 | /* Clean up miscellaneous memory */ | ||
1379 | if ( video->physpal ) { | ||
1380 | SDL_free(video->physpal->colors); | ||
1381 | SDL_free(video->physpal); | ||
1382 | video->physpal = NULL; | ||
1383 | } | ||
1384 | if ( video->gammacols ) { | ||
1385 | SDL_free(video->gammacols); | ||
1386 | video->gammacols = NULL; | ||
1387 | } | ||
1388 | if ( video->gamma ) { | ||
1389 | SDL_free(video->gamma); | ||
1390 | video->gamma = NULL; | ||
1391 | } | ||
1392 | if ( video->wm_title != NULL ) { | ||
1393 | SDL_free(video->wm_title); | ||
1394 | video->wm_title = NULL; | ||
1395 | } | ||
1396 | if ( video->wm_icon != NULL ) { | ||
1397 | SDL_free(video->wm_icon); | ||
1398 | video->wm_icon = NULL; | ||
1399 | } | ||
1400 | |||
1401 | /* Finish cleaning up video subsystem */ | ||
1402 | video->free(this); | ||
1403 | current_video = NULL; | ||
1404 | } | ||
1405 | return; | ||
1406 | } | ||
1407 | |||
1408 | /* Load the GL driver library */ | ||
1409 | int SDL_GL_LoadLibrary(const char *path) | ||
1410 | { | ||
1411 | SDL_VideoDevice *video = current_video; | ||
1412 | SDL_VideoDevice *this = current_video; | ||
1413 | int retval; | ||
1414 | |||
1415 | retval = -1; | ||
1416 | if ( video == NULL ) { | ||
1417 | SDL_SetError("Video subsystem has not been initialized"); | ||
1418 | } else { | ||
1419 | if ( video->GL_LoadLibrary ) { | ||
1420 | retval = video->GL_LoadLibrary(this, path); | ||
1421 | } else { | ||
1422 | SDL_SetError("No dynamic GL support in video driver"); | ||
1423 | } | ||
1424 | } | ||
1425 | return(retval); | ||
1426 | } | ||
1427 | |||
1428 | void *SDL_GL_GetProcAddress(const char* proc) | ||
1429 | { | ||
1430 | SDL_VideoDevice *video = current_video; | ||
1431 | SDL_VideoDevice *this = current_video; | ||
1432 | void *func; | ||
1433 | |||
1434 | func = NULL; | ||
1435 | if ( video->GL_GetProcAddress ) { | ||
1436 | if ( video->gl_config.driver_loaded ) { | ||
1437 | func = video->GL_GetProcAddress(this, proc); | ||
1438 | } else { | ||
1439 | SDL_SetError("No GL driver has been loaded"); | ||
1440 | } | ||
1441 | } else { | ||
1442 | SDL_SetError("No dynamic GL support in video driver"); | ||
1443 | } | ||
1444 | return func; | ||
1445 | } | ||
1446 | |||
1447 | /* Set the specified GL attribute for setting up a GL video mode */ | ||
1448 | int SDL_GL_SetAttribute( SDL_GLattr attr, int value ) | ||
1449 | { | ||
1450 | int retval; | ||
1451 | SDL_VideoDevice *video = current_video; | ||
1452 | |||
1453 | retval = 0; | ||
1454 | switch (attr) { | ||
1455 | case SDL_GL_RED_SIZE: | ||
1456 | video->gl_config.red_size = value; | ||
1457 | break; | ||
1458 | case SDL_GL_GREEN_SIZE: | ||
1459 | video->gl_config.green_size = value; | ||
1460 | break; | ||
1461 | case SDL_GL_BLUE_SIZE: | ||
1462 | video->gl_config.blue_size = value; | ||
1463 | break; | ||
1464 | case SDL_GL_ALPHA_SIZE: | ||
1465 | video->gl_config.alpha_size = value; | ||
1466 | break; | ||
1467 | case SDL_GL_DOUBLEBUFFER: | ||
1468 | video->gl_config.double_buffer = value; | ||
1469 | break; | ||
1470 | case SDL_GL_BUFFER_SIZE: | ||
1471 | video->gl_config.buffer_size = value; | ||
1472 | break; | ||
1473 | case SDL_GL_DEPTH_SIZE: | ||
1474 | video->gl_config.depth_size = value; | ||
1475 | break; | ||
1476 | case SDL_GL_STENCIL_SIZE: | ||
1477 | video->gl_config.stencil_size = value; | ||
1478 | break; | ||
1479 | case SDL_GL_ACCUM_RED_SIZE: | ||
1480 | video->gl_config.accum_red_size = value; | ||
1481 | break; | ||
1482 | case SDL_GL_ACCUM_GREEN_SIZE: | ||
1483 | video->gl_config.accum_green_size = value; | ||
1484 | break; | ||
1485 | case SDL_GL_ACCUM_BLUE_SIZE: | ||
1486 | video->gl_config.accum_blue_size = value; | ||
1487 | break; | ||
1488 | case SDL_GL_ACCUM_ALPHA_SIZE: | ||
1489 | video->gl_config.accum_alpha_size = value; | ||
1490 | break; | ||
1491 | case SDL_GL_STEREO: | ||
1492 | video->gl_config.stereo = value; | ||
1493 | break; | ||
1494 | case SDL_GL_MULTISAMPLEBUFFERS: | ||
1495 | video->gl_config.multisamplebuffers = value; | ||
1496 | break; | ||
1497 | case SDL_GL_MULTISAMPLESAMPLES: | ||
1498 | video->gl_config.multisamplesamples = value; | ||
1499 | break; | ||
1500 | case SDL_GL_ACCELERATED_VISUAL: | ||
1501 | video->gl_config.accelerated = value; | ||
1502 | break; | ||
1503 | case SDL_GL_SWAP_CONTROL: | ||
1504 | video->gl_config.swap_control = value; | ||
1505 | break; | ||
1506 | default: | ||
1507 | SDL_SetError("Unknown OpenGL attribute"); | ||
1508 | retval = -1; | ||
1509 | break; | ||
1510 | } | ||
1511 | return(retval); | ||
1512 | } | ||
1513 | |||
1514 | /* Retrieve an attribute value from the windowing system. */ | ||
1515 | int SDL_GL_GetAttribute(SDL_GLattr attr, int* value) | ||
1516 | { | ||
1517 | int retval = -1; | ||
1518 | SDL_VideoDevice* video = current_video; | ||
1519 | SDL_VideoDevice* this = current_video; | ||
1520 | |||
1521 | if ( video->GL_GetAttribute ) { | ||
1522 | retval = this->GL_GetAttribute(this, attr, value); | ||
1523 | } else { | ||
1524 | *value = 0; | ||
1525 | SDL_SetError("GL_GetAttribute not supported"); | ||
1526 | } | ||
1527 | return retval; | ||
1528 | } | ||
1529 | |||
1530 | /* Perform a GL buffer swap on the current GL context */ | ||
1531 | void SDL_GL_SwapBuffers(void) | ||
1532 | { | ||
1533 | SDL_VideoDevice *video = current_video; | ||
1534 | SDL_VideoDevice *this = current_video; | ||
1535 | |||
1536 | if ( video->screen->flags & SDL_OPENGL ) { | ||
1537 | video->GL_SwapBuffers(this); | ||
1538 | } else { | ||
1539 | SDL_SetError("OpenGL video mode has not been set"); | ||
1540 | } | ||
1541 | } | ||
1542 | |||
1543 | /* Update rects with locking */ | ||
1544 | void SDL_GL_UpdateRectsLock(SDL_VideoDevice* this, int numrects, SDL_Rect *rects) | ||
1545 | { | ||
1546 | SDL_GL_Lock(); | ||
1547 | SDL_GL_UpdateRects(numrects, rects); | ||
1548 | SDL_GL_Unlock(); | ||
1549 | } | ||
1550 | |||
1551 | /* Update rects without state setting and changing (the caller is responsible for it) */ | ||
1552 | void SDL_GL_UpdateRects(int numrects, SDL_Rect *rects) | ||
1553 | { | ||
1554 | #if SDL_VIDEO_OPENGL | ||
1555 | SDL_VideoDevice *this = current_video; | ||
1556 | SDL_Rect update, tmp; | ||
1557 | int x, y, i; | ||
1558 | |||
1559 | for ( i = 0; i < numrects; i++ ) | ||
1560 | { | ||
1561 | tmp.y = rects[i].y; | ||
1562 | tmp.h = rects[i].h; | ||
1563 | for ( y = 0; y <= rects[i].h / 256; y++ ) | ||
1564 | { | ||
1565 | tmp.x = rects[i].x; | ||
1566 | tmp.w = rects[i].w; | ||
1567 | for ( x = 0; x <= rects[i].w / 256; x++ ) | ||
1568 | { | ||
1569 | update.x = tmp.x; | ||
1570 | update.y = tmp.y; | ||
1571 | update.w = tmp.w; | ||
1572 | update.h = tmp.h; | ||
1573 | |||
1574 | if ( update.w > 256 ) | ||
1575 | update.w = 256; | ||
1576 | |||
1577 | if ( update.h > 256 ) | ||
1578 | update.h = 256; | ||
1579 | |||
1580 | this->glFlush(); | ||
1581 | this->glTexSubImage2D( | ||
1582 | GL_TEXTURE_2D, | ||
1583 | 0, | ||
1584 | 0, | ||
1585 | 0, | ||
1586 | update.w, | ||
1587 | update.h, | ||
1588 | this->is_32bit? GL_RGBA : GL_RGB, | ||
1589 | #ifdef GL_VERSION_1_2 | ||
1590 | this->is_32bit ? GL_UNSIGNED_BYTE : GL_UNSIGNED_SHORT_5_6_5, | ||
1591 | #else | ||
1592 | GL_UNSIGNED_BYTE, | ||
1593 | #endif | ||
1594 | (Uint8 *)this->screen->pixels + | ||
1595 | this->screen->format->BytesPerPixel * update.x + | ||
1596 | update.y * this->screen->pitch ); | ||
1597 | |||
1598 | this->glFlush(); | ||
1599 | /* | ||
1600 | * Note the parens around the function name: | ||
1601 | * This is because some OpenGL implementations define glTexCoord etc | ||
1602 | * as macros, and we don't want them expanded here. | ||
1603 | */ | ||
1604 | this->glBegin(GL_TRIANGLE_STRIP); | ||
1605 | (this->glTexCoord2f)( 0.0, 0.0 ); | ||
1606 | (this->glVertex2i)( update.x, update.y ); | ||
1607 | (this->glTexCoord2f)( (float)(update.w / 256.0), 0.0 ); | ||
1608 | (this->glVertex2i)( update.x + update.w, update.y ); | ||
1609 | (this->glTexCoord2f)( 0.0, (float)(update.h / 256.0) ); | ||
1610 | (this->glVertex2i)( update.x, update.y + update.h ); | ||
1611 | (this->glTexCoord2f)( (float)(update.w / 256.0), (float)(update.h / 256.0) ); | ||
1612 | (this->glVertex2i)( update.x + update.w , update.y + update.h ); | ||
1613 | this->glEnd(); | ||
1614 | |||
1615 | tmp.x += 256; | ||
1616 | tmp.w -= 256; | ||
1617 | } | ||
1618 | tmp.y += 256; | ||
1619 | tmp.h -= 256; | ||
1620 | } | ||
1621 | } | ||
1622 | #endif | ||
1623 | } | ||
1624 | |||
1625 | /* Lock == save current state */ | ||
1626 | void SDL_GL_Lock() | ||
1627 | { | ||
1628 | #if SDL_VIDEO_OPENGL | ||
1629 | lock_count--; | ||
1630 | if (lock_count==-1) | ||
1631 | { | ||
1632 | SDL_VideoDevice *this = current_video; | ||
1633 | |||
1634 | this->glPushAttrib( GL_ALL_ATTRIB_BITS ); /* TODO: narrow range of what is saved */ | ||
1635 | #ifdef GL_CLIENT_PIXEL_STORE_BIT | ||
1636 | this->glPushClientAttrib( GL_CLIENT_PIXEL_STORE_BIT ); | ||
1637 | #endif | ||
1638 | |||
1639 | this->glEnable(GL_TEXTURE_2D); | ||
1640 | this->glEnable(GL_BLEND); | ||
1641 | this->glDisable(GL_FOG); | ||
1642 | this->glDisable(GL_ALPHA_TEST); | ||
1643 | this->glDisable(GL_DEPTH_TEST); | ||
1644 | this->glDisable(GL_SCISSOR_TEST); | ||
1645 | this->glDisable(GL_STENCIL_TEST); | ||
1646 | this->glDisable(GL_CULL_FACE); | ||
1647 | |||
1648 | this->glBindTexture( GL_TEXTURE_2D, this->texture ); | ||
1649 | this->glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE ); | ||
1650 | this->glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST ); | ||
1651 | this->glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST ); | ||
1652 | this->glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT ); | ||
1653 | this->glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT ); | ||
1654 | |||
1655 | this->glPixelStorei( GL_UNPACK_ROW_LENGTH, this->screen->pitch / this->screen->format->BytesPerPixel ); | ||
1656 | this->glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); | ||
1657 | (this->glColor4f)(1.0, 1.0, 1.0, 1.0); /* Solaris workaround */ | ||
1658 | |||
1659 | this->glViewport(0, 0, this->screen->w, this->screen->h); | ||
1660 | this->glMatrixMode(GL_PROJECTION); | ||
1661 | this->glPushMatrix(); | ||
1662 | this->glLoadIdentity(); | ||
1663 | |||
1664 | this->glOrtho(0.0, (GLdouble) this->screen->w, (GLdouble) this->screen->h, 0.0, 0.0, 1.0); | ||
1665 | |||
1666 | this->glMatrixMode(GL_MODELVIEW); | ||
1667 | this->glPushMatrix(); | ||
1668 | this->glLoadIdentity(); | ||
1669 | } | ||
1670 | #endif | ||
1671 | } | ||
1672 | |||
1673 | /* Unlock == restore saved state */ | ||
1674 | void SDL_GL_Unlock() | ||
1675 | { | ||
1676 | #if SDL_VIDEO_OPENGL | ||
1677 | lock_count++; | ||
1678 | if (lock_count==0) | ||
1679 | { | ||
1680 | SDL_VideoDevice *this = current_video; | ||
1681 | |||
1682 | this->glPopMatrix(); | ||
1683 | this->glMatrixMode(GL_PROJECTION); | ||
1684 | this->glPopMatrix(); | ||
1685 | |||
1686 | this->glPopClientAttrib(); | ||
1687 | this->glPopAttrib(); | ||
1688 | } | ||
1689 | #endif | ||
1690 | } | ||
1691 | |||
1692 | |||
1693 | void SDL_Audio_SetCaption(const char *caption); | ||
1694 | |||
1695 | /* | ||
1696 | * Sets/Gets the title and icon text of the display window, if any. | ||
1697 | */ | ||
1698 | void SDL_WM_SetCaption (const char *title, const char *icon) | ||
1699 | { | ||
1700 | SDL_VideoDevice *video = current_video; | ||
1701 | SDL_VideoDevice *this = current_video; | ||
1702 | |||
1703 | if ( video ) { | ||
1704 | if ( title ) { | ||
1705 | if ( video->wm_title ) { | ||
1706 | SDL_free(video->wm_title); | ||
1707 | } | ||
1708 | video->wm_title = SDL_strdup(title); | ||
1709 | } | ||
1710 | if ( icon ) { | ||
1711 | if ( video->wm_icon ) { | ||
1712 | SDL_free(video->wm_icon); | ||
1713 | } | ||
1714 | video->wm_icon = SDL_strdup(icon); | ||
1715 | } | ||
1716 | if ( (title || icon) && (video->SetCaption != NULL) ) { | ||
1717 | video->SetCaption(this, video->wm_title,video->wm_icon); | ||
1718 | } | ||
1719 | } | ||
1720 | |||
1721 | /* PulseAudio can make use of this information. */ | ||
1722 | SDL_Audio_SetCaption(title); | ||
1723 | } | ||
1724 | |||
1725 | void SDL_WM_GetCaption (char **title, char **icon) | ||
1726 | { | ||
1727 | SDL_VideoDevice *video = current_video; | ||
1728 | |||
1729 | if ( video ) { | ||
1730 | if ( title ) { | ||
1731 | *title = video->wm_title; | ||
1732 | } | ||
1733 | if ( icon ) { | ||
1734 | *icon = video->wm_icon; | ||
1735 | } | ||
1736 | } | ||
1737 | } | ||
1738 | |||
1739 | /* Utility function used by SDL_WM_SetIcon(); | ||
1740 | * flags & 1 for color key, flags & 2 for alpha channel. */ | ||
1741 | static void CreateMaskFromColorKeyOrAlpha(SDL_Surface *icon, Uint8 *mask, int flags) | ||
1742 | { | ||
1743 | int x, y; | ||
1744 | Uint32 colorkey; | ||
1745 | #define SET_MASKBIT(icon, x, y, mask) \ | ||
1746 | mask[(y*((icon->w+7)/8))+(x/8)] &= ~(0x01<<(7-(x%8))) | ||
1747 | |||
1748 | colorkey = icon->format->colorkey; | ||
1749 | switch (icon->format->BytesPerPixel) { | ||
1750 | case 1: { Uint8 *pixels; | ||
1751 | for ( y=0; y<icon->h; ++y ) { | ||
1752 | pixels = (Uint8 *)icon->pixels + y*icon->pitch; | ||
1753 | for ( x=0; x<icon->w; ++x ) { | ||
1754 | if ( *pixels++ == colorkey ) { | ||
1755 | SET_MASKBIT(icon, x, y, mask); | ||
1756 | } | ||
1757 | } | ||
1758 | } | ||
1759 | } | ||
1760 | break; | ||
1761 | |||
1762 | case 2: { Uint16 *pixels; | ||
1763 | for ( y=0; y<icon->h; ++y ) { | ||
1764 | pixels = (Uint16 *)icon->pixels + | ||
1765 | y*icon->pitch/2; | ||
1766 | for ( x=0; x<icon->w; ++x ) { | ||
1767 | if ( (flags & 1) && *pixels == colorkey ) { | ||
1768 | SET_MASKBIT(icon, x, y, mask); | ||
1769 | } else if((flags & 2) && (*pixels & icon->format->Amask) == 0) { | ||
1770 | SET_MASKBIT(icon, x, y, mask); | ||
1771 | } | ||
1772 | pixels++; | ||
1773 | } | ||
1774 | } | ||
1775 | } | ||
1776 | break; | ||
1777 | |||
1778 | case 4: { Uint32 *pixels; | ||
1779 | for ( y=0; y<icon->h; ++y ) { | ||
1780 | pixels = (Uint32 *)icon->pixels + | ||
1781 | y*icon->pitch/4; | ||
1782 | for ( x=0; x<icon->w; ++x ) { | ||
1783 | if ( (flags & 1) && *pixels == colorkey ) { | ||
1784 | SET_MASKBIT(icon, x, y, mask); | ||
1785 | } else if((flags & 2) && (*pixels & icon->format->Amask) == 0) { | ||
1786 | SET_MASKBIT(icon, x, y, mask); | ||
1787 | } | ||
1788 | pixels++; | ||
1789 | } | ||
1790 | } | ||
1791 | } | ||
1792 | break; | ||
1793 | } | ||
1794 | } | ||
1795 | |||
1796 | /* | ||
1797 | * Sets the window manager icon for the display window. | ||
1798 | */ | ||
1799 | void SDL_WM_SetIcon (SDL_Surface *icon, Uint8 *mask) | ||
1800 | { | ||
1801 | SDL_VideoDevice *video = current_video; | ||
1802 | SDL_VideoDevice *this = current_video; | ||
1803 | |||
1804 | if ( icon && video->SetIcon ) { | ||
1805 | /* Generate a mask if necessary, and create the icon! */ | ||
1806 | if ( mask == NULL ) { | ||
1807 | int mask_len = icon->h*(icon->w+7)/8; | ||
1808 | int flags = 0; | ||
1809 | mask = (Uint8 *)SDL_malloc(mask_len); | ||
1810 | if ( mask == NULL ) { | ||
1811 | return; | ||
1812 | } | ||
1813 | SDL_memset(mask, ~0, mask_len); | ||
1814 | if ( icon->flags & SDL_SRCCOLORKEY ) flags |= 1; | ||
1815 | if ( icon->flags & SDL_SRCALPHA ) flags |= 2; | ||
1816 | if( flags ) { | ||
1817 | CreateMaskFromColorKeyOrAlpha(icon, mask, flags); | ||
1818 | } | ||
1819 | video->SetIcon(video, icon, mask); | ||
1820 | SDL_free(mask); | ||
1821 | } else { | ||
1822 | video->SetIcon(this, icon, mask); | ||
1823 | } | ||
1824 | } | ||
1825 | } | ||
1826 | |||
1827 | /* | ||
1828 | * Grab or ungrab the keyboard and mouse input. | ||
1829 | * This function returns the final grab mode after calling the | ||
1830 | * driver dependent function. | ||
1831 | */ | ||
1832 | static SDL_GrabMode SDL_WM_GrabInputRaw(SDL_GrabMode mode) | ||
1833 | { | ||
1834 | SDL_VideoDevice *video = current_video; | ||
1835 | SDL_VideoDevice *this = current_video; | ||
1836 | |||
1837 | /* Only do something if we have support for grabs */ | ||
1838 | if ( video->GrabInput == NULL ) { | ||
1839 | return(video->input_grab); | ||
1840 | } | ||
1841 | |||
1842 | /* If the final grab mode if off, only then do we actually grab */ | ||
1843 | #ifdef DEBUG_GRAB | ||
1844 | printf("SDL_WM_GrabInputRaw(%d) ... ", mode); | ||
1845 | #endif | ||
1846 | if ( mode == SDL_GRAB_OFF ) { | ||
1847 | if ( video->input_grab != SDL_GRAB_OFF ) { | ||
1848 | mode = video->GrabInput(this, mode); | ||
1849 | } | ||
1850 | } else { | ||
1851 | if ( video->input_grab == SDL_GRAB_OFF ) { | ||
1852 | mode = video->GrabInput(this, mode); | ||
1853 | } | ||
1854 | } | ||
1855 | if ( mode != video->input_grab ) { | ||
1856 | video->input_grab = mode; | ||
1857 | if ( video->CheckMouseMode ) { | ||
1858 | video->CheckMouseMode(this); | ||
1859 | } | ||
1860 | } | ||
1861 | #ifdef DEBUG_GRAB | ||
1862 | printf("Final mode %d\n", video->input_grab); | ||
1863 | #endif | ||
1864 | |||
1865 | /* Return the final grab state */ | ||
1866 | if ( mode >= SDL_GRAB_FULLSCREEN ) { | ||
1867 | mode -= SDL_GRAB_FULLSCREEN; | ||
1868 | } | ||
1869 | return(mode); | ||
1870 | } | ||
1871 | SDL_GrabMode SDL_WM_GrabInput(SDL_GrabMode mode) | ||
1872 | { | ||
1873 | SDL_VideoDevice *video = current_video; | ||
1874 | |||
1875 | /* If the video isn't initialized yet, we can't do anything */ | ||
1876 | if ( ! video ) { | ||
1877 | return SDL_GRAB_OFF; | ||
1878 | } | ||
1879 | |||
1880 | /* Return the current mode on query */ | ||
1881 | if ( mode == SDL_GRAB_QUERY ) { | ||
1882 | mode = video->input_grab; | ||
1883 | if ( mode >= SDL_GRAB_FULLSCREEN ) { | ||
1884 | mode -= SDL_GRAB_FULLSCREEN; | ||
1885 | } | ||
1886 | return(mode); | ||
1887 | } | ||
1888 | |||
1889 | #ifdef DEBUG_GRAB | ||
1890 | printf("SDL_WM_GrabInput(%d) ... ", mode); | ||
1891 | #endif | ||
1892 | /* If the video surface is fullscreen, we always grab */ | ||
1893 | if ( mode >= SDL_GRAB_FULLSCREEN ) { | ||
1894 | mode -= SDL_GRAB_FULLSCREEN; | ||
1895 | } | ||
1896 | if ( SDL_VideoSurface && (SDL_VideoSurface->flags & SDL_FULLSCREEN) ) { | ||
1897 | mode += SDL_GRAB_FULLSCREEN; | ||
1898 | } | ||
1899 | return(SDL_WM_GrabInputRaw(mode)); | ||
1900 | } | ||
1901 | static SDL_GrabMode SDL_WM_GrabInputOff(void) | ||
1902 | { | ||
1903 | SDL_GrabMode mode; | ||
1904 | |||
1905 | /* First query the current grab state */ | ||
1906 | mode = SDL_WM_GrabInput(SDL_GRAB_QUERY); | ||
1907 | |||
1908 | /* Now explicitly turn off input grab */ | ||
1909 | SDL_WM_GrabInputRaw(SDL_GRAB_OFF); | ||
1910 | |||
1911 | /* Return the old state */ | ||
1912 | return(mode); | ||
1913 | } | ||
1914 | |||
1915 | /* | ||
1916 | * Iconify the window in window managed environments. | ||
1917 | * A successful iconification will result in an SDL_APPACTIVE loss event. | ||
1918 | */ | ||
1919 | int SDL_WM_IconifyWindow(void) | ||
1920 | { | ||
1921 | SDL_VideoDevice *video = current_video; | ||
1922 | SDL_VideoDevice *this = current_video; | ||
1923 | int retval; | ||
1924 | |||
1925 | retval = 0; | ||
1926 | if ( video->IconifyWindow ) { | ||
1927 | retval = video->IconifyWindow(this); | ||
1928 | } | ||
1929 | return(retval); | ||
1930 | } | ||
1931 | |||
1932 | /* | ||
1933 | * Toggle fullscreen mode | ||
1934 | */ | ||
1935 | int SDL_WM_ToggleFullScreen(SDL_Surface *surface) | ||
1936 | { | ||
1937 | SDL_VideoDevice *video = current_video; | ||
1938 | SDL_VideoDevice *this = current_video; | ||
1939 | int toggled; | ||
1940 | |||
1941 | toggled = 0; | ||
1942 | if ( SDL_PublicSurface && (surface == SDL_PublicSurface) && | ||
1943 | video->ToggleFullScreen ) { | ||
1944 | if ( surface->flags & SDL_FULLSCREEN ) { | ||
1945 | toggled = video->ToggleFullScreen(this, 0); | ||
1946 | if ( toggled ) { | ||
1947 | SDL_VideoSurface->flags &= ~SDL_FULLSCREEN; | ||
1948 | SDL_PublicSurface->flags &= ~SDL_FULLSCREEN; | ||
1949 | } | ||
1950 | } else { | ||
1951 | toggled = video->ToggleFullScreen(this, 1); | ||
1952 | if ( toggled ) { | ||
1953 | SDL_VideoSurface->flags |= SDL_FULLSCREEN; | ||
1954 | SDL_PublicSurface->flags |= SDL_FULLSCREEN; | ||
1955 | } | ||
1956 | } | ||
1957 | /* Double-check the grab state inside SDL_WM_GrabInput() */ | ||
1958 | if ( toggled ) { | ||
1959 | SDL_WM_GrabInput(video->input_grab); | ||
1960 | } | ||
1961 | } | ||
1962 | return(toggled); | ||
1963 | } | ||
1964 | |||
1965 | /* | ||
1966 | * Get some platform dependent window manager information | ||
1967 | */ | ||
1968 | int SDL_GetWMInfo (SDL_SysWMinfo *info) | ||
1969 | { | ||
1970 | SDL_VideoDevice *video = current_video; | ||
1971 | SDL_VideoDevice *this = current_video; | ||
1972 | |||
1973 | if ( video && video->GetWMInfo ) { | ||
1974 | return(video->GetWMInfo(this, info)); | ||
1975 | } else { | ||
1976 | return(0); | ||
1977 | } | ||
1978 | } | ||