TextureResource.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * This source file is part of libRocket, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://www.librocket.com
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. *
  26. */
  27. #include "precompiled.h"
  28. #include "TextureResource.h"
  29. #include "FontFaceHandle.h"
  30. #include "TextureDatabase.h"
  31. #include <Rocket/Core.h>
  32. namespace Rocket {
  33. namespace Core {
  34. TextureResource::TextureResource()
  35. {
  36. }
  37. TextureResource::~TextureResource()
  38. {
  39. TextureDatabase::RemoveTexture(this);
  40. }
  41. // Attempts to load a texture from the application into the resource.
  42. bool TextureResource::Load(const String& _source)
  43. {
  44. Release();
  45. source = _source;
  46. return true;
  47. }
  48. // Returns the resource's underlying texture.
  49. TextureHandle TextureResource::GetHandle(RenderInterface* render_interface) const
  50. {
  51. TextureDataMap::iterator texture_iterator = texture_data.find(render_interface);
  52. if (texture_iterator == texture_data.end())
  53. {
  54. Load(render_interface);
  55. texture_iterator = texture_data.find(render_interface);
  56. }
  57. return texture_iterator->second.first;
  58. }
  59. // Returns the dimensions of the resource's texture.
  60. const Vector2i& TextureResource::GetDimensions(RenderInterface* render_interface) const
  61. {
  62. TextureDataMap::iterator texture_iterator = texture_data.find(render_interface);
  63. if (texture_iterator == texture_data.end())
  64. {
  65. Load(render_interface);
  66. texture_iterator = texture_data.find(render_interface);
  67. }
  68. return texture_iterator->second.second;
  69. }
  70. // Returns the resource's source.
  71. const String& TextureResource::GetSource() const
  72. {
  73. return source;
  74. }
  75. // Releases the texture's handle.
  76. void TextureResource::Release(RenderInterface* render_interface)
  77. {
  78. if (render_interface == NULL)
  79. {
  80. for (TextureDataMap::iterator texture_iterator = texture_data.begin(); texture_iterator != texture_data.end(); ++texture_iterator)
  81. {
  82. TextureHandle handle = texture_iterator->second.first;
  83. if (handle)
  84. texture_iterator->first->ReleaseTexture(handle);
  85. }
  86. texture_data.clear();
  87. }
  88. else
  89. {
  90. TextureDataMap::iterator texture_iterator = texture_data.find(render_interface);
  91. if (texture_iterator == texture_data.end())
  92. return;
  93. TextureHandle handle = texture_iterator->second.first;
  94. if (handle)
  95. texture_iterator->first->ReleaseTexture(handle);
  96. texture_data.erase(render_interface);
  97. }
  98. }
  99. // Attempts to load the texture from the source.
  100. bool TextureResource::Load(RenderInterface* render_interface) const
  101. {
  102. // Check for special loader tokens.
  103. if (!source.Empty() &&
  104. source[0] == '?')
  105. {
  106. Vector2i dimensions;
  107. bool delete_data = false;
  108. const byte* data = NULL;
  109. // Find the generation protocol and generate the data accordingly.
  110. String protocol = source.Substring(1, source.Find("::") - 1);
  111. if (protocol == "font")
  112. {
  113. // The requested texture is a font layer.
  114. delete_data = true;
  115. FontFaceHandle* handle;
  116. FontEffect* layer_id;
  117. int texture_id;
  118. if (sscanf(source.CString(), "?font::%p/%p/%d", &handle, &layer_id, &texture_id) == 3)
  119. {
  120. handle->GenerateLayerTexture(data,
  121. dimensions,
  122. layer_id,
  123. texture_id);
  124. }
  125. }
  126. // If texture data was generated, great! Otherwise, fallback to the LoadTexture() code and
  127. // hope the client knows what the hell to do with the question mark in their file name.
  128. if (data != NULL)
  129. {
  130. TextureHandle handle;
  131. bool success = render_interface->GenerateTexture(handle, data, dimensions);
  132. if (delete_data)
  133. delete[] data;
  134. if (success)
  135. {
  136. texture_data[render_interface] = TextureData(handle, dimensions);
  137. return true;
  138. }
  139. else
  140. {
  141. Log::Message(Log::LT_WARNING, "Failed to generate internal texture %s.", source.CString());
  142. texture_data[render_interface] = TextureData(NULL, Vector2i(0, 0));
  143. return false;
  144. }
  145. }
  146. }
  147. TextureHandle handle;
  148. Vector2i dimensions;
  149. if (!render_interface->LoadTexture(handle, dimensions, source))
  150. {
  151. Log::Message(Log::LT_WARNING, "Failed to load texture from %s.", source.CString());
  152. texture_data[render_interface] = TextureData(NULL, Vector2i(0, 0));
  153. return false;
  154. }
  155. texture_data[render_interface] = TextureData(handle, dimensions);
  156. return true;
  157. }
  158. void TextureResource::OnReferenceDeactivate()
  159. {
  160. Release();
  161. delete this;
  162. }
  163. }
  164. }