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 <algorithm>
  6. #include "log.h"
  7. #include "Clock.h"
  8. #include "gfxutil.h"
  9. #include "World.h"
  10. #include "Game.h"
  11. #include "Win.h"
  12. namespace Swan {
  13. void Chunk::compress() {
  14. if (isCompressed())
  15. return;
  16. // We only need a fixed-length temp buffer;
  17. // if the compressed data gets too big, there's no point in compressing
  18. uint8_t dest[CHUNK_WIDTH * CHUNK_HEIGHT * sizeof(Tile::ID)];
  19. uLongf destlen = sizeof(dest);
  20. int ret = compress2(
  21. (Bytef *)dest, &destlen,
  22. (Bytef *)data_.get(), DATA_SIZE,
  23. Z_BEST_COMPRESSION);
  24. if (ret == Z_OK) {
  25. data_.reset(new uint8_t[destlen]);
  26. memcpy(data_.get(), dest, destlen);
  27. texture_.reset();
  28. compressed_size_ = destlen;
  29. info
  30. << "Compressed chunk " << pos_ << " from "
  31. << DATA_SIZE << " bytes "
  32. << "to " << destlen << " bytes";
  33. } else if (ret == Z_BUF_ERROR) {
  34. info
  35. << "Didn't compress chunk " << pos_ << " "
  36. << "because compressing it would've made it bigger";
  37. } else {
  38. warn << "Chunk compression error: " << ret << " (Out of memory?)";
  39. }
  40. }
  41. void Chunk::decompress() {
  42. if (!isCompressed())
  43. return;
  44. auto dest = std::make_unique<uint8_t[]>(DATA_SIZE);
  45. uLongf destlen = DATA_SIZE;
  46. int ret = uncompress(
  47. dest.get(), &destlen,
  48. (Bytef *)data_.get(), compressed_size_);
  49. if (ret != Z_OK) {
  50. panic << "Decompressing chunk failed: " << ret;
  51. abort();
  52. }
  53. data_ = std::move(dest);
  54. need_render_ = true;
  55. info
  56. << "Decompressed chunk " << pos_ << " from "
  57. << compressed_size_ << " bytes to "
  58. << DATA_SIZE << " bytes.";
  59. compressed_size_ = -1;
  60. }
  61. void Chunk::renderLight(const Context &ctx, SDL_Renderer *rnd) {
  62. std::optional<RenderTarget> target;
  63. // The texture might not be created yet
  64. if (!light_texture_) {
  65. light_texture_.reset(SDL_CreateTexture(
  66. ctx.game.win_.renderer_, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_TARGET,
  67. CHUNK_WIDTH, CHUNK_HEIGHT));
  68. SDL_SetTextureBlendMode(light_texture_.get(), SDL_BLENDMODE_BLEND);
  69. target.emplace(rnd, texture_.get());
  70. } else {
  71. target.emplace(rnd, texture_.get());
  72. }
  73. // Fill light texture
  74. target.emplace(rnd, light_texture_.get());
  75. RenderBlendMode mode(rnd, SDL_BLENDMODE_NONE);
  76. RenderDrawColor color(rnd, 0, 0, 0, 0);
  77. for (int y = 0; y < CHUNK_HEIGHT; ++y) {
  78. for (int x = 0; x < CHUNK_WIDTH; ++x) {
  79. float level = (float)getLightLevel({ x, y }) / 255;
  80. float l = 1.055 * pow(level, 1/2.4) - 0.055;
  81. int b = std::clamp((int)(l * 1.5 * 255), 0, 255);
  82. color.change(0, 0, 0, 255 - b);
  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. }