Map tiles are now correctly sized and positioned. Errors from before were due to floating point being used for positioning.

master
Zed A. Shaw 2 days ago
parent 2c011079a8
commit b2d0b0ee4c
  1. 5
      Makefile
  2. BIN
      assets/map_tiles.png
  3. 17
      tests/map.cpp
  4. 28
      tools/icongen.cpp

@ -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

Binary file not shown.

Before

Width:  |  Height:  |  Size: 5.5 KiB

After

Width:  |  Height:  |  Size: 5.6 KiB

@ -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<components::Position, components::Tile>([&](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");

@ -33,26 +33,28 @@ struct MapTileBuilder {
sf::Glyph $glyph;
sf::Font $font{FONT_FILE_NAME};
std::shared_ptr<sf::RenderTexture> $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<sf::RenderTexture>(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)};

Loading…
Cancel
Save