Map.cpp 3.2 KB

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