make sure that the SpriteStore object correctly replaces the SpriteImages

This commit is contained in:
Konstantin Käfer
2015-07-06 13:53:30 +02:00
parent 32699333e6
commit d37bd549bc
2 changed files with 32 additions and 6 deletions

View File

@@ -13,14 +13,24 @@ void SpriteStore::_setSprite(const std::string& name,
const std::shared_ptr<const SpriteImage>& sprite) {
if (sprite) {
auto it = sprites.find(name);
if (it != sprites.end() &&
(it->second->width != sprite->width || it->second->height != sprite->height)) {
Log::Warning(Event::Sprite, "Can't change sprite dimensions for '%s'", name.c_str());
return;
if (it != sprites.end()) {
// There is already a sprite with that name in our store.
if ((it->second->width != sprite->width || it->second->height != sprite->height)) {
Log::Warning(Event::Sprite, "Can't change sprite dimensions for '%s'", name.c_str());
return;
}
it->second = sprite;
} else {
sprites.emplace(name, sprite);
}
sprites.emplace_hint(it, name, sprite);
dirty.emplace(name, sprite);
// Always add/replace the value in the dirty list.
auto dirty_it = dirty.find(name);
if (dirty_it != dirty.end()) {
dirty_it->second = sprite;
} else {
dirty.emplace(name, sprite);
}
} else if (sprites.erase(name) > 0) {
dirty.emplace(name, nullptr);
}

View File

@@ -83,6 +83,22 @@ TEST(Annotations, SpriteStoreMultiple) {
EXPECT_EQ(Sprites(), store.getDirty());
}
TEST(Annotations, SpriteStoreReplace) {
FixtureLog log;
const auto sprite1 = std::make_shared<SpriteImage>(8, 8, 2, std::string(16 * 16 * 4, '\0'));
const auto sprite2 = std::make_shared<SpriteImage>(8, 8, 2, std::string(16 * 16 * 4, '\0'));
using Sprites = std::map<std::string, std::shared_ptr<const SpriteImage>>;
SpriteStore store;
store.setSprite("sprite", sprite1);
EXPECT_EQ(sprite1, store.getSprite("sprite"));
store.setSprite("sprite", sprite2);
EXPECT_EQ(sprite2, store.getSprite("sprite"));
EXPECT_EQ(Sprites({ { "sprite", sprite2 } }), store.getDirty());
}
TEST(Annotations, SpriteStoreReplaceWithDifferentDimensions) {
FixtureLog log;