diff --git a/client/render/CDefFile.cpp b/client/render/CDefFile.cpp index 4501b40d2..8f6bcf6c4 100644 --- a/client/render/CDefFile.cpp +++ b/client/render/CDefFile.cpp @@ -83,13 +83,12 @@ CDefFile::CDefFile(const AnimationPath & Name): void CDefFile::loadFrame(size_t frame, size_t group, IImageLoader &loader) const { - std::map >::const_iterator it; - it = offset.find(group); - assert (it != offset.end()); + assert(hasFrame(frame, group)); // hasFrame() should be called before calling loadFrame() - const ui8 * FDef = data.get()+it->second[frame]; + const ui8 * FDef = data.get() + offset.at(group)[frame]; + + const SSpriteDef sd = *reinterpret_cast(FDef); - const SSpriteDef sd = * reinterpret_cast(FDef); SSpriteDef sprite; sprite.format = read_le_u32(&sd.format); @@ -229,6 +228,23 @@ void CDefFile::loadFrame(size_t frame, size_t group, IImageLoader &loader) const } } +bool CDefFile::hasFrame(size_t frame, size_t group) const +{ + std::map >::const_iterator it; + it = offset.find(group); + if(it == offset.end()) + { + return false; + } + + if(frame >= it->second.size()) + { + return false; + } + + return true; +} + CDefFile::~CDefFile() = default; const std::map CDefFile::getEntries() const diff --git a/client/render/CDefFile.h b/client/render/CDefFile.h index 72f3996db..a5c7c0469 100644 --- a/client/render/CDefFile.h +++ b/client/render/CDefFile.h @@ -45,6 +45,7 @@ public: //load frame as SDL_Surface void loadFrame(size_t frame, size_t group, IImageLoader &loader) const; + bool hasFrame(size_t frame, size_t group) const; const std::map getEntries() const; }; diff --git a/client/renderSDL/RenderHandler.cpp b/client/renderSDL/RenderHandler.cpp index 0447c9786..551473b7b 100644 --- a/client/renderSDL/RenderHandler.cpp +++ b/client/renderSDL/RenderHandler.cpp @@ -239,13 +239,13 @@ std::shared_ptr RenderHandler::loadImageImpl(const ImageLoca std::shared_ptr RenderHandler::loadImageFromFileUncached(const ImageLocator & locator) { - if (locator.image) + if(locator.image) { // TODO: create EmptySharedImage class that will be instantiated if image does not exists or fails to load return std::make_shared(*locator.image, locator.preScaledFactor); } - if (locator.defFile) + if(locator.defFile) { auto defFile = getAnimationFile(*locator.defFile); int preScaledFactor = locator.preScaledFactor; @@ -258,7 +258,14 @@ std::shared_ptr RenderHandler::loadImageFromFileUncached(con preScaledFactor = 1; defFile = getAnimationFile(AnimationPath::builtin(tmpPath)); } - return std::make_shared(defFile.get(), locator.defFrame, locator.defGroup, preScaledFactor); + if(defFile->hasFrame(locator.defFrame, locator.defGroup)) + return std::make_shared(defFile.get(), locator.defFrame, locator.defGroup, preScaledFactor); + else + { + logGlobal->error("Frame %d in group %d not found in file: %s", + locator.defFrame, locator.defGroup, locator.defFile->getName().c_str()); + return std::make_shared(ImagePath::builtin("DEFAULT"), locator.preScaledFactor); + } } throw std::runtime_error("Invalid image locator received!");