TextureDatabase.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 "TextureDatabase.h"
  29. #include "TextureResource.h"
  30. #include "../../Include/Rocket/Core.h"
  31. namespace Rocket {
  32. namespace Core {
  33. static TextureDatabase* instance = NULL;
  34. TextureDatabase::TextureDatabase()
  35. {
  36. ROCKET_ASSERT(instance == NULL);
  37. instance = this;
  38. }
  39. TextureDatabase::~TextureDatabase()
  40. {
  41. ROCKET_ASSERT(instance == this);
  42. instance = NULL;
  43. }
  44. void TextureDatabase::Initialise()
  45. {
  46. new TextureDatabase();
  47. }
  48. void TextureDatabase::Shutdown()
  49. {
  50. delete instance;
  51. }
  52. // If the requested texture is already in the database, it will be returned with an extra reference count. If not, it
  53. // will be loaded through the application's render interface.
  54. TextureResource* TextureDatabase::Fetch(const String& source, const String& source_directory)
  55. {
  56. String path;
  57. if (source.Substring(0, 1) == "?")
  58. path = source;
  59. else
  60. GetSystemInterface()->JoinPath(path, source_directory.Replace("|", ":"), source);
  61. TextureMap::iterator iterator = instance->textures.find(path);
  62. if (iterator != instance->textures.end())
  63. {
  64. (*iterator).second->AddReference();
  65. return (*iterator).second;
  66. }
  67. TextureResource* resource = new TextureResource();
  68. if (!resource->Load(path))
  69. {
  70. resource->RemoveReference();
  71. return NULL;
  72. }
  73. instance->textures[resource->GetSource()] = resource;
  74. return resource;
  75. }
  76. // Releases all textures in the database.
  77. void TextureDatabase::ReleaseTextures()
  78. {
  79. for (TextureMap::iterator i = instance->textures.begin(); i != instance->textures.end(); ++i)
  80. i->second->Release();
  81. }
  82. // Removes a texture from the database.
  83. void TextureDatabase::RemoveTexture(TextureResource* texture)
  84. {
  85. if (instance != NULL)
  86. {
  87. TextureMap::iterator iterator = instance->textures.find(texture->GetSource());
  88. if (iterator != instance->textures.end())
  89. instance->textures.erase(iterator);
  90. }
  91. }
  92. // Release all textures bound through a render interface.
  93. void TextureDatabase::ReleaseTextures(RenderInterface* render_interface)
  94. {
  95. if (instance != NULL)
  96. {
  97. for (TextureMap::iterator i = instance->textures.begin(); i != instance->textures.end(); ++i)
  98. i->second->Release(render_interface);
  99. }
  100. }
  101. }
  102. }