Spritesheet.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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-2023 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 "../../Include/RmlUi/Core/Spritesheet.h"
  29. #include "../../Include/RmlUi/Core/Log.h"
  30. #include "../../Include/RmlUi/Core/StringUtilities.h"
  31. namespace Rml {
  32. Spritesheet::Spritesheet(const String& name, const String& image_source, const String& definition_source, int definition_line_number,
  33. float display_scale, const Texture& texture) :
  34. name(name),
  35. image_source(image_source), definition_source(definition_source), definition_line_number(definition_line_number), display_scale(display_scale),
  36. texture(texture)
  37. {}
  38. bool SpritesheetList::AddSpriteSheet(const String& name, const String& image_source, const String& definition_source, int definition_line_number,
  39. float display_scale, const SpriteDefinitionList& sprite_definitions)
  40. {
  41. // Load the texture
  42. Texture texture;
  43. texture.Set(image_source, definition_source);
  44. auto sprite_sheet_ptr = MakeShared<Spritesheet>(name, image_source, definition_source, definition_line_number, display_scale, texture);
  45. Spritesheet* sprite_sheet = sprite_sheet_ptr.get();
  46. spritesheets.push_back(std::move(sprite_sheet_ptr));
  47. sprite_map.reserve(sprite_map.size() + sprite_definitions.size());
  48. // Insert all the sprites, overwriting any existing sprites already defined in the current media block scope.
  49. for (auto& sprite_definition : sprite_definitions)
  50. {
  51. const String& sprite_name = sprite_definition.first;
  52. const Rectanglef& sprite_rectangle = sprite_definition.second;
  53. Sprite& new_sprite = sprite_map[sprite_name];
  54. if (new_sprite.sprite_sheet)
  55. {
  56. Log::Message(Log::LT_WARNING, "Sprite '%s' was overwritten due to duplicate names at the same block scope. Declared at %s:%d and %s:%d",
  57. sprite_name.c_str(), new_sprite.sprite_sheet->definition_source.c_str(), new_sprite.sprite_sheet->definition_line_number,
  58. definition_source.c_str(), definition_line_number);
  59. }
  60. new_sprite = Sprite{sprite_rectangle, sprite_sheet};
  61. }
  62. return true;
  63. }
  64. const Sprite* SpritesheetList::GetSprite(const String& name) const
  65. {
  66. auto it = sprite_map.find(name);
  67. if (it != sprite_map.end())
  68. return &it->second;
  69. return nullptr;
  70. }
  71. void SpritesheetList::Merge(const SpritesheetList& other)
  72. {
  73. spritesheets.insert(spritesheets.end(), other.spritesheets.begin(), other.spritesheets.end());
  74. sprite_map.reserve(sprite_map.size() + other.sprite_map.size());
  75. for (auto& pair : other.sprite_map)
  76. {
  77. const String& sprite_name = pair.first;
  78. const Sprite& sprite = pair.second;
  79. // Add the sprite into our map. If a sprite with the same name exists, it will be overwritten by the other sprite.
  80. sprite_map[sprite_name] = sprite;
  81. }
  82. }
  83. void SpritesheetList::Reserve(size_t size_sprite_sheets, size_t size_sprites)
  84. {
  85. spritesheets.reserve(size_sprite_sheets);
  86. sprite_map.reserve(size_sprites);
  87. }
  88. size_t SpritesheetList::NumSpriteSheets() const
  89. {
  90. return spritesheets.size();
  91. }
  92. size_t SpritesheetList::NumSprites() const
  93. {
  94. return sprite_map.size();
  95. }
  96. } // namespace Rml