Skip to content

Commit 7e957d5

Browse files
committed
bruteforce tilemap canvas rendering
1 parent 6218c05 commit 7e957d5

2 files changed

Lines changed: 65 additions & 3 deletions

File tree

v3/src/gameobjects/tilemap/static/StaticTilemap.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,44 @@ var StaticTilemap = new Class({
157157
this.tilemapRenderer.shader.setConstantFloat2(this.tilemapRenderer.scrollFactorLocation, this.scrollFactorX, this.scrollFactorY);
158158
this.tilemapRenderer.shader.setConstantFloat2(this.tilemapRenderer.tilemapPositionLocation, this.x, this.y);
159159
}
160+
else if (this.dirty && !this.gl)
161+
{
162+
var mapWidth = this.mapWidth;
163+
var mapHeight = this.mapHeight;
164+
var border = this.tileBorder;
165+
var tileWidth = this.tileWidth;
166+
var tileHeight = this.tileHeight;
167+
var tileWidthBorder = tileWidth + border * 2;
168+
var tileHeightBorder = tileHeight + border * 2;
169+
var width = this.texture.source[0].width;
170+
var height = this.texture.source[0].height;
171+
var setWidth = width / tileWidth;
172+
var mapData = this.mapData;
173+
174+
this.tiles = [];
175+
176+
for (var y = 0; y < mapHeight; ++y)
177+
{
178+
for (var x = 0; x < mapWidth; ++x)
179+
{
180+
var tileId = mapData[y * mapWidth + x];
181+
var frameX = (((tileId % setWidth)|0) * tileWidthBorder);
182+
var frameY = (((tileId / setWidth)|0) * tileHeightBorder);
183+
var tx = x * tileWidth;
184+
var ty = y * tileHeight;
185+
186+
this.tiles.push({
187+
x: tx,
188+
y: ty,
189+
frameX: frameX,
190+
frameY: frameY
191+
});
192+
193+
}
194+
}
195+
196+
this.dirty = false;
197+
}
160198
},
161199

162200
getTotalTileCount: function ()
Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,35 @@
1-
var StaticTilemapCanvasRenderer = function (renderer, src, interpolationPercentage, camera)
1+
var StaticTilemapCanvasRenderer = function (renderer, gameObject, interpolationPercentage, camera)
22
{
3-
if (src.renderMask !== src.renderFlags || (src.cameraFilter > 0 && (src.cameraFilter & camera._id)))
3+
if (gameObject.renderMask !== gameObject.renderFlags || (gameObject.cameraFilter > 0 && (gameObject.cameraFilter & camera._id)))
44
{
55
return;
66
}
77

8-
// TODO :)
8+
gameObject.upload(camera);
9+
10+
var tiles = gameObject.tiles;
11+
var tileWidth = gameObject.tileWidth;
12+
var tileHeight = gameObject.tileHeight;
13+
var frame = gameObject.frame;
14+
var ctx = renderer.gameContext;
15+
var tileCount = tiles.length;
16+
var image = frame.source.image;
17+
var tx = gameObject.x - camera.scrollX * gameObject.scrollFactorX;
18+
var ty = gameObject.y - camera.scrollY * gameObject.scrollFactorY;
19+
ctx.save();
20+
ctx.translate(tx, ty);
21+
ctx.rotate(gameObject.rotation);
22+
ctx.scale(gameObject.scaleX, gameObject.scaleY);
23+
ctx.scale(gameObject.flipX ? -1 : 1, gameObject.flipY ? -1 : 1);
24+
25+
for (var index = 0; index < tileCount; ++index)
26+
{
27+
var tile = tiles[index];
28+
ctx.drawImage(image, tile.frameX, tile.frameY, tileWidth, tileHeight, tile.x, tile.y, tileWidth, tileHeight);
29+
}
30+
31+
ctx.restore();
32+
933
};
1034

1135
module.exports = StaticTilemapCanvasRenderer;

0 commit comments

Comments
 (0)