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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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, 1, 1, tex.width(), tex.height());
  24. }
  25. void show(GlTexture &tex, float x, float y, float sx, float sy) {
  26. show(tex.id(), x, y, sx, sy, tex.width(), tex.height());
  27. }
  28. void show(GlTexture &tex, float x, float y, float sx, float sy, float w, float h) {
  29. show(tex.id(), x, y, sx, sy, w, h);
  30. }
  31. void show(GLuint tex, float x, float y, float sx, float sy, float w, float h) {
  32. queue_.push_back({ tex, x, y, sx, sy, 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 sx, sy;
  46. float w, h;
  47. };
  48. Locs locs_;
  49. float pixScale_;
  50. GLfloat mat_[9] = {
  51. 1, 0, 0, // scaleX, 0, translateX,
  52. 0, -1, 0, // 0, -scaleY, translateY,
  53. 0, 0, 1, // 0, 0, 1
  54. };
  55. std::vector<Entry> queue_;
  56. };
  57. }