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

Latest commit

 

History

History
2479 lines (2175 loc) · 69.4 KB

SDL_glsdl.c

File metadata and controls

2479 lines (2175 loc) · 69.4 KB
 
May 1, 2006
May 1, 2006
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/*
SDL - Simple DirectMedia Layer
Copyright (C) 1997-2006 Sam Lantinga
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Sam Lantinga
slouken@libsdl.org
*/
#include "SDL_config.h"
/*
* glSDL "SDL-over-OpenGL" video driver implemented by
* David Olofson <david@olofson.net> and
* Stephane Marchesin <stephane.marchesin@wanadoo.fr>
*/
#include <math.h>
#include "SDL.h"
#include "SDL_error.h"
#include "SDL_video.h"
#include "SDL_mouse.h"
#include "../SDL_sysvideo.h"
#include "../SDL_pixels_c.h"
#include "SDL_glsdl.h"
#undef DEBUG_GLSDL
#undef DEBUG_GLSDL_CHOP
#define FAKE_MAXTEXSIZE 256
#undef GLSDL_GRAPHICAL_DEBUG
/* Initialization/Query functions */
/* Hardware surface functions */
May 28, 2006
May 28, 2006
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
static int glSDL_SetColors (_THIS, int firstcolor, int ncolors,
SDL_Color * colors);
static int glSDL_AllocHWSurface (_THIS, SDL_Surface * surface);
static int glSDL_LockHWSurface (_THIS, SDL_Surface * surface);
static int glSDL_FlipHWSurface (_THIS, SDL_Surface * surface);
static void glSDL_UnlockHWSurface (_THIS, SDL_Surface * surface);
static void glSDL_FreeHWSurface (_THIS, SDL_Surface * surface);
static int glSDL_FillHWRect (_THIS, SDL_Surface * dst, SDL_Rect * rect,
Uint32 color);
static int glSDL_CheckHWBlit (_THIS, SDL_Surface * src, SDL_Surface * dst);
static int glSDL_SetHWColorKey (_THIS, SDL_Surface * surface, Uint32 key);
static int glSDL_SetHWAlpha (_THIS, SDL_Surface * surface, Uint8 alpha);
static int glSDL_VideoInit (_THIS, SDL_PixelFormat * vformat);
static SDL_Rect **glSDL_ListModes (_THIS, SDL_PixelFormat * format,
Uint32 flags);
static void glSDL_VideoQuit (_THIS);
static void glSDL_UpdateRects (_THIS, int numrects, SDL_Rect * rects);
static SDL_Surface *glSDL_SetVideoMode (_THIS, SDL_Surface * current,
int width, int height, int bpp,
Uint32 flags);
May 1, 2006
May 1, 2006
68
69
70
71
72
73
74
75
76
77
78
79
80
#define IS_GLSDL_SURFACE(s) ((s) && glSDL_GetTexInfo(s))
#define LOGIC_W(s) ( IS_GLSDL_SURFACE(this,s) ? TEXINFO(s)->lw : (s)->w )
#define LOGIC_H(s) ( IS_GLSDL_SURFACE(this,s) ? TEXINFO(s)->lh : (s)->h )
#define GLSDL_NOTEX (~0)
/*
* Special version for glSDL, which ignores the fake SDL_HWSURFACE
* flags, so we don't have SDL calling us back whenever we want to
* do some internal blitting...
*/
May 28, 2006
May 28, 2006
81
82
83
static void
glSDL_SoftBlit (SDL_Surface * src, SDL_Rect * srcrect,
SDL_Surface * dst, SDL_Rect * dstrect)
May 1, 2006
May 1, 2006
84
{
May 28, 2006
May 28, 2006
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
SDL_BlitInfo info;
if (srcrect)
if (!srcrect->w || !srcrect->h)
return;
/* Check to make sure the blit mapping is valid */
if ((src->map->dst != dst) ||
(src->map->dst->format_version != src->map->format_version))
if (SDL_MapSurface (src, dst) < 0)
return;
/* Set up the blit information */
if (srcrect) {
info.s_pixels = (Uint8 *) src->pixels +
(Uint16) srcrect->y * src->pitch +
(Uint16) srcrect->x * src->format->BytesPerPixel;
info.s_width = srcrect->w;
info.s_height = srcrect->h;
} else {
info.s_pixels = (Uint8 *) src->pixels;
info.s_width = src->w;
info.s_height = src->h;
}
info.s_skip = src->pitch - info.s_width * src->format->BytesPerPixel;
if (dstrect) {
info.d_pixels = (Uint8 *) dst->pixels +
(Uint16) dstrect->y * dst->pitch +
(Uint16) dstrect->x * dst->format->BytesPerPixel;
/*
* NOTE: SDL_SoftBlit() uses the 'dstrect' for this!
* This version is more like SDL_BlitSurface().
*/
info.d_width = srcrect->w;
info.d_height = srcrect->h;
} else {
info.d_pixels = (Uint8 *) dst->pixels;
info.d_width = dst->w;
info.d_height = dst->h;
}
info.d_skip = dst->pitch - info.d_width * dst->format->BytesPerPixel;
info.aux_data = src->map->sw_data->aux_data;
info.src = src->format;
info.table = src->map->table;
info.dst = dst->format;
src->map->sw_data->blit (&info);
May 1, 2006
May 1, 2006
132
133
134
135
136
137
138
139
}
/*
* Another special version. Doesn't lock/unlock, and doesn't mess
* with flags and stuff. It just converts the surface, period.
* Does not convert into palletized formats.
*/
May 28, 2006
May 28, 2006
140
141
142
static SDL_Surface *
glSDL_ConvertSurface (SDL_Surface * surface,
SDL_PixelFormat * format, Uint32 flags)
May 1, 2006
May 1, 2006
143
{
May 28, 2006
May 28, 2006
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
SDL_Surface *convert;
Uint32 colorkey = 0;
Uint8 alpha = 0;
Uint32 surface_flags;
SDL_Rect bounds;
/* Create a new surface with the desired format */
convert = SDL_CreateRGBSurface (flags,
surface->w, surface->h,
format->BitsPerPixel, format->Rmask,
format->Gmask, format->Bmask,
format->Amask);
if (convert == NULL) {
return (NULL);
}
/* Save the original surface color key and alpha */
surface_flags = surface->flags;
if ((surface_flags & SDL_SRCCOLORKEY) == SDL_SRCCOLORKEY) {
/* Convert colourkeyed surfaces to RGBA if requested */
if ((flags & SDL_SRCCOLORKEY) != SDL_SRCCOLORKEY && format->Amask) {
surface_flags &= ~SDL_SRCCOLORKEY;
} else {
colorkey = surface->format->colorkey;
SDL_SetColorKey (surface, 0, 0);
}
}
if ((surface_flags & SDL_SRCALPHA) == SDL_SRCALPHA) {
/* Copy over the alpha channel to RGBA if requested */
if (format->Amask) {
surface->flags &= ~SDL_SRCALPHA;
} else {
alpha = surface->format->alpha;
SDL_SetAlpha (surface, 0, 0);
}
}
/* Copy over the image data */
bounds.x = 0;
bounds.y = 0;
bounds.w = surface->w;
bounds.h = surface->h;
glSDL_SoftBlit (surface, &bounds, convert, &bounds);
/* Clean up the original surface, and update converted surface */
if (convert != NULL) {
SDL_SetClipRect (convert, &surface->clip_rect);
}
if ((surface_flags & SDL_SRCCOLORKEY) == SDL_SRCCOLORKEY) {
Uint32 cflags = surface_flags & (SDL_SRCCOLORKEY | SDL_RLEACCELOK);
if (convert != NULL) {
Uint8 keyR, keyG, keyB;
SDL_GetRGB (colorkey, surface->format, &keyR, &keyG, &keyB);
SDL_SetColorKey (convert, cflags | (flags & SDL_RLEACCELOK),
SDL_MapRGB (convert->format, keyR, keyG, keyB));
}
SDL_SetColorKey (surface, cflags, colorkey);
}
if ((surface_flags & SDL_SRCALPHA) == SDL_SRCALPHA) {
Uint32 aflags = surface_flags & (SDL_SRCALPHA | SDL_RLEACCELOK);
if (convert != NULL) {
SDL_SetAlpha (convert, aflags | (flags & SDL_RLEACCELOK), alpha);
}
if (format->Amask) {
surface->flags |= SDL_SRCALPHA;
} else {
SDL_SetAlpha (surface, aflags, alpha);
}
}
/* We're ready to go! */
return (convert);
May 1, 2006
May 1, 2006
217
218
219
220
221
222
223
224
225
}
/*----------------------------------------------------------
Some OpenGL function wrappers
----------------------------------------------------------*/
static struct
{
May 28, 2006
May 28, 2006
226
227
228
229
int do_blend;
int do_texture;
GLuint texture;
GLenum sfactor, dfactor;
May 1, 2006
May 1, 2006
230
231
} glstate;
May 28, 2006
May 28, 2006
232
233
static void
glSDL_reset (void)
May 1, 2006
May 1, 2006
234
{
May 28, 2006
May 28, 2006
235
236
237
238
239
glstate.do_blend = -1;
glstate.do_blend = -1;
glstate.texture = GLSDL_NOTEX;
glstate.sfactor = 0xffffffff;
glstate.dfactor = 0xffffffff;
May 1, 2006
May 1, 2006
240
241
}
May 28, 2006
May 28, 2006
242
243
static __inline__ void
glSDL_do_blend (_THIS, int on)
May 1, 2006
May 1, 2006
244
{
May 28, 2006
May 28, 2006
245
246
247
248
249
250
251
252
if (glstate.do_blend == on)
return;
if (on)
this->glEnable (GL_BLEND);
else
this->glDisable (GL_BLEND);
glstate.do_blend = on;
May 1, 2006
May 1, 2006
253
254
}
May 28, 2006
May 28, 2006
255
256
static __inline__ void
glSDL_do_texture (_THIS, int on)
May 1, 2006
May 1, 2006
257
{
May 28, 2006
May 28, 2006
258
259
260
261
262
263
264
265
if (glstate.do_texture == on)
return;
if (on)
this->glEnable (GL_TEXTURE_2D);
else
this->glDisable (GL_TEXTURE_2D);
glstate.do_texture = on;
May 1, 2006
May 1, 2006
266
267
}
May 28, 2006
May 28, 2006
268
269
static __inline__ void
glSDL_blendfunc (_THIS, GLenum sfactor, GLenum dfactor)
May 1, 2006
May 1, 2006
270
{
May 28, 2006
May 28, 2006
271
272
if ((sfactor == glstate.sfactor) && (dfactor == glstate.dfactor))
return;
May 1, 2006
May 1, 2006
273
May 28, 2006
May 28, 2006
274
this->glBlendFunc (sfactor, dfactor);
May 1, 2006
May 1, 2006
275
May 28, 2006
May 28, 2006
276
277
glstate.sfactor = sfactor;
glstate.dfactor = dfactor;
May 1, 2006
May 1, 2006
278
279
}
May 28, 2006
May 28, 2006
280
281
static __inline__ void
glSDL_texture (_THIS, GLuint tx)
May 1, 2006
May 1, 2006
282
{
May 28, 2006
May 28, 2006
283
284
if (tx == glstate.texture)
return;
May 1, 2006
May 1, 2006
285
May 28, 2006
May 28, 2006
286
287
this->glBindTexture (GL_TEXTURE_2D, tx);
glstate.texture = tx;
May 1, 2006
May 1, 2006
288
289
290
291
292
293
294
295
296
297
298
}
/*----------------------------------------------------------
glSDL specific data types
----------------------------------------------------------*/
typedef enum
{
May 28, 2006
May 28, 2006
299
300
301
302
GLSDL_TM_SINGLE,
GLSDL_TM_HORIZONTAL,
GLSDL_TM_VERTICAL,
GLSDL_TM_HUGE
May 1, 2006
May 1, 2006
303
304
305
306
307
} GLSDL_TileModes;
typedef struct private_hwdata
{
May 28, 2006
May 28, 2006
308
309
/* Size of surface in logic screen pixels */
int lw, lh;
May 1, 2006
May 1, 2006
310
May 28, 2006
May 28, 2006
311
312
313
314
315
316
317
int textures;
GLuint *texture;
int texsize; /* width/height of OpenGL texture */
GLSDL_TileModes tilemode;
int tilew, tileh; /* At least one must equal texsize! */
int tilespertex;
SDL_Rect virt; /* Total size of assembled surface */
May 1, 2006
May 1, 2006
318
May 28, 2006
May 28, 2006
319
320
/* Area of surface to upload when/after unlocking */
SDL_Rect invalid_area;
May 1, 2006
May 1, 2006
321
May 28, 2006
May 28, 2006
322
int temporary; /* Throw away after one use. */
May 1, 2006
May 1, 2006
323
May 28, 2006
May 28, 2006
324
325
SDL_Surface *next; /* The next Surface in our linked list of hardware surfaces ; == NULL if first surface */
SDL_Surface *prev; /* The prev Surface in our linked list of hardware surfaces ; == NULL if last surface */
May 1, 2006
May 1, 2006
326
327
328
} private_hwdata;
/* some function prototypes */
May 28, 2006
May 28, 2006
329
330
331
332
333
334
335
336
337
338
static void glSDL_Invalidate (SDL_Surface * surface, SDL_Rect * area);
static void glSDL_SetLogicSize (_THIS, SDL_Surface * surface, int w, int h);
static private_hwdata *glSDL_UploadSurface (_THIS, SDL_Surface * surface);
static private_hwdata *glSDL_GetTexInfo (SDL_Surface * surface);
static void glSDL_init_formats (_THIS);
static private_hwdata *glSDL_AddTexInfo (_THIS, SDL_Surface * surface);
static void glSDL_RemoveTexInfo (_THIS, SDL_Surface * surface);
static void glSDL_UnloadTexture (_THIS, private_hwdata * txi);
static int glSDL_BlitGL (_THIS, SDL_Surface * src,
SDL_Rect * srcrect, SDL_Rect * dstrect);
May 1, 2006
May 1, 2006
339
340
341
342
343
344
345
/* some variables */
static GLint maxtexsize = -1;
static SDL_PixelFormat *RGBfmt = NULL;
static SDL_PixelFormat *RGBAfmt = NULL;
static void *mirrorbuf = NULL;
/* the raw 888 opengl surface, hidden from the application */
May 28, 2006
May 28, 2006
346
SDL_Surface *OpenGL_Surface;
May 1, 2006
May 1, 2006
347
348
/* pointer to the beggining of the list used for memory allocation */
May 28, 2006
May 28, 2006
349
SDL_Surface *first = NULL;
May 1, 2006
May 1, 2006
350
351
#ifdef DEBUG_GLSDL
May 28, 2006
May 28, 2006
352
353
static __inline__ int
GLERET (const char *txt)
May 1, 2006
May 1, 2006
354
{
May 28, 2006
May 28, 2006
355
356
fprintf (stderr, "glSDL ERROR: '%s'\n", txt);
return -1;
May 1, 2006
May 1, 2006
357
}
May 28, 2006
May 28, 2006
358
359
static __inline__ void
GLERR (const char *txt)
May 1, 2006
May 1, 2006
360
{
May 28, 2006
May 28, 2006
361
fprintf (stderr, "glSDL ERROR: '%s'\n", txt);
May 1, 2006
May 1, 2006
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
}
#else
#define GLERET(x) (-1)
#define GLERR(x)
#endif
static SDL_VideoDevice underlying_device;
static int old_screen_flags;
/*
* List of video drivers known to support OpenGL
* The purpose of this is to make glSDL "portable" across
* all video backends that support OpenGL
*/
static VideoBootStrap *opengl_bootstrap =
#if SDL_VIDEO_DRIVER_QUARTZ
May 28, 2006
May 28, 2006
378
&QZ_bootstrap;
May 1, 2006
May 1, 2006
379
#elif SDL_VIDEO_DRIVER_X11
May 28, 2006
May 28, 2006
380
&X11_bootstrap;
May 1, 2006
May 1, 2006
381
#elif SDL_VIDEO_DRIVER_WINDIB
May 28, 2006
May 28, 2006
382
&WINDIB_bootstrap;
May 1, 2006
May 1, 2006
383
#elif SDL_VIDEO_DRIVER_BWINDOW
May 28, 2006
May 28, 2006
384
&BWINDOW_bootstrap;
May 1, 2006
May 1, 2006
385
#elif SDL_VIDEO_DRIVER_TOOLBOX
May 28, 2006
May 28, 2006
386
&TOOLBOX_bootstrap;
May 1, 2006
May 1, 2006
387
#elif SDL_VIDEO_DRIVER_CYBERGRAPHICS
May 28, 2006
May 28, 2006
388
&CGX_bootstrap;
May 1, 2006
May 1, 2006
389
#elif SDL_VIDEO_DRIVER_PHOTON
May 28, 2006
May 28, 2006
390
&ph_bootstrap;
May 1, 2006
May 1, 2006
391
#elif SDL_VIDEO_DRIVER_DC
May 28, 2006
May 28, 2006
392
&DC_bootstrap;
May 1, 2006
May 1, 2006
393
#else
May 28, 2006
May 28, 2006
394
NULL;
May 1, 2006
May 1, 2006
395
396
#endif
May 28, 2006
May 28, 2006
397
398
static int
glSDL_Available (void)
May 1, 2006
May 1, 2006
399
400
{
#ifdef DEBUG_GLSDL
May 28, 2006
May 28, 2006
401
fprintf (stderr, "available\n");
May 1, 2006
May 1, 2006
402
#endif
May 28, 2006
May 28, 2006
403
404
405
if (opengl_bootstrap == NULL)
return 0;
return (opengl_bootstrap->available ());
May 1, 2006
May 1, 2006
406
407
}
May 28, 2006
May 28, 2006
408
409
static void
glSDL_DeleteDevice (SDL_VideoDevice * device)
May 1, 2006
May 1, 2006
410
{
May 28, 2006
May 28, 2006
411
412
SDL_free (device->hidden);
SDL_free (device);
May 1, 2006
May 1, 2006
413
414
415
}
/* Create a glSDL device */
May 28, 2006
May 28, 2006
416
417
static SDL_VideoDevice *
glSDL_CreateDevice (int devindex)
May 1, 2006
May 1, 2006
418
{
May 28, 2006
May 28, 2006
419
SDL_VideoDevice *device;
May 1, 2006
May 1, 2006
420
#ifdef DEBUG_GLSDL
May 28, 2006
May 28, 2006
421
fprintf (stderr, "entering createdevice\n");
May 1, 2006
May 1, 2006
422
423
#endif
May 28, 2006
May 28, 2006
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
/* Create the device with the underlying driver */
device = opengl_bootstrap->create (devindex);
/* Save the video device contents for future use */
SDL_memcpy (&underlying_device, device, sizeof (SDL_VideoDevice));
/* Hook glSDL on the video device */
device->VideoInit = glSDL_VideoInit;
device->ListModes = glSDL_ListModes;
device->VideoQuit = glSDL_VideoQuit;
device->UpdateRects = glSDL_UpdateRects;
device->FillHWRect = glSDL_FillHWRect;
device->SetHWColorKey = glSDL_SetHWColorKey;
device->SetHWAlpha = glSDL_SetHWAlpha;
device->AllocHWSurface = glSDL_AllocHWSurface;
device->LockHWSurface = glSDL_LockHWSurface;
device->UnlockHWSurface = glSDL_UnlockHWSurface;
device->FlipHWSurface = glSDL_FlipHWSurface;
device->FreeHWSurface = glSDL_FreeHWSurface;
device->CheckHWBlit = glSDL_CheckHWBlit;
device->SetColors = glSDL_SetColors;
device->SetVideoMode = glSDL_SetVideoMode;
device->info.hw_available = 1;
device->info.blit_hw = 1;
device->info.blit_hw_CC = 1;
device->info.blit_hw_A = 1;
device->info.blit_sw = 1;
device->info.blit_sw_CC = 1;
device->info.blit_sw_A = 1;
device->info.blit_fill = 1;
/* These functions are not supported by glSDL, so we NULLify them */
device->SetGamma = NULL;
device->GetGamma = NULL;
device->SetGammaRamp = NULL;
device->GetGammaRamp = NULL;
device->ToggleFullScreen = NULL;
device->free = glSDL_DeleteDevice;
May 1, 2006
May 1, 2006
463
464
#ifdef DEBUG_GLSDL
May 28, 2006
May 28, 2006
465
fprintf (stderr, "leaving createdevice\n");
May 1, 2006
May 1, 2006
466
467
#endif
May 28, 2006
May 28, 2006
468
return device;
May 1, 2006
May 1, 2006
469
470
471
472
}
/* Our bootstraping structure */
VideoBootStrap glSDL_bootstrap = {
May 28, 2006
May 28, 2006
473
474
"glSDL", "glSDL - SDL over OpenGL",
glSDL_Available, glSDL_CreateDevice
May 1, 2006
May 1, 2006
475
476
};
May 28, 2006
May 28, 2006
477
478
static int
glSDL_VideoInit (_THIS, SDL_PixelFormat * vformat)
May 1, 2006
May 1, 2006
479
{
May 28, 2006
May 28, 2006
480
481
int r;
printf ("glSDL videoinit\n");
May 1, 2006
May 1, 2006
482
#ifdef DEBUG_GLSDL
May 28, 2006
May 28, 2006
483
fprintf (stderr, "videoinit\n");
May 1, 2006
May 1, 2006
484
#endif
May 28, 2006
May 28, 2006
485
486
487
488
489
490
491
492
493
494
495
r = underlying_device.VideoInit (this, vformat);
this->info.hw_available = 1;
this->info.blit_hw = 1;
this->info.blit_hw_CC = 1;
this->info.blit_hw_A = 1;
this->info.blit_sw = 1;
this->info.blit_sw_CC = 1;
this->info.blit_sw_A = 1;
this->info.blit_fill = 1;
return r;
May 1, 2006
May 1, 2006
496
497
}
May 28, 2006
May 28, 2006
498
499
SDL_Rect **
glSDL_ListModes (_THIS, SDL_PixelFormat * format, Uint32 flags)
May 1, 2006
May 1, 2006
500
{
May 28, 2006
May 28, 2006
501
return ((SDL_Rect **) - 1);
May 1, 2006
May 1, 2006
502
503
}
May 28, 2006
May 28, 2006
504
505
static void
glSDL_VideoQuit (_THIS)
May 1, 2006
May 1, 2006
506
{
May 28, 2006
May 28, 2006
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
SDL_Surface *scr;
/* free all hwdata structures */
while (first != NULL)
glSDL_RemoveTexInfo (this, first);
SDL_free (mirrorbuf);
mirrorbuf = NULL;
SDL_FreeFormat (RGBfmt);
SDL_FreeFormat (RGBAfmt);
RGBfmt = RGBAfmt = NULL;
SDL_FreeFormat (this->displayformatalphapixel);
this->displayformatalphapixel = NULL;
SDL_FreeSurface (OpenGL_Surface);
OpenGL_Surface = NULL;
/* restore the flags to gracefully exit from fullscreen */
this->screen->flags = old_screen_flags;
/* keep the screen */
scr = this->screen;
/* we cleaned up our stuff, now restore the underlying video driver */
SDL_memcpy (this, &underlying_device, sizeof (SDL_VideoDevice));
this->screen = scr;
/* call the underlying video driver's VideoQuit function */
this->VideoQuit (this);
May 1, 2006
May 1, 2006
539
540
}
May 28, 2006
May 28, 2006
541
542
543
static SDL_Surface *
glSDL_SetVideoMode (_THIS, SDL_Surface * current, int width, int height,
int bpp, Uint32 flags)
May 1, 2006
May 1, 2006
544
{
May 28, 2006
May 28, 2006
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
SDL_Surface *hooked_screen;
int i;
int flag_doublebuf = 0;
if (opengl_bootstrap == NULL) {
GLERR ("No bootstrap for glSDL compiled in !\n");
return NULL;
}
/* we don't have OpenGL */
if ((flags & SDL_INTERNALOPENGL) == SDL_INTERNALOPENGL) {
GLERR ("OpenGL video modes are not supported by glSDL !\n");
return (NULL);
}
/*
* Adjust the flags
*/
flags &= ~SDL_HWPALETTE;
flags |= SDL_INTERNALOPENGL;
/* remember whether the user requested DOUBLEBUF */
if (flags & SDL_DOUBLEBUF) {
SDL_GL_SetAttribute (SDL_GL_DOUBLEBUFFER, 1);
flag_doublebuf = 1;
} else {
SDL_GL_SetAttribute (SDL_GL_DOUBLEBUFFER, 0);
flag_doublebuf = 0;
}
hooked_screen =
underlying_device.SetVideoMode (this, current, width, height, 0,
flags);
if (!hooked_screen) {
GLERR ("Unable to open an OpenGL window !\n");
return (NULL);
}
/* save the screen flags for restore time */
old_screen_flags = hooked_screen->flags;
May 1, 2006
May 1, 2006
587
588
#ifdef DEBUG_GLSDL
May 28, 2006
May 28, 2006
589
fprintf (stderr, "got %d bpp\n", bpp);
May 1, 2006
May 1, 2006
590
591
#endif
May 28, 2006
May 28, 2006
592
593
594
595
596
597
598
/* setup the public surface format
* glSDL always returns the bpp its asked
*/
switch (bpp) {
case 32:
this->is_32bit = 1;
this->screen = SDL_CreateRGBSurface (flags, width, height, bpp,
May 1, 2006
May 1, 2006
599
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
May 28, 2006
May 28, 2006
600
601
602
0x00FF0000,
0x0000FF00,
0x000000FF, 0x00000000
May 1, 2006
May 1, 2006
603
#else
May 28, 2006
May 28, 2006
604
605
606
0x0000FF00,
0x00FF0000,
0xFF000000, 0x00000000
May 1, 2006
May 1, 2006
607
#endif
May 28, 2006
May 28, 2006
608
609
610
611
612
);
break;
case 24:
this->is_32bit = 0;
this->screen = SDL_CreateRGBSurface (flags, width, height, bpp,
May 1, 2006
May 1, 2006
613
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
May 28, 2006
May 28, 2006
614
615
616
0x00FF0000,
0x0000FF00,
0x000000FF, 0x00000000
May 1, 2006
May 1, 2006
617
#else
May 28, 2006
May 28, 2006
618
619
620
0x0000FF00,
0x00FF0000,
0xFF000000, 0x00000000
May 1, 2006
May 1, 2006
621
#endif
May 28, 2006
May 28, 2006
622
623
624
625
626
);
break;
case 16:
this->is_32bit = 0;
this->screen = SDL_CreateRGBSurface (flags, width, height, bpp,
May 1, 2006
May 1, 2006
627
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
May 28, 2006
May 28, 2006
628
629
630
0x0000F800,
0x000007E0,
0x0000001F, 0x00000000
May 1, 2006
May 1, 2006
631
#else
May 28, 2006
May 28, 2006
632
633
634
0x0000001F,
0x000007E0,
0x0000F800, 0x00000000
May 1, 2006
May 1, 2006
635
#endif
May 28, 2006
May 28, 2006
636
637
638
639
640
);
break;
case 15:
this->is_32bit = 0;
this->screen = SDL_CreateRGBSurface (flags, width, height, bpp,
May 1, 2006
May 1, 2006
641
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
May 28, 2006
May 28, 2006
642
643
644
0x00007C00,
0x000003E0,
0x0000001F, 0x00000000
May 1, 2006
May 1, 2006
645
#else
May 28, 2006
May 28, 2006
646
647
648
0x0000001F,
0x000003E0,
0x00007C00, 0x00000000
May 1, 2006
May 1, 2006
649
#endif
May 28, 2006
May 28, 2006
650
651
652
653
654
655
656
657
658
659
);
break;
case 8:
default:
this->is_32bit = 0;
this->screen =
SDL_CreateRGBSurface (flags, width, height, bpp, 0, 0, 0, 0);
/* give it a default palette if 8 bpp
* note : SDL already takes care of the palette for 4 bits & 1 bit surfaces
*/
May 1, 2006
May 1, 2006
660
661
662
663
664
/* if (bpp==8)
{
this->screen->format->palette->ncolors=255;
SDL_DitherColors(this->screen->format->palette->colors,bpp);
}*/
May 28, 2006
May 28, 2006
665
666
667
668
669
670
671
672
673
674
675
676
break;
}
/* also, we add SDL_HWSURFACE all the time, and let SDL create a shadow surface accordingly */
this->screen->flags =
hooked_screen->flags | SDL_HWSURFACE | SDL_INTERNALOPENGL;
/* add SDL_DOUBLEBUF if it was requested */
if (flag_doublebuf)
this->screen->flags |= SDL_DOUBLEBUF;
/* Tell SDL the alpha pixel format we'd like to have */
this->displayformatalphapixel = SDL_AllocFormat (32,
May 1, 2006
May 1, 2006
677
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
May 28, 2006
May 28, 2006
678
679
680
0xFF000000,
0x00FF0000,
0x0000FF00, 0x000000FF
May 1, 2006
May 1, 2006
681
#else
May 28, 2006
May 28, 2006
682
683
684
0x000000FF,
0x0000FF00,
0x00FF0000, 0xFF000000
May 1, 2006
May 1, 2006
685
#endif
May 28, 2006
May 28, 2006
686
);
May 1, 2006
May 1, 2006
687
May 28, 2006
May 28, 2006
688
689
/* Now create the raw OpenGL surface */
OpenGL_Surface = SDL_CreateRGBSurface (flags, width, height, 24,
May 1, 2006
May 1, 2006
690
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
May 28, 2006
May 28, 2006
691
692
0x000000FF,
0x0000FF00, 0x00FF0000, 0x00000000
May 1, 2006
May 1, 2006
693
#else
May 28, 2006
May 28, 2006
694
695
0xFF000000,
0x00FF0000, 0x0000FF00, 0x00000000
May 1, 2006
May 1, 2006
696
#endif
May 28, 2006
May 28, 2006
697
);
May 1, 2006
May 1, 2006
698
May 28, 2006
May 28, 2006
699
/* Here we have to setup OpenGL funcs ourselves */
May 1, 2006
May 1, 2006
700
701
702
703
704
705
706
707
708
709
710
711
712
#ifndef __QNXNTO__
#define SDL_PROC(ret,func,params) \
do { \
this->func = SDL_GL_GetProcAddress(#func); \
if ( ! this->func ) { \
SDL_SetError("Couldn't load GL function: %s\n", #func); \
return(NULL); \
} \
} while ( 0 );
#else
#define SDL_PROC(ret,func,params) this->func=func;
#endif /* __QNXNTO__ */
#include "../SDL_glfuncs.h"
May 28, 2006
May 28, 2006
713
#undef SDL_PROC
May 1, 2006
May 1, 2006
714
May 28, 2006
May 28, 2006
715
716
if (this->GL_MakeCurrent (this) < 0)
return (NULL);
May 1, 2006
May 1, 2006
717
718
719
720
721
722
723
724
725
#define SDL_PROC(ret,func,params) \
do { \
this->func = SDL_GL_GetProcAddress(#func); \
if ( ! this->func ) { \
SDL_SetError("Couldn't load GL function: %s\n", #func); \
return(NULL); \
} \
} while ( 0 );
#include "../SDL_glfuncs.h"
May 28, 2006
May 28, 2006
726
#undef SDL_PROC
May 1, 2006
May 1, 2006
727
728
729
#ifdef FAKE_MAXTEXSIZE
May 28, 2006
May 28, 2006
730
maxtexsize = FAKE_MAXTEXSIZE;
May 1, 2006
May 1, 2006
731
#else
May 28, 2006
May 28, 2006
732
this->glGetIntegerv (GL_MAX_TEXTURE_SIZE, &maxtexsize);
May 1, 2006
May 1, 2006
733
734
#endif
#ifdef DEBUG_GLSDL
May 28, 2006
May 28, 2006
735
fprintf (stderr, "glSDL: Max texture size: %d\n", maxtexsize);
May 1, 2006
May 1, 2006
736
737
#endif
May 28, 2006
May 28, 2006
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
glSDL_init_formats (this);
if (flag_doublebuf)
this->glDrawBuffer (GL_BACK);
else
this->glDrawBuffer (GL_FRONT);
this->glDisable (GL_DITHER);
if (glSDL_AddTexInfo (this, this->screen) < 0) {
GLERR ("HookDevice() failed to add info to screen surface!");
return NULL;
}
glSDL_SetLogicSize (this, this->screen, this->screen->w, this->screen->h);
glSDL_do_texture (this, 0);
glSDL_do_blend (this, 0);
for (i = 0; i < 1 + flag_doublebuf; ++i) {
this->glBegin (GL_TRIANGLE_FAN);
this->glColor3ub (0, 0, 0);
this->glVertex2i (0, 0);
this->glVertex2i (this->screen->w, 0);
this->glVertex2i (this->screen->w, this->screen->h);
this->glVertex2i (0, this->screen->h);
this->glEnd ();
if (!i)
this->GL_SwapBuffers (this);
}
mirrorbuf = SDL_malloc (this->screen->h * this->screen->pitch);
if (!mirrorbuf) {
GLERR ("HookDevice() failed to allocate temp buffer for mirroring!");
return NULL;
}
return this->screen;
May 1, 2006
May 1, 2006
776
777
}
May 28, 2006
May 28, 2006
778
779
static int
glSDL_SetColors (_THIS, int firstcolor, int ncolors, SDL_Color * colors)
May 1, 2006
May 1, 2006
780
{
May 28, 2006
May 28, 2006
781
782
/* We don't need to fill this one */
return 0;
May 1, 2006
May 1, 2006
783
784
785
786
}
#ifdef DEBUG_GLSDL
May 28, 2006
May 28, 2006
787
788
static void
glSDL_print_glerror (_THIS, int point)
May 1, 2006
May 1, 2006
789
{
May 28, 2006
May 28, 2006
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
const char *err = "<unknown>";
switch (this->glGetError ()) {
case GL_NO_ERROR:
return;
case GL_INVALID_ENUM:
err = "GL_INVALID_ENUM";
break;
case GL_INVALID_VALUE:
err = "GL_INVALID_VALUE";
break;
case GL_INVALID_OPERATION:
err = "GL_INVALID_OPERATION";
break;
case GL_STACK_OVERFLOW:
err = "GL_STACK_OVERFLOW";
break;
case GL_STACK_UNDERFLOW:
err = "GL_STACK_UNDERFLOW";
break;
case GL_OUT_OF_MEMORY:
err = "GL_OUT_OF_MEMORY";
default:
break;
}
fprintf (stderr, "OpenGL error \"%s\" at point %d.\n", err, point);
May 1, 2006
May 1, 2006
815
816
817
818
}
#endif
/* Get texinfo for a surface. */
May 28, 2006
May 28, 2006
819
820
static __inline__ private_hwdata *
glSDL_GetTexInfo (SDL_Surface * surface)
May 1, 2006
May 1, 2006
821
{
May 28, 2006
May 28, 2006
822
823
824
if (!surface)
return NULL;
return surface->hwdata;
May 1, 2006
May 1, 2006
825
826
827
828
}
/* Allocate a "blank" texinfo for a suface. */
May 28, 2006
May 28, 2006
829
830
static private_hwdata *
glSDL_AllocTexInfo (SDL_Surface * surface)
May 1, 2006
May 1, 2006
831
{
May 28, 2006
May 28, 2006
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
private_hwdata *txi;
if (!surface)
return NULL;
txi = glSDL_GetTexInfo (surface);
if (txi)
return txi; /* There already is one! --> */
/* ...and hook a new texinfo struct up to it. */
txi = (private_hwdata *) SDL_calloc (1, sizeof (private_hwdata));
if (!txi) {
GLERR ("AllocTexInfo(): Failed allocating TexInfo struct!");
return NULL;
}
txi->temporary = 1;
#ifdef DEBUG_GLSDL
fprintf (stderr, "glSDL: Allocated TexInfo %p.\n", txi);
May 1, 2006
May 1, 2006
849
#endif
May 28, 2006
May 28, 2006
850
return txi;
May 1, 2006
May 1, 2006
851
852
853
}
May 28, 2006
May 28, 2006
854
855
static void
glSDL_FreeTexInfo (_THIS, private_hwdata * txi)
May 1, 2006
May 1, 2006
856
{
May 28, 2006
May 28, 2006
857
858
if (!txi)
return;
May 1, 2006
May 1, 2006
859
May 28, 2006
May 28, 2006
860
861
862
glSDL_UnloadTexture (this, txi);
SDL_free (txi->texture);
SDL_free (txi);
May 1, 2006
May 1, 2006
863
#ifdef DEBUG_GLSDL
May 28, 2006
May 28, 2006
864
fprintf (stderr, "glSDL: Freed TexInfo %p.\n", txi);
May 1, 2006
May 1, 2006
865
866
867
868
869
#endif
}
/* Detach and free the texinfo of a surface. */
May 28, 2006
May 28, 2006
870
871
static void
glSDL_RemoveTexInfo (_THIS, SDL_Surface * surface)
May 1, 2006
May 1, 2006
872
{
May 28, 2006
May 28, 2006
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
SDL_Surface *next, *prev;
if (!glSDL_GetTexInfo (surface))
return;
/* maintain our doubly linked list */
next = surface->hwdata->next;
prev = surface->hwdata->prev;
if (prev != NULL) {
prev->hwdata->next = next;
} else {
first = next;
}
if (next != NULL) {
next->hwdata->prev = prev;
}
glSDL_FreeTexInfo (this, surface->hwdata);
surface->hwdata = NULL;
May 1, 2006
May 1, 2006
891
892
893
894
895
896
897
898
}
/*
* Calculate chopping/tiling of a surface to
* fit it into the smallest possible OpenGL
* texture.
*/
May 28, 2006
May 28, 2006
899
900
static int
glSDL_CalcChop (private_hwdata * txi)
May 1, 2006
May 1, 2006
901
{
May 28, 2006
May 28, 2006
902
903
904
905
int rows, vw, vh;
int vertical = 0;
int texsize;
int lastw, lasth, minsize;
May 1, 2006
May 1, 2006
906
May 28, 2006
May 28, 2006
907
908
vw = txi->virt.w;
vh = txi->virt.h;
May 1, 2006
May 1, 2006
909
910
#ifdef DEBUG_GLSDL_CHOP
May 28, 2006
May 28, 2006
911
fprintf (stderr, "w=%d, h=%d ", vw, vh);
May 1, 2006
May 1, 2006
912
#endif
May 28, 2006
May 28, 2006
913
914
915
916
917
if (vh > vw) {
int t = vw;
vw = vh;
vh = t;
vertical = 1;
May 1, 2006
May 1, 2006
918
#ifdef DEBUG_GLSDL_CHOP
May 28, 2006
May 28, 2006
919
fprintf (stderr, "(vertical) \t");
May 1, 2006
May 1, 2006
920
#endif
May 28, 2006
May 28, 2006
921
}
May 1, 2006
May 1, 2006
922
May 28, 2006
May 28, 2006
923
924
925
926
927
/*
* Check whether this is a "huge" surface - at least one dimension
* must be <= than the maximum texture size, or we'll have to chop
* in both directions.
*/
May 1, 2006
May 1, 2006
928
#ifdef DEBUG_GLSDL
May 28, 2006
May 28, 2006
929
930
if (maxtexsize < 0)
return GLERET ("glSDL_CalcChop() called before OpenGL init!");
May 1, 2006
May 1, 2006
931
#endif
May 28, 2006
May 28, 2006
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
if (vh > maxtexsize) {
/*
* Very simple hack for now; we just tile
* both ways with maximum size textures.
*/
texsize = maxtexsize;
txi->tilemode = GLSDL_TM_HUGE;
txi->texsize = texsize;
txi->tilew = texsize;
txi->tileh = texsize;
txi->tilespertex = 1;
/* Calculate number of textures needed */
txi->textures = (vw + texsize - 1) / texsize;
txi->textures *= (vh + texsize - 1) / texsize;
txi->texture = SDL_malloc (txi->textures * sizeof (int));
SDL_memset (txi->texture, -1, txi->textures * sizeof (int));
#ifdef DEBUG_GLSDL
fprintf (stderr, "two-way tiling; textures=%d\n", txi->textures);
May 1, 2006
May 1, 2006
952
#endif
May 28, 2006
May 28, 2006
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
if (!txi->texture) {
fprintf (stderr, "glSDL: INTERNAL ERROR: Failed to allocate"
" texture name table!\n");
return -3;
}
return 0;
}
/* Calculate minimum size */
rows = 1;
lastw = vw;
lasth = vh;
minsize = lastw > lasth ? lastw : lasth;
while (1) {
int w, h, size;
++rows;
w = vw / rows;
h = rows * vh;
size = w > h ? w : h;
if (size >= minsize) {
--rows;
break;
}
lastw = w;
lasth = h;
minsize = size;
}
if (minsize > maxtexsize) {
/* Handle multiple textures for very wide/tall surfaces. */
minsize = maxtexsize;
rows = (vw + minsize - 1) / minsize;
}
May 1, 2006
May 1, 2006
985
#ifdef DEBUG_GLSDL_CHOP
May 28, 2006
May 28, 2006
986
987
fprintf (stderr, "==> minsize=%d ", minsize);
fprintf (stderr, "(rows=%d) \t", rows);
May 1, 2006
May 1, 2006
988
989
#endif
May 28, 2006
May 28, 2006
990
991
992
993
/* Recalculate with nearest higher power-of-2 width. */
for (texsize = 1; texsize < minsize; texsize <<= 1);
txi->texsize = texsize;
rows = (vw + texsize - 1) / texsize;
May 1, 2006
May 1, 2006
994
#ifdef DEBUG_GLSDL_CHOP
May 28, 2006
May 28, 2006
995
fprintf (stderr, "==> texsize=%d (rows=%d) \t", texsize, rows);
May 1, 2006
May 1, 2006
996
997
#endif
May 28, 2006
May 28, 2006
998
999
/* Calculate number of tiles per texture */
txi->tilespertex = txi->texsize / vh;
May 1, 2006
May 1, 2006
1000
#ifdef DEBUG_GLSDL_CHOP