|
@@ -3,9 +3,12 @@ package pathgame.rendering;
|
|
|
import me.hammerle.snuviengine.api.Renderer;
|
|
|
import me.hammerle.snuviengine.api.Texture;
|
|
|
import me.hammerle.snuviengine.api.TextureRenderer;
|
|
|
+import pathgame.tilemap.Tile;
|
|
|
import pathgame.tilemap.TileMap;
|
|
|
+import pathgame.tilemap.TileRenderType;
|
|
|
|
|
|
-/** A renderer for tile maps.
|
|
|
+/**
|
|
|
+ * A renderer for tile maps.
|
|
|
*
|
|
|
* @author kajetan
|
|
|
*/
|
|
@@ -14,19 +17,23 @@ public class TileMapRenderer
|
|
|
// prevents rendering artifacts especially on different zoom levels
|
|
|
private final static float ERROR = 1.0f / 512.0F;
|
|
|
private final static float T_ERROR = 1.0f / 4096.0F;
|
|
|
-
|
|
|
+
|
|
|
private final Texture tileTexture = new Texture("resources/tiles.png");
|
|
|
private final TextureRenderer textureRenderer = new TextureRenderer(20 * 20 * 2); // default to 20x20 map
|
|
|
+ private final TextureRenderer swampWaterOverlayRenderer = new TextureRenderer(20 * 20 * 2); // default to 20x20 map
|
|
|
+ private final TextureRenderer grassOverlayRenderer = new TextureRenderer(20 * 20 * 2); // default to 20x20 map
|
|
|
private float scale = 1.0f;
|
|
|
-
|
|
|
- /** Creates a new tile map renderer.
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Creates a new tile map renderer.
|
|
|
*
|
|
|
*/
|
|
|
public TileMapRenderer()
|
|
|
{
|
|
|
}
|
|
|
|
|
|
- /** Sets the scale of the map.
|
|
|
+ /**
|
|
|
+ * Sets the scale of the map.
|
|
|
*
|
|
|
* @param scale the scale of the map
|
|
|
*/
|
|
@@ -35,7 +42,8 @@ public class TileMapRenderer
|
|
|
this.scale = scale;
|
|
|
}
|
|
|
|
|
|
- /** Returns the scale of the map.
|
|
|
+ /**
|
|
|
+ * Returns the scale of the map.
|
|
|
*
|
|
|
* @return the scale of the map
|
|
|
*/
|
|
@@ -43,8 +51,9 @@ public class TileMapRenderer
|
|
|
{
|
|
|
return scale;
|
|
|
}
|
|
|
-
|
|
|
- /** Returns the scaled render width of a map.
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Returns the scaled render width of a map.
|
|
|
*
|
|
|
* @param map a map
|
|
|
* @return the scaled render width of a map
|
|
@@ -53,8 +62,9 @@ public class TileMapRenderer
|
|
|
{
|
|
|
return map.getWidth() * TileRenderer.TILE_SIZE * scale;
|
|
|
}
|
|
|
-
|
|
|
- /** Returns the scaled render height of a map.
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Returns the scaled render height of a map.
|
|
|
*
|
|
|
* @param map a map
|
|
|
* @return the scaled render height of a map
|
|
@@ -63,38 +73,158 @@ public class TileMapRenderer
|
|
|
{
|
|
|
return map.getHeight() * TileRenderer.TILE_SIZE * scale;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
+ private void addTile(TextureRenderer tr, int x, int y, float tMinX, float tMinY, float tMaxX, float tMaxY)
|
|
|
+ {
|
|
|
+ tr.addRectangle(
|
|
|
+ x * TileRenderer.TILE_SIZE - ERROR, y * TileRenderer.TILE_SIZE - ERROR,
|
|
|
+ (x + 1) * TileRenderer.TILE_SIZE + ERROR, (y + 1) * TileRenderer.TILE_SIZE + ERROR,
|
|
|
+ tMinX + T_ERROR, tMinY + T_ERROR,
|
|
|
+ tMaxX - T_ERROR, tMaxY - T_ERROR);
|
|
|
+ }
|
|
|
+
|
|
|
+ private boolean isInRange(TileMap map, int x, int y)
|
|
|
+ {
|
|
|
+ return x >= 0 && y >= 0 && x < map.getWidth() && y < map.getHeight();
|
|
|
+ }
|
|
|
+
|
|
|
+ private boolean isSwamp(TileMap map, int x, int y)
|
|
|
+ {
|
|
|
+ return isInRange(map, x, y) && map.getTile(x, y).getRenderType() == TileRenderType.SWAMP;
|
|
|
+ }
|
|
|
+
|
|
|
+ private boolean isSwampOrWaterOrBorder(TileMap map, int x, int y)
|
|
|
+ {
|
|
|
+ if(!isInRange(map, x, y))
|
|
|
+ {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ TileRenderType type = map.getTile(x, y).getRenderType();
|
|
|
+ return type == TileRenderType.SWAMP || type == TileRenderType.WATER;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void addWaterSwampOverlay(TileMap map, int x, int y)
|
|
|
+ {
|
|
|
+ boolean n = isSwamp(map, x, y - 1);
|
|
|
+ boolean e = isSwamp(map, x + 1, y);
|
|
|
+ boolean s = isSwamp(map, x, y + 1);
|
|
|
+ boolean w = isSwamp(map, x - 1, y);
|
|
|
+
|
|
|
+ if(!n && !w && isSwamp(map, x - 1, y - 1)) // upper, left corner
|
|
|
+ {
|
|
|
+ addTile(swampWaterOverlayRenderer, x, y, 0.0f, 0.0f, 0.5f, 0.5f);
|
|
|
+ }
|
|
|
+ if(!n && !e && isSwamp(map, x + 1, y - 1)) // upper, right corner
|
|
|
+ {
|
|
|
+ addTile(swampWaterOverlayRenderer, x, y, 0.0f, 0.0f, 0.5f, 0.5f);
|
|
|
+ }
|
|
|
+ if(!s && !w && isSwamp(map, x - 1, y + 1)) // lower, left corner
|
|
|
+ {
|
|
|
+ addTile(swampWaterOverlayRenderer, x, y, 0.0f, 0.0f, 0.5f, 0.5f);
|
|
|
+ }
|
|
|
+ if(!s && !e && isSwamp(map, x + 1, y + 1)) // lower, right corner
|
|
|
+ {
|
|
|
+ addTile(swampWaterOverlayRenderer, x, y, 0.0f, 0.0f, 0.5f, 0.5f);
|
|
|
+ }
|
|
|
+
|
|
|
+ int index = 0;
|
|
|
+ index += n ? 0 : 8;
|
|
|
+ index += e ? 0 : 4;
|
|
|
+ index += s ? 0 : 2;
|
|
|
+ index += w ? 0 : 1;
|
|
|
+ if(index == 15)
|
|
|
+ {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ float tMinX = index * TileRenderer.TILE_SIZE / TileTexture.TEXTURE_SIZE;
|
|
|
+ float tMaxX = (index + 1) * TileRenderer.TILE_SIZE / TileTexture.TEXTURE_SIZE;
|
|
|
+
|
|
|
+ addTile(swampWaterOverlayRenderer, x, y, tMinX, 0.0f, tMaxX, 0.5f);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void addGrassOverlay(TileMap map, int x, int y)
|
|
|
+ {
|
|
|
+ boolean n = isSwampOrWaterOrBorder(map, x, y - 1);
|
|
|
+ boolean e = isSwampOrWaterOrBorder(map, x + 1, y);
|
|
|
+ boolean s = isSwampOrWaterOrBorder(map, x, y + 1);
|
|
|
+ boolean w = isSwampOrWaterOrBorder(map, x - 1, y);
|
|
|
+
|
|
|
+ if(n && w && !isSwampOrWaterOrBorder(map, x - 1, y - 1)) // upper, left corner
|
|
|
+ {
|
|
|
+ addTile(grassOverlayRenderer, x, y, 0.0f, 0.0f, 0.5f, 0.5f);
|
|
|
+ }
|
|
|
+ if(n && e && !isSwampOrWaterOrBorder(map, x + 1, y - 1)) // upper, right corner
|
|
|
+ {
|
|
|
+ addTile(grassOverlayRenderer, x, y, 0.0f, 0.0f, 0.5f, 0.5f);
|
|
|
+ }
|
|
|
+ if(s && w && !isSwampOrWaterOrBorder(map, x - 1, y + 1)) // lower, left corner
|
|
|
+ {
|
|
|
+ addTile(grassOverlayRenderer, x, y, 0.0f, 0.0f, 0.5f, 0.5f);
|
|
|
+ }
|
|
|
+ if(s && e && !isSwampOrWaterOrBorder(map, x + 1, y + 1)) // lower, right corner
|
|
|
+ {
|
|
|
+ addTile(grassOverlayRenderer, x, y, 0.0f, 0.0f, 0.5f, 0.5f);
|
|
|
+ }
|
|
|
+
|
|
|
+ int index = 0;
|
|
|
+ index += !n ? 0 : 8;
|
|
|
+ index += !e ? 0 : 4;
|
|
|
+ index += !s ? 0 : 2;
|
|
|
+ index += !w ? 0 : 1;
|
|
|
+ if(index == 15)
|
|
|
+ {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ float tMinX = index * TileRenderer.TILE_SIZE / TileTexture.TEXTURE_SIZE;
|
|
|
+ float tMaxX = (index + 1) * TileRenderer.TILE_SIZE / TileTexture.TEXTURE_SIZE;
|
|
|
+ addTile(grassOverlayRenderer, x, y, tMinX, 0.0f, tMaxX, 0.5f);
|
|
|
+ }
|
|
|
+
|
|
|
private void updateData(TileMap map)
|
|
|
{
|
|
|
textureRenderer.clear();
|
|
|
+ swampWaterOverlayRenderer.clear();
|
|
|
+ grassOverlayRenderer.clear();
|
|
|
for(int x = 0; x < map.getWidth(); x++)
|
|
|
{
|
|
|
for(int y = 0; y < map.getHeight(); y++)
|
|
|
{
|
|
|
- TileTexture tt = TileRenderer.getTileTexture(map, map.getTile(x, y), x, y);
|
|
|
+ Tile t = map.getTile(x, y);
|
|
|
+ TileTexture tt = TileRenderer.getTileTexture(map, t, x, y);
|
|
|
if(tt == null)
|
|
|
{
|
|
|
continue;
|
|
|
}
|
|
|
- textureRenderer.addRectangle(
|
|
|
- x * TileRenderer.TILE_SIZE - ERROR, y * TileRenderer.TILE_SIZE - ERROR,
|
|
|
- (x + 1) * TileRenderer.TILE_SIZE + ERROR, (y + 1) * TileRenderer.TILE_SIZE + ERROR,
|
|
|
- tt.getMinX() + T_ERROR, tt.getMinY() + T_ERROR,
|
|
|
- tt.getMaxX() - T_ERROR, tt.getMaxY() - T_ERROR);
|
|
|
+ /*if(t.getRenderType() == TileRenderType.WATER)
|
|
|
+ {
|
|
|
+ addWaterSwampOverlay(map, x, y);
|
|
|
+ addGrassOverlay(map, x, y);
|
|
|
+ }
|
|
|
+ else if(t.getRenderType() == TileRenderType.SWAMP)
|
|
|
+ {
|
|
|
+ addGrassOverlay(map, x, y);
|
|
|
+ }*/
|
|
|
+ addTile(textureRenderer, x, y, tt.getMinX(), tt.getMinY(), tt.getMaxX(), tt.getMaxY());
|
|
|
}
|
|
|
}
|
|
|
textureRenderer.build();
|
|
|
+ swampWaterOverlayRenderer.build();
|
|
|
+ grassOverlayRenderer.build();
|
|
|
}
|
|
|
-
|
|
|
- /** Ticks the renderer. Used for animated tiles.
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Ticks the renderer. Used for animated tiles.
|
|
|
*
|
|
|
*/
|
|
|
public void tick()
|
|
|
{
|
|
|
// tick tile animations here
|
|
|
}
|
|
|
-
|
|
|
- /** Draws the given map at the given offset.
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Draws the given map at the given offset.
|
|
|
*
|
|
|
* @param map a map
|
|
|
* @param r the renderer given by the engine
|
|
@@ -107,22 +237,24 @@ public class TileMapRenderer
|
|
|
r.setTextureEnabled(true);
|
|
|
r.setColorEnabled(false);
|
|
|
r.setMixColorEnabled(false);
|
|
|
-
|
|
|
+
|
|
|
tileTexture.bind();
|
|
|
-
|
|
|
+
|
|
|
if(forceUpdate || map.isDirty())
|
|
|
{
|
|
|
updateData(map);
|
|
|
map.clean();
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
float viewScale = r.getViewScale();
|
|
|
offX = (int) (offX * viewScale) / viewScale;
|
|
|
offY = (int) (offY * viewScale) / viewScale;
|
|
|
-
|
|
|
+
|
|
|
r.translateTo(offX, offY);
|
|
|
r.scale(scale, scale);
|
|
|
r.updateMatrix();
|
|
|
textureRenderer.draw();
|
|
|
+ swampWaterOverlayRenderer.draw();
|
|
|
+ grassOverlayRenderer.draw();
|
|
|
}
|
|
|
}
|