There's a bug where the last item in tiles.json draws a black square, which is why I named ceiling_blue to zceiling_blue to temporarily solve it.

This commit is contained in:
Zed A. Shaw 2025-07-11 01:02:27 -04:00
parent b2a6262964
commit 5e01eb29a9
7 changed files with 116 additions and 102 deletions

View file

@ -12,7 +12,8 @@
namespace fs = std::filesystem;
constexpr const int TILE_COUNT=10;
constexpr const sf::Color DEFAULT_COLOR{255, 255, 255, 255};
constexpr const size_t DEFAULT_DIM=32;
constexpr const size_t DEFAULT_DIM=64;
using namespace nlohmann;
using namespace shiterator;
@ -92,30 +93,36 @@ struct MapTileBuilder {
for(each_row_t<MapGrid> it{config.map}; it.next();) {
// a 0 slot means we're done
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 = config.centered[it.y][it.x];
wchar_t display_char = config.map[it.y][it.x];
std::wstring content{display_char};
auto bg = config.backgrounds.at(display_char);
auto fg = config.colors.at(display_char);
best_size(display_char, is_centered);
sf::Text icon{$font, content, $font_size};
icon.setFillColor({0, 0, 0, 255});
icon.setFillColor({255, 255, 255, 255});
$temp_render.draw(icon);
$temp_render.clear({0,0,0,0});
auto font_texture = $font.getTexture($font_size);
auto& font_texture = $font.getTexture($font_size);
sf::Sprite sprite{font_texture, $glyph.textureRect};
auto t_size = $glyph.textureRect.size;
dbc::check($size.x - t_size.x >= 0, "font too big on x");
dbc::check($size.y - t_size.y >= 0, "font too big on y");
fmt::println("display: {}, bg: {},{},{},{}; fg: {},{},{},{}",
(int)display_char, bg.r, bg.g, bg.b, bg.a, fg.r, fg.g, fg.b, fg.a);
// draw the background first
background.setFillColor(config.backgrounds[display_char]);
background.setFillColor(bg);
if(is_centered) {
sf::Vector2f center{
@ -131,23 +138,22 @@ struct MapTileBuilder {
background.setPosition(cell_pos);
}
sprite.setColor(config.colors[display_char]);
sprite.setColor(fg);
$render->draw(background);
$render->draw(sprite);
$render->display();
}
$render->display();
}
void save_config(MapConfig& config, const std::string &path) {
(void)path;
nlohmann::json result = nlohmann::json::array();
json result = json::array();
for(each_row_t<MapGrid> it{config.map}; it.next();) {
if(config.map[it.y][it.x] == 0) break;
nlohmann::json val;
json val;
val["x"] = $size.x * it.x;
val["y"] = $size.y * it.y;
@ -162,60 +168,69 @@ struct MapTileBuilder {
}
};
void load_config(MapConfig& config, bool is_centered, std::string path, std::function<wchar_t(nlohmann::json&)> finder)
void load_config(MapConfig& config, bool is_centered, std::string path, std::function<json&(json&)> finder)
{
Config tiles(path);
for(auto [key, val] : tiles.json().items()) {
config.it.next();
auto display = finder(val);
auto data = finder(val);
wchar_t display = data["display"];
config.map[config.it.y][config.it.x] = display;
config.centered[config.it.y][config.it.x] = is_centered;
if(val.contains("foreground")) {
auto fg_color = val["foreground"];
dbc::check(!config.colors.contains(display),
fmt::format("duplicate color for display={} key={}",
(int)display, (std::string)key));
if(data.contains("foreground")) {
auto fg_color = data["foreground"];
sf::Color fg{fg_color[0], fg_color[1], fg_color[2]};
fmt::println("TILE {}, display: {} has foreground: {},{},{}", key, (int)display, fg.r, fg.g, fg.b);
config.colors.insert_or_assign(display, fg);
} else {
fmt::println("TILE {}, {} has DEFAULT COLOR", key, (int)display);
config.colors.insert_or_assign(display, DEFAULT_COLOR);
}
if(val.contains("background")) {
auto bg_color = val["background"];
if(data.contains("background")) {
auto bg_color = data["background"];
sf::Color bg{bg_color[0], bg_color[1], bg_color[2]};
fmt::println("TILE {} display: {} has background: {},{},{}", key, (int)display, bg.r, bg.g, bg.b);
config.backgrounds.insert_or_assign(display, bg);
} else {
fmt::println("TILE {} display: {} has transparent background", key, (int)display);
sf::Color bg{0, 0, 0, 0};
config.backgrounds.insert_or_assign(display, bg);
}
}
}
wchar_t component_display(nlohmann::json& val) {
json& component_display(json& val) {
auto& components = val["components"];
for(auto& comp : components) {
if(comp["_type"] == "Tile") {
return comp["display"];
return comp;
}
}
dbc::log("BAD CHAR");
return L'!';
return val;
}
int main() {
MapConfig config;
load_config(config, false, "./assets/tiles.json", [](nlohmann::json& val) -> wchar_t {
return val["display"];
load_config(config, false, "./assets/tiles.json", [](json& val) -> json& {
return val;
});
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);
fmt::println("-----------------------------------------");
MapTileBuilder builder(DEFAULT_DIM, DEFAULT_DIM);
builder.run(config);