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.

Renderer.h 1.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #pragma once
  2. #include <memory>
  3. #include <vector>
  4. #include <stdint.h>
  5. #include <swan-common/constants.h>
  6. #include <swan-common/Vector2.h>
  7. #include "util.h"
  8. namespace Cygnet {
  9. struct RendererState;
  10. struct RenderChunk {
  11. GLuint tex;
  12. };
  13. struct RenderSprite {
  14. GLuint tex;
  15. SwanCommon::Vec2 scale;
  16. int frameCount;
  17. };
  18. struct RenderTile {
  19. uint16_t id;
  20. };
  21. struct RenderCamera {
  22. SwanCommon::Vec2 pos = {0, 0};
  23. SwanCommon::Vec2i size = {1, 1};
  24. float zoom = 1;
  25. };
  26. class Renderer {
  27. public:
  28. using TileID = uint16_t;
  29. Renderer();
  30. ~Renderer();
  31. void drawChunk(RenderChunk chunk, SwanCommon::Vec2 pos);
  32. void drawSprite(RenderSprite sprite, Mat3gf mat, int y = 0);
  33. void draw(const RenderCamera &cam);
  34. void uploadTileAtlas(const void *data, int width, int height);
  35. void modifyTile(TileID id, const void *data);
  36. RenderChunk createChunk(
  37. TileID tiles[SwanCommon::CHUNK_WIDTH * SwanCommon::CHUNK_HEIGHT]);
  38. void modifyChunk(RenderChunk chunk, SwanCommon::Vec2i pos, TileID id);
  39. void destroyChunk(RenderChunk chunk);
  40. RenderSprite createSprite(void *data, int width, int height, int fh);
  41. RenderSprite createSprite(void *data, int width, int height);
  42. void destroySprite(RenderSprite sprite);
  43. SwanCommon::Vec2 winScale() { return winScale_; }
  44. private:
  45. struct DrawChunk {
  46. SwanCommon::Vec2 pos;
  47. RenderChunk chunk;
  48. };
  49. struct DrawSprite {
  50. Mat3gf transform;
  51. int frame;
  52. RenderSprite sprite;
  53. };
  54. SwanCommon::Vec2 winScale_ = {1, 1};
  55. std::unique_ptr<RendererState> state_;
  56. std::vector<DrawChunk> drawChunks_;
  57. std::vector<DrawSprite> drawSprites_;
  58. };
  59. inline void Renderer::drawChunk(RenderChunk chunk, SwanCommon::Vec2 pos) {
  60. drawChunks_.push_back({pos, chunk});
  61. }
  62. inline void Renderer::drawSprite(RenderSprite sprite, Mat3gf mat, int frame) {
  63. drawSprites_.push_back({mat, frame, sprite});
  64. }
  65. }