Add WebGL sprite rendering with optional shaders.
Replace Canvas2D drawing with a GL path, default shaders matching prior look, and preloadShaders for game-authored fragment/vertex files. Co-authored-by: Cursor <cursoragent@cursor.com>
@@ -1,24 +1,4 @@
1
1
#!/usr/bin/env bash
2
BUILD_FILE="../js/framework.js"
3
SRC_DIR="../framework"
4
5
printf "Build started, output file: $BUILD_FILE\n"
6
unlink $BUILD_FILE
7
8
loop() {
9
shopt -s nullglob
10
shopt -s globstar
11
12
for file in "$@"/**
13
do
14
if [[ -f "$file" && "${file##*.}" = "js" ]]
15
then
16
printf "\n/*\n * Start File:\n * "$file"\n */ \n" >> "$BUILD_FILE";
17
printf "Building "$file"\n";
18
cat "$file" >> "$BUILD_FILE";
19
printf "\n/*\n * End File:\n * "$file"\n */ \n" >> "$BUILD_FILE";
20
fi
21
done
22
}
23
loop $SRC_DIR
24
printf "DONE"
2
# Keep in sync with ../glbuild.sh — VS Code tasks may call this path.
3
cd "$(dirname "$0")/.."
4
exec ./glbuild.sh
@@ -1,24 +1,4 @@1
#!/usr/bin/env bash1
#!/usr/bin/env bash2
BUILD_FILE="../js/framework.js"2
# Keep in sync with ../glbuild.sh — VS Code tasks may call this path.3
SRC_DIR="../framework"3
cd "$(dirname "$0")/.."4
4
exec ./glbuild.sh5
printf "Build started, output file: $BUILD_FILE\n"6
unlink $BUILD_FILE7
8
loop() {9
shopt -s nullglob10
shopt -s globstar11
12
for file in "$@"/**13
do14
if [[ -f "$file" && "${file##*.}" = "js" ]]15
then16
printf "\n/*\n * Start File:\n * "$file"\n */ \n" >> "$BUILD_FILE"; 17
printf "Building "$file"\n";18
cat "$file" >> "$BUILD_FILE"; 19
printf "\n/*\n * End File:\n * "$file"\n */ \n" >> "$BUILD_FILE"; 20
fi21
done22
}23
loop $SRC_DIR24
printf "DONE"@@ -8,6 +8,7 @@ class Game
8
8
rafId = null;
9
9
10
10
images = {};
11
shaders = {};
11
12
12
13
constructor ()
13
14
{
@@ -15,6 +16,7 @@ class Game
15
16
this.activeScenes = [];
16
17
this.isRunning = false;
17
18
this.rafId = null;
19
this.shaders = {};
18
20
this.update = this.update.bind(this);
19
21
}
20
22
@@ -117,6 +119,55 @@ class Game
117
119
return this.images;
118
120
}
119
121
122
async preloadShaders (shadersData)
123
{
124
const context = this;
125
const promises = [];
126
127
for (const shaderData of shadersData)
128
{
129
if (!shaderData || !shaderData.name)
130
{ throw new Error("preloadShaders: each entry needs a name"); }
131
132
if (!shaderData.frag)
133
{ throw new Error("preloadShaders: '" + shaderData.name + "' needs a frag path"); }
134
135
if (context.shaders[shaderData.name])
136
{ console.warn("Shader '" + shaderData.name + "' is already loaded! (overwriting anyway)"); }
137
138
promises.push((async () =>
139
{
140
let vertSource = Shader.DEFAULT_VERT;
141
142
if (shaderData.vert)
143
{
144
const vertResponse = await fetch(shaderData.vert);
145
if (!vertResponse.ok)
146
{
147
throw new Error("Failed to load vertex shader '" + shaderData.name + "' from " + shaderData.vert);
148
}
149
150
vertSource = await vertResponse.text();
151
}
152
153
const fragResponse = await fetch(shaderData.frag);
154
if (!fragResponse.ok)
155
{
156
throw new Error("Failed to load fragment shader '" + shaderData.name + "' from " + shaderData.frag);
157
}
158
159
const fragSource = await fragResponse.text();
160
const shader = new Shader(vertSource, fragSource);
161
shader.name = shaderData.name;
162
context.shaders[shaderData.name] = shader;
163
})());
164
}
165
166
await Promise.all(promises);
167
console.log("all shaders loaded ", this.shaders);
168
return this.shaders;
169
}
170
120
171
loadScene (scene)
121
172
{
122
173
if (this.activeScenes.length == 0)
@@ -8,6 +8,7 @@ class Game8
rafId = null;8
rafId = null;9
9
10
images = {};10
images = {};11
shaders = {};11
12
12
constructor ()13
constructor ()13
{14
{@@ -15,6 +16,7 @@ class Game15
this.activeScenes = [];16
this.activeScenes = [];16
this.isRunning = false;17
this.isRunning = false;17
this.rafId = null;18
this.rafId = null;19
this.shaders = {};18
this.update = this.update.bind(this);20
this.update = this.update.bind(this);19
}21
}20
22
@@ -117,6 +119,55 @@ class Game117
return this.images;119
return this.images;118
}120
}119
121
122
async preloadShaders (shadersData)123
{124
const context = this;125
const promises = [];126
127
for (const shaderData of shadersData)128
{129
if (!shaderData || !shaderData.name)130
{ throw new Error("preloadShaders: each entry needs a name"); }131
132
if (!shaderData.frag)133
{ throw new Error("preloadShaders: '" + shaderData.name + "' needs a frag path"); }134
135
if (context.shaders[shaderData.name])136
{ console.warn("Shader '" + shaderData.name + "' is already loaded! (overwriting anyway)"); }137
138
promises.push((async () =>139
{140
let vertSource = Shader.DEFAULT_VERT;141
142
if (shaderData.vert)143
{144
const vertResponse = await fetch(shaderData.vert);145
if (!vertResponse.ok)146
{147
throw new Error("Failed to load vertex shader '" + shaderData.name + "' from " + shaderData.vert);148
}149
150
vertSource = await vertResponse.text();151
}152
153
const fragResponse = await fetch(shaderData.frag);154
if (!fragResponse.ok)155
{156
throw new Error("Failed to load fragment shader '" + shaderData.name + "' from " + shaderData.frag);157
}158
159
const fragSource = await fragResponse.text();160
const shader = new Shader(vertSource, fragSource);161
shader.name = shaderData.name;162
context.shaders[shaderData.name] = shader;163
})());164
}165
166
await Promise.all(promises);167
console.log("all shaders loaded ", this.shaders);168
return this.shaders;169
}170
120
loadScene (scene)171
loadScene (scene)121
{172
{122
if (this.activeScenes.length == 0)173
if (this.activeScenes.length == 0)@@ -0,0 +1,230 @@
1
class GLContext
2
{
3
constructor (canvas)
4
{
5
this.canvas = canvas;
6
7
const attrs = { alpha: true, premultipliedAlpha: false, antialias: true };
8
this.gl = canvas.getContext("webgl", attrs) || canvas.getContext("experimental-webgl", attrs);
9
10
if (!this.gl)
11
{ throw new Error("WebGL is not supported in this browser."); }
12
13
const gl = this.gl;
14
gl.enable(gl.BLEND);
15
gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);
16
gl.disable(gl.DEPTH_TEST);
17
gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, false);
18
19
this.defaultShader = new Shader(Shader.DEFAULT_VERT, Shader.DEFAULT_FRAG);
20
this.textures = new WeakMap();
21
22
this.positionBuffer = gl.createBuffer();
23
gl.bindBuffer(gl.ARRAY_BUFFER, this.positionBuffer);
24
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([
25
-0.5, -0.5,
26
0.5, -0.5,
27
-0.5, 0.5,
28
0.5, 0.5
29
]), gl.STATIC_DRAW);
30
31
this.texCoordBuffer = gl.createBuffer();
32
this._lastWidth = 0;
33
this._lastHeight = 0;
34
this.setSize(canvas.width, canvas.height);
35
}
36
37
setSize (width, height)
38
{
39
const gl = this.gl;
40
if (width !== this._lastWidth || height !== this._lastHeight)
41
{
42
this._lastWidth = width;
43
this._lastHeight = height;
44
gl.viewport(0, 0, width, height);
45
}
46
}
47
48
clear (r, g, b, a = 1)
49
{
50
const gl = this.gl;
51
gl.clearColor(r / 255, g / 255, b / 255, a);
52
gl.clear(gl.COLOR_BUFFER_BIT);
53
}
54
55
clearTransparent ()
56
{
57
const gl = this.gl;
58
gl.clearColor(0, 0, 0, 0);
59
gl.clear(gl.COLOR_BUFFER_BIT);
60
}
61
62
getTexture (htmlImage)
63
{
64
if (!htmlImage)
65
{ return null; }
66
67
let texture = this.textures.get(htmlImage);
68
if (texture)
69
{ return texture; }
70
71
const gl = this.gl;
72
texture = gl.createTexture();
73
gl.bindTexture(gl.TEXTURE_2D, texture);
74
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
75
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
76
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
77
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
78
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, htmlImage);
79
80
this.textures.set(htmlImage, texture);
81
return texture;
82
}
83
84
drawSprite (options)
85
{
86
const gl = this.gl;
87
const width = this._lastWidth;
88
const height = this._lastHeight;
89
if (width <= 0 || height <= 0)
90
{ return; }
91
92
const shader = options.shader || this.defaultShader;
93
const info = shader.getProgramInfo(gl);
94
if (!info)
95
{ return; }
96
97
const texture = options.texture;
98
if (!texture)
99
{ return; }
100
101
gl.useProgram(info.program);
102
103
gl.bindBuffer(gl.ARRAY_BUFFER, this.positionBuffer);
104
if (info.attribs.position >= 0)
105
{
106
gl.enableVertexAttribArray(info.attribs.position);
107
gl.vertexAttribPointer(info.attribs.position, 2, gl.FLOAT, false, 0, 0);
108
}
109
110
const uv = options.uvRect || { u0: 0, v0: 0, u1: 1, v1: 1 };
111
gl.bindBuffer(gl.ARRAY_BUFFER, this.texCoordBuffer);
112
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([
113
uv.u0, uv.v0,
114
uv.u1, uv.v0,
115
uv.u0, uv.v1,
116
uv.u1, uv.v1
117
]), gl.DYNAMIC_DRAW);
118
119
if (info.attribs.texCoord >= 0)
120
{
121
gl.enableVertexAttribArray(info.attribs.texCoord);
122
gl.vertexAttribPointer(info.attribs.texCoord, 2, gl.FLOAT, false, 0, 0);
123
}
124
125
const matrix = GLContext._buildSpriteMatrix(
126
options.x, options.y,
127
options.w, options.h,
128
options.rotationDeg || 0,
129
options.mirrorX, options.mirrorY,
130
width, height
131
);
132
133
this._setUniform(info, "u_matrix", matrix);
134
this._setUniform(info, "u_alpha", options.alpha == null ? 1 : options.alpha);
135
136
gl.activeTexture(gl.TEXTURE0);
137
gl.bindTexture(gl.TEXTURE_2D, texture);
138
this._setUniform(info, "u_texture", 0);
139
140
if (options.uniforms)
141
{
142
for (const name in options.uniforms)
143
{
144
if (!Object.prototype.hasOwnProperty.call(options.uniforms, name))
145
{ continue; }
146
147
this._setUniform(info, name, options.uniforms[name]);
148
}
149
}
150
151
gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
152
}
153
154
_setUniform (info, name, value)
155
{
156
const entry = info.uniforms[name];
157
if (!entry || entry.location == null)
158
{ return; }
159
160
const gl = this.gl;
161
const loc = entry.location;
162
163
if (typeof value === "number")
164
{
165
if (entry.type === gl.INT || entry.type === gl.SAMPLER_2D)
166
{ gl.uniform1i(loc, value); }
167
else
168
{ gl.uniform1f(loc, value); }
169
return;
170
}
171
172
if (value instanceof Float32Array)
173
{
174
if (value.length === 16)
175
{ gl.uniformMatrix4fv(loc, false, value); }
176
else if (value.length === 9)
177
{ gl.uniformMatrix3fv(loc, false, value); }
178
else if (value.length === 4)
179
{ gl.uniform4fv(loc, value); }
180
else if (value.length === 3)
181
{ gl.uniform3fv(loc, value); }
182
else if (value.length === 2)
183
{ gl.uniform2fv(loc, value); }
184
return;
185
}
186
187
if (Array.isArray(value))
188
{
189
if (value.length === 4)
190
{ gl.uniform4fv(loc, value); }
191
else if (value.length === 3)
192
{ gl.uniform3fv(loc, value); }
193
else if (value.length === 2)
194
{ gl.uniform2fv(loc, value); }
195
else if (value.length === 1)
196
{ gl.uniform1f(loc, value[0]); }
197
}
198
}
199
200
// Column-major mat4: projection (canvas Y-down) * T * R * S
201
static _buildSpriteMatrix (x, y, w, h, rotationDeg, mirrorX, mirrorY, canvasW, canvasH)
202
{
203
const sx = (mirrorX ? -1 : 1) * w;
204
const sy = (mirrorY ? -1 : 1) * h;
205
const rad = rotationDeg * Math.PI / 180;
206
const c = Math.cos(rad);
207
const s = Math.sin(rad);
208
209
// Model maps unit quad [-0.5,0.5] into pixel space.
210
const m00 = c * sx;
211
const m01 = -s * sy;
212
const m10 = s * sx;
213
const m11 = c * sy;
214
const m30 = x;
215
const m31 = y;
216
217
const pr0 = 2 / canvasW;
218
const pr5 = -2 / canvasH;
219
const pr12 = -1;
220
const pr13 = 1;
221
222
// P * M (only 2D affine terms matter)
223
return new Float32Array([
224
pr0 * m00, pr5 * m10, 0, 0,
225
pr0 * m01, pr5 * m11, 0, 0,
226
0, 0, -1, 0,
227
pr0 * m30 + pr12, pr5 * m31 + pr13, 0, 1
228
]);
229
}
230
}
@@ -0,0 +1,230 @@1
class GLContext2
{3
constructor (canvas)4
{5
this.canvas = canvas;6
7
const attrs = { alpha: true, premultipliedAlpha: false, antialias: true };8
this.gl = canvas.getContext("webgl", attrs) || canvas.getContext("experimental-webgl", attrs);9
10
if (!this.gl)11
{ throw new Error("WebGL is not supported in this browser."); }12
13
const gl = this.gl;14
gl.enable(gl.BLEND);15
gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);16
gl.disable(gl.DEPTH_TEST);17
gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, false);18
19
this.defaultShader = new Shader(Shader.DEFAULT_VERT, Shader.DEFAULT_FRAG);20
this.textures = new WeakMap();21
22
this.positionBuffer = gl.createBuffer();23
gl.bindBuffer(gl.ARRAY_BUFFER, this.positionBuffer);24
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([25
-0.5, -0.5,26
0.5, -0.5,27
-0.5, 0.5,28
0.5, 0.529
]), gl.STATIC_DRAW);30
31
this.texCoordBuffer = gl.createBuffer();32
this._lastWidth = 0;33
this._lastHeight = 0;34
this.setSize(canvas.width, canvas.height);35
}36
37
setSize (width, height)38
{39
const gl = this.gl;40
if (width !== this._lastWidth || height !== this._lastHeight)41
{42
this._lastWidth = width;43
this._lastHeight = height;44
gl.viewport(0, 0, width, height);45
}46
}47
48
clear (r, g, b, a = 1)49
{50
const gl = this.gl;51
gl.clearColor(r / 255, g / 255, b / 255, a);52
gl.clear(gl.COLOR_BUFFER_BIT);53
}54
55
clearTransparent ()56
{57
const gl = this.gl;58
gl.clearColor(0, 0, 0, 0);59
gl.clear(gl.COLOR_BUFFER_BIT);60
}61
62
getTexture (htmlImage)63
{64
if (!htmlImage)65
{ return null; }66
67
let texture = this.textures.get(htmlImage);68
if (texture)69
{ return texture; }70
71
const gl = this.gl;72
texture = gl.createTexture();73
gl.bindTexture(gl.TEXTURE_2D, texture);74
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);75
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);76
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);77
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);78
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, htmlImage);79
80
this.textures.set(htmlImage, texture);81
return texture;82
}83
84
drawSprite (options)85
{86
const gl = this.gl;87
const width = this._lastWidth;88
const height = this._lastHeight;89
if (width <= 0 || height <= 0)90
{ return; }91
92
const shader = options.shader || this.defaultShader;93
const info = shader.getProgramInfo(gl);94
if (!info)95
{ return; }96
97
const texture = options.texture;98
if (!texture)99
{ return; }100
101
gl.useProgram(info.program);102
103
gl.bindBuffer(gl.ARRAY_BUFFER, this.positionBuffer);104
if (info.attribs.position >= 0)105
{106
gl.enableVertexAttribArray(info.attribs.position);107
gl.vertexAttribPointer(info.attribs.position, 2, gl.FLOAT, false, 0, 0);108
}109
110
const uv = options.uvRect || { u0: 0, v0: 0, u1: 1, v1: 1 };111
gl.bindBuffer(gl.ARRAY_BUFFER, this.texCoordBuffer);112
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([113
uv.u0, uv.v0,114
uv.u1, uv.v0,115
uv.u0, uv.v1,116
uv.u1, uv.v1117
]), gl.DYNAMIC_DRAW);118
119
if (info.attribs.texCoord >= 0)120
{121
gl.enableVertexAttribArray(info.attribs.texCoord);122
gl.vertexAttribPointer(info.attribs.texCoord, 2, gl.FLOAT, false, 0, 0);123
}124
125
const matrix = GLContext._buildSpriteMatrix(126
options.x, options.y,127
options.w, options.h,128
options.rotationDeg || 0,129
options.mirrorX, options.mirrorY,130
width, height131
);132
133
this._setUniform(info, "u_matrix", matrix);134
this._setUniform(info, "u_alpha", options.alpha == null ? 1 : options.alpha);135
136
gl.activeTexture(gl.TEXTURE0);137
gl.bindTexture(gl.TEXTURE_2D, texture);138
this._setUniform(info, "u_texture", 0);139
140
if (options.uniforms)141
{142
for (const name in options.uniforms)143
{144
if (!Object.prototype.hasOwnProperty.call(options.uniforms, name))145
{ continue; }146
147
this._setUniform(info, name, options.uniforms[name]);148
}149
}150
151
gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);152
}153
154
_setUniform (info, name, value)155
{156
const entry = info.uniforms[name];157
if (!entry || entry.location == null)158
{ return; }159
160
const gl = this.gl;161
const loc = entry.location;162
163
if (typeof value === "number")164
{165
if (entry.type === gl.INT || entry.type === gl.SAMPLER_2D)166
{ gl.uniform1i(loc, value); }167
else168
{ gl.uniform1f(loc, value); }169
return;170
}171
172
if (value instanceof Float32Array)173
{174
if (value.length === 16)175
{ gl.uniformMatrix4fv(loc, false, value); }176
else if (value.length === 9)177
{ gl.uniformMatrix3fv(loc, false, value); }178
else if (value.length === 4)179
{ gl.uniform4fv(loc, value); }180
else if (value.length === 3)181
{ gl.uniform3fv(loc, value); }182
else if (value.length === 2)183
{ gl.uniform2fv(loc, value); }184
return;185
}186
187
if (Array.isArray(value))188
{189
if (value.length === 4)190
{ gl.uniform4fv(loc, value); }191
else if (value.length === 3)192
{ gl.uniform3fv(loc, value); }193
else if (value.length === 2)194
{ gl.uniform2fv(loc, value); }195
else if (value.length === 1)196
{ gl.uniform1f(loc, value[0]); }197
}198
}199
200
// Column-major mat4: projection (canvas Y-down) * T * R * S201
static _buildSpriteMatrix (x, y, w, h, rotationDeg, mirrorX, mirrorY, canvasW, canvasH)202
{203
const sx = (mirrorX ? -1 : 1) * w;204
const sy = (mirrorY ? -1 : 1) * h;205
const rad = rotationDeg * Math.PI / 180;206
const c = Math.cos(rad);207
const s = Math.sin(rad);208
209
// Model maps unit quad [-0.5,0.5] into pixel space.210
const m00 = c * sx;211
const m01 = -s * sy;212
const m10 = s * sx;213
const m11 = c * sy;214
const m30 = x;215
const m31 = y;216
217
const pr0 = 2 / canvasW;218
const pr5 = -2 / canvasH;219
const pr12 = -1;220
const pr13 = 1;221
222
// P * M (only 2D affine terms matter)223
return new Float32Array([224
pr0 * m00, pr5 * m10, 0, 0,225
pr0 * m01, pr5 * m11, 0, 0,226
0, 0, -1, 0,227
pr0 * m30 + pr12, pr5 * m31 + pr13, 0, 1228
]);229
}230
}@@ -0,0 +1,115 @@
1
class Shader
2
{
3
static DEFAULT_VERT = [
4
"attribute vec2 a_position;",
5
"attribute vec2 a_texCoord;",
6
"uniform mat4 u_matrix;",
7
"varying vec2 v_texCoord;",
8
"void main() {",
9
" gl_Position = u_matrix * vec4(a_position, 0.0, 1.0);",
10
" v_texCoord = a_texCoord;",
11
"}"
12
].join("\n");
13
14
static DEFAULT_FRAG = [
15
"precision mediump float;",
16
"uniform sampler2D u_texture;",
17
"uniform float u_alpha;",
18
"varying vec2 v_texCoord;",
19
"void main() {",
20
" vec4 color = texture2D(u_texture, v_texCoord);",
21
" gl_FragColor = vec4(color.rgb, color.a * u_alpha);",
22
"}"
23
].join("\n");
24
25
constructor (vertSource, fragSource)
26
{
27
this.vertSource = vertSource || Shader.DEFAULT_VERT;
28
this.fragSource = fragSource || Shader.DEFAULT_FRAG;
29
this._programs = new WeakMap();
30
}
31
32
static fragment (fragSource)
33
{
34
return new Shader(Shader.DEFAULT_VERT, fragSource);
35
}
36
37
getProgramInfo (gl)
38
{
39
let info = this._programs.get(gl);
40
if (info)
41
{ return info; }
42
43
const program = Shader._createProgram(gl, this.vertSource, this.fragSource);
44
if (!program)
45
{ return null; }
46
47
info = {
48
program: program,
49
attribs: {
50
position: gl.getAttribLocation(program, "a_position"),
51
texCoord: gl.getAttribLocation(program, "a_texCoord")
52
},
53
uniforms: {}
54
};
55
56
const numUniforms = gl.getProgramParameter(program, gl.ACTIVE_UNIFORMS);
57
for (let i = 0; i < numUniforms; i++)
58
{
59
const uniformInfo = gl.getActiveUniform(program, i);
60
if (!uniformInfo)
61
{ continue; }
62
63
info.uniforms[uniformInfo.name] = {
64
location: gl.getUniformLocation(program, uniformInfo.name),
65
type: uniformInfo.type,
66
size: uniformInfo.size
67
};
68
}
69
70
this._programs.set(gl, info);
71
return info;
72
}
73
74
static _createShader (gl, type, source)
75
{
76
const shader = gl.createShader(type);
77
gl.shaderSource(shader, source);
78
gl.compileShader(shader);
79
80
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS))
81
{
82
console.error("Shader compile error:", gl.getShaderInfoLog(shader));
83
console.error(source);
84
gl.deleteShader(shader);
85
return null;
86
}
87
88
return shader;
89
}
90
91
static _createProgram (gl, vertSource, fragSource)
92
{
93
const vertexShader = Shader._createShader(gl, gl.VERTEX_SHADER, vertSource);
94
const fragmentShader = Shader._createShader(gl, gl.FRAGMENT_SHADER, fragSource);
95
if (!vertexShader || !fragmentShader)
96
{ return null; }
97
98
const program = gl.createProgram();
99
gl.attachShader(program, vertexShader);
100
gl.attachShader(program, fragmentShader);
101
gl.linkProgram(program);
102
103
gl.deleteShader(vertexShader);
104
gl.deleteShader(fragmentShader);
105
106
if (!gl.getProgramParameter(program, gl.LINK_STATUS))
107
{
108
console.error("Shader link error:", gl.getProgramInfoLog(program));
109
gl.deleteProgram(program);
110
return null;
111
}
112
113
return program;
114
}
115
}
@@ -0,0 +1,115 @@1
class Shader2
{3
static DEFAULT_VERT = [4
"attribute vec2 a_position;",5
"attribute vec2 a_texCoord;",6
"uniform mat4 u_matrix;",7
"varying vec2 v_texCoord;",8
"void main() {",9
" gl_Position = u_matrix * vec4(a_position, 0.0, 1.0);",10
" v_texCoord = a_texCoord;",11
"}"12
].join("\n");13
14
static DEFAULT_FRAG = [15
"precision mediump float;",16
"uniform sampler2D u_texture;",17
"uniform float u_alpha;",18
"varying vec2 v_texCoord;",19
"void main() {",20
" vec4 color = texture2D(u_texture, v_texCoord);",21
" gl_FragColor = vec4(color.rgb, color.a * u_alpha);",22
"}"23
].join("\n");24
25
constructor (vertSource, fragSource)26
{27
this.vertSource = vertSource || Shader.DEFAULT_VERT;28
this.fragSource = fragSource || Shader.DEFAULT_FRAG;29
this._programs = new WeakMap();30
}31
32
static fragment (fragSource)33
{34
return new Shader(Shader.DEFAULT_VERT, fragSource);35
}36
37
getProgramInfo (gl)38
{39
let info = this._programs.get(gl);40
if (info)41
{ return info; }42
43
const program = Shader._createProgram(gl, this.vertSource, this.fragSource);44
if (!program)45
{ return null; }46
47
info = {48
program: program,49
attribs: {50
position: gl.getAttribLocation(program, "a_position"),51
texCoord: gl.getAttribLocation(program, "a_texCoord")52
},53
uniforms: {}54
};55
56
const numUniforms = gl.getProgramParameter(program, gl.ACTIVE_UNIFORMS);57
for (let i = 0; i < numUniforms; i++)58
{59
const uniformInfo = gl.getActiveUniform(program, i);60
if (!uniformInfo)61
{ continue; }62
63
info.uniforms[uniformInfo.name] = {64
location: gl.getUniformLocation(program, uniformInfo.name),65
type: uniformInfo.type,66
size: uniformInfo.size67
};68
}69
70
this._programs.set(gl, info);71
return info;72
}73
74
static _createShader (gl, type, source)75
{76
const shader = gl.createShader(type);77
gl.shaderSource(shader, source);78
gl.compileShader(shader);79
80
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS))81
{82
console.error("Shader compile error:", gl.getShaderInfoLog(shader));83
console.error(source);84
gl.deleteShader(shader);85
return null;86
}87
88
return shader;89
}90
91
static _createProgram (gl, vertSource, fragSource)92
{93
const vertexShader = Shader._createShader(gl, gl.VERTEX_SHADER, vertSource);94
const fragmentShader = Shader._createShader(gl, gl.FRAGMENT_SHADER, fragSource);95
if (!vertexShader || !fragmentShader)96
{ return null; }97
98
const program = gl.createProgram();99
gl.attachShader(program, vertexShader);100
gl.attachShader(program, fragmentShader);101
gl.linkProgram(program);102
103
gl.deleteShader(vertexShader);104
gl.deleteShader(fragmentShader);105
106
if (!gl.getProgramParameter(program, gl.LINK_STATUS))107
{108
console.error("Shader link error:", gl.getProgramInfoLog(program));109
gl.deleteProgram(program);110
return null;111
}112
113
return program;114
}115
}@@ -30,41 +30,48 @@ class image
30
30
console.log("Image loaded: " + (image && image.src ? image.src : ""));
31
31
}
32
32
33
draw (scene, dx, dy, dw, dh, rotation, mirrorX, mirrorY, alpha, subImage)
33
draw (scene, dx, dy, dw, dh, rotation, mirrorX, mirrorY, alpha, subImage, shader, uniforms)
34
34
{
35
35
var shouldDraw = true;
36
36
37
37
if (dx < -Math.sqrt(Math.pow(dw, 2) + Math.pow(dh, 2)))
38
{shouldDraw = false;}
38
{ shouldDraw = false; }
39
39
40
40
if (dy < -Math.sqrt(Math.pow(dw, 2) + Math.pow(dh, 2)))
41
{shouldDraw = false;}
41
{ shouldDraw = false; }
42
42
43
43
if (dx > scene.real_size.x + Math.sqrt(Math.pow(dw, 2) + Math.pow(dh, 2)))
44
{shouldDraw = false;}
44
{ shouldDraw = false; }
45
45
46
46
if (dy > scene.real_size.y + Math.sqrt(Math.pow(dw, 2) + Math.pow(dh, 2)))
47
{shouldDraw = false;}
48
49
if (shouldDraw)
50
{
51
var context = scene.context;
52
context.save();
53
var oldAlpha = context.globalAlpha;
54
context.globalAlpha = alpha;
55
56
context.translate(dx, dy);
57
context.rotate(rotation * (Math.PI / 180));
58
59
(mirrorX || mirrorY) && context.scale(mirrorX ? -1 : 1, mirrorY ? -1 : 1);
60
61
var subImageData = this.getSubImage(subImage);
62
63
context.drawImage(this.g_image, subImageData.sx, subImageData.sy, subImageData.sw, subImageData.sh, -dw / 2, -dh / 2, dw, dh);
64
65
context.restore();
66
context.globalAlpha = oldAlpha;
67
}
47
{ shouldDraw = false; }
48
49
if (!shouldDraw || !scene.glContext || !this.g_image)
50
{ return; }
51
52
const subImageData = this.getSubImage(subImage);
53
const imgW = this.g_image.width;
54
const imgH = this.g_image.height;
55
56
scene.glContext.drawSprite({
57
texture: scene.glContext.getTexture(this.g_image),
58
x: dx,
59
y: dy,
60
w: dw,
61
h: dh,
62
rotationDeg: rotation,
63
mirrorX: mirrorX,
64
mirrorY: mirrorY,
65
alpha: alpha,
66
uvRect: {
67
u0: subImageData.sx / imgW,
68
v0: subImageData.sy / imgH,
69
u1: (subImageData.sx + subImageData.sw) / imgW,
70
v1: (subImageData.sy + subImageData.sh) / imgH
71
},
72
shader: shader || scene.glContext.defaultShader,
73
uniforms: uniforms
74
});
68
75
}
69
76
70
77
hasAnimationCycle (label)
@@ -87,7 +94,7 @@ class image
87
94
get width ()
88
95
{
89
96
if (this.cacheWidth < 0)
90
{this.cacheWidth = this.g_image.width / this.subImagesPerRow;}
97
{ this.cacheWidth = this.g_image.width / this.subImagesPerRow; }
91
98
92
99
return this.cacheWidth;
93
100
}
@@ -95,7 +102,7 @@ class image
95
102
get height ()
96
103
{
97
104
if (this.cacheHeight < 0)
98
{this.cacheHeight = this.g_image.height / (Math.ceil(this.subImages / this.subImagesPerRow) + 0);}
105
{ this.cacheHeight = this.g_image.height / (Math.ceil(this.subImages / this.subImagesPerRow) + 0); }
99
106
100
107
return this.cacheHeight;
101
108
}
@@ -117,4 +124,4 @@ class image
117
124
118
125
return { sw: sw, sh: sh, sx: sx, sy: sy };
119
126
}
120
}
127
}
@@ -30,41 +30,48 @@ class image30
console.log("Image loaded: " + (image && image.src ? image.src : ""));
30
console.log("Image loaded: " + (image && image.src ? image.src : ""));
31
}
31
}
32
32
33
draw (scene, dx, dy, dw, dh, rotation, mirrorX, mirrorY, alpha, subImage)
33
draw (scene, dx, dy, dw, dh, rotation, mirrorX, mirrorY, alpha, subImage, shader, uniforms)
34
{
34
{
35
var shouldDraw = true;
35
var shouldDraw = true;
36
36
37
if (dx < -Math.sqrt(Math.pow(dw, 2) + Math.pow(dh, 2)))
37
if (dx < -Math.sqrt(Math.pow(dw, 2) + Math.pow(dh, 2)))
38
{shouldDraw = false;}
38
{ shouldDraw = false; }
39
39
40
if (dy < -Math.sqrt(Math.pow(dw, 2) + Math.pow(dh, 2)))
40
if (dy < -Math.sqrt(Math.pow(dw, 2) + Math.pow(dh, 2)))
41
{shouldDraw = false;}
41
{ shouldDraw = false; }
42
42
43
if (dx > scene.real_size.x + Math.sqrt(Math.pow(dw, 2) + Math.pow(dh, 2)))
43
if (dx > scene.real_size.x + Math.sqrt(Math.pow(dw, 2) + Math.pow(dh, 2)))
44
{shouldDraw = false;}
44
{ shouldDraw = false; }
45
45
46
if (dy > scene.real_size.y + Math.sqrt(Math.pow(dw, 2) + Math.pow(dh, 2)))
46
if (dy > scene.real_size.y + Math.sqrt(Math.pow(dw, 2) + Math.pow(dh, 2)))
47
{shouldDraw = false;}
47
{ shouldDraw = false; }
48
48
49
if (shouldDraw)
49
if (!shouldDraw || !scene.glContext || !this.g_image)
50
{
50
{ return; }
51
var context = scene.context;
51
52
context.save();
52
const subImageData = this.getSubImage(subImage);
53
var oldAlpha = context.globalAlpha;
53
const imgW = this.g_image.width;
54
context.globalAlpha = alpha;
54
const imgH = this.g_image.height;
55
55
56
context.translate(dx, dy);
56
scene.glContext.drawSprite({
57
context.rotate(rotation * (Math.PI / 180));
57
texture: scene.glContext.getTexture(this.g_image),
58
58
x: dx,
59
(mirrorX || mirrorY) && context.scale(mirrorX ? -1 : 1, mirrorY ? -1 : 1);
59
y: dy,
60
60
w: dw,
61
var subImageData = this.getSubImage(subImage);
61
h: dh,
62
62
rotationDeg: rotation,
63
context.drawImage(this.g_image, subImageData.sx, subImageData.sy, subImageData.sw, subImageData.sh, -dw / 2, -dh / 2, dw, dh);
63
mirrorX: mirrorX,
64
64
mirrorY: mirrorY,
65
context.restore();
65
alpha: alpha,
66
context.globalAlpha = oldAlpha;
66
uvRect: {
67
}
67
u0: subImageData.sx / imgW,
68
v0: subImageData.sy / imgH,
69
u1: (subImageData.sx + subImageData.sw) / imgW,
70
v1: (subImageData.sy + subImageData.sh) / imgH
71
},
72
shader: shader || scene.glContext.defaultShader,
73
uniforms: uniforms
74
});
68
}
75
}
69
76
70
hasAnimationCycle (label)
77
hasAnimationCycle (label)
@@ -87,7 +94,7 @@ class image87
get width ()
94
get width ()
88
{
95
{
89
if (this.cacheWidth < 0)
96
if (this.cacheWidth < 0)
90
{this.cacheWidth = this.g_image.width / this.subImagesPerRow;}
97
{ this.cacheWidth = this.g_image.width / this.subImagesPerRow; }
91
98
92
return this.cacheWidth;
99
return this.cacheWidth;
93
}
100
}
@@ -95,7 +102,7 @@ class image95
get height ()
102
get height ()
96
{
103
{
97
if (this.cacheHeight < 0)
104
if (this.cacheHeight < 0)
98
{this.cacheHeight = this.g_image.height / (Math.ceil(this.subImages / this.subImagesPerRow) + 0);}
105
{ this.cacheHeight = this.g_image.height / (Math.ceil(this.subImages / this.subImagesPerRow) + 0); }
99
106
100
return this.cacheHeight;
107
return this.cacheHeight;
101
}
108
}
@@ -117,4 +124,4 @@ class image117
124
118
return { sw: sw, sh: sh, sx: sx, sy: sy };
125
return { sw: sw, sh: sh, sx: sx, sy: sy };
119
}
126
}
120
}127
}
@@ -13,10 +13,15 @@ class Renderer
13
13
14
14
visible = true;
15
15
16
shader = null;
17
shaderUniforms = null;
18
16
19
constructor (parentObject, sprite)
17
20
{
18
21
this.gameObject = parentObject;
19
22
this.sprite = sprite;
23
this.shader = null;
24
this.shaderUniforms = null;
20
25
}
21
26
22
27
update (deltaTime)
@@ -106,7 +111,9 @@ class Renderer
106
111
this.mirrorX,
107
112
this.mirrorY,
108
113
this.alpha,
109
this.subImage
114
this.subImage,
115
this.shader,
116
this.shaderUniforms
110
117
);
111
118
}
112
119
@@ -13,10 +13,15 @@ class Renderer13
13
14
visible = true;14
visible = true;15
15
16
shader = null;17
shaderUniforms = null;18
16
constructor (parentObject, sprite)19
constructor (parentObject, sprite)17
{20
{18
this.gameObject = parentObject;21
this.gameObject = parentObject;19
this.sprite = sprite;22
this.sprite = sprite;23
this.shader = null;24
this.shaderUniforms = null;20
}25
}21
26
22
update (deltaTime) 27
update (deltaTime) @@ -106,7 +111,9 @@ class Renderer106
this.mirrorX,111
this.mirrorX,107
this.mirrorY,112
this.mirrorY,108
this.alpha,113
this.alpha,109
this.subImage114
this.subImage,115
this.shader,116
this.shaderUniforms110
);117
);111
}118
}112
119
@@ -22,7 +22,7 @@ class Scene
22
22
displayMode = Scene.DisplayModes.absolute;
23
23
24
24
cv_canvas = null;
25
cv_context = null;
25
glContext = null;
26
26
destroyed = false;
27
27
28
28
game = null;
@@ -50,7 +50,7 @@ class Scene
50
50
body.insertAdjacentHTML('beforeend', '<canvas id="' + this.id + '" style="position: absolute; left: ' + x + 'px; top: ' + y + 'px;" width="' + width + '" height="' + height + '"></canvas>');
51
51
52
52
this.cv_canvas = document.getElementById(this.id);
53
this.cv_context = this.cv_canvas.getContext('2d');
53
this.glContext = new GLContext(this.cv_canvas);
54
54
55
55
this.g_fixSize();
56
56
this.quadTree = new QuadTree(this.real_size.x, this.real_size.y);
@@ -129,7 +129,7 @@ class Scene
129
129
}
130
130
131
131
this.cv_canvas = null;
132
this.cv_context = null;
132
this.glContext = null;
133
133
}
134
134
135
135
backgroundColor =
@@ -146,7 +146,7 @@ class Scene
146
146
147
147
get context ()
148
148
{
149
return this.cv_context;
149
return this.glContext ? this.glContext.gl : null;
150
150
}
151
151
152
152
addObject (obj)
@@ -232,11 +232,27 @@ class Scene
232
232
this.real_size.y = this.size.y;
233
233
}
234
234
235
if (this.canvas.width != this.real_size.x)
236
{ this.canvas.width = this.real_size.x; }
235
const canvasW = Math.max(1, Math.round(this.real_size.x));
236
const canvasH = Math.max(1, Math.round(this.real_size.y));
237
let resized = false;
237
238
238
if (this.canvas.height != this.real_size.y)
239
{ this.canvas.height = this.real_size.y; }
239
if (this.canvas.width != canvasW)
240
{
241
this.canvas.width = canvasW;
242
resized = true;
243
}
244
245
if (this.canvas.height != canvasH)
246
{
247
this.canvas.height = canvasH;
248
resized = true;
249
}
250
251
// Changing canvas dimensions resets WebGL — rebuild helper/buffers/programs.
252
if (resized || !this.glContext)
253
{ this.glContext = new GLContext(this.cv_canvas); }
254
else
255
{ this.glContext.setSize(this.canvas.width, this.canvas.height); }
240
256
241
257
if (this.px != this.real_position.x)
242
258
{
@@ -253,19 +269,23 @@ class Scene
253
269
254
270
draw ()
255
271
{
256
if (this.destroyed)
272
if (this.destroyed || !this.glContext)
257
273
{ return; }
258
274
259
275
this.g_fixSize();
260
276
261
277
if (this.renderBackground)
262
278
{
263
this.context.fillStyle = "rgb(" + this.backgroundColor.r + "," + this.backgroundColor.g + "," + this.backgroundColor.b + ")"; // sets the color to fill in the rectangle with
264
this.context.fillRect(0, 0, this.real_size.x, this.real_size.y);
279
this.glContext.clear(
280
this.backgroundColor.r,
281
this.backgroundColor.g,
282
this.backgroundColor.b,
283
1
284
);
265
285
}
266
else
286
else
267
287
{
268
this.context.clearRect(0, 0, this.real_size.x, this.real_size.y);
288
this.glContext.clearTransparent();
269
289
}
270
290
271
291
const sortedActiveObjects = this.activeObjects.slice().sort((a, b) => a.layer - b.layer);
@@ -22,7 +22,7 @@ class Scene22
displayMode = Scene.DisplayModes.absolute;22
displayMode = Scene.DisplayModes.absolute;23
23
24
cv_canvas = null;24
cv_canvas = null;25
cv_context = null;25
glContext = null;26
destroyed = false;26
destroyed = false;27
27
28
game = null;28
game = null;@@ -50,7 +50,7 @@ class Scene50
body.insertAdjacentHTML('beforeend', '<canvas id="' + this.id + '" style="position: absolute; left: ' + x + 'px; top: ' + y + 'px;" width="' + width + '" height="' + height + '"></canvas>');50
body.insertAdjacentHTML('beforeend', '<canvas id="' + this.id + '" style="position: absolute; left: ' + x + 'px; top: ' + y + 'px;" width="' + width + '" height="' + height + '"></canvas>');51
51
52
this.cv_canvas = document.getElementById(this.id);52
this.cv_canvas = document.getElementById(this.id);53
this.cv_context = this.cv_canvas.getContext('2d');53
this.glContext = new GLContext(this.cv_canvas);54
54
55
this.g_fixSize();55
this.g_fixSize();56
this.quadTree = new QuadTree(this.real_size.x, this.real_size.y);56
this.quadTree = new QuadTree(this.real_size.x, this.real_size.y);@@ -129,7 +129,7 @@ class Scene129
}129
}130
130
131
this.cv_canvas = null;131
this.cv_canvas = null;132
this.cv_context = null;132
this.glContext = null;133
}133
}134
134
135
backgroundColor =135
backgroundColor =@@ -146,7 +146,7 @@ class Scene146
146
147
get context ()147
get context ()148
{148
{149
return this.cv_context;149
return this.glContext ? this.glContext.gl : null;150
}150
}151
151
152
addObject (obj)152
addObject (obj)@@ -232,11 +232,27 @@ class Scene232
this.real_size.y = this.size.y;232
this.real_size.y = this.size.y;233
}233
}234
234
235
if (this.canvas.width != this.real_size.x)235
const canvasW = Math.max(1, Math.round(this.real_size.x));236
{ this.canvas.width = this.real_size.x; }236
const canvasH = Math.max(1, Math.round(this.real_size.y));237
let resized = false;237
238
238
if (this.canvas.height != this.real_size.y)239
if (this.canvas.width != canvasW)239
{ this.canvas.height = this.real_size.y; }240
{241
this.canvas.width = canvasW;242
resized = true;243
}244
245
if (this.canvas.height != canvasH)246
{247
this.canvas.height = canvasH;248
resized = true;249
}250
251
// Changing canvas dimensions resets WebGL — rebuild helper/buffers/programs.252
if (resized || !this.glContext)253
{ this.glContext = new GLContext(this.cv_canvas); }254
else255
{ this.glContext.setSize(this.canvas.width, this.canvas.height); }240
256
241
if (this.px != this.real_position.x)257
if (this.px != this.real_position.x)242
{258
{@@ -253,19 +269,23 @@ class Scene253
269
254
draw ()270
draw ()255
{271
{256
if (this.destroyed)272
if (this.destroyed || !this.glContext)257
{ return; }273
{ return; }258
274
259
this.g_fixSize();275
this.g_fixSize();260
276
261
if (this.renderBackground)277
if (this.renderBackground)262
{278
{263
this.context.fillStyle = "rgb(" + this.backgroundColor.r + "," + this.backgroundColor.g + "," + this.backgroundColor.b + ")"; // sets the color to fill in the rectangle with279
this.glContext.clear(264
this.context.fillRect(0, 0, this.real_size.x, this.real_size.y);280
this.backgroundColor.r,281
this.backgroundColor.g,282
this.backgroundColor.b,283
1284
);265
}285
}266
else 286
else267
{287
{268
this.context.clearRect(0, 0, this.real_size.x, this.real_size.y);288
this.glContext.clearTransparent();269
}289
}270
290
271
const sortedActiveObjects = this.activeObjects.slice().sort((a, b) => a.layer - b.layer);291
const sortedActiveObjects = this.activeObjects.slice().sort((a, b) => a.layer - b.layer);@@ -1,24 +1,51 @@
1
1
#!/usr/bin/env bash
2
set -euo pipefail
3
2
4
BUILD_FILE="./js/framework.js"
3
SRC_DIR="./framework"
4
5
5
printf "Build started, output file: $BUILD_FILE\n"
6
unlink $BUILD_FILE
6
# Explicit order: GL helpers before scene/image consumers; extensions last.
7
FILES=(
8
"framework/extensions/polyfills.js"
9
"framework/extensions/extentions.js"
10
"framework/extensions/thirdparty.js"
11
"framework/util/vector.js"
12
"framework/util/ease.js"
13
"framework/util/timingUtil.js"
14
"framework/util/signal.js"
15
"framework/util/cookie.js"
16
"framework/util/urlParams.js"
17
"framework/util/input.js"
18
"framework/util/audio.js"
19
"framework/util/ray.js"
20
"framework/util/quadTree.js"
21
"framework/util/particleEmitter.js"
22
"framework/engine/gl/shader.js"
23
"framework/engine/gl/glContext.js"
24
"framework/engine/objectComponents/transform.js"
25
"framework/engine/objectComponents/collider.js"
26
"framework/engine/objectComponents/renderer.js"
27
"framework/engine/image.js"
28
"framework/engine/gameobject.js"
29
"framework/engine/scene.js"
30
"framework/engine/game.js"
31
)
32
33
printf "Build started, output file: %s\n" "$BUILD_FILE"
34
rm -f "$BUILD_FILE"
35
mkdir -p "$(dirname "$BUILD_FILE")"
36
37
for file in "${FILES[@]}"
38
do
39
if [[ ! -f "$file" ]]
40
then
41
printf "Missing source file: %s\n" "$file" >&2
42
exit 1
43
fi
7
44
8
loop() {
9
shopt -s nullglob
10
shopt -s globstar
45
printf "\n/*\n * Start File:\n * %s\n */ \n" "$file" >> "$BUILD_FILE"
46
printf "Building %s\n" "$file"
47
cat "$file" >> "$BUILD_FILE"
48
printf "\n/*\n * End File:\n * %s\n */ \n" "$file" >> "$BUILD_FILE"
49
done
11
50
12
for file in "$@"/**
13
do
14
if [[ -f "$file" && "${file##*.}" = "js" ]]
15
then
16
printf "\n/*\n * Start File:\n * "$file"\n */ \n" >> "$BUILD_FILE";
17
printf "Building "$file"\n";
18
cat "$file" >> "$BUILD_FILE";
19
printf "\n/*\n * End File:\n * "$file"\n */ \n" >> "$BUILD_FILE";
20
fi
21
done
22
}
23
loop $SRC_DIR
24
printf "DONE"
51
printf "DONE\n"
@@ -1,24 +1,51 @@1
#!/usr/bin/env bash1
#!/usr/bin/env bash2
set -euo pipefail3
2
BUILD_FILE="./js/framework.js"4
BUILD_FILE="./js/framework.js"3
SRC_DIR="./framework"4
5
5
printf "Build started, output file: $BUILD_FILE\n"6
# Explicit order: GL helpers before scene/image consumers; extensions last.6
unlink $BUILD_FILE7
FILES=(8
"framework/extensions/polyfills.js"9
"framework/extensions/extentions.js"10
"framework/extensions/thirdparty.js"11
"framework/util/vector.js"12
"framework/util/ease.js"13
"framework/util/timingUtil.js"14
"framework/util/signal.js"15
"framework/util/cookie.js"16
"framework/util/urlParams.js"17
"framework/util/input.js"18
"framework/util/audio.js"19
"framework/util/ray.js"20
"framework/util/quadTree.js"21
"framework/util/particleEmitter.js"22
"framework/engine/gl/shader.js"23
"framework/engine/gl/glContext.js"24
"framework/engine/objectComponents/transform.js"25
"framework/engine/objectComponents/collider.js"26
"framework/engine/objectComponents/renderer.js"27
"framework/engine/image.js"28
"framework/engine/gameobject.js"29
"framework/engine/scene.js"30
"framework/engine/game.js"31
)32
33
printf "Build started, output file: %s\n" "$BUILD_FILE"34
rm -f "$BUILD_FILE"35
mkdir -p "$(dirname "$BUILD_FILE")"36
37
for file in "${FILES[@]}"38
do39
if [[ ! -f "$file" ]]40
then41
printf "Missing source file: %s\n" "$file" >&242
exit 143
fi7
44
8
loop() {45
printf "\n/*\n * Start File:\n * %s\n */ \n" "$file" >> "$BUILD_FILE"9
shopt -s nullglob46
printf "Building %s\n" "$file"10
shopt -s globstar47
cat "$file" >> "$BUILD_FILE"48
printf "\n/*\n * End File:\n * %s\n */ \n" "$file" >> "$BUILD_FILE"49
done11
50
12
for file in "$@"/**51
printf "DONE\n"13
do14
if [[ -f "$file" && "${file##*.}" = "js" ]]15
then16
printf "\n/*\n * Start File:\n * "$file"\n */ \n" >> "$BUILD_FILE"; 17
printf "Building "$file"\n";18
cat "$file" >> "$BUILD_FILE"; 19
printf "\n/*\n * End File:\n * "$file"\n */ \n" >> "$BUILD_FILE"; 20
fi21
done22
}23
loop $SRC_DIR24
printf "DONE"