BlueGit

timfa frameworkv3

Files

frameworkv3 / framework / window.js

window.js

remov
timfa Tim · pushed by timfa · 4 years ago
d238f51f History
window.js 2.0 KiB Download Raw
1
2
3var GameWindow =
4{
5
6 width: 0,
7
8 height: 0,
9
10 g_width: 0,
11 g_height: 0,
12
13 cameraPosition: new vector(0, 0),
14
15 fullscreen: false,
16
17
18 backgroundColor:
19 {
20 r: 255,
21 g: 255,
22 b: 255
23 },
24
25 windowElement: null,
26 elementID: "GameBuilder_Window",
27
28 init: function(x, y, width, height)
29 {
30 GameWindow.x = x;
31 GameWindow.y = y;
32
33 GameWindow.width = width;
34 GameWindow.height = height;
35
36 GameWindow.g_width = width;
37 GameWindow.g_height = height;
38
39 var body = document.body;
40 body.insertAdjacentHTML('beforeend', '<canvas id="' + GameWindow.elementID + '" style="position: absolute; left: ' + x + 'px; top: ' + y + 'px; border: 1px solid black;" width="' + GameWindow.width + '" height="' + GameWindow.height + '"></canvas>');
41
42 GameWindow.windowElement = document.getElementById(GameWindow.elementID);
43
44 GameWindow.canvas = document.getElementById(GameWindow.elementID);
45
46 GameWindow.canvasContext = GameWindow.canvas.getContext('2d');
47
48 console.log("GAME: Window initialized: [" + width + ", " + height + "]");
49 },
50
51 redraw: function()
52 {
53 TWEEN.update();
54
55 if(GameWindow.fullscreen)
56 {
57 GameWindow.width = window.innerWidth - 2;
58 GameWindow.height = window.innerHeight - 2;
59 }
60 else
61 {
62 GameWindow.width = GameWindow.g_width;
63 GameWindow.height = GameWindow.g_height;
64 }
65
66 GameWindow.canvas.width = GameWindow.width;
67 GameWindow.canvas.height = GameWindow.height;
68
69 GameWindow.canvasContext.fillStyle = "rgb(" + GameWindow.backgroundColor.r + "," + GameWindow.backgroundColor.g + "," + GameWindow.backgroundColor.b + ")"; // sets the color to fill in the rectangle with
70 GameWindow.canvasContext.fillRect(0, 0, GameWindow.width, GameWindow.height);
71
72 for(var i = 0; i < Game.activeScene.activeObjects.length; i++)
73 {
74 if(Game.activeScene.activeObjects[i].active && Game.activeScene.activeObjects[i].renderer.visible)
75 Game.activeScene.activeObjects[i].g_draw(GameWindow.canvasContext);
76 }
77
78 requestAnimationFrame(GameWindow.redraw);
79 }
80}