BlueGit

timfa frameworkv3

Files

frameworkv3 / framework / engine / image.js

image.js

engine optimisations
timfa Tim Falken · pushed by timfa · 3 months ago
74d8cdb3 History
image.js 3.3 KiB Download Raw
1class image
2{
3 subImages = null;
4 subImagesPerRow = null;
5 loop = false;
6 chain = null;
7
8 g_image = null;
9
10 animationCycles = {};
11
12 constructor (image, subImgTotal = 1, perRow = 1, loop = false, chain = null)
13 {
14 subImgTotal = subImgTotal || 1;
15 perRow = perRow || 1;
16
17 this.subImages = subImgTotal;
18 this.subImagesPerRow = perRow;
19 this.g_image = image;
20 this.loop = loop;
21 this.chain = chain;
22
23 this.cacheWidth = -1;
24 this.cacheHeight = -1;
25
26 // Keep the Image out of the DOM — canvas can draw from it directly.
27 if (image && image.style)
28 { image.style.display = "none"; }
29
30 console.log("Image loaded: " + (image && image.src ? image.src : ""));
31 }
32
33 draw (scene, dx, dy, dw, dh, rotation, mirrorX, mirrorY, alpha, subImage, shader, uniforms, textures, lights)
34 {
35 if (!scene.glContext || !this.g_image)
36 { return; }
37
38 // One hypot for frustum pad (was computed 4× per sprite).
39 const pad = Math.sqrt(dw * dw + dh * dh);
40 if (dx < -pad || dy < -pad || dx > scene.real_size.x + pad || dy > scene.real_size.y + pad)
41 { return; }
42
43 const subImageData = this.getSubImage(subImage);
44 const imgW = this.g_image.width;
45 const imgH = this.g_image.height;
46 let glTextures = null;
47
48 if (textures)
49 {
50 for (const name in textures)
51 {
52 if (!Object.prototype.hasOwnProperty.call(textures, name))
53 { continue; }
54
55 const src = textures[name];
56 if (!src)
57 { continue; }
58
59 if (!glTextures)
60 { glTextures = {}; }
61
62 const htmlImage = src.g_image || src;
63 glTextures[name] = scene.glContext.getTexture(htmlImage);
64 }
65 }
66
67 scene.glContext.drawSprite({
68 texture: scene.glContext.getTexture(this.g_image),
69 x: dx,
70 y: dy,
71 w: dw,
72 h: dh,
73 rotationDeg: rotation,
74 mirrorX: mirrorX,
75 mirrorY: mirrorY,
76 alpha: alpha,
77 uvRect: {
78 u0: subImageData.sx / imgW,
79 v0: subImageData.sy / imgH,
80 u1: (subImageData.sx + subImageData.sw) / imgW,
81 v1: (subImageData.sy + subImageData.sh) / imgH
82 },
83 shader: shader || scene.glContext.defaultShader,
84 uniforms: uniforms,
85 textures: glTextures,
86 lights: lights
87 });
88 }
89
90 hasAnimationCycle (label)
91 {
92 return this.animationCycles[label] != null;
93 }
94
95 addAnimationCycle (label, cycle, loop, chain, chainDefaultIndex, chainSpeed)
96 {
97 this.animationCycles[label] =
98 {
99 frames: cycle,
100 loop: loop,
101 chain: chain,
102 chainDefaultIndex: chainDefaultIndex,
103 chainSpeed: chainSpeed
104 };
105 }
106
107 get width ()
108 {
109 if (this.cacheWidth < 0)
110 { this.cacheWidth = this.g_image.width / this.subImagesPerRow; }
111
112 return this.cacheWidth;
113 }
114
115 get height ()
116 {
117 if (this.cacheHeight < 0)
118 { this.cacheHeight = this.g_image.height / (Math.ceil(this.subImages / this.subImagesPerRow) + 0); }
119
120 return this.cacheHeight;
121 }
122
123 cacheWidth = -1;
124 cacheHeight = -1;
125
126 getSubImage (subImageIndex)
127 {
128 var subImage = Math.round(subImageIndex);
129 subImage %= this.subImages;
130
131 var c = subImage % this.subImagesPerRow;
132 var r = Math.floor(subImage / this.subImagesPerRow);
133 var sw = (this.g_image.width / this.subImagesPerRow);
134 var sh = (this.g_image.height / Math.ceil(this.subImages / this.subImagesPerRow));
135 var sx = sw * c;
136 var sy = sh * r;
137
138 return { sw: sw, sh: sh, sx: sx, sy: sy };
139 }
140}