2
0

Geometry.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. #include "GeometryDatabase.h"
  35. #include <utility>
  36. namespace Rml {
  37. Geometry::Geometry(Element* host_element) : host_element(host_element)
  38. {
  39. database_handle = GeometryDatabase::Insert(this);
  40. }
  41. Geometry::Geometry(Context* host_context) : host_context(host_context)
  42. {
  43. database_handle = GeometryDatabase::Insert(this);
  44. }
  45. Geometry::Geometry(Geometry&& other)
  46. {
  47. MoveFrom(other);
  48. database_handle = GeometryDatabase::Insert(this);
  49. }
  50. Geometry& Geometry::operator=(Geometry&& other)
  51. {
  52. MoveFrom(other);
  53. // Keep the database handles from construction unchanged, they are tied to the *this* pointer and should not change.
  54. return *this;
  55. }
  56. void Geometry::MoveFrom(Geometry& other)
  57. {
  58. host_context = std::exchange(other.host_context, nullptr);
  59. host_element = std::exchange(other.host_element, nullptr);
  60. vertices = std::move(other.vertices);
  61. indices = std::move(other.indices);
  62. texture = std::exchange(other.texture, nullptr);
  63. compiled_geometry = std::exchange(other.compiled_geometry, 0);
  64. compile_attempted = std::exchange(other.compile_attempted, false);
  65. }
  66. Geometry::~Geometry()
  67. {
  68. GeometryDatabase::Erase(database_handle);
  69. Release();
  70. }
  71. // Set the host element for this geometry; this should be passed in the constructor if possible.
  72. void Geometry::SetHostElement(Element* _host_element)
  73. {
  74. if (host_element == _host_element)
  75. return;
  76. if (host_element != nullptr)
  77. {
  78. Release();
  79. host_context = nullptr;
  80. }
  81. host_element = _host_element;
  82. }
  83. void Geometry::Render(const Vector2f& translation)
  84. {
  85. RenderInterface* const render_interface = GetRenderInterface();
  86. if (!render_interface)
  87. return;
  88. // Render our compiled geometry if possible.
  89. if (compiled_geometry)
  90. {
  91. RMLUI_ZoneScopedN("RenderCompiled");
  92. render_interface->RenderCompiledGeometry(compiled_geometry, translation);
  93. }
  94. // Otherwise, if we actually have geometry, try to compile it if we haven't already done so, otherwise render it in
  95. // immediate mode.
  96. else
  97. {
  98. if (vertices.empty() ||
  99. indices.empty())
  100. return;
  101. RMLUI_ZoneScopedN("RenderGeometry");
  102. if (!compile_attempted)
  103. {
  104. compile_attempted = true;
  105. compiled_geometry = render_interface->CompileGeometry(&vertices[0], (int)vertices.size(), &indices[0], (int)indices.size(), texture ? texture->GetHandle(render_interface) : 0);
  106. // If we managed to compile the geometry, we can clear the local copy of vertices and indices and
  107. // immediately render the compiled version.
  108. if (compiled_geometry)
  109. {
  110. render_interface->RenderCompiledGeometry(compiled_geometry, translation);
  111. return;
  112. }
  113. }
  114. // Either we've attempted to compile before (and failed), or the compile we just attempted failed; either way,
  115. // render the uncompiled version.
  116. render_interface->RenderGeometry(&vertices[0], (int)vertices.size(), &indices[0], (int)indices.size(), texture ? texture->GetHandle(GetRenderInterface()) : 0, translation);
  117. }
  118. }
  119. // Returns the geometry's vertices. If these are written to, Release() should be called to force a recompile.
  120. Vector< Vertex >& Geometry::GetVertices()
  121. {
  122. return vertices;
  123. }
  124. // Returns the geometry's indices. If these are written to, Release() should be called to force a recompile.
  125. Vector< int >& Geometry::GetIndices()
  126. {
  127. return indices;
  128. }
  129. // Gets the geometry's texture.
  130. const Texture* Geometry::GetTexture() const
  131. {
  132. return texture;
  133. }
  134. // Sets the geometry's texture.
  135. void Geometry::SetTexture(const Texture* _texture)
  136. {
  137. texture = _texture;
  138. Release();
  139. }
  140. void Geometry::Release(bool clear_buffers)
  141. {
  142. if (compiled_geometry)
  143. {
  144. GetRenderInterface()->ReleaseCompiledGeometry(compiled_geometry);
  145. compiled_geometry = 0;
  146. }
  147. compile_attempted = false;
  148. if (clear_buffers)
  149. {
  150. vertices.clear();
  151. indices.clear();
  152. }
  153. }
  154. Geometry::operator bool() const
  155. {
  156. return !indices.empty();
  157. }
  158. // Returns the host context's render interface.
  159. RenderInterface* Geometry::GetRenderInterface()
  160. {
  161. if (!host_context)
  162. {
  163. if (host_element)
  164. host_context = host_element->GetContext();
  165. }
  166. if (host_context)
  167. return host_context->GetRenderInterface();
  168. else
  169. return ::Rml::GetRenderInterface();
  170. }
  171. } // namespace Rml