Skip to content

Commit 9197e88

Browse files
committed
v1.0.1 commit.
This is the first commit.
1 parent a14fd2d commit 9197e88

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

86 files changed

+8368
-2
lines changed

README.md

+20-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,22 @@
11
HexGL
2-
=====
2+
=========
33

4-
Source code of HexGL, the futuristic HTML5 racing game by Thibaut Despoulain (me)
4+
Source code of [HexGL](http://hexgl.bkcore.com), the futuristic HTML5 racing game by [Thibaut Despoulain](http://bkcore.com)
5+
6+
## License
7+
8+
Unless specified in the file, HexGL's code and resources are licensed under the *Creative Commons Attribution-NonCommercial 3.0 Unported License*.
9+
10+
To view a copy of this license, visit http://creativecommons.org/licenses/by-nc/3.0/.
11+
12+
If you feel like you deserve another license for a special case, [drop me a note](http://bkcore.com/contact.html), and we'll talk about it.
13+
14+
## Regarding the code
15+
16+
As of now the code is pretty much undocumented. I'll be commenting it someday, but that won't be until I've finished the next content update and code refactoring sorry!
17+
18+
## Note
19+
20+
After much thinking, I've decided to release HexGL's source code under CC 3.0 BY-NC until further notice. This is not a definite choice, and the game may end up under the MIT license someday.
21+
22+
Feel free to post issues, patch or anything to make the game better.

bkcore/ImageData.js

+149
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
/*!
2+
* @class bkcore.ImageData
3+
*
4+
* Loads an image and gives access to pixel data.
5+
*
6+
* @author Thibaut 'BKcore' Despoulain <http://bkcore.com>
7+
*/
8+
9+
/*!
10+
* @package bkcore
11+
*/
12+
var bkcore = bkcore || {};
13+
14+
/*!
15+
* Creates a new ImageData object
16+
*
17+
* @param path String The path of the image
18+
* @param callback Function A callback function to be called once th eimage is loaded
19+
*/
20+
bkcore.ImageData = function(path, callback)
21+
{
22+
var self = this;
23+
24+
this.image = new Image();
25+
this.pixels = null;
26+
this.canvas = null;
27+
this.loaded = false;
28+
29+
this.image.onload = function() {
30+
self.canvas = document.createElement('canvas');
31+
self.canvas.width = self.image.width;
32+
self.canvas.height = self.image.height;
33+
34+
var context = self.canvas.getContext('2d');
35+
context.drawImage(self.image, 0, 0);
36+
37+
self.pixels = context.getImageData(0, 0, self.canvas.width, self.canvas.height);
38+
39+
self.loaded = true;
40+
41+
context = null;
42+
self.canvas = null;
43+
self.image = null;
44+
45+
if(callback) callback.call(self);
46+
};
47+
this.image.crossOrigin = "anonymous";
48+
this.image.src = path;
49+
};
50+
51+
/*!
52+
* Gets pixel RGBA data at given index
53+
*
54+
* @param x int In pixels
55+
* @param y int In pixels
56+
* @return Object{r,g,b,a}
57+
*/
58+
bkcore.ImageData.prototype.getPixel = function(x, y)
59+
{
60+
if(this.pixels == null
61+
|| x < 0
62+
|| y < 0
63+
|| x >= this.pixels.width
64+
|| y >= this.pixels.height)
65+
return {r: 0, g: 0, b: 0, a:0};
66+
67+
var index = (y*this.pixels.width + x) * 4;
68+
69+
return {
70+
r: this.pixels.data[index],
71+
g: this.pixels.data[index + 1],
72+
b: this.pixels.data[index + 2],
73+
a: this.pixels.data[index + 3]
74+
};
75+
};
76+
77+
/*!
78+
* Gets pixel RGBA data at given float index using bilinear interpolation
79+
*
80+
* @param x float In subpixels
81+
* @param y float In subpixels
82+
* @return Object{r,g,b,a}
83+
*/
84+
bkcore.ImageData.prototype.getPixelBilinear = function(fx, fy)
85+
{
86+
var x = Math.floor(fx);
87+
var y = Math.floor(fy);
88+
var rx = fx - x - .5;
89+
var ry = fy - y - .5;
90+
var ax = Math.abs(rx);
91+
var ay = Math.abs(ry);
92+
var c, cxy, cx, cy, cf1, cf2;
93+
var dx = rx < 0 ? -1 : 1;
94+
var dy = ry < 0 ? -1 : 1;
95+
96+
c = this.getPixel(x, y);
97+
cx = this.getPixel(x+dx, y);
98+
cy = this.getPixel(x, y+dy);
99+
cxy = this.getPixel(x+dx, y+dy);
100+
101+
cf1 = [
102+
(1-ax) * c.r + ax * cx.r,
103+
(1-ax) * c.g + ax * cx.g,
104+
(1-ax) * c.b + ax * cx.b,
105+
(1-ax) * c.a + ax * cx.a
106+
];
107+
108+
cf2 = [
109+
(1-ax) * cy.r + ax * cxy.r,
110+
(1-ax) * cy.g + ax * cxy.g,
111+
(1-ax) * cy.b + ax * cxy.b,
112+
(1-ax) * cy.a + ax * cxy.a
113+
];
114+
115+
return {
116+
r: (1-ay) * cf1[0] + ay * cf2[0],
117+
g: (1-ay) * cf1[1] + ay * cf2[1],
118+
b: (1-ay) * cf1[2] + ay * cf2[2],
119+
a: (1-ay) * cf1[3] + ay * cf2[3]
120+
};
121+
}
122+
123+
/*!
124+
* Gets pixel data at given index
125+
* as 3-bytes integer (for floating-point textures erzats, from RGB values)
126+
*
127+
* @param x int In pixels
128+
* @param y int In pixels
129+
* @return int (R + G*255 + B*255*255)
130+
*/
131+
bkcore.ImageData.prototype.getPixelF = function(x, y)
132+
{
133+
var color = this.getPixel(x, y);
134+
return color.r + color.g * 255 + color.b * 255 * 255;
135+
};
136+
137+
/*!
138+
* Gets pixel data at given float index using bilinear interpolationas
139+
* as 3-bytes integer (for floating-point textures erzats, from RGB values)
140+
*
141+
* @param x float In subpixels
142+
* @param y float In subpixels
143+
* @return Object{r,g,b,a}
144+
*/
145+
bkcore.ImageData.prototype.getPixelFBilinear = function(fx, fy)
146+
{
147+
var color = this.getPixelBilinear(fx, fy);
148+
return color.r + color.g * 255.0 + color.b * 255.0 * 255.0;
149+
}

bkcore/Timer.js

+123
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
/*!
2+
* @class bkcore.Timer
3+
*
4+
* new Date().getTime() wrapper to use as timers.
5+
*
6+
* @author Thibaut 'BKcore' Despoulain <http://bkcore.com>
7+
*/
8+
9+
/*!
10+
* @package bkcore
11+
*/
12+
var bkcore = bkcore || {};
13+
14+
/*!
15+
* Creates a new timer, inactive by default.
16+
* Call Timer.start() to activate.
17+
*/
18+
bkcore.Timer = function()
19+
{
20+
this.time = {
21+
start: 0,
22+
current: 0,
23+
previous: 0,
24+
elapsed: 0,
25+
delta: 0
26+
}
27+
28+
this.active = false;
29+
}
30+
31+
/*!
32+
* Starts/restarts the timer.
33+
*/
34+
bkcore.Timer.prototype.start = function()
35+
{
36+
var now = new Date().getTime();
37+
38+
this.time.start = now;
39+
this.time.current = now;
40+
this.time.previous = now;
41+
this.time.elapsed = 0;
42+
this.time.delta = 0;
43+
44+
this.active = true;
45+
}
46+
47+
/*!
48+
* Pauses(true)/Unpauses(false) the timer.
49+
*
50+
* @param bool Do pause
51+
*/
52+
bkcore.Timer.prototype.pause = function(bool)
53+
{
54+
this.active = !bool;
55+
}
56+
57+
/*!
58+
* Update method to be called inside a RAF loop
59+
*/
60+
bkcore.Timer.prototype.update = function()
61+
{
62+
if(!this.active) return;
63+
64+
var now = new Date().getTime();
65+
66+
this.time.current = now;
67+
this.time.elapsed = this.time.current - this.time.start;
68+
this.time.delta = now - this.time.previous;
69+
this.time.previous = now;
70+
}
71+
72+
/*!
73+
* Returns a formatted version of the current elapsed time using msToTime().
74+
*
75+
*
76+
*/
77+
bkcore.Timer.prototype.getElapsedTime = function()
78+
{
79+
return bkcore.Timer.msToTime(this.time.elapsed);
80+
}
81+
82+
/*!
83+
* Formats a millisecond integer into a h/m/s/ms object
84+
*
85+
* @param x int In milliseconds
86+
* @return Object{h,m,s,ms}
87+
*/
88+
bkcore.Timer.msToTime = function(t)
89+
{
90+
var ms, s, m, h;
91+
92+
ms = t%1000;
93+
94+
s = Math.floor((t/1000)%60);
95+
96+
m = Math.floor((t/60000)%60);
97+
h = Math.floor((t/3600000));
98+
99+
return {h:h, m:m, s:s, ms:ms};
100+
}
101+
102+
/*!
103+
* Formats a millisecond integer into a h/m/s/ms object with prefix zeros
104+
*
105+
* @param x int In milliseconds
106+
* @return Object<string>{h,m,s,ms}
107+
*/
108+
bkcore.Timer.msToTimeString = function(t)
109+
{
110+
var ms, s, m, h;
111+
112+
ms = t%1000;
113+
if(ms < 10) ms = "00"+ms;
114+
else if(ms < 100) ms = "0"+ms;
115+
116+
s = Math.floor((t/1000)%60);
117+
if(s < 10) s = "0"+s;
118+
119+
m = Math.floor((t/60000)%60);
120+
h = Math.floor((t/3600000));
121+
122+
return {h:h, m:m, s:s, ms:ms};
123+
}

0 commit comments

Comments
 (0)