From 144d6201929ebd84f48f6ab5b056b3cf1d2dc5af Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Fri, 26 Sep 2014 04:12:27 +0200 Subject: Implement a very simple map file format for further testing --- resources/map/test.map | 104 +++++++++++++++++++++++++++++++++++ resources/sprite/tile/grass.png | Bin 0 -> 1296 bytes resources/sprite/tile/road_left.png | Bin 0 -> 245 bytes resources/sprite/tile/road_right.png | Bin 0 -> 242 bytes src/model/Map.cpp | 86 ++++++++++++++++++++++------- 5 files changed, 170 insertions(+), 20 deletions(-) create mode 100644 resources/map/test.map create mode 100644 resources/sprite/tile/grass.png create mode 100644 resources/sprite/tile/road_left.png create mode 100644 resources/sprite/tile/road_right.png diff --git a/resources/map/test.map b/resources/map/test.map new file mode 100644 index 0000000..35fada7 --- /dev/null +++ b/resources/map/test.map @@ -0,0 +1,104 @@ +32 32 2 + +G 0 grass +< 0 road_left +> 0 road_right + +11111111111111111111111111111111 +11111111111111111111111111111111 +11111111111111111111111111111111 +11111111111111111111111111111111 +11111111111111111111111111111111 +11111111111111111111111111111111 +11111111111111111111111111111111 +11111111111111111111111111111111 +11111111111111111111111111111111 +11111111111111111111111111111111 +11111111111111111111111111111111 +11111111111111111111111111111111 +11111111111111111111111111111111 +11111111111111111111111111111111 +11111111111111111111111111111111 +11111111111111111111111111111111 +11111111111111111111111111111111 +11111111111111111111111111111111 +11111111111111111111111111111111 +11111111111111111111111111111111 +11111111111111111111111111111111 +11111111111111111111111111111111 +11111111111111111111111111111111 +11111111111111111111111111111111 +11111111111111111111111111111111 +11111111111111111111111111111111 +11111111111111111111111111111111 +11111111111111111111111111111111 +11111111111111111111111111111111 +11111111111111111111111111111111 +11111111111111111111111111111111 +11111111111111111111111111111111 + +GGGGGGGGGGG<>GGGGGGGGGGGGGGGGGGG +GGGGGGGGGGG<>GGGGGGGGGGGGGGGGGGG +GGGGGGGGGGG<>GGGGGGGGGGGGGGGGGGG +GGGGGGGGGGG<>GGGGGGGGGGGGGGGGGGG +GGGGGGGGGGG<>GGGGGGGGGGGGGGGGGGG +GGGGGGGGGGG<>GGGGGGGGGGGGGGGGGGG +GGGGGGGGGGG<>GGGGGGGGGGGGGGGGGGG +GGGGGGGGGGG<>GGGGGGGGGGGGGGGGGGG +GGGGGGGGGGG<>GGGGGGGGGGGGGGGGGGG +GGGGGGGGGGG<>GGGGGGGGGGGGGGGGGGG +GGGGGGGGGGG<>GGGGGGGGGGGGGGGGGGG +GGGGGGGGGGG<>GGGGGGGGGGGGGGGGGGG +GGGGGGGGGGG<>GGGGGGGGGGGGGGGGGGG +GGGGGGGGGGG<>GGGGGGGGGGGGGGGGGGG +GGGGGGGGGGG<>GGGGGGGGGGGGGGGGGGG +GGGGGGGGGGG<>GGGGGGGGGGGGGGGGGGG +GGGGGGGGGGG<>GGGGGGGGGGGGGGGGGGG +GGGGGGGGGGG<>GGGGGGGGGGGGGGGGGGG +GGGGGGGGGGG<>GGGGGGGGGGGGGGGGGGG +GGGGGGGGGGG<>GGGGGGGGGGGGGGGGGGG +GGGGGGGGGGG<>GGGGGGGGGGGGGGGGGGG +GGGGGGGGGGG<>GGGGGGGGGGGGGGGGGGG +GGGGGGGGGGG<>GGGGGGGGGGGGGGGGGGG +GGGGGGGGGGG<>GGGGGGGGGGGGGGGGGGG +GGGGGGGGGGG<>GGGGGGGGGGGGGGGGGGG +GGGGGGGGGGG<>GGGGGGGGGGGGGGGGGGG +GGGGGGGGGGG<>GGGGGGGGGGGGGGGGGGG +GGGGGGGGGGG<>GGGGGGGGGGGGGGGGGGG +GGGGGGGGGGG<>GGGGGGGGGGGGGGGGGGG +GGGGGGGGGGG<>GGGGGGGGGGGGGGGGGGG +GGGGGGGGGGG<>GGGGGGGGGGGGGGGGGGG +GGGGGGGGGGG<>GGGGGGGGGGGGGGGGGGG + +................................ +................................ +................................ +................................ +................................ +................................ +................................ +................................ +................................ +................................ +................................ +................................ +................................ +................................ +................................ +................................ +................................ +................................ +................................ +................................ +................................ +................................ +................................ +................................ +................................ +................................ +................................ +................................ +................................ +................................ +................................ +................................ diff --git a/resources/sprite/tile/grass.png b/resources/sprite/tile/grass.png new file mode 100644 index 0000000..7835867 Binary files /dev/null and b/resources/sprite/tile/grass.png differ diff --git a/resources/sprite/tile/road_left.png b/resources/sprite/tile/road_left.png new file mode 100644 index 0000000..341772d Binary files /dev/null and b/resources/sprite/tile/road_left.png differ diff --git a/resources/sprite/tile/road_right.png b/resources/sprite/tile/road_right.png new file mode 100644 index 0000000..a04f097 Binary files /dev/null and b/resources/sprite/tile/road_right.png differ diff --git a/src/model/Map.cpp b/src/model/Map.cpp index cb1cb30..ac0b6ee 100644 --- a/src/model/Map.cpp +++ b/src/model/Map.cpp @@ -26,6 +26,10 @@ #include "Map.hpp" +#include +#include +#include + namespace RPGEdit { @@ -85,32 +89,74 @@ void Map::finishEntityTransition(Entity *entity) { } std::unique_ptr Map::load(__attribute__((unused)) const std::string &name) { - std::unique_ptr map(new Map(16, 16, 2)); + std::string filename = "../resources/map/" + name + ".map"; - map->tileset.emplace_back("dirt", 0); - map->tileset.emplace_back("horizontal_bar", 0); - map->tileset.emplace_back("horizontal_bar", 2); + std::ifstream file; - for (int i = 0; i < 16; i++) { - for (int j = 0; j < 16; j++) { - if (4 <= i && i < 12 && 4 <= j && j < 12) { - map->setTileAt(0, Position{i, j}, 1); - map->setCollisionAt(Position{i, j}, CollisionType::EMPTY); - } - else { - map->setTileAt(0, Position{i, j}, 0); - } + file.exceptions(std::ifstream::failbit | std::ifstream::badbit); + file.open(filename.c_str()); + + std::string line; + + std::getline(file, line); + size_t w, h, layers; + if (std::sscanf(line.c_str(), "%zu %zu %zu", &w, &h, &layers) != 3) + throw std::invalid_argument("invalid map file"); - if (4 <= i && i < 12 && j == 7) - map->setTileAt(1, Position{i, j}, 3); - else if (4 <= i && i < 12 && j == 8) - map->setTileAt(1, Position{i, j}, 2); - else - map->setTileAt(1, Position{i, j}, 0); + std::unique_ptr map(new Map(w, h, layers)); + + std::getline(file, line); + if (line.length()) + throw std::invalid_argument("invalid map file"); + + size_t n = 1; + std::unordered_map tileset; + + for (std::getline(file, line); line.length(); std::getline(file, line)) { + char c; + unsigned rot; + char *tile = nullptr; + + if (std::sscanf(line.c_str(), "%c %u %ms", &c, &rot, &tile) != 3) { + std::free(tile); + throw std::invalid_argument("invalid map file"); } + + tileset.emplace(c, n++); + map->tileset.emplace_back(tile, rot); + std::free(tile); } - map->addEntity("square", Model::Position{6, 6}); + for (size_t i = 0; i < h; i++) { + std::getline(file, line); + if (line.length() != w) + throw std::invalid_argument("invalid map file"); + + for (size_t j = 0; j < w; j++) { + if (line[j] == '1') + map->setCollisionAt(Position{int(j), int(i)}, CollisionType::EMPTY); + else if (line[j] != '0') + throw std::invalid_argument("invalid map file"); + } + } + + for (size_t layer = 0; layer < layers; layer++) { + std::getline(file, line); + if (line.length()) + throw std::invalid_argument("invalid map file"); + + for (size_t i = 0; i < h; i++) { + std::getline(file, line); + if (line.length() != w) + throw std::invalid_argument("invalid map file"); + + for (size_t j = 0; j < w; j++) { + auto it = tileset.find(line[j]); + if (it != tileset.end()) + map->setTileAt(layer, Position{int(j), int(i)}, it->second); + } + } + } return map; } -- cgit v1.2.3