A 2D tile-based sandbox game.
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

Chunk.cc 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. Tile::ID prevID = Tile::INVALID_ID;
  85. Tile *tile = ctx.game.invalid_tile_.get();
  86. // Get info about or texture for later
  87. uint32_t format;
  88. int access, texw, texh;
  89. if (SDL_QueryTexture(visuals_->texture_.get(), &format, &access, &texw, &texh) < 0) {
  90. panic << "Failed to query texture: " << SDL_GetError();
  91. abort();
  92. }
  93. // ...Now convert the format to an actually useful mask
  94. int bpp = 32;
  95. uint32_t rmask, gmask, bmask, amask;
  96. if (SDL_PixelFormatEnumToMasks(format, &bpp, &rmask, &gmask, &bmask, &amask) != SDL_TRUE) {
  97. panic << "Failed to get pixel mask: " << SDL_GetError();
  98. abort();
  99. }
  100. // We lock the txture to get write access to its raw pixels
  101. SDL_Rect rect{ 0, 0, CHUNK_WIDTH * TILE_SIZE, CHUNK_HEIGHT * TILE_SIZE };
  102. uint8_t *pixels;
  103. int pitch;
  104. if (SDL_LockTexture(visuals_->texture_.get(), &rect, (void **)&pixels, &pitch) < 0) {
  105. panic << "Failed to lock texture: " << SDL_GetError();
  106. abort();
  107. }
  108. auto lock = makeDeferred([this] { SDL_UnlockTexture(visuals_->texture_.get()); });
  109. // This surface will refer to the pixels of our texture which we want to update.
  110. auto destsurf = makeRaiiPtr(
  111. SDL_CreateRGBSurfaceFrom(
  112. pixels, TILE_SIZE, TILE_SIZE, 32, pitch,
  113. rmask, gmask, bmask, amask),
  114. SDL_FreeSurface);
  115. for (int y = 0; y < CHUNK_HEIGHT; ++y) {
  116. for (int x = 0; x < CHUNK_WIDTH; ++x) {
  117. Tile::ID id = getTileID(RelPos(x, y));
  118. if (id != prevID) {
  119. prevID = id;
  120. tile = &ctx.world.getTileByID(id);
  121. }
  122. auto &srcsurf = tile->image_.surface_;
  123. SDL_Rect srcrect{ 0, 0, srcsurf->w, srcsurf->h };
  124. //SDL_Rect destrect{ x * TILE_SIZE, y * TILE_SIZE, TILE_SIZE, TILE_SIZE };
  125. destsurf->pixels = pixels + (y * TILE_SIZE * pitch) + x * TILE_SIZE * 4;
  126. SDL_Rect destrect{ 0, 0, TILE_SIZE, TILE_SIZE };
  127. if (SDL_BlitSurface(srcsurf.get(), &srcrect, destsurf.get(), &destrect) < 0) {
  128. warn << "Failed to blit surface: " << SDL_GetError();
  129. }
  130. }
  131. }
  132. }
  133. void Chunk::draw(const Context &ctx, Win &win) {
  134. if (isCompressed())
  135. return;
  136. if (need_render_) {
  137. render(ctx);
  138. need_render_ = false;
  139. }
  140. if (visuals_->dirty_) {
  141. need_render_ = true;
  142. visuals_->dirty_ = false;
  143. }
  144. SDL_Rect rect{ 0, 0, CHUNK_WIDTH * TILE_SIZE, CHUNK_HEIGHT * TILE_SIZE };
  145. win.showTexture(
  146. pos_ * Vec2i(CHUNK_WIDTH, CHUNK_HEIGHT),
  147. visuals_->texture_.get(), &rect);
  148. }
  149. void Chunk::tick(float dt) {
  150. if (deactivate_timer_ <= 0)
  151. return;
  152. deactivate_timer_ -= dt;
  153. if (deactivate_timer_ <= 0) {
  154. compress();
  155. }
  156. }
  157. bool Chunk::keepActive() {
  158. bool wasActive = isActive();
  159. deactivate_timer_ = DEACTIVATE_INTERVAL;
  160. if (wasActive)
  161. return false;
  162. decompress();
  163. return true;
  164. }
  165. }