TileTexture.java 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package pathgame.rendering;
  2. /** A container for tile texture coordinates.
  3. *
  4. * @author kajetan
  5. */
  6. public class TileTexture
  7. {
  8. private final static float TEXTURE_SIZE = 512;
  9. private final static float TILE_TEXTURE_PERCENT = TileRenderer.TILE_SIZE / TEXTURE_SIZE;
  10. private final static int TILES_PER_LINE = (int) (TEXTURE_SIZE / TileRenderer.TILE_SIZE);
  11. /** Returns a tile texture constructed from a given texture id.
  12. *
  13. * @param textureId a texture id
  14. * @return a tile texture constructed from a given texture id
  15. */
  16. public static TileTexture fromTextureId(int textureId)
  17. {
  18. float tMinX = (textureId % TILES_PER_LINE) * TILE_TEXTURE_PERCENT;
  19. float tMinY = (textureId/ TILES_PER_LINE) * TILE_TEXTURE_PERCENT;
  20. float tMaxX = tMinX + TILE_TEXTURE_PERCENT;
  21. float tMaxY = tMinY + TILE_TEXTURE_PERCENT;
  22. return new TileTexture(tMinX, tMinY, tMaxX, tMaxY);
  23. }
  24. private final float tMinX;
  25. private final float tMinY;
  26. private final float tMaxX;
  27. private final float tMaxY;
  28. /** Creates a new tile texture.
  29. *
  30. * @param tMinX the left x coordinate in the texture atlas
  31. * @param tMinY the upper y coordinate in the texture atlas
  32. * @param tMaxX the right x coordinate in the texture atlas
  33. * @param tMaxY the lower y coordinate in the texture atlas
  34. */
  35. public TileTexture(float tMinX, float tMinY, float tMaxX, float tMaxY)
  36. {
  37. this.tMinX = tMinX;
  38. this.tMinY = tMinY;
  39. this.tMaxX = tMaxX;
  40. this.tMaxY = tMaxY;
  41. }
  42. /** Returns the the left x coordinate in the texture atlas.
  43. *
  44. * @return the left x coordinate in the texture atlas
  45. */
  46. public float getMinX()
  47. {
  48. return tMinX;
  49. }
  50. /** Returns the upper y coordinate in the texture atlas.
  51. *
  52. * @return the upper y coordinate in the texture atlas
  53. */
  54. public float getMinY()
  55. {
  56. return tMinY;
  57. }
  58. /** Returns the right x coordinate in the texture atlas.
  59. *
  60. * @return the right x coordinate in the texture atlas
  61. */
  62. public float getMaxX()
  63. {
  64. return tMaxX;
  65. }
  66. /** Returns the lower y coordinate in the texture atlas.
  67. *
  68. * @return the lower y coordinate in the texture atlas
  69. */
  70. public float getMaxY()
  71. {
  72. return tMaxY;
  73. }
  74. }