Geometry.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /**
  2. * Copyright (c) 2006-2013 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 "Geometry.h"
  21. #include "common/Exception.h"
  22. #include "common/Vector.h"
  23. #include "modules/math/MathModule.h"
  24. using love::math::Math;
  25. // STD
  26. #include <limits>
  27. #include <algorithm> // for std::swap()
  28. #include <cstring> // For memcpy
  29. namespace love
  30. {
  31. namespace graphics
  32. {
  33. Geometry::Geometry(const std::vector<Vertex> &polygon, const std::vector<uint16> &elements, Geometry::DrawMode mode)
  34. : vertexArray(NULL)
  35. , vertexCount(polygon.size())
  36. , elementArray(NULL)
  37. , elementCount(elements.size())
  38. , vertexColors(false)
  39. , drawMode(mode)
  40. {
  41. if (polygon.size() < 3)
  42. throw love::Exception("At least 3 vertices are needed to create a Geometry.");
  43. for (size_t i = 0; i < elementCount; i++)
  44. {
  45. // All values in the element array need to be a valid vertex index.
  46. if (elements[i] >= vertexCount)
  47. throw love::Exception("Invalid vertex map value");
  48. }
  49. vertexArray = new Vertex[vertexCount];
  50. memcpy(vertexArray, &polygon[0], vertexCount * sizeof(Vertex));
  51. if (elementCount > 0)
  52. {
  53. elementArray = new uint16[elementCount];
  54. memcpy(elementArray, &elements[0], elementCount * sizeof(uint16));
  55. }
  56. }
  57. Geometry::Geometry(float x, float y, float w, float h, float sw, float sh)
  58. : vertexArray(NULL)
  59. , vertexCount(4)
  60. , elementArray(NULL)
  61. , elementCount(0)
  62. , vertexColors(false)
  63. , drawMode(DRAW_MODE_FAN)
  64. {
  65. float s0 = x/sw, s1 = (x+w)/sw, t0 = y/sh, t1 = (y+h)/sh;
  66. Vertex verts[4] = {
  67. {0,0, s0,t0, 255, 255, 255, 255},
  68. {w,0, s1,t0, 255, 255, 255, 255},
  69. {w,h, s1,t1, 255, 255, 255, 255},
  70. {0,h, s0,t1, 255, 255, 255, 255}
  71. };
  72. vertexArray = new Vertex[4];
  73. for (int i = 0; i < 4; i++)
  74. vertexArray[i] = verts[i];
  75. }
  76. Geometry::Geometry(const Geometry &other)
  77. : vertexCount(other.vertexCount)
  78. , elementCount(other.elementCount)
  79. , vertexColors(other.vertexColors)
  80. , drawMode(other.drawMode)
  81. {
  82. vertexArray = new Vertex[vertexCount];
  83. memcpy(vertexArray, other.vertexArray, vertexCount * sizeof(Vertex));
  84. if (elementCount > 0)
  85. {
  86. elementArray = new uint16[elementCount];
  87. memcpy(elementArray, other.elementArray, elementCount * sizeof(uint16));
  88. }
  89. }
  90. Geometry &Geometry::operator=(const Geometry &other)
  91. {
  92. if (this != &other)
  93. {
  94. Geometry temp(other);
  95. std::swap(vertexArray, temp.vertexArray);
  96. std::swap(elementArray, temp.elementArray);
  97. vertexCount = temp.vertexCount;
  98. elementCount = temp.elementCount;
  99. vertexColors = other.vertexColors;
  100. drawMode = other.drawMode;
  101. }
  102. return *this;
  103. }
  104. Geometry::~Geometry()
  105. {
  106. delete[] vertexArray;
  107. delete[] elementArray;
  108. }
  109. const Vertex &Geometry::getVertex(size_t i) const
  110. {
  111. if (i >= vertexCount)
  112. throw Exception("Invalid vertex index");
  113. return vertexArray[i];
  114. }
  115. void Geometry::setVertex(size_t i, const Vertex &v)
  116. {
  117. if (i >= vertexCount)
  118. throw Exception("Invalid vertex index");
  119. vertexArray[i] = v;
  120. }
  121. void Geometry::setElementArray(const uint16 *elements, size_t count)
  122. {
  123. if (count == 0 || elements == NULL)
  124. {
  125. delete[] elementArray;
  126. elementArray = NULL;
  127. elementCount = 0;
  128. return;
  129. }
  130. for (size_t i = 0; i < count; i++)
  131. {
  132. if (elements[i] >= vertexCount)
  133. throw love::Exception("Invalid vertex map value");
  134. }
  135. if (count > elementCount)
  136. {
  137. delete[] elementArray;
  138. elementArray = new uint16[count];
  139. }
  140. elementCount = count;
  141. memcpy(elementArray, elements, elementCount * sizeof(uint16));
  142. }
  143. void Geometry::setVertexColors(bool on)
  144. {
  145. vertexColors = on;
  146. }
  147. bool Geometry::getConstant(const char *in, Geometry::DrawMode &out)
  148. {
  149. return drawModes.find(in, out);
  150. }
  151. bool Geometry::getConstant(Geometry::DrawMode in, const char *&out)
  152. {
  153. return drawModes.find(in, out);
  154. }
  155. StringMap<Geometry::DrawMode, Geometry::DRAW_MODE_MAX_ENUM>::Entry Geometry::drawModeEntries[] =
  156. {
  157. {"fan", Geometry::DRAW_MODE_FAN},
  158. {"strip", Geometry::DRAW_MODE_STRIP},
  159. {"triangles", Geometry::DRAW_MODE_TRIANGLES}
  160. };
  161. StringMap<Geometry::DrawMode, Geometry::DRAW_MODE_MAX_ENUM> Geometry::drawModes(Geometry::drawModeEntries, sizeof(Geometry::drawModeEntries));
  162. } // graphics
  163. } // love