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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. namespace Swan {
  12. void Chunk::compress(Cygnet::Renderer &rnd) {
  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(), DATA_SIZE,
  22. Z_BEST_COMPRESSION);
  23. if (ret == Z_OK) {
  24. data_.reset(new uint8_t[destlen]);
  25. memcpy(data_.get(), dest, destlen);
  26. compressedSize_ = destlen;
  27. info
  28. << "Compressed chunk " << pos_ << " from "
  29. << DATA_SIZE << " bytes "
  30. << "to " << destlen << " bytes";
  31. } else if (ret == Z_BUF_ERROR) {
  32. info
  33. << "Didn't compress chunk " << pos_ << " "
  34. << "because compressing it would've made it bigger";
  35. } else {
  36. warn << "Chunk compression error: " << ret << " (Out of memory?)";
  37. }
  38. rnd.destroyChunk(renderChunk_);
  39. rnd.destroyChunkShadow(renderChunkShadow_);
  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. info
  55. << "Decompressed chunk " << pos_ << " from "
  56. << compressedSize_ << " bytes to "
  57. << DATA_SIZE << " bytes.";
  58. compressedSize_ = -1;
  59. needChunkRender_ = true;
  60. }
  61. void Chunk::draw(const Context &ctx, Cygnet::Renderer &rnd) {
  62. if (isCompressed())
  63. return;
  64. if (needChunkRender_) {
  65. renderChunk_ = rnd.createChunk(getTileData());
  66. renderChunkShadow_ = rnd.createChunkShadow(getLightData());
  67. needChunkRender_ = false;
  68. needLightRender_ = false;
  69. } else {
  70. for (auto &change: changeList_) {
  71. rnd.modifyChunk(renderChunk_, change.first, change.second);
  72. }
  73. }
  74. if (needLightRender_) {
  75. rnd.modifyChunkShadow(renderChunkShadow_, getLightData());
  76. needLightRender_ = false;
  77. }
  78. Vec2 pos = (Vec2)pos_ * Vec2{CHUNK_WIDTH, CHUNK_HEIGHT};
  79. rnd.drawChunk(renderChunk_, pos);
  80. rnd.drawChunkShadow(renderChunkShadow_, pos);
  81. }
  82. Chunk::TickAction Chunk::tick(float dt) {
  83. assert(isActive());
  84. deactivateTimer_ -= dt;
  85. if (deactivateTimer_ <= 0) {
  86. if (isModified_)
  87. return TickAction::DEACTIVATE;
  88. else
  89. return TickAction::DELETE;
  90. }
  91. return TickAction::NOTHING;
  92. }
  93. void Chunk::keepActive() {
  94. deactivateTimer_ = DEACTIVATE_INTERVAL;
  95. decompress();
  96. }
  97. }