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 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. compressedSize_ = 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(), compressedSize_);
  49. if (ret != Z_OK) {
  50. panic << "Decompressing chunk failed: " << ret;
  51. abort();
  52. }
  53. data_ = std::move(dest);
  54. needRender_ = true;
  55. info
  56. << "Decompressed chunk " << pos_ << " from "
  57. << compressedSize_ << " bytes to "
  58. << DATA_SIZE << " bytes.";
  59. compressedSize_ = -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 (!lightTexture_) {
  65. lightTexture_.reset(SDL_CreateTexture(
  66. ctx.game.win_.renderer_, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_TARGET,
  67. CHUNK_WIDTH, CHUNK_HEIGHT));
  68. SDL_SetTextureBlendMode(lightTexture_.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, lightTexture_.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. int b = getLightLevel({ x, y });
  80. color.change(0, 0, 0, 255 - b);
  81. SDL_Rect rect{ x, y, 1, 1 };
  82. SDL_RenderFillRect(rnd, &rect);
  83. }
  84. }
  85. needLightRender_ = false;
  86. }
  87. void Chunk::render(const Context &ctx, SDL_Renderer *rnd) {
  88. std::optional<RenderTarget> target;
  89. // The texture might not be created yet
  90. if (!texture_) {
  91. texture_.reset(SDL_CreateTexture(
  92. ctx.game.win_.renderer_, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_TARGET,
  93. CHUNK_WIDTH * TILE_SIZE, CHUNK_HEIGHT * TILE_SIZE));
  94. SDL_SetTextureBlendMode(texture_.get(), SDL_BLENDMODE_BLEND);
  95. target.emplace(rnd, texture_.get());
  96. RenderBlendMode mode(rnd, SDL_BLENDMODE_NONE);
  97. RenderDrawColor color(rnd, 0, 0, 0, 0);
  98. SDL_Rect rect{ 0, 0, CHUNK_WIDTH * TILE_SIZE, CHUNK_HEIGHT * TILE_SIZE };
  99. SDL_RenderFillRect(rnd, &rect);
  100. } else {
  101. target.emplace(rnd, texture_.get());
  102. }
  103. // We're caching tiles so we don't have to world.getTileByID() every time
  104. Tile::ID prevID = Tile::INVALID_ID;
  105. Tile *tile = ctx.game.invalidTile_.get();
  106. // Fill tile texture
  107. for (int y = 0; y < CHUNK_HEIGHT; ++y) {
  108. for (int x = 0; x < CHUNK_WIDTH; ++x) {
  109. Tile::ID id = getTileID(RelPos(x, y));
  110. if (id != prevID) {
  111. prevID = id;
  112. tile = &ctx.world.getTileByID(id);
  113. }
  114. SDL_Rect dest{x * TILE_SIZE, y * TILE_SIZE, TILE_SIZE, TILE_SIZE};
  115. SDL_RenderCopy(rnd, tile->image_.texture_.get(), nullptr, &dest);
  116. }
  117. }
  118. needRender_ = false;
  119. renderLight(ctx, rnd);
  120. }
  121. void Chunk::renderList(SDL_Renderer *rnd) {
  122. // Here, we know that the texture is created.
  123. // We still wanna render directly to the target texture
  124. RenderTarget target(rnd, texture_.get());
  125. // We must make sure the blend mode is NONE, because we want transparent
  126. // pixels to actually overwrite non-transparent pixels
  127. RenderBlendMode mode(rnd, SDL_BLENDMODE_NONE);
  128. // When we FillRect, we must fill transparency.
  129. RenderDrawColor color(rnd, 0, 0, 0, 0);
  130. for (auto &[pos, tex]: drawList_) {
  131. SDL_Rect dest{pos.x * TILE_SIZE, pos.y * TILE_SIZE, TILE_SIZE, TILE_SIZE};
  132. SDL_RenderFillRect(rnd, &dest);
  133. SDL_RenderCopy(rnd, tex, nullptr, &dest);
  134. }
  135. }
  136. void Chunk::draw(const Context &ctx, Win &win) {
  137. if (isCompressed())
  138. return;
  139. // The world plane is responsible for managing initial renders
  140. if (needRender_)
  141. return;
  142. // We're responsible for the light level rendering though
  143. if (needLightRender_)
  144. renderLight(ctx, win.renderer_);
  145. if (drawList_.size() > 0) {
  146. renderList(win.renderer_);
  147. drawList_.clear();
  148. }
  149. auto chunkpos = pos_ * Vec2i(CHUNK_WIDTH, CHUNK_HEIGHT);
  150. SDL_Rect rect{ 0, 0, CHUNK_WIDTH * TILE_SIZE, CHUNK_HEIGHT * TILE_SIZE };
  151. win.showTexture(chunkpos, texture_.get(), &rect);
  152. SDL_Rect texrect{ 0, 0, CHUNK_WIDTH, CHUNK_HEIGHT };
  153. win.showTexture(chunkpos, lightTexture_.get(), &texrect, &rect);
  154. }
  155. Chunk::TickAction Chunk::tick(float dt) {
  156. assert(isActive());
  157. deactivateTimer_ -= dt;
  158. if (deactivateTimer_ <= 0) {
  159. if (isModified_)
  160. return TickAction::DEACTIVATE;
  161. else
  162. return TickAction::DELETE;
  163. }
  164. return TickAction::NOTHING;
  165. }
  166. void Chunk::keepActive() {
  167. deactivateTimer_ = DEACTIVATE_INTERVAL;
  168. decompress();
  169. }
  170. }