Created the TileSheet class

This is incomplete on it's own, but used as part of the whole map system,
it should work well. This class keeps track of various, seemingly
unrelated states, which are used as part of the larger map system.

Also, fixed Region's members being public.
This commit is contained in:
Kayne Ruse
2013-09-25 20:39:59 +10:00
parent a54fbfb9e9
commit d5b14c2679
3 changed files with 124 additions and 3 deletions
+1 -3
View File
@@ -30,8 +30,6 @@
* This class stores the tiles relative to it's own position, but
* there are functions for referencing the tiles' absolute position.
* These functions simply wrap the normal functions.
*
* TODO: This class needs to be thoroughly tested.
*/
class Region {
public:
@@ -89,7 +87,7 @@ public:
friend bool operator>(Region const& lhs, Region const& rhs);
friend bool operator==(Region const& lhs, Region const& rhs);
public: //TMP
private:
int const x;
int const y;
int const width;
+68
View File
@@ -0,0 +1,68 @@
#include "tile_sheet.hpp"
#include <algorithm>
#include <stdexcept>
static inline std::string truncatePath(std::string pathname) {
//TODO: move this into a utilities module?
return std::string(
std::find_if(
pathname.rbegin(),
pathname.rend(),
[](char ch) -> bool {
//windows only
return ch == '/' || ch == '\\';
// //unix only
// return ch == '/';
}).base(),
pathname.end());
}
SDL_Surface* TileSheet::LoadSurface(std::string fname, Uint16 w, Uint16 h) {
//setup the image
image.LoadSurface(fname);
image.SetClipW(w);
image.SetClipH(h);
//get the tile counts
xCount = image.GetSurface()->w / w;
yCount = image.GetSurface()->h / h;
totalCount = xCount * yCount;
//cache the name
name = truncatePath(fname);
}
SDL_Surface* TileSheet::SetSurface(SDL_Surface* const ptr, Uint16 w, Uint16 h) {
//setup the image
image.SetSurface(ptr);
image.SetClipW(w);
image.SetClipH(h);
//get the tile counts
xCount = image.GetSurface()->w / w;
yCount = image.GetSurface()->h / h;
totalCount = xCount * yCount;
}
SDL_Surface* TileSheet::GetSurface() {
return image.GetSurface();
}
void TileSheet::FreeSurface() {
image.FreeSurface();
totalCount = xCount = yCount = 0;
}
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"));
}
Sint16 clipX = tileIndex % xCount * image.GetClipW();
Sint16 clipY = tileIndex / xCount * image.GetClipH();
image.SetClipX(clipX);
image.SetClipY(clipY);
image.DrawTo(dest, x, y);
}
+55
View File
@@ -0,0 +1,55 @@
#ifndef TILESHEET_HPP_
#define TILESHEET_HPP_
#include "image.hpp"
#include <string>
/* The TileSheet class is used for drawing tiles of the map to the screen.
* This class also has some code to allow the management of several TileSheets,
* namely the index, begin & end members, tracking of the tile indexes, and
* custom wrapper functions.
*/
class TileSheet {
public:
TileSheet() = default;
~TileSheet() = default;
//these load/set functions need to be followed by bookkeeping code
//w & h are the width & height of individual tiles
SDL_Surface* LoadSurface(std::string fname, Uint16 w, Uint16 h);
SDL_Surface* SetSurface(SDL_Surface* const, Uint16 w, Uint16 h);
SDL_Surface* GetSurface();
void FreeSurface();
void DrawTo(SDL_Surface* const dest, int x, int y, int tileIndex);
bool InRange(int i) { return i >= begin && i < end; }
//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; }
int SetIndex(int i) { return index = i; }
int SetBegin(int i) { return begin = i; }
int SetEnd(int i) { return end = i; }
int GetIndex() const { return index; }
int GetBegin() const { return begin; }
int GetEnd() const { return end; }
private:
Image image;
std::string name;
//these are generated and used by internal processes
int totalCount = 0, xCount = 0, yCount = 0;
int index = -1, begin = -1, end = -1;
};
#endif