Geometry.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. * This source file is part of RmlUi, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://github.com/mikke89/RmlUi
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. * Copyright (c) 2019 The RmlUi Team, and contributors
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. */
  28. #include "../../Include/RmlUi/Core/Geometry.h"
  29. #include "../../Include/RmlUi/Core/Context.h"
  30. #include "../../Include/RmlUi/Core/Core.h"
  31. #include "../../Include/RmlUi/Core/Element.h"
  32. #include "../../Include/RmlUi/Core/Profiling.h"
  33. #include "../../Include/RmlUi/Core/RenderInterface.h"
  34. namespace Rml {
  35. namespace Core {
  36. Geometry::Geometry(Element* _host_element)
  37. {
  38. host_element = _host_element;
  39. host_context = nullptr;
  40. texture = nullptr;
  41. compile_attempted = false;
  42. compiled_geometry = 0;
  43. }
  44. Geometry::Geometry(Context* _host_context)
  45. {
  46. host_element = nullptr;
  47. host_context = _host_context;
  48. texture = nullptr;
  49. compile_attempted = false;
  50. compiled_geometry = 0;
  51. }
  52. Geometry::~Geometry()
  53. {
  54. Release();
  55. }
  56. // Set the host element for this geometry; this should be passed in the constructor if possible.
  57. void Geometry::SetHostElement(Element* _host_element)
  58. {
  59. if (host_element == _host_element)
  60. return;
  61. if (host_element != nullptr)
  62. {
  63. Release();
  64. host_context = nullptr;
  65. }
  66. host_element = _host_element;
  67. }
  68. void Geometry::Render(const Vector2f& translation)
  69. {
  70. RenderInterface* render_interface = GetRenderInterface();
  71. if (render_interface == nullptr)
  72. return;
  73. // Render our compiled geometry if possible.
  74. if (compiled_geometry)
  75. {
  76. RMLUI_ZoneScopedN("RenderCompiled");
  77. render_interface->RenderCompiledGeometry(compiled_geometry, translation);
  78. }
  79. // Otherwise, if we actually have geometry, try to compile it if we haven't already done so, otherwise render it in
  80. // immediate mode.
  81. else
  82. {
  83. if (vertices.empty() ||
  84. indices.empty())
  85. return;
  86. RMLUI_ZoneScopedN("RenderGeometry");
  87. if (!compile_attempted)
  88. {
  89. compile_attempted = true;
  90. compiled_geometry = render_interface->CompileGeometry(&vertices[0], (int) vertices.size(), &indices[0], (int) indices.size(), texture != nullptr ? texture->GetHandle(GetRenderInterface()) : 0);
  91. // If we managed to compile the geometry, we can clear the local copy of vertices and indices and
  92. // immediately render the compiled version.
  93. if (compiled_geometry)
  94. {
  95. render_interface->RenderCompiledGeometry(compiled_geometry, translation);
  96. return;
  97. }
  98. }
  99. // Either we've attempted to compile before (and failed), or the compile we just attempted failed; either way,
  100. // render the uncompiled version.
  101. render_interface->RenderGeometry(&vertices[0], (int) vertices.size(), &indices[0], (int) indices.size(), texture != nullptr ? texture->GetHandle(GetRenderInterface()) : 0, translation);
  102. }
  103. }
  104. // Returns the geometry's vertices. If these are written to, Release() should be called to force a recompile.
  105. std::vector< Vertex >& Geometry::GetVertices()
  106. {
  107. return vertices;
  108. }
  109. // Returns the geometry's indices. If these are written to, Release() should be called to force a recompile.
  110. std::vector< int >& Geometry::GetIndices()
  111. {
  112. return indices;
  113. }
  114. // Gets the geometry's texture.
  115. const Texture* Geometry::GetTexture() const
  116. {
  117. return texture;
  118. }
  119. // Sets the geometry's texture.
  120. void Geometry::SetTexture(const Texture* _texture)
  121. {
  122. texture = _texture;
  123. Release();
  124. }
  125. void Geometry::Release(bool clear_buffers)
  126. {
  127. if (compiled_geometry)
  128. {
  129. GetRenderInterface()->ReleaseCompiledGeometry(compiled_geometry);
  130. compiled_geometry = 0;
  131. }
  132. compile_attempted = false;
  133. if (clear_buffers)
  134. {
  135. vertices.clear();
  136. indices.clear();
  137. }
  138. }
  139. // Returns the host context's render interface.
  140. RenderInterface* Geometry::GetRenderInterface()
  141. {
  142. if (host_context == nullptr)
  143. {
  144. if (host_element != nullptr)
  145. host_context = host_element->GetContext();
  146. }
  147. if (host_context == nullptr)
  148. return Rml::Core::GetRenderInterface();
  149. else
  150. return host_context->GetRenderInterface();
  151. }
  152. }
  153. }