1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-24 22:14:36 +02:00

further refactoring-related changes

This commit is contained in:
Zyx-2000 2016-01-29 15:16:14 +01:00
parent c3e8c8bc1c
commit 3a1a8e3d47
10 changed files with 79 additions and 93 deletions

View File

@ -70,12 +70,11 @@
<Add option="-Wno-unused-parameter" />
<Add option="-Wno-overloaded-virtual" />
<Add option="-fpermissive" />
<Add option="-isystem $(#boost.include)" />
<Add option="-DBOOST_THREAD_USE_LIB" />
<Add option="-DBOOST_SYSTEM_NO_DEPRECATED" />
<Add option="-D_WIN32_WINNT=0x0501" />
<Add option="-D_WIN32" />
<Add directory="$(#boost.include)" />
<Add directory="../include" />
<Add directory="../client" />
<Add directory="$(#ffmpeg.include)" />
<Add directory="$(#sdl2.include)" />

View File

@ -51,11 +51,11 @@ class CompImageLoader
ui8 *position;
ui8 *entry;
ui32 currentLine;
inline ui8 typeOf(ui8 color);
inline void NewEntry(ui8 color, size_t size);
inline void NewEntry(const ui8 * &data, size_t size);
public:
//load size raw pixels from data
inline void Load(size_t size, const ui8 * data);
@ -75,29 +75,26 @@ class CFileCache
static const int cacheSize = 50; //Max number of cached files
struct FileData
{
ResourceID name;
size_t size;
ui8 * data;
ResourceID name;
size_t size;
std::unique_ptr<ui8[]> data;
ui8 * getCopy()
std::unique_ptr<ui8[]> getCopy()
{
auto ret = new ui8[size];
std::copy(data, data + size, ret);
auto ret = std::unique_ptr<ui8[]>(new ui8[size]);
std::copy(data.get(), data.get() + size, ret.get());
return ret;
}
FileData():
size(0),
data(nullptr)
FileData(ResourceID name_, size_t size_, std::unique_ptr<ui8[]> data_):
name{std::move(name_)},
size{size_},
data{std::move(data_)}
{}
~FileData()
{
delete [] data;
}
};
std::list<FileData> cache;
std::deque<FileData> cache;
public:
ui8 * getCachedFile(ResourceID && rid)
std::unique_ptr<ui8[]> getCachedFile(ResourceID rid)
{
for(auto & file : cache)
{
@ -107,12 +104,10 @@ public:
// Still here? Cache miss
if (cache.size() > cacheSize)
cache.pop_front();
cache.push_back(FileData());
auto data = CResourceHandler::get()->load(rid)->readAll();
cache.back().name = ResourceID(rid);
cache.back().size = data.second;
cache.back().data = data.first.release();
cache.emplace_back(std::move(rid), data.second, std::move(data.first));
return cache.back().getCopy();
}
@ -142,15 +137,15 @@ CDefFile::CDefFile(std::string Name):
};
data = animationCache.getCachedFile(ResourceID(std::string("SPRITES/") + Name, EResType::ANIMATION));
palette = new SDL_Color[256];
palette = std::unique_ptr<SDL_Color[]>(new SDL_Color[256]);
int it = 0;
ui32 type = read_le_u32(data + it);
ui32 type = read_le_u32(data.get() + it);
it+=4;
//int width = read_le_u32(data + it); it+=4;//not used
//int height = read_le_u32(data + it); it+=4;
it+=8;
ui32 totalBlocks = read_le_u32(data + it);
ui32 totalBlocks = read_le_u32(data.get() + it);
it+=4;
for (ui32 i= 0; i<256; i++)
@ -161,15 +156,15 @@ CDefFile::CDefFile(std::string Name):
palette[i].a = SDL_ALPHA_OPAQUE;
}
if (type == 71 || type == 64)//Buttons/buildings don't have shadows\semi-transparency
memset(palette, 0, sizeof(SDL_Color)*2);
memset(palette.get(), 0, sizeof(SDL_Color)*2);
else
memcpy(palette, H3Palette, sizeof(SDL_Color)*8);//initialize shadow\selection colors
memcpy(palette.get(), H3Palette, sizeof(SDL_Color)*8);//initialize shadow\selection colors
for (ui32 i=0; i<totalBlocks; i++)
{
size_t blockID = read_le_u32(data + it);
size_t blockID = read_le_u32(data.get() + it);
it+=4;
size_t totalEntries = read_le_u32(data + it);
size_t totalEntries = read_le_u32(data.get() + it);
it+=12;
//8 unknown bytes - skipping
@ -178,7 +173,7 @@ CDefFile::CDefFile(std::string Name):
for (ui32 j=0; j<totalEntries; j++)
{
size_t currOffset = read_le_u32(data + it);
size_t currOffset = read_le_u32(data.get() + it);
offset[blockID].push_back(currOffset);
it += 4;
}
@ -192,7 +187,7 @@ void CDefFile::loadFrame(size_t frame, size_t group, ImageLoader &loader) const
it = offset.find(group);
assert (it != offset.end());
const ui8 * FDef = data+it->second[frame];
const ui8 * FDef = data.get()+it->second[frame];
const SSpriteDef sd = * reinterpret_cast<const SSpriteDef *>(FDef);
SSpriteDef sprite;
@ -210,7 +205,7 @@ void CDefFile::loadFrame(size_t frame, size_t group, ImageLoader &loader) const
loader.init(Point(sprite.width, sprite.height),
Point(sprite.leftMargin, sprite.topMargin),
Point(sprite.fullWidth, sprite.fullHeight), palette);
Point(sprite.fullWidth, sprite.fullHeight), palette.get());
switch (sprite.format)
{
@ -321,11 +316,7 @@ void CDefFile::loadFrame(size_t frame, size_t group, ImageLoader &loader) const
}
};
CDefFile::~CDefFile()
{
delete[] data;
delete[] palette;
}
CDefFile::~CDefFile() = default;
const std::map<size_t, size_t > CDefFile::getEntries() const
{
@ -355,10 +346,10 @@ void SDLImageLoader::init(Point SpriteSize, Point Margins, Point FullSize, SDL_C
image->fullSize = FullSize;
//Prepare surface
SDL_Palette * p = SDL_AllocPalette(256);
SDL_Palette * p = SDL_AllocPalette(256);
SDL_SetPaletteColors(p, pal, 0, 256);
SDL_SetSurfacePalette(image->surf, p);
SDL_FreePalette(p);
SDL_FreePalette(p);
SDL_LockSurface(image->surf);
lineStart = position = (ui8*)image->surf->pixels;
@ -396,14 +387,14 @@ SDLImageLoader::~SDLImageLoader()
}
////////////////////////////////////////////////////////////////////////////////
CompImageLoader::CompImageLoader(CompImage * Img):
image(Img),
position(nullptr),
entry(nullptr),
currentLine(0)
{
}
void CompImageLoader::init(Point SpriteSize, Point Margins, Point FullSize, SDL_Color *pal)
@ -451,7 +442,7 @@ inline ui8 CompImageLoader::typeOf(ui8 color)
if (image->palette[color].a != 255)
return 1;
return 2;
}
@ -489,7 +480,7 @@ inline void CompImageLoader::Load(size_t size, const ui8 * data)
ui8 type = typeOf(color);
ui8 color2;
ui8 type2;
if (size > 1)
{
do
@ -632,7 +623,7 @@ SDLImage::SDLImage(std::string filename, bool compressed):
{
CSDL_Ext::setColorKey(temp,temp->format->palette->colors[0]);
}
SDL_SetSurfaceRLE(temp, SDL_RLEACCEL);
SDL_SetSurfaceRLE(temp, SDL_RLEACCEL);
// convert surface to enable RLE
surf = SDL_ConvertSurface(temp, temp->format, temp->flags);
@ -680,7 +671,7 @@ CompImage::CompImage(const CDefFile *data, size_t frame, size_t group):
surf(nullptr),
line(nullptr),
palette(nullptr)
{
CompImageLoader loader(this);
data->loadFrame(frame, group, loader);
@ -738,7 +729,7 @@ void CompImage::draw(SDL_Surface *where, int posX, int posY, Rect *src, ui8 alph
currX = 0;
ui8 bpp = where->format->BytesPerPixel;
//Calculate position for blitting: pixels + Y + X
ui8* blitPos = (ui8*) where->pixels;
if (rotation & 4)
@ -769,7 +760,7 @@ void CompImage::draw(SDL_Surface *where, int posX, int posY, Rect *src, ui8 alph
void CompImage::BlitBlockWithBpp(ui8 bpp, ui8 type, ui8 size, ui8 *&data, ui8 *&dest, ui8 alpha, bool rotated) const
{
assert(bpp>1 && bpp<5);
if (rotated)
switch (bpp)
{
@ -817,7 +808,7 @@ void CompImage::BlitBlock(ui8 type, ui8 size, ui8 *&data, ui8 *&dest, ui8 alpha)
//Put row of RGBA data
for (size_t i=0; i<size; i++)
ColorPutter<bpp, 1>::PutColorAlpha(dest, palette[*(data++)]);
}
}
//RLE-d sequence
@ -831,7 +822,7 @@ void CompImage::BlitBlock(ui8 type, ui8 size, ui8 *&data, ui8 *&dest, ui8 alpha)
ColorPutter<bpp, 1>::PutColorAlpha(dest, col);
return;
}
switch (palette[type].a)
{
case 0:
@ -958,7 +949,7 @@ bool CAnimation::loadFrame(CDefFile * file, size_t frame, size_t group)
else //load from separate file
{
std::string filename = source[group][frame].Struct().find("file")->second.String();
IImage * img = getFromExtraDef(filename);
if (!img)
img = new SDLImage(filename, compressed);
@ -1183,7 +1174,7 @@ void CAnimation::getAnimInfo()
logGlobal->errorStream()<<"Animation stats: Loaded "<<loadedAnims.size()<<" total";
for (auto anim : loadedAnims)
{
logGlobal->errorStream()<<"Name: "<<anim->name<<" Groups: "<<anim->images.size();
if (!anim->images.empty())
logGlobal->errorStream()<<", "<<anim->images.begin()->second.size()<<" image loaded in group "<< anim->images.begin()->first;
@ -1202,12 +1193,12 @@ void CFadeAnimation::update()
{
if (!fading)
return;
if (fadingMode == EMode::OUT)
fadingCounter -= delta;
else
fadingCounter += delta;
if (isFinished())
{
fading = false;
@ -1236,7 +1227,7 @@ CFadeAnimation::CFadeAnimation()
CFadeAnimation::~CFadeAnimation()
{
if (fadingSurface && shouldFreeSurface)
SDL_FreeSurface(fadingSurface);
SDL_FreeSurface(fadingSurface);
}
void CFadeAnimation::init(EMode mode, SDL_Surface * sourceSurface, bool freeSurfaceAtEnd /* = false */, float animDelta /* = DEFAULT_DELTA */)
@ -1247,17 +1238,17 @@ void CFadeAnimation::init(EMode mode, SDL_Surface * sourceSurface, bool freeSurf
// (alternatively, we could just return here to ignore the new fade request until this one finished (but we'd need to free the passed bitmap to avoid leaks))
logGlobal->warnStream() << "Tried to init fading animation that is already running.";
if (fadingSurface && shouldFreeSurface)
SDL_FreeSurface(fadingSurface);
}
SDL_FreeSurface(fadingSurface);
}
if (animDelta <= 0.0f)
{
logGlobal->warnStream() << "Fade anim: delta should be positive; " << animDelta << " given.";
animDelta = DEFAULT_DELTA;
}
if (sourceSurface)
fadingSurface = sourceSurface;
delta = animDelta;
fadingMode = mode;
fadingCounter = initialCounter();
@ -1266,13 +1257,13 @@ void CFadeAnimation::init(EMode mode, SDL_Surface * sourceSurface, bool freeSurf
}
void CFadeAnimation::draw(SDL_Surface * targetSurface, const SDL_Rect * sourceRect, SDL_Rect * destRect)
{
{
if (!fading || !fadingSurface || fadingMode == EMode::NONE)
{
fading = false;
return;
}
CSDL_Ext::setAlpha(fadingSurface, fadingCounter * 255);
SDL_BlitSurface(fadingSurface, const_cast<SDL_Rect *>(sourceRect), targetSurface, destRect); //FIXME
CSDL_Ext::setAlpha(fadingSurface, 255);

View File

@ -39,8 +39,8 @@ private:
//offset[group][frame] - offset of frame data in file
std::map<size_t, std::vector <size_t> > offset;
ui8 * data;
SDL_Color * palette;
std::unique_ptr<ui8[]> data;
std::unique_ptr<SDL_Color[]> palette;
public:
CDefFile(std::string Name);
@ -236,7 +236,7 @@ private:
bool fading;
float fadingCounter;
bool shouldFreeSurface;
float initialCounter() const;
bool isFinished() const;
public:

View File

@ -9,17 +9,18 @@
<Target title="Debug Win32">
<Option output="../VCMI_launcher" prefix_auto="1" extension_auto="1" />
<Option working_dir="../" />
<Option object_output=".objs/debug" />
<Option object_output="../obj/Launcher/Debug" />
<Option type="0" />
<Option compiler="gcc" />
<Compiler>
<Add option="-g" />
<Add option="-Og" />
</Compiler>
</Target>
<Target title="Release Win32">
<Option output="../VCMI_launcher" prefix_auto="1" extension_auto="1" />
<Option working_dir="../" />
<Option object_output=".objs/release" />
<Option object_output="../obj/Launcher/Release" />
<Option type="0" />
<Option compiler="gcc" />
<Compiler>
@ -36,11 +37,10 @@
<Add option="-Wextra" />
<Add option="-Wall" />
<Add option="-std=gnu++11" />
<Add option="-isystem $(#boost.include)" />
<Add option="-DBOOST_SYSTEM_NO_DEPRECATED" />
<Add option="-D_WIN32" />
<Add directory="." />
<Add directory="../include" />
<Add directory="$(#boost.include)" />
<Add directory="$(#qt.include)" />
<Add directory="$(#qt.include)/QtGui" />
<Add directory="$(#qt.include)/QtCore" />

View File

@ -19,7 +19,6 @@
<Compiler>
<Add option="-g" />
<Add option="-Og" />
<Add directory="$(#zlib.include)" />
</Compiler>
<Linker>
<Add option="-lws2_32" />
@ -33,9 +32,6 @@
<Add option="-lboost_date_time$(#boost.libsuffix)" />
<Add option="-liconv" />
<Add option="-ldbghelp" />
<Add directory="$(#sdl2.lib)" />
<Add directory="$(#boost.lib32)" />
<Add directory="$(#zlib.lib)" />
</Linker>
</Target>
<Target title="Release-win32">
@ -48,7 +44,6 @@
<Compiler>
<Add option="-fomit-frame-pointer" />
<Add option="-O2" />
<Add directory="$(#zlib.include)" />
</Compiler>
<Linker>
<Add option="-s" />
@ -62,9 +57,6 @@
<Add option="-lboost_locale$(#boost.libsuffix)" />
<Add option="-lboost_date_time$(#boost.libsuffix)" />
<Add option="-liconv" />
<Add directory="$(#sdl2.lib)" />
<Add directory="$(#boost.lib32)" />
<Add directory="$(#zlib.lib)" />
</Linker>
</Target>
<Target title="Debug-win64">
@ -110,18 +102,21 @@
<Add option="-Wno-unused-parameter" />
<Add option="-Wno-overloaded-virtual" />
<Add option="-Wno-unused-local-typedefs" />
<Add option="-isystem $(#boost.include)" />
<Add option="-DVCMI_DLL" />
<Add option="-DBOOST_THREAD_USE_LIB" />
<Add option="-DBOOST_SYSTEM_NO_DEPRECATED" />
<Add option="-D_WIN32_WINNT=0x0501" />
<Add option="-D_WIN32" />
<Add directory="$(#boost.include)" />
<Add directory="../include" />
<Add directory="../lib" />
<Add directory="." />
<Add directory="$(#sdl2.include)" />
<Add directory="$(#zlib.include)" />
</Compiler>
<Linker>
<Add directory="../" />
<Add directory="$(#sdl2.lib)" />
<Add directory="$(#boost.lib32)" />
<Add directory="$(#zlib.lib)" />
</Linker>
<Unit filename="AI_Base.h" />
<Unit filename="BattleAction.cpp" />

View File

@ -8,7 +8,8 @@ CMappedFileLoader::CMappedFileLoader(const std::string & mountPoint, const JsonN
{
for(auto entry : config.Struct())
{
fileList[ResourceID(mountPoint + entry.first)] = ResourceID(mountPoint + entry.second.String());
//fileList[ResourceID(mountPoint + entry.first)] = ResourceID(mountPoint + entry.second.String());
fileList.emplace(ResourceID(mountPoint + entry.first), ResourceID(mountPoint + entry.second.String()));
}
}

View File

@ -46,10 +46,12 @@ static inline std::string readName(std::string name)
return name;
}
#if 0
ResourceID::ResourceID()
:type(EResType::OTHER)
{
}
#endif
ResourceID::ResourceID(std::string name_)
:type{readType(name_)},
@ -62,7 +64,7 @@ ResourceID::ResourceID(std::string name_, EResType::Type type_)
name{readName(std::move(name_))}
{
}
#if 0
std::string ResourceID::getName() const
{
return name;
@ -72,7 +74,7 @@ EResType::Type ResourceID::getType() const
{
return type;
}
#if 0
void ResourceID::setName(std::string name)
{
// setName shouldn't be used if type is UNDEFINED

View File

@ -70,7 +70,7 @@ public:
/**
* Default c-tor.
*/
ResourceID();
//ResourceID();
/**
* Ctor. Can be used to create identifier for resource loading using one parameter
@ -98,8 +98,8 @@ public:
return name == other.name && type == other.type;
}
std::string getName() const;
EResType::Type getType() const;
std::string getName() const {return name;}
EResType::Type getType() const {return type;}
//void setName(std::string name);
//void setType(EResType::Type type);

View File

@ -15,9 +15,8 @@
<Option compiler="gcc" />
<Option use_console_runner="0" />
<Compiler>
<Add option="-g" />
<Add option="-Og" />
<Add option="-ggdb" />
<Add directory="$(#zlib.include)" />
</Compiler>
<Linker>
<Add directory="$(#boost.lib32)" />
@ -34,7 +33,6 @@
<Compiler>
<Add option="-fomit-frame-pointer" />
<Add option="-O2" />
<Add directory="$(#zlib.include)" />
</Compiler>
<Linker>
<Add option="-s" />
@ -69,13 +67,13 @@
<Add option="-Wno-sign-compare" />
<Add option="-Wno-unused-parameter" />
<Add option="-Wno-overloaded-virtual" />
<Add option="-isystem $(#boost.include)" />
<Add option="-D_WIN32_WINNT=0x0501" />
<Add option="-DBOOST_THREAD_USE_LIB" />
<Add option="-DBOOST_SYSTEM_NO_DEPRECATED" />
<Add option="-D_WIN32" />
<Add directory="$(#boost.include)" />
<Add directory="../include" />
<Add directory="$(#sdl2.include)" />
<Add directory="$(#zlib.include)" />
</Compiler>
<Linker>
<Add option="-lole32" />

View File

@ -6,8 +6,8 @@
<Depends filename="lib/minizip/minizip.cbp" />
</Project>
<Project filename="client/VCMI_client.cbp">
<Depends filename="server/VCMI_server.cbp" />
<Depends filename="lib/VCMI_lib.cbp" />
<Depends filename="server/VCMI_server.cbp" />
</Project>
<Project filename="server/VCMI_server.cbp">
<Depends filename="lib/VCMI_lib.cbp" />
@ -27,13 +27,13 @@
<Depends filename="lib/VCMI_lib.cbp" />
</Project>
<Project filename="test/Test.cbp">
<Depends filename="server/VCMI_server.cbp" />
<Depends filename="AI/EmptyAI/EmptyAI.cbp" />
<Depends filename="AI/VCAI/VCAI.cbp" />
<Depends filename="AI/StupidAI/StupidAI.cbp" />
<Depends filename="AI/BattleAI/BattleAI.cbp" />
<Depends filename="lib/VCMI_lib.cbp" />
<Depends filename="client/VCMI_client.cbp" />
<Depends filename="server/VCMI_server.cbp" />
<Depends filename="AI/VCAI/VCAI.cbp" />
</Project>
<Project filename="scripting/erm/ERM.cbp">
<Depends filename="lib/VCMI_lib.cbp" />