-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathscreen.c
4852 lines (4471 loc) · 171 KB
/
screen.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
/*
* $Id: screen.c,v 1.6 2003/08/10 03:21:28 kenta Exp $
*
* Copyright 2003 Kenta Cho. All rights reserved.
*/
/**
* OpenGL screen handler.
*
* @version $Revision: 1.6 $
*/
#include <stdio.h>
#include <stdlib.h>
#include "SDL.h"
#include "SDL_mixer.h"
#include "SDL_image.h"
#include <math.h>
#include <string.h>
#include "genmcr.h"
#include "screen.h"
#include "rr.h"
#include "degutil.h"
#include "attractmanager.h"
#include "letterrender.h"
#include "boss_mtd.h"
////senquack - added for gp2x volume control:
#include "soundmanager.h"
//senquack - holds our port-specific definitions
#include "portcfg.h"
#define FAR_PLANE 720
#define SHARE_LOC "rr_share/"
typedef struct eglConnection {
EGLDisplay display;
EGLContext context;
EGLSurface surface;
NativeWindowType window;
} eglConnection;
static eglConnection egl_screen = {
EGL_NO_DISPLAY,
EGL_NO_CONTEXT,
EGL_NO_SURFACE,
(NativeWindowType)0
};
SDL_Surface * conv_surf_gl (SDL_Surface * s, int want_alpha);
// fps() returns the current FPS as an integer. Meant to be called once every frame.
int fps()
{
static int frames_drawn = 0; // Frame counter (resets after recording new FPS value)
static int current_fps = 0; // Current FPS
// Remembers last time we reported a FPS value:
static struct timespec lasttime = {.tv_sec=0, .tv_nsec=0};
static uint64_t lasttime_in_ns = 0;
// Get current time:
struct timespec curtime = {.tv_sec=0, .tv_nsec=0};
clock_gettime(CLOCK_MONOTONIC_RAW, &curtime);
uint64_t curtime_in_ns = (uint64_t)curtime.tv_nsec + (uint64_t)curtime.tv_sec * 1000000000;
// If one second has passed, record the time and reset the frame counter:
if ((curtime_in_ns - lasttime_in_ns) >= 1000000000) {
clock_gettime(CLOCK_MONOTONIC_RAW, &lasttime);
lasttime_in_ns = (uint64_t)lasttime.tv_nsec + (uint64_t)lasttime.tv_sec * 1000000000;
current_fps = frames_drawn;
frames_drawn = 0;
} else {
frames_drawn++;
}
return current_fps;
}
void swapGLScene ()
{
if (eglSwapBuffers(egl_screen.display, egl_screen.surface) != GL_TRUE) {
printf("OpenGLES: eglSwapBuffers failed!\n");
}
//debug
// } else {
// printf("FPS: %d\n", fps());
// }
}
//OpenGLES-related:
/** @brief Error checking function
* @param file : string reference that contains the source file that the check is occuring in
* @param line : numeric reference that contains the line number that the check is occuring in
* @return : 0 if the function passed, else 1
*/
int8_t CheckGLESErrors( const char* file, uint16_t line )
{
EGLenum error;
const char* errortext;
const char* description;
error = eglGetError();
if (error != EGL_SUCCESS && error != 0)
{
switch (error)
{
case EGL_NOT_INITIALIZED:
errortext = "EGL_NOT_INITIALIZED.";
description = "EGL is not or could not be initialized, for the specified display.";
break;
case EGL_BAD_ACCESS:
errortext = "EGL_BAD_ACCESS EGL";
description = "Cannot access a requested resource (for example, a context is bound in another thread).";
break;
case EGL_BAD_ALLOC:
errortext = "EGL_BAD_ALLOC EGL";
description = "Failed to allocate resources for the requested operation.";
break;
case EGL_BAD_ATTRIBUTE:
errortext = "EGL_BAD_ATTRIBUTE";
description = "An unrecognized attribute or attribute value was passed in anattribute list.";
break;
case EGL_BAD_CONFIG:
errortext = "EGL_BAD_CONFIG";
description = "An EGLConfig argument does not name a valid EGLConfig.";
break;
case EGL_BAD_CONTEXT:
errortext = "EGL_BAD_CONTEXT";
description = "An EGLContext argument does not name a valid EGLContext.";
break;
case EGL_BAD_CURRENT_SURFACE:
errortext = "EGL_BAD_CURRENT_SURFACE";
description = "The current surface of the calling thread is a window, pbuffer,or pixmap that is no longer valid.";
break;
case EGL_BAD_DISPLAY:
errortext = "EGL_BAD_DISPLAY";
description = "An EGLDisplay argument does not name a valid EGLDisplay.";
break;
case EGL_BAD_MATCH:
errortext = "EGL_BAD_MATCH";
description = "Arguments are inconsistent; for example, an otherwise valid context requires buffers (e.g. depth or stencil) not allocated by an otherwise valid surface.";
break;
case EGL_BAD_NATIVE_PIXMAP:
errortext = "EGL_BAD_NATIVE_PIXMAP";
description = "An EGLNativePixmapType argument does not refer to a validnative pixmap.";
break;
case EGL_BAD_NATIVE_WINDOW:
errortext = "EGL_BAD_NATIVE_WINDOW";
description = "An EGLNativeWindowType argument does not refer to a validnative window.";
break;
case EGL_BAD_PARAMETER:
errortext = "EGL_BAD_PARAMETER";
description = "One or more argument values are invalid.";
break;
case EGL_BAD_SURFACE:
errortext = "EGL_BAD_SURFACE";
description = "An EGLSurface argument does not name a valid surface (window,pbuffer, or pixmap) configured for rendering";
break;
case EGL_CONTEXT_LOST:
errortext = "EGL_CONTEXT_LOST";
description = "A power management event has occurred. The application must destroy all contexts and reinitialise client API state and objects to continue rendering.";
break;
default:
errortext = "Unknown EGL Error";
description = "";
break;
}
printf( "OpenGLES ERROR: EGL Error detected in file %s at line %d: %s (0x%X)\n Description: %s\n", file, line, errortext, error, description );
return 1;
}
return 0;
}
//Initialize OpenGLES 1.1 (return 0 for success, 1 for error)
int initGLES()
{
// These settings are correct for the GCW0
EGLint egl_config_attr[] = {
////#if SCREEN_BPP > 16
//// // RGBA8888
// EGL_RED_SIZE, 8,
// EGL_BLUE_SIZE, 8,
// EGL_GREEN_SIZE, 8,
// EGL_ALPHA_SIZE, 8,
//// EGL_BUFFER_SIZE, 32,
////#else
//// // RGBA5650
//// EGL_RED_SIZE, 5,
//// EGL_BLUE_SIZE, 6,
//// EGL_GREEN_SIZE, 5,
//// EGL_ALPHA_SIZE, 0,
//// EGL_BUFFER_SIZE, 16,
////#endif
// // senquack TODO - Do we really even need a depth buffer?
// EGL_DEPTH_SIZE, 16,
////// EGL_DEPTH_SIZE, 0,
//// EGL_STENCIL_SIZE, 0,
// EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
// EGL_RENDERABLE_TYPE, EGL_OPENGL_ES_BIT, // We want OpenGLES 1, not 2
// EGL_NONE // This is important as it tells EGL when to stop looking in the array
//#if SCREEN_BPP > 16
// // RGBA8888
// EGL_RED_SIZE, 8,
// EGL_BLUE_SIZE, 8,
// EGL_GREEN_SIZE, 8,
// EGL_ALPHA_SIZE, 8,
// EGL_BUFFER_SIZE, 32,
//#else
// // RGBA5650
// EGL_RED_SIZE, 5,
// EGL_BLUE_SIZE, 6,
// EGL_GREEN_SIZE, 5,
// EGL_ALPHA_SIZE, 0,
// EGL_BUFFER_SIZE, 16,
//#endif
EGL_BUFFER_SIZE, SCREEN_BPP,
EGL_DEPTH_SIZE, 16,
EGL_STENCIL_SIZE, 0,
EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
// Don't really need this:
// EGL_RENDERABLE_TYPE, EGL_OPENGL_ES_BIT, // We want OpenGLES 1, not 2
EGL_NONE // This is important as it tells EGL when to stop looking in the array
};
EGLConfig egl_config;
EGLint num_configs_found;
EGLBoolean result;
EGLint eglMajorVer, eglMinorVer;
const char* output;
// Start up OpenGLES
printf( "OpenGLES: Opening EGL display\n" );
egl_screen.display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
if (egl_screen.display == EGL_NO_DISPLAY)
{
CheckGLESErrors( __FILE__, __LINE__ );
printf( "OpenGLES ERROR: Unable to create EGL display.\n" );
return 1;
}
printf( "OpenGLES: Initializing\n" );
result = eglInitialize(egl_screen.display, &eglMajorVer, &eglMinorVer );
if (result != EGL_TRUE )
{
CheckGLESErrors( __FILE__, __LINE__ );
printf( "OpenGLES ERROR: Unable to initialize EGL display.\n" );
return 1;
}
/* Get EGL Library Information */
printf( "EGL Implementation Version: Major %d Minor %d\n", eglMajorVer, eglMinorVer );
output = eglQueryString( egl_screen.display, EGL_VENDOR );
printf( "EGL_VENDOR: %s\n", output );
output = eglQueryString( egl_screen.display, EGL_VERSION );
printf( "EGL_VERSION: %s\n", output );
output = eglQueryString( egl_screen.display, EGL_EXTENSIONS );
printf( "EGL_EXTENSIONS: %s\n", output );
printf("OpenGLES: Requesting %dx%d %dbpp configuration\n", SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP);
result = eglChooseConfig(egl_screen.display, egl_config_attr, &egl_config, 1, &num_configs_found);
if (result != EGL_TRUE || num_configs_found == 0)
{
CheckGLESErrors( __FILE__, __LINE__ );
printf( "EGLport ERROR: Unable to query for available configs, found %d.\n", num_configs_found );
return 1;
}
printf( "EGLport: Found %d matching configs. Using first one.\n", num_configs_found );
printf( "OpenGLES: Creating context\n" );
egl_screen.context = eglCreateContext(egl_screen.display, egl_config, EGL_NO_CONTEXT, NULL);
if (egl_screen.context == EGL_NO_CONTEXT)
{
CheckGLESErrors( __FILE__, __LINE__ );
printf( "OpenGLES ERROR: Unable to create GLES context!\n");
return 1;
}
// For raw FBDEV on GCW0:
egl_screen.window = 0;
printf( "OpenGLES: Creating window surface\n" );
egl_screen.surface = eglCreateWindowSurface(egl_screen.display, egl_config, egl_screen.window, NULL);
if (egl_screen.surface == EGL_NO_SURFACE)
{
CheckGLESErrors( __FILE__, __LINE__ );
printf( "OpenGLES ERROR: Unable to create EGL surface!\n" );
return 1;
}
printf( "OpenGLES: Making current the new context\n" );
result = eglMakeCurrent(egl_screen.display, egl_screen.surface, egl_screen.surface, egl_screen.context);
if (result != EGL_TRUE)
{
CheckGLESErrors( __FILE__, __LINE__ );
printf( "OpenGLES ERROR: Unable to make GLES context current\n" );
return 1;
}
printf( "OpenGLES: Setting swap interval\n" );
eglSwapInterval(egl_screen.display, 1); // We want VSYNC
printf( "OpenGLES: Initialization complete\n" );
CheckGLESErrors( __FILE__, __LINE__ );
glViewport (0, 0, 320, 240);
//senquack - tried disabling:
glClearColor (0.0f, 0.0f, 0.0f, 0.0f);
glLineWidth(1);
glEnable(GL_LINE_SMOOTH);
glEnable (GL_BLEND);
// glBlendFunc(GL_SRC_ALPHA, GL_ONE); // original rr code had this
glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // GL docs say is best for transparency
glDisable (GL_LIGHTING);
glDisable (GL_CULL_FACE);
glDisable (GL_DEPTH_TEST);
glDisable (GL_TEXTURE_2D);
glDisable (GL_COLOR_MATERIAL);
//senquack - for OpenGLES, we enable these once and leave them enabled:
glEnableClientState (GL_VERTEX_ARRAY);
glEnableClientState (GL_COLOR_ARRAY);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// //DEBUG
// //new support for screen rotations:
// glMatrixMode(GL_MODELVIEW);
// //DEBUG:
// settings.rotated = SCREEN_ROTATED_RIGHT;
// if (settings.rotated == SCREEN_ROTATED_LEFT) {
// glRotatef (90.0f, 0.0f, 0.0f, 1.0f);
// glScalef(1.5f,1.34f,1.0f); // As close to perfect as I can manage
// } else if (settings.rotated == SCREEN_ROTATED_RIGHT) {
// glRotatef (-90.0f, 0.0f, 0.0f, 1.0f);
// glScalef(1.5f,1.34f,1.0f); // As close to perfect as I can manage
//// glScalef(.1,.1,0);
// }
// glPushMatrix();
//
//new support for screen rotations:
// glMatrixMode(GL_PROJECTION);
//DEBUG:
// settings.rotated = SCREEN_ROTATED_RIGHT;
// if (settings.rotated == SCREEN_ROTATED_LEFT) {
// glRotatef (-90.0, 0, 0, 1.0);
// } else if (settings.rotated == SCREEN_ROTATED_RIGHT) {
// glRotatef (90.0, 0, 0, 1.0);
// }
// glPushMatrix();
//senquack - TODO : gotta stop calling resize() all the time
resized (SCREEN_WIDTH, SCREEN_HEIGHT);
return 0;
}
// Close down OpenGLES (return 0 for success, 1 for error)
int closeGLES()
{
// //DEBUG rotation
// glMatrixMode(GL_MODELVIEW);
// glPopMatrix();
EGLBoolean result;
printf("Closing OpenGLES..\n");
result = eglMakeCurrent(egl_screen.display, NULL, NULL, EGL_NO_CONTEXT);
if (result != EGL_TRUE)
{
CheckGLESErrors( __FILE__, __LINE__ );
return 1;
}
printf("Destroying OpenGLES surface..\n");
result = eglDestroySurface(egl_screen.display, egl_screen.surface);
if (result != EGL_TRUE)
{
CheckGLESErrors( __FILE__, __LINE__ );
return 1;
}
printf("Destroying OpenGLES context..\n");
result = eglDestroyContext(egl_screen.display, egl_screen.context);
if (result != EGL_TRUE)
{
CheckGLESErrors( __FILE__, __LINE__ );
return 1;
}
egl_screen.surface = 0;
egl_screen.context = 0;
// egl_config = 0;
printf("Terminating OpenGLES display..\n");
result = eglTerminate(egl_screen.display);
if (result != EGL_TRUE)
{
CheckGLESErrors( __FILE__, __LINE__ );
return 1;
}
egl_screen.display = 0;
return 0;
}
// Reset viewport when the screen is resized.
//static void screenResized() {
// glViewport(0, 0, screenWidth, screenHeight);
// glMatrixMode(GL_PROJECTION);
// glLoadIdentity();
// gluPerspective(45.0f, (GLfloat)screenWidth/(GLfloat)screenHeight, 0.1f, FAR_PLANE);
// glMatrixMode(GL_MODELVIEW);
//}
static void
screenResized ()
{
//senquack - do we really need to call glViewport every single screen refresh!?
// glViewport(0, 0, screenWidth, screenHeight);
// glViewport(320-80, 0, 240, 320);
// glViewport(0, 0, 320, 240);
//senquack - border on left, flashing border on right, drawboard on top:
// glViewport(0, -80, 320 , 240);
//senquack - slight borders on top and bottom (no drawboards), shifted to right 80
// glViewport(-80, 0, 400, 320);
//senquack - shifted to the right 160 pixels, slight borders on top and bottom (top larger than bottom):
// glViewport(-80, 80, 420, 320);
//senquack - bottom border (maybe 40 pixels), left border (maybe 80 pixels) ,(squished on games' x axis 40 pixels)
// glViewport((int)(-80.0 * (400.0/320.0)), -80, 400, 320);
//senquack - left border (80 pixels maybe), top drawboard, good perspective on games' x axis
// glViewport(0,-80, 400, 320);
//still has left border of 80 pixels maybe, shifted left 80 pixels, bottom border of 40 pixels maybe
// glViewport((int)(-80.0 * (400.0/320.0)), -160, 400, 320);
// glViewport((int)(-80.0 * (400.0/320.0)), -80, 400, 320);
//senquack - almost there (squished 40 pix on game's x axis)
// glViewport((int)(-80.0 * (400.0/320.0)), 0, 400, 320);
//senquack - almost there, slight border on top and bottom:
// glViewport((int)(-80.0 * (400.0/320.0)), 0, 440, 320);
//senquack - close: (shifted a bit to the top)
// glViewport((int)(-90.0 * (400.0/320.0)), 0, 460, 320);
// if (screenRotated) {
//DEBUG
// if (settings.rotated) {
//// glViewport((int)(-87.0 * (400.0/320.0)), 0, 460, 320);
// glViewport ((int) (-90.0 * (400.0 / 320.0)), 0, 463, 320);
// } else {
// glViewport (0, 0, 320, 240);
glViewport (0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
// }
glMatrixMode (GL_PROJECTION);
//senquack - note: calling glLoadIdentity() first pushes the new matrix generated by gluPerspective onto the stack:
glLoadIdentity ();
// gluPerspective(45.0f, (GLfloat)screenWidth/(GLfloat)screenHeight, 0.1f, FAR_PLANE);
//senquack - TODO do we really need to call this every single screen refresh!?
#ifdef FIXEDMATH
gluPerspective(2949120, 87381, 6554, FAR_PLANE_X);
#else
//debug - THIS DID NOOOOT HELP, everything stretched top/bottom
// if (settings.rotated == SCREEN_ROTATED_LEFT) {
// gluPerspective(45.0f, (GLfloat)SCREEN_HEIGHT/(GLfloat)SCREEN_WIDTH, 0.1f, FAR_PLANE);
// } else if (settings.rotated == SCREEN_ROTATED_RIGHT) {
// gluPerspective(45.0f, (GLfloat)SCREEN_HEIGHT/(GLfloat)SCREEN_WIDTH, 0.1f, FAR_PLANE);
// } else {
//
gluPerspective(45.0f, (GLfloat)SCREEN_WIDTH/(GLfloat)SCREEN_HEIGHT, 0.1f, FAR_PLANE);
//}
//// gluPerspective(45.0f, 320.0f/240.0f, 0.1f, FAR_PLANE);
#endif //FIXEDMATH
glMatrixMode (GL_MODELVIEW);
}
//senquack - support screen rotation:
void
resized (int width, int height)
{
// screenWidth = width;
// screenHeight = height;
screenResized ();
}
//senquack - added our own copy:
//void gluPerspective(GLfloat fovy, GLfloat ratio, GLfloat near, GLfloat far)
//{
// GLfloat top = near * tan(fovy * M_PI/360.0f);
// GLfloat bottom = -top;
// GLfloat right = top * ratio;
// GLfloat left = -right;
//
// glFrustum(left, right, bottom, top, near, far);
//}
////senquack - speeding up for wiz:
//void gluPerspective(GLfloat fovy, GLfloat ratio, GLfloat near, GLfloat far)
//{
//// GLfloat top = near * tan(fovy * M_PI/360.0f);
// GLfloat top = near * tanf(fovy * 3.14159f/360.0f);
// GLfloat bottom = -top;
// GLfloat right = top * ratio;
// GLfloat left = -right;
//
// glFrustum(left, right, bottom, top, near, far);
//}
//senquack - fixed point version
//void gluPerspectivex(GLfixed fovy, GLfixed ratio, GLfixed near, GLfixed far)
//{
//// GLfloat top = near * tan(fovy * M_PI/360.0f);
//// GLfloat top = near * tanf(fovy * 3.14159f/360.0f);
//// GLfloat bottom = -top;
//// GLfloat right = top * ratio;
//// GLfloat left = -right;
//
//// GLfixed top = FMUL(near,INT2FNUM(tantbl[FMUL(fovy,572)]));
// GLfloat top = near * tanf(fovy * 3.14159f/360.0f);
// GLfixed bottom = -top;
// GLfixed right = FMUL(top, ratio);
// GLfixed left = -right;
//
//// glFrustum(left, right, bottom, top, near, far);
// glFrustumx(left, right, bottom, top, near, far);
//}
#ifdef FIXEDMATH
void gluPerspective(GLfixed fovy, GLfixed ratio, GLfixed near, GLfixed far)
{
// GLfloat top = near * tan(fovy * M_PI/360.0f);
// GLfloat top = near * tanf(fovy * 3.14159f/360.0f);
// GLfloat bottom = -top;
// GLfloat right = top * ratio;
// GLfloat left = -right;
// GLfixed top = FMUL(near,INT2FNUM(tantbl[FNUM2INT(FMUL(fovy,572))]));
// GLfloat top = near * tanf(fovy * 3.14159f/360.0f);
//senquack TODO - I renabled the below line vs the one below it, as it's probably faster.. check on this
GLfixed top = f2x(near * tanf(fovy * 3.14159f/360.0f));
// GLfixed top = FMUL (near, f2x (tanf (x2f (fovy) * 0.008726646f)));
GLfixed bottom = -top;
GLfixed right = FMUL (top, ratio);
GLfixed left = -right;
// glFrustum(left, right, bottom, top, near, far);
glFrustumx (left, right, bottom, top, near, far);
}
#else
void gluPerspective(GLfloat fovy, GLfloat ratio, GLfloat near, GLfloat far)
{
GLfloat top = near * tanf(fovy * 3.14159f/360.0f);
GLfloat bottom = -top;
GLfloat right = top * ratio;
GLfloat left = -right;
glFrustumf(left, right, bottom, top, near, far);
}
#endif //FIXEDMATH
//senquack - speeding up for wiz:
//void gluPerspective(GLfloat fovy, GLfloat ratio, GLfloat near, GLfloat far)
//{
//// GLfloat top = near * tan(fovy * M_PI/360.0f);
//// GLfloat top = near * tanf(fovy * 3.14159f/360.0f);
// GLfloat top = near * tanf(fovy * 0.008726646f);
// GLfloat bottom = -top;
// GLfloat right = top * ratio;
// GLfloat left = -right;
//
// //senquack - for Wiz, glFrustumf seems to be broken:
//// glFrustumf(left, right, bottom, top, near, far);
// glFrustumx(f2x(left), f2x(right), f2x(bottom), f2x(top), f2x(near), f2x(far));
//}
////senquack - added for Wiz (pulled from libglues)
//#define __glPi 3.14159265358979323846
//
////senquack - added for Wiz (pulled from libglues)
//static void __gluMakeIdentityf(GLfloat m[16])
//{
// m[0+4*0] = 1; m[0+4*1] = 0; m[0+4*2] = 0; m[0+4*3] = 0;
// m[1+4*0] = 0; m[1+4*1] = 1; m[1+4*2] = 0; m[1+4*3] = 0;
// m[2+4*0] = 0; m[2+4*1] = 0; m[2+4*2] = 1; m[2+4*3] = 0;
// m[3+4*0] = 0; m[3+4*1] = 0; m[3+4*2] = 0; m[3+4*3] = 1;
//}
//
////senquack - added for Wiz (pulled from libglues)
//static void gluPerspective(GLfloat fovy, GLfloat aspect, GLfloat zNear, GLfloat zFar)
//{
// GLfloat m[4][4];
// GLfloat sine, cotangent, deltaZ;
// GLfloat radians = fovy / 2 * __glPi / 180;
//
// deltaZ = zFar - zNear;
// sine = sin(radians);
// if ((deltaZ == 0) || (sine == 0) || (aspect == 0))
// {
// return;
// }
// cotangent = cos(radians) / sine;
//
// __gluMakeIdentityf(&m[0][0]);
// m[0][0] = cotangent / aspect;
// m[1][1] = cotangent;
// m[2][2] = -(zFar + zNear) / deltaZ;
// m[2][3] = -1;
// m[3][2] = -2 * zNear * zFar / deltaZ;
// m[3][3] = 0;
// glMultMatrixf(&m[0][0]);
//}
//senquack - 2/12
// Init OpenGL.
//static void initGL()
//{
// printf("Opening gpu940\n");
//
// //senquack
// glOpen(DEPTH_BUFFER); //Added for gpu940
//
// printf("Opened gpu940\n");
// glViewport(0, 0, screenWidth, screenHeight);
// glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
//
// glLineWidth(1);
// glEnable(GL_LINE_SMOOTH);
//
// glEnable(GL_BLEND);
// //glBlendFunc(GL_SRC_ALPHA, GL_ONE);
// glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
//
//
// //senquack
// //Added for gpu940
// glDepthMask(GL_TRUE);
//
// glDisable(GL_LIGHTING);
// glDisable(GL_CULL_FACE);
// glDisable(GL_DEPTH_TEST);
// glDisable(GL_TEXTURE_2D);
// glDisable(GL_COLOR_MATERIAL);
//
// resized(screenWidth, screenHeight);
//}
//// Init OpenGL.
//static void initGL ()
//{
//
// //senquack - trying to support rotated, zoomed screen
//// glViewport(0, 0, screenWidth, screenHeight);
//// glViewport(320-80, 0, 240, 320);
//
// //senquack - border on left, flashing border on right, drawboard on top:
//// glViewport(0, -80, 320 , 240);
// //senquack - slight borders on top and bottom (no drawboards), shifted to right 80
//// glViewport(-80, 0, 400, 320);
// //senquack - shifted to the right 160 pixels, slight borders on top and bottom (top larger than bottom):
//// glViewport(-80, 80, 400, 320);
// //senquack - bottom border (maybe 40 pixels), left border (maybe 80 pixels) ,(squished on games' x axis 40 pixels)
//// glViewport((int)(-80.0 * (400.0/320.0)), -80, 400, 320);
// //senquack - left border (80 pixels maybe), top drawboard
//// glViewport(0,-80, 400, 320);
// //still has left border of 80 pixels maybe, shifted left 80 pixels, bottom border of 40 pixels maybe
//// glViewport((int)(-80.0 * (400.0/320.0)), -160, 400, 320);
//// glViewport((int)(-80.0 * (400.0/320.0)), -80, 400, 320);
// //senquack - almost there (squished 40 pix on game's x axis)
//// glViewport((int)(-80.0 * (400.0/320.0)), 0, 400, 320);
// //senquack - almost there, slight border on top and bottom:
//// glViewport((int)(-80.0 * (400.0/320.0)), 0, 440, 320);
// //senquack - close: (shifted a bit to the top)
//// glViewport((int)(-90.0 * (400.0/320.0)), 0, 460, 320);
//// glViewport((int)(-87.0 * (400.0/320.0)), 0, 460, 320);
//
//// if (screenRotated) {
// if (settings.rotated) {
//// glViewport((int)(-87.0 * (400.0/320.0)), 0, 460, 320);
// glViewport ((int) (-90.0 * (400.0 / 320.0)), 0, 463, 320);
// } else {
// glViewport (0, 0, 320, 240);
// }
//
// //senquack - tried disabling:
// glClearColor (0.0f, 0.0f, 0.0f, 0.0f);
//
// glLineWidth(1);
// glEnable(GL_LINE_SMOOTH);
// glEnable (GL_BLEND);
// glBlendFunc(GL_SRC_ALPHA, GL_ONE); // original rr code had this
//// senquack - for some reason I changed it to this for Wiz/GP2X (opengl docs say it is better for transparency)
//// glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
// glDisable (GL_LIGHTING);
// glDisable (GL_CULL_FACE);
// glDisable (GL_DEPTH_TEST);
// glDisable (GL_TEXTURE_2D);
// glDisable (GL_COLOR_MATERIAL);
//
// //senquack - for OpenGLES, we enable these once and leave them enabled:
// glEnableClientState (GL_VERTEX_ARRAY);
// glEnableClientState (GL_COLOR_ARRAY);
// glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
// glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
//
// resized (screenWidth, screenHeight);
//
//// //senquack - temporary:
////// printf("sizeof(shapes) = %d\n", sizeof(shapes));
//// memset(shapes, sizeof(shapes),0);
//
//}
//// Load bitmaps and convert to textures.
void
loadGLTexture (char *fileName, GLuint * texture)
{
SDL_Surface *surface;
int mode; //The bit-depth of the texture.
//senquack: far too short for safety:
// char name[32];
char name[1024];
strcpy (name, SHARE_LOC);
strcat (name, "images/");
strcat (name, fileName);
surface = IMG_Load (name); //Changed by Albert... this will load any image (hopefully transparent PNGs)
if (!surface) {
fprintf (stderr, "Unable to load texture: %s\n", SDL_GetError ());
SDL_Quit ();
exit (1);
}
surface = conv_surf_gl (surface, surface->format->Amask
|| (surface->flags & SDL_SRCCOLORKEY));
/*
//Attempted hackery to make transparencies/color-keying work - Albert
SDL_PixelFormat RGBAFormat;
RGBAFormat.palette = 0; RGBAFormat.colorkey = 0; RGBAFormat.alpha = 0;
RGBAFormat.BitsPerPixel = 32; RGBAFormat.BytesPerPixel = 4;
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
RGBAFormat.Rmask = 0xFF000000; RGBAFormat.Rshift = 0; RGBAFormat.Rloss = 0;
RGBAFormat.Gmask = 0x00FF0000; RGBAFormat.Gshift = 8; RGBAFormat.Gloss = 0;
RGBAFormat.Bmask = 0x0000FF00; RGBAFormat.Bshift = 16; RGBAFormat.Bloss = 0;
RGBAFormat.Amask = 0x000000FF; RGBAFormat.Ashift = 24; RGBAFormat.Aloss = 0;
#else
RGBAFormat.Rmask = 0x000000FF; RGBAFormat.Rshift = 24; RGBAFormat.Rloss = 0;
RGBAFormat.Gmask = 0x0000FF00; RGBAFormat.Gshift = 16; RGBAFormat.Gloss = 0;
RGBAFormat.Bmask = 0x00FF0000; RGBAFormat.Bshift = 8; RGBAFormat.Bloss = 0;
RGBAFormat.Amask = 0xFF000000; RGBAFormat.Ashift = 0; RGBAFormat.Aloss = 0;
#endif
*/
/* Create the target alpha surface with correct color component ordering */
/*
SDL_Surface *alphaImage = SDL_CreateRGBSurface( SDL_SWSURFACE, surface->w,
surface->h, 32,
#if SDL_BYTEORDER == SDL_LIL_ENDIAN // OpenGL RGBA masks
0x000000FF,
0x0000FF00,
0x00FF0000,
0xFF000000
#else
0xFF000000,
0x00FF0000,
0x0000FF00,
0x000000FF
#endif
);
if (alphaImage == 0)
printf("ruh oh, alphaImage creation failed in loadGLTexture() (screen.c)\n");
*/
// Set up so that colorkey pixels become transparent :
/* Uint32 colorkey = SDL_MapRGBA(alphaImage->format, 255, 255, 0, 0); //R=255, G=255, B=0
SDL_FillRect(alphaImage, 0, colorkey);
colorkey = SDL_MapRGBA(surface->format, 255, 255, 0, 0 );
SDL_SetColorKey(surface, SDL_SRCCOLORKEY, colorkey);
SDL_Rect area;
// Copy the surface into the GL texture image :
area.x = 0;
area.y = 0;
area.w = surface->w;
area.h = surface->h;
SDL_BlitSurface(surface, &area, alphaImage, &area);
*/
/*
for (int i = 0; i < conv->w * conv->h; i++)
{
}
*/
//SDL_Surface *conv = SDL_ConvertSurface(surface, &RGBAFormat, SDL_SWSURFACE);
//http://osdl.sourceforge.net/OSDL/OSDL-0.3/src/doc/web/main/documentation/rendering/SDL-openGL-examples.html
//http://osdl.sourceforge.net/main/documentation/rendering/SDL-openGL.html --> Good explainations
// work out what format to tell glTexImage2D to use...
if (surface->format->BytesPerPixel == 3) { // RGB 24bit
mode = GL_RGB;
} else if (surface->format->BytesPerPixel == 4) { // RGBA 32bit
mode = GL_RGBA;
} else {
printf ("loadGLTexture: Error: Weird RGB mode found in surface.\n");
SDL_Quit ();
}
//Disabled for gpu940
glGenTextures (1, texture);
glBindTexture (GL_TEXTURE_2D, *texture);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
//senquack - shouldn't be using mipmap if we don't use it below anymore
// glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
//Disabled for gpu940
//gluBuild2DMipmaps(GL_TEXTURE_2D, 3, surface->w, surface->h, GL_RGB, GL_UNSIGNED_BYTE, surface->pixels);
if (surface == NULL) {
printf ("Error: surface NULL in loadGLTexture.\n");
SDL_Quit ();
}
//Added by Albert (somehow Kenta Cho managed to get OpenGL to render SDL textures, looks like)
glTexImage2D (GL_TEXTURE_2D, 0, mode, surface->w, surface->h, 0,
mode, GL_UNSIGNED_BYTE, surface->pixels);
//Added by Albert
if (surface)
SDL_FreeSurface (surface);
//if (alphaImage)
// SDL_FreeSurface(alphaImage);
//if (conv)
// SDL_FreeSurface(conv);
}
void
generateTexture (GLuint * texture)
{
glGenTextures (1, texture);
}
void
deleteTexture (GLuint * texture)
{
glDeleteTextures (1, texture);
}
static GLuint starTexture;
#define STAR_BMP "star.bmp"
static GLuint smokeTexture;
#define SMOKE_BMP "smoke.bmp"
static GLuint titleTexture;
#define TITLE_BMP "title.bmp"
int lowres = 0;
int windowMode = 0;
int brightness = DEFAULT_BRIGHTNESS;
//Uint8 *keys;
//int joystickMode = 1;
//SDL_Joystick *stick = NULL;
//senquack - GCW's analog stick
#ifdef GCW
SDL_Joystick *joy_analog = NULL;
#endif
//senquack - support screen rotation:
//void initSDL() {
// Uint32 videoFlags;
//
// if ( lowres ) {
// screenWidth = LOWRES_SCREEN_WIDTH;
// screenHeight = LOWRES_SCREEN_HEIGHT;
// } else {
// screenWidth = SCREEN_WIDTH;
// screenHeight = SCREEN_HEIGHT;
// }
//
//////senquack - for Wiz Opengl, re-enable this:
//// /* Initialize SDL */
//// if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
//// fprintf(stderr, "Unable to initialize SDL: %s\n", SDL_GetError());
//// exit(1);
//// }
//
// // if ( SDL_InitSubSystem(SDL_INIT_JOYSTICK) < 0 ) {
// if ( SDL_Init(SDL_INIT_JOYSTICK) < 0 ) {
// fprintf(stderr, "Unable to initialize SDL_JOYSTICK: %s\n", SDL_GetError());
// joystickMode = 0;
// exit(1);
// }
//
////Changed for gpu940:
// /* Create an OpenGL screen */
// //if ( windowMode ) {
//
// //videoFlags = SDL
// //videoFlags = SDL_OPENGL | SDL_RESIZABLE;
// //} else {
// //videoFlags = SDL_OPENGL | SDL_FULLSCREEN;
// //}
//
// /*if ( SDL_SetVideoMode(screenWidth, screenHeight, 16, videoFlags) == NULL ) {
// fprintf(stderr, "Unable to create OpenGL screen: %s\n", SDL_GetError());
// SDL_Quit();
// exit(2);
// }*/
//
////senquack - for Wiz Opengl, re-enable this:
////senquack - leaving OPENGL here causes segfault:
//// videoFlags = SDL_OPENGL | SDL_FULLSCREEN;
//// videoFlags = SDL_FULLSCREEN;
//// if ( SDL_SetVideoMode(screenWidth, screenHeight, 16, videoFlags) == NULL ) {
//// fprintf(stderr, "Unable to create OpenGL screen: %s\n", SDL_GetError());
//// SDL_Quit();
//// exit(2);
//// }
//
//// /* Select first display */
//// int status;
//// status=SDL_SelectVideoDisplay(0);
//// if (status<0)
//// {
//// fprintf(stderr, "Can't attach to first display: %s\n", SDL_GetError());
//// exit(-1);
//// }
////
////// window=SDL_CreateWindow("Nehe: SDL/OpenGL ES Tutorial, Lesson 02",
////// SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
////// WINDOW_WIDTH, WINDOW_HEIGHT,
////// SDL_WINDOW_RESIZABLE | SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);
//// window=SDL_CreateWindow("rRootage",
//// SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
//// 320, 240,
////// SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN | SDL_WINDOW_BORDERLESS | SDL_WINDOW_FULLSCREEN );
//// SDL_WINDOW_RESIZABLE | SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN | SDL_WINDOW_FULLSCREEN);
////// | SDL_WINDOW_FOREIGN);
//// if (window==0)
//// {
//// fprintf(stderr, "Can't create window: %s\n", SDL_GetError());
//// SDL_Quit();
//// }
////
//// glcontext=SDL_GL_CreateContext(window);
//// if (glcontext==NULL)
//// {
//// fprintf(stderr, "Can't create OpenGL ES context: %s\n", SDL_GetError());
//// SDL_Quit();
//// }
////
//// status=SDL_GL_MakeCurrent(window, glcontext);
//// if (status<0)
//// {
//// fprintf(stderr, "Can't set current OpenGL ES context: %s\n", SDL_GetError());
//// SDL_Quit();
//// }
////
//// /* Enable swap on VSYNC */
//// SDL_GL_SetSwapInterval(1);
//
// if (joystickMode == 1) {
// SDL_JoystickEventState(SDL_ENABLE);
// stick = SDL_JoystickOpen(0);
// }
//
// /* Set the title bar in environments that support it */
// SDL_WM_SetCaption(CAPTION, NULL);
//
////senquack - was left enabled for Wiz accidentally:
// initGL();
//
//// loadGLTexture(STAR_BMP, &starTexture);
//// loadGLTexture(SMOKE_BMP, &smokeTexture);
//// loadGLTexture(TITLE_BMP, &titleTexture);
//
// SDL_ShowCursor(SDL_DISABLE);
//
//}
void initSDL ()
{
//senquack
// if ( lowres ) {
// screenWidth = LOWRES_SCREEN_WIDTH;