Crop the map sprite so it's not bigger than necessary.

master
Zed A. Shaw 24 hours ago
parent 04b3cf3f16
commit 40611d4d54
  1. BIN
      assets/map_tiles.png
  2. 29
      tools/icongen.cpp

Binary file not shown.

Before

Width:  |  Height:  |  Size: 5.9 KiB

After

Width:  |  Height:  |  Size: 15 KiB

@ -10,6 +10,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 size_t DEFAULT_DIM=128;
using namespace shiterator; using namespace shiterator;
@ -23,19 +25,17 @@ struct MapTileBuilder {
unsigned int $font_size = 20; unsigned int $font_size = 20;
sf::Glyph $glyph; sf::Glyph $glyph;
sf::Font $font{FONT_FILE_NAME}; sf::Font $font{FONT_FILE_NAME};
std::shared_ptr<sf::RenderTexture> $render = nullptr;
sf::Vector2u $size; sf::Vector2u $size;
sf::Vector2u $image_size; sf::Vector2u $image_size;
sf::RenderTexture $render;
sf::RenderTexture $temp_render; sf::RenderTexture $temp_render;
MapTileBuilder(size_t x, size_t y) : MapTileBuilder(size_t x, size_t y) :
$size(x, y), $size(x, y),
$image_size($size.x * TILE_COUNT, $size.y * TILE_COUNT), $image_size($size.x * TILE_COUNT, $size.y * TILE_COUNT),
$render($image_size),
$temp_render($size) $temp_render($size)
{ {
$font.setSmooth(false); $font.setSmooth(false);
$render.setSmooth(false);
} }
void best_size(wchar_t for_char) { void best_size(wchar_t for_char) {
@ -56,25 +56,29 @@ struct MapTileBuilder {
} }
void save_image(std::string icon_path) { void save_image(std::string icon_path) {
dbc::check($render != nullptr, "You have to call run() first.");
fs::path out_path{icon_path}; fs::path out_path{icon_path};
if(fs::exists(out_path)) { if(fs::exists(out_path)) {
fs::remove(out_path); fs::remove(out_path);
} }
sf::Image out_img = $render.getTexture().copyToImage(); sf::Image out_img = $render->getTexture().copyToImage();
bool worked = out_img.saveToFile(out_path); bool worked = out_img.saveToFile(out_path);
dbc::check(worked, "Failed to write screenshot.png"); dbc::check(worked, "Failed to write screenshot.png");
} }
void run(MapGrid& map, unsigned int width, unsigned int height, BoolGrid& centered) {
sf::Vector2u crop{$size.x * width, $size.y * height};
$render = std::make_shared<sf::RenderTexture>(crop);
void run(MapGrid& map, BoolGrid& centered) { $render->setSmooth(false);
sf::Vector2f cell_pos{0.0f,0.0f}; sf::Vector2f cell_pos{0.0f,0.0f};
for(each_row_t<MapGrid> it{map}; it.next();) { for(each_row_t<MapGrid> it{map}; it.next();) {
// skip empty slots // a 0 slot means we're done
if(map[it.y][it.x] == 0) continue; if(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 = centered[it.y][it.x]; bool is_centered = centered[it.y][it.x];
@ -97,7 +101,6 @@ struct MapTileBuilder {
dbc::check(int($size.x - t_size.x) > 0, "font too big on x"); 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(int($size.y - t_size.y) > 0, "font too big on y");
if(is_centered) { if(is_centered) {
sf::Vector2f center{ sf::Vector2f center{
(float($size.x) - float(t_size.x)) / 2.0f, (float($size.x) - float(t_size.x)) / 2.0f,
@ -112,12 +115,12 @@ struct MapTileBuilder {
sprite.setPosition(cell_pos); sprite.setPosition(cell_pos);
} }
sprite.setColor({0, 0, 0, 255}); sprite.setColor(DEFAULT_COLOR);
$render.draw(sprite); $render->draw(sprite);
} }
$render.display(); $render->display();
} }
}; };
@ -159,8 +162,8 @@ int main() {
load_config(map, centered, true, it, "./assets/devices.json", component_display); load_config(map, centered, true, it, "./assets/devices.json", component_display);
load_config(map, centered, true, it, "./assets/enemies.json", component_display); load_config(map, centered, true, it, "./assets/enemies.json", component_display);
MapTileBuilder builder(32, 32); MapTileBuilder builder(DEFAULT_DIM, DEFAULT_DIM);
builder.run(map, centered); builder.run(map, it.width, it.y, centered);
builder.save_image("./assets/map_tiles.png"); builder.save_image("./assets/map_tiles.png");
return 0; return 0;

Loading…
Cancel
Save