diff options
-rw-r--r-- | CMakeLists.txt | 1 | ||||
-rw-r--r-- | Canvas.cpp | 26 | ||||
-rw-r--r-- | Canvas.h | 2 | ||||
-rw-r--r-- | Deserializer.cpp | 10 | ||||
-rw-r--r-- | Deserializer.h | 2 | ||||
-rw-r--r-- | Tile.h | 3 | ||||
-rw-r--r-- | TileData.h | 4 | ||||
-rw-r--r-- | XMLParser.cpp | 4 | ||||
-rw-r--r-- | main.cpp | 4 | ||||
-rw-r--r-- | util.cpp | 26 | ||||
-rw-r--r-- | util.h | 6 |
11 files changed, 77 insertions, 11 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 8217d1d..2cef305 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,6 +15,7 @@ project(main C CXX) add_executable(main main.cpp + util.cpp File.cpp HTTPFile.cpp LocalFile.cpp @@ -1,6 +1,10 @@ #include <cstdio> +#include <string> #include "Canvas.h" +#include "util.h" + +using namespace std; Tile & Canvas::get_tile(unsigned x, unsigned y) { return *this->tiles[this->pos_to_index(x, y)]; @@ -35,3 +39,25 @@ Canvas::~Canvas() { } } +string Canvas::to_string(bool truecolor) { + string out = ""; + + for (size_t y = 0; y < this->data.rows; y++) { + for (size_t x = 0; x < this->data.columns; x++) { + Tile & tile = this->get_tile(x, y); + string type_str = tile.data.type; + if (type_str.length() == 0) type_str = "."; + + if (truecolor) + out += stringf("\e[48;2;%d;%d;%dm", tile.appearance.color.red, + tile.appearance.color.green, tile.appearance.color.blue); + + out += stringf("%-2s ", type_str.c_str()); + } + if (truecolor) out += "\e[0m"; + out += "\n"; + } + + return out; +} + @@ -1,5 +1,6 @@ #pragma once +#include <string> #include <vector> #include "CanvasData.h" @@ -9,6 +10,7 @@ class Canvas { public: Canvas() = default; virtual ~Canvas(); + std::string to_string(bool truecolor = false); public: virtual Tile & get_tile(unsigned x, unsigned y); diff --git a/Deserializer.cpp b/Deserializer.cpp index 03e58b8..89489a5 100644 --- a/Deserializer.cpp +++ b/Deserializer.cpp @@ -36,15 +36,15 @@ void Deserializer::set_canvas(CanvasData data) { } void Deserializer::add_tile(unsigned int x, unsigned int y, TileData data) { - printf("add tile(%d,%d) data(%c)...\n", x, y, data.type); + printf("add tile(%d,%d) data(%s)...\n", x, y, data.type.c_str()); Museum & museum = this->get_target(); museum.canvas.set_tile(x, y, data); } -void Deserializer::add_type(char type, Color color, unsigned int weight) { - if (type == '\0') return; - printf("add type(%c) color(#%02x%02x%02x) weight(%d)...\n", type, color.red, - color.green, color.blue, weight); +void Deserializer::add_type(std::string type, Color color, unsigned int weight) { + if (type.length() == 0) return; + printf("add type(%s) color(#%02x%02x%02x) weight(%d)...\n", type.c_str(), + color.red, color.green, color.blue, weight); } diff --git a/Deserializer.h b/Deserializer.h index 7a37518..655caca 100644 --- a/Deserializer.h +++ b/Deserializer.h @@ -13,7 +13,7 @@ public: public: void set_target(Museum * m); - void add_type(char type, Color color, unsigned int weight); + void add_type(std::string type, Color color, unsigned int weight); void set_canvas(CanvasData); @@ -11,9 +11,8 @@ public: public: TileData data; - + TileAppearance appearance; private: TileBehavior behavior; - TileAppearance appearance; }; @@ -1,6 +1,8 @@ #pragma once +#include <string> + typedef struct { - char type; + std::string type; } TileData; diff --git a/XMLParser.cpp b/XMLParser.cpp index 431c71b..8dda552 100644 --- a/XMLParser.cpp +++ b/XMLParser.cpp @@ -48,7 +48,7 @@ void XMLParser::parse(File & f, Deserializer & d) { throw Exception("missing <nodeTypes>"); for (xml_node node_type : node_types) { d.add_type( - node_type.attribute("tag").as_string()[0], + node_type.attribute("tag").as_string(), { .red = node_type.attribute("red").as_uint(), .green = node_type.attribute("green").as_uint(), @@ -66,7 +66,7 @@ void XMLParser::parse(File & f, Deserializer & d) { node.attribute("x").as_uint(), node.attribute("y").as_uint(), { - .type = node.name()[0], + .type = node.name(), } ); } @@ -6,6 +6,8 @@ #include "Deserializer.h" #include "Parser.h" +using namespace std; + void load_museum(Museum & m, int argc, char** argv) { Deserializer d {}; d.set_target(&m); @@ -39,6 +41,8 @@ int main(int argc, char** argv) { load_museum(m, argc, argv); + printf("%s", m.canvas.to_string(true).c_str()); + return EXIT_SUCCESS; } diff --git a/util.cpp b/util.cpp new file mode 100644 index 0000000..04354db --- /dev/null +++ b/util.cpp @@ -0,0 +1,26 @@ +#include <cstdio> +#include <cstdarg> + +#include "util.h" + +using namespace std; + +string stringf(const char * fmt, ...) { + va_list args, args_copy; + va_start(args, fmt); + va_copy(args_copy, args); + + size_t sz = vsnprintf(NULL, 0, fmt, args_copy) + 1; + char * msg = (char *) malloc(sz); + va_end(args_copy); + + vsnprintf(msg, sz, fmt, args); + + string out = msg; + free(msg); + + va_end(args); + + return out; +} + @@ -0,0 +1,6 @@ +#pragma once + +#include <string> + +std::string stringf(const char * fmt, ...); + |