A 2D tile-based sandbox game.
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

Chunk.h 1.3KB

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