Skip to content

Commit

Permalink
Node-ify modules, separate out render loop and step loop
Browse files Browse the repository at this point in the history
  • Loading branch information
cykod committed Nov 3, 2014
1 parent 175947b commit bac4f2d
Show file tree
Hide file tree
Showing 13 changed files with 210 additions and 24 deletions.
1 change: 1 addition & 0 deletions examples/server/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
26 changes: 26 additions & 0 deletions examples/server/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
var Quintus = require("../../lib/quintus.js");

require("../../lib/quintus_sprites.js")(Quintus);
require("../../lib/quintus_scenes.js")(Quintus);

var Q = Quintus().include("Sprites, Scenes");


Q.Sprite.extend("Box", {
step: function(dt) {
console.log("p");
}
});



Q.scene("level1",function(stage) {
stage.insert(new Q.Box({ x: 10, y: 50 }));
});


Q.gameLoop(Q.stageStepLoop);
Q.stageScene("level1");



8 changes: 8 additions & 0 deletions examples/server/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "quintus-server-example",
"version": "0.0.1",
"description": "Quintus Multi-user server example",
"dependencies": {
"express": "^4.10.1"
}
}
40 changes: 33 additions & 7 deletions lib/quintus.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/*global module:false */


// Quintus Game Engine
// (c) 2012 Pascal Rettig, Cykod LLC
// Quintus may be freely distributed under the MIT license or GPLv2 License.
Expand Down Expand Up @@ -25,6 +28,9 @@ various other modules:
@module Quintus
*/

var quintusCore = function(window,key) {
"use strict";

/**
Top-level Quintus engine factory wrapper,
creates new instances of the engine by calling:
Expand All @@ -46,7 +52,7 @@ various other modules:
@class Quintus
**/
var Quintus = function Quintus(opts) {
window[key] =function Quintus(opts) {

/**
A la jQuery - the returned `Q` object is actually
Expand Down Expand Up @@ -523,6 +529,13 @@ var Quintus = function Quintus(opts) {
};
if(opts) { Q._extend(Q.options,opts); }

Q.scheduleFrame = function(callback) {
return window.requestAnimationFrame(callback);
};

Q.cancelFrame = function(loop) {
window.cancelAnimationFrame(loop);
};

/**
Game Loop support
Expand Down Expand Up @@ -565,18 +578,21 @@ var Quintus = function Quintus(opts) {
Q.gameLoopCallbackWrapper = function() {
var now = new Date().getTime();
Q._loopFrame++;
Q.loop = window.requestAnimationFrame(Q.gameLoopCallbackWrapper);
Q.loop = Q.scheduleFrame(Q.gameLoopCallbackWrapper);
var dt = now - Q.lastGameLoopFrame;
/* Prevent fast-forwarding by limiting the length of a single frame. */
if(dt > Q.options.frameTimeLimit) { dt = Q.options.frameTimeLimit; }
callback.apply(Q,[dt / 1000]);
Q.lastGameLoopFrame = now;
};

window.requestAnimationFrame(Q.gameLoopCallbackWrapper);
Q.scheduleFrame(Q.gameLoopCallbackWrapper);
return Q;
};




/**
Pause the entire game by canceling the requestAnimationFrame call. If you use setTimeout or
setInterval in your game, those will, of course, keep on rolling...
Expand All @@ -586,7 +602,7 @@ var Quintus = function Quintus(opts) {
*/
Q.pauseGame = function() {
if(Q.loop) {
window.cancelAnimationFrame(Q.loop);
Q.cancelFrame(Q.loop);
}
Q.loop = null;
};
Expand All @@ -602,7 +618,7 @@ var Quintus = function Quintus(opts) {
Q.unpauseGame = function() {
if(!Q.loop) {
Q.lastGameLoopFrame = new Date().getTime();
Q.loop = window.requestAnimationFrame(Q.gameLoopCallbackWrapper);
Q.loop = Q.scheduleFrame(Q.gameLoopCallbackWrapper);
}
};

Expand Down Expand Up @@ -1265,7 +1281,7 @@ var Quintus = function Quintus(opts) {



Q.touchDevice = ('ontouchstart' in document);
Q.touchDevice = (typeof exports === 'undefined') && ('ontouchstart' in document);

/**
Expand Down Expand Up @@ -2245,7 +2261,7 @@ var Quintus = function Quintus(opts) {
window.requestAnimationFrame = function(callback, element) {
var currTime = new Date().getTime();
var timeToCall = Math.max(0, 16 - (currTime - lastTime));
var id = window.setTimeout(function() { callback(currTime + timeToCall); },
var id = setTimeout(function() { callback(currTime + timeToCall); },
timeToCall);
lastTime = currTime + timeToCall;
return id;
Expand All @@ -2260,3 +2276,13 @@ var Quintus = function Quintus(opts) {
}());


return Quintus;
};

if(typeof exports === 'undefined') {
quintusCore(this,"Quintus");
} else {
var Quintus = quintusCore(module,"exports");
}


15 changes: 14 additions & 1 deletion lib/quintus_2d.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
/*global Quintus:false */
/*global Quintus:false, module:false */

var quintus2D = function(Quintus) {
"use strict";

Quintus["2D"] = function(Q) {

Expand Down Expand Up @@ -531,3 +534,13 @@ Quintus["2D"] = function(Q) {
});

};


};


if(typeof Quintus === 'undefined') {
module.exports = quintus2D;
} else {
quintus2D(Quintus);
}
13 changes: 12 additions & 1 deletion lib/quintus_anim.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
/*global Quintus:false */
/*global Quintus:false, module:false */

var quintusAnim = function(Quintus) {
"use strict";

Quintus.Anim = function(Q) {

Expand Down Expand Up @@ -272,3 +275,11 @@ Quintus.Anim = function(Q) {

};


};

if(typeof Quintus === 'undefined') {
module.exports = quintusAnim;
} else {
quintusAnim(Quintus);
}
15 changes: 13 additions & 2 deletions lib/quintus_audio.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
/*global Quintus:false, AudioContext:false, window:false */
/*global Quintus:false, AudioContext:false, window:false, module: false */

var quintusAudio = function(Quintus) {
"use strict";

Quintus.Audio = function(Q) {

Expand All @@ -21,7 +24,7 @@ Quintus.Audio = function(Q) {
}

Q.enableSound = function() {
var hasTouch = !!('ontouchstart' in window);
var hasTouch = (typeof window !== "undefined") && !!('ontouchstart' in window);

if(Q.hasWebAudio) {
Q.audio.enableWebAudioSound();
Expand Down Expand Up @@ -155,3 +158,11 @@ Quintus.Audio = function(Q) {

};


};

if(typeof Quintus === 'undefined') {
module.exports = quintusAudio;
} else {
quintusAudio(Quintus);
}
15 changes: 14 additions & 1 deletion lib/quintus_input.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/*global Quintus:false */
/*global Quintus:false, module:false */

/**
Quintus HTML5 Game Engine - Input Module
Expand All @@ -8,6 +9,10 @@ concerns itself with game-type (pretty anything besides touchscreen input)
@module Quintus.Input
*/


var quintusInput = function(Quintus) {
"use strict";

/**
* Quintus Input Module
*
Expand Down Expand Up @@ -953,3 +958,11 @@ Quintus.Input = function(Q) {
});
};


};

if(typeof Quintus === 'undefined') {
module.exports = quintusInput;
} else {
quintusInput(Quintus);
}
33 changes: 28 additions & 5 deletions lib/quintus_scenes.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/*global Quintus:false */
/*global Quintus:false, module:false */


/**
Quintus HTML5 Game Engine - Scenes Module
Expand All @@ -16,6 +17,10 @@ stepping, rendering and collision detection.
@module Quintus.Scenes
*/


var quintusScenes = function(Quintus) {
"use strict";

/**
* Quintus Scenes Module Class
*
Expand Down Expand Up @@ -1087,7 +1092,7 @@ Quintus.Scenes = function(Q) {
return stage;
};

Q.stageGameLoop = function(dt) {
Q.stageStepLoop = function(dt) {
var i,len,stage;


Expand All @@ -1102,19 +1107,29 @@ Quintus.Scenes = function(Q) {
}
}

Q.activeStage = 0;
};

Q.stageRenderLoop = function() {

if(Q.ctx) { Q.clear(); }

for(i =0,len=Q.stages.length;i<len;i++) {
for(var i =0,len=Q.stages.length;i<len;i++) {
Q.activeStage = i;
stage = Q.stage();
var stage = Q.stage();
if(stage) {
stage.render(Q.ctx);
}
}

if(Q.input && Q.ctx) { Q.input.drawCanvas(Q.ctx); }

Q.activeStage = 0;
};

if(Q.input && Q.ctx) { Q.input.drawCanvas(Q.ctx); }
Q.stageGameLoop = function(dt) {
Q.stageStepLoop(dt);
Q.stageRenderLoop();
};

/**
Expand Down Expand Up @@ -1147,3 +1162,11 @@ Quintus.Scenes = function(Q) {

};


};

if(typeof Quintus === 'undefined') {
module.exports = quintusScenes;
} else {
quintusScenes(Quintus);
}
16 changes: 15 additions & 1 deletion lib/quintus_sprites.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/*global Quintus:false */
/*global Quintus:false, module:false */

/**
Quintus HTML5 Game Engine - Sprites Module
Expand All @@ -11,6 +12,10 @@ Most games will include at a minimum `Quintus.Sprites` and `Quintus.Scenes`
*/


var quintusSprites = function(Quintus) {
"use strict";


/**
* Quintus Sprites Module Class
*
Expand Down Expand Up @@ -754,3 +759,12 @@ Quintus.Sprites = function(Q) {
return Q;
};


};


if(typeof Quintus === 'undefined') {
module.exports = quintusSprites;
} else {
quintusSprites(Quintus);
}
16 changes: 14 additions & 2 deletions lib/quintus_tmx.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/*global Quintus:false */
/*global Quintus:false, module:false */

/*global Quintus:false */
/**
Quintus HTML5 Game Engine - TMX Loader module
Expand All @@ -9,6 +8,10 @@ Module responsible for loading Tiled TMX files
@module Quintus.Input
*/


var quintusTMX = function(Quintus) {
"use strict";

/**
* Quintus TMX Loading module
*
Expand Down Expand Up @@ -258,3 +261,12 @@ Quintus.TMX = function(Q) {

};


};


if(typeof Quintus === 'undefined') {
module.exports = quintusTMX;
} else {
quintusTMX(Quintus);
}
Loading

0 comments on commit bac4f2d

Please sign in to comment.