-
Notifications
You must be signed in to change notification settings - Fork 1
/
tinygame.js
66 lines (53 loc) · 1.52 KB
/
tinygame.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
pre: c has to have an x,y and width and height
this ended up being a useful routine. anything that has
an x,y,width,height (aka a sprite) can use it.
*/
function tinyGameCheckObject(c,pointerX,pointerY) {
if ((pointerX>=c.x) && (pointerX<=(c.x+c.width)) &&
(pointerY>=c.y) && (pointerY<=(c.y+c.height))) {
return true;
}
return false;
}
function timestamp() {
if (window.performance && window.performance.now)
return window.performance.now();
else
return new Date().getTime();
}
function tinyGameRender() {
game.ctx.clearRect(0,0,W,H);
game.render();
}
function tinyGameUpdate() {
game.update();
}
// this is really just standard stuff and there are probably
// even better ways to do it.
// it worked for me...
function tinyGameFrame() {
game.now = timestamp();
game.dt = game.dt + Math.min(1, (game.now - game.last) / 1000);
while(game.dt > game.step) {
game.dt = game.dt - game.step;
tinyGameUpdate(game.step);
}
tinyGameRender(game.ctx, game.dt);
game.last = game.now;
requestAnimationFrame(tinyGameFrame, game.canvas);
}
function tinyGameInit() {
game.fps = 60;
game.step = 1/game.fps;
game.dt = 0;
game.now = 0;
game.last = timestamp();
game.canvas = document.getElementById("c");
game.ctx = game.canvas.getContext("2d");
W = game.canvas.width;
H = game.canvas.height;
console.log("tinyGameInit:",W,H);
console.log("tinyGameInit:canvas",game.canvas);
tinyGameFrame();
}