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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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;
  23. SwanCommon::Vec2i size;
  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. private:
  44. struct DrawChunk {
  45. SwanCommon::Vec2 pos;
  46. RenderChunk chunk;
  47. };
  48. struct DrawSprite {
  49. Mat3gf transform;
  50. int frame;
  51. RenderSprite sprite;
  52. };
  53. std::unique_ptr<RendererState> state_;
  54. std::vector<DrawChunk> drawChunks_;
  55. std::vector<DrawSprite> drawSprites_;
  56. };
  57. inline void Renderer::drawChunk(RenderChunk chunk, SwanCommon::Vec2 pos) {
  58. drawChunks_.push_back({pos, chunk});
  59. }
  60. inline void Renderer::drawSprite(RenderSprite sprite, Mat3gf mat, int frame) {
  61. drawSprites_.push_back({mat, frame, sprite});
  62. }
  63. }