diff --git a/Makefile b/Makefile index 4384648..08c53ac 100644 --- a/Makefile +++ b/Makefile @@ -21,6 +21,9 @@ endif build: meson compile -j 10 -C $(ROOT_DIR)/builddir +asset_build: build + ./builddir/icongen + release_build: meson --wipe builddir -Db_ndebug=true --buildtype release meson compile -j 10 -C builddir @@ -33,7 +36,7 @@ tracy_build: meson setup --wipe builddir --buildtype debugoptimized -Dtracy_enable=true -Dtracy:on_demand=true meson compile -j 10 -C builddir -test: build +test: asset_build build ./builddir/runtests "[map-sprite]" run: build test diff --git a/assets/map_tiles.png b/assets/map_tiles.png index b024baa..aec7e89 100644 Binary files a/assets/map_tiles.png and b/assets/map_tiles.png differ diff --git a/tests/map.cpp b/tests/map.cpp index 436924b..5ebb1e2 100644 --- a/tests/map.cpp +++ b/tests/map.cpp @@ -99,6 +99,8 @@ TEST_CASE("map image test", "[map-sprite]") { for(auto tile : tiles) { sf::Vector2i coords{tile["x"], tile["y"]}; + REQUIRE(coords.x % size.x == 0); + REQUIRE(coords.y % size.y == 0); sprite_coord.insert_or_assign(tile["display"], coords); } @@ -117,13 +119,24 @@ TEST_CASE("map image test", "[map-sprite]") { REQUIRE(sprite_coord.contains(display)); auto coords = sprite_coord.at(display); - sf::IntRect square{coords, size}; + 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) * float(size.x), float(it.y) * float(size.y)}); + sprite.setPosition({float(it.x * size.x), float(it.y * size.y)}); render.draw(sprite); } + + level.world->query([&](auto, auto &pos, auto &entity_glyph) { + REQUIRE(sprite_coord.contains(entity_glyph.display)); + auto coords = sprite_coord.at(entity_glyph.display); + sf::IntRect square{coords, {size.x, size.y}}; + sf::Sprite sprite{map_sprites, square}; + sprite.setColor({255,150,150,255}); + sprite.setPosition({float(pos.location.x * size.x), float(pos.location.y * size.y)}); + render.draw(sprite); + }); + render.display(); sf::Image out_img = render.getTexture().copyToImage(); bool worked = out_img.saveToFile("map_test.png"); diff --git a/tools/icongen.cpp b/tools/icongen.cpp index aaf2ca5..a06bb7a 100644 --- a/tools/icongen.cpp +++ b/tools/icongen.cpp @@ -33,26 +33,28 @@ struct MapTileBuilder { sf::Glyph $glyph; sf::Font $font{FONT_FILE_NAME}; std::shared_ptr $render = nullptr; - sf::Vector2u $size; - sf::Vector2u $image_size; + sf::Vector2i $size; + sf::Vector2i $image_size; sf::RenderTexture $temp_render; MapTileBuilder(size_t x, size_t y) : $size(x, y), $image_size($size.x * TILE_COUNT, $size.y * TILE_COUNT), - $temp_render($size) + $temp_render({(unsigned int)$size.x, (unsigned int)$size.y}) { $font.setSmooth(false); } - void best_size(wchar_t for_char) { + void best_size(wchar_t for_char, bool centered) { + float factor = centered ? 0.8f : 1.0f; + sf::Vector2i adjusted_size = {int($size.x * factor), int($size.y * factor)}; $font_size = 20; // reset the size // fit the glyph in our box height auto temp = $font.getGlyph(for_char, $font_size, false); auto temp_size = $font_size; - while(temp.textureRect.size.y < int($size.y) - && temp.textureRect.size.x < int($size.x)) + while(temp.textureRect.size.y <= adjusted_size.y + && temp.textureRect.size.x <= adjusted_size.x) { $glyph = temp; $font_size = temp_size; @@ -79,6 +81,7 @@ struct MapTileBuilder { void run(MapConfig& config) { sf::Vector2u crop{$size.x * (unsigned int)config.it.width, $size.y * (unsigned int)config.it.y}; $render = std::make_shared(crop); + $render->clear({0,0,0,0}); $render->setSmooth(false); sf::Vector2f cell_pos{0.0f,0.0f}; @@ -93,7 +96,7 @@ struct MapTileBuilder { wchar_t display_char = config.map[it.y][it.x]; std::wstring content{display_char}; - best_size(display_char); + best_size(display_char, is_centered); sf::Text icon{$font, content, $font_size}; icon.setFillColor({0, 0, 0, 255}); @@ -105,16 +108,15 @@ struct MapTileBuilder { sf::Sprite sprite{font_texture, $glyph.textureRect}; auto t_size = $glyph.textureRect.size; - dbc::check(int($size.x - t_size.x) > 0, "font too big on x"); - dbc::check(int($size.y - t_size.y) > 0, "font too big on y"); + 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"); if(is_centered) { sf::Vector2f center{ - (float($size.x) - float(t_size.x)) / 2.0f, - (float($size.y) - float(t_size.y)) / 2.0f}; + float(($size.x - t_size.x) / 2), + float(($size.y - t_size.y) / 2)}; - sf::Vector2f scale{float($size.x) / float(t_size.x) * 0.8f, float($size.y) / float(t_size.y) * 0.8f}; - sprite.setScale(scale); + sprite.setScale({1.0f, 1.0f}); sprite.setPosition({cell_pos.x + center.x, cell_pos.y + center.y}); } else { sf::Vector2f scale{float($size.x) / float(t_size.x), float($size.y) / float(t_size.y)};