BlueGit

timfa raytracer

Files

raytracer / archive / legacy-js / js / GameWindow.js

GameWindow.js

GameWindow.js 1.9 KiB Download Raw
1var GameWindow =
2{
3 width: 0,
4
5 height: 0,
6
7 g_width: 0,
8 g_height: 0,
9
10 fullscreen: false,
11
12 backgroundColor:
13 {
14 r: 0,
15 g: 0,
16 b: 0
17 },
18
19 windowElement: null,
20 elementID: "GameBuilder_Window",
21
22 init: function(x, y, width, height)
23 {
24 GameWindow.x = x;
25 GameWindow.y = y;
26
27 GameWindow.width = width;
28 GameWindow.height = height;
29
30 GameWindow.g_width = width;
31 GameWindow.g_height = height;
32
33 var body = document.body;
34 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>');
35
36 //$("body").append('<div id="' + GameWindow.elementID + '" style="position: absolute; left: ' + x + 'px; top: ' + y + 'px; width: ' + GameWindow.width + 'px; height: ' + GameWindow.height + 'px; overflow: hidden; border: 1px solid black;"></div>');
37 GameWindow.windowElement = document.getElementById(GameWindow.elementID);
38
39 GameWindow.canvas = document.getElementById(GameWindow.elementID);
40
41 GameWindow.canvasContext = GameWindow.canvas.getContext('2d');
42
43 console.log("GAME: Window initialized: [" + width + ", " + height + "]");
44 },
45
46 redraw: function()
47 {
48 if(GameWindow.fullscreen)
49 {
50 GameWindow.width = window.innerWidth - 2;
51 GameWindow.height = window.innerHeight - 2;
52 }
53 else
54 {
55 GameWindow.width = GameWindow.g_width;
56 GameWindow.height = GameWindow.g_height;
57 }
58
59 GameWindow.canvas.width = GameWindow.width;
60 GameWindow.canvas.height = GameWindow.height;
61
62 GameWindow.canvasContext.fillStyle = "rgb(" + GameWindow.backgroundColor.r + "," + GameWindow.backgroundColor.g + "," + GameWindow.backgroundColor.b + ")"; // sets the color to fill in the rectangle with
63 GameWindow.canvasContext.fillRect(0, 0, GameWindow.width, GameWindow.height);
64
65 Player.draw3D();
66
67 requestAnimationFrame(GameWindow.redraw);
68 }
69}