Skip to content

Commit

Permalink
Fixed Tilemap rendering when camera moves
Browse files Browse the repository at this point in the history
  • Loading branch information
MattTuttle committed Jan 22, 2013
1 parent 0f0e252 commit 5684e65
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 24 deletions.
32 changes: 17 additions & 15 deletions examples/src/platformer/GameWorld.hx
Expand Up @@ -12,21 +12,21 @@ class GameWorld extends DemoWorld
{

private static var map:Array<Array<Int>> = [
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1],
[1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1],
[1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1],
[1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1],
[1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1],
[1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1],
[1, 1, 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, 1, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
];

public function new()
Expand Down Expand Up @@ -80,6 +80,8 @@ class GameWorld extends DemoWorld
{
backdrop.x += 1;
backdrop.y += 2 * HXP.sign(player.gravity.y);
HXP.camera.x = player.x - HXP.halfWidth;
HXP.camera.y = player.y - HXP.halfWidth;
super.update();
}

Expand Down
28 changes: 19 additions & 9 deletions src/com/haxepunk/graphics/Tilemap.hx
Expand Up @@ -421,23 +421,33 @@ class Tilemap extends Canvas
_point.x = point.x + x - camera.x * scrollX;
_point.y = point.y + y - camera.y * scrollY;

var wx:Float = _point.x, wy:Float = _point.y, tile:Int = 0,
scalex:Float = HXP.screen.fullScaleX, scaley:Float = HXP.screen.fullScaleY,
tw:Int = Std.int(tileWidth * scalex), th:Int = Std.int(tileHeight * scaley);
var scalex:Float = HXP.screen.fullScaleX, scaley:Float = HXP.screen.fullScaleY,
tw:Int = Std.int(tileWidth * scalex), th:Int = Std.int(tileHeight * scaley);

// determine start and end tiles to draw (optimization)
var startx = Math.floor(_point.x / tw),
starty = Math.floor(_point.y / th),
destx = Math.ceil((_point.x + HXP.width) / tw),
desty = Math.ceil((_point.y + HXP.height) / th),
r = HXP.getRed(color)/255, g = HXP.getGreen(color)/255, b = HXP.getBlue(color)/255;
var startx = -Math.floor(_point.x / tw),
starty = -Math.floor(_point.y / th),
destx = startx + Math.ceil(HXP.width / tw),
desty = starty + Math.ceil(HXP.height / th);

// BUG? the starting point seems to be off by 1...
startx = startx - 1; starty = starty - 1;

// nothing will render if we're completely off screen
if (startx > _columns || starty > _rows || destx < 0 || desty < 0)
return;

// clamp values to boundaries
if (startx < 0) startx = 0;
if (destx > _columns) destx = _columns;
if (starty < 0) starty = 0;
if (desty > _rows) desty = _rows;

var r = HXP.getRed(color)/255, g = HXP.getGreen(color)/255, b = HXP.getBlue(color)/255,
wx = _point.x + startx * tw,
wy = _point.y + starty * th,
tile = 0;

for (y in starty...desty)
{
for (x in startx...destx)
Expand All @@ -450,7 +460,7 @@ class Tilemap extends Canvas

wx += tw;
}
wx = _point.x;
wx = _point.x + startx * tw;
wy += th;
}
}
Expand Down

0 comments on commit 5684e65

Please sign in to comment.