diff --git a/common/mapsystem/tile_Sheet_manager.hpp b/common/mapsystem/tile_Sheet_manager.hpp new file mode 100644 index 0000000..2fea37c --- /dev/null +++ b/common/mapsystem/tile_Sheet_manager.hpp @@ -0,0 +1,52 @@ +/* Copyright: (c) Kayne Ruse 2013 + * + * This software is provided 'as-is', without any express or implied + * warranty. In no event will the authors be held liable for any damages + * arising from the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software + * in a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * + * 3. This notice may not be removed or altered from any source + * distribution. +*/ +#ifndef TILESHEETMANAGER_HPP_ +#define TILESHEETMANAGER_HPP_ + +#include "tile_sheet.hpp" + +#include +#include + +class TileSheetManager { +public: + TileSheetManager() = default; + ~TileSheetManager() = default; + + TileSheet* LoadSheet(std::string fname, Uint16 w, Uint16 h); + TileSheet* GetSheet(std::string name) { + return &sheetMap.at(name); + } + void UnloadSheet(std::string name) { + sheetMap.erase(name); + } + + void DrawTo(SDL_Surface* const, int x, int y, int tileIndex); + + int SetRangeEnd(int i) { return rangeEnd += i; } + int GetRangeEnd() const { return rangeEnd; } +private: + std::map sheetMap; + int rangeEnd = 0; +}; + +#endif diff --git a/common/mapsystem/tile_sheet.cpp b/common/mapsystem/tile_sheet.cpp index 3124cc0..9608a5e 100644 --- a/common/mapsystem/tile_sheet.cpp +++ b/common/mapsystem/tile_sheet.cpp @@ -21,12 +21,8 @@ */ #include "tile_sheet.hpp" -#include "utility.hpp" - #include -int TileSheet::rangeEnd = 0; - SDL_Surface* TileSheet::LoadSurface(std::string fname, Uint16 w, Uint16 h) { //setup the image image.LoadSurface(fname); @@ -37,14 +33,6 @@ SDL_Surface* TileSheet::LoadSurface(std::string fname, Uint16 w, Uint16 h) { xCount = image.GetSurface()->w / w; yCount = image.GetSurface()->h / h; totalCount = xCount * yCount; - - //cache the name - name = truncatePath(fname); - - //set the range - begin = rangeEnd; - end = begin + totalCount; - rangeEnd += totalCount; } SDL_Surface* TileSheet::GetSurface() { @@ -55,12 +43,11 @@ void TileSheet::FreeSurface() { image.FreeSurface(); totalCount = xCount = yCount = 0; begin = end = -1; - name.clear(); } void TileSheet::DrawTo(SDL_Surface* const dest, int x, int y, int tileIndex) { if (!InRange(tileIndex)) { - throw(std::runtime_error("Tile index out of range of Tile Sheet")); + throw(std::invalid_argument("Tile index out of range")); } Sint16 clipX = (tileIndex-begin) % xCount * image.GetClipW(); Sint16 clipY = (tileIndex-begin) / xCount * image.GetClipH(); diff --git a/common/mapsystem/tile_sheet.hpp b/common/mapsystem/tile_sheet.hpp index 9f55be2..1dfa94b 100644 --- a/common/mapsystem/tile_sheet.hpp +++ b/common/mapsystem/tile_sheet.hpp @@ -44,15 +44,9 @@ public: bool InRange(int i) { return i >= begin && i < end; } - static int SetRangeEnd(int i) { return rangeEnd = i; } - static int GetRangeEnd() { return rangeEnd; } - //accessors and mutators Image* GetImage() { return ℑ } - std::string SetName(std::string s) { return name = s; } - std::string GetName() const { return name; } - int GetTotalCount() const { return totalCount; } int GetXCount() const { return xCount; } int GetYCount() const { return yCount; } @@ -63,10 +57,7 @@ public: int GetBegin() const { return begin; } int GetEnd() const { return end; } private: - static int rangeEnd; - Image image; - std::string name; //these are generated and used by internal processes int totalCount = 0, xCount = 0, yCount = 0; diff --git a/common/mapsystem/tile_sheet_manager.cpp b/common/mapsystem/tile_sheet_manager.cpp new file mode 100644 index 0000000..7f0275b --- /dev/null +++ b/common/mapsystem/tile_sheet_manager.cpp @@ -0,0 +1,48 @@ +/* Copyright: (c) Kayne Ruse 2013 + * + * This software is provided 'as-is', without any express or implied + * warranty. In no event will the authors be held liable for any damages + * arising from the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software + * in a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * + * 3. This notice may not be removed or altered from any source + * distribution. +*/ +#include "tile_sheet_manager.hpp" + +#include "utility.hpp" + +#include + +TileSheet* TileSheetManager::LoadSheet(std::string fname, Uint16 w, Uint16 h) { + //get the key + std::string key = truncatePath(fname); + + //override what's already here + sheetMap.erase(key); + + sheetMap[key].LoadSurface(fname, w, h); + rangeEnd += sheetMap[key].GetTotalCount(); +} + +void TileSheetManager::DrawTo(SDL_Surface* const dest, int x, int y, int tileIndex) { + for (auto& it : sheetMap) { + if (it.second.InRange(tileIndex)) { + it.second.DrawTo(dest, x, y, tileIndex); + return; + } + } + //No matching tile index + throw(std::invalid_argument("Tile index is out of range of all tile sheets")); +}