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