diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/library_glfw.js | 237 |
1 files changed, 196 insertions, 41 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'); |