Map.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #include "Map.h"
  2. #include "Camera.h"
  3. #include "Graphics.h"
  4. #include "sprites.h"
  5. #include "misc.h"
  6. #define MAP WalrusRPG::Map
  7. MAP::Map(int width, int height, unsigned *layer0, unsigned *layer1)
  8. {
  9. this->width = width;
  10. this->height = height;
  11. this->layer0 = layer0;
  12. this->layer1 = layer1;
  13. }
  14. MAP::~Map()
  15. {
  16. // TODO if you allocate dynamically members
  17. }
  18. void MAP::update(unsigned dt)
  19. {
  20. UNUSED(dt);
  21. // TODO update map's data according to elasped time
  22. }
  23. void MAP::render(WalrusRPG::Camera &camera, unsigned dt) const
  24. {
  25. UNUSED(dt);
  26. // By Eiyeron : I assumed that the camera's position is the top left pixel.
  27. // Margins moves the rendered map if we go outside of the bounds (specially on the left or on the top).
  28. signed offset_x = camera.get_x() % 24 * -1;
  29. signed offset_y = camera.get_y() % 24 * -1;
  30. signed start_x = camera.get_x() / 24;
  31. signed start_y = camera.get_y() / 24;
  32. signed end_x = start_x + 15;
  33. signed end_y = start_y + 16;
  34. // Bound-checking code. To avoid reading outside of the map.
  35. // The offset edition allows the map to be correctly moved to the right to make up for the lack of renderable tiles.
  36. if (start_x < 0)
  37. {
  38. offset_x -= start_x * 24;
  39. start_x = 0;
  40. }
  41. if (start_y < 0)
  42. {
  43. offset_y -= start_y * 24;
  44. start_y = 0;
  45. }
  46. // warning fix. Even if end_x/y is negative, it should be higher than width/height.
  47. if ((unsigned) end_x > this->width)
  48. end_x = this->width;
  49. if ((unsigned) end_y > this->height)
  50. end_y = this->height;
  51. // pre-calculating variables to speed up loop condition check
  52. signed delta_x = end_x - start_x;
  53. signed delta_y = end_y - start_y;
  54. // Creating a region clip. Why does it smell like SDL?
  55. WalrusRPG::Graphics::Rect_t sprite;
  56. sprite.y = 0;
  57. sprite.w = 24;
  58. sprite.h = 24;
  59. // rendering part.
  60. for (signed j = 0; j < delta_y; j++)
  61. {
  62. for (signed i = 0; i < delta_x; i++)
  63. {
  64. unsigned index = (start_x + i) + (start_y + j) * this->width;
  65. sprite.x = this->layer0[index] * 24;
  66. draw_sprite_sheet(tiles, offset_x + i * 24, offset_y + j * 24, &sprite);
  67. unsigned tile_over = this->layer1[index];
  68. // layer1 : Over-layer
  69. if (tile_over != 0)
  70. {
  71. sprite.x = tile_over * 24;
  72. draw_sprite_sheet(tiles, offset_x + i * 24, offset_y + j * 24, &sprite);
  73. }
  74. }
  75. }
  76. }
  77. bool MAP::is_tile_solid(unsigned x, unsigned y) const
  78. {
  79. if (x >= width || y >= height)
  80. return true;
  81. return this->layer0[y * width + x] != 0;
  82. }
  83. bool MAP::is_pixel_solid(unsigned x, unsigned y) const
  84. {
  85. return is_tile_solid(x / 24, y / 24);
  86. }
  87. unsigned MAP::get_width() const
  88. {
  89. return this->width;
  90. }
  91. unsigned MAP::get_height() const
  92. {
  93. return this->width;
  94. }