Skip to content

Instantly share code, notes, and snippets.

@sjkillen
Created January 2, 2014 08:07
Show Gist options
  • Save sjkillen/8216262 to your computer and use it in GitHub Desktop.
Save sjkillen/8216262 to your computer and use it in GitHub Desktop.
Html5 Game Handy Framework
loadJavascript('html_event_listeners.js');
loadJavascript('main.js');
htmlEvent=new Object();
htmlEvent.windowResize=function()
{
gwidth=window.innerWidth;
gheight=window.innerHeight;
canvas.element.width=gwidth;
canvas.element.height=gheight;
}
window.addEventListener('resize',htmlEvent.windowResize);
<html>
<head>
<title>HTML5 GAME FRAMEWORK</title>
</head>
<body>
<style>
canvas
{
position:absolute;
left:0;
top:0;
background:navy;
}
</style>
<canvas id="canvas"></canvas>
<script src="js_file_loader.js"></script>
<script src="file_init.js"></script>
</body>
</html>
function loadJavascript(path)
{
if (typeof (path)==='string')
{
var element=document.createElement("script");
element.src=path;
document.body.appendChild(element);
}
else {error('Cannot load Javascript, path is not a string');}
}
canvas=
{
element:document.getElementById('canvas'),
context:document.getElementById('canvas').getContext('2d')
};
fps=
{
draw:1000/60,
think:1000/60
};
game=new Object();
game.vSync=true;
game.looping=false;
htmlEvent.windowResize();
game.thinkLoop=function()
{
};
game.drawLoop=function()
{
};
game.startLoops=function()
{
if (!game.looping)
{
game.thinkLoopSession=setInterval(game.thinkLoop,fps.think);
if (game.vSync) {game.drawLoopSession=requestAnimationFrame(game.drawLoop);}
else {game.drawLoopSession=setInterval(game.drawLoop,fps.draw);}
game.looping=true;
}
else{error('Game is already looping');}
}
game.haltLoops=function()
{
if (game.looping)
{
if (game.vSync) {window.cancelAnimationFrame(game.drawLoopSession);}
else {clearInterval(game.drawLoopSession);}
clearInterval(game.thinkLoopSession);
game.looping=false;
}
else{error('Game is not looping');}
}
function error(err)
{
if (typeof (err)=="string") {console.log('ERROR: '+err);}
else {console.log('ERROR REASON NOT GIVEN');}
game.haltLoops();
alert('Game has crashed, please restart');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment