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

Latest commit

 

History

History
574 lines (453 loc) · 14.5 KB

File metadata and controls

574 lines (453 loc) · 14.5 KB
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/*
* This code was created by Jeff Molofee '99
* (ported to Linux/SDL by Ti Leggett '01)
*
* If you've found this code useful, please let me know.
*
* Visit Jeff at http://nehe.gamedev.net/
*
* or for port-specific comments, questions, bugreports etc.
* email to leggett@eecs.tulane.edu
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
Jul 27, 2010
Jul 27, 2010
17
18
#include <signal.h>
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <android/log.h>
#ifdef ANDROID
#include <GLES/gl.h>
#else
#include <GL/gl.h>
#include <GL/glu.h>
#endif
#include "SDL.h"
/* screen width, height, and bit depth */
#define SCREEN_WIDTH 320
Jul 27, 2010
Jul 27, 2010
32
#define SCREEN_HEIGHT 430
33
34
35
36
37
38
39
40
41
#define SCREEN_BPP 16
/* Define our booleans */
#define TRUE 1
#define FALSE 0
/* This is our SDL surface */
SDL_Surface *surface;
Jul 6, 2010
Jul 6, 2010
42
43
int rotation = 0;
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
/**************************************
gluperspective implementation
**************************************/
void gluPerspective(double fovy, double aspect, double zNear, double zFar){
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
double xmin, xmax, ymin, ymax;
ymax = zNear * tan(fovy * M_PI / 360.0);
ymin = -ymax;
xmin = ymin * aspect;
xmax = ymax * aspect;
glFrustumf(xmin, xmax, ymin, ymax, zNear, zFar);
}
/**************************************
glulookat implementation
**************************************/
void gluLookAt(GLfloat eyex, GLfloat eyey, GLfloat eyez,
GLfloat centerx, GLfloat centery, GLfloat centerz,
GLfloat upx, GLfloat upy, GLfloat upz)
{
GLfloat m[16];
GLfloat x[3], y[3], z[3];
GLfloat mag;
/* Make rotation matrix */
/* Z vector */
z[0] = eyex - centerx;
z[1] = eyey - centery;
z[2] = eyez - centerz;
mag = sqrt(z[0] * z[0] + z[1] * z[1] + z[2] * z[2]);
if (mag) { /* mpichler, 19950515 */
z[0] /= mag;
z[1] /= mag;
z[2] /= mag;
}
/* Y vector */
y[0] = upx;
y[1] = upy;
y[2] = upz;
/* X vector = Y cross Z */
x[0] = y[1] * z[2] - y[2] * z[1];
x[1] = -y[0] * z[2] + y[2] * z[0];
x[2] = y[0] * z[1] - y[1] * z[0];
/* Recompute Y = Z cross X */
y[0] = z[1] * x[2] - z[2] * x[1];
y[1] = -z[0] * x[2] + z[2] * x[0];
y[2] = z[0] * x[1] - z[1] * x[0];
/* mpichler, 19950515 */
/* cross product gives area of parallelogram, which is < 1.0 for
* non-perpendicular unit-length vectors; so normalize x, y here
*/
mag = sqrt(x[0] * x[0] + x[1] * x[1] + x[2] * x[2]);
if (mag) {
x[0] /= mag;
x[1] /= mag;
x[2] /= mag;
}
mag = sqrt(y[0] * y[0] + y[1] * y[1] + y[2] * y[2]);
if (mag) {
y[0] /= mag;
y[1] /= mag;
y[2] /= mag;
}
#define M(row,col) m[col*4+row]
M(0, 0) = x[0];
M(0, 1) = x[1];
M(0, 2) = x[2];
M(0, 3) = 0.0;
M(1, 0) = y[0];
M(1, 1) = y[1];
M(1, 2) = y[2];
M(1, 3) = 0.0;
M(2, 0) = z[0];
M(2, 1) = z[1];
M(2, 2) = z[2];
M(2, 3) = 0.0;
M(3, 0) = 0.0;
M(3, 1) = 0.0;
M(3, 2) = 0.0;
M(3, 3) = 1.0;
#undef M
glMultMatrixf(m);
/* Translate Eye to Origin */
glTranslatef(-eyex, -eyey, -eyez);
}
/* function to release/destroy our resources and restoring the old desktop */
void Quit( int returnCode )
{
/* clean up the window */
SDL_Quit( );
/* and exit appropriately */
exit( returnCode );
}
/* function to reset our viewport after a window resize */
int resizeWindow( int width, int height )
{
/* Height / width ration */
GLfloat ratio;
/* Protect against a divide by zero */
if ( height == 0 )
height = 1;
ratio = ( GLfloat )width / ( GLfloat )height;
/* Setup our viewport. */
glViewport( 0, 0, ( GLsizei )width, ( GLsizei )height );
/* change to the projection matrix and set our viewing volume. */
glMatrixMode( GL_PROJECTION );
glLoadIdentity( );
/* Set our perspective */
gluPerspective( 45.0f, ratio, 0.1f, 100.0f );
/* Make sure we're chaning the model view and not the projection */
glMatrixMode( GL_MODELVIEW );
/* Reset The View */
glLoadIdentity( );
return( TRUE );
}
/* function to handle key press events */
void handleKeyPress( SDL_keysym *keysym )
{
switch ( keysym->sym )
{
case SDLK_ESCAPE:
/* ESC key was pressed */
Quit( 0 );
break;
case SDLK_F1:
/* F1 key was pressed
* this toggles fullscreen mode
*/
SDL_WM_ToggleFullScreen( surface );
break;
Jul 6, 2010
Jul 6, 2010
203
204
205
206
207
208
209
210
case SDLK_LEFT:
rotation -= 30;
break;
case SDLK_RIGHT:
rotation += 30;
break;
211
212
213
214
default:
break;
}
Jul 6, 2010
Jul 6, 2010
215
216
__android_log_print(ANDROID_LOG_INFO, "SDL","Keycode: %d, %d, %d\n", keysym->sym, SDLK_LEFT, SDLK_RIGHT);
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
return;
}
/* general OpenGL initialization function */
int initGL( GLvoid )
{
/* Enable smooth shading */
glShadeModel( GL_SMOOTH );
/* Set the background black */
glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
/* Depth buffer setup */
//glClearDepth( 1.0f );
/* Enables Depth Testing */
glEnable( GL_DEPTH_TEST );
/* The Type Of Depth Test To Do */
glDepthFunc( GL_LEQUAL );
/* Really Nice Perspective Calculations */
glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST );
return( TRUE );
}
/* Here goes our drawing code */
int drawGLScene( GLvoid )
{
Jul 6, 2010
Jul 6, 2010
248
249
250
251
252
253
static int Frames = 0;
static int T0 = 0;
glViewport(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
Jun 28, 2010
Jun 28, 2010
254
glClearColorx(0,0,0,255);
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45, (float)SCREEN_WIDTH / SCREEN_HEIGHT, 0.5f, 150);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
//Camera
gluLookAt(0,0,5, 0,0,0, 0,1,0);
//Draw a triangle
//glRotatef(iRot, 0, 1, 0);
Jul 6, 2010
Jul 6, 2010
271
glRotatef( rotation, 0.0f, 1.0f, 0.0f );
272
273
274
275
276
277
glEnableClientState (GL_VERTEX_ARRAY);
glEnableClientState (GL_COLOR_ARRAY);
/* Rotate The Triangle On The Y axis ( NEW ) */
Jul 6, 2010
Jul 6, 2010
278
//glRotatef( Frames % 360, 0.0f, 1.0f, 0.0f );
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
/* GLES variant of drawing a triangle */
const GLfloat triVertices[][9] = {
{ /* Front Triangle */
0.0f, 1.0f, 0.0f, /* Top Of Triangle */
-1.0f, -1.0f, 1.0f, /* Left Of Triangle */
1.0f, -1.0f, 1.0f /* Right Of Triangle */
}, { /* Right Triangle */
0.0f, 1.0f, 0.0f, /* Top Of Triangle */
1.0f, -1.0f, 1.0f, /* Left Of Triangle */
1.0f, -1.0f, -1.0f /* Right Of Triangle */
}, { /* Back Triangle */
0.0f, 1.0f, 0.0f, /* Top Of Triangle */
1.0f, -1.0f, -1.0f, /* Left Of Triangle */
-1.0f, -1.0f, -1.0f /* Right Of Triangle */
}, { /* Left Triangle */
0.0f, 1.0f, 0.0f, /* Top Of Triangle */
-1.0f, -1.0f, -1.0f, /* Left Of Triangle */
-1.0f, -1.0f, 1.0f /* Right Of Triangle */
}
};
/* unlike GL, GLES does not support RGB. We have to use RGBA instead */
const GLfloat triColors[][12] = {
{ /* Front triangle */
1.0f, 0.0f, 0.0f, 1.0f, /* Red */
0.0f, 1.0f, 0.0f, 1.0f, /* Green */
0.0f, 0.0f, 1.0f, 1.0f /* Blue */
}, { /* Right triangle */
1.0f, 0.0f, 0.0f, 1.0f, /* Red */
0.0f, 0.0f, 1.0f, 1.0f, /* Blue */
0.0f, 1.0f, 0.0f, 1.0f /* Green */
}, { /* Back triangle */
1.0f, 0.0f, 0.0f, 1.0f, /* Red */
0.0f, 1.0f, 0.0f, 1.0f, /* Green */
0.0f, 0.0f, 1.0f, 1.0f /* Blue */
}, { /* Left triangle */
1.0f, 0.0f, 0.0f, 1.0f, /* Red */
0.0f, 0.0f, 1.0f, 1.0f, /* Blue */
0.0f, 1.0f, 0.0f, 1.0f /* Green */
}
};
glEnableClientState(GL_COLOR_ARRAY);
int tri=0;
/* Loop through all Triangles */
for(tri=0;tri<sizeof(triVertices)/(9*sizeof(GLfloat));tri++)
{
glVertexPointer(3, GL_FLOAT, 0, triVertices[tri]);
glColorPointer(4, GL_FLOAT, 0, triColors[tri]);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 3);
}
//__android_log_print(ANDROID_LOG_INFO, "SDL", "render %d", Frames++);
/* Draw it to the screen */
SDL_GL_SwapBuffers( );
/* Gather our frames per second */
Frames++;
{
GLint t = SDL_GetTicks();
if (t - T0 >= 5000) {
GLfloat seconds = (t - T0) / 1000.0;
GLfloat fps = Frames / seconds;
__android_log_print(ANDROID_LOG_INFO, "SDL","%d frames in %g seconds = %g FPS\n", Frames, seconds, fps);
T0 = t;
Frames = 0;
}
}
Jul 27, 2010
Jul 27, 2010
353
rotation++;
Jun 28, 2010
Jun 28, 2010
354
355
356
357
return( TRUE );
}
Jul 27, 2010
Jul 27, 2010
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
struct
{
SDL_AudioSpec spec;
Uint8 *sound; /* Pointer to wave data */
Uint32 soundlen; /* Length of wave data */
int soundpos; /* Current play position */
} wave;
void SDLCALL
fillerup(void *unused, Uint8 * stream, int len)
{
__android_log_print(ANDROID_LOG_INFO, "SDL","FILLERUP\n");
Uint8 *waveptr;
int waveleft;
/* Set up the pointers */
waveptr = wave.sound + wave.soundpos;
waveleft = wave.soundlen - wave.soundpos;
/* Go! */
while (waveleft <= len) {
SDL_memcpy(stream, waveptr, waveleft);
stream += waveleft;
len -= waveleft;
waveptr = wave.sound;
waveleft = wave.soundlen;
wave.soundpos = 0;
}
SDL_memcpy(stream, waveptr, len);
wave.soundpos += len;
}
void testAudio(){
const char *file = "/sdcard/sample.wav";
/* Load the SDL library */
if (SDL_Init(SDL_INIT_AUDIO) < 0) {
__android_log_print(ANDROID_LOG_INFO, "SDL","Couldn't initialize SDL Audio: %s\n", SDL_GetError());
return;
}else{
__android_log_print(ANDROID_LOG_INFO, "SDL","Init audio ok\n");
}
/* Load the wave file into memory */
if (SDL_LoadWAV(file, &wave.spec, &wave.sound, &wave.soundlen) == NULL) {
__android_log_print(ANDROID_LOG_INFO, "SDL", "Couldn't load %s: %s\n", file, SDL_GetError());
return;
}
wave.spec.callback = fillerup;
__android_log_print(ANDROID_LOG_INFO, "SDL","Loaded: %d\n", wave.soundlen);
/* Initialize fillerup() variables */
if (SDL_OpenAudio(&wave.spec, NULL) < 0) {
__android_log_print(ANDROID_LOG_INFO, "SDL", "Couldn't open audio: %s\n", SDL_GetError());
SDL_FreeWAV(wave.sound);
return;
}
__android_log_print(ANDROID_LOG_INFO, "SDL","Using audio driver: %s\n", SDL_GetCurrentAudioDriver());
/* Let the audio run */
SDL_PauseAudio(0);
__android_log_print(ANDROID_LOG_INFO, "SDL","Playing\n");
while (SDL_GetAudioStatus() == SDL_AUDIO_PLAYING){
//__android_log_print(ANDROID_LOG_INFO, "SDL","Still playing\n");
//SDL_Delay(100);
}
__android_log_print(ANDROID_LOG_INFO, "SDL","Closing down\n");
/* Clean up on signal */
SDL_CloseAudio();
SDL_FreeWAV(wave.sound);
}
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
int SDL_main( int argc, char **argv )
{
__android_log_print(ANDROID_LOG_INFO, "SDL","entry\n");
/* Flags to pass to SDL_SetVideoMode */
int videoFlags;
/* main loop variable */
int done = FALSE;
/* used to collect events */
SDL_Event event;
/* this holds some info about our display */
const SDL_VideoInfo *videoInfo;
/* whether or not the window is active */
int isActive = TRUE;
/* initialize SDL */
if ( SDL_Init( SDL_INIT_VIDEO ) < 0 )
{
__android_log_print(ANDROID_LOG_INFO, "SDL", "Video initialization failed: %s\n",
SDL_GetError( ) );
Quit( 1 );
}
/* Fetch the video info */
videoInfo = SDL_GetVideoInfo( );
if ( !videoInfo )
{
__android_log_print(ANDROID_LOG_INFO, "SDL", "Video query failed: %s\n",
SDL_GetError( ) );
Quit( 1 );
}
/* the flags to pass to SDL_SetVideoMode */
videoFlags = SDL_OPENGL; /* Enable OpenGL in SDL */
videoFlags |= SDL_GL_DOUBLEBUFFER; /* Enable double buffering */
videoFlags |= SDL_HWPALETTE; /* Store the palette in hardware */
videoFlags |= SDL_RESIZABLE; /* Enable window resizing */
/* This checks to see if surfaces can be stored in memory */
if ( videoInfo->hw_available )
videoFlags |= SDL_HWSURFACE;
else
videoFlags |= SDL_SWSURFACE;
/* This checks if hardware blits can be done */
if ( videoInfo->blit_hw )
videoFlags |= SDL_HWACCEL;
/* Sets up OpenGL double buffering */
SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
/* get a SDL surface */
surface = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP,
videoFlags );
/* Verify there is a surface */
if ( !surface )
{
__android_log_print(ANDROID_LOG_INFO, "SDL", "Video mode set failed: %s\n", SDL_GetError( ) );
Quit( 1 );
}
__android_log_print(ANDROID_LOG_INFO, "SDL","Made a video mode!\n");
/* initialize OpenGL */
initGL( );
/* resize the initial window */
resizeWindow( SCREEN_WIDTH, SCREEN_HEIGHT );
Jul 27, 2010
Jul 27, 2010
513
Jul 27, 2010
Jul 27, 2010
514
//testAudio();
Jul 27, 2010
Jul 27, 2010
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
/* wait for events */
while ( !done )
{
/* handle the events in the queue */
while ( SDL_PollEvent( &event ) )
{
switch( event.type )
{
case SDL_ACTIVEEVENT:
/* Something's happend with our focus
* If we lost focus or we are iconified, we
* shouldn't draw the screen
*/
if ( event.active.gain == 0 )
isActive = FALSE;
else
isActive = TRUE;
break;
case SDL_VIDEORESIZE:
/* handle resize event */
surface = SDL_SetVideoMode( event.resize.w,
event.resize.h,
16, videoFlags );
if ( !surface )
{
__android_log_print(ANDROID_LOG_INFO, "SDL","Could not get a surface after resize: %s\n", SDL_GetError( ) );
Quit( 1 );
}
resizeWindow( event.resize.w, event.resize.h );
break;
case SDL_KEYDOWN:
/* handle key presses */
handleKeyPress( &event.key.keysym );
break;
case SDL_QUIT:
/* handle quit requests */
done = TRUE;
Jul 27, 2010
Jul 27, 2010
555
__android_log_print(ANDROID_LOG_INFO, "SDL","App is shutting down\n");
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
break;
default:
break;
}
}
/* draw the scene */
if ( isActive )
drawGLScene( );
}
/* clean ourselves up and exit */
Quit( 0 );
/* Should never get here */
return( 0 );
}