android/project/jni/lesson05.c
author Paul Hunkin <paul@bieh.net>
Tue, 17 Aug 2010 15:35:56 +1200
changeset 4726 9076cdb027af
permissions -rw-r--r--
Cleanups.

- Moved to 'project' instead of 'testproject'
- Removed extraneous .c files
- Removed the android_libs folder (that was against the NDK agreement anyway)
paul@4726
     1
/*
paul@4726
     2
 * This code was created by Jeff Molofee '99 
paul@4726
     3
 * (ported to Linux/SDL by Ti Leggett '01)
paul@4726
     4
 *
paul@4726
     5
 * If you've found this code useful, please let me know.
paul@4726
     6
 *
paul@4726
     7
 * Visit Jeff at http://nehe.gamedev.net/
paul@4726
     8
 * 
paul@4726
     9
 * or for port-specific comments, questions, bugreports etc. 
paul@4726
    10
 * email to leggett@eecs.tulane.edu
paul@4726
    11
 */
paul@4726
    12
 
paul@4726
    13
#include <stdio.h>
paul@4726
    14
#include <stdlib.h>
paul@4726
    15
#include <math.h>
paul@4726
    16
paul@4726
    17
#include <signal.h>
paul@4726
    18
paul@4726
    19
#include <android/log.h>
paul@4726
    20
paul@4726
    21
paul@4726
    22
#ifdef ANDROID
paul@4726
    23
#include <GLES/gl.h>
paul@4726
    24
#else
paul@4726
    25
#include <GL/gl.h>
paul@4726
    26
#include <GL/glu.h>
paul@4726
    27
#endif
paul@4726
    28
#include "SDL.h"
paul@4726
    29
paul@4726
    30
/* screen width, height, and bit depth */
paul@4726
    31
#define SCREEN_WIDTH  320
paul@4726
    32
#define SCREEN_HEIGHT 430
paul@4726
    33
#define SCREEN_BPP     16
paul@4726
    34
paul@4726
    35
/* Define our booleans */
paul@4726
    36
#define TRUE  1
paul@4726
    37
#define FALSE 0
paul@4726
    38
paul@4726
    39
/* This is our SDL surface */
paul@4726
    40
SDL_Surface *surface;
paul@4726
    41
paul@4726
    42
int rotation = 0;
paul@4726
    43
paul@4726
    44
paul@4726
    45
/**************************************
paul@4726
    46
	gluperspective implementation
paul@4726
    47
**************************************/
paul@4726
    48
void gluPerspective(double fovy, double aspect, double zNear, double zFar){
paul@4726
    49
	glMatrixMode(GL_PROJECTION);
paul@4726
    50
	glLoadIdentity();
paul@4726
    51
	double xmin, xmax, ymin, ymax;
paul@4726
    52
	ymax = zNear * tan(fovy * M_PI / 360.0);
paul@4726
    53
	ymin = -ymax;
paul@4726
    54
	xmin = ymin * aspect;
paul@4726
    55
	xmax = ymax * aspect;
paul@4726
    56
	glFrustumf(xmin, xmax, ymin, ymax, zNear, zFar);
paul@4726
    57
}
paul@4726
    58
paul@4726
    59
paul@4726
    60
/**************************************
paul@4726
    61
	  glulookat implementation
paul@4726
    62
**************************************/
paul@4726
    63
void gluLookAt(GLfloat eyex, GLfloat eyey, GLfloat eyez,
paul@4726
    64
          GLfloat centerx, GLfloat centery, GLfloat centerz,
paul@4726
    65
          GLfloat upx, GLfloat upy, GLfloat upz)
paul@4726
    66
{
paul@4726
    67
    GLfloat m[16];
paul@4726
    68
    GLfloat x[3], y[3], z[3];
paul@4726
    69
    GLfloat mag;
paul@4726
    70
    
paul@4726
    71
    /* Make rotation matrix */
paul@4726
    72
    
paul@4726
    73
    /* Z vector */
paul@4726
    74
    z[0] = eyex - centerx;
paul@4726
    75
    z[1] = eyey - centery;
paul@4726
    76
    z[2] = eyez - centerz;
paul@4726
    77
    mag = sqrt(z[0] * z[0] + z[1] * z[1] + z[2] * z[2]);
paul@4726
    78
    if (mag) {          /* mpichler, 19950515 */
paul@4726
    79
        z[0] /= mag;
paul@4726
    80
        z[1] /= mag;
paul@4726
    81
        z[2] /= mag;
paul@4726
    82
    }
paul@4726
    83
    
paul@4726
    84
    /* Y vector */
paul@4726
    85
    y[0] = upx;
paul@4726
    86
    y[1] = upy;
paul@4726
    87
    y[2] = upz;
paul@4726
    88
    
paul@4726
    89
    /* X vector = Y cross Z */
paul@4726
    90
    x[0] = y[1] * z[2] - y[2] * z[1];
paul@4726
    91
    x[1] = -y[0] * z[2] + y[2] * z[0];
paul@4726
    92
    x[2] = y[0] * z[1] - y[1] * z[0];
paul@4726
    93
    
paul@4726
    94
    /* Recompute Y = Z cross X */
paul@4726
    95
    y[0] = z[1] * x[2] - z[2] * x[1];
paul@4726
    96
    y[1] = -z[0] * x[2] + z[2] * x[0];
paul@4726
    97
    y[2] = z[0] * x[1] - z[1] * x[0];
paul@4726
    98
    
paul@4726
    99
    /* mpichler, 19950515 */
paul@4726
   100
    /* cross product gives area of parallelogram, which is < 1.0 for
paul@4726
   101
     * non-perpendicular unit-length vectors; so normalize x, y here
paul@4726
   102
     */
paul@4726
   103
    
paul@4726
   104
    mag = sqrt(x[0] * x[0] + x[1] * x[1] + x[2] * x[2]);
paul@4726
   105
    if (mag) {
paul@4726
   106
        x[0] /= mag;
paul@4726
   107
        x[1] /= mag;
paul@4726
   108
        x[2] /= mag;
paul@4726
   109
    }
paul@4726
   110
    
paul@4726
   111
    mag = sqrt(y[0] * y[0] + y[1] * y[1] + y[2] * y[2]);
paul@4726
   112
    if (mag) {
paul@4726
   113
        y[0] /= mag;
paul@4726
   114
        y[1] /= mag;
paul@4726
   115
        y[2] /= mag;
paul@4726
   116
    }
paul@4726
   117
    
paul@4726
   118
#define M(row,col)  m[col*4+row]
paul@4726
   119
    M(0, 0) = x[0];
paul@4726
   120
    M(0, 1) = x[1];
paul@4726
   121
    M(0, 2) = x[2];
paul@4726
   122
    M(0, 3) = 0.0;
paul@4726
   123
    M(1, 0) = y[0];
paul@4726
   124
    M(1, 1) = y[1];
paul@4726
   125
    M(1, 2) = y[2];
paul@4726
   126
    M(1, 3) = 0.0;
paul@4726
   127
    M(2, 0) = z[0];
paul@4726
   128
    M(2, 1) = z[1];
paul@4726
   129
    M(2, 2) = z[2];
paul@4726
   130
    M(2, 3) = 0.0;
paul@4726
   131
    M(3, 0) = 0.0;
paul@4726
   132
    M(3, 1) = 0.0;
paul@4726
   133
    M(3, 2) = 0.0;
paul@4726
   134
    M(3, 3) = 1.0;
paul@4726
   135
#undef M
paul@4726
   136
    glMultMatrixf(m);
paul@4726
   137
    
paul@4726
   138
    /* Translate Eye to Origin */
paul@4726
   139
    glTranslatef(-eyex, -eyey, -eyez);
paul@4726
   140
    
paul@4726
   141
}
paul@4726
   142
paul@4726
   143
paul@4726
   144
paul@4726
   145
paul@4726
   146
paul@4726
   147
/* function to release/destroy our resources and restoring the old desktop */
paul@4726
   148
void Quit( int returnCode )
paul@4726
   149
{
paul@4726
   150
    /* clean up the window */
paul@4726
   151
    SDL_Quit( );
paul@4726
   152
paul@4726
   153
    /* and exit appropriately */
paul@4726
   154
    exit( returnCode );
paul@4726
   155
}
paul@4726
   156
paul@4726
   157
/* function to reset our viewport after a window resize */
paul@4726
   158
int resizeWindow( int width, int height )
paul@4726
   159
{
paul@4726
   160
    /* Height / width ration */
paul@4726
   161
    GLfloat ratio;
paul@4726
   162
 
paul@4726
   163
    /* Protect against a divide by zero */
paul@4726
   164
   if ( height == 0 )
paul@4726
   165
	height = 1;
paul@4726
   166
paul@4726
   167
    ratio = ( GLfloat )width / ( GLfloat )height;
paul@4726
   168
paul@4726
   169
    /* Setup our viewport. */
paul@4726
   170
    glViewport( 0, 0, ( GLsizei )width, ( GLsizei )height );
paul@4726
   171
paul@4726
   172
    /* change to the projection matrix and set our viewing volume. */
paul@4726
   173
    glMatrixMode( GL_PROJECTION );
paul@4726
   174
    glLoadIdentity( );
paul@4726
   175
paul@4726
   176
    /* Set our perspective */
paul@4726
   177
    gluPerspective( 45.0f, ratio, 0.1f, 100.0f );
paul@4726
   178
paul@4726
   179
    /* Make sure we're chaning the model view and not the projection */
paul@4726
   180
    glMatrixMode( GL_MODELVIEW );
paul@4726
   181
paul@4726
   182
    /* Reset The View */
paul@4726
   183
    glLoadIdentity( );
paul@4726
   184
paul@4726
   185
    return( TRUE );
paul@4726
   186
}
paul@4726
   187
paul@4726
   188
/* function to handle key press events */
paul@4726
   189
void handleKeyPress( SDL_keysym *keysym )
paul@4726
   190
{
paul@4726
   191
    switch ( keysym->sym )
paul@4726
   192
	{
paul@4726
   193
	case SDLK_ESCAPE:
paul@4726
   194
	    /* ESC key was pressed */
paul@4726
   195
	    Quit( 0 );
paul@4726
   196
	    break;
paul@4726
   197
	case SDLK_F1:
paul@4726
   198
	    /* F1 key was pressed
paul@4726
   199
	     * this toggles fullscreen mode
paul@4726
   200
	     */
paul@4726
   201
	    SDL_WM_ToggleFullScreen( surface );
paul@4726
   202
	    break;
paul@4726
   203
    case SDLK_LEFT:
paul@4726
   204
        rotation -= 30;
paul@4726
   205
        break;
paul@4726
   206
paul@4726
   207
    case SDLK_RIGHT:
paul@4726
   208
        rotation += 30;
paul@4726
   209
        break;
paul@4726
   210
        
paul@4726
   211
	default:
paul@4726
   212
	    break;
paul@4726
   213
	}
paul@4726
   214
paul@4726
   215
    __android_log_print(ANDROID_LOG_INFO, "SDL","Keycode: %d, %d, %d\n", keysym->sym, SDLK_LEFT, SDLK_RIGHT);
paul@4726
   216
paul@4726
   217
    return;
paul@4726
   218
}
paul@4726
   219
paul@4726
   220
/* general OpenGL initialization function */
paul@4726
   221
int initGL( GLvoid )
paul@4726
   222
{
paul@4726
   223
paul@4726
   224
    /* Enable smooth shading */
paul@4726
   225
    glShadeModel( GL_SMOOTH );
paul@4726
   226
paul@4726
   227
    /* Set the background black */
paul@4726
   228
    glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
paul@4726
   229
paul@4726
   230
    /* Depth buffer setup */
paul@4726
   231
    //glClearDepth( 1.0f );
paul@4726
   232
paul@4726
   233
    /* Enables Depth Testing */
paul@4726
   234
    glEnable( GL_DEPTH_TEST );
paul@4726
   235
paul@4726
   236
    /* The Type Of Depth Test To Do */
paul@4726
   237
    glDepthFunc( GL_LEQUAL );
paul@4726
   238
paul@4726
   239
    /* Really Nice Perspective Calculations */
paul@4726
   240
    glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST );
paul@4726
   241
paul@4726
   242
    return( TRUE );
paul@4726
   243
}
paul@4726
   244
paul@4726
   245
/* Here goes our drawing code */
paul@4726
   246
int drawGLScene( GLvoid )
paul@4726
   247
{
paul@4726
   248
      
paul@4726
   249
	static int Frames = 0;
paul@4726
   250
	static int T0 = 0;
paul@4726
   251
	
paul@4726
   252
    glViewport(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
paul@4726
   253
paul@4726
   254
    glClearColorx(0,0,0,255);
paul@4726
   255
    glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
paul@4726
   256
paul@4726
   257
    glMatrixMode(GL_PROJECTION);
paul@4726
   258
    glLoadIdentity();
paul@4726
   259
    gluPerspective(45, (float)SCREEN_WIDTH / SCREEN_HEIGHT, 0.5f, 150);
paul@4726
   260
paul@4726
   261
    glMatrixMode(GL_MODELVIEW);
paul@4726
   262
paul@4726
   263
    glLoadIdentity();
paul@4726
   264
paul@4726
   265
	//Camera
paul@4726
   266
	gluLookAt(0,0,5, 0,0,0, 0,1,0);
paul@4726
   267
			
paul@4726
   268
	//Draw a triangle
paul@4726
   269
	//glRotatef(iRot, 0, 1, 0);
paul@4726
   270
paul@4726
   271
	glRotatef( rotation, 0.0f, 1.0f, 0.0f );
paul@4726
   272
paul@4726
   273
paul@4726
   274
	glEnableClientState (GL_VERTEX_ARRAY);
paul@4726
   275
	glEnableClientState (GL_COLOR_ARRAY);
paul@4726
   276
	
paul@4726
   277
	/* Rotate The Triangle On The Y axis ( NEW ) */
paul@4726
   278
    //glRotatef( Frames % 360, 0.0f, 1.0f, 0.0f );
paul@4726
   279
paul@4726
   280
    /* GLES variant of drawing a triangle */
paul@4726
   281
    const GLfloat triVertices[][9] = {
paul@4726
   282
      {     /* Front Triangle */
paul@4726
   283
	 0.0f,  1.0f,  0.0f,               /* Top Of Triangle               */
paul@4726
   284
	-1.0f, -1.0f,  1.0f,               /* Left Of Triangle              */
paul@4726
   285
	 1.0f, -1.0f,  1.0f                /* Right Of Triangle             */
paul@4726
   286
      }, {  /* Right Triangle */
paul@4726
   287
	 0.0f,  1.0f,  0.0f,               /* Top Of Triangle               */
paul@4726
   288
	 1.0f, -1.0f,  1.0f,               /* Left Of Triangle              */
paul@4726
   289
	 1.0f, -1.0f, -1.0f                /* Right Of Triangle             */
paul@4726
   290
      }, {  /* Back Triangle */
paul@4726
   291
	 0.0f,  1.0f,  0.0f,               /* Top Of Triangle               */
paul@4726
   292
	 1.0f, -1.0f, -1.0f,               /* Left Of Triangle              */
paul@4726
   293
	-1.0f, -1.0f, -1.0f                /* Right Of Triangle             */
paul@4726
   294
      }, {  /* Left Triangle */
paul@4726
   295
	 0.0f,  1.0f,  0.0f,               /* Top Of Triangle               */
paul@4726
   296
	-1.0f, -1.0f, -1.0f,               /* Left Of Triangle              */
paul@4726
   297
	-1.0f, -1.0f,  1.0f                /* Right Of Triangle             */
paul@4726
   298
      }
paul@4726
   299
    };
paul@4726
   300
paul@4726
   301
    /* unlike GL, GLES does not support RGB. We have to use RGBA instead */
paul@4726
   302
    const GLfloat triColors[][12] = {
paul@4726
   303
      {     /* Front triangle */
paul@4726
   304
        1.0f, 0.0f, 0.0f, 1.0f,            /* Red                           */
paul@4726
   305
	0.0f, 1.0f, 0.0f, 1.0f,            /* Green                         */
paul@4726
   306
	0.0f, 0.0f, 1.0f, 1.0f             /* Blue                          */
paul@4726
   307
      }, {  /* Right triangle */
paul@4726
   308
        1.0f, 0.0f, 0.0f, 1.0f,            /* Red                           */
paul@4726
   309
	0.0f, 0.0f, 1.0f, 1.0f,            /* Blue                          */
paul@4726
   310
	0.0f, 1.0f, 0.0f, 1.0f             /* Green                         */
paul@4726
   311
      }, {  /* Back triangle */
paul@4726
   312
        1.0f, 0.0f, 0.0f, 1.0f,            /* Red                           */
paul@4726
   313
	0.0f, 1.0f, 0.0f, 1.0f,            /* Green                         */
paul@4726
   314
	0.0f, 0.0f, 1.0f, 1.0f             /* Blue                          */
paul@4726
   315
      }, {  /* Left triangle */
paul@4726
   316
        1.0f, 0.0f, 0.0f, 1.0f,            /* Red                           */
paul@4726
   317
	0.0f, 0.0f, 1.0f, 1.0f,            /* Blue                          */
paul@4726
   318
	0.0f, 1.0f, 0.0f, 1.0f             /* Green                         */
paul@4726
   319
      }
paul@4726
   320
    };
paul@4726
   321
paul@4726
   322
    glEnableClientState(GL_COLOR_ARRAY);
paul@4726
   323
paul@4726
   324
    int tri=0;
paul@4726
   325
paul@4726
   326
    /* Loop through all Triangles */
paul@4726
   327
    for(tri=0;tri<sizeof(triVertices)/(9*sizeof(GLfloat));tri++) 
paul@4726
   328
    {
paul@4726
   329
      glVertexPointer(3, GL_FLOAT, 0, triVertices[tri]);
paul@4726
   330
      glColorPointer(4, GL_FLOAT, 0, triColors[tri]);
paul@4726
   331
      
paul@4726
   332
      glDrawArrays(GL_TRIANGLE_STRIP, 0, 3);
paul@4726
   333
    }
paul@4726
   334
		
paul@4726
   335
    //__android_log_print(ANDROID_LOG_INFO, "SDL", "render %d", Frames++);
paul@4726
   336
paul@4726
   337
    /* Draw it to the screen */
paul@4726
   338
    SDL_GL_SwapBuffers( );
paul@4726
   339
paul@4726
   340
    /* Gather our frames per second */
paul@4726
   341
    Frames++;
paul@4726
   342
    {
paul@4726
   343
	GLint t = SDL_GetTicks();
paul@4726
   344
	if (t - T0 >= 5000) {
paul@4726
   345
	    GLfloat seconds = (t - T0) / 1000.0;
paul@4726
   346
	    GLfloat fps = Frames / seconds;
paul@4726
   347
	    __android_log_print(ANDROID_LOG_INFO, "SDL","%d frames in %g seconds = %g FPS\n", Frames, seconds, fps);
paul@4726
   348
	    T0 = t;
paul@4726
   349
	    Frames = 0;
paul@4726
   350
	}
paul@4726
   351
    }
paul@4726
   352
paul@4726
   353
    rotation++;
paul@4726
   354
paul@4726
   355
    return( TRUE );
paul@4726
   356
}
paul@4726
   357
paul@4726
   358
paul@4726
   359
struct
paul@4726
   360
{
paul@4726
   361
    SDL_AudioSpec spec;
paul@4726
   362
    Uint8 *sound;               /* Pointer to wave data */
paul@4726
   363
    Uint32 soundlen;            /* Length of wave data */
paul@4726
   364
    int soundpos;               /* Current play position */
paul@4726
   365
} wave;
paul@4726
   366
paul@4726
   367
void SDLCALL
paul@4726
   368
fillerup(void *unused, Uint8 * stream, int len)
paul@4726
   369
{
paul@4726
   370
    __android_log_print(ANDROID_LOG_INFO, "SDL","FILLERUP\n");
paul@4726
   371
    
paul@4726
   372
    Uint8 *waveptr;
paul@4726
   373
    int waveleft;
paul@4726
   374
paul@4726
   375
    /* Set up the pointers */
paul@4726
   376
    waveptr = wave.sound + wave.soundpos;
paul@4726
   377
    waveleft = wave.soundlen - wave.soundpos;
paul@4726
   378
paul@4726
   379
    /* Go! */
paul@4726
   380
    while (waveleft <= len) {
paul@4726
   381
        SDL_memcpy(stream, waveptr, waveleft);
paul@4726
   382
        stream += waveleft;
paul@4726
   383
        len -= waveleft;
paul@4726
   384
        waveptr = wave.sound;
paul@4726
   385
        waveleft = wave.soundlen;
paul@4726
   386
        wave.soundpos = 0;
paul@4726
   387
    }
paul@4726
   388
    SDL_memcpy(stream, waveptr, len);
paul@4726
   389
    wave.soundpos += len;
paul@4726
   390
}
paul@4726
   391
paul@4726
   392
void testAudio(){
paul@4726
   393
paul@4726
   394
    const char *file = "/sdcard/sample.wav";
paul@4726
   395
paul@4726
   396
    /* Load the SDL library */
paul@4726
   397
    if (SDL_Init(SDL_INIT_AUDIO) < 0) {
paul@4726
   398
        __android_log_print(ANDROID_LOG_INFO, "SDL","Couldn't initialize SDL Audio: %s\n", SDL_GetError());
paul@4726
   399
        return;
paul@4726
   400
    }else{
paul@4726
   401
        __android_log_print(ANDROID_LOG_INFO, "SDL","Init audio ok\n");
paul@4726
   402
    }
paul@4726
   403
paul@4726
   404
    /* Load the wave file into memory */
paul@4726
   405
    if (SDL_LoadWAV(file, &wave.spec, &wave.sound, &wave.soundlen) == NULL) {
paul@4726
   406
        __android_log_print(ANDROID_LOG_INFO, "SDL", "Couldn't load %s: %s\n", file, SDL_GetError());
paul@4726
   407
        return;
paul@4726
   408
    }
paul@4726
   409
paul@4726
   410
    wave.spec.callback = fillerup;
paul@4726
   411
paul@4726
   412
    __android_log_print(ANDROID_LOG_INFO, "SDL","Loaded: %d\n", wave.soundlen);
paul@4726
   413
paul@4726
   414
paul@4726
   415
    /* Initialize fillerup() variables */
paul@4726
   416
    if (SDL_OpenAudio(&wave.spec, NULL) < 0) {
paul@4726
   417
        __android_log_print(ANDROID_LOG_INFO, "SDL", "Couldn't open audio: %s\n", SDL_GetError());
paul@4726
   418
        SDL_FreeWAV(wave.sound);
paul@4726
   419
        return;
paul@4726
   420
    }
paul@4726
   421
paul@4726
   422
     __android_log_print(ANDROID_LOG_INFO, "SDL","Using audio driver: %s\n", SDL_GetCurrentAudioDriver());
paul@4726
   423
paul@4726
   424
    /* Let the audio run */
paul@4726
   425
    SDL_PauseAudio(0);
paul@4726
   426
paul@4726
   427
     __android_log_print(ANDROID_LOG_INFO, "SDL","Playing\n");
paul@4726
   428
    
paul@4726
   429
    while (SDL_GetAudioStatus() == SDL_AUDIO_PLAYING){
paul@4726
   430
         //__android_log_print(ANDROID_LOG_INFO, "SDL","Still playing\n");
paul@4726
   431
        SDL_Delay(100);
paul@4726
   432
    }
paul@4726
   433
paul@4726
   434
     __android_log_print(ANDROID_LOG_INFO, "SDL","Closing down\n");
paul@4726
   435
paul@4726
   436
    /* Clean up on signal */
paul@4726
   437
    SDL_CloseAudio();
paul@4726
   438
    SDL_FreeWAV(wave.sound);
paul@4726
   439
}
paul@4726
   440
paul@4726
   441
int SDL_main( int argc, char **argv )
paul@4726
   442
{
paul@4726
   443
paul@4726
   444
	__android_log_print(ANDROID_LOG_INFO, "SDL","entry\n");
paul@4726
   445
paul@4726
   446
    /* Flags to pass to SDL_SetVideoMode */
paul@4726
   447
    int videoFlags;
paul@4726
   448
    /* main loop variable */
paul@4726
   449
    int done = FALSE;
paul@4726
   450
    /* used to collect events */
paul@4726
   451
    SDL_Event event;
paul@4726
   452
    /* this holds some info about our display */
paul@4726
   453
    const SDL_VideoInfo *videoInfo;
paul@4726
   454
    /* whether or not the window is active */
paul@4726
   455
    int isActive = TRUE;
paul@4726
   456
paul@4726
   457
    /* initialize SDL */
paul@4726
   458
    if ( SDL_Init( SDL_INIT_VIDEO ) < 0 )
paul@4726
   459
	{
paul@4726
   460
	    __android_log_print(ANDROID_LOG_INFO, "SDL", "Video initialization failed: %s\n",
paul@4726
   461
		     SDL_GetError( ) );
paul@4726
   462
	    Quit( 1 );
paul@4726
   463
	}
paul@4726
   464
paul@4726
   465
    /* Fetch the video info */
paul@4726
   466
    videoInfo = SDL_GetVideoInfo( );
paul@4726
   467
paul@4726
   468
    if ( !videoInfo )
paul@4726
   469
	{
paul@4726
   470
	    __android_log_print(ANDROID_LOG_INFO, "SDL", "Video query failed: %s\n",
paul@4726
   471
		     SDL_GetError( ) );
paul@4726
   472
	    Quit( 1 );
paul@4726
   473
	}
paul@4726
   474
paul@4726
   475
    /* the flags to pass to SDL_SetVideoMode */
paul@4726
   476
    videoFlags  = SDL_OPENGL;          /* Enable OpenGL in SDL */
paul@4726
   477
    videoFlags |= SDL_GL_DOUBLEBUFFER; /* Enable double buffering */
paul@4726
   478
    videoFlags |= SDL_HWPALETTE;       /* Store the palette in hardware */
paul@4726
   479
    videoFlags |= SDL_RESIZABLE;       /* Enable window resizing */
paul@4726
   480
paul@4726
   481
    /* This checks to see if surfaces can be stored in memory */
paul@4726
   482
    if ( videoInfo->hw_available )
paul@4726
   483
	videoFlags |= SDL_HWSURFACE;
paul@4726
   484
    else
paul@4726
   485
	videoFlags |= SDL_SWSURFACE;
paul@4726
   486
paul@4726
   487
    /* This checks if hardware blits can be done */
paul@4726
   488
    if ( videoInfo->blit_hw )
paul@4726
   489
	videoFlags |= SDL_HWACCEL;
paul@4726
   490
paul@4726
   491
    /* Sets up OpenGL double buffering */
paul@4726
   492
    SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
paul@4726
   493
paul@4726
   494
    /* get a SDL surface */
paul@4726
   495
    surface = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP,
paul@4726
   496
				videoFlags );
paul@4726
   497
paul@4726
   498
    /* Verify there is a surface */
paul@4726
   499
    if ( !surface )
paul@4726
   500
	{
paul@4726
   501
	    __android_log_print(ANDROID_LOG_INFO, "SDL",  "Video mode set failed: %s\n", SDL_GetError( ) );
paul@4726
   502
	    Quit( 1 );
paul@4726
   503
	}
paul@4726
   504
paul@4726
   505
	__android_log_print(ANDROID_LOG_INFO, "SDL","Made a video mode!\n");
paul@4726
   506
paul@4726
   507
    /* initialize OpenGL */
paul@4726
   508
    initGL( );
paul@4726
   509
paul@4726
   510
    /* resize the initial window */
paul@4726
   511
    resizeWindow( SCREEN_WIDTH, SCREEN_HEIGHT );
paul@4726
   512
paul@4726
   513
paul@4726
   514
    //testAudio();
paul@4726
   515
paul@4726
   516
paul@4726
   517
    /* wait for events */ 
paul@4726
   518
    while ( !done )
paul@4726
   519
	{
paul@4726
   520
	    /* handle the events in the queue */
paul@4726
   521
paul@4726
   522
	    while ( SDL_PollEvent( &event ) )
paul@4726
   523
		{
paul@4726
   524
		    switch( event.type )
paul@4726
   525
			{
paul@4726
   526
			case SDL_ACTIVEEVENT:
paul@4726
   527
			    /* Something's happend with our focus
paul@4726
   528
			     * If we lost focus or we are iconified, we
paul@4726
   529
			     * shouldn't draw the screen
paul@4726
   530
			     */
paul@4726
   531
			    if ( event.active.gain == 0 )
paul@4726
   532
				isActive = FALSE;
paul@4726
   533
			    else
paul@4726
   534
				isActive = TRUE;
paul@4726
   535
			    break;			    
paul@4726
   536
			case SDL_VIDEORESIZE:
paul@4726
   537
			    /* handle resize event */
paul@4726
   538
			    surface = SDL_SetVideoMode( event.resize.w,
paul@4726
   539
							event.resize.h,
paul@4726
   540
							16, videoFlags );
paul@4726
   541
			    if ( !surface )
paul@4726
   542
				{
paul@4726
   543
				    __android_log_print(ANDROID_LOG_INFO, "SDL","Could not get a surface after resize: %s\n", SDL_GetError( ) );
paul@4726
   544
				    Quit( 1 );
paul@4726
   545
				}
paul@4726
   546
			    resizeWindow( event.resize.w, event.resize.h );
paul@4726
   547
			    break;
paul@4726
   548
			case SDL_KEYDOWN:
paul@4726
   549
			    /* handle key presses */
paul@4726
   550
			    handleKeyPress( &event.key.keysym );
paul@4726
   551
			    break;
paul@4726
   552
			case SDL_QUIT:
paul@4726
   553
			    /* handle quit requests */
paul@4726
   554
			    done = TRUE;
paul@4726
   555
			    __android_log_print(ANDROID_LOG_INFO, "SDL","App is shutting down\n");
paul@4726
   556
			    break;
paul@4726
   557
			default:
paul@4726
   558
			    break;
paul@4726
   559
			}
paul@4726
   560
		}
paul@4726
   561
paul@4726
   562
	    /* draw the scene */
paul@4726
   563
	    if ( isActive )
paul@4726
   564
		drawGLScene( );
paul@4726
   565
	}
paul@4726
   566
paul@4726
   567
    /* clean ourselves up and exit */
paul@4726
   568
    Quit( 0 );
paul@4726
   569
paul@4726
   570
    /* Should never get here */
paul@4726
   571
    return( 0 );
paul@4726
   572
}
paul@4726
   573
paul@4726
   574