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