mirror of
https://github.com/vcmi/vcmi.git
synced 2025-07-05 00:49:09 +02:00
fix_crash_loading_frames_from_def
This commit is contained in:
@ -38,7 +38,8 @@ enum class DefType : uint32_t
|
|||||||
|
|
||||||
CDefFile::CDefFile(const AnimationPath & Name):
|
CDefFile::CDefFile(const AnimationPath & Name):
|
||||||
data(nullptr),
|
data(nullptr),
|
||||||
palette(nullptr)
|
palette(nullptr),
|
||||||
|
fileName(Name.getName())
|
||||||
{
|
{
|
||||||
data = CResourceHandler::get()->load(Name)->readAll().first;
|
data = CResourceHandler::get()->load(Name)->readAll().first;
|
||||||
|
|
||||||
@ -83,13 +84,12 @@ CDefFile::CDefFile(const AnimationPath & Name):
|
|||||||
|
|
||||||
void CDefFile::loadFrame(size_t frame, size_t group, IImageLoader &loader) const
|
void CDefFile::loadFrame(size_t frame, size_t group, IImageLoader &loader) const
|
||||||
{
|
{
|
||||||
std::map<size_t, std::vector <size_t> >::const_iterator it;
|
assert(hasFrame(frame, group)); // hasFrame() should be called before calling loadFrame()
|
||||||
it = offset.find(group);
|
|
||||||
assert (it != offset.end());
|
const ui8 * FDef = data.get() + offset.at(group)[frame];
|
||||||
|
|
||||||
const ui8 * FDef = data.get()+it->second[frame];
|
const SSpriteDef sd = *reinterpret_cast<const SSpriteDef *>(FDef);
|
||||||
|
|
||||||
const SSpriteDef sd = * reinterpret_cast<const SSpriteDef *>(FDef);
|
|
||||||
SSpriteDef sprite;
|
SSpriteDef sprite;
|
||||||
|
|
||||||
sprite.format = read_le_u32(&sd.format);
|
sprite.format = read_le_u32(&sd.format);
|
||||||
@ -227,6 +227,23 @@ void CDefFile::loadFrame(size_t frame, size_t group, IImageLoader &loader) const
|
|||||||
logGlobal->error("Error: unsupported format of def file: %d", sprite.format);
|
logGlobal->error("Error: unsupported format of def file: %d", sprite.format);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool CDefFile::hasFrame(size_t frame, size_t group) const
|
||||||
|
{
|
||||||
|
std::map<size_t, std::vector <size_t> >::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;
|
CDefFile::~CDefFile() = default;
|
||||||
|
@ -43,8 +43,11 @@ public:
|
|||||||
CDefFile(const AnimationPath & Name);
|
CDefFile(const AnimationPath & Name);
|
||||||
~CDefFile();
|
~CDefFile();
|
||||||
|
|
||||||
|
std::string fileName;
|
||||||
|
|
||||||
//load frame as SDL_Surface
|
//load frame as SDL_Surface
|
||||||
void loadFrame(size_t frame, size_t group, IImageLoader &loader) const;
|
void loadFrame(size_t frame, size_t group, IImageLoader &loader) const;
|
||||||
|
bool hasFrame(size_t frame, size_t group) const;
|
||||||
|
|
||||||
const std::map<size_t, size_t> getEntries() const;
|
const std::map<size_t, size_t> getEntries() const;
|
||||||
};
|
};
|
||||||
|
@ -239,7 +239,7 @@ std::shared_ptr<const ISharedImage> RenderHandler::loadImageImpl(const ImageLoca
|
|||||||
|
|
||||||
std::shared_ptr<const ISharedImage> RenderHandler::loadImageFromFileUncached(const ImageLocator & locator)
|
std::shared_ptr<const ISharedImage> 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
|
// TODO: create EmptySharedImage class that will be instantiated if image does not exists or fails to load
|
||||||
return std::make_shared<SDLImageShared>(*locator.image, locator.preScaledFactor);
|
return std::make_shared<SDLImageShared>(*locator.image, locator.preScaledFactor);
|
||||||
@ -258,7 +258,14 @@ std::shared_ptr<const ISharedImage> RenderHandler::loadImageFromFileUncached(con
|
|||||||
preScaledFactor = 1;
|
preScaledFactor = 1;
|
||||||
defFile = getAnimationFile(AnimationPath::builtin(tmpPath));
|
defFile = getAnimationFile(AnimationPath::builtin(tmpPath));
|
||||||
}
|
}
|
||||||
return std::make_shared<SDLImageShared>(defFile.get(), locator.defFrame, locator.defGroup, preScaledFactor);
|
if(defFile->hasFrame(locator.defFrame, locator.defGroup))
|
||||||
|
return std::make_shared<SDLImageShared>(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<SDLEmptyImageShared>();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
throw std::runtime_error("Invalid image locator received!");
|
throw std::runtime_error("Invalid image locator received!");
|
||||||
|
@ -99,6 +99,9 @@ SDLImageShared::SDLImageShared(const CDefFile * data, size_t frame, size_t group
|
|||||||
SDLImageLoader loader(this);
|
SDLImageLoader loader(this);
|
||||||
data->loadFrame(frame, group, loader);
|
data->loadFrame(frame, group, loader);
|
||||||
|
|
||||||
|
if(surf == nullptr)
|
||||||
|
return;
|
||||||
|
|
||||||
savePalette();
|
savePalette();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -11,6 +11,7 @@
|
|||||||
|
|
||||||
#include "../render/IImage.h"
|
#include "../render/IImage.h"
|
||||||
#include "../../lib/Point.h"
|
#include "../../lib/Point.h"
|
||||||
|
#include <SDL_surface.h>
|
||||||
|
|
||||||
VCMI_LIB_NAMESPACE_BEGIN
|
VCMI_LIB_NAMESPACE_BEGIN
|
||||||
class JsonNode;
|
class JsonNode;
|
||||||
@ -24,7 +25,7 @@ struct SDL_Palette;
|
|||||||
/*
|
/*
|
||||||
* Wrapper around SDL_Surface
|
* Wrapper around SDL_Surface
|
||||||
*/
|
*/
|
||||||
class SDLImageShared final : public ISharedImage, public std::enable_shared_from_this<SDLImageShared>, boost::noncopyable
|
class SDLImageShared : public ISharedImage, public std::enable_shared_from_this<SDLImageShared>, boost::noncopyable
|
||||||
{
|
{
|
||||||
//Surface without empty borders
|
//Surface without empty borders
|
||||||
SDL_Surface * surf;
|
SDL_Surface * surf;
|
||||||
@ -66,6 +67,20 @@ public:
|
|||||||
friend class SDLImageLoader;
|
friend class SDLImageLoader;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class SDLEmptyImageShared : public SDLImageShared
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
SDLEmptyImageShared() : SDLImageShared(createDefaultSurface().get()) {}
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::shared_ptr<SDL_Surface> createDefaultSurface()
|
||||||
|
{
|
||||||
|
auto surface = SDL_CreateRGBSurface(0, 32, 32, 32, 0, 0, 0, 0);
|
||||||
|
SDL_FillRect(surface, nullptr, SDL_MapRGB(surface->format, 255, 0, 0));
|
||||||
|
return std::shared_ptr<SDL_Surface>(surface, SDL_FreeSurface);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
class SDLImageBase : public IImage, boost::noncopyable
|
class SDLImageBase : public IImage, boost::noncopyable
|
||||||
{
|
{
|
||||||
protected:
|
protected:
|
||||||
|
@ -38,6 +38,7 @@ void SDLImageLoader::init(Point SpriteSize, Point Margins, Point FullSize, SDL_C
|
|||||||
SDL_FreePalette(p);
|
SDL_FreePalette(p);
|
||||||
|
|
||||||
SDL_LockSurface(image->surf);
|
SDL_LockSurface(image->surf);
|
||||||
|
|
||||||
lineStart = position = (ui8*)image->surf->pixels;
|
lineStart = position = (ui8*)image->surf->pixels;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -67,8 +68,11 @@ inline void SDLImageLoader::endLine()
|
|||||||
|
|
||||||
SDLImageLoader::~SDLImageLoader()
|
SDLImageLoader::~SDLImageLoader()
|
||||||
{
|
{
|
||||||
SDL_UnlockSurface(image->surf);
|
if(image->surf)
|
||||||
SDL_SetColorKey(image->surf, SDL_TRUE, 0);
|
{
|
||||||
//TODO: RLE if compressed and bpp>1
|
SDL_UnlockSurface(image->surf);
|
||||||
|
SDL_SetColorKey(image->surf, SDL_TRUE, 0);
|
||||||
|
//TODO: RLE if compressed and bpp>1
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user