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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 RenderCamera {
  19. SwanCommon::Vec2 pos = {0, 0};
  20. SwanCommon::Vec2i size = {1, 1};
  21. float zoom = 1;
  22. };
  23. class Renderer {
  24. public:
  25. using TileID = uint16_t;
  26. Renderer();
  27. ~Renderer();
  28. void drawChunk(RenderChunk chunk, SwanCommon::Vec2 pos);
  29. void drawTile(TileID id, Mat3gf mat);
  30. void drawSprite(RenderSprite sprite, Mat3gf mat, int y = 0);
  31. void drawRect(SwanCommon::Vec2 pos, SwanCommon::Vec2 size);
  32. void draw(const RenderCamera &cam);
  33. void uploadTileAtlas(const void *data, int width, int height);
  34. void modifyTile(TileID id, const void *data);
  35. RenderChunk createChunk(
  36. TileID tiles[SwanCommon::CHUNK_WIDTH * SwanCommon::CHUNK_HEIGHT]);
  37. void modifyChunk(RenderChunk chunk, SwanCommon::Vec2i pos, TileID id);
  38. void destroyChunk(RenderChunk chunk);
  39. RenderSprite createSprite(void *data, int width, int height, int fh);
  40. RenderSprite createSprite(void *data, int width, int height);
  41. void destroySprite(RenderSprite sprite);
  42. SwanCommon::Vec2 winScale() { return winScale_; }
  43. private:
  44. struct DrawChunk {
  45. SwanCommon::Vec2 pos;
  46. RenderChunk chunk;
  47. };
  48. struct DrawTile {
  49. Mat3gf transform;
  50. TileID id;
  51. };
  52. struct DrawSprite {
  53. Mat3gf transform;
  54. int frame;
  55. RenderSprite sprite;
  56. };
  57. struct DrawRect {
  58. SwanCommon::Vec2 pos;
  59. SwanCommon::Vec2 size;
  60. };
  61. SwanCommon::Vec2 winScale_ = {1, 1};
  62. std::unique_ptr<RendererState> state_;
  63. std::vector<DrawChunk> drawChunks_;
  64. std::vector<DrawTile> drawTiles_;
  65. std::vector<DrawSprite> drawSprites_;
  66. std::vector<DrawRect> drawRects_;
  67. };
  68. inline void Renderer::drawChunk(RenderChunk chunk, SwanCommon::Vec2 pos) {
  69. drawChunks_.push_back({pos, chunk});
  70. }
  71. inline void Renderer::drawTile(TileID id, Mat3gf mat) {
  72. drawTiles_.push_back({mat, id});
  73. }
  74. inline void Renderer::drawSprite(RenderSprite sprite, Mat3gf mat, int frame) {
  75. drawSprites_.push_back({mat, frame, sprite});
  76. }
  77. inline void Renderer::drawRect(SwanCommon::Vec2 pos, SwanCommon::Vec2 size) {
  78. drawRects_.push_back({pos, size});
  79. }
  80. }