Quad.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /**
  2. * Copyright (c) 2006-2012 LOVE Development Team
  3. *
  4. * This software is provided 'as-is', without any express or implied
  5. * warranty. In no event will the authors be held liable for any damages
  6. * arising from the use of this software.
  7. *
  8. * Permission is granted to anyone to use this software for any purpose,
  9. * including commercial applications, and to alter it and redistribute it
  10. * freely, subject to the following restrictions:
  11. *
  12. * 1. The origin of this software must not be misrepresented; you must not
  13. * claim that you wrote the original software. If you use this software
  14. * in a product, an acknowledgment in the product documentation would be
  15. * appreciated but is not required.
  16. * 2. Altered source versions must be plainly marked as such, and must not be
  17. * misrepresented as being the original software.
  18. * 3. This notice may not be removed or altered from any source distribution.
  19. **/
  20. #include "Quad.h"
  21. #include "common/Matrix.h"
  22. // OpenGL
  23. #include "OpenGL.h"
  24. // STD
  25. #include <cstring> // For memcpy
  26. namespace love
  27. {
  28. namespace graphics
  29. {
  30. namespace opengl
  31. {
  32. Quad::Quad(const Viewport &v, float sw, float sh)
  33. : sw(sw)
  34. , sh(sh)
  35. {
  36. memset(vertices, 255, sizeof(vertex)*NUM_VERTICES);
  37. refresh(v, sw, sh);
  38. }
  39. Quad::~Quad()
  40. {
  41. }
  42. void Quad::refresh(const Viewport &v, float sw, float sh)
  43. {
  44. if (!GLEE_ARB_texture_non_power_of_two)
  45. {
  46. sw = next_p2(sw);
  47. sh = next_p2(sh);
  48. }
  49. viewport = v;
  50. vertices[0].x = 0;
  51. vertices[0].y = 0;
  52. vertices[1].x = 0;
  53. vertices[1].y = v.h;
  54. vertices[2].x = v.w;
  55. vertices[2].y = v.h;
  56. vertices[3].x = v.w;
  57. vertices[3].y = 0;
  58. vertices[0].s = v.x/sw;
  59. vertices[0].t = v.y/sh;
  60. vertices[1].s = v.x/sw;
  61. vertices[1].t = (v.y+v.h)/sh;
  62. vertices[2].s = (v.x+v.w)/sw;
  63. vertices[2].t = (v.y+v.h)/sh;
  64. vertices[3].s = (v.x+v.w)/sw;
  65. vertices[3].t = v.y/sh;
  66. }
  67. void Quad::setViewport(const Quad::Viewport &v)
  68. {
  69. refresh(v, sw, sh);
  70. }
  71. Quad::Viewport Quad::getViewport() const
  72. {
  73. return viewport;
  74. }
  75. void Quad::flip(bool x, bool y)
  76. {
  77. vertex temp[4];
  78. if (x)
  79. {
  80. memcpy(temp, vertices, sizeof(vertex)*NUM_VERTICES);
  81. vertices[0].s = temp[3].s;
  82. vertices[0].t = temp[3].t;
  83. vertices[1].s = temp[2].s;
  84. vertices[1].t = temp[2].t;
  85. vertices[2].s = temp[1].s;
  86. vertices[2].t = temp[1].t;
  87. vertices[3].s = temp[0].s;
  88. vertices[3].t = temp[0].t;
  89. }
  90. if (y)
  91. {
  92. memcpy(temp, vertices, sizeof(vertex)*NUM_VERTICES);
  93. vertices[0].s = temp[1].s;
  94. vertices[0].t = temp[1].t;
  95. vertices[1].s = temp[0].s;
  96. vertices[1].t = temp[0].t;
  97. vertices[2].s = temp[3].s;
  98. vertices[2].t = temp[3].t;
  99. vertices[3].s = temp[2].s;
  100. vertices[3].t = temp[2].t;
  101. }
  102. }
  103. const vertex *Quad::getVertices() const
  104. {
  105. return vertices;
  106. }
  107. } // opengl
  108. } // graphics
  109. } // love