From 3a1a8e3d472c23c467cdb99f089ac2abc5661717 Mon Sep 17 00:00:00 2001 From: Zyx-2000 Date: Fri, 29 Jan 2016 15:16:14 +0100 Subject: [PATCH] further refactoring-related changes --- client/VCMI_client.cbp | 3 +- client/gui/CAnimation.cpp | 109 ++++++++++++++---------------- client/gui/CAnimation.h | 6 +- launcher/VCMI_launcher.cbp | 8 +-- lib/VCMI_lib.cbp | 17 ++--- lib/filesystem/AdapterLoaders.cpp | 3 +- lib/filesystem/ResourceID.cpp | 6 +- lib/filesystem/ResourceID.h | 6 +- server/VCMI_server.cbp | 8 +-- vcmi.workspace | 6 +- 10 files changed, 79 insertions(+), 93 deletions(-) diff --git a/client/VCMI_client.cbp b/client/VCMI_client.cbp index dff43308e..69fbe9f73 100644 --- a/client/VCMI_client.cbp +++ b/client/VCMI_client.cbp @@ -70,12 +70,11 @@ + - - diff --git a/client/gui/CAnimation.cpp b/client/gui/CAnimation.cpp index 6ce5075ed..881a76f7a 100644 --- a/client/gui/CAnimation.cpp +++ b/client/gui/CAnimation.cpp @@ -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 data; - ui8 * getCopy() + std::unique_ptr getCopy() { - auto ret = new ui8[size]; - std::copy(data, data + size, ret); + auto ret = std::unique_ptr(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 data_): + name{std::move(name_)}, + size{size_}, + data{std::move(data_)} {} - ~FileData() - { - delete [] data; - } }; - std::list cache; + std::deque cache; public: - ui8 * getCachedFile(ResourceID && rid) + std::unique_ptr 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(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; isecond[frame]; + const ui8 * FDef = data.get()+it->second[frame]; const SSpriteDef sd = * reinterpret_cast(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 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::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::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 "<errorStream()<<"Name: "<name<<" Groups: "<images.size(); if (!anim->images.empty()) logGlobal->errorStream()<<", "<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(sourceRect), targetSurface, destRect); //FIXME CSDL_Ext::setAlpha(fadingSurface, 255); diff --git a/client/gui/CAnimation.h b/client/gui/CAnimation.h index 4897ac092..329d31206 100644 --- a/client/gui/CAnimation.h +++ b/client/gui/CAnimation.h @@ -39,8 +39,8 @@ private: //offset[group][frame] - offset of frame data in file std::map > offset; - ui8 * data; - SDL_Color * palette; + std::unique_ptr data; + std::unique_ptr palette; public: CDefFile(std::string Name); @@ -236,7 +236,7 @@ private: bool fading; float fadingCounter; bool shouldFreeSurface; - + float initialCounter() const; bool isFinished() const; public: diff --git a/launcher/VCMI_launcher.cbp b/launcher/VCMI_launcher.cbp index 0aba3f0f0..973802006 100644 --- a/launcher/VCMI_launcher.cbp +++ b/launcher/VCMI_launcher.cbp @@ -9,17 +9,18 @@ @@ -48,7 +44,6 @@ - @@ -62,9 +57,6 @@ - - - @@ -110,18 +102,21 @@ + - - - + + + + + diff --git a/lib/filesystem/AdapterLoaders.cpp b/lib/filesystem/AdapterLoaders.cpp index 26193b228..6eebbd20b 100644 --- a/lib/filesystem/AdapterLoaders.cpp +++ b/lib/filesystem/AdapterLoaders.cpp @@ -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())); } } diff --git a/lib/filesystem/ResourceID.cpp b/lib/filesystem/ResourceID.cpp index 129b2d1e4..c0fd3e134 100644 --- a/lib/filesystem/ResourceID.cpp +++ b/lib/filesystem/ResourceID.cpp @@ -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 diff --git a/lib/filesystem/ResourceID.h b/lib/filesystem/ResourceID.h index 84ee90d58..93b0ad6e6 100644 --- a/lib/filesystem/ResourceID.h +++ b/lib/filesystem/ResourceID.h @@ -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); diff --git a/server/VCMI_server.cbp b/server/VCMI_server.cbp index c13d7031f..0fd71f736 100644 --- a/server/VCMI_server.cbp +++ b/server/VCMI_server.cbp @@ -15,9 +15,8 @@