|
|
@ -3,60 +3,152 @@ |
|
|
|
#include <SFML/Graphics.hpp> |
|
|
|
#include <SFML/Graphics.hpp> |
|
|
|
#include <SFML/Graphics/RenderWindow.hpp> |
|
|
|
#include <SFML/Graphics/RenderWindow.hpp> |
|
|
|
#include "constants.hpp" |
|
|
|
#include "constants.hpp" |
|
|
|
|
|
|
|
#include "config.hpp" |
|
|
|
|
|
|
|
#include <filesystem> |
|
|
|
|
|
|
|
#include "shiterator.hpp" |
|
|
|
|
|
|
|
#include <functional> |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace fs = std::filesystem; |
|
|
|
|
|
|
|
constexpr const int TILE_COUNT=10; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
using namespace shiterator; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
using MapRow = BaseRow<wchar_t>; |
|
|
|
|
|
|
|
using MapGrid = Base<wchar_t>; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
struct MapTileBuilder { |
|
|
|
|
|
|
|
unsigned int $font_size = 20; |
|
|
|
|
|
|
|
sf::Glyph $glyph; |
|
|
|
|
|
|
|
sf::Font $font{FONT_FILE_NAME}; |
|
|
|
|
|
|
|
sf::Vector2u $size; |
|
|
|
|
|
|
|
sf::Vector2u $image_size; |
|
|
|
|
|
|
|
sf::RenderTexture $render; |
|
|
|
|
|
|
|
sf::RenderTexture $temp_render; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
MapTileBuilder(size_t x, size_t y) : |
|
|
|
|
|
|
|
$size(x, y), |
|
|
|
|
|
|
|
$image_size($size.x * TILE_COUNT, $size.y * TILE_COUNT), |
|
|
|
|
|
|
|
$render($image_size), |
|
|
|
|
|
|
|
$temp_render($size) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
$font.setSmooth(false); |
|
|
|
|
|
|
|
$render.setSmooth(false); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
int main() { |
|
|
|
void best_size(wchar_t for_char) { |
|
|
|
unsigned int font_size = 30; |
|
|
|
$font_size = 20; // reset the size
|
|
|
|
sf::Vector2u size{32, 32}; |
|
|
|
// 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)-1 |
|
|
|
|
|
|
|
&& temp.textureRect.size.x < int($size.x)-1) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
$glyph = temp; |
|
|
|
|
|
|
|
$font_size = temp_size; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
temp_size++; |
|
|
|
|
|
|
|
temp = $font.getGlyph(for_char, temp_size, false); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void save_image(std::string icon_path) { |
|
|
|
|
|
|
|
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) { |
|
|
|
|
|
|
|
sf::Vector2f cell_pos{0.0f,0.0f}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for(each_row_t<MapGrid> it{map}; it.next();) { |
|
|
|
|
|
|
|
// skip empty slots
|
|
|
|
|
|
|
|
if(map[it.y][it.x] == 0) continue; |
|
|
|
|
|
|
|
cell_pos.x = it.x * $size.x; |
|
|
|
|
|
|
|
cell_pos.y = it.y * $size.y; |
|
|
|
|
|
|
|
|
|
|
|
sf::RenderTexture render{size}; |
|
|
|
wchar_t display_char = map[it.y][it.x]; |
|
|
|
render.setSmooth(false); |
|
|
|
std::wstring content{display_char}; |
|
|
|
|
|
|
|
|
|
|
|
sf::Font font{FONT_FILE_NAME}; |
|
|
|
best_size(display_char); |
|
|
|
font.setSmooth(false); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
wchar_t display_char = 3848; |
|
|
|
sf::Text icon{$font, content, $font_size}; |
|
|
|
std::wstring content{display_char}; |
|
|
|
icon.setFillColor({0, 0, 0, 255}); |
|
|
|
sf::Text icon{font, content, font_size}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
icon.setFillColor({0, 0, 0, 255}); |
|
|
|
$temp_render.draw(icon); |
|
|
|
render.draw(icon); |
|
|
|
$temp_render.clear({0,0,0,0}); |
|
|
|
render.clear({0,0,0,0}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// fit the glyph in our box height
|
|
|
|
auto font_texture = $font.getTexture($font_size); |
|
|
|
auto glyph = font.getGlyph(content[0], font_size, false); |
|
|
|
sf::Sprite sprite{font_texture, $glyph.textureRect}; |
|
|
|
|
|
|
|
auto t_size = $glyph.textureRect.size; |
|
|
|
|
|
|
|
|
|
|
|
while(glyph.textureRect.size.y < int(size.y)-1) { |
|
|
|
dbc::check(int($size.x - t_size.x) > 0, "font too big on x"); |
|
|
|
font_size++; |
|
|
|
dbc::check(int($size.y - t_size.y) > 0, "font too big on y"); |
|
|
|
glyph = font.getGlyph(content[0], font_size, false); |
|
|
|
|
|
|
|
|
|
|
|
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), float($size.y) / float(t_size.y)}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sprite.setScale(scale); |
|
|
|
|
|
|
|
sprite.setPosition(cell_pos); |
|
|
|
|
|
|
|
sprite.setColor({0, 0, 0, 255}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$render.draw(sprite); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$render.display(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
auto font_texture = font.getTexture(font_size); |
|
|
|
void load_config(MapGrid& map, each_row_t<MapGrid>& it, std::string path, |
|
|
|
sf::Sprite sprite{font_texture, glyph.textureRect}; |
|
|
|
std::function<wchar_t(nlohmann::json&)> finder) { |
|
|
|
auto t_size = glyph.textureRect.size; |
|
|
|
Config tiles(path); |
|
|
|
|
|
|
|
|
|
|
|
dbc::check(int(size.x - t_size.x) > 0, "font too big on x"); |
|
|
|
for(auto [key, val] : tiles.json().items()) { |
|
|
|
dbc::check(int(size.y - t_size.y) > 0, "font too big on y"); |
|
|
|
it.next(); |
|
|
|
|
|
|
|
map[it.y][it.x] = finder(val); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
sf::Vector2f center{ |
|
|
|
wchar_t component_display(nlohmann::json& val) { |
|
|
|
(float(size.x) - float(t_size.x)) / 2.0f, |
|
|
|
auto& components = val["components"]; |
|
|
|
(float(size.y) - float(t_size.y)) / 2.0f}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sf::Vector2f scale{float(size.x) / float(t_size.x), float(size.y) / float(t_size.y)}; |
|
|
|
for(auto& comp : components) { |
|
|
|
|
|
|
|
if(comp["_type"] == "Tile") { |
|
|
|
|
|
|
|
return comp["display"]; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fmt::println("scale={},{}; t_size={},{}; size={},{}", |
|
|
|
dbc::log("BAD CHAR"); |
|
|
|
scale.x, scale.y, t_size.x, t_size.y, size.x, size.y); |
|
|
|
return L'!'; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
sprite.setScale(scale); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sprite.setPosition({0,0}); |
|
|
|
int main() { |
|
|
|
sprite.setColor({0, 0, 0, 255}); |
|
|
|
MapGrid map = make<wchar_t>(TILE_COUNT, TILE_COUNT); |
|
|
|
|
|
|
|
each_row_t<MapGrid> it{map}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
load_config(map, it, "./assets/tiles.json", [](nlohmann::json& val) -> wchar_t { |
|
|
|
|
|
|
|
return val["display"]; |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
render.draw(sprite); |
|
|
|
load_config(map, it, "./assets/items.json", component_display); |
|
|
|
render.display(); |
|
|
|
load_config(map, it, "./assets/devices.json", component_display); |
|
|
|
sf::Image out_img = render.getTexture().copyToImage(); |
|
|
|
load_config(map, it, "./assets/enemies.json", component_display); |
|
|
|
|
|
|
|
|
|
|
|
bool worked = out_img.saveToFile("./screenshot.png"); |
|
|
|
MapTileBuilder builder(32, 32); |
|
|
|
dbc::check(worked, "Failed to write screenshot.png"); |
|
|
|
builder.run(map); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
builder.save_image("./assets/map_tiles.png"); |
|
|
|
return 0; |
|
|
|
return 0; |
|
|
|
} |
|
|
|
} |
|
|
|