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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 drawSprite(RenderSprite sprite, Mat3gf mat, int y = 0);
  30. void drawRect(SwanCommon::Vec2 pos, SwanCommon::Vec2 size);
  31. void draw(const RenderCamera &cam);
  32. void uploadTileAtlas(const void *data, int width, int height);
  33. void modifyTile(TileID id, const void *data);
  34. RenderChunk createChunk(
  35. TileID tiles[SwanCommon::CHUNK_WIDTH * SwanCommon::CHUNK_HEIGHT]);
  36. void modifyChunk(RenderChunk chunk, SwanCommon::Vec2i pos, TileID id);
  37. void destroyChunk(RenderChunk chunk);
  38. RenderSprite createSprite(void *data, int width, int height, int fh);
  39. RenderSprite createSprite(void *data, int width, int height);
  40. void destroySprite(RenderSprite sprite);
  41. SwanCommon::Vec2 winScale() { return winScale_; }
  42. private:
  43. struct DrawChunk {
  44. SwanCommon::Vec2 pos;
  45. RenderChunk chunk;
  46. };
  47. struct DrawSprite {
  48. Mat3gf transform;
  49. int frame;
  50. RenderSprite sprite;
  51. };
  52. struct DrawRect {
  53. SwanCommon::Vec2 pos;
  54. SwanCommon::Vec2 size;
  55. };
  56. SwanCommon::Vec2 winScale_ = {1, 1};
  57. std::unique_ptr<RendererState> state_;
  58. std::vector<DrawChunk> drawChunks_;
  59. std::vector<DrawSprite> drawSprites_;
  60. std::vector<DrawRect> drawRects_;
  61. };
  62. inline void Renderer::drawChunk(RenderChunk chunk, SwanCommon::Vec2 pos) {
  63. drawChunks_.push_back({pos, chunk});
  64. }
  65. inline void Renderer::drawSprite(RenderSprite sprite, Mat3gf mat, int frame) {
  66. drawSprites_.push_back({mat, frame, sprite});
  67. }
  68. inline void Renderer::drawRect(SwanCommon::Vec2 pos, SwanCommon::Vec2 size) {
  69. drawRects_.push_back({pos, size});
  70. }
  71. }