TileMapRenderer.java 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package pathgame.rendering;
  2. import me.hammerle.snuviengine.api.Renderer;
  3. import me.hammerle.snuviengine.api.Texture;
  4. import me.hammerle.snuviengine.api.TextureRenderer;
  5. import pathgame.tilemap.TileMap;
  6. /** A renderer for tile maps.
  7. *
  8. * @author kajetan
  9. */
  10. public class TileMapRenderer
  11. {
  12. private final Texture tileTexture = new Texture("resources/tiles.png");
  13. private final TextureRenderer textureRenderer = new TextureRenderer(20 * 20 * 2); // default to 20x20 map
  14. private float scale = 1.0f;
  15. /** Creates a new tile map renderer.
  16. *
  17. */
  18. public TileMapRenderer()
  19. {
  20. }
  21. /** Sets the scale of the map.
  22. *
  23. * @param scale the scale of the map
  24. */
  25. public void setScale(float scale)
  26. {
  27. this.scale = scale;
  28. }
  29. /** Returns the scale of the map.
  30. *
  31. * @return the scale of the map
  32. */
  33. public float getScale()
  34. {
  35. return scale;
  36. }
  37. /** Returns the scaled render width of a map.
  38. *
  39. * @param map a map
  40. * @return the scaled render width of a map
  41. */
  42. public float getWidth(TileMap map)
  43. {
  44. return map.getWidth() * TileRenderer.TILE_SIZE * scale;
  45. }
  46. /** Returns the scaled render height of a map.
  47. *
  48. * @param map a map
  49. * @return the scaled render height of a map
  50. */
  51. public float getHeight(TileMap map)
  52. {
  53. return map.getHeight() * TileRenderer.TILE_SIZE * scale;
  54. }
  55. private void updateData(TileMap map)
  56. {
  57. textureRenderer.clear();
  58. for(int x = 0; x < map.getWidth(); x++)
  59. {
  60. for(int y = 0; y < map.getHeight(); y++)
  61. {
  62. TileTexture tt = TileRenderer.getTileTexture(map, map.getTile(x, y), x, y);
  63. if(tt == null)
  64. {
  65. continue;
  66. }
  67. textureRenderer.addRectangle(
  68. x * TileRenderer.TILE_SIZE, y * TileRenderer.TILE_SIZE,
  69. (x + 1) * TileRenderer.TILE_SIZE, (y + 1) * TileRenderer.TILE_SIZE,
  70. tt.getMinX(), tt.getMinY(), tt.getMaxX(), tt.getMaxY());
  71. }
  72. }
  73. textureRenderer.build();
  74. }
  75. /** Ticks the renderer. Used for animated tiles.
  76. *
  77. */
  78. public void tick()
  79. {
  80. // tick tile animations here
  81. }
  82. /** Draws the given map at the given offset.
  83. *
  84. * @param map a map
  85. * @param r the renderer given by the engine
  86. * @param forceUpdate whether an update of the render data should be forced
  87. * @param offX the x coordinate of the offset
  88. * @param offY the y coordinate of the offset
  89. */
  90. public void renderTick(TileMap map, Renderer r, boolean forceUpdate, float offX, float offY)
  91. {
  92. r.setTextureEnabled(true);
  93. r.setColorEnabled(false);
  94. r.setMixColorEnabled(false);
  95. tileTexture.bind();
  96. if(forceUpdate || map.isDirty())
  97. {
  98. updateData(map);
  99. map.clean();
  100. }
  101. r.translateTo(offX, offY);
  102. r.scale(scale, scale);
  103. r.updateMatrix();
  104. textureRenderer.draw();
  105. }
  106. }