Created the TileSheetManager class

This class will manage the large(ish) number of tile sheets for a specific
map.
This commit is contained in:
Kayne Ruse
2013-10-21 23:53:36 +11:00
parent 7e603ffa89
commit 0b4e6003d6
4 changed files with 101 additions and 23 deletions
+52
View File
@@ -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 <map>
#include <string>
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<std::string, TileSheet> sheetMap;
int rangeEnd = 0;
};
#endif
+1 -14
View File
@@ -21,12 +21,8 @@
*/
#include "tile_sheet.hpp"
#include "utility.hpp"
#include <stdexcept>
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();
-9
View File
@@ -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 &image; }
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;
+48
View File
@@ -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 <stdexcept>
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"));
}