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.

master
Zed A. Shaw 2 days ago
parent 40611d4d54
commit cfefffe1cc
  1. 128
      assets/map_tiles.json
  2. 66
      tools/icongen.cpp

@ -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
}
]

@ -7,6 +7,7 @@
#include <filesystem>
#include "shiterator.hpp"
#include <functional>
#include <iostream>
namespace fs = std::filesystem;
constexpr const int TILE_COUNT=10;
@ -21,6 +22,12 @@ using MapGrid = Base<wchar_t>;
using BoolRow = BaseRow<bool>;
using BoolGrid = Base<bool>;
struct MapConfig {
MapGrid map = make<wchar_t>(TILE_COUNT, TILE_COUNT);
BoolGrid centered = make<bool>(TILE_COUNT, TILE_COUNT);
each_row_t<MapGrid> 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<sf::RenderTexture>(crop);
$render->setSmooth(false);
sf::Vector2f cell_pos{0.0f,0.0f};
for(each_row_t<MapGrid> it{map}; it.next();) {
for(each_row_t<MapGrid> 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<MapGrid> 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<MapGrid>& it, std::string path,
std::function<wchar_t(nlohmann::json&)> finder) {
void load_config(MapConfig& config, bool is_centered, std::string path, std::function<wchar_t(nlohmann::json&)> 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<wchar_t>(TILE_COUNT, TILE_COUNT);
BoolGrid centered = make<bool>(TILE_COUNT, TILE_COUNT);
each_row_t<MapGrid> 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;
}

Loading…
Cancel
Save