From cfefffe1cc02ceadc931cb612251632f9a17547b Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Wed, 9 Jul 2025 00:34:50 -0400 Subject: [PATCH] I now can output a map_tiles.json that has all of the tiles in the tile sheet tagged by their display char and where they are. --- assets/map_tiles.json | 128 ++++++++++++++++++++++++++++++++++++++++++ tools/icongen.cpp | 66 +++++++++++++++------- 2 files changed, 174 insertions(+), 20 deletions(-) create mode 100644 assets/map_tiles.json diff --git a/assets/map_tiles.json b/assets/map_tiles.json new file mode 100644 index 0000000..01f47c2 --- /dev/null +++ b/assets/map_tiles.json @@ -0,0 +1,128 @@ +[ + { + "centered": false, + "display": 35, + "x": 0, + "y": 0 + }, + { + "centered": false, + "display": 35, + "x": 128, + "y": 0 + }, + { + "centered": false, + "display": 10398, + "x": 256, + "y": 0 + }, + { + "centered": false, + "display": 35, + "x": 384, + "y": 0 + }, + { + "centered": false, + "display": 35, + "x": 512, + "y": 0 + }, + { + "centered": false, + "display": 8820, + "x": 640, + "y": 0 + }, + { + "centered": false, + "display": 9608, + "x": 768, + "y": 0 + }, + { + "centered": false, + "display": 35, + "x": 896, + "y": 0 + }, + { + "centered": true, + "display": 1003, + "x": 1024, + "y": 0 + }, + { + "centered": true, + "display": 3848, + "x": 1152, + "y": 0 + }, + { + "centered": true, + "display": 85, + "x": 0, + "y": 128 + }, + { + "centered": true, + "display": 8687, + "x": 128, + "y": 128 + }, + { + "centered": true, + "display": 10949, + "x": 256, + "y": 128 + }, + { + "centered": true, + "display": 8793, + "x": 384, + "y": 128 + }, + { + "centered": true, + "display": 95, + "x": 512, + "y": 128 + }, + { + "centered": true, + "display": 1898, + "x": 640, + "y": 128 + }, + { + "centered": true, + "display": 2189, + "x": 768, + "y": 128 + }, + { + "centered": true, + "display": 2189, + "x": 896, + "y": 128 + }, + { + "centered": true, + "display": 42603, + "x": 1024, + "y": 128 + }, + { + "centered": true, + "display": 2220, + "x": 1152, + "y": 128 + }, + { + "centered": true, + "display": 1218, + "x": 0, + "y": 256 + } +] diff --git a/tools/icongen.cpp b/tools/icongen.cpp index 7bee6ab..c285d9a 100644 --- a/tools/icongen.cpp +++ b/tools/icongen.cpp @@ -7,6 +7,7 @@ #include #include "shiterator.hpp" #include +#include namespace fs = std::filesystem; constexpr const int TILE_COUNT=10; @@ -21,6 +22,12 @@ using MapGrid = Base; using BoolRow = BaseRow; using BoolGrid = Base; +struct MapConfig { + MapGrid map = make(TILE_COUNT, TILE_COUNT); + BoolGrid centered = make(TILE_COUNT, TILE_COUNT); + each_row_t it{map}; +}; + struct MapTileBuilder { unsigned int $font_size = 20; sf::Glyph $glyph; @@ -69,21 +76,21 @@ struct MapTileBuilder { dbc::check(worked, "Failed to write screenshot.png"); } - void run(MapGrid& map, unsigned int width, unsigned int height, BoolGrid& centered) { - sf::Vector2u crop{$size.x * width, $size.y * height}; + void run(MapConfig& config) { + sf::Vector2u crop{$size.x * (unsigned int)config.it.width, $size.y * (unsigned int)config.it.y}; $render = std::make_shared(crop); $render->setSmooth(false); sf::Vector2f cell_pos{0.0f,0.0f}; - for(each_row_t it{map}; it.next();) { + for(each_row_t it{config.map}; it.next();) { // a 0 slot means we're done - if(map[it.y][it.x] == 0) break; + if(config.map[it.y][it.x] == 0) break; cell_pos.x = it.x * $size.x; cell_pos.y = it.y * $size.y; - bool is_centered = centered[it.y][it.x]; + bool is_centered = config.centered[it.y][it.x]; - wchar_t display_char = map[it.y][it.x]; + wchar_t display_char = config.map[it.y][it.x]; std::wstring content{display_char}; best_size(display_char); @@ -122,16 +129,37 @@ struct MapTileBuilder { $render->display(); } + + void save_config(MapConfig& config, const std::string &path) { + (void)path; + nlohmann::json result = nlohmann::json::array(); + + for(each_row_t it{config.map}; it.next();) { + if(config.map[it.y][it.x] == 0) break; + + nlohmann::json val; + + val["x"] = $size.x * it.x; + val["y"] = $size.y * it.y; + val["display"] = (int)config.map[it.y][it.x]; + val["centered"] = config.centered[it.y][it.x]; + + result.push_back(val); + } + + std::ofstream o(path, std::ios::out | std::ios::binary); + o << std::setw(4) << result << std::endl; + } }; -void load_config(MapGrid& map, BoolGrid& centered, bool is_centered, each_row_t& it, std::string path, - std::function finder) { +void load_config(MapConfig& config, bool is_centered, std::string path, std::function finder) +{ Config tiles(path); for(auto [key, val] : tiles.json().items()) { - it.next(); - map[it.y][it.x] = finder(val); - centered[it.y][it.x] = is_centered; + config.it.next(); + config.map[config.it.y][config.it.x] = finder(val); + config.centered[config.it.y][config.it.x] = is_centered; } } @@ -148,23 +176,21 @@ wchar_t component_display(nlohmann::json& val) { return L'!'; } - int main() { - MapGrid map = make(TILE_COUNT, TILE_COUNT); - BoolGrid centered = make(TILE_COUNT, TILE_COUNT); - each_row_t it{map}; + MapConfig config; - load_config(map, centered, false, it, "./assets/tiles.json", [](nlohmann::json& val) -> wchar_t { + load_config(config, false, "./assets/tiles.json", [](nlohmann::json& val) -> wchar_t { return val["display"]; }); - load_config(map, centered, true, it, "./assets/items.json", component_display); - load_config(map, centered, true, it, "./assets/devices.json", component_display); - load_config(map, centered, true, it, "./assets/enemies.json", component_display); + load_config(config, true, "./assets/items.json", component_display); + load_config(config, true, "./assets/devices.json", component_display); + load_config(config, true, "./assets/enemies.json", component_display); MapTileBuilder builder(DEFAULT_DIM, DEFAULT_DIM); - builder.run(map, it.width, it.y, centered); + builder.run(config); builder.save_image("./assets/map_tiles.png"); + builder.save_config(config, "./assets/map_tiles.json"); return 0; }