Now have background color for the sprites used in the maps.

This commit is contained in:
Zed A. Shaw 2025-07-10 00:12:32 -04:00
parent b16ca3fd65
commit b2a6262964
5 changed files with 89 additions and 68 deletions

View file

@ -12,7 +12,7 @@
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=64;
constexpr const size_t DEFAULT_DIM=32;
using namespace shiterator;
@ -26,6 +26,7 @@ struct MapConfig {
MapGrid map = make<wchar_t>(TILE_COUNT, TILE_COUNT);
BoolGrid centered = make<bool>(TILE_COUNT, TILE_COUNT);
std::unordered_map<wchar_t, sf::Color> colors;
std::unordered_map<wchar_t, sf::Color> backgrounds;
each_row_t<MapGrid> it{map};
};
@ -86,6 +87,7 @@ struct MapTileBuilder {
$render->setSmooth(false);
sf::Vector2f cell_pos{0.0f,0.0f};
sf::RectangleShape background({(float)$size.x, (float)$size.y});
for(each_row_t<MapGrid> it{config.map}; it.next();) {
// a 0 slot means we're done
@ -112,6 +114,9 @@ struct MapTileBuilder {
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");
// draw the background first
background.setFillColor(config.backgrounds[display_char]);
if(is_centered) {
sf::Vector2f center{
float(($size.x - t_size.x) / 2),
@ -123,10 +128,12 @@ struct MapTileBuilder {
sf::Vector2f scale{float($size.x) / float(t_size.x), float($size.y) / float(t_size.y)};
sprite.setScale(scale);
sprite.setPosition(cell_pos);
background.setPosition(cell_pos);
}
sprite.setColor(config.colors[display_char]);
$render->draw(background);
$render->draw(sprite);
}
@ -170,9 +177,18 @@ void load_config(MapConfig& config, bool is_centered, std::string path, std::fun
sf::Color fg{fg_color[0], fg_color[1], fg_color[2]};
config.colors.insert_or_assign(display, fg);
} else {
sf::Color fg{255, 100, 100};
config.colors.insert_or_assign(display, fg);
config.colors.insert_or_assign(display, DEFAULT_COLOR);
}
if(val.contains("background")) {
auto bg_color = val["background"];
sf::Color bg{bg_color[0], bg_color[1], bg_color[2]};
config.backgrounds.insert_or_assign(display, bg);
} else {
sf::Color bg{0, 0, 0, 0};
config.backgrounds.insert_or_assign(display, bg);
}
}
}