git repos / saland

commit 9659174b

sago007 · 2018-09-09 12:08
9659174b8f3ff20ed323f9761d858af7bcf373dd patch · browse files
parent 4f66addf11597d1183b8b31590471210e8f1bc58

No longer crash when trying w and e out of bound

Changed files

M src/sagotmx/tmx_struct.h before
M src/saland/Game.cpp before
diff --git a/src/sagotmx/tmx_struct.h b/src/sagotmx/tmx_struct.h index e9b5723..f4e466e 100644 --- a/src/sagotmx/tmx_struct.h +++ b/src/sagotmx/tmx_struct.h
@@ -505,6 +505,13 @@ inline void getTextureLocationFromGid(const TileMap& tm, int gid, std::string* i
}
}
+inline bool tileInBound(const TileMap& tm, int x, int y) {
+ if (x < 0 || y < 0 || x >= tm.width || y >= tm.height) {
+ return false;
+ }
+ return true;
+}
+
/**
* This function tells the gid of the tile in a given location on a given map, on a given layer
*
diff --git a/src/saland/Game.cpp b/src/saland/Game.cpp index 1d43d9e..3d27616 100644 --- a/src/saland/Game.cpp +++ b/src/saland/Game.cpp
@@ -264,7 +264,7 @@ static void SetLengthToOne(float& x, float& y) {
static std::string GetLayerInfoForTile(const World& w, int x, int y) {
std::stringstream ret;
- if (x < 0 || y < 0 || x >= w.tm.width || y >= w.tm.height) {
+ if (!sago::tiled::tileInBound(w.tm, x, y)) {
ret << "Out of bound";
return ret.str();
}
@@ -347,13 +347,13 @@ void Game::ProcessInput(const SDL_Event& event, bool& processed) {
}
int tile_x = data->world_mouse_x/32;
int tile_y = data->world_mouse_y/32;
- if (event.key.keysym.sym == SDLK_w) {
+ if (event.key.keysym.sym == SDLK_w && sago::tiled::tileInBound(data->world.tm, tile_x, tile_y)) {
int layer_number = 2; // Do not hardcode
uint32_t tile = 485;
sago::tiled::setTileOnLayerNumber(data->world.tm, layer_number, tile_x, tile_y, tile);
data->world.init_physics(data->physicsBox);
}
- if (event.key.keysym.sym == SDLK_e) {
+ if (event.key.keysym.sym == SDLK_e && sago::tiled::tileInBound(data->world.tm, tile_x, tile_y)) {
int layer_number = 2; // Do not hardcode
uint32_t tile = 0;
sago::tiled::setTileOnLayerNumber(data->world.tm, layer_number, tile_x, tile_y, tile);