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

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