A 2D tile-based sandbox game.
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

Chunk.cc 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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(), CHUNK_WIDTH * CHUNK_HEIGHT * sizeof(Tile::ID),
  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. << CHUNK_WIDTH * CHUNK_HEIGHT * sizeof(Tile::ID) << " 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[]>(CHUNK_WIDTH * CHUNK_HEIGHT * sizeof(Tile::ID));
  44. uLongf destlen = CHUNK_WIDTH * CHUNK_HEIGHT * sizeof(Tile::ID);
  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. << CHUNK_WIDTH * CHUNK_HEIGHT * sizeof(Tile::ID) << " bytes.";
  58. compressed_size_ = -1;
  59. }
  60. void Chunk::render(const Context &ctx, SDL_Renderer *rnd) {
  61. std::optional<RenderTarget> target;
  62. // The texture might not be created yet
  63. if (!texture_) {
  64. texture_.reset(SDL_CreateTexture(
  65. ctx.game.win_.renderer_, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_TARGET,
  66. CHUNK_WIDTH * TILE_SIZE, CHUNK_HEIGHT * TILE_SIZE));
  67. SDL_SetTextureBlendMode(texture_.get(), SDL_BLENDMODE_BLEND);
  68. target.emplace(rnd, texture_.get());
  69. RenderBlendMode mode(rnd, SDL_BLENDMODE_NONE);
  70. RenderDrawColor color(rnd, 0, 0, 0, 0);
  71. SDL_Rect rect{ 0, 0, CHUNK_WIDTH * TILE_SIZE, CHUNK_HEIGHT * TILE_SIZE };
  72. SDL_RenderFillRect(rnd, &rect);
  73. } else {
  74. target.emplace(rnd, texture_.get());
  75. }
  76. // We're caching tiles so we don't have to world.getTileByID() every time
  77. Tile::ID prevID = Tile::INVALID_ID;
  78. Tile *tile = ctx.game.invalid_tile_.get();
  79. for (int y = 0; y < CHUNK_HEIGHT; ++y) {
  80. for (int x = 0; x < CHUNK_WIDTH; ++x) {
  81. Tile::ID id = getTileID(RelPos(x, y));
  82. if (id != prevID) {
  83. prevID = id;
  84. tile = &ctx.world.getTileByID(id);
  85. }
  86. SDL_Rect dest{x * TILE_SIZE, y * TILE_SIZE, TILE_SIZE, TILE_SIZE};
  87. SDL_RenderCopy(rnd, tile->image_.texture_.get(), nullptr, &dest);
  88. }
  89. }
  90. need_render_ = false;
  91. }
  92. void Chunk::renderList(SDL_Renderer *rnd) {
  93. // Here, we know that the texture is created.
  94. // We still wanna render directly to the target texture
  95. RenderTarget target(rnd, texture_.get());
  96. // We must make sure the blend mode is NONE, because we want transparent
  97. // pixels to actually overwrite non-transparent pixels
  98. RenderBlendMode mode(rnd, SDL_BLENDMODE_NONE);
  99. // When we FillRect, we must fill transparency.
  100. RenderDrawColor color(rnd, 0, 0, 0, 0);
  101. for (auto &[pos, tex]: draw_list_) {
  102. SDL_Rect dest{pos.x * TILE_SIZE, pos.y * TILE_SIZE, TILE_SIZE, TILE_SIZE};
  103. SDL_RenderFillRect(rnd, &dest);
  104. SDL_RenderCopy(rnd, tex, nullptr, &dest);
  105. }
  106. }
  107. void Chunk::draw(const Context &ctx, Win &win) {
  108. if (isCompressed())
  109. return;
  110. // The world plane is responsible for managing initial renders
  111. if (need_render_)
  112. return;
  113. if (draw_list_.size() > 0) {
  114. renderList(win.renderer_);
  115. draw_list_.clear();
  116. }
  117. SDL_Rect rect{ 0, 0, CHUNK_WIDTH * TILE_SIZE, CHUNK_HEIGHT * TILE_SIZE };
  118. win.showTexture(
  119. pos_ * Vec2i(CHUNK_WIDTH, CHUNK_HEIGHT),
  120. texture_.get(), &rect);
  121. }
  122. Chunk::TickAction Chunk::tick(float dt) {
  123. assert(isActive());
  124. deactivate_timer_ -= dt;
  125. if (deactivate_timer_ <= 0) {
  126. if (is_modified_)
  127. return TickAction::DEACTIVATE;
  128. else
  129. return TickAction::DELETE;
  130. }
  131. return TickAction::NOTHING;
  132. }
  133. void Chunk::keepActive() {
  134. deactivate_timer_ = DEACTIVATE_INTERVAL;
  135. decompress();
  136. }
  137. }