melonjs 10.7.0 → 10.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (66) hide show
  1. package/dist/melonjs.js +1488 -666
  2. package/dist/melonjs.min.js +4 -4
  3. package/dist/melonjs.module.d.ts +929 -202
  4. package/dist/melonjs.module.js +1575 -777
  5. package/package.json +9 -9
  6. package/src/camera/camera2d.js +1 -1
  7. package/src/entity/entity.js +6 -7
  8. package/src/geometries/ellipse.js +10 -11
  9. package/src/geometries/line.js +3 -3
  10. package/src/geometries/path2d.js +319 -0
  11. package/src/geometries/poly.js +11 -11
  12. package/src/geometries/rectangle.js +15 -15
  13. package/src/geometries/roundrect.js +164 -0
  14. package/src/index.js +5 -1
  15. package/src/input/gamepad.js +2 -2
  16. package/src/input/pointerevent.js +1 -1
  17. package/src/lang/deprecated.js +1 -1
  18. package/src/level/tiled/TMXLayer.js +1 -1
  19. package/src/level/tiled/TMXObject.js +9 -12
  20. package/src/level/tiled/TMXTileMap.js +23 -4
  21. package/src/level/tiled/renderer/TMXHexagonalRenderer.js +1 -1
  22. package/src/level/tiled/renderer/TMXIsometricRenderer.js +1 -1
  23. package/src/level/tiled/renderer/TMXOrthogonalRenderer.js +1 -1
  24. package/src/level/tiled/renderer/TMXRenderer.js +1 -1
  25. package/src/level/tiled/renderer/TMXStaggeredRenderer.js +1 -1
  26. package/src/loader/loader.js +4 -4
  27. package/src/loader/loadingscreen.js +1 -1
  28. package/src/math/color.js +1 -1
  29. package/src/math/matrix2.js +1 -1
  30. package/src/math/matrix3.js +1 -1
  31. package/src/math/observable_vector2.js +1 -1
  32. package/src/math/observable_vector3.js +1 -1
  33. package/src/math/vector2.js +1 -1
  34. package/src/math/vector3.js +1 -1
  35. package/src/particles/emitter.js +23 -14
  36. package/src/particles/particle.js +3 -2
  37. package/src/physics/body.js +67 -51
  38. package/src/physics/bounds.js +8 -9
  39. package/src/physics/world.js +1 -1
  40. package/src/polyfill/index.js +1 -0
  41. package/src/polyfill/roundrect.js +235 -0
  42. package/src/renderable/collectable.js +9 -2
  43. package/src/renderable/colorlayer.js +1 -1
  44. package/src/renderable/container.js +1 -1
  45. package/src/renderable/imagelayer.js +1 -1
  46. package/src/renderable/renderable.js +2 -2
  47. package/src/renderable/sprite.js +2 -3
  48. package/src/renderable/trigger.js +10 -4
  49. package/src/state/stage.js +1 -1
  50. package/src/state/state.js +1 -1
  51. package/src/system/device.js +10 -8
  52. package/src/system/pooling.js +156 -149
  53. package/src/text/bitmaptext.js +1 -1
  54. package/src/text/text.js +1 -1
  55. package/src/utils/utils.js +2 -2
  56. package/src/video/canvas/canvas_renderer.js +83 -39
  57. package/src/video/renderer.js +36 -16
  58. package/src/video/texture.js +1 -1
  59. package/src/video/webgl/glshader.js +29 -193
  60. package/src/video/webgl/utils/attributes.js +16 -0
  61. package/src/video/webgl/utils/precision.js +11 -0
  62. package/src/video/webgl/utils/program.js +58 -0
  63. package/src/video/webgl/utils/string.js +16 -0
  64. package/src/video/webgl/utils/uniforms.js +87 -0
  65. package/src/video/webgl/webgl_compositor.js +1 -14
  66. package/src/video/webgl/webgl_renderer.js +124 -182
@@ -5,10 +5,12 @@ import * as event from "./../system/event.js";
5
5
  import device from "./../system/device.js";
6
6
  import { setPrefixed } from "./../utils/agent.js";
7
7
  import Rect from "./../geometries/rectangle.js";
8
+ import RoundRect from "./../geometries/roundrect.js";
8
9
  import Ellipse from "./../geometries/ellipse.js";
9
10
  import Polygon from "./../geometries/poly.js";
10
11
  import Line from "./../geometries/line.js";
11
12
  import Bounds from "./../physics/bounds.js";
13
+ import Path2D from "./../geometries/path2d.js";
12
14
 
13
15
  /**
14
16
  * @classdesc
@@ -43,12 +45,20 @@ class Renderer {
43
45
  /**
44
46
  * true if the current rendering context is valid
45
47
  * @name isContextValid
46
- * @memberof Renderer
48
+ * @memberof Renderer#
47
49
  * @default true
48
50
  * type {boolean}
49
51
  */
50
52
  this.isContextValid = true;
51
53
 
54
+ /**
55
+ * The Path2D instance used by the renderer to draw primitives
56
+ * @name path2D
57
+ * @type {Path2D}
58
+ * @memberof Renderer#
59
+ */
60
+ this.path2D = new Path2D();
61
+
52
62
  /**
53
63
  * @ignore
54
64
  */
@@ -331,15 +341,23 @@ class Renderer {
331
341
  * @name stroke
332
342
  * @memberof Renderer.prototype
333
343
  * @function
334
- * @param {Rect|Polygon|Line|Ellipse} shape a shape object to stroke
344
+ * @param {Rect|RoundRect|Polygon|Line|Ellipse} shape a shape object to stroke
335
345
  * @param {boolean} [fill=false] fill the shape with the current color if true
336
346
  */
337
347
  stroke(shape, fill) {
338
348
  if (shape instanceof Rect || shape instanceof Bounds) {
339
349
  this.strokeRect(shape.left, shape.top, shape.width, shape.height, fill);
340
- } else if (shape instanceof Line || shape instanceof Polygon) {
350
+ return;
351
+ }
352
+ if (shape instanceof Line || shape instanceof Polygon) {
341
353
  this.strokePolygon(shape, fill);
342
- } else if (shape instanceof Ellipse) {
354
+ return;
355
+ }
356
+ if (shape instanceof RoundRect) {
357
+ this.strokeRoundRect(shape.left, shape.top, shape.width, shape.height, shape.radius, fill);
358
+ return;
359
+ }
360
+ if (shape instanceof Ellipse) {
343
361
  this.strokeEllipse(
344
362
  shape.pos.x,
345
363
  shape.pos.y,
@@ -347,7 +365,20 @@ class Renderer {
347
365
  shape.radiusV.y,
348
366
  fill
349
367
  );
368
+ return;
350
369
  }
370
+ throw new Error("Invalid geometry for fill/stroke");
371
+ }
372
+
373
+ /**
374
+ * fill the given shape
375
+ * @name fill
376
+ * @memberof Renderer.prototype
377
+ * @function
378
+ * @param {Rect|RoundRect|Polygon|Line|Ellipse} shape a shape object to fill
379
+ */
380
+ fill(shape) {
381
+ this.stroke(shape, true);
351
382
  }
352
383
 
353
384
  /**
@@ -379,17 +410,6 @@ class Renderer {
379
410
  return canvas;
380
411
  }
381
412
 
382
- /**
383
- * fill the given shape
384
- * @name fill
385
- * @memberof Renderer.prototype
386
- * @function
387
- * @param {Rect|Polygon|Line|Ellipse} shape a shape object to fill
388
- */
389
- fill(shape) {
390
- this.stroke(shape, true);
391
- }
392
-
393
413
  /**
394
414
  * A mask limits rendering elements to the shape and position of the given mask object.
395
415
  * So, if the renderable is larger than the mask, only the intersecting part of the renderable will be visible.
@@ -397,7 +417,7 @@ class Renderer {
397
417
  * @name setMask
398
418
  * @memberof Renderer.prototype
399
419
  * @function
400
- * @param {Rect|Polygon|Line|Ellipse} [mask] the shape defining the mask to be applied
420
+ * @param {Rect|RoundRect|Polygon|Line|Ellipse} [mask] the shape defining the mask to be applied
401
421
  */
402
422
  // eslint-disable-next-line no-unused-vars
403
423
  setMask(mask) {}
@@ -3,7 +3,7 @@ import WebGLRenderer from "./webgl/webgl_renderer.js";
3
3
  import TextureCache from "./texture_cache.js";
4
4
  import Sprite from "./../renderable/sprite.js";
5
5
  import { renderer } from "./video.js";
6
- import * as pool from "./../system/pooling.js";
6
+ import pool from "./../system/pooling.js";
7
7
  import loader from "./../loader/loader.js";
8
8
  import { ETA } from "./../math/math.js";
9
9
 
@@ -1,198 +1,10 @@
1
1
  import * as event from "./../../system/event.js";
2
2
  import device from "./../../system/device.js";
3
-
4
- /**
5
- * @ignore
6
- */
7
- function extractUniforms(gl, shader) {
8
- var uniforms = {},
9
- uniRx = /uniform\s+(\w+)\s+(\w+)/g,
10
- uniformsData = {},
11
- descriptor = {},
12
- locations = {},
13
- match;
14
-
15
- // Detect all uniform names and types
16
- [ shader.vertex, shader.fragment ].forEach(function (shader) {
17
- while ((match = uniRx.exec(shader))) {
18
- uniformsData[match[2]] = match[1];
19
- }
20
- });
21
-
22
- // Get uniform references
23
- Object.keys(uniformsData).forEach(function (name) {
24
- var type = uniformsData[name];
25
- locations[name] = gl.getUniformLocation(shader.program, name);
26
-
27
- descriptor[name] = {
28
- "get" : (function (name) {
29
- /**
30
- * A getter for the uniform location
31
- * @ignore
32
- */
33
- return function () {
34
- return locations[name];
35
- };
36
- })(name),
37
- "set" : (function (name, type, fn) {
38
- if (type.indexOf("mat") === 0) {
39
- /**
40
- * A generic setter for uniform matrices
41
- * @ignore
42
- */
43
- return function (val) {
44
- gl[fn](locations[name], false, val);
45
- };
46
- }
47
- else {
48
- /**
49
- * A generic setter for uniform vectors
50
- * @ignore
51
- */
52
- return function (val) {
53
- var fnv = fn;
54
- if (val.length && fn.substr(-1) !== "v") {
55
- fnv += "v";
56
- }
57
- gl[fnv](locations[name], val);
58
- };
59
- }
60
- })(name, type, "uniform" + fnHash[type])
61
- };
62
- });
63
- Object.defineProperties(uniforms, descriptor);
64
-
65
- return uniforms;
66
- };
67
-
68
- /**
69
- * @ignore
70
- */
71
- function extractAttributes(gl, shader) {
72
- var attributes = {},
73
- attrRx = /attribute\s+\w+\s+(\w+)/g,
74
- match,
75
- i = 0;
76
-
77
- // Detect all attribute names
78
- while ((match = attrRx.exec(shader.vertex))) {
79
- attributes[match[1]] = i++;
80
- }
81
-
82
- return attributes;
83
- };
84
-
85
- /**
86
- * @ignore
87
- */
88
- function compileShader(gl, type, source) {
89
- var shader = gl.createShader(type);
90
- gl.shaderSource(shader, source);
91
- gl.compileShader(shader);
92
-
93
- if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
94
- throw new Error(gl.getShaderInfoLog(shader));
95
- }
96
-
97
- return shader;
98
- };
99
-
100
- /**
101
- * Compile GLSL into a shader object
102
- * @ignore
103
- */
104
- function compileProgram(gl, vertex, fragment, attributes) {
105
- var vertShader = compileShader(gl, gl.VERTEX_SHADER, vertex);
106
- var fragShader = compileShader(gl, gl.FRAGMENT_SHADER, fragment);
107
-
108
- var program = gl.createProgram();
109
-
110
- gl.attachShader(program, vertShader);
111
- gl.attachShader(program, fragShader);
112
-
113
-
114
- // force vertex attributes to use location 0 as starting location to prevent
115
- // browser to do complicated emulation when running on desktop OpenGL (e.g. on macOS)
116
- for (var location in attributes) {
117
- gl.bindAttribLocation(program, attributes[location], location);
118
- }
119
-
120
- gl.linkProgram(program);
121
-
122
- if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {
123
- var error_msg =
124
- "Error initializing Shader " + this + "\n" +
125
- "gl.VALIDATE_STATUS: " + gl.getProgramParameter(program, gl.VALIDATE_STATUS) + "\n" +
126
- "gl.getError()" + gl.getError() + "\n" +
127
- "gl.getProgramInfoLog()" + gl.getProgramInfoLog(program);
128
- // house cleaning
129
- gl.deleteProgram(program);
130
- program = null;
131
- // throw the exception
132
- throw new Error(error_msg);
133
- }
134
-
135
- gl.useProgram(program);
136
-
137
- // clean-up
138
- gl.deleteShader(vertShader);
139
- gl.deleteShader(fragShader);
140
-
141
- return program;
142
- };
143
-
144
-
145
- /**
146
- * Hash map of GLSL data types to WebGL Uniform methods
147
- * @ignore
148
- */
149
- var fnHash = {
150
- "bool" : "1i",
151
- "int" : "1i",
152
- "float" : "1f",
153
- "vec2" : "2fv",
154
- "vec3" : "3fv",
155
- "vec4" : "4fv",
156
- "bvec2" : "2iv",
157
- "bvec3" : "3iv",
158
- "bvec4" : "4iv",
159
- "ivec2" : "2iv",
160
- "ivec3" : "3iv",
161
- "ivec4" : "4iv",
162
- "mat2" : "Matrix2fv",
163
- "mat3" : "Matrix3fv",
164
- "mat4" : "Matrix4fv",
165
- "sampler2D" : "1i"
166
- };
167
-
168
- /**
169
- * set precision for the fiven shader source
170
- * won't do anything if the precision is already specified
171
- * @ignore
172
- */
173
- function setPrecision(src, precision) {
174
- if (src.substring(0, 9) !== "precision") {
175
- return "precision " + precision + " float;" + src;
176
- }
177
- return src;
178
- };
179
-
180
- /**
181
- * clean the given source from space, comments, etc...
182
- * @ignore
183
- */
184
- function minify(src) {
185
- // remove comments
186
- src = src.replace(/\/\*[\s\S]*?\*\/|([^\\:]|^)\/\/.*$/gm, "$1");
187
- // Remove leading and trailing whitespace from lines
188
- src = src.replace(/(\\n\s+)|(\s+\\n)/g, "");
189
- // Remove line breaks
190
- src = src.replace(/(\\r|\\n)+/g, "");
191
- // Remove unnecessary whitespace
192
- src = src.replace(/\s*([;,[\](){}\\\/\-+*|^&!=<>?~%])\s*/g, "$1");
193
-
194
- return src;
195
- };
3
+ import { extractUniforms } from "./utils/uniforms.js";
4
+ import { extractAttributes } from "./utils/attributes.js";
5
+ import { compileProgram } from "./utils/program.js";
6
+ import { setPrecision } from "./utils/precision.js";
7
+ import { minify } from "./utils/string.js";
196
8
 
197
9
  /**
198
10
  * @classdesc
@@ -337,6 +149,30 @@ class GLShader {
337
149
  }
338
150
  }
339
151
 
152
+ /**
153
+ * activate the given vertex attribute for this shader
154
+ * @name setVertexAttributes
155
+ * @memberof GLShader
156
+ * @function
157
+ * @param {WebGLRenderingContext} gl the current WebGL rendering context
158
+ * @param {object[]} attributes an array of vertex attributes
159
+ * @param {number} vertexByteSize the size of a single vertex in bytes
160
+ */
161
+ setVertexAttributes(gl, attributes, vertexByteSize) {
162
+ // set the vertex attributes
163
+ for (var index = 0; index < attributes.length; ++index) {
164
+ var element = attributes[index];
165
+ var location = this.getAttribLocation(element.name);
166
+
167
+ if (location !== -1) {
168
+ gl.enableVertexAttribArray(location);
169
+ gl.vertexAttribPointer(location, element.size, element.type, element.normalized, vertexByteSize, element.offset);
170
+ } else {
171
+ gl.disableVertexAttribArray(index);
172
+ }
173
+ }
174
+ }
175
+
340
176
  /**
341
177
  * destroy this shader objects resources (program, attributes, uniforms)
342
178
  * @name destroy
@@ -0,0 +1,16 @@
1
+ /**
2
+ * @ignore
3
+ */
4
+ export function extractAttributes(gl, shader) {
5
+ var attributes = {},
6
+ attrRx = /attribute\s+\w+\s+(\w+)/g,
7
+ match,
8
+ i = 0;
9
+
10
+ // Detect all attribute names
11
+ while ((match = attrRx.exec(shader.vertex))) {
12
+ attributes[match[1]] = i++;
13
+ }
14
+
15
+ return attributes;
16
+ };
@@ -0,0 +1,11 @@
1
+ /**
2
+ * set precision for the fiven shader source
3
+ * won't do anything if the precision is already specified
4
+ * @ignore
5
+ */
6
+ export function setPrecision(src, precision) {
7
+ if (src.substring(0, 9) !== "precision") {
8
+ return "precision " + precision + " float;" + src;
9
+ }
10
+ return src;
11
+ };
@@ -0,0 +1,58 @@
1
+ /**
2
+ * @ignore
3
+ */
4
+ function compileShader(gl, type, source) {
5
+ var shader = gl.createShader(type);
6
+ gl.shaderSource(shader, source);
7
+ gl.compileShader(shader);
8
+
9
+ if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
10
+ throw new Error(gl.getShaderInfoLog(shader));
11
+ }
12
+
13
+ return shader;
14
+ };
15
+
16
+ /**
17
+ * Compile GLSL into a shader object
18
+ * @ignore
19
+ */
20
+ export function compileProgram(gl, vertex, fragment, attributes) {
21
+ var vertShader = compileShader(gl, gl.VERTEX_SHADER, vertex);
22
+ var fragShader = compileShader(gl, gl.FRAGMENT_SHADER, fragment);
23
+
24
+ var program = gl.createProgram();
25
+
26
+ gl.attachShader(program, vertShader);
27
+ gl.attachShader(program, fragShader);
28
+
29
+
30
+ // force vertex attributes to use location 0 as starting location to prevent
31
+ // browser to do complicated emulation when running on desktop OpenGL (e.g. on macOS)
32
+ for (var location in attributes) {
33
+ gl.bindAttribLocation(program, attributes[location], location);
34
+ }
35
+
36
+ gl.linkProgram(program);
37
+
38
+ if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {
39
+ var error_msg =
40
+ "Error initializing Shader " + this + "\n" +
41
+ "gl.VALIDATE_STATUS: " + gl.getProgramParameter(program, gl.VALIDATE_STATUS) + "\n" +
42
+ "gl.getError()" + gl.getError() + "\n" +
43
+ "gl.getProgramInfoLog()" + gl.getProgramInfoLog(program);
44
+ // house cleaning
45
+ gl.deleteProgram(program);
46
+ program = null;
47
+ // throw the exception
48
+ throw new Error(error_msg);
49
+ }
50
+
51
+ gl.useProgram(program);
52
+
53
+ // clean-up
54
+ gl.deleteShader(vertShader);
55
+ gl.deleteShader(fragShader);
56
+
57
+ return program;
58
+ };
@@ -0,0 +1,16 @@
1
+ /**
2
+ * clean the given source from space, comments, etc...
3
+ * @ignore
4
+ */
5
+ export function minify(src) {
6
+ // remove comments
7
+ src = src.replace(/\/\*[\s\S]*?\*\/|([^\\:]|^)\/\/.*$/gm, "$1");
8
+ // Remove leading and trailing whitespace from lines
9
+ src = src.replace(/(\\n\s+)|(\s+\\n)/g, "");
10
+ // Remove line breaks
11
+ src = src.replace(/(\\r|\\n)+/g, "");
12
+ // Remove unnecessary whitespace
13
+ src = src.replace(/\s*([;,[\](){}\\\/\-+*|^&!=<>?~%])\s*/g, "$1");
14
+
15
+ return src;
16
+ };
@@ -0,0 +1,87 @@
1
+
2
+ /**
3
+ * Hash map of GLSL data types to WebGL Uniform methods
4
+ * @ignore
5
+ */
6
+ const fnHash = {
7
+ "bool" : "1i",
8
+ "int" : "1i",
9
+ "float" : "1f",
10
+ "vec2" : "2fv",
11
+ "vec3" : "3fv",
12
+ "vec4" : "4fv",
13
+ "bvec2" : "2iv",
14
+ "bvec3" : "3iv",
15
+ "bvec4" : "4iv",
16
+ "ivec2" : "2iv",
17
+ "ivec3" : "3iv",
18
+ "ivec4" : "4iv",
19
+ "mat2" : "Matrix2fv",
20
+ "mat3" : "Matrix3fv",
21
+ "mat4" : "Matrix4fv",
22
+ "sampler2D" : "1i"
23
+ };
24
+
25
+ /**
26
+ * @ignore
27
+ */
28
+ export function extractUniforms(gl, shader) {
29
+ var uniforms = {},
30
+ uniRx = /uniform\s+(\w+)\s+(\w+)/g,
31
+ uniformsData = {},
32
+ descriptor = {},
33
+ locations = {},
34
+ match;
35
+
36
+ // Detect all uniform names and types
37
+ [ shader.vertex, shader.fragment ].forEach(function (shader) {
38
+ while ((match = uniRx.exec(shader))) {
39
+ uniformsData[match[2]] = match[1];
40
+ }
41
+ });
42
+
43
+ // Get uniform references
44
+ Object.keys(uniformsData).forEach(function (name) {
45
+ var type = uniformsData[name];
46
+ locations[name] = gl.getUniformLocation(shader.program, name);
47
+
48
+ descriptor[name] = {
49
+ "get" : (function (name) {
50
+ /**
51
+ * A getter for the uniform location
52
+ * @ignore
53
+ */
54
+ return function () {
55
+ return locations[name];
56
+ };
57
+ })(name),
58
+ "set" : (function (name, type, fn) {
59
+ if (type.indexOf("mat") === 0) {
60
+ /**
61
+ * A generic setter for uniform matrices
62
+ * @ignore
63
+ */
64
+ return function (val) {
65
+ gl[fn](locations[name], false, val);
66
+ };
67
+ }
68
+ else {
69
+ /**
70
+ * A generic setter for uniform vectors
71
+ * @ignore
72
+ */
73
+ return function (val) {
74
+ var fnv = fn;
75
+ if (val.length && fn.slice(-1) !== "v") {
76
+ fnv += "v";
77
+ }
78
+ gl[fnv](locations[name], val);
79
+ };
80
+ }
81
+ })(name, type, "uniform" + fnHash[type])
82
+ };
83
+ });
84
+ Object.defineProperties(uniforms, descriptor);
85
+
86
+ return uniforms;
87
+ };
@@ -384,20 +384,7 @@ class WebGLCompositor {
384
384
  this.activeShader = shader;
385
385
  this.activeShader.bind();
386
386
  this.activeShader.setUniform("uProjectionMatrix", this.renderer.projectionMatrix);
387
-
388
- // set the vertex attributes
389
- for (var index = 0; index < this.attributes.length; ++index) {
390
- var gl = this.gl;
391
- var element = this.attributes[index];
392
- var location = this.activeShader.getAttribLocation(element.name);
393
-
394
- if (location !== -1) {
395
- gl.enableVertexAttribArray(location);
396
- gl.vertexAttribPointer(location, element.size, element.type, element.normalized, this.vertexByteSize, element.offset);
397
- } else {
398
- gl.disableVertexAttribArray(index);
399
- }
400
- }
387
+ this.activeShader.setVertexAttributes(this.gl, this.attributes, this.vertexByteSize);
401
388
  }
402
389
  }
403
390