3
* (c) Michael Vance, 2000
4
* briareos@lokigames.com
6
* Distributed under terms of the LGPL.
14
#include "emscripten.h"
23
#define emColor4ubv(x)
25
#define emColor4ubv(x) glColor4ubv(x)
28
static GLboolean should_rotate = GL_TRUE;
30
static void quit_tutorial( int code )
33
* Quit SDL so we can release the fullscreen
34
* mode and restore the previous video settings,
43
static void handle_key_down( SDL_keysym* keysym )
47
* We're only interested if 'Esc' has
51
* Handle the arrow keys and have that change the
52
* viewing position/angle.
54
switch( keysym->sym ) {
59
should_rotate = !should_rotate;
67
static void process_events( void )
69
/* Our SDL event placeholder. */
72
/* Grab all the events off the queue. */
73
while( SDL_PollEvent( &event ) ) {
75
switch( event.type ) {
77
/* Handle key presses. */
78
handle_key_down( &event.key.keysym );
81
/* Handle quit requests (like Ctrl-c). */
90
static void draw_screen( void )
92
/* Our angle of rotation. */
93
static float angle = 0.0f;
97
* Replace this awful mess with vertex
98
* arrays and a call to glDrawElements.
101
* After completing the above, change
102
* it to use compiled vertex arrays.
105
* Verify my windings are correct here ;).
107
static GLfloat v0[] = { -1.0f, -1.0f, 1.0f };
108
static GLfloat v1[] = { 1.0f, -1.0f, 1.0f };
109
static GLfloat v2[] = { 1.0f, 1.0f, 1.0f };
110
static GLfloat v3[] = { -1.0f, 1.0f, 1.0f };
111
static GLfloat v4[] = { -1.0f, -1.0f, -1.0f };
112
static GLfloat v5[] = { 1.0f, -1.0f, -1.0f };
113
static GLfloat v6[] = { 1.0f, 1.0f, -1.0f };
114
static GLfloat v7[] = { -1.0f, 1.0f, -1.0f };
115
static GLubyte red[] = { 255, 0, 0, 255 };
116
static GLubyte green[] = { 0, 255, 0, 255 };
117
static GLubyte blue[] = { 0, 0, 255, 255 };
118
static GLubyte white[] = { 255, 255, 255, 255 };
119
static GLubyte yellow[] = { 0, 255, 255, 255 };
120
static GLubyte black[] = { 0, 0, 0, 255 };
121
static GLubyte orange[] = { 255, 255, 0, 255 };
122
static GLubyte purple[] = { 255, 0, 255, 0 };
124
/* Clear the color and depth buffers. */
125
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
127
/* We don't want to modify the projection matrix. */
128
glMatrixMode( GL_MODELVIEW );
131
/* Move down the z-axis. */
132
glTranslatef( 0.0, 0.0, -5.0 );
135
glRotatef( angle, 0.0, 1.0, 0.0 );
137
if( should_rotate ) {
139
if( ++angle > 360.0f ) {
145
/* Send our triangle data to the pipeline. */
146
glBegin( GL_TRIANGLES );
150
emColor4ubv( green );
159
emColor4ubv( white );
162
emColor4ubv( green );
164
emColor4ubv( black );
166
emColor4ubv( orange );
169
emColor4ubv( green );
171
emColor4ubv( orange );
176
emColor4ubv( black );
178
emColor4ubv( yellow );
180
emColor4ubv( purple );
183
emColor4ubv( black );
185
emColor4ubv( purple );
187
emColor4ubv( orange );
190
emColor4ubv( yellow );
194
emColor4ubv( white );
197
emColor4ubv( yellow );
199
emColor4ubv( white );
201
emColor4ubv( purple );
204
emColor4ubv( white );
208
emColor4ubv( orange );
211
emColor4ubv( white );
213
emColor4ubv( orange );
215
emColor4ubv( purple );
218
emColor4ubv( green );
222
emColor4ubv( yellow );
225
emColor4ubv( green );
227
emColor4ubv( yellow );
229
emColor4ubv( black );
236
* Draw text telling the user that 'Spc'
237
* pauses the rotation and 'Esc' quits.
238
* Do it using vetors and textured quads.
242
* Swap the buffers. This this tells the driver to
243
* render the next frame from the contents of the
244
* back-buffer, and to set all rendering operations
245
* to occur on what was the front-buffer.
247
* Double buffering prevents nasty visual tearing
248
* from the application drawing on areas of the
249
* screen that are being updated at the same time.
251
SDL_GL_SwapBuffers( );
254
static void setup_opengl( int width, int height )
256
float ratio = (float) width / (float) height;
258
/* Our shading model--Gouraud (smooth). */
259
glShadeModel( GL_SMOOTH );
262
glCullFace( GL_BACK );
263
glFrontFace( GL_CCW );
264
glEnable( GL_CULL_FACE );
266
/* Set the clear color. */
267
glClearColor( 0, 0, 0, 0 );
269
/* Setup our viewport. */
270
glViewport( 0, 0, width, height );
273
* Change to the projection matrix and set
274
* our viewing volume.
276
glMatrixMode( GL_PROJECTION );
280
* Replace this with a call to glFrustum.
282
gluPerspective( 60.0, ratio, 1.0, 1024.0 );
288
/* Draw the screen. */
292
int main( int argc, char* argv[] )
294
/* Information about the current video settings. */
295
const SDL_VideoInfo* info = NULL;
296
/* Dimensions of our window. */
299
/* Color depth in bits of our window. */
301
/* Flags we will pass into SDL_SetVideoMode. */
304
/* First, initialize SDL's video subsystem. */
305
if( SDL_Init( SDL_INIT_VIDEO ) < 0 ) {
307
fprintf( stderr, "Video initialization failed: %s\n",
312
/* Let's get some video information. */
313
info = SDL_GetVideoInfo( );
316
/* This should probably never happen. */
317
fprintf( stderr, "Video query failed: %s\n",
323
* Set our width/height to 640/480 (you would
324
* of course let the user decide this in a normal
325
* app). We get the bpp we will request from
326
* the display. On X11, VidMode can't change
327
* resolution, so this is probably being overly
328
* safe. Under Win32, ChangeDisplaySettings
329
* can change the bpp.
333
bpp = info->vfmt->BitsPerPixel;
336
* Now, we want to setup our requested
337
* window attributes for our OpenGL window.
338
* We want *at least* 5 bits of red, green
339
* and blue. We also want at least a 16-bit
342
* The last thing we do is request a double
343
* buffered window. '1' turns on double
344
* buffering, '0' turns it off.
346
* Note that we do not use SDL_DOUBLEBUF in
347
* the flags to SDL_SetVideoMode. That does
348
* not affect the GL attribute state, only
349
* the standard 2D blitting setup.
351
// SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 5 );
352
// SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 5 );
353
// SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 5 );
354
// SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16 );
355
// SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
358
* We want to request that SDL provide us
359
* with an OpenGL window, in a fullscreen
363
* Make starting windowed an option, and
364
* handle the resize events properly with
367
flags = SDL_OPENGL;// | SDL_FULLSCREEN;
372
if( SDL_SetVideoMode( width, height, bpp, flags ) == 0 ) {
374
* This could happen for a variety of reasons,
375
* including DISPLAY not being set, the specified
376
* resolution not being available, etc.
378
fprintf( stderr, "Video mode set failed: %s\n",
384
* At this point, we should have a properly setup
385
* double-buffered window for use with OpenGL.
387
setup_opengl( width, height );
390
* Now we want to begin our normal app process--
391
* an event loop with a lot of redrawing.
393
one_iter(); // just one for testing purposes
401
* Record timings using SDL_GetTicks() and
402
* and print out frames per second at program