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.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. }
  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((Tile::ID *)data_.get());
  65. needChunkRender_ = false;
  66. } else {
  67. for (auto &change: changeList_) {
  68. rnd.modifyChunk(renderChunk_, change.first, change.second);
  69. }
  70. }
  71. rnd.drawChunk(renderChunk_, (Vec2)pos_ * Vec2{CHUNK_WIDTH, CHUNK_HEIGHT});
  72. }
  73. Chunk::TickAction Chunk::tick(float dt) {
  74. assert(isActive());
  75. deactivateTimer_ -= dt;
  76. if (deactivateTimer_ <= 0) {
  77. if (isModified_)
  78. return TickAction::DEACTIVATE;
  79. else
  80. return TickAction::DELETE;
  81. }
  82. return TickAction::NOTHING;
  83. }
  84. void Chunk::keepActive() {
  85. deactivateTimer_ = DEACTIVATE_INTERVAL;
  86. decompress();
  87. }
  88. }