Spritesheet.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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) 2019 Michael R. P. Ragazzon
  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 "../../Include/Rocket/Core/Spritesheet.h"
  29. namespace Rocket {
  30. namespace Core {
  31. bool SpritesheetList::AddSpriteSheet(const String& name, const String& image_source, const String& definition_source, int definition_line_number, const SpriteDefinitionList& sprite_definitions)
  32. {
  33. // Load the texture
  34. Texture texture;
  35. if (!texture.Load(image_source, definition_source))
  36. {
  37. 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);
  38. return false;
  39. }
  40. auto result = spritesheet_map.emplace(name, std::make_shared<Spritesheet>(name, image_source, definition_source, definition_line_number, texture));
  41. if (!result.second)
  42. {
  43. 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);
  44. return false;
  45. }
  46. Spritesheet& spritesheet = *result.first->second;
  47. StringList& sprite_names = spritesheet.sprite_names;
  48. // Insert all the sprites with names not already defined in the global sprite list.
  49. int num_removed_sprite_names = 0;
  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, &spritesheet });
  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. bool sheet_inserted = sheet_result.second;
  79. if (sheet_inserted)
  80. {
  81. const String& sheet_name = sheet_result.first->first;
  82. Spritesheet& sheet = *sheet_result.first->second;
  83. // The spritesheet was succesfully added, now try to add each sprite to the global list.
  84. // Each sprite name must be unique, if we fail, we must also remove the sprite from the spritesheet.
  85. for (auto it = sheet.sprite_names.begin(); it != sheet.sprite_names.end(); )
  86. {
  87. const String& sprite_name = *it;
  88. auto it_sprite = other.sprite_map.find(sprite_name);
  89. bool success = false;
  90. if (it_sprite != other.sprite_map.end())
  91. {
  92. auto sprite_result = sprite_map.emplace(*it, it_sprite->second);
  93. success = sprite_result.second;
  94. }
  95. if (success)
  96. {
  97. ++it;
  98. }
  99. else
  100. {
  101. 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);
  102. it = sheet.sprite_names.erase(it);
  103. }
  104. }
  105. }
  106. }
  107. }
  108. }
  109. }