diff --git a/editor/region.hpp b/editor/region.hpp index 5c17001..a866b8b 100644 --- a/editor/region.hpp +++ b/editor/region.hpp @@ -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; diff --git a/editor/tile_sheet.cpp b/editor/tile_sheet.cpp new file mode 100644 index 0000000..f1509d1 --- /dev/null +++ b/editor/tile_sheet.cpp @@ -0,0 +1,68 @@ +#include "tile_sheet.hpp" + +#include +#include + +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); +} diff --git a/editor/tile_sheet.hpp b/editor/tile_sheet.hpp new file mode 100644 index 0000000..ac63288 --- /dev/null +++ b/editor/tile_sheet.hpp @@ -0,0 +1,55 @@ +#ifndef TILESHEET_HPP_ +#define TILESHEET_HPP_ + +#include "image.hpp" + +#include + +/* 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 ℑ } + + 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