Texture.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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/Texture.h"
  30. #include "TextureDatabase.h"
  31. #include "TextureResource.h"
  32. namespace Rml {
  33. namespace Core {
  34. // Constructs an unloaded texture with no resource.
  35. Texture::Texture()
  36. {
  37. resource = NULL;
  38. }
  39. // Constructs a texture sharing the resource of another.
  40. Texture::Texture(const Texture& copy)
  41. {
  42. resource = NULL;
  43. *this = copy;
  44. }
  45. Texture::~Texture()
  46. {
  47. if (resource)
  48. resource->RemoveReference();
  49. }
  50. // Attempts to load a texture.
  51. bool Texture::Load(const String& source, const String& source_path)
  52. {
  53. if (resource != NULL)
  54. resource->RemoveReference();
  55. resource = TextureDatabase::Fetch(source, source_path);
  56. return resource != NULL;
  57. }
  58. // Returns the texture's source name. This is usually the name of the file the texture was loaded from.
  59. String Texture::GetSource() const
  60. {
  61. if (resource == NULL)
  62. return String();
  63. return resource->GetSource();
  64. }
  65. // Returns the texture's handle.
  66. TextureHandle Texture::GetHandle(RenderInterface* render_interface) const
  67. {
  68. if (resource == NULL)
  69. return 0;
  70. return resource->GetHandle(render_interface);
  71. }
  72. // Returns the texture's dimensions.
  73. Vector2i Texture::GetDimensions(RenderInterface* render_interface) const
  74. {
  75. if (resource == NULL)
  76. return Vector2i(0, 0);
  77. return resource->GetDimensions(render_interface);
  78. }
  79. // Releases this texture's resource (if any), and sets it to another texture's resource.
  80. const Texture& Texture::operator=(const Texture& copy)
  81. {
  82. if (resource != NULL)
  83. resource->RemoveReference();
  84. resource = copy.resource;
  85. if (resource != NULL)
  86. resource->AddReference();
  87. return *this;
  88. }
  89. bool Texture::operator==(const Texture& other) const
  90. {
  91. return resource == other.resource;
  92. }
  93. Texture::operator bool() const
  94. {
  95. return static_cast<bool>(resource);
  96. }
  97. }
  98. }