Geometry.cpp 4.8 KB

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