Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed Tilemap rendering when camera moves

  • Loading branch information...
commit 5684e65f4e4c0872b69c797c9aca896043c2e971 1 parent 0f0e252
@MattTuttle authored
View
32 examples/src/platformer/GameWorld.hx
@@ -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, 1, 1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1, 1, 1, 0, 0, 0, 0, 0, 0, 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()
@@ -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();
}
View
28 src/com/haxepunk/graphics/Tilemap.hx
@@ -421,16 +421,21 @@ 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;
@@ -438,6 +443,11 @@ class Tilemap extends Canvas
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)
@@ -450,7 +460,7 @@ class Tilemap extends Canvas
wx += tw;
}
- wx = _point.x;
+ wx = _point.x + startx * tw;
wy += th;
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.