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

master
Zed A. Shaw 1 day ago
parent b16ca3fd65
commit b2a6262964
  1. 58
      assets/map_tiles.json
  2. BIN
      assets/map_tiles.png
  3. 20
      assets/tiles.json
  4. 29
      tests/map.cpp
  5. 22
      tools/icongen.cpp

@ -8,121 +8,121 @@
{ {
"centered": false, "centered": false,
"display": 35, "display": 35,
"x": 64, "x": 32,
"y": 0 "y": 0
}, },
{ {
"centered": false, "centered": false,
"display": 10398, "display": 10398,
"x": 128, "x": 64,
"y": 0 "y": 0
}, },
{ {
"centered": false, "centered": false,
"display": 35, "display": 35,
"x": 192, "x": 96,
"y": 0 "y": 0
}, },
{ {
"centered": false, "centered": false,
"display": 35, "display": 35,
"x": 256, "x": 128,
"y": 0 "y": 0
}, },
{ {
"centered": false, "centered": false,
"display": 8820, "display": 8820,
"x": 320, "x": 160,
"y": 0 "y": 0
}, },
{ {
"centered": false, "centered": false,
"display": 9608, "display": 9608,
"x": 384, "x": 192,
"y": 0 "y": 0
}, },
{ {
"centered": false, "centered": false,
"display": 35, "display": 35,
"x": 448, "x": 224,
"y": 0 "y": 0
}, },
{ {
"centered": true, "centered": true,
"display": 1003, "display": 1003,
"x": 512, "x": 256,
"y": 0 "y": 0
}, },
{ {
"centered": true, "centered": true,
"display": 3848, "display": 3848,
"x": 576, "x": 288,
"y": 0 "y": 0
}, },
{ {
"centered": true, "centered": true,
"display": 85, "display": 85,
"x": 0, "x": 0,
"y": 64 "y": 32
}, },
{ {
"centered": true, "centered": true,
"display": 8687, "display": 8687,
"x": 64, "x": 32,
"y": 64 "y": 32
}, },
{ {
"centered": true, "centered": true,
"display": 10949, "display": 10949,
"x": 128, "x": 64,
"y": 64 "y": 32
}, },
{ {
"centered": true, "centered": true,
"display": 8793, "display": 8793,
"x": 192, "x": 96,
"y": 64 "y": 32
}, },
{ {
"centered": true, "centered": true,
"display": 95, "display": 95,
"x": 256, "x": 128,
"y": 64 "y": 32
}, },
{ {
"centered": true, "centered": true,
"display": 1898, "display": 1898,
"x": 320, "x": 160,
"y": 64 "y": 32
}, },
{ {
"centered": true, "centered": true,
"display": 2189, "display": 2189,
"x": 384, "x": 192,
"y": 64 "y": 32
}, },
{ {
"centered": true, "centered": true,
"display": 2189, "display": 2189,
"x": 448, "x": 224,
"y": 64 "y": 32
}, },
{ {
"centered": true, "centered": true,
"display": 42603, "display": 42603,
"x": 512, "x": 256,
"y": 64 "y": 32
}, },
{ {
"centered": true, "centered": true,
"display": 2220, "display": 2220,
"x": 576, "x": 288,
"y": 64 "y": 32
}, },
{ {
"centered": true, "centered": true,
"display": 1218, "display": 1218,
"x": 0, "x": 0,
"y": 128 "y": 64
} }
] ]

Binary file not shown.

Before

Width:  |  Height:  |  Size: 6.1 KiB

After

Width:  |  Height:  |  Size: 2.9 KiB

@ -6,7 +6,6 @@
"ceiling": "ceiling_black", "ceiling": "ceiling_black",
"light": 0, "light": 0,
"foreground": [40, 40, 40], "foreground": [40, 40, 40],
"background": [10, 10, 10],
"id": 0 "id": 0
}, },
"wall_plain": { "wall_plain": {
@ -23,8 +22,8 @@
"collision": true, "collision": true,
"display": 8820, "display": 8820,
"light": 20, "light": 20,
"foreground": [150, 255, 150], "foreground": [100, 150, 100],
"background": [10, 10, 10], "background": [100, 100, 100],
"id": 2 "id": 2
}, },
"ceiling_black": { "ceiling_black": {
@ -32,8 +31,7 @@
"collision": false, "collision": false,
"display": 35, "display": 35,
"light": 0, "light": 0,
"foreground": [150, 150, 150], "foreground": [100, 100, 100],
"background": [10, 10, 10],
"id": 4 "id": 4
}, },
"lava_floor": { "lava_floor": {
@ -42,7 +40,7 @@
"display": 35, "display": 35,
"ceiling": "ceiling_black", "ceiling": "ceiling_black",
"light": 20, "light": 20,
"foreground": [255, 0, 0], "foreground": [200, 100, 100],
"background": [10, 10, 10], "background": [10, 10, 10],
"id": 5 "id": 5
}, },
@ -52,8 +50,7 @@
"display": 35, "display": 35,
"ceiling": "ceiling_blue_light", "ceiling": "ceiling_blue_light",
"light": 40, "light": 40,
"foreground": [150, 150, 150], "foreground": [40, 40, 40],
"background": [10, 10, 10],
"id": 6 "id": 6
}, },
"ceiling_blue_light": { "ceiling_blue_light": {
@ -61,8 +58,7 @@
"collision": false, "collision": false,
"display": 35, "display": 35,
"light": 0, "light": 0,
"foreground": [150, 150, 150], "foreground": [100, 100, 100],
"background": [10, 10, 10],
"id": 7 "id": 7
}, },
"wood_wall": { "wood_wall": {
@ -70,8 +66,8 @@
"collision": false, "collision": false,
"display": 35, "display": 35,
"light": 0, "light": 0,
"foreground": [250, 250, 150], "foreground": [70, 70, 70],
"background": [10, 10, 10], "background": [100, 100, 100],
"id": 8 "id": 8
} }
} }

@ -80,6 +80,13 @@ TEST_CASE("dijkstra algo test", "[map]") {
} }
} }
sf::Sprite render_sprite(std::unordered_map<wchar_t, sf::Vector2i>& sprite_coord, sf::Vector2i size, wchar_t display, sf::Texture& map_sprites) {
auto coords = sprite_coord.at(display);
sf::IntRect square{coords, {size.x, size.y}};
sf::Sprite sprite{map_sprites, square};
return sprite;
}
TEST_CASE("map image test", "[map-sprite]") { TEST_CASE("map image test", "[map-sprite]") {
components::init(); components::init();
@ -89,7 +96,7 @@ TEST_CASE("map image test", "[map-sprite]") {
auto &walls = level.map->tiles(); auto &walls = level.map->tiles();
auto &tile_set = textures::get_map_tile_set(); auto &tile_set = textures::get_map_tile_set();
sf::Vector2i size{64,64}; sf::Vector2i size{32,32};
matrix::dump("TILES?", walls); matrix::dump("TILES?", walls);
std::unordered_map<wchar_t, sf::Vector2i> sprite_coord; std::unordered_map<wchar_t, sf::Vector2i> sprite_coord;
@ -109,29 +116,31 @@ TEST_CASE("map image test", "[map-sprite]") {
(unsigned int)matrix::height(walls) * size.y}; (unsigned int)matrix::height(walls) * size.y};
sf::RenderTexture render{dim}; sf::RenderTexture render{dim};
render.clear({50,50,50,255}); render.clear({0,0,0,0});
sf::Texture map_sprites{"./assets/map_tiles.png"}; sf::Texture map_sprites{"./assets/map_tiles.png"};
sf::Texture paper{"./assets/ui/full_screen_paper.png"};
sf::Sprite paper_sprite{paper};
paper_sprite.scale({1.5f, 1.5f});
paper_sprite.setPosition({-30.0f, -30.0f});
render.draw(paper_sprite);
for(matrix::each_row it{walls}; it.next();) { for(matrix::each_row it{walls}; it.next();) {
size_t tid = walls[it.y][it.x]; size_t tid = walls[it.y][it.x];
wchar_t display = tile_set[tid]; wchar_t display = tile_set[tid];
REQUIRE(sprite_coord.contains(display)); REQUIRE(sprite_coord.contains(display));
auto coords = sprite_coord.at(display); auto sprite = render_sprite(sprite_coord, size, display, map_sprites);
sf::IntRect square{coords, {size.x, size.y}};
sf::Sprite sprite{map_sprites, square};
// sprite.setColor({150,150,150,255});
sprite.setPosition({float(it.x * size.x), float(it.y * size.y)}); sprite.setPosition({float(it.x * size.x), float(it.y * size.y)});
sprite.setColor({255, 255, 255, 200});
render.draw(sprite); render.draw(sprite);
} }
level.world->query<components::Position, components::Tile>([&](auto, auto &pos, auto &entity_glyph) { level.world->query<components::Position, components::Tile>([&](auto, auto &pos, auto &entity_glyph) {
REQUIRE(sprite_coord.contains(entity_glyph.display)); REQUIRE(sprite_coord.contains(entity_glyph.display));
auto coords = sprite_coord.at(entity_glyph.display);
sf::IntRect square{coords, {size.x, size.y}}; auto sprite = render_sprite(sprite_coord, size, entity_glyph.display, map_sprites);
sf::Sprite sprite{map_sprites, square};
sprite.setColor({255,150,150,255}); sprite.setColor({255,150,150,255});
sprite.setPosition({float(pos.location.x * size.x), float(pos.location.y * size.y)}); sprite.setPosition({float(pos.location.x * size.x), float(pos.location.y * size.y)});
render.draw(sprite); render.draw(sprite);

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

Loading…
Cancel
Save