1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-07-05 00:49:09 +02:00
Files
vcmi/client/Gfx/Manager.cpp
paracelsus d96e7cae4a Error handling in GL2D::makeShaderProgram,
Gfx::PlettedBitmap class correction
2013-03-04 19:43:38 +00:00

92 lines
2.3 KiB
C++

#include "StdInc.h"
#include <map>
#include "Manager.h"
#include "Animations.h"
#include "Images.h"
#include "FilesHeaders.h"
#include "../../lib/Filesystem/CResourceLoader.h"
#include "../../lib/Filesystem/CFileInfo.h"
namespace Gfx
{
class Resources
{
friend class CManager;
static std::map<std::string, PImage> images;
static std::map<std::string, PAnimation> anims;
};
std::map<std::string, PImage> Resources::images;
std::map<std::string, PAnimation> Resources::anims;
PImage CManager::getImage(const std::string& fname)
{
PImage& img_ptr = Resources::images[fname];
if (img_ptr) return img_ptr;
ResourceID resImageId("DATA/" + fname, EResType::IMAGE);
auto stream = CResourceHandler::get()->load(resImageId);
auto streamSize = stream->getSize();
if (streamSize < H3PCX_HEADER_SIZE) return nullptr;
if (streamSize > 0x10000000) streamSize = 0x10000000;
std::unique_ptr<ui8[]> data(new ui8[(size_t)streamSize]);
auto readSize = stream->read(data.get(), streamSize);
assert(readSize == stream->getSize());
CImage* img_tmp = CImage::makeFromPCX(*(SH3PcxFile*)data.get(), (size_t)readSize);
if (img_tmp == nullptr)
{
CFileInfo info(CResourceHandler::get()->getResourceName(resImageId));
img_tmp = CImage::makeBySDL(data.get(), (size_t)readSize, info.getExtension().c_str()+1);
if (img_tmp == nullptr)
{
tlog1 << "Gfx: Image " << fname << " not loaded!\n";
return nullptr;
}
}
return img_ptr = PImage(img_tmp);
}
PAnimation CManager::getAnimation(const std::string& fname)
{
PAnimation& anim_ptr = Resources::anims[fname];
if (anim_ptr) return anim_ptr;
ResourceID resAnimId("SPRITES/" + fname, EResType::ANIMATION);
auto stream = CResourceHandler::get()->load(resAnimId);
auto streamSize = stream->getSize();
if (streamSize < H3DEF_HEADER_SIZE) return nullptr;
if (streamSize > 0x7FFFFFF0) streamSize = 0x7FFFFFF0;
std::unique_ptr<ui8[]> data(new ui8[(size_t)streamSize]);
auto readSize = stream->read(data.get(), streamSize);
assert(readSize == stream->getSize());
CAnimation* anim_tmp = CAnimation::makeFromDEF(*(SH3DefFile*)data.get(), (size_t)readSize);
if (anim_tmp == nullptr)
{
tlog1 << "Gfx: Animation " << fname << " not loaded!\n";
return nullptr;
}
return anim_ptr = PAnimation(anim_tmp);
}
}