A 2D tile-based sandbox game.
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. #include "Chunk.h"
  2. #include <zlib.h>
  3. #include <stdint.h>
  4. #include "log.h"
  5. #include "Clock.h"
  6. #include "gfxutil.h"
  7. #include "World.h"
  8. #include "Game.h"
  9. #include "Win.h"
  10. namespace Swan {
  11. uint8_t *Chunk::renderbuf = new uint8_t[CHUNK_WIDTH * TILE_SIZE * CHUNK_HEIGHT * TILE_SIZE * 4];
  12. Tile::ID *Chunk::getTileData() {
  13. keepActive();
  14. return (Tile::ID *)data_.get();
  15. }
  16. Tile::ID Chunk::getTileID(RelPos pos) {
  17. return getTileData()[pos.y * CHUNK_WIDTH + pos.x];
  18. }
  19. void Chunk::setTileID(RelPos pos, Tile::ID id) {
  20. getTileData()[pos.y * CHUNK_WIDTH + pos.x] = id;
  21. }
  22. void Chunk::drawBlock(RelPos pos, const Tile &t) {
  23. keepActive();
  24. //visuals_->tex_.update(*t.image_, pos.x * TILE_SIZE, pos.y * TILE_SIZE);
  25. visuals_->dirty_ = true;
  26. }
  27. void Chunk::compress() {
  28. if (isCompressed())
  29. return;
  30. // We only need a fixed-length temp buffer;
  31. // if the compressed data gets too big, there's no point in compressing
  32. uint8_t dest[CHUNK_WIDTH * CHUNK_HEIGHT * sizeof(Tile::ID)];
  33. uLongf destlen = sizeof(dest);
  34. int ret = compress2(
  35. (Bytef *)dest, &destlen,
  36. (Bytef *)data_.get(), CHUNK_WIDTH * CHUNK_HEIGHT * sizeof(Tile::ID),
  37. Z_BEST_COMPRESSION);
  38. if (ret == Z_OK) {
  39. data_.reset(new uint8_t[destlen]);
  40. memcpy(data_.get(), dest, destlen);
  41. visuals_.reset();
  42. compressed_size_ = destlen;
  43. info
  44. << "Compressed chunk " << pos_ << " from "
  45. << CHUNK_WIDTH * CHUNK_HEIGHT * sizeof(Tile::ID) << " bytes "
  46. << "to " << destlen << " bytes.";
  47. } else if (ret == Z_BUF_ERROR) {
  48. info
  49. << "Didn't compress chunk " << pos_ << " "
  50. << "because compressing it would've made it bigger.";
  51. } else {
  52. warn << "Chunk compression error: " << ret << " (Out of memory?)";
  53. }
  54. }
  55. void Chunk::decompress() {
  56. if (!isCompressed())
  57. return;
  58. auto dest = std::make_unique<uint8_t[]>(CHUNK_WIDTH * CHUNK_HEIGHT * sizeof(Tile::ID));
  59. uLongf destlen = CHUNK_WIDTH * CHUNK_HEIGHT * sizeof(Tile::ID);
  60. int ret = uncompress(
  61. dest.get(), &destlen,
  62. (Bytef *)data_.get(), compressed_size_);
  63. if (ret != Z_OK) {
  64. panic << "Decompressing chunk failed: " << ret;
  65. abort();
  66. }
  67. data_ = std::move(dest);
  68. visuals_.reset(new Visuals());
  69. visuals_->dirty_ = true;
  70. need_render_ = true;
  71. info
  72. << "Decompressed chunk " << pos_ << " from "
  73. << compressed_size_ << " bytes to "
  74. << CHUNK_WIDTH * CHUNK_HEIGHT * sizeof(Tile::ID) << " bytes.";
  75. compressed_size_ = -1;
  76. }
  77. void Chunk::render(const Context &ctx) {
  78. // The texture might not be created yet
  79. if (!visuals_->texture_) {
  80. visuals_->texture_.reset(SDL_CreateTexture(
  81. ctx.game.win_.renderer_, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING,
  82. CHUNK_WIDTH * TILE_SIZE, CHUNK_HEIGHT * TILE_SIZE));
  83. }
  84. // We're caching tiles so we don't have to world.getTileByID() every time
  85. Tile::ID prevID = Tile::INVALID_ID;
  86. Tile *tile = ctx.game.invalid_tile_.get();
  87. // Locking the texture lets us write to its piexls
  88. TexLock lock(visuals_->texture_.get());
  89. for (int y = 0; y < CHUNK_HEIGHT; ++y) {
  90. for (int x = 0; x < CHUNK_WIDTH; ++x) {
  91. Tile::ID id = getTileID(RelPos(x, y));
  92. if (id != prevID) {
  93. prevID = id;
  94. tile = &ctx.world.getTileByID(id);
  95. }
  96. // Find the source surface and rect...
  97. auto &srcsurf = tile->image_.surface_;
  98. SDL_Rect srcrect{ 0, 0, srcsurf->w, srcsurf->h };
  99. // ...and blit it to the appropriate place
  100. SDL_Rect destrect{ x * TILE_SIZE, y * TILE_SIZE, TILE_SIZE, TILE_SIZE };
  101. if (lock.blit(&destrect, srcsurf.get(), &srcrect) < 0)
  102. warn << "Failed to blit surface: " << SDL_GetError();
  103. }
  104. }
  105. }
  106. void Chunk::draw(const Context &ctx, Win &win) {
  107. if (isCompressed())
  108. return;
  109. if (need_render_) {
  110. render(ctx);
  111. need_render_ = false;
  112. }
  113. if (visuals_->dirty_) {
  114. need_render_ = true;
  115. visuals_->dirty_ = false;
  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. visuals_->texture_.get(), &rect);
  121. }
  122. void Chunk::tick(float dt) {
  123. if (deactivate_timer_ <= 0)
  124. return;
  125. deactivate_timer_ -= dt;
  126. if (deactivate_timer_ <= 0) {
  127. compress();
  128. }
  129. }
  130. bool Chunk::keepActive() {
  131. bool wasActive = isActive();
  132. deactivate_timer_ = DEACTIVATE_INTERVAL;
  133. if (wasActive)
  134. return false;
  135. decompress();
  136. return true;
  137. }
  138. }