A 2D tile-based sandbox game.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. #include "Chunk.h"
  2. #include <zlib.h>
  3. #include <stdint.h>
  4. #include <assert.h>
  5. #include "log.h"
  6. #include "Clock.h"
  7. #include "gfxutil.h"
  8. #include "World.h"
  9. #include "Game.h"
  10. #include "Win.h"
  11. namespace Swan {
  12. void Chunk::compress() {
  13. if (isCompressed())
  14. return;
  15. // We only need a fixed-length temp buffer;
  16. // if the compressed data gets too big, there's no point in compressing
  17. uint8_t dest[CHUNK_WIDTH * CHUNK_HEIGHT * sizeof(Tile::ID)];
  18. uLongf destlen = sizeof(dest);
  19. int ret = compress2(
  20. (Bytef *)dest, &destlen,
  21. (Bytef *)data_.get(), DATA_SIZE,
  22. Z_BEST_COMPRESSION);
  23. if (ret == Z_OK) {
  24. data_.reset(new uint8_t[destlen]);
  25. memcpy(data_.get(), dest, destlen);
  26. texture_.reset();
  27. compressed_size_ = destlen;
  28. info
  29. << "Compressed chunk " << pos_ << " from "
  30. << DATA_SIZE << " bytes "
  31. << "to " << destlen << " bytes";
  32. } else if (ret == Z_BUF_ERROR) {
  33. info
  34. << "Didn't compress chunk " << pos_ << " "
  35. << "because compressing it would've made it bigger";
  36. } else {
  37. warn << "Chunk compression error: " << ret << " (Out of memory?)";
  38. }
  39. }
  40. void Chunk::decompress() {
  41. if (!isCompressed())
  42. return;
  43. auto dest = std::make_unique<uint8_t[]>(DATA_SIZE);
  44. uLongf destlen = DATA_SIZE;
  45. int ret = uncompress(
  46. dest.get(), &destlen,
  47. (Bytef *)data_.get(), compressed_size_);
  48. if (ret != Z_OK) {
  49. panic << "Decompressing chunk failed: " << ret;
  50. abort();
  51. }
  52. data_ = std::move(dest);
  53. need_render_ = true;
  54. info
  55. << "Decompressed chunk " << pos_ << " from "
  56. << compressed_size_ << " bytes to "
  57. << DATA_SIZE << " bytes.";
  58. compressed_size_ = -1;
  59. }
  60. void Chunk::renderLight(const Context &ctx, SDL_Renderer *rnd) {
  61. std::optional<RenderTarget> target;
  62. // The texture might not be created yet
  63. if (!light_texture_) {
  64. light_texture_.reset(SDL_CreateTexture(
  65. ctx.game.win_.renderer_, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_TARGET,
  66. CHUNK_WIDTH, CHUNK_HEIGHT));
  67. SDL_SetTextureBlendMode(light_texture_.get(), SDL_BLENDMODE_BLEND);
  68. target.emplace(rnd, texture_.get());
  69. } else {
  70. target.emplace(rnd, texture_.get());
  71. }
  72. // Fill light texture
  73. target.emplace(rnd, light_texture_.get());
  74. RenderBlendMode mode(rnd, SDL_BLENDMODE_NONE);
  75. RenderDrawColor color(rnd, 0, 0, 0, 0);
  76. for (int y = 0; y < CHUNK_HEIGHT; ++y) {
  77. for (int x = 0; x < CHUNK_WIDTH; ++x) {
  78. int level = getLightLevel({ x, y });
  79. if (level >= 8)
  80. color.change(0, 0, 0, 0);
  81. else
  82. color.change(0, 0, 0, 255 - level * 32);
  83. SDL_Rect rect{ x, y, 1, 1 };
  84. SDL_RenderFillRect(rnd, &rect);
  85. }
  86. }
  87. need_light_render_ = false;
  88. }
  89. void Chunk::render(const Context &ctx, SDL_Renderer *rnd) {
  90. std::optional<RenderTarget> target;
  91. // The texture might not be created yet
  92. if (!texture_) {
  93. texture_.reset(SDL_CreateTexture(
  94. ctx.game.win_.renderer_, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_TARGET,
  95. CHUNK_WIDTH * TILE_SIZE, CHUNK_HEIGHT * TILE_SIZE));
  96. SDL_SetTextureBlendMode(texture_.get(), SDL_BLENDMODE_BLEND);
  97. target.emplace(rnd, texture_.get());
  98. RenderBlendMode mode(rnd, SDL_BLENDMODE_NONE);
  99. RenderDrawColor color(rnd, 0, 0, 0, 0);
  100. SDL_Rect rect{ 0, 0, CHUNK_WIDTH * TILE_SIZE, CHUNK_HEIGHT * TILE_SIZE };
  101. SDL_RenderFillRect(rnd, &rect);
  102. } else {
  103. target.emplace(rnd, texture_.get());
  104. }
  105. // We're caching tiles so we don't have to world.getTileByID() every time
  106. Tile::ID prevID = Tile::INVALID_ID;
  107. Tile *tile = ctx.game.invalid_tile_.get();
  108. // Fill tile texture
  109. for (int y = 0; y < CHUNK_HEIGHT; ++y) {
  110. for (int x = 0; x < CHUNK_WIDTH; ++x) {
  111. Tile::ID id = getTileID(RelPos(x, y));
  112. if (id != prevID) {
  113. prevID = id;
  114. tile = &ctx.world.getTileByID(id);
  115. }
  116. SDL_Rect dest{x * TILE_SIZE, y * TILE_SIZE, TILE_SIZE, TILE_SIZE};
  117. SDL_RenderCopy(rnd, tile->image_.texture_.get(), nullptr, &dest);
  118. }
  119. }
  120. need_render_ = false;
  121. renderLight(ctx, rnd);
  122. }
  123. void Chunk::renderList(SDL_Renderer *rnd) {
  124. // Here, we know that the texture is created.
  125. // We still wanna render directly to the target texture
  126. RenderTarget target(rnd, texture_.get());
  127. // We must make sure the blend mode is NONE, because we want transparent
  128. // pixels to actually overwrite non-transparent pixels
  129. RenderBlendMode mode(rnd, SDL_BLENDMODE_NONE);
  130. // When we FillRect, we must fill transparency.
  131. RenderDrawColor color(rnd, 0, 0, 0, 0);
  132. for (auto &[pos, tex]: draw_list_) {
  133. SDL_Rect dest{pos.x * TILE_SIZE, pos.y * TILE_SIZE, TILE_SIZE, TILE_SIZE};
  134. SDL_RenderFillRect(rnd, &dest);
  135. SDL_RenderCopy(rnd, tex, nullptr, &dest);
  136. }
  137. }
  138. void Chunk::draw(const Context &ctx, Win &win) {
  139. if (isCompressed())
  140. return;
  141. // The world plane is responsible for managing initial renders
  142. if (need_render_)
  143. return;
  144. // We're responsible for the light level rendering though
  145. if (need_light_render_)
  146. renderLight(ctx, win.renderer_);
  147. if (draw_list_.size() > 0) {
  148. renderList(win.renderer_);
  149. draw_list_.clear();
  150. }
  151. auto chunkpos = pos_ * Vec2i(CHUNK_WIDTH, CHUNK_HEIGHT);
  152. SDL_Rect rect{ 0, 0, CHUNK_WIDTH * TILE_SIZE, CHUNK_HEIGHT * TILE_SIZE };
  153. win.showTexture(chunkpos, texture_.get(), &rect);
  154. SDL_Rect texrect{ 0, 0, CHUNK_WIDTH, CHUNK_HEIGHT };
  155. win.showTexture(chunkpos, light_texture_.get(), &texrect, &rect);
  156. }
  157. Chunk::TickAction Chunk::tick(float dt) {
  158. assert(isActive());
  159. deactivate_timer_ -= dt;
  160. if (deactivate_timer_ <= 0) {
  161. if (is_modified_)
  162. return TickAction::DEACTIVATE;
  163. else
  164. return TickAction::DELETE;
  165. }
  166. return TickAction::NOTHING;
  167. }
  168. void Chunk::keepActive() {
  169. deactivate_timer_ = DEACTIVATE_INTERVAL;
  170. decompress();
  171. }
  172. }