diff --git a/common/utility.cpp b/common/utility.cpp new file mode 100644 index 0000000..666927f --- /dev/null +++ b/common/utility.cpp @@ -0,0 +1,47 @@ +/* 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 "utility.hpp" + +#include + +int snapToBase(int base, int x) { + //snap to a grid + if (x < 0) { + x++; + return x - x % base - base; + } + return x - x % base; +} + +std::string truncatePath(std::string pathname) { + 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()); +} diff --git a/common/utility.hpp b/common/utility.hpp new file mode 100644 index 0000000..e2c3a95 --- /dev/null +++ b/common/utility.hpp @@ -0,0 +1,30 @@ +/* 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 UTILITY_HPP_ +#define UTILITY_HPP_ + +#include + +int snapToBase(int base, int x); +std::string truncatePath(std::string pathname); + +#endif diff --git a/editor/region.cpp b/editor/region.cpp index ab59971..7f11de3 100644 --- a/editor/region.cpp +++ b/editor/region.cpp @@ -21,26 +21,19 @@ */ #include "region.hpp" +#include "utility.hpp" + #include #include -static int snap(int base, int x) { - //snap to a grid - if (x < 0) { - x++; - return x - x % base - base; - } - return x - x % base; -} - -Region::Region(int i, int j, int k, int l): - x(i), - y(j), - width(k), - height(l) +Region::Region(int _x, int _y, int _w, int _h): + x(_x), + y(_y), + width(_w), + height(_h) { //make sure that the region's position lines up - if (x != snap(width, x) || y != snap(height, y)) { + if (x != snapToBase(width, x) || y != snapToBase(height, y)) { std::ostringstream os; os << "Region is unaligned; x: " << x << ", y: " << y << ", width: " << width << ", height: " << height; throw(std::runtime_error(os.str())); @@ -58,34 +51,41 @@ bool Region::NewTileR(Tile const& tile) { return ret; } -Tile Region::GetTileR(int tx, int ty, int tw, int th, int minDepth) { +Tile Region::GetTileR(int tx, int ty, int minDepth) { std::set::iterator ptr = tiles.begin(); + + //skip the tiles that are too deep while(ptr != tiles.end()) { - //skip the tiles that are too deep if (ptr->depth >= minDepth) { break; } ptr++; } + + //find the first tile here while(ptr != tiles.end()) { - //find the first tile here - if ((ptr->x <= tx) && (ptr->y <= ty) && (ptr->x + tw > tx) && (ptr->y + th > ty)) { + //bounds + if ((ptr->x <= tx) && (ptr->y <= ty) && (ptr->x + ptr->width > tx) && (ptr->y + ptr->height > ty)) { break; } ptr++; } + + //found it if (ptr != tiles.end()) { return *ptr; } - return {0,0,0,-1}; //TODO: value = -1 is a crappy error code + + //a tileIndex of -1 is an error code, the rest is for show + return {0,0,0,-1,-1,-1,-1}; } bool Region::DeleteTileR(Tile const& tile) { if (!InBoundsR(tile.x, tile.y)) { throw(std::runtime_error("Deleted tile location out of bounds")); } - //sentinel - if (tile.value == -1) { + //sentinel/error code + if (tile.tileIndex == -1) { return 0; } return tiles.erase(tile); diff --git a/editor/region.hpp b/editor/region.hpp index a866b8b..f03faff 100644 --- a/editor/region.hpp +++ b/editor/region.hpp @@ -40,30 +40,46 @@ public: //create and insert a new tile, overwriting an existing tile at that location bool NewTileR(Tile const& tile); bool NewTileA(Tile const& tile) { - return NewTileR({tile.x - x, tile.y - y, tile.depth, tile.value}); + //these can change, if the Tile class is changed + return NewTileR({ + tile.x - x, + tile.y - y, + tile.depth, + tile.width, + tile.height, + tile.sheetIndex, + tile.tileIndex + }); } //find the first tile at this location, with the specified minimum depth - //since neither the Region or Tile classes store the tile sizes, - //this function takes the sizes as arguments - Tile GetTileR(int tx, int ty, int tw, int th, int minDepth); - Tile GetTileA(int tx, int ty, int tw, int th, int minDepth) { - return GetTileR(tx - x, ty - y, tw, th, minDepth); + Tile GetTileR(int tx, int ty, int minDepth); + Tile GetTileA(int tx, int ty, int minDepth) { + return GetTileR(tx - x, ty - y, minDepth); } //wrap the other delete functions - bool DeleteTileR(int tx, int ty, int tw, int th, int minDepth) { - return DeleteTileR(GetTileR(tx, ty, tw, th, minDepth)); + bool DeleteTileR(int tx, int ty, int minDepth) { + return DeleteTileR(GetTileR(tx, ty, minDepth)); } - bool DeleteTileA(int tx, int ty, int tw, int th, int minDepth) { + bool DeleteTileA(int tx, int ty, int minDepth) { //explicitly skip one function call by adjusting from A to R - return DeleteTileR(GetTileR(tx - x, ty - y, tw, th, minDepth)); + return DeleteTileR(GetTileR(tx - x, ty - y, minDepth)); } //delete the specified tile bool DeleteTileR(Tile const& tile); bool DeleteTileA(Tile const& tile) { - return DeleteTileR({tile.x - x, tile.y - y, tile.depth, tile.value}); + //these can change, if the Tile class is changed + return DeleteTileR({ + tile.x - x, + tile.y - y, + tile.depth, + tile.width, + tile.height, + tile.sheetIndex, + tile.tileIndex + }); } //find if the specified location exists within the region's bounds diff --git a/editor/tile.hpp b/editor/tile.hpp index 1e362e8..0a2de58 100644 --- a/editor/tile.hpp +++ b/editor/tile.hpp @@ -24,11 +24,24 @@ //explicitly a POD struct Tile { + //position relative to the Region int x, y, depth; - int value; + + //graphics + int width, height; + int sheetIndex, tileIndex; Tile() = default; - Tile(int i, int j, int k, int l) : x(i), y(j), depth(k), value(l) {} + Tile(int _x, int _y, int _depth, int _width, int _height, int _sheetIndex, int _tileIndex) { + //The order of the arguments should be explicit + x = _x; + y = _y; + depth = _depth; + width = _width; + height = _height; + sheetIndex = _sheetIndex; + tileIndex = _tileIndex; + } }; bool operator<(Tile const& lhs, Tile const& rhs); diff --git a/editor/tile_sheet.cpp b/editor/tile_sheet.cpp index a36e945..da5e377 100644 --- a/editor/tile_sheet.cpp +++ b/editor/tile_sheet.cpp @@ -1,22 +1,29 @@ +/* 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.hpp" -#include -#include +#include "utility.hpp" -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()); -} +#include SDL_Surface* TileSheet::LoadSurface(std::string fname, Uint16 w, Uint16 h) { //setup the image diff --git a/editor/tile_sheet.hpp b/editor/tile_sheet.hpp index ac63288..3eb0723 100644 --- a/editor/tile_sheet.hpp +++ b/editor/tile_sheet.hpp @@ -1,3 +1,24 @@ +/* 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 TILESHEET_HPP_ #define TILESHEET_HPP_