Added width & height members to Tile, moved local functions to utility.*pp
I've added more members to the Tile structure, namely the width & height of the tile. This is to fix a circular logic problem, which is too abstract for this commit message ;) I've also moved the local/static/inline/etc. utility functions to their own module in the common directory, because this is just cleaner that redefining the same code a dozen times. The map's logic is still the same, but I need to keep moving; I've been procrastinating far too much.
This commit is contained in:
@@ -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 <algorithm>
|
||||
|
||||
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());
|
||||
}
|
||||
@@ -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 <string>
|
||||
|
||||
int snapToBase(int base, int x);
|
||||
std::string truncatePath(std::string pathname);
|
||||
|
||||
#endif
|
||||
+22
-22
@@ -21,26 +21,19 @@
|
||||
*/
|
||||
#include "region.hpp"
|
||||
|
||||
#include "utility.hpp"
|
||||
|
||||
#include <stdexcept>
|
||||
#include <sstream>
|
||||
|
||||
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<Tile>::iterator ptr = tiles.begin();
|
||||
while(ptr != tiles.end()) {
|
||||
|
||||
//skip the tiles that are too deep
|
||||
while(ptr != tiles.end()) {
|
||||
if (ptr->depth >= minDepth) {
|
||||
break;
|
||||
}
|
||||
ptr++;
|
||||
}
|
||||
while(ptr != tiles.end()) {
|
||||
|
||||
//find the first tile here
|
||||
if ((ptr->x <= tx) && (ptr->y <= ty) && (ptr->x + tw > tx) && (ptr->y + th > ty)) {
|
||||
while(ptr != tiles.end()) {
|
||||
//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);
|
||||
|
||||
+27
-11
@@ -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
|
||||
|
||||
+15
-2
@@ -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);
|
||||
|
||||
+23
-16
@@ -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 <algorithm>
|
||||
#include <stdexcept>
|
||||
#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 <stdexcept>
|
||||
|
||||
SDL_Surface* TileSheet::LoadSurface(std::string fname, Uint16 w, Uint16 h) {
|
||||
//setup the image
|
||||
|
||||
@@ -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_
|
||||
|
||||
|
||||
Reference in New Issue
Block a user