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.h 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #pragma once
  2. #include <string.h>
  3. #include <stdint.h>
  4. #include <memory>
  5. #include <cygnet/Renderer.h>
  6. #include "util.h"
  7. #include "common.h"
  8. #include "Tile.h"
  9. namespace Swan {
  10. class World;
  11. class Game;
  12. class Chunk {
  13. public:
  14. using RelPos = TilePos;
  15. static constexpr size_t DATA_SIZE =
  16. CHUNK_WIDTH * CHUNK_HEIGHT * sizeof(Tile::ID) + // Tiles
  17. CHUNK_WIDTH * CHUNK_HEIGHT; // Light levels
  18. // What does this chunk want the world gen to do after a tick?
  19. enum class TickAction {
  20. DEACTIVATE,
  21. DELETE,
  22. NOTHING,
  23. };
  24. Chunk(ChunkPos pos): pos_(pos) {
  25. data_.reset(new uint8_t[DATA_SIZE]);
  26. memset(getLightData(), 0, CHUNK_WIDTH * CHUNK_HEIGHT);
  27. }
  28. Tile::ID *getTileData() {
  29. assert(isActive());
  30. return (Tile::ID *)data_.get();
  31. }
  32. uint8_t *getLightData() {
  33. assert(isActive());
  34. return data_.get() + CHUNK_WIDTH * CHUNK_HEIGHT * sizeof(Tile::ID);
  35. }
  36. Tile::ID getTileID(RelPos pos) {
  37. return getTileData()[pos.y * CHUNK_WIDTH + pos.x];
  38. }
  39. void setTileID(RelPos pos, Tile::ID id) {
  40. getTileData()[pos.y * CHUNK_WIDTH + pos.x] = id;
  41. changeList_.emplace_back(pos, id);
  42. isModified_ = true;
  43. }
  44. void setTileData(RelPos pos, Tile::ID id) {
  45. getTileData()[pos.y * CHUNK_WIDTH + pos.x] = id;
  46. }
  47. uint8_t getLightLevel(RelPos pos) {
  48. return getLightData()[pos.y * CHUNK_WIDTH + pos.x];
  49. }
  50. void setLightData(const uint8_t *data) {
  51. memcpy(getLightData(), data, CHUNK_WIDTH * CHUNK_HEIGHT);
  52. needLightRender_ = true;
  53. }
  54. void generateDone();
  55. void keepActive();
  56. void decompress();
  57. void compress(Cygnet::Renderer &rnd);
  58. void destroy(Cygnet::Renderer &rnd) { rnd.destroyChunk(renderChunk_); }
  59. void draw(const Context &ctx, Cygnet::Renderer &rnd);
  60. TickAction tick(float dt);
  61. bool isActive() { return deactivateTimer_ > 0; }
  62. ChunkPos pos_;
  63. private:
  64. static constexpr float DEACTIVATE_INTERVAL = 20;
  65. bool isCompressed() { return compressedSize_ != -1; }
  66. std::unique_ptr<uint8_t[]> data_;
  67. std::vector<std::pair<RelPos, Tile::ID>> changeList_;
  68. ssize_t compressedSize_ = -1; // -1 if not compressed, a positive number if compressed
  69. Cygnet::RenderChunk renderChunk_;
  70. Cygnet::RenderChunkShadow renderChunkShadow_;
  71. bool needChunkRender_ = true;
  72. bool needLightRender_ = false;
  73. float deactivateTimer_ = DEACTIVATE_INTERVAL;
  74. bool isModified_ = false;
  75. };
  76. }