Skip to content
Browse files

bug fixes

  • Loading branch information...
1 parent 9b70b1e commit c61322a27fff402c8e3acbfadf3b75991c8bcf53 @mikeseven committed Feb 17, 2012
Showing with 188 additions and 27 deletions.
  1. +13 −13 index.js
  2. +1 −1 package.json
  3. +155 −12 src/glfw.cc
  4. +18 −0 test/test.js
  5. +1 −1 wscript
View
26 index.js
@@ -10,19 +10,19 @@ Object.defineProperty(GLFW, 'events', {
get: function () {
if (events) return events;
events = new (require('events').EventEmitter);
- var now = Date.now();
- setInterval(function () {
- var after = Date.now();
- var delta = after - now;
- now = after;
- var data;
- /*while (data = GLFW.pollEvent()) {
- events.emit('event', data);
- events.emit(data.type, data);
- }*/
- events.emit('tick', delta);
- }, 16);
+
+ var _emit=events.emit;
+ events.emit=function() {
+ var args = Array.prototype.slice.call(arguments);
+ var evt= args[1]; // args[1] is the event, args[0] is the type of event
+ //console.log("emitting event: "+require('util').inspect(args));
+ evt.preventDefault = function () {};
+ evt.stopPropagation = function () {};
+ _emit.apply(this,args);
+ };
return events;
- }
+ },
+ enumerable: true,
+ configurable: true
});
View
2 package.json
@@ -32,7 +32,7 @@
"node": ">=0.6.5"
},
"scripts": {
- "install": "rm -rf build; node-waf configure clean build"
+ "install": "rm -rf build; node-waf configure build"
}
}
View
167 src/glfw.cc
@@ -1,13 +1,20 @@
#include "common.h"
-#define GLFW_INCLUDE_GL3
-#define GLFW_NO_GLU
+//#include <OpenGL/OpenGL.h>
+//#include <OpenGL/gl3.h>
+#define GLEW_NO_GLU
#include <GL/glew.h>
+
+#define GLFW_INCLUDE_GL3
+#define GLFW_NO_GLU
+#undef __gl_h_
#include <GL/glfw.h>
+
#include <cstdio>
using namespace v8;
+using namespace node;
#include <iostream>
using namespace std;
@@ -51,12 +58,130 @@ JS_METHOD(GetVersion) {
}
/* Window handling */
-void keycb(int key, int action) {
- cout<<"keycb: key="<<key<<" action="<<action<<endl;
+Persistent<Object> glfw_events;
+int lastX=0,lastY=0;
+bool windowCreated=false;
+
+void keyCB(int key, int action) {
+ HandleScope scope;
+
+ Local<Array> evt=Array::New(7);
+ evt->Set(JS_STR("type"),JS_STR(action ? "keydown" : "keyup"));
+ evt->Set(JS_STR("ctrlKey"),JS_BOOL(glfwGetKey(GLFW_KEY_LCTRL) || glfwGetKey(GLFW_KEY_RCTRL)));
+ evt->Set(JS_STR("shiftKey"),JS_BOOL(glfwGetKey(GLFW_KEY_LSHIFT) || glfwGetKey(GLFW_KEY_RSHIFT)));
+ evt->Set(JS_STR("altKey"),JS_BOOL(glfwGetKey(GLFW_KEY_LALT) || glfwGetKey(GLFW_KEY_RALT)));
+ evt->Set(JS_STR("metaKey"),JS_BOOL(glfwGetKey(GLFW_KEY_LSUPER) || glfwGetKey(GLFW_KEY_RSUPER)));
+
+ if(key==GLFW_KEY_ESC) key=27;
+ else if(key==GLFW_KEY_LSHIFT || key==GLFW_KEY_RSHIFT) key=16;
+ else if(key==GLFW_KEY_LCTRL || key==GLFW_KEY_RCTRL) key=17;
+ else if(key==GLFW_KEY_LALT || key==GLFW_KEY_RALT) key=18;
+ else if(key==GLFW_KEY_LSUPER) key=91;
+ else if(key==GLFW_KEY_RSUPER) key=93;
+ evt->Set(JS_STR("which"),JS_INT(key));
+ evt->Set(JS_STR("keyCode"),JS_INT(key));
+ evt->Set(JS_STR("charCode"),JS_INT(key));
+
+ Handle<Value> argv[2] = {
+ JS_STR(action ? "keydown" : "keyup"), // event name
+ evt
+ };
+
+ MakeCallback(glfw_events, "emit", 2, argv);
+}
+
+void mousePosCB(int x, int y) {
+ int w,h;
+ glfwGetWindowSize(&w, &h);
+ if(x<0 || x>=w) return;
+ if(y<0 || y>=h) return;
+
+ lastX=x;
+ lastY=y;
+
+ HandleScope scope;
+
+ Local<Array> evt=Array::New(5);
+ evt->Set(JS_STR("type"),JS_STR("mousemove"));
+ evt->Set(JS_STR("pageX"),JS_INT(x));
+ evt->Set(JS_STR("pageY"),JS_INT(y));
+ evt->Set(JS_STR("x"),JS_INT(x));
+ evt->Set(JS_STR("y"),JS_INT(y));
+
+ Handle<Value> argv[2] = {
+ JS_STR("mousemove"), // event name
+ evt
+ };
+
+ MakeCallback(glfw_events, "emit", 2, argv);
}
-void mouseposcb(int x, int y) {
- cout<<"mouseposcb: "<<x<<" "<<y<<endl;
+void windowSizeCB(int w, int h) {
+ HandleScope scope;
+ //cout<<"resizeCB: "<<w<<" "<<h<<endl;
+
+ Local<Array> evt=Array::New(3);
+ evt->Set(JS_STR("type"),JS_STR("resize"));
+ evt->Set(JS_STR("width"),JS_INT(w));
+ evt->Set(JS_STR("height"),JS_INT(h));
+
+ Handle<Value> argv[2] = {
+ JS_STR("resize"), // event name
+ evt
+ };
+
+ MakeCallback(glfw_events, "emit", 2, argv);
+}
+
+void mouseButtonCB(int button, int action) {
+ HandleScope scope;
+
+ Local<Array> evt=Array::New(7);
+ evt->Set(JS_STR("type"),JS_STR(action ? "mousedown" : "mouseup"));
+ evt->Set(JS_STR("button"),JS_INT(button));
+ evt->Set(JS_STR("which"),JS_INT(button));
+ evt->Set(JS_STR("x"),JS_INT(lastX));
+ evt->Set(JS_STR("y"),JS_INT(lastY));
+ evt->Set(JS_STR("pageX"),JS_INT(lastX));
+ evt->Set(JS_STR("pageY"),JS_INT(lastY));
+
+ Handle<Value> argv[2] = {
+ JS_STR(action ? "mousedown" : "mouseup"), // event name
+ evt
+ };
+
+ MakeCallback(glfw_events, "emit", 2, argv);
+}
+
+void mouseWheelCB(int pos) {
+ HandleScope scope;
+
+ Local<Array> evt=Array::New(2);
+ evt->Set(JS_STR("type"),JS_STR("mousewheel"));
+ evt->Set(JS_STR("wheelDelta"),JS_INT(pos));
+
+ Handle<Value> argv[2] = {
+ JS_STR("mousewheel"), // event name
+ evt
+ };
+
+ MakeCallback(glfw_events, "emit", 2, argv);
+}
+
+void windowRefreshCB() {
+ cout<<"windowRefreshCB"<<endl;
+}
+
+int windowCloseCB() {
+ HandleScope scope;
+
+ Handle<Value> argv[1] = {
+ JS_STR("quit"), // event name
+ };
+
+ MakeCallback(glfw_events, "emit", 1, argv);
+
+ return GL_TRUE;
}
JS_METHOD(OpenWindow) {
@@ -71,14 +196,25 @@ JS_METHOD(OpenWindow) {
int stencilbits=args[7]->Uint32Value();
int mode=args[8]->Uint32Value();
- bool ret=glfwOpenWindow(width,height,redbits,greenbits,bluebits,alphabits,depthbits,stencilbits,mode);
- glewInit();
+ if(!windowCreated) {
+ windowCreated=glfwOpenWindow(width,height,redbits,greenbits,bluebits,alphabits,depthbits,stencilbits,mode);
+ glewInit();
+ }
+ else
+ glfwSetWindowSize(width,height);
+
+ // Set callback functions
+ glfw_events=Persistent<Object>::New(args.This()->Get(JS_STR("events"))->ToObject());
- // input callbacks
- glfwSetKeyCallback(keycb);
- glfwSetMousePosCallback(mouseposcb);
+ glfwSetWindowSizeCallback( windowSizeCB );
+ glfwSetWindowRefreshCallback( windowRefreshCB );
+ glfwSetWindowCloseCallback( windowCloseCB );
+ glfwSetMousePosCallback( mousePosCB );
+ glfwSetMouseButtonCallback( mouseButtonCB );
+ glfwSetMouseWheelCallback( mouseWheelCB );
+ glfwSetKeyCallback(keyCB);
- return scope.Close(JS_BOOL(ret));
+ return scope.Close(JS_BOOL(windowCreated));
}
JS_METHOD(OpenWindowHint) {
@@ -259,6 +395,11 @@ JS_METHOD(Disable) {
return scope.Close(Undefined());
}
+// make sure we close everything when we exit
+void AtExit() {
+ glfwTerminate();
+}
+
} // namespace glfw
///////////////////////////////////////////////////////////////////////////////
@@ -271,6 +412,8 @@ JS_METHOD(Disable) {
extern "C" {
void init(Handle<Object> target) {
+ atexit(glfw::AtExit);
+
HandleScope scope;
/* GLFW initialization, termination and version querying */
View
18 test/test.js
@@ -1,4 +1,5 @@
var glfw = require('../index');
+var util=require('util');
var log = console.log;
var version = glfw.GetVersion();
@@ -24,6 +25,23 @@ glfw.WINDOW)) {
glfw.SetWindowTitle("Trilinear interpolation");
+// testing events
+glfw.events.on('keydown',function(evt){
+ log("[keydown] "+util.inspect(evt));
+});
+
+glfw.events.on('mousemove',function(evt){
+ log("[mousemove] "+evt.x+", "+evt.y);
+});
+
+glfw.events.on('mousewheel',function(evt){
+ log("[mousewheel] "+evt.position);
+});
+
+glfw.events.on('resize',function(evt){
+ log("[resize] "+evt.width+", "+evt.height);
+});
+
var glVersion = glfw.GetGLVersion(); // can only be called after window creation!
log('gl ' + glVersion.major + '.' + glVersion.minor + '.' + glVersion.rev);
View
2 wscript
@@ -32,7 +32,7 @@ def build(bld):
#obj.ldflags=['/usr/local/lib/libglfw.a']
obj.uselib=['GLFW','GLEW']
if sys.platform.startswith('darwin'):
- obj.framework=['OpenGL','Cocoa']
+ obj.framework=['OpenGL']
elif sys.platform.startswith('linux'):
obj.ldflags = [ "-lglfw", "-lGL" ]

0 comments on commit c61322a

Please sign in to comment.
Something went wrong with that request. Please try again.