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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. // What does this chunk want the world gen to do after a tick?
  15. enum class TickAction {
  16. DEACTIVATE,
  17. DELETE,
  18. NOTHING,
  19. };
  20. Chunk(ChunkPos pos): pos_(pos) {
  21. data_.reset(new uint8_t[CHUNK_WIDTH * CHUNK_HEIGHT * sizeof(Tile::ID)]);
  22. }
  23. Tile::ID *getTileData();
  24. Tile::ID getTileID(RelPos pos);
  25. void setTileID(RelPos pos, Tile::ID id, SDL_Texture *tex);
  26. void setTileData(RelPos pos, Tile::ID id);
  27. void render(const Context &ctx, SDL_Renderer *rnd);
  28. void compress();
  29. void decompress();
  30. void draw(const Context &ctx, Win &win);
  31. TickAction tick(float dt);
  32. bool isActive() { return deactivate_timer_ > 0; }
  33. void keepActive();
  34. void markModified() { is_modified_ = true; }
  35. ChunkPos pos_;
  36. private:
  37. static constexpr float DEACTIVATE_INTERVAL = 20;
  38. static uint8_t *renderbuf;
  39. void renderList(SDL_Renderer *rnd);
  40. bool isCompressed() { return compressed_size_ != -1; }
  41. std::unique_ptr<uint8_t[]> data_;
  42. std::vector<std::pair<RelPos, SDL_Texture *>> draw_list_;
  43. ssize_t compressed_size_ = -1; // -1 if not compressed, a positive number if compressed
  44. bool need_render_ = false;
  45. float deactivate_timer_ = DEACTIVATE_INTERVAL;
  46. bool is_modified_ = false;
  47. CPtr<SDL_Texture, SDL_DestroyTexture> texture_;
  48. };
  49. }