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

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