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

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