-
Notifications
You must be signed in to change notification settings - Fork 12
/
display.c
1101 lines (1002 loc) · 29.6 KB
/
display.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
/* ACID WARP (c)Copyright 1992, 1993 by Noah Spurrier
* All Rights reserved. Private Proprietary Source Code by Noah Spurrier
* Ported to Linux by Steven Wills
* Ported to SDL by Boris Gjenero
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <SDL.h>
#ifdef WITH_GL
#undef WITH_GLES
#undef WITH_GLEW
#ifdef __APPLE__
#include <OpenGL/gl.h>
#elif defined(_WIN32) || defined(_WIN64)
/* Functions not defined in in opengl32.dll are used, and their
locations need to be loaded at runtime. Use GLEW for that */
#include <GL/glew.h>
#define WITH_GLEW
#else
#include <GLES2/gl2.h>
#define WITH_GLES
#endif
#if !SDL_VERSION_ATLEAST(2,0,0)
#error OpenGL only supported with SDL 2
#endif
#endif
#include "handy.h"
#include "acidwarp.h"
#include "display.h"
#if defined(HAVE_FULLSCREEN) && defined(EMSCRIPTEN)
#include <emscripten/html5.h>
#endif
#if !defined(WIN32) && !SDL_VERSION_ATLEAST(2,0,0)
#define HAVE_PALETTE
#endif
#if SDL_VERSION_ATLEAST(2,0,0)
static SDL_Window *window = NULL;
#endif
#ifdef WITH_GL
SDL_GLContext context;
GLuint indtex, paltex, glprogram;
const GLchar vertex[] =
#ifdef WITH_GLES
"#version 100\n"
"precision mediump float;\n"
#else
"#version 110\n"
#endif
"attribute vec4 Position;\n"
"attribute vec2 TexPos;\n"
"varying vec2 TexCoord0;\n"
"void main()\n"
"{\n"
"gl_Position = Position;\n"
"TexCoord0 = TexPos;\n"
"}\0";
const GLchar fragment[] =
#ifdef WITH_GLES
"#version 100\n"
"precision mediump float;\n"
#else
"#version 110\n"
#endif
"uniform sampler2D Palette;\n"
"uniform sampler2D IndexTexture;\n"
// Texture coordinates are passed from vertex shader
"varying vec2 TexCoord0;\n"
"void main()\n"
"{\n"
// Look up pixel in 8 bpp indexed colour image texture
"vec4 myindex = texture2D(IndexTexture, TexCoord0);\n"
// Read RGBA value for that pixel from palette texture
"gl_FragColor = texture2D(Palette, vec2(myindex.r, 0.0));\n"
"}\0";
#else /* !WITH_GL */
static SDL_Surface *screen = NULL, *out_surf = NULL;
#ifdef ENABLE_THREADS
static UCHAR *out_buf = NULL;
static SDL_Surface *draw_surf = NULL;
#else /* !ENABLE_THREADS */
#define draw_surf out_surf
#endif /* !ENABLE_THREADS */
static int disp_DrawingOnSurface;
#endif /* !WITH_GL */
static UCHAR *draw_buf = NULL;
#ifdef HAVE_PALETTE
static int disp_UsePalette;
#endif
#ifdef HAVE_FULLSCREEN
static int fullscreen = 0;
#if SDL_VERSION_ATLEAST(2,0,0)
static int desktopfs = 0;
#else
static int nativewidth = 0, nativeheight;
#endif
static int winwidth = 0;
static int winheight;
#endif /* HAVE_FULLSCREEN */
static int scaling = 1;
static int width, height;
void disp_setPalette(unsigned char *palette)
{
#ifdef WITH_GL
static GLubyte glcolors[256 * 4];
int i;
for (i = 0; i < 256; i++) {
glcolors[i*4+0] = palette[i*3+0] << 2;
glcolors[i*4+1] = palette[i*3+1] << 2;
glcolors[i*4+2] = palette[i*3+2] << 2;
glcolors[i*4+3] = 255;
}
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, paltex);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 256, 1, GL_RGBA,
GL_UNSIGNED_BYTE, glcolors);
glClear(GL_COLOR_BUFFER_BIT);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
SDL_GL_SwapWindow(window);
#else /* !WITH_GL */
static SDL_Color sdlColors[256];
int i;
for(i=0;i<256;i++) {
sdlColors[i].r = palette[i*3+0] << 2;
sdlColors[i].g = palette[i*3+1] << 2;
sdlColors[i].b = palette[i*3+2] << 2;
}
#ifdef HAVE_PALETTE
if (disp_UsePalette) {
/* Simply change the palette */
SDL_SetPalette(screen, SDL_PHYSPAL, sdlColors, 0, 256);
#ifdef EMSCRIPTEN
/* This is needed for palette change to take effect. */
SDL_LockSurface(screen);
SDL_UnlockSurface(screen);
#endif
} else
#endif /* HAVE_PALETTE */
{
/* Update colours in software surface, blit it to the screen
* with updated colours, and then show it on the screen.
*/
#if SDL_VERSION_ATLEAST(2,0,0)
/* Is this really necessary,
* or could code above write directly into sdlPalette->Colors?
*/
SDL_SetPaletteColors(out_surf->format->palette, sdlColors, 0, 256);
#else /* !SDL_VERSION_ATLEAST(2,0,0) */
SDL_SetColors(out_surf, sdlColors, 0, 256);
#endif
if (out_surf != screen) {
SDL_BlitSurface(out_surf, NULL, screen, NULL);
}
#if SDL_VERSION_ATLEAST(2,0,0)
SDL_UpdateWindowSurface(window);
#else /* !SDL_VERSION_ATLEAST(2,0,0) */
SDL_Flip(screen);
#endif
}
#endif /* !WITH_GL */
}
void disp_beginUpdate(UCHAR **p, unsigned int *pitch,
unsigned int *w, unsigned int *h)
{
#ifndef WITH_GL
/* Locking only needed at this point if drawing routines directly draw
* on a surface, and that surface needs locking.
*/
if (disp_DrawingOnSurface) {
if (SDL_MUSTLOCK(draw_surf)) {
if (SDL_LockSurface(draw_surf) != 0) {
fatalSDLError("locking surface when starting update");
}
}
*p = draw_surf->pixels;
*pitch = draw_surf->pitch;
} else
#endif
{
*p = draw_buf;
*pitch = width;
}
*w = width;
*h = height;
}
void disp_finishUpdate(void)
{
#ifndef WITH_GL
/* Locking only needed at this point if drawing routines directly draw
* on a surface, and that surface needs locking.
*/
if (disp_DrawingOnSurface) {
if (SDL_MUSTLOCK(draw_surf)) SDL_UnlockSurface(draw_surf);
draw_buf = NULL;
}
#endif
}
#ifndef WITH_GL
static void disp_toSurface(void)
{
int row;
unsigned char *outp, *inp =
#ifdef ENABLE_THREADS
out_buf;
#else /* !ENABLE_THREADS */
draw_buf;
#endif
/* This means drawing was on a separate buffer and it needs to be
* copied to the surface. It also means the surface hasn't been locked.
*/
if (SDL_MUSTLOCK(out_surf)) {
if (SDL_LockSurface(out_surf) != 0) {
fatalSDLError("locking surface when ending update");
}
}
outp = out_surf->pixels;
if (scaling == 1) {
for (row = 0; row < height; row++) {
memcpy(outp, inp, width);
outp += out_surf->pitch;
inp += width;
}
} else if (scaling == 2) {
unsigned char *outp2 = outp + out_surf->pitch;
int skip = (out_surf->pitch - width) << 1;
int col;
unsigned char c;
for (row = 0; row < height; row++) {
for (col = 0; col < width; col++) {
c = *(inp++);
*(outp++) = c;
*(outp++) = c;
*(outp2++) = c;
*(outp2++) = c;
}
outp += skip;
outp2 += skip;
}
}
if (SDL_MUSTLOCK(out_surf)) {
SDL_UnlockSurface(out_surf);
}
}
#endif /* !WITH_GL */
void disp_swapBuffers(void)
{
#ifdef WITH_GL
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, indtex);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, GL_LUMINANCE,
GL_UNSIGNED_BYTE, draw_buf);
#else /* !WITH_GL */
if (!disp_DrawingOnSurface) {
#ifdef ENABLE_THREADS
{
UCHAR *temp = draw_buf;
draw_buf = out_buf;
out_buf = temp;
}
#endif /* ENABLE_THREADS */
disp_toSurface();
}
#ifdef ENABLE_THREADS
else {
SDL_Surface *temp = draw_surf;
draw_surf = out_surf;
out_surf = temp;
}
#endif /* ENABLE_THREADS */
if (out_surf != screen) {
SDL_BlitSurface(out_surf, NULL, screen, NULL);
}
#if SDL_VERSION_ATLEAST(2,0,0)
SDL_UpdateWindowSurface(window);
#else /* !SDL_VERSION_ATLEAST(2,0,0) */
SDL_Flip(screen);
#endif
#endif /* !WITH_GL */
}
#ifdef HAVE_FULLSCREEN
static void disp_toggleFullscreen(void)
{
#if SDL_VERSION_ATLEAST(2,0,0)
if (fullscreen) {
SDL_SetWindowFullscreen(window, 0);
fullscreen = FALSE;
} else {
winwidth = width;
winheight = height;
SDL_SetWindowFullscreen(window, desktopfs ?
SDL_WINDOW_FULLSCREEN_DESKTOP :
SDL_WINDOW_FULLSCREEN);
fullscreen = TRUE;
}
#else
if (fullscreen) {
/* If going back to windowed mode, restore window size */
if (winwidth != 0) {
disp_init(winwidth, winheight, 0);
winwidth = 0;
} else {
disp_init(width, height, 0);
}
} else {
/* Save window size for return to windowed mode */
winwidth = width;
winheight = height;
/* disp_init() may select a different size than suggested. It will
* handle resizing if needed.
*/
disp_init(width, height, DISP_FULLSCREEN);
}
#endif
SDL_ShowCursor(!fullscreen);
}
#endif
static void disp_processKey(
#if SDL_VERSION_ATLEAST(2,0,0)
SDL_Keycode key
#define keymod SDL_GetModState()
#else
SDLKey key, SDLMod keymod
#endif
)
{
switch (key) {
case SDLK_UP: handleinput(CMD_PAL_FASTER); break;
case SDLK_DOWN: handleinput(CMD_PAL_SLOWER); break;
case SDLK_p: handleinput(CMD_PAUSE); break;
case SDLK_n: handleinput(CMD_SKIP); break;
#ifndef EMSCRIPTEN
case SDLK_c:
case SDLK_PAUSE:
if ((keymod & KMOD_CTRL) == 0) break; /* else like SDLK_q */
case SDLK_q: handleinput(CMD_QUIT); break;
#endif
case SDLK_k: handleinput(CMD_NEWPAL); break;
case SDLK_l: handleinput(CMD_LOCK); break;
#ifdef HAVE_FULLSCREEN
case SDLK_ESCAPE:
if (fullscreen) disp_toggleFullscreen();
break;
case SDLK_RETURN:
if (keymod & KMOD_ALT) disp_toggleFullscreen();
break;
#endif /* HAVE_FULLSCREEN */
default: break;
}
#undef keymod
}
static void display_redraw(void)
{
#ifdef WITH_GL
glClear(GL_COLOR_BUFFER_BIT);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
SDL_GL_SwapWindow(window);
#elif 0
/* Redraw parts that were overwritten. (This is unlikely with
* modern compositing window managers */
if (surface != screen) {
SDL_BlitSurface(surface, NULL, screen, NULL);
#if SDL_VERSION_ATLEAST(2,0,0)
SDL_UpdateWindowSurface(window);
#else
SDL_Flip(screen);
#endif
} else {
/* Copy from draw_buf to screen */
disp_beginUpdate();
disp_finishUpdate();
}
#endif /* !WITH_GL */
}
#if defined(HAVE_FULLSCREEN) && defined(EMSCRIPTEN)
/* Full screen toggle must occur in event handler */
static EMSCRIPTEN_RESULT disp_dblClick(int eventType,
const EmscriptenMouseEvent *mouseEvent,
void *userData)
{
if (eventType == EMSCRIPTEN_EVENT_DBLCLICK &&
mouseEvent->button == 0) {
disp_toggleFullscreen();
}
return EMSCRIPTEN_RESULT_SUCCESS;
}
#endif /* HAVE_FULLSCREEN && EMSCRIPTEN */
void disp_processInput(void) {
SDL_Event event;
while ( SDL_PollEvent(&event) > 0 ) {
switch (event.type) {
#if !SDL_VERSION_ATLEAST(2,0,0)
case SDL_VIDEOEXPOSE:
display_redraw();
break;
#endif /* !SDL_VERSION_ATLEAST(2,0,0) */
#if defined(HAVE_FULLSCREEN) && !defined(EMSCRIPTEN)
/* SDL full screen switching has no useful effect with Emscripten */
case SDL_MOUSEBUTTONDOWN:
if (event.button.button == SDL_BUTTON_LEFT) {
#if SDL_VERSION_ATLEAST(2,0,2)
if (event.button.clicks == 2) {
disp_toggleFullscreen();
}
#else
// Earlier SDL versions don't report double clicks
static Uint32 dblclicktm = 0;
Uint32 clicktime = SDL_GetTicks();
// Like !SDL_TICKS_PASSED(), which may not be available
if ((Sint32)(dblclicktm - clicktime) > 0) {
disp_toggleFullscreen();
}
dblclicktm = clicktime + 200;
#endif // !SDL_VERSION_ATLEAST(2,0,2)
}
break;
#endif /* HAVE_FULLSCREEN */
case SDL_KEYDOWN:
disp_processKey(event.key.keysym.sym
#if !SDL_VERSION_ATLEAST(2,0,0)
, event.key.keysym.mod
#endif
);
break;
#if SDL_VERSION_ATLEAST(2,0,0)
case SDL_WINDOWEVENT:
switch (event.window.event) {
case SDL_WINDOWEVENT_RESIZED:
/* If full screen resolution is at least twice as large as
* previous window, then use 2x scaling, else no scaling.
*/
#if defined(HAVE_FULLSCREEN) && !defined(WITH_GL)
scaling = fullscreen ?
((winwidth != 0 &&
event.window.data1 >= 2 * winwidth) ? 2 : 1) : 1;
#endif /* HAVE_FULLSCREEN */
if (width != (event.window.data1 / scaling) ||
height != (event.window.data2 / scaling)) {
disp_init(event.window.data1 / scaling,
event.window.data2 / scaling,
#ifdef HAVE_FULLSCREEN
fullscreen
#else /* !HAVE_FULLSCREEN */
0
#endif
);
}
break;
case SDL_WINDOWEVENT_EXPOSED:
display_redraw();
break;
}
break;
#else /* !SDL_VERSION_ATLEAST(2,0,0) */
case SDL_VIDEORESIZE:
/* Why are there events when there is no resize? */
if (width != (event.resize.w / scaling) ||
height != (event.resize.h / scaling)) {
disp_init(event.resize.w / scaling, event.resize.h / scaling,
#ifdef HAVE_FULLSCREEN
fullscreen
#else /* !HAVE_FULLSCREEN */
0
#endif
);
}
break;
#endif
case SDL_QUIT:
handleinput(CMD_QUIT);
break;
default:
break;
}
}
}
#if defined(HAVE_FULLSCREEN) && !SDL_VERSION_ATLEAST(2,0,0)
/* Function for finding the best SDL full screen mode for filling the screen.
*
* Inputs:
* modes: array of pointers to SDL_Rect structures describing modes.
* width, height: dimensions of desired mode
* desiredaspect: desired aspect ratio
*
* Outputs:
* width, height: updated with dimensions of found mode
* scaling: updated with scaling to be used along with that mode
*/
static void disp_findBestMode(SDL_Rect ** modes,
int *width, int *height,
int *scaling, int desiredaspect)
{
int bestdiff = -1;
int curpix = *width * *height;
int i, j;
for(i=0;modes[i];i++) {
/* For every mode, try every possible scaling */
for (j=1; j<=2; j++) {
int asperr, pixerr, curdiff;
/* Difference in number of pixels */
pixerr = modes[i]->w * modes[i]->h / (j * j) - curpix;
if (pixerr < 0) pixerr = -pixerr;
/* Difference in aspect ratio compared to desktop */
if (desiredaspect > 0) {
int aspect = modes[i]->w * 1024 / modes[i]->h;
asperr = aspect - desiredaspect;
if (asperr < 0) asperr = -asperr;
/* Aspect ratio is important because we want to fill screen */
asperr *= 1024;
} else {
asperr = 0;
}
/* Use sum of pixel and aspect ratio error */
curdiff = pixerr + asperr;
/* Check if this mode is better */
if (bestdiff == -1 || curdiff < bestdiff ||
(curdiff == bestdiff && j < *scaling)) {
*scaling = j;
*width = modes[i]->w / j;
*height = modes[i]->h / j;
bestdiff = curdiff;
}
}
}
}
#endif /* HAVE_FULLSCREEN */
#ifdef ADDICON
extern unsigned char acidwarp_rgb[];
/* For SDL 1 call before SDL_SetWindowMode().
* For SDL 2 call after window is created.
*/
static void disp_setIcon(void)
{
SDL_Surface *iconsurface =
SDL_CreateRGBSurfaceFrom(acidwarp_rgb, 64, 64, 24, 64*3,
0x0000ff, 0x00ff00, 0xff0000, 0
/* Big endian may need: 0xff0000, 0x00ff00, 0x0000ff, 0 */
);
if (iconsurface == NULL) fatalSDLError("creating icon surface");
#if SDL_VERSION_ATLEAST(2,0,0)
SDL_SetWindowIcon(window, iconsurface);
#else
/* Must be called before SDL_SetVideoMode() */
SDL_WM_SetIcon(iconsurface, NULL);
#endif
SDL_FreeSurface(iconsurface);
}
#endif /* ADDICON */
static void disp_freeBuffer(UCHAR **buf)
{
if (*buf != NULL) {
free(*buf);
*buf = NULL;
}
}
static void disp_reallocBuffer(UCHAR **buf)
{
disp_freeBuffer(buf);
*buf = calloc(width * height, 1);
if (*buf == NULL) {
printf("Couldn't allocate graphics buffer.\n");
quit(-1);
}
}
#ifndef WITH_GL
static void disp_freeSurface(SDL_Surface **surf)
{
if (*surf != NULL) {
SDL_FreeSurface(*surf);
*surf = NULL;
}
}
static void disp_allocSurface(SDL_Surface **surf)
{
*surf = SDL_CreateRGBSurface(SDL_SWSURFACE,
width*scaling, height*scaling,
8, 0, 0, 0, 0);
if (!(*surf)) fatalSDLError("creating secondary surface");
}
#endif /* !WITH_GL */
static void disp_allocateOffscreen(void)
{
#ifdef ENABLE_THREADS
/* Drawing must not be happening in the background
* while the memory being drawn to gets reallocated!
*/
draw_abort();
#endif /* ENABLE_THREADS */
#ifdef WITH_GL
disp_reallocBuffer(&draw_buf);
#else /* !WITH_GL */
/* Free secondary surface */
if (out_surf != screen) disp_freeSurface(&out_surf);
#ifdef ENABLE_THREADS
disp_freeSurface(&draw_surf);
#endif /* ENABLE_THREADS */
#ifdef HAVE_PALETTE
if (disp_UsePalette) {
/* When using a real palette, draw_buf is used instead. */
out_surf = screen;
} else
#endif /* HAVE_PALETTE */
{
/* Create 8 bit surface to draw into. This is needed if pixel
* formats differ or to respond to SDL_VIDEOEXPOSE events.
*/
disp_allocSurface(&out_surf);
}
if (scaling == 1
/* Normally need to have offscreen data for expose events,
* but no need for that with Emscripten.
*/
#if defined(HAVE_PALETTE) && !defined(EMSCRIPTEN)
&& !disp_UsePalette
#endif
) {
if (!disp_DrawingOnSurface) {
disp_freeBuffer(&draw_buf);
#ifdef ENABLE_THREADS
disp_freeBuffer(&out_buf);
#endif /* ENABLE_THREADS */
}
disp_DrawingOnSurface = 1;
#ifdef ENABLE_THREADS
disp_allocSurface(&draw_surf);
#endif /* ENABLE_THREADS */
} else {
disp_DrawingOnSurface = 0;
disp_reallocBuffer(&draw_buf);
#ifdef ENABLE_THREADS
disp_reallocBuffer(&out_buf);
#endif /* ENABLE_THREADS */
}
#endif /* !WITH_GL */
}
#ifdef WITH_GL
static void disp_glerror(char *s)
{
GLenum err;
fprintf(stderr, "OpenGL error at %s. Error log follows:", s);
while((err = glGetError()) != GL_NO_ERROR) {
fprintf(stderr, "%x\n", err);
}
exit(-1);
}
static GLuint disp_newtex(void)
{
GLuint texname;
glGenTextures(1, &texname);
glBindTexture(GL_TEXTURE_2D, texname);
/* Needed for GL_NEAREST sampling on non power of 2 (NPOT) textures
* in OpenGL ES 2.0 and WebGL.
*/
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
/* Setting GL_TEXTURE_MAG_FILTER to nearest prevents image defects */
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
return texname;
}
static GLuint loadShader(GLuint program, GLenum type,
const GLchar *shaderSrc) {
GLint compile_status;
GLuint shader;
shader = glCreateShader(type);
if (shader == 0) disp_glerror("glCreateShader");
glShaderSource(shader, 1, &shaderSrc, NULL);
glCompileShader(shader);
glGetShaderiv(shader, GL_COMPILE_STATUS, &compile_status);
if (compile_status != GL_TRUE) {
GLsizei loglen = 0;
GLchar infolog[1024];
printf("OpenGL error: %s shader failed to compile. Info log follows:\n",
(type == GL_VERTEX_SHADER) ? "vertex" : "fragment");
glGetShaderInfoLog(shader, sizeof(infolog), &loglen, infolog);
fwrite(infolog, loglen, 1, stderr);
quit(-1);
}
glAttachShader(program, shader);
return 0;
}
static void disp_glinit(int width, int height, Uint32 videoflags)
{
GLuint buffer;
GLint status;
/* Vertices consist of point x, y, z, w followed by texture x and y */
static const GLfloat vertices[] = {
-1.0, -1.0, 0.0, 1.0, 0.0, 1.0,
-1.0, 1.0, 0.0, 1.0, 0.0, 0.0,
1.0, -1.0, 0.0, 1.0, 1.0, 1.0,
1.0, 1.0, 0.0, 1.0, 1.0, 0.0,
};
#ifdef WITH_GLES
/* WebGL 1.0 is based on OpenGL ES 2.0 */
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
#else
// TODO: Make it work with core profile
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_COMPATIBILITY);
#endif
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
window = SDL_CreateWindow("Acidwarp",
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
width, height,
videoflags | SDL_WINDOW_OPENGL);
if (window == NULL) fatalSDLError("creating SDL OpenGL window");
context = SDL_GL_CreateContext(window);
if (context == NULL) fatalSDLError("creating OpenGL profile");
#ifdef WITH_GLEW
{
GLenum err = glewInit();
if (GLEW_OK != err) {
fprintf(stderr, "Error initializing GLEW: %s\n",
glewGetErrorString(err));
quit(-1);
}
/* TODO: Check exactly what version is required */
if (!GLEW_VERSION_2_0) {
fprintf(stderr, "Quitting because OpenGL 2.0 not supported\n");
quit(-1);
}
}
#endif
glprogram = glCreateProgram();
if (glprogram == 0) disp_glerror("glCreateProgram");
loadShader(glprogram, GL_VERTEX_SHADER, vertex);
loadShader(glprogram, GL_FRAGMENT_SHADER, fragment);
glBindAttribLocation(glprogram, 0, "Position");
glBindAttribLocation(glprogram, 1, "TexPos");
glLinkProgram(glprogram);
glGetProgramiv(glprogram, GL_LINK_STATUS, &status);
if (status != GL_TRUE) disp_glerror("glLinkProgram");
glUseProgram(glprogram);
glGenBuffers(1, &buffer);
glBindBuffer(GL_ARRAY_BUFFER, buffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE,
(char *)&vertices[6] - (char *)&vertices[0], 0);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE,
(char *)&vertices[6] - (char *)&vertices[0],
/* Why is this parameter declared as a pointer? */
(void *)((char *)&vertices[4] - (char *)&vertices[0]));
/* https://www.opengl.org/discussion_boards/showthread.php/163092-Passing-Multiple-Textures-from-OpenGL-to-GLSL-shader */
/* 256 x 1 texture used as palette lookup table */
glUniform1i(glGetUniformLocation(glprogram, "Palette"), 0);
glActiveTexture(GL_TEXTURE0);
paltex = disp_newtex();
glBindTexture(GL_TEXTURE_2D, paltex);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256, 1, 0,
GL_RGBA, GL_UNSIGNED_BYTE, NULL);
/* 8 bpp indexed colour texture used for image */
glUniform1i(glGetUniformLocation(glprogram, "IndexTexture"), 1);
glActiveTexture(GL_TEXTURE1);
indtex = disp_newtex();
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
}
#endif /* WITH_GL */
#if SDL_VERSION_ATLEAST(2,0,0)
void disp_init(int newwidth, int newheight, int flags)
{
Uint32 videoflags;
static int inited = 0;
width = newwidth;
height = newheight;
#ifdef HAVE_FULLSCREEN
fullscreen = (flags & DISP_FULLSCREEN) ? 1 : 0;
#endif
if (!inited) {
#ifdef HAVE_FULLSCREEN
#ifdef EMSCRIPTEN
emscripten_set_dblclick_callback(NULL, NULL, EM_FALSE, disp_dblClick));
#endif /* EMSCRIPTEN */
if (flags & DISP_DESKTOP_RES_FS) {
/* Need to know later when entering full screen another time */
desktopfs = TRUE;
}
#endif
videoflags =
#ifdef HAVE_FULLSCREEN
(fullscreen ?
(desktopfs ? SDL_WINDOW_FULLSCREEN_DESKTOP :
SDL_WINDOW_FULLSCREEN)
: SDL_WINDOW_RESIZABLE);
#else
SDL_WINDOW_RESIZABLE;
#endif
#ifdef HAVE_FULLSCREEN
SDL_ShowCursor(!fullscreen);
#endif
scaling = 1;
#ifdef WITH_GL
disp_glinit(width, height, videoflags);
#else /* !WITH_GL */
/* The screen is a destination for SDL_BlitSurface() copies.
* Nothing is ever directly drawn here, except with Emscripten.
*/
window = SDL_CreateWindow("Acidwarp",
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
width*scaling, height*scaling, videoflags);
if (window == NULL) fatalSDLError("creating SDL window");
#endif /* !WITH_GL */
#ifdef ADDICON
/* Must be called after window is created */
disp_setIcon();
#endif
inited = 1;
} /* !inited */
/* Raspberry Pi console will set window to size of full screen,
* and not give a resize event. */
SDL_GetWindowSize(window, &width, &height);
#ifdef WITH_GL
/* Create or recreate texture and set viewport, eg. when resizing */
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, indtex);
glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, width, height, 0,
GL_LUMINANCE, GL_UNSIGNED_BYTE, NULL);
glViewport(0, 0, width, height);
#else /* !WITH_GL */
/* Needs to be called again after resizing */
screen = SDL_GetWindowSurface(window);
if (!screen) fatalSDLError("getting window surface");
#endif /* !WITH_GL */
disp_allocateOffscreen();
/* This may be unnecessary if switching between windowed
* and full screen mode with the same dimensions. */
handleinput(CMD_RESIZE);
}
#else /* !SDL_VERSION_ATLEAST(2,0,0) */
void disp_init(int newwidth, int newheight, int flags)
{
Uint32 videoflags;
static int inited = 0;
#ifdef HAVE_PALETTE
static int nativedepth = 8;
int usedepth;
#endif
#ifdef HAVE_FULLSCREEN
static int desktopaspect = 0;
#endif
width = newwidth;
height = newheight;
#ifdef HAVE_FULLSCREEN
fullscreen = (flags & DISP_FULLSCREEN) ? 1 : 0;
#endif
videoflags = SDL_HWSURFACE | SDL_DOUBLEBUF |
#ifndef HAVE_PALETTE
SDL_ANYFORMAT |
#endif
#ifdef HAVE_FULLSCREEN
/* It would make sense to remove SDL_RESIZABLE for full screen,
* but that causes window to not be resizable anymore in Linux
* after it returns to windowed mode. */
(fullscreen ? SDL_FULLSCREEN : 0) |
#endif
SDL_RESIZABLE;
if (!inited) {
#ifdef HAVE_FULLSCREEN
const SDL_VideoInfo *vi;
/* Save information about desktop video mode */
vi = SDL_GetVideoInfo();
if (vi != NULL) {
#ifdef HAVE_PALETTE
nativedepth = vi->vfmt->BitsPerPixel;
#endif
if (vi->current_w > 0 && vi->current_h > 0) {
if (flags & DISP_DESKTOP_RES_FS) {
nativewidth = vi->current_w;
nativeheight = vi->current_h;
if (flags & DISP_FULLSCREEN) {
/* Save size, which is for windowed mode */
winwidth = newwidth;
winheight = newheight;
}
} else {
desktopaspect = vi->current_w * 1024 / vi->current_h;
}
}
}
#endif /* HAVE_FULLSCREEN */
SDL_WM_SetCaption("Acidwarp","acidwarp");
#ifdef ADDICON
/* Must be called before SDL_SetVideoMode() */
disp_setIcon();
#endif
#ifdef HAVE_FULLSCREEN
/* This causes an error when using Emscripten and Firefox */
SDL_ShowCursor(!fullscreen);
#endif
inited = 1;
} /* !inited */
#ifdef HAVE_PALETTE
usedepth = nativedepth;
#endif
#ifdef HAVE_FULLSCREEN
if (fullscreen && nativewidth == 0) {
SDL_Rect **modes;
#ifdef HAVE_PALETTE
/* Attempt to list 256 colour modes */
struct SDL_PixelFormat wantpf;
memset(&wantpf, 0, sizeof(wantpf));
wantpf.BitsPerPixel = 8;
wantpf.BytesPerPixel = 1;