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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. Chunk(ChunkPos pos): pos_(pos) {
  15. data_.reset(new uint8_t[CHUNK_WIDTH * CHUNK_HEIGHT * sizeof(Tile::ID)]);
  16. }
  17. Tile::ID *getTileData();
  18. Tile::ID getTileID(RelPos pos);
  19. void setTileID(RelPos pos, Tile::ID id, SDL_Texture *tex);
  20. void setTileData(RelPos pos, Tile::ID id);
  21. void render(const Context &ctx, SDL_Renderer *rnd);
  22. void compress();
  23. void decompress();
  24. void draw(const Context &ctx, Win &win);
  25. void tick(float dt);
  26. bool keepActive(); // Returns true if chunk was inactive
  27. bool isActive() { return deactivate_timer_ > 0; }
  28. ChunkPos pos_;
  29. private:
  30. static constexpr float DEACTIVATE_INTERVAL = 20;
  31. static uint8_t *renderbuf;
  32. void renderList(SDL_Renderer *rnd);
  33. bool isCompressed() { return compressed_size_ != -1; }
  34. std::unique_ptr<uint8_t[]> data_;
  35. std::vector<std::pair<RelPos, SDL_Texture *>> draw_list_;
  36. ssize_t compressed_size_ = -1; // -1 if not compressed, a positive number if compressed
  37. bool need_render_ = false;
  38. float deactivate_timer_ = DEACTIVATE_INTERVAL;
  39. CPtr<SDL_Texture, SDL_DestroyTexture> texture_;
  40. };
  41. }