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