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.
This commit is contained in:
parent
40611d4d54
commit
cfefffe1cc
2 changed files with 174 additions and 20 deletions
128
assets/map_tiles.json
Normal file
128
assets/map_tiles.json
Normal file
|
@ -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 <filesystem>
|
||||||
#include "shiterator.hpp"
|
#include "shiterator.hpp"
|
||||||
#include <functional>
|
#include <functional>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
namespace fs = std::filesystem;
|
namespace fs = std::filesystem;
|
||||||
constexpr const int TILE_COUNT=10;
|
constexpr const int TILE_COUNT=10;
|
||||||
|
@ -21,6 +22,12 @@ using MapGrid = Base<wchar_t>;
|
||||||
using BoolRow = BaseRow<bool>;
|
using BoolRow = BaseRow<bool>;
|
||||||
using BoolGrid = Base<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 {
|
struct MapTileBuilder {
|
||||||
unsigned int $font_size = 20;
|
unsigned int $font_size = 20;
|
||||||
sf::Glyph $glyph;
|
sf::Glyph $glyph;
|
||||||
|
@ -69,21 +76,21 @@ struct MapTileBuilder {
|
||||||
dbc::check(worked, "Failed to write screenshot.png");
|
dbc::check(worked, "Failed to write screenshot.png");
|
||||||
}
|
}
|
||||||
|
|
||||||
void run(MapGrid& map, unsigned int width, unsigned int height, BoolGrid& centered) {
|
void run(MapConfig& config) {
|
||||||
sf::Vector2u crop{$size.x * width, $size.y * height};
|
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 = std::make_shared<sf::RenderTexture>(crop);
|
||||||
|
|
||||||
$render->setSmooth(false);
|
$render->setSmooth(false);
|
||||||
sf::Vector2f cell_pos{0.0f,0.0f};
|
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
|
// 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.x = it.x * $size.x;
|
||||||
cell_pos.y = it.y * $size.y;
|
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};
|
std::wstring content{display_char};
|
||||||
|
|
||||||
best_size(display_char);
|
best_size(display_char);
|
||||||
|
@ -122,16 +129,37 @@ struct MapTileBuilder {
|
||||||
|
|
||||||
$render->display();
|
$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,
|
void load_config(MapConfig& config, bool is_centered, std::string path, std::function<wchar_t(nlohmann::json&)> finder)
|
||||||
std::function<wchar_t(nlohmann::json&)> finder) {
|
{
|
||||||
Config tiles(path);
|
Config tiles(path);
|
||||||
|
|
||||||
for(auto [key, val] : tiles.json().items()) {
|
for(auto [key, val] : tiles.json().items()) {
|
||||||
it.next();
|
config.it.next();
|
||||||
map[it.y][it.x] = finder(val);
|
config.map[config.it.y][config.it.x] = finder(val);
|
||||||
centered[it.y][it.x] = is_centered;
|
config.centered[config.it.y][config.it.x] = is_centered;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -148,23 +176,21 @@ wchar_t component_display(nlohmann::json& val) {
|
||||||
return L'!';
|
return L'!';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
MapGrid map = make<wchar_t>(TILE_COUNT, TILE_COUNT);
|
MapConfig config;
|
||||||
BoolGrid centered = make<bool>(TILE_COUNT, TILE_COUNT);
|
|
||||||
each_row_t<MapGrid> it{map};
|
|
||||||
|
|
||||||
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"];
|
return val["display"];
|
||||||
});
|
});
|
||||||
|
|
||||||
load_config(map, centered, true, it, "./assets/items.json", component_display);
|
load_config(config, true, "./assets/items.json", component_display);
|
||||||
load_config(map, centered, true, it, "./assets/devices.json", component_display);
|
load_config(config, true, "./assets/devices.json", component_display);
|
||||||
load_config(map, centered, true, it, "./assets/enemies.json", component_display);
|
load_config(config, true, "./assets/enemies.json", component_display);
|
||||||
|
|
||||||
MapTileBuilder builder(DEFAULT_DIM, DEFAULT_DIM);
|
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_image("./assets/map_tiles.png");
|
||||||
|
builder.save_config(config, "./assets/map_tiles.json");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue