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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 "World.h"
  9. #include "Game.h"
  10. namespace Swan {
  11. void Chunk::compress(Cygnet::Renderer &rnd) {
  12. if (isCompressed())
  13. return;
  14. // We only need a fixed-length temp buffer;
  15. // if the compressed data gets too big, there's no point in compressing
  16. uint8_t dest[CHUNK_WIDTH * CHUNK_HEIGHT * sizeof(Tile::ID)];
  17. uLongf destlen = sizeof(dest);
  18. int ret = compress2(
  19. (Bytef *)dest, &destlen,
  20. (Bytef *)data_.get(), DATA_SIZE,
  21. Z_BEST_COMPRESSION);
  22. if (ret == Z_OK) {
  23. data_.reset(new uint8_t[destlen]);
  24. memcpy(data_.get(), dest, destlen);
  25. compressedSize_ = destlen;
  26. info
  27. << "Compressed chunk " << pos_ << " from "
  28. << DATA_SIZE << " bytes "
  29. << "to " << destlen << " bytes";
  30. } else if (ret == Z_BUF_ERROR) {
  31. info
  32. << "Didn't compress chunk " << pos_ << " "
  33. << "because compressing it would've made it bigger";
  34. } else {
  35. warn << "Chunk compression error: " << ret << " (Out of memory?)";
  36. }
  37. rnd.destroyChunk(renderChunk_);
  38. rnd.destroyChunkShadow(renderChunkShadow_);
  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(), compressedSize_);
  48. if (ret != Z_OK) {
  49. panic << "Decompressing chunk failed: " << ret;
  50. abort();
  51. }
  52. data_ = std::move(dest);
  53. info
  54. << "Decompressed chunk " << pos_ << " from "
  55. << compressedSize_ << " bytes to "
  56. << DATA_SIZE << " bytes.";
  57. compressedSize_ = -1;
  58. needChunkRender_ = true;
  59. }
  60. void Chunk::draw(const Context &ctx, Cygnet::Renderer &rnd) {
  61. if (isCompressed())
  62. return;
  63. if (needChunkRender_) {
  64. renderChunk_ = rnd.createChunk(getTileData());
  65. renderChunkShadow_ = rnd.createChunkShadow(getLightData());
  66. needChunkRender_ = false;
  67. needLightRender_ = false;
  68. } else {
  69. for (auto &change: changeList_) {
  70. rnd.modifyChunk(renderChunk_, change.first, change.second);
  71. }
  72. }
  73. if (needLightRender_) {
  74. rnd.modifyChunkShadow(renderChunkShadow_, getLightData());
  75. needLightRender_ = false;
  76. }
  77. Vec2 pos = (Vec2)pos_ * Vec2{CHUNK_WIDTH, CHUNK_HEIGHT};
  78. rnd.drawChunk(renderChunk_, pos);
  79. rnd.drawChunkShadow(renderChunkShadow_, pos);
  80. }
  81. Chunk::TickAction Chunk::tick(float dt) {
  82. assert(isActive());
  83. deactivateTimer_ -= dt;
  84. if (deactivateTimer_ <= 0) {
  85. if (isModified_)
  86. return TickAction::DEACTIVATE;
  87. else
  88. return TickAction::DELETE;
  89. }
  90. return TickAction::NOTHING;
  91. }
  92. void Chunk::keepActive() {
  93. deactivateTimer_ = DEACTIVATE_INTERVAL;
  94. decompress();
  95. }
  96. }