This repository has been archived by the owner on Feb 11, 2021. It is now read-only.
/
SDL_glsdl.c
2465 lines (2161 loc) · 68.4 KB
/
SDL_glsdl.c
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
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
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
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
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
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 */
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);
#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...
*/
static void
glSDL_SoftBlit(SDL_Surface * src, SDL_Rect * srcrect,
SDL_Surface * dst, SDL_Rect * dstrect)
{
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);
}
/*
* 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.
*/
static SDL_Surface *
glSDL_ConvertSurface(SDL_Surface * surface,
SDL_PixelFormat * format, Uint32 flags)
{
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);
}
/*----------------------------------------------------------
Some OpenGL function wrappers
----------------------------------------------------------*/
static struct
{
int do_blend;
int do_texture;
GLuint texture;
GLenum sfactor, dfactor;
} glstate;
static void
glSDL_reset(void)
{
glstate.do_blend = -1;
glstate.do_blend = -1;
glstate.texture = GLSDL_NOTEX;
glstate.sfactor = 0xffffffff;
glstate.dfactor = 0xffffffff;
}
static __inline__ void
glSDL_do_blend(_THIS, int on)
{
if (glstate.do_blend == on)
return;
if (on)
this->glEnable(GL_BLEND);
else
this->glDisable(GL_BLEND);
glstate.do_blend = on;
}
static __inline__ void
glSDL_do_texture(_THIS, int on)
{
if (glstate.do_texture == on)
return;
if (on)
this->glEnable(GL_TEXTURE_2D);
else
this->glDisable(GL_TEXTURE_2D);
glstate.do_texture = on;
}
static __inline__ void
glSDL_blendfunc(_THIS, GLenum sfactor, GLenum dfactor)
{
if ((sfactor == glstate.sfactor) && (dfactor == glstate.dfactor))
return;
this->glBlendFunc(sfactor, dfactor);
glstate.sfactor = sfactor;
glstate.dfactor = dfactor;
}
static __inline__ void
glSDL_texture(_THIS, GLuint tx)
{
if (tx == glstate.texture)
return;
this->glBindTexture(GL_TEXTURE_2D, tx);
glstate.texture = tx;
}
/*----------------------------------------------------------
glSDL specific data types
----------------------------------------------------------*/
typedef enum
{
GLSDL_TM_SINGLE,
GLSDL_TM_HORIZONTAL,
GLSDL_TM_VERTICAL,
GLSDL_TM_HUGE
} GLSDL_TileModes;
typedef struct private_hwdata
{
/* Size of surface in logic screen pixels */
int lw, lh;
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 */
/* Area of surface to upload when/after unlocking */
SDL_Rect invalid_area;
int temporary; /* Throw away after one use. */
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 */
} private_hwdata;
/* some function prototypes */
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);
/* 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 */
SDL_Surface *OpenGL_Surface;
/* pointer to the beggining of the list used for memory allocation */
SDL_Surface *first = NULL;
#ifdef DEBUG_GLSDL
static __inline__ int
GLERET(const char *txt)
{
fprintf(stderr, "glSDL ERROR: '%s'\n", txt);
return -1;
}
static __inline__ void
GLERR(const char *txt)
{
fprintf(stderr, "glSDL ERROR: '%s'\n", txt);
}
#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
&QZ_bootstrap;
#elif SDL_VIDEO_DRIVER_X11
&X11_bootstrap;
#elif SDL_VIDEO_DRIVER_WINDIB
&WINDIB_bootstrap;
#elif SDL_VIDEO_DRIVER_BWINDOW
&BWINDOW_bootstrap;
#elif SDL_VIDEO_DRIVER_TOOLBOX
&TOOLBOX_bootstrap;
#elif SDL_VIDEO_DRIVER_CYBERGRAPHICS
&CGX_bootstrap;
#elif SDL_VIDEO_DRIVER_PHOTON
&ph_bootstrap;
#elif SDL_VIDEO_DRIVER_DC
&DC_bootstrap;
#else
NULL;
#endif
static int
glSDL_Available(void)
{
#ifdef DEBUG_GLSDL
fprintf(stderr, "available\n");
#endif
if (opengl_bootstrap == NULL)
return 0;
return (opengl_bootstrap->available());
}
static void
glSDL_DeleteDevice(SDL_VideoDevice * device)
{
SDL_free(device->hidden);
SDL_free(device);
}
/* Create a glSDL device */
static SDL_VideoDevice *
glSDL_CreateDevice(int devindex)
{
SDL_VideoDevice *device;
#ifdef DEBUG_GLSDL
fprintf(stderr, "entering createdevice\n");
#endif
/* 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;
#ifdef DEBUG_GLSDL
fprintf(stderr, "leaving createdevice\n");
#endif
return device;
}
/* Our bootstraping structure */
VideoBootStrap glSDL_bootstrap = {
"glSDL", "glSDL - SDL over OpenGL",
glSDL_Available, glSDL_CreateDevice
};
static int
glSDL_VideoInit(_THIS, SDL_PixelFormat * vformat)
{
int r;
printf("glSDL videoinit\n");
#ifdef DEBUG_GLSDL
fprintf(stderr, "videoinit\n");
#endif
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;
}
SDL_Rect **
glSDL_ListModes(_THIS, SDL_PixelFormat * format, Uint32 flags)
{
return ((SDL_Rect **) - 1);
}
static void
glSDL_VideoQuit(_THIS)
{
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);
}
static SDL_Surface *
glSDL_SetVideoMode(_THIS, SDL_Surface * current, int width, int height,
int bpp, Uint32 flags)
{
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;
#ifdef DEBUG_GLSDL
fprintf(stderr, "got %d bpp\n", bpp);
#endif
/* 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,
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
0x00FF0000,
0x0000FF00, 0x000000FF, 0x00000000
#else
0x0000FF00,
0x00FF0000, 0xFF000000, 0x00000000
#endif
);
break;
case 24:
this->is_32bit = 0;
this->screen = SDL_CreateRGBSurface(flags, width, height, bpp,
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
0x00FF0000,
0x0000FF00, 0x000000FF, 0x00000000
#else
0x0000FF00,
0x00FF0000, 0xFF000000, 0x00000000
#endif
);
break;
case 16:
this->is_32bit = 0;
this->screen = SDL_CreateRGBSurface(flags, width, height, bpp,
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
0x0000F800,
0x000007E0, 0x0000001F, 0x00000000
#else
0x0000001F,
0x000007E0, 0x0000F800, 0x00000000
#endif
);
break;
case 15:
this->is_32bit = 0;
this->screen = SDL_CreateRGBSurface(flags, width, height, bpp,
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
0x00007C00,
0x000003E0, 0x0000001F, 0x00000000
#else
0x0000001F,
0x000003E0, 0x00007C00, 0x00000000
#endif
);
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
*/
/* if (bpp==8)
{
this->screen->format->palette->ncolors=255;
SDL_DitherColors(this->screen->format->palette->colors,bpp);
}*/
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,
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
0xFF000000,
0x00FF0000,
0x0000FF00, 0x000000FF
#else
0x000000FF,
0x0000FF00,
0x00FF0000, 0xFF000000
#endif
);
/* Now create the raw OpenGL surface */
OpenGL_Surface = SDL_CreateRGBSurface(flags, width, height, 24,
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
0x000000FF,
0x0000FF00, 0x00FF0000, 0x00000000
#else
0xFF000000,
0x00FF0000, 0x0000FF00, 0x00000000
#endif
);
/* Here we have to setup OpenGL funcs ourselves */
#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"
#undef SDL_PROC
if (this->GL_MakeCurrent(this) < 0)
return (NULL);
#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"
#undef SDL_PROC
#ifdef FAKE_MAXTEXSIZE
maxtexsize = FAKE_MAXTEXSIZE;
#else
this->glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxtexsize);
#endif
#ifdef DEBUG_GLSDL
fprintf(stderr, "glSDL: Max texture size: %d\n", maxtexsize);
#endif
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;
}
static int
glSDL_SetColors(_THIS, int firstcolor, int ncolors, SDL_Color * colors)
{
/* We don't need to fill this one */
return 0;
}
#ifdef DEBUG_GLSDL
static void
glSDL_print_glerror(_THIS, int point)
{
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);
}
#endif
/* Get texinfo for a surface. */
static __inline__ private_hwdata *
glSDL_GetTexInfo(SDL_Surface * surface)
{
if (!surface)
return NULL;
return surface->hwdata;
}
/* Allocate a "blank" texinfo for a suface. */
static private_hwdata *
glSDL_AllocTexInfo(SDL_Surface * surface)
{
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);
#endif
return txi;
}
static void
glSDL_FreeTexInfo(_THIS, private_hwdata * txi)
{
if (!txi)
return;
glSDL_UnloadTexture(this, txi);
SDL_free(txi->texture);
SDL_free(txi);
#ifdef DEBUG_GLSDL
fprintf(stderr, "glSDL: Freed TexInfo %p.\n", txi);
#endif
}
/* Detach and free the texinfo of a surface. */
static void
glSDL_RemoveTexInfo(_THIS, SDL_Surface * surface)
{
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;
}
/*
* Calculate chopping/tiling of a surface to
* fit it into the smallest possible OpenGL
* texture.
*/
static int
glSDL_CalcChop(private_hwdata * txi)
{
int rows, vw, vh;
int vertical = 0;
int texsize;
int lastw, lasth, minsize;
vw = txi->virt.w;
vh = txi->virt.h;
#ifdef DEBUG_GLSDL_CHOP
fprintf(stderr, "w=%d, h=%d ", vw, vh);
#endif
if (vh > vw) {
int t = vw;
vw = vh;
vh = t;
vertical = 1;
#ifdef DEBUG_GLSDL_CHOP
fprintf(stderr, "(vertical) \t");
#endif
}
/*
* 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.
*/
#ifdef DEBUG_GLSDL
if (maxtexsize < 0)
return GLERET("glSDL_CalcChop() called before OpenGL init!");
#endif
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);
#endif
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;
}
#ifdef DEBUG_GLSDL_CHOP
fprintf(stderr, "==> minsize=%d ", minsize);
fprintf(stderr, "(rows=%d) \t", rows);
#endif
/* Recalculate with nearest higher power-of-2 width. */
for (texsize = 1; texsize < minsize; texsize <<= 1);
txi->texsize = texsize;
rows = (vw + texsize - 1) / texsize;
#ifdef DEBUG_GLSDL_CHOP
fprintf(stderr, "==> texsize=%d (rows=%d) \t", texsize, rows);
#endif
/* Calculate number of tiles per texture */
txi->tilespertex = txi->texsize / vh;
#ifdef DEBUG_GLSDL_CHOP
fprintf(stderr, "tilespertex=%d \t", txi->tilespertex);
#endif
/* Calculate number of textures needed */
txi->textures = (rows + txi->tilespertex - 1) / txi->tilespertex;
txi->texture = (GLuint *) SDL_malloc(txi->textures * sizeof(GLuint));
SDL_memset(txi->texture, GLSDL_NOTEX, txi->textures * sizeof(GLuint));
#ifdef DEBUG_GLSDL_CHOP