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.

RenderQueue.h 1.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #pragma once
  2. #include <vector>
  3. #include "GlWrappers.h"
  4. namespace Cygnet {
  5. class RenderQueue {
  6. public:
  7. struct Locs {
  8. GLint transform;
  9. GLint position;
  10. GLint texCoord;
  11. GLint tex;
  12. };
  13. RenderQueue(Locs locs, float scale):
  14. locs_(std::move(locs)), pixScale_(scale) {};
  15. RenderQueue(GlProgram &prog, float scale):
  16. RenderQueue({
  17. prog.uniformLoc("transform"),
  18. prog.attribLoc("position"),
  19. prog.attribLoc("texCoord"),
  20. prog.uniformLoc("tex"),
  21. }, scale) {}
  22. void show(GlTexture &tex, float x, float y) {
  23. show(tex.id(), x, y, tex.width(), tex.height());
  24. }
  25. void show(GlTexture &tex, float x, float y, float sx, float sy) {
  26. show(tex.id(), x, y, tex.width() * sx, tex.height() * sy);
  27. }
  28. void show(GlTexture &tex, float x, float y, float sx, float sy, float w, float h) {
  29. show(tex.id(), x, y, w * sx, h * sy);
  30. }
  31. void show(GLuint tex, float x, float y, float w, float h) {
  32. queue_.push_back({ tex, x, y, w * pixScale_, h * pixScale_ });
  33. }
  34. float getScaleX() { return mat_[0]; }
  35. float getScaleY() { return -mat_[4]; }
  36. float getTranslateX() { return mat_[3]; }
  37. float getTranslateY() { return mat_[5]; }
  38. void setScale(float sx, float sy) { mat_[0] = sx; mat_[4] = -sy; }
  39. void setTranslate(float tx, float ty) { mat_[2] = tx; mat_[5] = ty; }
  40. void draw();
  41. private:
  42. struct Entry {
  43. GLuint tex;
  44. float x, y;
  45. float w, h;
  46. };
  47. Locs locs_;
  48. float pixScale_;
  49. GLfloat mat_[9] = {
  50. 1, 0, 0, // scaleX, 0, translateX,
  51. 0, -1, 0, // 0, -scaleY, translateY,
  52. 0, 0, 1, // 0, 0, 1
  53. };
  54. std::vector<Entry> queue_;
  55. };
  56. }