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.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #include "Renderer.h"
  2. #include <SDL_opengles2.h>
  3. #include "shaders.h"
  4. #include "GlWrappers.h"
  5. #include "TileAtlas.h"
  6. #include "util.h"
  7. namespace Cygnet {
  8. struct TexturedProg: public GlProgram {
  9. using GlProgram::GlProgram;
  10. GLint position = attribLoc("position");
  11. GLint texCoord = attribLoc("texCoord");
  12. GLint tex = uniformLoc("tex");
  13. };
  14. struct SolidColorProg: public GlProgram {
  15. using GlProgram::GlProgram;
  16. GLint position = attribLoc("position");
  17. GLint color = uniformLoc("color");
  18. };
  19. struct RendererState {
  20. GlVxShader basicVx{Shaders::basicVx};
  21. GlVxShader texturedVx{Shaders::texturedVx};
  22. GlFrShader solidColorFr{Shaders::solidColorFr};
  23. GlFrShader texturedFr{Shaders::texturedFr};
  24. TexturedProg texturedProg{texturedVx, texturedFr};
  25. SolidColorProg solidColorProg{basicVx, solidColorFr};
  26. TileAtlas atlas;
  27. GlTexture atlasTex;
  28. };
  29. Renderer::Renderer(): state_(std::make_unique<RendererState>()) {}
  30. Renderer::~Renderer() = default;
  31. void Renderer::draw() {
  32. state_->texturedProg.use();
  33. }
  34. void Renderer::registerTileTexture(size_t tileId, const void *data, size_t len) {
  35. state_->atlas.addTile(tileId, data, len);
  36. }
  37. void Renderer::uploadTileTexture() {
  38. size_t w, h;
  39. const unsigned char *data = state_->atlas.getImage(&w, &h);
  40. state_->atlasTex.upload(w, h, (void *)data, GL_RGBA, GL_UNSIGNED_BYTE);
  41. }
  42. }