Map.cpp 2.9 KB

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