A retro style homage to 80s dungeon crawlers hand crafted in C++.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 
raycaster/tools/icongen.cpp

170 lines
4.8 KiB

#include <fmt/core.h>
#include "dbc.hpp"
#include <SFML/Graphics.hpp>
#include <SFML/Graphics/RenderWindow.hpp>
#include "constants.hpp"
#include "config.hpp"
#include <filesystem>
#include "shiterator.hpp"
#include <functional>
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=128;
using namespace shiterator;
using MapRow = BaseRow<wchar_t>;
using MapGrid = Base<wchar_t>;
using BoolRow = BaseRow<bool>;
using BoolGrid = Base<bool>;
struct MapTileBuilder {
unsigned int $font_size = 20;
sf::Glyph $glyph;
sf::Font $font{FONT_FILE_NAME};
std::shared_ptr<sf::RenderTexture> $render = nullptr;
sf::Vector2u $size;
sf::Vector2u $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)
{
$font.setSmooth(false);
}
void best_size(wchar_t for_char) {
$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))
{
$glyph = temp;
$font_size = temp_size;
temp_size++;
temp = $font.getGlyph(for_char, temp_size, false);
}
}
void save_image(std::string icon_path) {
dbc::check($render != nullptr, "You have to call run() first.");
fs::path out_path{icon_path};
if(fs::exists(out_path)) {
fs::remove(out_path);
}
sf::Image out_img = $render->getTexture().copyToImage();
bool worked = out_img.saveToFile(out_path);
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);
$render->setSmooth(false);
sf::Vector2f cell_pos{0.0f,0.0f};
for(each_row_t<MapGrid> it{map}; it.next();) {
// a 0 slot means we're done
if(map[it.y][it.x] == 0) break;
cell_pos.x = it.x * $size.x;
cell_pos.y = it.y * $size.y;
bool is_centered = centered[it.y][it.x];
wchar_t display_char = map[it.y][it.x];
std::wstring content{display_char};
best_size(display_char);
sf::Text icon{$font, content, $font_size};
icon.setFillColor({0, 0, 0, 255});
$temp_render.draw(icon);
$temp_render.clear({0,0,0,0});
auto font_texture = $font.getTexture($font_size);
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");
if(is_centered) {
sf::Vector2f center{
(float($size.x) - float(t_size.x)) / 2.0f,
(float($size.y) - float(t_size.y)) / 2.0f};
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.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)};
sprite.setScale(scale);
sprite.setPosition(cell_pos);
}
sprite.setColor(DEFAULT_COLOR);
$render->draw(sprite);
}
$render->display();
}
};
void load_config(MapGrid& map, BoolGrid& centered, bool is_centered, each_row_t<MapGrid>& it, std::string path,
std::function<wchar_t(nlohmann::json&)> finder) {
Config tiles(path);
for(auto [key, val] : tiles.json().items()) {
it.next();
map[it.y][it.x] = finder(val);
centered[it.y][it.x] = is_centered;
}
}
wchar_t component_display(nlohmann::json& val) {
auto& components = val["components"];
for(auto& comp : components) {
if(comp["_type"] == "Tile") {
return comp["display"];
}
}
dbc::log("BAD CHAR");
return L'!';
}
int main() {
MapGrid map = make<wchar_t>(TILE_COUNT, TILE_COUNT);
BoolGrid centered = make<bool>(TILE_COUNT, TILE_COUNT);
each_row_t<MapGrid> it{map};
load_config(map, centered, false, it, "./assets/tiles.json", [](nlohmann::json& val) -> wchar_t {
return val["display"];
});
load_config(map, centered, true, it, "./assets/items.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);
MapTileBuilder builder(DEFAULT_DIM, DEFAULT_DIM);
builder.run(map, it.width, it.y, centered);
builder.save_image("./assets/map_tiles.png");
return 0;
}