diff options
author | Éloi Rivard <azmeuk@gmail.com> | 2013-03-05 12:21:00 +0100 |
---|---|---|
committer | Éloi Rivard <azmeuk@gmail.com> | 2013-04-04 11:17:36 +0200 |
commit | 463d996cb002ccd3565088776ea6db60d54466c1 (patch) | |
tree | f6268c587e5f951b055be8f4c59252f3907eb1f8 | |
parent | 656bba1da1024c5b35936b0d375f27f14f8701e3 (diff) |
* Edited tests so they use emscripten_main_loop.
* Got triangle test about to work.
* Implemented misc functions.
-rw-r--r-- | src/library_glfw.js | 237 | ||||
-rw-r--r-- | tests/glfw/Makefile | 55 | ||||
-rw-r--r-- | tests/glfw/boing.c | 34 | ||||
-rw-r--r-- | tests/glfw/gears.c | 40 | ||||
-rw-r--r-- | tests/glfw/heightmap.c | 34 | ||||
-rw-r--r-- | tests/glfw/mipmaps.c | 114 | ||||
-rw-r--r-- | tests/glfw/particles.c | 58 | ||||
-rw-r--r-- | tests/glfw/pong3d.c | 62 | ||||
-rw-r--r-- | tests/glfw/splitview.c | 43 | ||||
-rw-r--r-- | tests/glfw/triangle.c | 152 |
10 files changed, 541 insertions, 288 deletions
diff --git a/src/library_glfw.js b/src/library_glfw.js index d171ffcb..db457bc4 100644 --- a/src/library_glfw.js +++ b/src/library_glfw.js @@ -2,22 +2,25 @@ var LibraryGLFW = { $GLFW: { initTime: null, - idleFunc: null, - displayFunc: null, keyboardFunc: null, - keyboardUpFunc: null, - specialFunc: null, - specialUpFunc: null, - reshapeFunc: null, + charFunc: null, + mouseButtonFunc: null, + mousePosFunc: null, + mouseWheelFunc: null, + resizeFunc: null, + closeFunc: null, + refreshFunc: null, motionFunc: null, passiveMotionFunc: null, mouseFunc: null, + features: null, + params: null, lastX: 0, lastY: 0, buttons: 0, modifiers: 0, - initWindowWidth: 256, - initWindowHeight: 256, + initWindowWidth: 640, + initWindowHeight: 480, // Set when going fullscreen windowX: 0, windowY: 0, @@ -288,9 +291,9 @@ var LibraryGLFW = { } Browser.setCanvasSize(width, height); // Can't call _glfwReshapeWindow as that requests cancelling fullscreen. - if (GLFW.reshapeFunc) { - // console.log("GLFW.reshapeFunc (from FS): " + width + ", " + height); - Runtime.dynCall('vii', GLFW.reshapeFunc, [width, height]); + if (GLFW.resizeFunc) { + // console.log("GLFW.resizeFunc (from FS): " + width + ", " + height); + Runtime.dynCall('vii', GLFW.resizeFunc, [width, height]); } _glfwPostRedisplay(); */ @@ -316,26 +319,140 @@ var LibraryGLFW = { }, /* GLFW initialization, termination and version querying */ - glfwInit : function() { throw "glfwInit is not implemented yet."; }, - glfwTerminate : function() { throw "glfwTerminate is not implemented yet."; }, - glfwGetVersion : function( major, minor, rev ) { throw "glfwGetVersion is not implemented yet."; }, + glfwInit : function() { + GLFW.initTime = Date.now() / 1000; +/* + window.addEventListener("keydown", GLFW.onKeydown, true); + window.addEventListener("keyup", GLFW.onKeyup, true); +*/ window.addEventListener("mousemove", GLFW.onMousemove, true); +/* window.addEventListener("mousedown", GLFW.onMouseButtonDown, true); + window.addEventListener("mouseup", GLFW.onMouseButtonUp, true); + + __ATEXIT__.push({ func: function() { + window.removeEventListener("keydown", GLFW.onKeydown, true); + window.removeEventListener("keyup", GLFW.onKeyup, true); + window.removeEventListener("mousemove", GLFW.onMousemove, true); + window.removeEventListener("mousedown", GLFW.onMouseButtonDown, true); + window.removeEventListener("mouseup", GLFW.onMouseButtonUp, true); + Module["canvas"].width = Module["canvas"].height = 1; + } }); +*/ + GLFW.features = new Array(); + GLFW.features[0x00030001] = true; //GLFW_MOUSE_CURSOR + GLFW.features[0x00030002] = false; //GLFW_STICKY_KEYS + GLFW.features[0x00030003] = true; //GLFW_STICKY_MOUSE_BUTTONS + GLFW.features[0x00030004] = false; //GLFW_SYSTEM_KEYS + GLFW.features[0x00030005] = false; //GLFW_KEY_REPEAT + GLFW.features[0x00030006] = true; //GLFW_AUTO_POLL_EVENTS + + GLFW.params = new Array(); + GLFW.params[0x00020001] = true; //GLFW_OPENED + GLFW.params[0x00020002] = true; //GLFW_ACTIVE + GLFW.params[0x00020003] = false; //GLFW_ICONIFIED + GLFW.params[0x00020004] = true; //GLFW_ACCELERATED + GLFW.params[0x00020005] = 0; //GLFW_RED_BITS + GLFW.params[0x00020006] = 0; //GLFW_GREEN_BITS + GLFW.params[0x00020007] = 0; //GLFW_BLUE_BITS + GLFW.params[0x00020008] = 0; //GLFW_ALPHA_BITS + GLFW.params[0x00020009] = 0; //GLFW_DEPTH_BITS + GLFW.params[0x0002000A] = 0; //GLFW_STENCIL_BITS + GLFW.params[0x0002000B] = 0; //GLFW_REFRESH_RATE + GLFW.params[0x0002000C] = 0; //GLFW_ACCUM_RED_BITS + GLFW.params[0x0002000D] = 0; //GLFW_ACCUM_GREEN_BITS + GLFW.params[0x0002000E] = 0; //GLFW_ACCUM_BLUE_BITS + GLFW.params[0x0002000F] = 0; //GLFW_ACCUM_ALPHA_BITS + GLFW.params[0x00020010] = 0; //GLFW_AUX_BUFFERS + GLFW.params[0x00020011] = 0; //GLFW_STEREO + GLFW.params[0x00020012] = 0; //GLFW_WINDOW_NO_RESIZE + GLFW.params[0x00020013] = 0; //GLFW_FSAA_SAMPLES + GLFW.params[0x00020014] = 0; //GLFW_OPENGL_VERSION_MAJOR + GLFW.params[0x00020015] = 0; //GLFW_OPENGL_VERSION_MINOR + GLFW.params[0x00020016] = 0; //GLFW_OPENGL_FORWARD_COMPAT + GLFW.params[0x00020017] = 0; //GLFW_OPENGL_DEBUG_CONTEXT + GLFW.params[0x00020018] = 0; //GLFW_OPENGL_PROFILE + + return 1; //GL_TRUE + }, + + glfwTerminate : function() {}, + + glfwGetVersion : function( major, minor, rev ) { + setValue(major, 2, 'i32'); + setValue(minor, 7, 'i32'); + setValue(rev, 7, 'i32'); + }, /* Window handling */ - glfwOpenWindow : function( width, height, redbits, greenbits, bluebits, alphabits, depthbits, stencilbits, mode ) { throw "glfwOpenWindow is not implemented yet."; }, - glfwOpenWindowHint : function( target, hint ) { throw "glfwOpenWindowHint is not implemented yet."; }, - glfwCloseWindow : function() { throw "glfwCloseWindow is not implemented yet."; }, - glfwSetWindowTitle : function( title ) { throw "glfwSetWindowTitle is not implemented yet."; }, - glfwGetWindowSize : function( width, height ) { throw "glfwGetWindowSize is not implemented yet."; }, + glfwOpenWindow__deps: ['$Browser'], + glfwOpenWindow : function( width, height, redbits, greenbits, bluebits, alphabits, depthbits, stencilbits, mode ) { + if(width == 0 && height > 0) + width = 4 * height / 3; + if(width > 0 && height == 0) + height = 3 * width / 4; + + GLFW.params[0x00020005] = redbits; //GLFW_RED_BITS + GLFW.params[0x00020006] = greenbits; //GLFW_GREEN_BITS + GLFW.params[0x00020007] = bluebits; //GLFW_BLUE_BITS + GLFW.params[0x00020008] = alphabits; //GLFW_ALPHA_BITS + GLFW.params[0x00020009] = depthbits; //GLFW_DEPTH_BITS + GLFW.params[0x0002000A] = stencilbits; //GLFW_STENCIL_BITS + + if(mode == 0x00010001){//GLFW_WINDOW + Browser.setCanvasSize( GLFW.initWindowWidth = width, + GLFW.initWindowHeight = height ); + GLFW.features[0x00030003] = true; //GLFW_STICKY_MOUSE_BUTTONS + } + else if(mode == 0x00010002){//GLFW_FULLSCREEN + GLFW.features[0x00030003] = false; //GLFW_STICKY_MOUSE_BUTTONS + } + else{ + throw "Invalid glfwOpenWindow mode."; + } + + Module.ctx = Browser.createContext(Module['canvas'], true, true); + return 1; //GL_TRUE + }, + + glfwOpenWindowHint : function( target, hint ) { + GLFW.params[target] = hint; + }, + + glfwCloseWindow__deps: ['$Browser'], + glfwCloseWindow : function() { + Module.ctx = Browser.destroyContext(Module['canvas'], true, true); + }, + + glfwSetWindowTitle : function( title ) { + document.title = Pointer_stringify(title); + }, + + glfwGetWindowSize : function( width, height ) { + setValue(width, Module['canvas'].width, 'i32'); + setValue(height, Module['canvas'].height, 'i32'); + }, + glfwSetWindowSize : function( width, height ) { throw "glfwSetWindowSize is not implemented yet."; }, glfwSetWindowPos : function( x, y ) { throw "glfwSetWindowPos is not implemented yet."; }, glfwIconifyWindow : function() { throw "glfwIconifyWindow is not implemented yet."; }, glfwRestoreWindow : function() { throw "glfwRestoreWindow is not implemented yet."; }, - glfwSwapBuffers : function() { throw "glfwSwapBuffers is not implemented yet."; }, - glfwSwapInterval : function( interval ) { throw "glfwSwapInterval is not implemented yet."; }, - glfwGetWindowParam : function( param ) { throw "glfwGetWindowParam is not implemented yet."; }, - glfwSetWindowSizeCallback : function( cbfun ) { throw "glfwSetWindowSizeCallback is not implemented yet."; }, - glfwSetWindowCloseCallback : function( cbfun ) { throw "glfwSetWindowCloseCallback is not implemented yet."; }, - glfwSetWindowRefreshCallback : function( cbfun ) { throw "glfwSetWindowRefreshCallback is not implemented yet."; }, + glfwSwapBuffers : function() {}, + glfwSwapInterval : function( interval ) {}, + + glfwGetWindowParam : function( param ) { + return GLFW.params[param]; + }, + + glfwSetWindowSizeCallback : function( cbfun ) { + GLFW.resizeFunc = cbfun; + }, + + glfwSetWindowCloseCallback : function( cbfun ) { + GLFW.closeFunc = cbfun; + }, + + glfwSetWindowRefreshCallback : function( cbfun ) { + GLFW.refreshFunc = cbfun; + }, /* Video mode functions */ glfwGetVideoModes : function( list, maxcount ) { throw "glfwGetVideoModes is not implemented yet."; }, @@ -344,17 +461,45 @@ var LibraryGLFW = { /* Input handling */ glfwPollEvents : function() { throw "glfwPollEvents is not implemented yet."; }, glfwWaitEvents : function() { throw "glfwWaitEvents is not implemented yet."; }, - glfwGetKey : function( key ) { throw "glfwGetKey is not implemented yet."; }, + + glfwGetKey : function( key ) { + //TODO, actually something + return 0;//GLFW_RELEASE + }, + glfwGetMouseButton : function( button ) { throw "glfwGetMouseButton is not implemented yet."; }, - glfwGetMousePos : function( xpos, ypos ) { throw "glfwGetMousePos is not implemented yet."; }, - glfwSetMousePos : function( xpos, ypos ) { throw "glfwSetMousePos is not implemented yet."; }, + + glfwGetMousePos : function( xpos, ypos ) { + setValue(xpos, GLFW.lastX, 'i32'); + setValue(ypos, GLFW.lastY, 'i32'); + }, + + glfwSetMousePos : function( xpos, ypos ) { + throw "glfwSetMousePos is not implemented yet."; + }, + glfwGetMouseWheel : function() { throw "glfwGetMouseWheel is not implemented yet."; }, glfwSetMouseWheel : function( pos ) { throw "glfwSetMouseWheel is not implemented yet."; }, - glfwSetKeyCallback : function( cbfun ) { throw "glfwSetKeyCallback is not implemented yet."; }, - glfwSetCharCallback : function( cbfun ) { throw "glfwSetCharCallback is not implemented yet."; }, - glfwSetMouseButtonCallback : function( cbfun ) { throw "glfwSetMouseButtonCallback is not implemented yet."; }, - glfwSetMousePosCallback : function( cbfun ) { throw "glfwSetMousePosCallback is not implemented yet."; }, - glfwSetMouseWheelCallback : function( cbfun ) { throw "glfwSetMouseWheelCallback is not implemented yet."; }, + + glfwSetKeyCallback : function( cbfun ) { + GLFW.keyFunc = cbfun; + }, + + glfwSetCharCallback : function( cbfun ) { + GLFW.charFunc = cbfun; + }, + + glfwSetMouseButtonCallback : function( cbfun ) { + GLFW.mouseButtonFunc = cbfun; + }, + + glfwSetMousePosCallback : function( cbfun ) { + GLFW.mousePosFunc = cbfun; + }, + + glfwSetMouseWheelCallback : function( cbfun ) { + GLFW.mouseWheelFunc = cbfun; + }, /* Joystick input */ glfwGetJoystickParam : function( joy, param ) { throw "glfwGetJoystickParam is not implemented yet."; }, @@ -362,14 +507,20 @@ var LibraryGLFW = { glfwGetJoystickButtons : function( joy, buttons, numbuttons ) { throw "glfwGetJoystickButtons is not implemented yet."; }, /* Time */ - glfwGetTime : function() { throw "glfwGetTime is not implemented yet."; }, - glfwSetTime : function( time ) { throw "glfwSetTime is not implemented yet."; }, + glfwGetTime : function() { + return (Date.now()/1000) - GLFW.initTime; + }, + + glfwSetTime : function( time ) { + GLFW.initTime = Date.now()/1000 + time; + }, + glfwSleep : function( time ) { throw "glfwSleep is not implemented yet."; }, /* Extension support */ - glfwExtensionSupported : function( extension ) { throw " is not implemented yet."; }, - glfwGetProcAddress : function( procname ) { throw " is not implemented yet."; }, - glfwGetGLVersion : function( major, minor, rev ) { throw " is not implemented yet."; }, + glfwExtensionSupported : function( extension ) { throw "glfwExtensionSupported is not implemented yet."; }, + glfwGetProcAddress : function( procname ) { throw "glfwGetProcAddress is not implemented yet."; }, + glfwGetGLVersion : function( major, minor, rev ) { throw "glfwGetGLVersion is not implemented yet."; }, /* Threading support */ glfwCreateThread : function( fun, arg ) { throw "glfwCreateThread is not implemented yet."; }, @@ -388,8 +539,13 @@ var LibraryGLFW = { glfwGetNumberOfProcessors : function() { throw "glfwGetNumberOfProcessors is not implemented yet."; }, /* Enable/disable functions */ - glfwEnable : function( token ) { throw "glfwEnable is not implemented yet."; }, - glfwDisable : function( token ) { throw "glfwDisable is not implemented yet."; }, + glfwEnable : function( token ) { + GLFW.features[token] = false; + }, + + glfwDisable : function( token ) { + GLFW.features[token] = true; + }, /* Image/texture I/O support */ glfwReadImage : function( name, img, flags ) { throw "glfwReadImage is not implemented yet."; }, @@ -398,7 +554,6 @@ var LibraryGLFW = { glfwLoadTexture2D : function( name, flags ) { throw "glfwLoadTexture2D is not implemented yet."; }, glfwLoadMemoryTexture2D : function( data, size, flags ) { throw "glfwLoadMemoryTexture2D is not implemented yet."; }, glfwLoadTextureImage2D : function( img, flags ) { throw "glfwLoadTextureImage2D is not implemented yet."; }, - }; autoAddDeps(LibraryGLFW, '$GLFW'); diff --git a/tests/glfw/Makefile b/tests/glfw/Makefile index 89138d74..85b9067c 100644 --- a/tests/glfw/Makefile +++ b/tests/glfw/Makefile @@ -2,54 +2,55 @@ # Makefile for GLFW example programs on X11 (generated by compile.sh) ########################################################################## CC = emcc -CFLAGS = -I../include +CFLAGS = -I../include -DEMSCRIPTEN LIB = -lglfw SOLIB = LFLAGS = $(LIB) SO_LFLAGS = $(SOLIB) +EXT = html -BINARIES = triangle.js listmodes.js mthello.js pong3d.js mtbench.js particles.js splitview.js \ - mipmaps.js gears.js boing.js heightmap.js +BINARIES = triangle listmodes mthello pong3d mtbench particles splitview \ + mipmaps gears boing heightmap ## wave all: $(BINARIES) -triangle.js: triangle.c - $(CC) $(CFLAGS) triangle.c $(LFLAGS) -o $@ +triangle: triangle.c + $(CC) $(CFLAGS) triangle.c $(LFLAGS) -o $@.$(EXT) -listmodes.js: listmodes.c - $(CC) $(CFLAGS) listmodes.c $(LFLAGS) -o $@ +listmodes: listmodes.c + $(CC) $(CFLAGS) listmodes.c $(LFLAGS) -o $@.$(EXT) -mthello.js: mthello.c - $(CC) $(CFLAGS) mthello.c $(LFLAGS) -o $@ +mthello: mthello.c + $(CC) $(CFLAGS) mthello.c $(LFLAGS) -o $@.$(EXT) -pong3d.js: pong3d.c - $(CC) $(CFLAGS) pong3d.c $(LFLAGS) -o $@ +pong3d: pong3d.c + $(CC) $(CFLAGS) pong3d.c $(LFLAGS) -o $@.$(EXT) -mtbench.js: mtbench.c - $(CC) $(CFLAGS) mtbench.c $(LFLAGS) -o $@ +mtbench: mtbench.c + $(CC) $(CFLAGS) mtbench.c $(LFLAGS) -o $@.$(EXT) -particles.js: particles.c - $(CC) $(CFLAGS) particles.c $(LFLAGS) -o $@ +particles: particles.c + $(CC) $(CFLAGS) particles.c $(LFLAGS) -o $@.$(EXT) -splitview.js: splitview.c - $(CC) $(CFLAGS) splitview.c $(LFLAGS) -o $@ +splitview: splitview.c + $(CC) $(CFLAGS) splitview.c $(LFLAGS) -o $@.$(EXT) -mipmaps.js: mipmaps.c - $(CC) $(CFLAGS) mipmaps.c $(LFLAGS) -o $@ +mipmaps: mipmaps.c + $(CC) $(CFLAGS) mipmaps.c $(LFLAGS) -o $@.$(EXT) -gears.js: gears.c - $(CC) $(CFLAGS) gears.c $(LFLAGS) -o $@ +gears: gears.c + $(CC) $(CFLAGS) gears.c $(LFLAGS) -o $@.$(EXT) -boing.js: boing.c - $(CC) $(CFLAGS) boing.c $(LFLAGS) -o $@ +boing: boing.c + $(CC) $(CFLAGS) boing.c $(LFLAGS) -o $@.$(EXT) -wave.js: wave.c - $(CC) $(CFLAGS) wave.c $(LFLAGS) -o $@ +wave: wave.c + $(CC) $(CFLAGS) wave.c $(LFLAGS) -o $@.$(EXT) -heightmap.js: heightmap.c - $(CC) $(CFLAGS) heightmap.c $(LFLAGS) -o $@ +heightmap: heightmap.c + $(CC) $(CFLAGS) heightmap.c $(LFLAGS) -o $@.$(EXT) clean: rm -f $(BINARIES) diff --git a/tests/glfw/boing.c b/tests/glfw/boing.c index 08544d1a..36c45b19 100644 --- a/tests/glfw/boing.c +++ b/tests/glfw/boing.c @@ -32,6 +32,10 @@ #include <math.h> #include <GL/glfw.h> +#ifdef EMSCRIPTEN +#include <emscripten/emscripten.h> +#endif + /***************************************************************************** * Various declarations and macros @@ -563,6 +567,19 @@ void DrawGrid( void ) * main() *======================================================================*/ +void iteration(){ + /* Timing */ + t = glfwGetTime(); + dt = t - t_old; + t_old = t; + + /* Draw one frame */ + display(); + + /* Swap buffers */ + glfwSwapBuffers(); +} + int main( void ) { int running; @@ -590,25 +607,18 @@ int main( void ) init(); /* Main loop */ +#ifdef EMSCRIPTEN + emscripten_set_main_loop (iteration, 0, 1); +#else do { - /* Timing */ - t = glfwGetTime(); - dt = t - t_old; - t_old = t; - - /* Draw one frame */ - display(); - - /* Swap buffers */ - glfwSwapBuffers(); - + iteration(); /* Check if we are still running */ running = !glfwGetKey( GLFW_KEY_ESC ) && glfwGetWindowParam( GLFW_OPENED ); } while( running ); - +#endif glfwTerminate(); exit( EXIT_SUCCESS ); } diff --git a/tests/glfw/gears.c b/tests/glfw/gears.c index d9efdf9a..01288414 100644 --- a/tests/glfw/gears.c +++ b/tests/glfw/gears.c @@ -32,6 +32,10 @@ #define M_PI 3.141592654 #endif +#ifdef EMSCRIPTEN +#include <emscripten/emscripten.h> +#endif + /* The program exits when this is zero. */ static int running = 1; @@ -317,6 +321,22 @@ static void init(int argc, char *argv[]) } } +void iteration(){ + // Draw gears + draw(); + + // Update animation + animate(); + + // Swap buffers + glfwSwapBuffers(); + + // Was the window closed? + if( !glfwGetWindowParam( GLFW_OPENED ) ) + { + running = 0; + } +} /* program entry */ int main(int argc, char *argv[]) @@ -345,25 +365,15 @@ int main(int argc, char *argv[]) glfwSetWindowSizeCallback( reshape ); glfwSetKeyCallback( key ); +#ifdef EMSCRIPTEN + emscripten_set_main_loop (iteration, 0, 1); +#else // Main loop while( running ) { - // Draw gears - draw(); - - // Update animation - animate(); - - // Swap buffers - glfwSwapBuffers(); - - // Was the window closed? - if( !glfwGetWindowParam( GLFW_OPENED ) ) - { - running = 0; - } + iteration(); } - +#endif // Terminate GLFW glfwTerminate(); diff --git a/tests/glfw/heightmap.c b/tests/glfw/heightmap.c index 7faa5d1f..4967ea11 100644 --- a/tests/glfw/heightmap.c +++ b/tests/glfw/heightmap.c @@ -30,6 +30,9 @@ #include <stddef.h> #include "getopt.h" +#ifdef EMSCRIPTEN +#include <emscripten/emscripten.h> +#endif #define GLFW_NO_GLU 1 #include <GL/glfw.h> @@ -674,12 +677,16 @@ static void usage(void) printf(" heightmap [-h]\n"); } +void iteration(); + +double dt; +int frame; +int iter; +double last_update_time; + int main(int argc, char** argv) { - int ch, iter; - double dt; - double last_update_time; - int frame; + int ch; float f; GLint uloc_modelview; GLint uloc_project; @@ -820,9 +827,21 @@ int main(int argc, char** argv) iter = 0; dt = last_update_time = glfwGetTime(); - while (running) +#ifdef EMSCRIPTEN + emscripten_set_main_loop (iteration, 0, 1); +#else + // Main loop + while( running ) { - ++frame; + iteration(); + } +#endif + + exit(EXIT_SUCCESS); +} + +void iteration(){ + ++frame; /* render the next frame */ glClear(GL_COLOR_BUFFER_BIT); glDrawElements(GL_LINES, 2* MAP_NUM_LINES , GL_UNSIGNED_INT, 0); @@ -843,8 +862,5 @@ int main(int argc, char** argv) last_update_time = dt; frame = 0; } - } - - exit(EXIT_SUCCESS); } diff --git a/tests/glfw/mipmaps.c b/tests/glfw/mipmaps.c index 59bbef2e..bfd77f06 100644 --- a/tests/glfw/mipmaps.c +++ b/tests/glfw/mipmaps.c @@ -10,11 +10,66 @@ #include <GL/glfw.h> +#ifdef EMSCRIPTEN +#include <emscripten/emscripten.h> +#endif + +int width, height, x; +double time; +GLboolean running; + +void iteration(){ + // Get time and mouse position + time = glfwGetTime(); + glfwGetMousePos( &x, NULL ); + + // Get window size (may be different than the requested size) + glfwGetWindowSize( &width, &height ); + height = height > 0 ? height : 1; + + // Set viewport + glViewport( 0, 0, width, height ); + + // Clear color buffer + glClearColor( 0.0f, 0.0f, 0.0f, 0.0f); + glClear( GL_COLOR_BUFFER_BIT ); + + // Select and setup the projection matrix + glMatrixMode( GL_PROJECTION ); + glLoadIdentity(); + gluPerspective( 65.0f, (GLfloat)width / (GLfloat)height, 1.0f, + 50.0f ); + + // Select and setup the modelview matrix + glMatrixMode( GL_MODELVIEW ); + glLoadIdentity(); + gluLookAt( 0.0f, 3.0f, -20.0f, // Eye-position + 0.0f, -4.0f, -11.0f, // View-point + 0.0f, 1.0f, 0.0f ); // Up-vector + + // Draw a textured quad + glRotatef( 0.05f * (GLfloat)x + (GLfloat)time * 5.0f, 0.0f, 1.0f, 0.0f ); + glBegin( GL_QUADS ); + glTexCoord2f( -20.0f, 20.0f ); + glVertex3f( -50.0f, 0.0f, -50.0f ); + glTexCoord2f( 20.0f, 20.0f ); + glVertex3f( 50.0f, 0.0f, -50.0f ); + glTexCoord2f( 20.0f, -20.0f ); + glVertex3f( 50.0f, 0.0f, 50.0f ); + glTexCoord2f( -20.0f, -20.0f ); + glVertex3f( -50.0f, 0.0f, 50.0f ); + glEnd(); + + // Swap buffers + glfwSwapBuffers(); + + // Check if the ESC key was pressed or the window was closed + running = !glfwGetKey( GLFW_KEY_ESC ) && + glfwGetWindowParam( GLFW_OPENED ); +} + int main( void ) { - int width, height, x; - double time; - GLboolean running; GLuint textureID; char* texturePath = "mipmaps.tga"; @@ -63,56 +118,15 @@ int main( void ) glEnable( GL_TEXTURE_2D ); running = GL_TRUE; +#ifdef EMSCRIPTEN + emscripten_set_main_loop (iteration, 0, 1); +#else + // Main loop while( running ) { - // Get time and mouse position - time = glfwGetTime(); - glfwGetMousePos( &x, NULL ); - - // Get window size (may be different than the requested size) - glfwGetWindowSize( &width, &height ); - height = height > 0 ? height : 1; - - // Set viewport - glViewport( 0, 0, width, height ); - - // Clear color buffer - glClearColor( 0.0f, 0.0f, 0.0f, 0.0f); - glClear( GL_COLOR_BUFFER_BIT ); - - // Select and setup the projection matrix - glMatrixMode( GL_PROJECTION ); - glLoadIdentity(); - gluPerspective( 65.0f, (GLfloat)width / (GLfloat)height, 1.0f, - 50.0f ); - - // Select and setup the modelview matrix - glMatrixMode( GL_MODELVIEW ); - glLoadIdentity(); - gluLookAt( 0.0f, 3.0f, -20.0f, // Eye-position - 0.0f, -4.0f, -11.0f, // View-point - 0.0f, 1.0f, 0.0f ); // Up-vector - - // Draw a textured quad - glRotatef( 0.05f * (GLfloat)x + (GLfloat)time * 5.0f, 0.0f, 1.0f, 0.0f ); - glBegin( GL_QUADS ); - glTexCoord2f( -20.0f, 20.0f ); - glVertex3f( -50.0f, 0.0f, -50.0f ); - glTexCoord2f( 20.0f, 20.0f ); - glVertex3f( 50.0f, 0.0f, -50.0f ); - glTexCoord2f( 20.0f, -20.0f ); - glVertex3f( 50.0f, 0.0f, 50.0f ); - glTexCoord2f( -20.0f, -20.0f ); - glVertex3f( -50.0f, 0.0f, 50.0f ); - glEnd(); - - // Swap buffers - glfwSwapBuffers(); - - // Check if the ESC key was pressed or the window was closed - running = !glfwGetKey( GLFW_KEY_ESC ) && - glfwGetWindowParam( GLFW_OPENED ); + iteration(); } +#endif // Close OpenGL window and terminate GLFW glfwTerminate(); diff --git a/tests/glfw/particles.c b/tests/glfw/particles.c index 403a9997..15c133a0 100644 --- a/tests/glfw/particles.c +++ b/tests/glfw/particles.c @@ -43,6 +43,10 @@ #include <math.h> #include <GL/glfw.h> +#ifdef EMSCRIPTEN +#include <emscripten/emscripten.h> +#endif + // Define tokens for GL_EXT_separate_specular_color if not already defined #ifndef GL_EXT_separate_specular_color #define GL_LIGHT_MODEL_COLOR_CONTROL_EXT 0x81F8 @@ -970,10 +974,34 @@ void GLFWCALL PhysicsThreadFun( void *arg ) // main() //======================================================================== +double t0, t; +int frames, benchmark; +void iteration(){ + // Get frame time + t = glfwGetTime() - t0; + + // Draw... + Draw( t ); + + // Swap buffers + glfwSwapBuffers(); + + // Check if window was closed + running = running && glfwGetWindowParam( GLFW_OPENED ); + + // Increase frame count + frames ++; + + // End of benchmark? + if( benchmark && t >= 60.0 ) + { + running = 0; + } +} + int main( int argc, char **argv ) { - int i, frames, benchmark; - double t0, t; + int i; GLFWthread physics_thread = 0; // Use multithreading by default, but don't benchmark @@ -1108,29 +1136,15 @@ int main( int argc, char **argv ) // Main loop t0 = glfwGetTime(); frames = 0; +#ifdef EMSCRIPTEN + emscripten_set_main_loop (iteration, 0, 1); +#else + // Main loop while( running ) { - // Get frame time - t = glfwGetTime() - t0; - - // Draw... - Draw( t ); - - // Swap buffers - glfwSwapBuffers(); - - // Check if window was closed - running = running && glfwGetWindowParam( GLFW_OPENED ); - - // Increase frame count - frames ++; - - // End of benchmark? - if( benchmark && t >= 60.0 ) - { - running = 0; - } + iteration(); } +#endif t = glfwGetTime() - t0; // Wait for particle physics thread to die diff --git a/tests/glfw/pong3d.c b/tests/glfw/pong3d.c index 1d1afd1f..1f136c93 100644 --- a/tests/glfw/pong3d.c +++ b/tests/glfw/pong3d.c @@ -10,6 +10,9 @@ #include <stdlib.h> #include <math.h> +#ifdef EMSCRIPTEN +#include <emscripten/emscripten.h> +#endif //======================================================================== // Constants @@ -719,20 +722,9 @@ void GameOver( void ) // GameLoop() - Game loop //======================================================================== -void GameLoop( void ) -{ - int playing, event; - - // Initialize a new game - NewGame(); +int playing, event; - // Enable sticky keys - glfwEnable( GLFW_STICKY_KEYS ); - - // Loop until the game ends - playing = GL_TRUE; - while( playing && glfwGetWindowParam( GLFW_OPENED ) ) - { +void iteration(){ // Frame timer oldtime = thistime; thistime = glfwGetTime(); @@ -784,7 +776,36 @@ void GameLoop( void ) // Swap buffers glfwSwapBuffers(); +} + +void GameLoop( void ) +{ + int menuoption; + + // Initialize a new game + NewGame(); + + // Enable sticky keys + glfwEnable( GLFW_STICKY_KEYS ); + + // Loop until the game ends + playing = GL_TRUE; + + menuoption = GameMenu(); + + // If the user wants to play, let him... + if( menuoption != MENU_PLAY) + playing = GL_FALSE; + +#ifdef EMSCRIPTEN + emscripten_set_main_loop (iteration, 0, 1); +#else + // Main loop + while( playing && glfwGetWindowParam( GLFW_OPENED ) ) + { + iteration(); } +#endif // Disable sticky keys glfwDisable( GLFW_STICKY_KEYS ); @@ -800,7 +821,6 @@ void GameLoop( void ) int main( void ) { - int menuoption; // Initialize GLFW if( !glfwInit() ) @@ -826,19 +846,7 @@ int main( void ) exit( EXIT_FAILURE ); } - // Main loop - do - { - // Get menu option - menuoption = GameMenu(); - - // If the user wants to play, let him... - if( menuoption == MENU_PLAY ) - { - GameLoop(); - } - } - while( menuoption != MENU_QUIT ); + GameLoop(); // Unload all textures if( glfwGetWindowParam( GLFW_OPENED ) ) diff --git a/tests/glfw/splitview.c b/tests/glfw/splitview.c index 932cd0d6..bb85a6e7 100644 --- a/tests/glfw/splitview.c +++ b/tests/glfw/splitview.c @@ -15,6 +15,10 @@ #include <stdio.h> #include <stdlib.h> +#ifdef EMSCRIPTEN +#include <emscripten/emscripten.h> +#endif + #ifndef M_PI #define M_PI 3.14159265358979323846 #endif @@ -446,6 +450,23 @@ static void GLFWCALL mouseButtonFun( int button, int action ) // main() //======================================================================== +void iteration(){ + // Only redraw if we need to + if( do_redraw ) + { + // Draw all views + drawAllViews(); + + // Swap buffers + glfwSwapBuffers(); + + do_redraw = 0; + } + + // Wait for new events + glfwWaitEvents(); +} + int main( void ) { // Initialise GLFW @@ -484,27 +505,17 @@ int main( void ) glfwSetMousePosCallback( mousePosFun ); glfwSetMouseButtonCallback( mouseButtonFun ); +#ifdef EMSCRIPTEN + emscripten_set_main_loop (iteration, 0, 1); +#else // Main loop - do + do { - // Only redraw if we need to - if( do_redraw ) - { - // Draw all views - drawAllViews(); - - // Swap buffers - glfwSwapBuffers(); - - do_redraw = 0; - } - - // Wait for new events - glfwWaitEvents(); - + iteration(); } // Check if the ESC key was pressed or the window was closed while( glfwGetKey( GLFW_KEY_ESC ) != GLFW_PRESS && glfwGetWindowParam( GLFW_OPENED ) ); +#endif // Close OpenGL window and terminate GLFW glfwTerminate(); diff --git a/tests/glfw/triangle.c b/tests/glfw/triangle.c index a8b737be..db9d9ab1 100644 --- a/tests/glfw/triangle.c +++ b/tests/glfw/triangle.c @@ -8,87 +8,101 @@ #include <stdlib.h> #include <GL/glfw.h> +#ifdef EMSCRIPTEN +#include <emscripten/emscripten.h> +#endif -int main( void ) +void +iteration () { - int width, height, x; - double t; + int width, height, x; + double t; + + t = glfwGetTime (); + glfwGetMousePos (&x, NULL); + + // Get window size (may be different than the requested size) + glfwGetWindowSize (&width, &height); + + // Special case: avoid division by zero below + height = height > 0 ? height : 1; + + glViewport (0, 0, width, height); + + // Clear color buffer to black + glClearColor (0.0f, 0.0f, 0.0f, 0.0f); + gl |