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.

Chunk.cc 4.9KB

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