Skip to content
Browse files

Include small bit of test code.

  • Loading branch information...
1 parent d236efd commit d64febd773afdc15377fc71912f14e127f4a1f25 Vincent Kuyatt committed Jan 21, 2014
Showing with 372 additions and 1 deletion.
  1. +250 −0 engine.js
  2. +6 −1 src/window.cc
  3. +116 −0 test.js
View
250 engine.js
@@ -0,0 +1,250 @@
+var sdl = require("./build/Release/node_sdl.node");
+
+var entities = [];
+var msPerFrame = 0;
+var sPerFrame = 0;
+var window, renderer, world;
+var _quitFun;
+var _going = true;
+
+var tickerEvent = function() {
+ var event;
+ while((event = sdl.pollEvent()) !== undefined) {
+ if(_quitFun(event, world) || sdl.quitRequested()) {
+ _going = false;
+ }
+ // for(var i in entities) {
+ // entities[i].processInput(event, world);
+ // }
+ // for(i = 0; i < entities.length; i++) {
+ // entities[i].processInput(event, world);
+ // }
+ var i = entities.length;
+ while(i--) {
+ entities[i].processInput(event, world);
+ }
+ if(event.type === "KEYDOWN") {
+ if(event.scancode == sdl.SCANCODE.Q) {
+ _going = false;
+ }
+ }
+ }
+}
+var tickerTick = function() {
+ for(i = 0; i < entities.length; i++) {
+ entities[i].tick(sPerFrame, world);
+ }
+}
+var tickerRender = function() {
+ var befc = process.hrtime();
+ renderer.clear();
+ // console.log("Clear took: " + hrtoms(process.hrtime(befc)) + "ms.");
+ var befl = process.hrtime();
+ for(i = 0; i < entities.length; i++) {
+ entities[i].render(renderer);
+ }
+ // console.log("Render loop took: " + hrtoms(process.hrtime(befl)) + "ms.");
+}
+var ticker = function() {
+ var start = process.hrtime();
+
+ // Process things.
+ var befev = process.hrtime();
+ tickerEvent();
+ // console.log("Event took: " + hrtoms(process.hrtime(befev)) + "ms.");
+ var beft = process.hrtime();
+ tickerTick();
+ // console.log("Tick took: " + hrtoms(process.hrtime(beft)) + "ms.");
+ var befr = process.hrtime();
+ tickerRender();
+ // console.log("Render took: " + hrtoms(process.hrtime(befr)) + "ms.");
+
+ var delta = hrtoms(process.hrtime(start));
+ // console.log("Ticker took: " + delta + "ms.");
+ // var timeout = msPerFrame - delta;
+ // if(timeout < 0) {
+ // timeout = 0;
+ // }
+ // console.log("Setting timeout: " + timeout);
+ // if(_going) {
+ // setTimeout(ticker, timeout);
+ // }
+}
+var baseline;
+var last, lastpresent, lastpresentdelta;
+var presenter = function() {
+ while(_going) {
+ if(last) {
+ var calldelta = hrtoms(process.hrtime(last));
+ // console.log("Time to have presenter called: " + calldelta + "ms.");
+ }
+
+ var befp = process.hrtime();
+ // console.log("Calling present at: " + (new Date()).toISOString());
+ var calledat = hrtoms(process.hrtime(baseline));
+ renderer.present();
+ var delta = hrtoms(process.hrtime(befp));
+ if(delta > 20) {
+ console.log("Present took too long: " + delta + "ms.");
+ // console.log("Last present at: " + lastpresent);
+ // console.log("Was called at: " + calledat);
+ // console.log("Last delta: " + lastpresentdelta);
+ }
+
+ if(_going) {
+ // var wait = 16.0 - (delta % 16.0) + 1;
+ // var wait = 16.7 - (delta % 16.7) + 1;
+ // if(delta < 16) {
+ // wait = 16 - delta;
+ // }
+ // else {
+ // wait = 32 - delta;
+ // }
+ // console.log("Setting presenter timeout: " + wait);
+ // setTimeout(presenter, 1);
+ last = process.hrtime();
+ lastpresent = calledat;
+ lastpresentdelta = delta;
+ // setImmediate(presenter);
+ var beft = process.hrtime();
+ ticker();
+ // console.log("Ticker took: " + hrtoms(process.hrtime(beft)) + "ms.");
+ }
+ }
+}
+var hrtoms = function(hr) {
+ return (hr[0] * 1e9 + hr[1]) / 1e6;
+}
+
+module.exports = {
+ Entity: function (args) {
+ this._pos = {x: 0, y: 0};
+ this._size = {x: 0, y: 0};
+ this._vel = {x: 0, y: 0};
+ this._accel = {x: 0, y: 0};
+
+ this.pos = args.pos;
+ this.size = args.size;
+ this.vel = args.vel;
+ this.accel = args.accel;
+ this._texture = args.texture === undefined ? testTex : args.texture;
+ this._id = args.id === undefined ? "BasicID" : args.id;
+
+ this.render = args.render || this.render;
+ this.processInput = args.processInput || this.processInput;
+ this.tick = args.tick || this.tick;
+
+ entities.push(this);
+ },
+ choice: function (l) {
+ var chosen = Math.floor(Math.random() * l.length);
+ return l[chosen];
+ },
+ gameLoop: function(fps, setupFun, quitFun, windowArgs) {
+ msPerFrame = 1000 / fps;
+ sPerFrame = msPerFrame / 1000;
+ _quitFun = quitFun;
+
+ window = new sdl.Window(!!windowArgs.title ? "Default" : windowArgs.title,
+ !!windowArgs.x ? sdl.WINDOWPOS.CENTERED : windowArgs.x,
+ !!windowArgs.y ? sdl.WINDOWPOS.CENTERED : windowArgs.y,
+ !!windowArgs.w ? 640 : windowArgs.w,
+ !!windowArgs.h ? 480 : windowArgs.h);
+ // msPerFrame = 1000 / window.getDisplayMode().refreshRate;
+ // sPerFrame = msPerFrame / 1000;
+ renderer = new sdl.Renderer(window, -1, sdl.RENDERER.ACCELERATED | sdl.RENDERER.PRESENTVSYNC);
+ // renderer = new sdl.Renderer(window, -1, sdl.RENDERER.ACCELERATED);
+ world = setupFun(window, renderer);
+ baseline = process.hrtime();
+ renderer.clear();
+ presenter();
+ // setTimeout(presenter, 0);
+ // setTimeout(ticker, 0);
+ // ticker();
+ }
+}
+
+module.exports.Entity.prototype = {
+ move: function(delta) {
+ this.x = this.x + delta.x;
+ this.y = this.y + delta.y;
+ },
+
+ get x() {
+ return this._pos.x;
+ },
+ get y() {
+ return this._pos.y;
+ },
+ set x(x) {
+ this._pos.x = x;
+ },
+ set y(y) {
+ this._pos.y = y;
+ },
+ get pos() {
+ return {x: this._pos.x, y: this._pos.y};
+ },
+ set pos(pos) {
+ this._pos.x = pos.x === undefined ? this._pos.x : pos.x;
+ this._pos.y = pos.y === undefined ? this._pos.y : pos.y;
+ },
+
+ get w() {
+ return this._size.x;
+ },
+ get h() {
+ return this._size.y;
+ },
+ set w(w) {
+ this._size.x = w;
+ },
+ set h(h) {
+ this._size.y = h;
+ },
+ get size() {
+ return {x: this._size.x, y: this._size.y};
+ },
+ set size(size) {
+ if(size) {
+ this._size.x = size.x === undefined ? this._size.x : size.x;
+ this._size.y = size.y === undefined ? this._size.y : size.y;
+ }
+ },
+
+ get vel() {
+ return this._vel;
+ },
+ set vel(vel) {
+ if(vel) {
+ this._vel.x = vel.x === undefined ? this._vel.x : vel.x;
+ this._vel.y = vel.y === undefined ? this._vel.y : vel.y;
+ }
+ },
+
+ get accel() {
+ return {x: this._accel.x, y: this._accel.y};
+ },
+ set accel(accel) {
+ if(accel) {
+ this._accel.x = accel.x === undefined ? this._accel.x : accel.x;
+ this._accel.y = accel.y === undefined ? this._accel.y : accel.y;
+ }
+ },
+
+ processInput: function(event, world) {
+ // console.log("Entity basic processInput: " + event.type);
+ },
+ render: function(renderer) {
+ renderer.copy(this._texture, undefined, new sdl.Rect(this.x, this.y, this.w, this.h));
+ },
+ tick: function(dt, world) {
+ // console.log("accel: " + JSON.stringify(this._accel));
+ this._vel.x += this._accel.x * dt;
+ this._vel.y += this._accel.y * dt;
+ // console.log("vel: " + JSON.stringify(this._vel));
+ this._pos.x += this._vel.x * dt;
+ this._pos.y += this._vel.y * dt;
+ // console.log("pos: " + JSON.stringify(this._pos));
+ }
+};
View
7 src/window.cc
@@ -36,6 +36,7 @@ void sdl::WindowWrapper::Init(Handle<Object> exports) {
window_wrap_template_->SetClassName(String::NewSymbol("WindowWrapper"));
NODE_SET_PROTOTYPE_METHOD(window_wrap_template_, "getBrightness", GetBrightness);
+ NODE_SET_PROTOTYPE_METHOD(window_wrap_template_, "getDisplayMode", GetDisplayMode);
NODE_SET_PROTOTYPE_METHOD(window_wrap_template_, "getDisplayIndex", GetDisplayIndex);
NODE_SET_PROTOTYPE_METHOD(window_wrap_template_, "getFlags", GetFlags);
NODE_SET_PROTOTYPE_METHOD(window_wrap_template_, "getGammaRamp", GetGammaRamp);
@@ -130,7 +131,11 @@ Handle<Value> sdl::WindowWrapper::GetDisplayMode(const Arguments& args) {
WindowWrapper* obj = ObjectWrap::Unwrap<WindowWrapper>(args.This());
SDL_DisplayMode mode;
- int err = SDL_GetWindowDisplayMode(obj->window_, &mode);
+ int err = SDL_GetWindowDisplayIndex(obj->window_);
+ if(err < 0) {
+ return ThrowSDLException(__func__);
+ }
+ err = SDL_GetCurrentDisplayMode(err, &mode);
// SDL documentation says that 0 is success, and less than 0 is when an error
// occurred.
if(err < 0) {
View
116 test.js
@@ -0,0 +1,116 @@
+var sdl = require("./build/Release/node_sdl.node");
+var eng = require("./engine.js");
+
+var width = 640;
+var height = 480;
+var title = "Pong";
+var fps = 60;
+
+var paddleSize = {x: width / 10, y: width / 20};
+var paddleSpeed = 300;
+
+var paddleInput = function(event, world) {
+ if(event.repeat !== undefined && event.repeat) {
+ return;
+ }
+
+ if(event.type === "KEYDOWN") {
+ if(event.scancode === sdl.SCANCODE.LEFT) {
+ this.vel.x += -paddleSpeed;
+ }
+ else if(event.scancode === sdl.SCANCODE.RIGHT) {
+ this.vel.x += paddleSpeed;
+ }
+ }
+ else if(event.type === "KEYUP") {
+ if(event.scancode === sdl.SCANCODE.LEFT) {
+ this.vel.x += paddleSpeed;
+ }
+ else if(event.scancode === sdl.SCANCODE.RIGHT) {
+ this.vel.x += -paddleSpeed;
+ }
+ }
+}
+var paddleTick = function(dt, world) {
+ eng.Entity.prototype.tick.apply(this, [dt, world]);
+
+ if(this.x < world.leftWall.x + world.leftWall.w) {
+ this.x = world.leftWall.x + world.leftWall.w - 1;
+ }
+ else if(this.x + this.w > world.rightWall.x) {
+ this.x = world.rightWall.x - this.w;
+ }
+}
+
+var ballTick = function(dt, world) {
+ eng.Entity.prototype.tick.apply(this, [dt, world]);
+
+ if(this.x < world.leftWall.x + world.leftWall.w) {
+ this.vel.x = -this.vel.x;
+ }
+ else if(this.x + this.w > world.rightWall.x) {
+ this.vel.x = -this.vel.x;
+ }
+ else if(this.y < 0) {
+ this.vel.y = -this.vel.y;
+ }
+ else if(this.y + this.h > height) {
+ this.vel.y = -this.vel.y;
+ }
+}
+
+var shouldQuit = function(event, world) {
+ if(event.type === "KEYDOWN") {
+ if(event.scancode === sdl.SCANCODE.Q) {
+ return true;
+ }
+ }
+ return false;
+}
+
+var setup = function(window, renderer) {
+ var world = {};
+
+ var surface = new sdl.Surface(paddleSize.x, paddleSize.y);
+ surface.fillRect(new sdl.Color(255, 255, 255).getColor(surface.getPixelFormat()));
+ world.paddleTex = new sdl.Texture(renderer, surface);
+
+ surface = new sdl.Surface(width / 30, width / 30);
+ surface.fillRect(new sdl.Color(255, 255, 255).getColor(surface.getPixelFormat()));
+ world.ballTex = new sdl.Texture(renderer, surface);
+
+ surface = new sdl.Surface(width / 100, height);
+ surface.fillRect(new sdl.Color(255, 255, 255).getColor(surface.getPixelFormat()));
+ world.wallTex = new sdl.Texture(renderer, surface);
+
+ world.paddle = new eng.Entity({
+ texture: world.paddleTex,
+ processInput: paddleInput,
+ tick: paddleTick,
+ pos: {y: height - paddleSize.y * 2, x: width / 2 - paddleSize.x},
+ size: paddleSize
+ });
+
+ world.leftWall = new eng.Entity({
+ texture: world.wallTex,
+ pos: {y: 0, x: (width / 100) * 2},
+ size: {x: width / 100, y: height}
+ });
+ world.rightWall = new eng.Entity({
+ texture: world.wallTex,
+ pos: {y: 0, x: width - (width / 100) * 2},
+ size: {x: width / 100, y: height}
+ });
+
+ world.ball = new eng.Entity({
+ texture: world.ballTex,
+ tick: ballTick,
+ pos: {y: height / 2, x: width / 2},
+ size: {x: width / 30, y: width / 30},
+ vel: {x: 100, y: 100}
+ });
+
+ return world;
+}
+
+eng.gameLoop(fps, setup, shouldQuit, {width: width, height: height, title: title});

0 comments on commit d64febd

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