From 70e3c81b9f5bb3250b70e7e0db2370de607f4338 Mon Sep 17 00:00:00 2001 From: Zyx-2000 Date: Tue, 26 Jan 2016 14:51:38 +0100 Subject: [PATCH 01/14] Replaced CFileInfo with more correct and efficient alternative --- client/CBitmapHandler.cpp | 1 - client/CPreGame.cpp | 1 - client/NetPacksClient.cpp | 12 +++---- lib/VCMIDirs.cpp | 22 ++++++------ lib/VCMI_lib.cbp | 2 ++ lib/filesystem/AdapterLoaders.h | 1 - lib/filesystem/CArchiveLoader.cpp | 7 ++-- lib/filesystem/CArchiveLoader.h | 1 - lib/filesystem/CFileInputStream.cpp | 5 --- lib/filesystem/CFileInputStream.h | 9 ----- lib/filesystem/CFilesystemLoader.cpp | 4 +-- lib/filesystem/CFilesystemLoader.h | 1 - lib/filesystem/CZipLoader.cpp | 2 +- lib/filesystem/Filesystem.cpp | 2 -- lib/filesystem/ResourceID.cpp | 53 ++++++++++++++++------------ lib/filesystem/ResourceID.h | 10 +++--- server/CGameHandler.cpp | 12 +++---- 17 files changed, 65 insertions(+), 80 deletions(-) diff --git a/client/CBitmapHandler.cpp b/client/CBitmapHandler.cpp index 5fb8a4388..c5d53d29b 100644 --- a/client/CBitmapHandler.cpp +++ b/client/CBitmapHandler.cpp @@ -1,7 +1,6 @@ #include "StdInc.h" #include "../lib/filesystem/Filesystem.h" -#include "../lib/filesystem/CFileInfo.h" #include "SDL.h" #include "SDL_image.h" #include "CBitmapHandler.h" diff --git a/client/CPreGame.cpp b/client/CPreGame.cpp index d59157aeb..ff00530bc 100644 --- a/client/CPreGame.cpp +++ b/client/CPreGame.cpp @@ -2,7 +2,6 @@ #include "CPreGame.h" #include "../lib/filesystem/Filesystem.h" -#include "../lib/filesystem/CFileInfo.h" #include "../lib/filesystem/CCompressedStream.h" #include "../lib/CStopWatch.h" diff --git a/client/NetPacksClient.cpp b/client/NetPacksClient.cpp index b8382868f..58a52e2ca 100644 --- a/client/NetPacksClient.cpp +++ b/client/NetPacksClient.cpp @@ -2,7 +2,7 @@ #include "../lib/NetPacks.h" #include "../lib/filesystem/Filesystem.h" -#include "../lib/filesystem/CFileInfo.h" +#include "../lib/filesystem/FileInfo.h" #include "../CCallback.h" #include "Client.h" #include "CPlayerInterface.h" @@ -636,8 +636,8 @@ void BattleSetActiveStack::applyCl( CClient *cl ) PlayerColor playerToCall; //player that will move activated stack if( activated->hasBonusOfType(Bonus::HYPNOTIZED) ) { - playerToCall = ( GS(cl)->curB->sides[0].color == activated->owner - ? GS(cl)->curB->sides[1].color + playerToCall = ( GS(cl)->curB->sides[0].color == activated->owner + ? GS(cl)->curB->sides[1].color : GS(cl)->curB->sides[0].color ); } else @@ -803,12 +803,12 @@ void YourTurn::applyCl( CClient *cl ) void SaveGame::applyCl(CClient *cl) { - CFileInfo info(fname); - CResourceHandler::get("local")->createResource(info.getStem() + ".vcgm1"); + const auto stem = FileInfo::GetPathStem(fname); + CResourceHandler::get("local")->createResource(stem.to_string() + ".vcgm1"); try { - CSaveFile save(*CResourceHandler::get()->getResourceName(ResourceID(info.getStem(), EResType::CLIENT_SAVEGAME))); + CSaveFile save(*CResourceHandler::get()->getResourceName(ResourceID(stem.to_string(), EResType::CLIENT_SAVEGAME))); cl->saveCommonState(save); save << *cl; } diff --git a/lib/VCMIDirs.cpp b/lib/VCMIDirs.cpp index 7c2110afa..a2148a60b 100644 --- a/lib/VCMIDirs.cpp +++ b/lib/VCMIDirs.cpp @@ -31,7 +31,7 @@ void IVCMIDirs::init() #ifndef CSIDL_MYDOCUMENTS #define CSIDL_MYDOCUMENTS CSIDL_PERSONAL - #endif + #endif #endif // __MINGW32__ #include @@ -87,7 +87,7 @@ bool StartBatchCopyDataProgram( "%5%" "\n" "del \"%%~f0\"&exit" "\n" // Script deletes itself ; - + const auto startGameString = bfs::equivalent(currentPath, from) ? (boost::format("start \"\" %1%") % (to / exeName)) : // Start game in new path. @@ -107,7 +107,7 @@ bool StartBatchCopyDataProgram( return true; } -class VCMIDirsWIN32 : public IVCMIDirs +class VCMIDirsWIN32 final : public IVCMIDirs { public: boost::filesystem::path userDataPath() const override; @@ -166,7 +166,7 @@ void VCMIDirsWIN32::init() { const std::wstring& pathStr = path.native(); std::unique_ptr result(new wchar_t[pathStr.length() + 2]); - + size_t i = 0; for (const wchar_t ch : pathStr) result[i++] = ch; @@ -195,7 +195,7 @@ void VCMIDirsWIN32::init() return false; else if (!bfs::is_empty(from)) // TODO: Log warn. Some files not moved. User should try to move files. return false; - + if (bfs::current_path() == from) bfs::current_path(to); @@ -203,7 +203,7 @@ void VCMIDirsWIN32::init() bfs::remove(from); return true; }; - + // Retrieves the fully qualified path for the file that contains the specified module. // The module must have been loaded by the current process. // If this parameter is nullptr, retrieves the path of the executable file of the current process. @@ -247,7 +247,7 @@ void VCMIDirsWIN32::init() // Start copying script and exit program. if (StartBatchCopyDataProgram(from, to, executableName)) exit(ERROR_SUCCESS); - + // Everything failed :C return false; }; @@ -262,14 +262,14 @@ bfs::path VCMIDirsWIN32::userDataPath() const if (SHGetSpecialFolderPathW(nullptr, profileDir, CSIDL_MYDOCUMENTS, FALSE) != FALSE) return bfs::path(profileDir) / "My Games\\vcmi"; - + return "."; } bfs::path VCMIDirsWIN32::oldUserDataPath() const { wchar_t profileDir[MAX_PATH]; - + if (SHGetSpecialFolderPathW(nullptr, profileDir, CSIDL_PROFILE, FALSE) == FALSE) // WinAPI way failed { #if defined(_MSC_VER) && _MSC_VER >= 1700 @@ -284,7 +284,7 @@ bfs::path VCMIDirsWIN32::oldUserDataPath() const } #else const char* profileDirA; - if (profileDirA = std::getenv("userprofile")) // STL way succeed + if ((profileDirA = std::getenv("userprofile"))) // STL way succeed return bfs::path(profileDirA) / "vcmi"; #endif else @@ -365,7 +365,7 @@ std::string IVCMIDirsUNIX::genHelpString() const } #ifdef VCMI_APPLE -class VCMIDirsOSX : public IVCMIDirsUNIX +class VCMIDirsOSX final : public IVCMIDirsUNIX { public: boost::filesystem::path userDataPath() const override; diff --git a/lib/VCMI_lib.cbp b/lib/VCMI_lib.cbp index da84a74df..2717c7682 100644 --- a/lib/VCMI_lib.cbp +++ b/lib/VCMI_lib.cbp @@ -229,6 +229,8 @@ + + diff --git a/lib/filesystem/AdapterLoaders.h b/lib/filesystem/AdapterLoaders.h index ca3058097..2a9b852fa 100644 --- a/lib/filesystem/AdapterLoaders.h +++ b/lib/filesystem/AdapterLoaders.h @@ -13,7 +13,6 @@ #include "ISimpleResourceLoader.h" #include "ResourceID.h" -class CFileInfo; class CInputStream; class JsonNode; diff --git a/lib/filesystem/CArchiveLoader.cpp b/lib/filesystem/CArchiveLoader.cpp index fdebd315a..d572eb610 100644 --- a/lib/filesystem/CArchiveLoader.cpp +++ b/lib/filesystem/CArchiveLoader.cpp @@ -5,7 +5,6 @@ #include "CCompressedStream.h" #include "CBinaryReader.h" -#include "CFileInfo.h" ArchiveEntry::ArchiveEntry() : offset(0), fullSize(0), compressedSize(0) @@ -140,13 +139,13 @@ std::unique_ptr CArchiveLoader::load(const ResourceID & resourceNa if (entry.compressedSize != 0) //compressed data { - std::unique_ptr fileStream(new CFileInputStream(archive, entry.offset, entry.compressedSize)); + auto fileStream = make_unique(archive, entry.offset, entry.compressedSize); - return std::unique_ptr(new CCompressedStream(std::move(fileStream), false, entry.fullSize)); + return make_unique(std::move(fileStream), false, entry.fullSize); } else { - return std::unique_ptr(new CFileInputStream(archive, entry.offset, entry.fullSize)); + return make_unique(archive, entry.offset, entry.fullSize); } } diff --git a/lib/filesystem/CArchiveLoader.h b/lib/filesystem/CArchiveLoader.h index 90643a148..936645928 100644 --- a/lib/filesystem/CArchiveLoader.h +++ b/lib/filesystem/CArchiveLoader.h @@ -13,7 +13,6 @@ #include "ISimpleResourceLoader.h" #include "ResourceID.h" -class CFileInfo; class CFileInputStream; /** diff --git a/lib/filesystem/CFileInputStream.cpp b/lib/filesystem/CFileInputStream.cpp index dcec9e76a..3d514d9cb 100644 --- a/lib/filesystem/CFileInputStream.cpp +++ b/lib/filesystem/CFileInputStream.cpp @@ -1,8 +1,6 @@ #include "StdInc.h" #include "CFileInputStream.h" -#include "CFileInfo.h" - CFileInputStream::CFileInputStream(const boost::filesystem::path & file, si64 start, si64 size) : dataStart{start}, dataSize{size}, @@ -20,9 +18,6 @@ CFileInputStream::CFileInputStream(const boost::filesystem::path & file, si64 st fileStream.seekg(dataStart, std::ios::beg); } -CFileInputStream::CFileInputStream(const CFileInfo & file, si64 start, si64 size) - : CFileInputStream{file.getName(), start, size} {} - si64 CFileInputStream::read(ui8 * data, si64 size) { si64 origin = tell(); diff --git a/lib/filesystem/CFileInputStream.h b/lib/filesystem/CFileInputStream.h index 09c167789..99a0cde0e 100644 --- a/lib/filesystem/CFileInputStream.h +++ b/lib/filesystem/CFileInputStream.h @@ -13,8 +13,6 @@ #include "CInputStream.h" #include "FileStream.h" -class CFileInfo; - /** * A class which provides method definitions for reading a file from the filesystem. */ @@ -32,13 +30,6 @@ public: */ CFileInputStream(const boost::filesystem::path & file, si64 start = 0, si64 size = 0); - /** - * C-tor. Opens the specified file. - * - * @see CFileInputStream::CFileInputStream(const boost::filesystem::path &, si64, si64) - */ - CFileInputStream(const CFileInfo & file, si64 start=0, si64 size=0); - /** * Reads n bytes from the stream into the data buffer. * diff --git a/lib/filesystem/CFilesystemLoader.cpp b/lib/filesystem/CFilesystemLoader.cpp index 5746cab70..28b60c471 100644 --- a/lib/filesystem/CFilesystemLoader.cpp +++ b/lib/filesystem/CFilesystemLoader.cpp @@ -1,7 +1,6 @@ #include "StdInc.h" #include "CFilesystemLoader.h" -#include "CFileInfo.h" #include "CFileInputStream.h" #include "FileStream.h" @@ -19,8 +18,7 @@ std::unique_ptr CFilesystemLoader::load(const ResourceID & resourc { assert(fileList.count(resourceName)); - std::unique_ptr stream(new CFileInputStream(baseDirectory / fileList.at(resourceName))); - return stream; + return make_unique(baseDirectory / fileList.at(resourceName)); } bool CFilesystemLoader::existsResource(const ResourceID & resourceName) const diff --git a/lib/filesystem/CFilesystemLoader.h b/lib/filesystem/CFilesystemLoader.h index f5573986b..01cd2286f 100644 --- a/lib/filesystem/CFilesystemLoader.h +++ b/lib/filesystem/CFilesystemLoader.h @@ -13,7 +13,6 @@ #include "ISimpleResourceLoader.h" #include "ResourceID.h" -class CFileInfo; class CInputStream; /** diff --git a/lib/filesystem/CZipLoader.cpp b/lib/filesystem/CZipLoader.cpp index 4880f7a76..efb213b3b 100644 --- a/lib/filesystem/CZipLoader.cpp +++ b/lib/filesystem/CZipLoader.cpp @@ -86,7 +86,7 @@ std::unordered_map CZipLoader::listFiles(const std:: std::unique_ptr CZipLoader::load(const ResourceID & resourceName) const { - return std::unique_ptr(new CZipStream(archiveName, files.at(resourceName))); + return make_unique(archiveName, files.at(resourceName)); } bool CZipLoader::existsResource(const ResourceID & resourceName) const diff --git a/lib/filesystem/Filesystem.cpp b/lib/filesystem/Filesystem.cpp index c9c9790e5..502bd253f 100644 --- a/lib/filesystem/Filesystem.cpp +++ b/lib/filesystem/Filesystem.cpp @@ -1,8 +1,6 @@ #include "StdInc.h" #include "Filesystem.h" -#include "CFileInfo.h" - #include "CArchiveLoader.h" #include "CFilesystemLoader.h" #include "AdapterLoaders.h" diff --git a/lib/filesystem/ResourceID.cpp b/lib/filesystem/ResourceID.cpp index 79e5498b2..129b2d1e4 100644 --- a/lib/filesystem/ResourceID.cpp +++ b/lib/filesystem/ResourceID.cpp @@ -1,7 +1,6 @@ #include "StdInc.h" #include "ResourceID.h" - -#include "CFileInfo.h" +#include "FileInfo.h" // trivial to_upper that completely ignores localization and only work with ASCII // Technically not a problem since @@ -23,28 +22,45 @@ static inline void toUpper(std::string & string) for (char & symbol : string) toUpper(symbol); } - +#else +static inline void toUpper(std::string & string) +{ + boost::to_upper(string); +} #endif +static inline EResType::Type readType(const std::string& name) +{ + return EResTypeHelper::getTypeFromExtension(FileInfo::GetExtension(name).to_string()); +} + +static inline std::string readName(std::string name) +{ + const auto dotPos = name.find_last_of('.'); + + if (dotPos != std::string::npos) + name.resize(dotPos); + + toUpper(name); + + return name; +} ResourceID::ResourceID() :type(EResType::OTHER) { } -ResourceID::ResourceID(std::string name) - :type(EResType::UNDEFINED) +ResourceID::ResourceID(std::string name_) + :type{readType(name_)}, + name{readName(std::move(name_))} { - CFileInfo info(std::move(name)); - setType(info.getType()); - setName(info.getStem()); } -ResourceID::ResourceID(std::string name, EResType::Type type) - :type(EResType::UNDEFINED) +ResourceID::ResourceID(std::string name_, EResType::Type type_) + :type{type_}, + name{readName(std::move(name_))} { - setType(type); - setName(std::move(name)); } std::string ResourceID::getName() const @@ -56,7 +72,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 @@ -72,26 +88,17 @@ void ResourceID::setName(std::string name) this->name.erase(dotPos); } -#ifdef ENABLE_TRIVIAL_TOUPPER toUpper(this->name); -#else - // strangely enough but this line takes 40-50% of filesystem loading time - boost::to_upper(this->name); -#endif } void ResourceID::setType(EResType::Type type) { this->type = type; } - +#endif EResType::Type EResTypeHelper::getTypeFromExtension(std::string extension) { -#ifdef ENABLE_TRIVIAL_TOUPPER toUpper(extension); -#else - boost::to_upper(extension); -#endif static const std::map stringToRes = { diff --git a/lib/filesystem/ResourceID.h b/lib/filesystem/ResourceID.h index 8b1c31798..84ee90d58 100644 --- a/lib/filesystem/ResourceID.h +++ b/lib/filesystem/ResourceID.h @@ -100,18 +100,18 @@ public: std::string getName() const; EResType::Type getType() const; - void setName(std::string name); - void setType(EResType::Type type); + //void setName(std::string name); + //void setType(EResType::Type type); private: - /** Specifies the resource name. No extension so .pcx and .png can override each other, always in upper case. **/ - std::string name; - /** * Specifies the resource type. EResType::OTHER if not initialized. * Required to prevent conflicts if files with different types (e.g. text and image) have the same name. */ EResType::Type type; + + /** Specifies the resource name. No extension so .pcx and .png can override each other, always in upper case. **/ + std::string name; }; namespace std diff --git a/server/CGameHandler.cpp b/server/CGameHandler.cpp index 1be4bd9e1..7d947bfee 100644 --- a/server/CGameHandler.cpp +++ b/server/CGameHandler.cpp @@ -1,7 +1,7 @@ #include "StdInc.h" #include "../lib/filesystem/Filesystem.h" -#include "../lib/filesystem/CFileInfo.h" +#include "../lib/filesystem/FileInfo.h" #include "../lib/int3.h" #include "../lib/mapping/CCampaignHandler.h" #include "../lib/StartInfo.h" @@ -2362,13 +2362,13 @@ void CGameHandler::sendAndApply( NewStructures * info ) void CGameHandler::save(const std::string & filename ) { logGlobal->infoStream() << "Saving to " << filename; - CFileInfo info(filename); - //CResourceHandler::get("local")->createResource(info.getStem() + ".vlgm1"); - CResourceHandler::get("local")->createResource(info.getStem() + ".vsgm1"); + const auto stem = FileInfo::GetPathStem(filename); + const auto savefname = stem.to_string() + ".vsgm1"; + CResourceHandler::get("local")->createResource(savefname); { logGlobal->infoStream() << "Ordering clients to serialize..."; - SaveGame sg(info.getStem() + ".vcgm1"); + SaveGame sg(savefname); sendToAllClients(&sg); } @@ -2383,7 +2383,7 @@ void CGameHandler::save(const std::string & filename ) // } { - CSaveFile save(*CResourceHandler::get("local")->getResourceName(ResourceID(info.getStem(), EResType::SERVER_SAVEGAME))); + CSaveFile save(*CResourceHandler::get("local")->getResourceName(ResourceID(stem.to_string(), EResType::SERVER_SAVEGAME))); saveCommonState(save); logGlobal->infoStream() << "Saving server state"; save << *this; From 3a1a8e3d472c23c467cdb99f089ac2abc5661717 Mon Sep 17 00:00:00 2001 From: Zyx-2000 Date: Fri, 29 Jan 2016 15:16:14 +0100 Subject: [PATCH 02/14] 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 @@ @@ -57,6 +58,7 @@ + @@ -115,7 +117,6 @@ - From 523bc22a30b1cc418a488c85d38bfec47d351611 Mon Sep 17 00:00:00 2001 From: Zyx-2000 Date: Fri, 29 Jan 2016 15:32:37 +0100 Subject: [PATCH 04/14] removed old files --- lib/filesystem/CFileInfo.cpp | 96 ----------------------------- lib/filesystem/CFileInfo.h | 113 ----------------------------------- 2 files changed, 209 deletions(-) delete mode 100644 lib/filesystem/CFileInfo.cpp delete mode 100644 lib/filesystem/CFileInfo.h diff --git a/lib/filesystem/CFileInfo.cpp b/lib/filesystem/CFileInfo.cpp deleted file mode 100644 index c2347e897..000000000 --- a/lib/filesystem/CFileInfo.cpp +++ /dev/null @@ -1,96 +0,0 @@ -#include "StdInc.h" -#include "CFileInfo.h" - -CFileInfo::CFileInfo() : name("") -{ - -} - -CFileInfo::CFileInfo(std::string name) - : name(std::move(name)) -{ - -} - -bool CFileInfo::exists() const -{ - return boost::filesystem::exists(name); -} - -bool CFileInfo::isDirectory() const -{ - return boost::filesystem::is_directory(name); -} - -void CFileInfo::setName(const std::string & name) -{ - this->name = name; -} - -std::string CFileInfo::getName() const -{ - return name; -} - -std::string CFileInfo::getPath() const -{ - size_t found = name.find_last_of("/\\"); - return name.substr(0, found); -} - -std::string CFileInfo::getExtension() const -{ - // Get position of file extension dot - size_t dotPos = name.find_last_of('.'); - - if(dotPos != std::string::npos) - return name.substr(dotPos); - - return ""; -} - -std::string CFileInfo::getFilename() const -{ - const size_t found = name.find_last_of("/\\"); - return name.substr(found + 1); -} - -std::string CFileInfo::getStem() const -{ - std::string rslt = name; - - // Remove file extension - const size_t dotPos = name.find_last_of('.'); - - if(dotPos != std::string::npos) - rslt.erase(dotPos); - - return rslt; -} - -std::string CFileInfo::getBaseName() const -{ - size_t begin = name.find_last_of("/\\"); - size_t end = name.find_last_of("."); - - if(begin == std::string::npos) - begin = 0; - else - ++begin; - - if (end < begin) - end = std::string::npos; - - size_t len = (end == std::string::npos ? std::string::npos : end - begin); - return name.substr(begin, len); -} - -EResType::Type CFileInfo::getType() const -{ - return EResTypeHelper::getTypeFromExtension(getExtension()); -} - -std::time_t CFileInfo::getDate() const -{ - return boost::filesystem::last_write_time(name); -} diff --git a/lib/filesystem/CFileInfo.h b/lib/filesystem/CFileInfo.h deleted file mode 100644 index 8753eebac..000000000 --- a/lib/filesystem/CFileInfo.h +++ /dev/null @@ -1,113 +0,0 @@ -#pragma once - -/* - * CFileInfo.h, part of VCMI engine - * - * Authors: listed in file AUTHORS in main folder - * - * License: GNU General Public License v2.0 or later - * Full text of license available in license.txt file, in main folder - * - */ - -#include "ResourceID.h" - -/** - * A class which holds information about a file. - */ -class DLL_LINKAGE CFileInfo -{ -public: - /** - * Default ctor. - */ - CFileInfo(); - - /** - * Ctor. - * - * @param name The path and name of the file. - */ - explicit CFileInfo(std::string name); - - /** - * Checks if the file exists. - * - * @return true if the file exists, false if not. - */ - bool exists() const; - - /** - * Checks if the file is a directory. - * - * @return true if the file is a directory, false if not. - */ - bool isDirectory() const; - - /** - * Sets the name. - * - * @param name The name of the file - */ - void setName(const std::string & name); - - /** - * Gets the name of the file. - * - * @return the path and name of the file. E.g. ./dir/file.ext - */ - std::string getName() const; - - /** - * Gets the path to the file only. - * - * @return the path to the file only. E.g. ./dir/ - */ - std::string getPath() const; - - /** - * Gets the file extension. - * - * @return the file extension. E.g. .ext - */ - std::string getExtension() const; - - /** - * Returns the name of the file. - * - * @return the name of the file. E.g. foo.txt - */ - std::string getFilename() const; - - /** - * Gets the file name + path exclusive the extension of the file. - * - * @return the file name exclusive the extension of the file. E.g. ./dir/foo - */ - std::string getStem() const; - - /** - * Gets the file name exclusive the extension of the file. - * - * @return the file name exclusive the extension and a path of the file. E.g. foo - */ - std::string getBaseName() const; - - /** - * Gets the extension type as a EResType enumeration. - * - * @return the extension type as a EResType enumeration. - */ - EResType::Type getType() const; - - /** - * Gets the timestamp of the file. - * - * @return the timestamp of the file, 0 if no timestamp was set - */ - std::time_t getDate() const; - -private: - /** Contains the original URI(not modified) e.g. ./dir/foo.txt */ - std::string name; -}; From 29faa9ce063632f1c5aa6ac56669085632c7f9f6 Mon Sep 17 00:00:00 2001 From: Zyx-2000 Date: Fri, 29 Jan 2016 15:33:07 +0100 Subject: [PATCH 05/14] ...and project updated --- lib/VCMI_lib.cbp | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/VCMI_lib.cbp b/lib/VCMI_lib.cbp index 15a21c34e..e49996ada 100644 --- a/lib/VCMI_lib.cbp +++ b/lib/VCMI_lib.cbp @@ -213,8 +213,6 @@ - - From 2b88de8e249ba3f275c12906c9722cc1a16579d1 Mon Sep 17 00:00:00 2001 From: Zyx-2000 Date: Fri, 29 Jan 2016 15:39:44 +0100 Subject: [PATCH 06/14] added new files --- lib/filesystem/FileInfo.cpp | 56 +++++++++++++++++++++++++++++++++++++ lib/filesystem/FileInfo.h | 53 +++++++++++++++++++++++++++++++++++ 2 files changed, 109 insertions(+) create mode 100644 lib/filesystem/FileInfo.cpp create mode 100644 lib/filesystem/FileInfo.h diff --git a/lib/filesystem/FileInfo.cpp b/lib/filesystem/FileInfo.cpp new file mode 100644 index 000000000..9f3572cda --- /dev/null +++ b/lib/filesystem/FileInfo.cpp @@ -0,0 +1,56 @@ +#include "StdInc.h" + +#include "FileInfo.h" + +namespace FileInfo +{ + +boost::string_ref GetFilename(boost::string_ref path) +{ + const auto pos = path.find_last_of("/\\"); + + if (pos != boost::string_ref::npos) + return path.substr(pos + 1); + + return path; +} + +boost::string_ref GetExtension(boost::string_ref path) +{ + const auto dotPos = path.find_last_of('.'); + + if(dotPos != boost::string_ref::npos) + return path.substr(dotPos); + + return boost::string_ref{}; +} + +boost::string_ref GetStem(boost::string_ref path) +{ + auto begin = path.find_last_of("/\\"); + auto end = path.find_last_of('.'); + + if (begin == boost::string_ref::npos) + begin = 0; + else + begin += 1; + + if (end < begin) + end = boost::string_ref::npos; + + return path.substr(begin, end); +} + +boost::string_ref GetParentPath(boost::string_ref path) +{ + const auto pos = path.find_last_of("/\\"); + return path.substr(0, pos); +} + +boost::string_ref GetPathStem(boost::string_ref path) +{ + const auto dotPos = path.find_last_of('.'); + return path.substr(0, dotPos); +} + +} // namespace FileInfo diff --git a/lib/filesystem/FileInfo.h b/lib/filesystem/FileInfo.h new file mode 100644 index 000000000..4c1154893 --- /dev/null +++ b/lib/filesystem/FileInfo.h @@ -0,0 +1,53 @@ +#pragma once + +/* + * FileInfo.h, part of VCMI engine + * + * Authors: listed in file AUTHORS in main folder + * + * License: GNU General Public License v2.0 or later + * Full text of license available in license.txt file, in main folder + * + */ + +#include + +namespace FileInfo +{ + +/** + * Returns the name of the file. + * + * @return the name of the file. E.g. foo.txt + */ +boost::string_ref DLL_LINKAGE GetFilename(boost::string_ref path); + +/** + * Gets the file extension. + * + * @return the file extension. E.g. .ext + */ +boost::string_ref DLL_LINKAGE GetExtension(boost::string_ref path); + +/** + * Gets the file name exclusive the extension of the file. + * + * @return the file name exclusive the extension and the path of the file. E.g. foo + */ +boost::string_ref DLL_LINKAGE GetStem(boost::string_ref path); + +/** + * Gets the path to the file only. + * + * @return the path to the file only. E.g. ./dir/ + */ +boost::string_ref DLL_LINKAGE GetParentPath(boost::string_ref path); + +/** + * Gets the file name + path exclusive the extension of the file. + * + * @return the file name exclusive the extension of the file. E.g. ./dir/foo + */ +boost::string_ref DLL_LINKAGE GetPathStem(boost::string_ref path); + +} // namespace FileInfo From 7fbd5479e2dc715d9dced13a1133543b4a0b35ed Mon Sep 17 00:00:00 2001 From: Zyx-2000 Date: Fri, 29 Jan 2016 16:08:44 +0100 Subject: [PATCH 07/14] alignment & makefile fix --- client/gui/CAnimation.cpp | 6 +++--- client/gui/CAnimation.h | 4 ++-- lib/CMakeLists.txt | 2 +- lib/filesystem/ResourceID.cpp | 14 ++++++-------- 4 files changed, 12 insertions(+), 14 deletions(-) diff --git a/client/gui/CAnimation.cpp b/client/gui/CAnimation.cpp index 881a76f7a..d80421d9d 100644 --- a/client/gui/CAnimation.cpp +++ b/client/gui/CAnimation.cpp @@ -75,9 +75,9 @@ class CFileCache static const int cacheSize = 50; //Max number of cached files struct FileData { - ResourceID name; - size_t size; - std::unique_ptr data; + ResourceID name; + size_t size; + std::unique_ptr data; std::unique_ptr getCopy() { diff --git a/client/gui/CAnimation.h b/client/gui/CAnimation.h index 329d31206..1dafd26a0 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; - std::unique_ptr data; - std::unique_ptr palette; + std::unique_ptr data; + std::unique_ptr palette; public: CDefFile(std::string Name); diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index 9b50c1ed9..28fafdaee 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -11,11 +11,11 @@ set(lib_SRCS filesystem/CCompressedStream.cpp filesystem/CFilesystemLoader.cpp filesystem/CArchiveLoader.cpp - filesystem/CFileInfo.cpp filesystem/CMemoryStream.cpp filesystem/CBinaryReader.cpp filesystem/CFileInputStream.cpp filesystem/CZipLoader.cpp + filesystem/FileInfo.cpp filesystem/Filesystem.cpp filesystem/FileStream.cpp filesystem/ResourceID.cpp diff --git a/lib/filesystem/ResourceID.cpp b/lib/filesystem/ResourceID.cpp index c0fd3e134..3b146a489 100644 --- a/lib/filesystem/ResourceID.cpp +++ b/lib/filesystem/ResourceID.cpp @@ -53,17 +53,15 @@ ResourceID::ResourceID() } #endif -ResourceID::ResourceID(std::string name_) - :type{readType(name_)}, +ResourceID::ResourceID(std::string name_): + type{readType(name_)}, name{readName(std::move(name_))} -{ -} +{} -ResourceID::ResourceID(std::string name_, EResType::Type type_) - :type{type_}, +ResourceID::ResourceID(std::string name_, EResType::Type type_): + type{type_}, name{readName(std::move(name_))} -{ -} +{} #if 0 std::string ResourceID::getName() const { From 33d07388592cf8514ed45066600eb40eae834c8e Mon Sep 17 00:00:00 2001 From: Zyx-2000 Date: Sat, 30 Jan 2016 22:34:46 +0100 Subject: [PATCH 08/14] support for music streaming --- client/CMakeLists.txt | 1 + client/CMusicHandler.cpp | 21 +++++----- client/CMusicHandler.h | 4 +- client/SDLRWwrapper.cpp | 85 ++++++++++++++++++++++++++++++++++++++++ client/SDLRWwrapper.h | 6 +++ client/VCMI_client.cbp | 2 + 6 files changed, 104 insertions(+), 15 deletions(-) create mode 100644 client/SDLRWwrapper.cpp create mode 100644 client/SDLRWwrapper.h diff --git a/client/CMakeLists.txt b/client/CMakeLists.txt index ed86e969e..9eb3bc802 100644 --- a/client/CMakeLists.txt +++ b/client/CMakeLists.txt @@ -57,6 +57,7 @@ set(client_SRCS Graphics.cpp mapHandler.cpp NetPacksClient.cpp + SDLRWwrapper.cpp ) set(client_HEADERS diff --git a/client/CMusicHandler.cpp b/client/CMusicHandler.cpp index 2bc5853b2..b3e49d413 100644 --- a/client/CMusicHandler.cpp +++ b/client/CMusicHandler.cpp @@ -3,6 +3,7 @@ #include "CMusicHandler.h" #include "CGameInfo.h" +#include "SDLRWwrapper.h" #include "../lib/CCreatureHandler.h" #include "../lib/spells/CSpellHandler.h" #include "../lib/JsonNode.h" @@ -86,7 +87,7 @@ CSoundHandler::CSoundHandler(): listener(std::bind(&CSoundHandler::onVolumeChange, this, _1)); // Vectors for helper(s) - pickupSounds = + pickupSounds = { soundBase::pickup01, soundBase::pickup02, soundBase::pickup03, soundBase::pickup04, soundBase::pickup05, soundBase::pickup06, soundBase::pickup07 @@ -303,7 +304,7 @@ void CMusicHandler::release() void CMusicHandler::playMusic(std::string musicURI, bool loop) { - if (current && current->isTrack( musicURI)) + if (current && current->isTrack(musicURI)) return; queueNext(this, "", musicURI, loop); @@ -342,7 +343,7 @@ void CMusicHandler::playMusicFromSet(std::string whichSet, int entryID, bool loo return; } - if (current && current->isTrack( selectedEntry->second)) + if (current && current->isTrack(selectedEntry->second)) return; // in this mode - play specific track from set @@ -421,12 +422,11 @@ void CMusicHandler::musicFinishedCallback(void) MusicEntry::MusicEntry(CMusicHandler *owner, std::string setName, std::string musicURI, bool looped): owner(owner), music(nullptr), - musicFile(nullptr), loop(looped ? -1 : 1), - setName(setName) + setName(std::move(setName)) { if (!musicURI.empty()) - load(musicURI); + load(std::move(musicURI)); } MusicEntry::~MusicEntry() { @@ -448,15 +448,12 @@ void MusicEntry::load(std::string musicURI) logGlobal->traceStream()<<"Loading music file "<load(ResourceID(musicURI, EResType::MUSIC))->readAll(); - musicFile = SDL_RWFromConstMem(data.first.get(), data.second); - - music = Mix_LoadMUS_RW(musicFile, SDL_FALSE); + auto musicFile = MakeSDLRWops(CResourceHandler::get()->load(ResourceID(std::move(musicURI), EResType::MUSIC))); + + music = Mix_LoadMUS_RW(musicFile, SDL_TRUE); if(!music) { - SDL_FreeRW(musicFile); - musicFile = nullptr; logGlobal->warnStream() << "Warning: Cannot open " << currentName << ": " << Mix_GetError(); return; } diff --git a/client/CMusicHandler.h b/client/CMusicHandler.h index f0031d56a..eaafa8bf9 100644 --- a/client/CMusicHandler.h +++ b/client/CMusicHandler.h @@ -80,10 +80,8 @@ class CMusicHandler; //Class for handling one music file class MusicEntry { - std::pair, size_t> data; CMusicHandler *owner; Mix_Music *music; - SDL_RWops *musicFile; int loop; // -1 = indefinite //if not null - set from which music will be randomly selected @@ -116,7 +114,7 @@ private: std::unique_ptr current; std::unique_ptr next; - + void queueNext(CMusicHandler *owner, std::string setName, std::string musicURI, bool looped); void queueNext(std::unique_ptr queued); diff --git a/client/SDLRWwrapper.cpp b/client/SDLRWwrapper.cpp new file mode 100644 index 000000000..2ce1a1e96 --- /dev/null +++ b/client/SDLRWwrapper.cpp @@ -0,0 +1,85 @@ +#include "StdInc.h" +#include "SDLRWwrapper.h" +#include "../lib/filesystem/CInputStream.h" +#include + +static inline CInputStream* get_stream(SDL_RWops* context) +{ + return static_cast(context->hidden.unknown.data1); +} + +static Sint64 impl_size(SDL_RWops* context) +{ + return get_stream(context)->getSize(); +} + +static Sint64 impl_seek(SDL_RWops* context, Sint64 offset, int whence) +{ + auto stream = get_stream(context); + switch (whence) + { + case RW_SEEK_SET: + return stream->seek(offset); + break; + case RW_SEEK_CUR: + return stream->seek(stream->tell() + offset); + break; + case RW_SEEK_END: + return stream->seek(stream->getSize() + offset); + break; + default: + return -1; + } + +} + +static std::size_t impl_read(SDL_RWops* context, void *ptr, size_t size, size_t maxnum) +{ + auto stream = get_stream(context); + auto oldpos = stream->tell(); + + auto count = stream->read(static_cast(ptr), size*maxnum); + + if (count != 0 && count != size*maxnum) + { + // if not a whole amount of objects of size has been read, we need to seek + stream->seek(oldpos + size * (count / size)); + } + + return count / size; +} + +static std::size_t impl_write(SDL_RWops* context, const void *ptr, size_t size, size_t num) +{ + // writing is not supported + return 0; +} + +static int impl_close(SDL_RWops* context) +{ + if (context == nullptr) + return 0; + + delete get_stream(context); + SDL_FreeRW(context); + return 0; +} + + +SDL_RWops* MakeSDLRWops(std::unique_ptr in) +{ + SDL_RWops* result = SDL_AllocRW(); + if (!result) + return nullptr; + + result->size = &impl_size; + result->seek = &impl_seek; + result->read = &impl_read; + result->write = &impl_write; + result->close = &impl_close; + + result->type = SDL_RWOPS_UNKNOWN; + result->hidden.unknown.data1 = in.release(); + + return result; +} diff --git a/client/SDLRWwrapper.h b/client/SDLRWwrapper.h new file mode 100644 index 000000000..e5b7c2bb5 --- /dev/null +++ b/client/SDLRWwrapper.h @@ -0,0 +1,6 @@ +#pragma once + +struct SDL_RWops; +class CInputStream; + +SDL_RWops* MakeSDLRWops(std::unique_ptr in); diff --git a/client/VCMI_client.cbp b/client/VCMI_client.cbp index dff43308e..5099d1efd 100644 --- a/client/VCMI_client.cbp +++ b/client/VCMI_client.cbp @@ -126,6 +126,8 @@ + + From b0bb7f1f254f1330975c8ba4331846bba6f6624b Mon Sep 17 00:00:00 2001 From: Zyx-2000 Date: Sun, 31 Jan 2016 11:13:56 +0100 Subject: [PATCH 09/14] copyright stuff --- client/SDLRWwrapper.cpp | 10 ++++++++++ client/SDLRWwrapper.h | 10 ++++++++++ 2 files changed, 20 insertions(+) diff --git a/client/SDLRWwrapper.cpp b/client/SDLRWwrapper.cpp index 2ce1a1e96..6e1a98b7a 100644 --- a/client/SDLRWwrapper.cpp +++ b/client/SDLRWwrapper.cpp @@ -1,3 +1,13 @@ +/* + * SDLRWwrapper.cpp, part of VCMI engine + * + * Authors: listed in file AUTHORS in main folder + * + * License: GNU General Public License v2.0 or later + * Full text of license available in license.txt file, in main folder + * + */ + #include "StdInc.h" #include "SDLRWwrapper.h" #include "../lib/filesystem/CInputStream.h" diff --git a/client/SDLRWwrapper.h b/client/SDLRWwrapper.h index e5b7c2bb5..1ed37ccf8 100644 --- a/client/SDLRWwrapper.h +++ b/client/SDLRWwrapper.h @@ -1,5 +1,15 @@ #pragma once +/* + * SDLRWwrapper.h, part of VCMI engine + * + * Authors: listed in file AUTHORS in main folder + * + * License: GNU General Public License v2.0 or later + * Full text of license available in license.txt file, in main folder + * + */ + struct SDL_RWops; class CInputStream; From 4a326e163bce4d5e6390ecf8a2317caf6889977b Mon Sep 17 00:00:00 2001 From: Vadim Markovtsev Date: Wed, 3 Feb 2016 17:58:30 +0300 Subject: [PATCH 10/14] Implement blinded units attack penalties --- config/spells/ability.json | 66 ++++++++++++++++++++++---------------- 1 file changed, 38 insertions(+), 28 deletions(-) diff --git a/config/spells/ability.json b/config/spells/ability.json index 5f1ba9530..6c8cf17ea 100644 --- a/config/spells/ability.json +++ b/config/spells/ability.json @@ -1,10 +1,10 @@ { "stoneGaze" : { "index" : 70, - "targetType": "NO_TARGET", + "targetType": "NO_TARGET", "animation":{ //need special animation - }, + }, "sounds": { "cast": "PARALYZE" }, @@ -26,8 +26,18 @@ "val" : 0, "type" : "NO_RETALIATION", "duration" : "UNITL_BEING_ATTACKED" + }, + "defenceBoost" : { + "type" : "PRIMARY_SKILL", + "subtype" : "primSkill.defence", + "val" : 50, + "valueType" : "PERCENT_TO_ALL", + "duration" : [ + "UNITL_BEING_ATTACKED", + "N_TURNS" + ]} } - } + } } }, "flags" : { @@ -37,10 +47,10 @@ "poison" : { "index" : 71, "targetType": "NO_TARGET", - + "animation":{ "affect":["SP11_"] - }, + }, "sounds": { "cast": "POISON" }, @@ -61,7 +71,7 @@ "valueType" : "PERCENT_TO_ALL", "duration" : "N_TURNS" } - } + } } }, "immunity" : { @@ -75,10 +85,10 @@ "bind" : { "index" : 72, "targetType": "NO_TARGET", - + "animation":{ "affect":["SP02_"] - }, + }, "sounds": { "cast": "BIND" }, @@ -92,7 +102,7 @@ "duration" : "PERMANENT", "addInfo" : -1 } - } + } } }, "flags" : { @@ -102,10 +112,10 @@ "disease" : { "index" : 73, "targetType": "NO_TARGET", - + "animation":{ "affect":["SP05_"] - }, + }, "sounds": { "cast": "DISEASE" }, @@ -126,7 +136,7 @@ "subtype" : "primSkill.defence", "duration" : "N_TURNS" } - } + } } }, "immunity" : { @@ -140,10 +150,10 @@ "paralyze" : { "index" : 74, "targetType": "NO_TARGET", - + "animation":{ //missing - }, + }, "sounds": { "cast": "PARALYZE" }, @@ -176,10 +186,10 @@ "age" : { "index" : 75, "targetType": "NO_TARGET", - + "animation":{ "affect":["SP01_"] - }, + }, "sounds": { "cast": "AGE" }, @@ -208,10 +218,10 @@ "deathCloud" : { "index" : 76, "targetType": "NO_TARGET", - + "animation":{ "hit":["SP04_"] - }, + }, "sounds": { "cast": "DEATHCLD" }, @@ -231,10 +241,10 @@ "thunderbolt" : { "index" : 77, "targetType": "NO_TARGET", - + "animation":{ "affect":[{"defName":"C03SPA0", "verticalPosition":"bottom"}, "C11SPA1"] - }, + }, "sounds": { "cast": "LIGHTBLT" }, @@ -252,10 +262,10 @@ "dispelHelpful" : { "index" : 78, "targetType": "NO_TARGET", - + "animation":{ "affect":["C05SPW"] - }, + }, "sounds": { "cast": "DISPELL" }, @@ -272,10 +282,10 @@ "deathStare" : { "index" : 79, "targetType": "NO_TARGET", - + "animation":{ "affect":["C07SPE0"] - }, + }, "sounds": { "cast": "DEATHSTR" }, @@ -295,10 +305,10 @@ "acidBreath" : { "index" : 80, "targetType": "NO_TARGET", - + "animation":{ //??? - }, + }, "sounds": { "cast": "ACID" }, @@ -324,10 +334,10 @@ "acidBreathDamage" : { "index" : 81, "targetType": "NO_TARGET", - + "animation":{ "affect":["C17SPW0"] - }, + }, "sounds": { "cast": "ACID" }, From b98a83f7431c9d51ffccbc7e276c9d307bc9e54b Mon Sep 17 00:00:00 2001 From: DjWarmonger Date: Thu, 4 Feb 2016 17:57:51 +0100 Subject: [PATCH 11/14] Added string identifiers for terrain types. --- config/creatures/neutral.json | 2 +- lib/CHeroHandler.cpp | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/config/creatures/neutral.json b/config/creatures/neutral.json index ff4c68bcc..7f66d6988 100644 --- a/config/creatures/neutral.json +++ b/config/creatures/neutral.json @@ -480,7 +480,7 @@ "sandWalker" : { "type" : "NO_TERRAIN_PENALTY", - "subtype" : 1, + "subtype" : "terrain.sand", "propagator" : "HERO" } }, diff --git a/lib/CHeroHandler.cpp b/lib/CHeroHandler.cpp index 25c43d4a4..44f772375 100644 --- a/lib/CHeroHandler.cpp +++ b/lib/CHeroHandler.cpp @@ -284,6 +284,10 @@ CHeroHandler::CHeroHandler() } loadObstacles(); loadTerrains(); + for (int i = 0; i < GameConstants::TERRAIN_TYPES; ++i) + { + VLC->modh->identifiers.registerObject("core", "terrain", GameConstants::TERRAIN_NAMES[i], i); + } loadBallistics(); loadExperience(); } From 6f5706352d6983ac3db105cf326a4221ace079ec Mon Sep 17 00:00:00 2001 From: DjWarmonger Date: Sat, 6 Feb 2016 12:36:56 +0100 Subject: [PATCH 12/14] Possible fix for #2371 --- lib/mapObjects/ObjectTemplate.cpp | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/mapObjects/ObjectTemplate.cpp b/lib/mapObjects/ObjectTemplate.cpp index ec8d9908c..9c9e9b37e 100644 --- a/lib/mapObjects/ObjectTemplate.cpp +++ b/lib/mapObjects/ObjectTemplate.cpp @@ -258,11 +258,18 @@ void ObjectTemplate::readJson(const JsonNode &node) ui32 ObjectTemplate::getWidth() const { - return usedTiles.empty() ? 0 : usedTiles.front().size(); + //TODO: Use 2D array + ui32 ret = 0; + for (auto row : usedTiles) + { + ret = std::max(ret, row.size()); + } + return ret; } ui32 ObjectTemplate::getHeight() const { + //TODO: Use 2D array return usedTiles.size(); } From 441c94d56c9a73607ed9d838a28ca837f3c12d22 Mon Sep 17 00:00:00 2001 From: DjWarmonger Date: Sat, 6 Feb 2016 15:11:56 +0100 Subject: [PATCH 13/14] Remove log for Seer Huts. --- lib/rmg/CRmgTemplateZone.cpp | 7 ------- 1 file changed, 7 deletions(-) diff --git a/lib/rmg/CRmgTemplateZone.cpp b/lib/rmg/CRmgTemplateZone.cpp index 71d3fa34d..f29fdecc0 100644 --- a/lib/rmg/CRmgTemplateZone.cpp +++ b/lib/rmg/CRmgTemplateZone.cpp @@ -2073,13 +2073,6 @@ void CRmgTemplateZone::placeObject(CMapGenerator* gen, CGObjectInstance* object, gen->setNearestObjectDistance(tile, std::min(d, gen->getNearestObjectDistance(tile))); } } - if (object->ID == Obj::SEER_HUT) //debug - { - CGSeerHut * sh = dynamic_cast(object); - auto artid = sh->quest->m5arts.front(); - logGlobal->warnStream() << boost::format("Placed Seer Hut at %s, quest artifact %d is %s") % object->pos % artid % VLC->arth->artifacts[artid]->Name(); - } - switch (object->ID) { From a13aec4fd7bcb79664001453f13930b4b80e6912 Mon Sep 17 00:00:00 2001 From: Arseniy Shestakov Date: Mon, 8 Feb 2016 04:09:02 +0300 Subject: [PATCH 14/14] Aplly some fix for merge and explicitly specify type for std::max --- config/spells/ability.json | 2 +- lib/mapObjects/ObjectTemplate.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/config/spells/ability.json b/config/spells/ability.json index 6c8cf17ea..7595cdc6b 100644 --- a/config/spells/ability.json +++ b/config/spells/ability.json @@ -35,7 +35,7 @@ "duration" : [ "UNITL_BEING_ATTACKED", "N_TURNS" - ]} + ] } } } diff --git a/lib/mapObjects/ObjectTemplate.cpp b/lib/mapObjects/ObjectTemplate.cpp index 9c9e9b37e..450c66249 100644 --- a/lib/mapObjects/ObjectTemplate.cpp +++ b/lib/mapObjects/ObjectTemplate.cpp @@ -262,7 +262,7 @@ ui32 ObjectTemplate::getWidth() const ui32 ret = 0; for (auto row : usedTiles) { - ret = std::max(ret, row.size()); + ret = std::max(ret, row.size()); } return ret; }