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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #include "RenderQueue.h"
  2. #include "glutil.h"
  3. namespace Cygnet {
  4. static const GLfloat texCoords[] = {
  5. 0.0f, 0.0f, // tex 0: top left
  6. 0.0f, 1.0f, // tex 1: bottom left
  7. 1.0f, 1.0f, // tex 2: bottom right
  8. 1.0f, 0.0f, // tex 3: top right
  9. };
  10. static const GLushort indexes[] = {
  11. 0, 1, 2, // top left -> bottom left -> bottom right
  12. 2, 3, 0, // bottom right -> top right -> top left
  13. };
  14. void RenderQueue::draw() {
  15. glUniform1i(locs_.tex, 0);
  16. glVertexAttribPointer(locs_.texCoord, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(GLfloat), texCoords);
  17. glEnableVertexAttribArray(locs_.texCoord);
  18. glCheck();
  19. glUniformMatrix3fv(locs_.transform, 1, GL_TRUE, mat_);
  20. glCheck();
  21. glActiveTexture(GL_TEXTURE0);
  22. for (auto &entry: queue_) {
  23. float x = entry.x;
  24. float y = entry.y;
  25. float w = entry.w;
  26. float h = entry.h;
  27. GLfloat vertexes[] = {
  28. x, y, // top left
  29. x, y + h, // bottom left
  30. x + w, y + h, // bottom right
  31. x + w, y, // top right
  32. };
  33. glVertexAttribPointer(locs_.position, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(GLfloat), vertexes);
  34. glEnableVertexAttribArray(locs_.position);
  35. glCheck();
  36. glBindTexture(GL_TEXTURE_2D, entry.tex);
  37. glCheck();
  38. glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indexes);
  39. }
  40. queue_.clear();
  41. }
  42. }