Spritesheet.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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) 2019 Michael R. P. Ragazzon
  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/Spritesheet.h"
  30. namespace Rml {
  31. namespace Core {
  32. bool SpritesheetList::AddSpriteSheet(const String& name, const String& image_source, const String& definition_source, int definition_line_number, const SpriteDefinitionList& sprite_definitions)
  33. {
  34. // Load the texture
  35. Texture texture;
  36. if (!texture.Load(image_source, definition_source))
  37. {
  38. Log::Message(Log::LT_WARNING, "Could not load image '%s' specified in spritesheet '%s' at %s:%d", image_source.c_str(), name.c_str(), definition_source.c_str(), definition_line_number);
  39. return false;
  40. }
  41. auto sprite_sheet = std::make_shared<Spritesheet>(name, image_source, definition_source, definition_line_number, texture);
  42. auto result = spritesheet_map.emplace(name, sprite_sheet);
  43. if (!result.second)
  44. {
  45. Log::Message(Log::LT_WARNING, "Spritesheet '%s' has the same name as another spritesheet, ignored. See %s:%d", name.c_str(), definition_source.c_str(), definition_line_number);
  46. return false;
  47. }
  48. StringList& sprite_names = sprite_sheet->sprite_names;
  49. // Insert all the sprites with names not already defined in the global sprite list.
  50. for (auto& sprite_definition : sprite_definitions)
  51. {
  52. const String& sprite_name = sprite_definition.first;
  53. const Rectangle& sprite_rectangle = sprite_definition.second;
  54. auto result = sprite_map.emplace(sprite_name, Sprite{ sprite_rectangle, sprite_sheet.get() });
  55. if (result.second)
  56. {
  57. sprite_names.push_back(sprite_name);
  58. }
  59. else
  60. {
  61. Log::Message(Log::LT_WARNING, "Sprite '%s' has the same name as an existing sprite, skipped. See %s:%d", sprite_name.c_str(), definition_source.c_str(), definition_line_number);
  62. }
  63. }
  64. return true;
  65. }
  66. const Sprite* SpritesheetList::GetSprite(const String& name) const
  67. {
  68. auto it = sprite_map.find(name);
  69. if (it != sprite_map.end())
  70. return &it->second;
  71. return nullptr;
  72. }
  73. void SpritesheetList::Merge(const SpritesheetList& other)
  74. {
  75. for (auto& pair : other.spritesheet_map)
  76. {
  77. auto sheet_result = spritesheet_map.emplace(pair);
  78. const String& sheet_name = sheet_result.first->first;
  79. const Spritesheet& sheet = *sheet_result.first->second;
  80. bool sheet_inserted = sheet_result.second;
  81. if (sheet_inserted)
  82. {
  83. // The sprite sheet was succesfully added, now try to add each sprite to the global list.
  84. for (const String& sprite_name : sheet.sprite_names)
  85. {
  86. // Lookup the sprite in the other map.
  87. auto it_sprite = other.sprite_map.find(sprite_name);
  88. if (it_sprite != other.sprite_map.end())
  89. {
  90. // Add the sprite into our map. Each sprite name must be unique.
  91. auto sprite_result = sprite_map.emplace(sprite_name, it_sprite->second);
  92. bool inserted = sprite_result.second;
  93. if (!inserted)
  94. {
  95. Log::Message(Log::LT_WARNING, "Duplicate sprite name '%s' found while merging style sheets, defined in %s:%d.", sprite_name.c_str(), sheet.definition_source.c_str(), sheet.definition_line_number);
  96. }
  97. }
  98. else
  99. {
  100. RMLUI_ERRORMSG("Something went wrong while merging style sheets.");
  101. }
  102. }
  103. }
  104. else
  105. {
  106. Log::Message(Log::LT_WARNING, "Duplicate sprite sheet name '%s' found while merging style sheets, defined in %s:%d.", sheet_name.c_str(), sheet.definition_source.c_str(), sheet.definition_line_number);
  107. }
  108. }
  109. }
  110. void SpritesheetList::Reserve(size_t size_sprite_sheets, size_t size_sprites)
  111. {
  112. spritesheet_map.reserve(size_sprite_sheets);
  113. sprite_map.reserve(size_sprites);
  114. }
  115. size_t SpritesheetList::NumSpriteSheets() const
  116. {
  117. return spritesheet_map.size();
  118. }
  119. size_t SpritesheetList::NumSprites() const
  120. {
  121. return sprite_map.size();
  122. }
  123. String SpritesheetList::ToString() const
  124. {
  125. String result = CreateString(100, "#SpriteSheets: %d\n", spritesheet_map.size());
  126. for (auto& sheet : spritesheet_map)
  127. {
  128. result += CreateString(100, " Sheet '%s'. #Sprites %d.\n", sheet.first.c_str(), sheet.second->sprite_names.size());
  129. }
  130. result += CreateString(100, "\n#Sprites: %d\n", sprite_map.size());
  131. for (auto& sprite : sprite_map)
  132. {
  133. result += CreateString(100, " In '%s': %s\n", sprite.second.sprite_sheet->name.c_str(), sprite.first.c_str());
  134. }
  135. return result;
  136. }
  137. }
  138. }