Map.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #include "Map.h"
  2. #include "Camera.h"
  3. #include "Graphics.h"
  4. #include "sprites.h"
  5. #include "Rect.h"
  6. #include "TileRenderer.h"
  7. #include "misc.h"
  8. #define MAP WalrusRPG::Map
  9. #define RECT WalrusRPG::Utils::Rect
  10. #define TILERENDERER WalrusRPG::TileRenderer
  11. MAP::Map(int width, int height, uint16_t *layer0, uint16_t *layer1) : anim()
  12. {
  13. this->renderer = new TileRenderer(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. // By Eiyeron : I assumed that the camera's position is the top left pixel.
  34. // Margins moves the rendered map if we go outside of the bounds (specially on the
  35. // left or on the top).
  36. signed offset_x = camera.get_x() % t_width * -1;
  37. signed offset_y = camera.get_y() % t_height * -1;
  38. signed start_x = camera.get_x() / t_width;
  39. signed start_y = camera.get_y() / t_height;
  40. signed end_x = start_x + 320 / t_width + 2; // Why 2? I don't freaking know.
  41. signed end_y = start_y + 240 / t_height + 1;
  42. // Bound-checking code. To avoid reading outside of the map.
  43. // The offset edition allows the map to be correctly moved to the right to make up for
  44. // the lack of renderable tiles.
  45. if (start_x < 0)
  46. {
  47. offset_x -= start_x * t_width;
  48. start_x = 0;
  49. }
  50. if (start_y < 0)
  51. {
  52. offset_y -= start_y * t_height;
  53. start_y = 0;
  54. }
  55. // warning fix. Even if end_x/y is negative, it should be higher than width/height.
  56. if ((unsigned) end_x > this->width)
  57. end_x = this->width;
  58. if ((unsigned) end_y > this->height)
  59. end_y = this->height;
  60. // pre-calculating variables to speed up loop condition check
  61. signed delta_x = end_x - start_x;
  62. signed delta_y = end_y - start_y;
  63. // Creating a region clip. Why does it smell like SDL?
  64. /*WalrusRPG::Graphics::Rect_t sprite;
  65. sprite.y = 0;
  66. sprite.w = 24;
  67. sprite.h = 24;*/
  68. // rendering part.
  69. for (signed j = 0; j < delta_y; j++)
  70. {
  71. for (signed i = 0; i < delta_x; i++)
  72. {
  73. unsigned index = (start_x + i) + (start_y + j) * this->width;
  74. unsigned tile_over = anim.get_animation_frame(this->layer0[index]);
  75. if (tile_over != 0)
  76. renderer->render(tile_over,
  77. RECT(offset_x + i * t_width, offset_y + j * t_height));
  78. // layer1 : Over-layer
  79. if (this->layer1 == NULL)
  80. continue;
  81. tile_over = anim.get_animation_frame(this->layer1[index]);
  82. if (tile_over != 0)
  83. renderer->render(anim.get_animation_frame(tile_over),
  84. RECT(offset_x + i * t_width, offset_y + j * t_height));
  85. }
  86. }
  87. }
  88. bool MAP::is_tile_solid(unsigned x, unsigned y) const
  89. {
  90. if (x >= width || y >= height)
  91. return true;
  92. return this->layer0[y * width + x] != 0;
  93. }
  94. bool MAP::is_pixel_solid(unsigned x, unsigned y) const
  95. {
  96. return is_tile_solid(x / renderer->get_tile_width(), y / renderer->get_tile_height());
  97. }
  98. unsigned MAP::get_width() const
  99. {
  100. return this->width;
  101. }
  102. unsigned MAP::get_height() const
  103. {
  104. return this->width;
  105. }