Map.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #include "Map.h"
  2. #include "render/TileRenderer.h"
  3. #include "Graphics.h"
  4. #include "sprites.h"
  5. #include "utility/Rect.h"
  6. #include "utility/misc.h"
  7. #define MAP WalrusRPG::Map
  8. using namespace WalrusRPG;
  9. using namespace WalrusRPG::Utils;
  10. Graphics::Texture tex_overworld((char *) overworld);
  11. MAP::Map(int width, int height, uint16_t *layer0, uint16_t *layer1) : anim()
  12. {
  13. this->renderer = new TileRenderer(tex_overworld, 16, 16);
  14. this->width = width;
  15. this->height = height;
  16. this->layer0 = layer0;
  17. this->layer1 = layer1;
  18. }
  19. MAP::~Map()
  20. {
  21. // TODO if you allocate dynamically members
  22. }
  23. void MAP::update(unsigned dt)
  24. {
  25. // TODO update map's data according to elasped time
  26. UNUSED(dt);
  27. }
  28. void MAP::render(WalrusRPG::Camera &camera, unsigned dt)
  29. {
  30. anim.update(dt);
  31. signed t_width = renderer->get_tile_width();
  32. signed t_height = renderer->get_tile_height();
  33. // Substractions here because we want to always round down when dividing
  34. signed offset_x = camera.get_x() % t_width * -1 - (camera.get_x() < 0) * t_width;
  35. signed offset_y = camera.get_y() % t_height * -1 - (camera.get_y() < 0) * t_height;
  36. signed start_x = camera.get_x() / t_width - (camera.get_x() < 0);
  37. signed start_y = camera.get_y() / t_height - (camera.get_y() < 0);
  38. // pre-calculating variables to speed up loop condition check
  39. signed delta_x = 320 / t_width + 1;
  40. signed delta_y = 240 / t_height + 1;
  41. // rendering part.
  42. for (signed j = 0; j < delta_y; j++)
  43. {
  44. for (signed i = 0; i < delta_x; i++)
  45. {
  46. int index, tile_over;
  47. index = (start_x + i) + (start_y + j) * this->width;
  48. if (in_range(start_x + i, 0, (signed) width) &&
  49. in_range(start_y + j, 0, (signed) height))
  50. tile_over = anim.get_animation_frame(this->layer0[index]);
  51. else
  52. tile_over = 0;
  53. renderer->render(tile_over,
  54. Rect(offset_x + i * t_width, offset_y + j * t_height));
  55. // layer1 : Over-layer
  56. if (this->layer1 == nullptr)
  57. continue;
  58. if (in_range(start_x + i, 0, (signed) width) &&
  59. in_range(start_y + j, 0, (signed) height))
  60. tile_over = anim.get_animation_frame(this->layer1[index]);
  61. else
  62. tile_over = 0;
  63. if (tile_over != 0)
  64. renderer->render(anim.get_animation_frame(tile_over),
  65. Rect(offset_x + i * t_width, offset_y + j * t_height));
  66. }
  67. }
  68. }
  69. bool MAP::is_tile_solid(int x, int y) const
  70. {
  71. if (x >= width || y >= height)
  72. return true;
  73. return this->layer0[y * width + x] != 0;
  74. }
  75. bool MAP::is_pixel_solid(int x, int y) const
  76. {
  77. return is_tile_solid(x / renderer->get_tile_width(), y / renderer->get_tile_height());
  78. }
  79. int MAP::get_width() const
  80. {
  81. return this->width;
  82. }
  83. int MAP::get_height() const
  84. {
  85. return this->width;
  86. }