Quad.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /**
  2. * Copyright (c) 2006-2011 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. // GLee
  23. #include "GLee.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), sh(sh)
  34. {
  35. memset(vertices, 255, sizeof(vertex)*NUM_VERTICES);
  36. refresh(v, sw, sh);
  37. }
  38. Quad::~Quad()
  39. {
  40. }
  41. void Quad::refresh(const Viewport & v, float sw, float sh)
  42. {
  43. if (!GLEE_ARB_texture_non_power_of_two)
  44. {
  45. sw = next_p2(sw);
  46. sh = next_p2(sh);
  47. }
  48. viewport = v;
  49. vertices[0].x = 0;
  50. vertices[0].y = 0;
  51. vertices[1].x = 0;
  52. vertices[1].y = v.h;
  53. vertices[2].x = v.w;
  54. vertices[2].y = v.h;
  55. vertices[3].x = v.w;
  56. vertices[3].y = 0;
  57. vertices[0].s = v.x/sw;
  58. vertices[0].t = v.y/sh;
  59. vertices[1].s = v.x/sw;
  60. vertices[1].t = (v.y+v.h)/sh;
  61. vertices[2].s = (v.x+v.w)/sw;
  62. vertices[2].t = (v.y+v.h)/sh;
  63. vertices[3].s = (v.x+v.w)/sw;
  64. vertices[3].t = v.y/sh;
  65. }
  66. void Quad::setViewport(const Quad::Viewport & v)
  67. {
  68. refresh(v, sw, sh);
  69. }
  70. Quad::Viewport Quad::getViewport() const
  71. {
  72. return viewport;
  73. }
  74. void Quad::flip(bool x, bool y)
  75. {
  76. vertex temp[4];
  77. if (x)
  78. {
  79. memcpy(temp, vertices, sizeof(vertex)*NUM_VERTICES);
  80. vertices[0].s = temp[3].s; vertices[0].t = temp[3].t;
  81. vertices[1].s = temp[2].s; vertices[1].t = temp[2].t;
  82. vertices[2].s = temp[1].s; vertices[2].t = temp[1].t;
  83. vertices[3].s = temp[0].s; vertices[3].t = temp[0].t;
  84. }
  85. if (y)
  86. {
  87. memcpy(temp, vertices, sizeof(vertex)*NUM_VERTICES);
  88. vertices[0].s = temp[1].s; vertices[0].t = temp[1].t;
  89. vertices[1].s = temp[0].s; vertices[1].t = temp[0].t;
  90. vertices[2].s = temp[3].s; vertices[2].t = temp[3].t;
  91. vertices[3].s = temp[2].s; vertices[3].t = temp[2].t;
  92. }
  93. }
  94. void Quad::mirror(bool x, bool y)
  95. {
  96. for (size_t i = 0; i < NUM_VERTICES; ++i) {
  97. if (x)
  98. vertices[i].s = 1.0 - vertices[i].s;
  99. if (y)
  100. vertices[i].s = 1.0 - vertices[i].s;
  101. }
  102. }
  103. const vertex * Quad::getVertices() const
  104. {
  105. return vertices;
  106. }
  107. } // opengl
  108. } // graphics
  109. } // love