icongen now makes a sprite sheet for the map which should be easier to work with.
This commit is contained in:
parent
dbc2000434
commit
d6e2b64140
5 changed files with 136 additions and 43 deletions
2
Makefile
2
Makefile
|
@ -45,7 +45,7 @@ else
|
||||||
endif
|
endif
|
||||||
|
|
||||||
debug: build
|
debug: build
|
||||||
gdb --nx -x .gdbinit --ex run --args builddir/zedcaster
|
gdb --nx -x .gdbinit --ex run --args builddir/icongen
|
||||||
|
|
||||||
debug_run: build
|
debug_run: build
|
||||||
gdb --nx -x .gdbinit --batch --ex run --ex bt --ex q --args builddir/zedcaster
|
gdb --nx -x .gdbinit --batch --ex run --ex bt --ex q --args builddir/zedcaster
|
||||||
|
|
BIN
assets/map_tiles.png
Normal file
BIN
assets/map_tiles.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 5.9 KiB |
|
@ -170,7 +170,7 @@ executable('zedcaster',
|
||||||
dependencies: dependencies)
|
dependencies: dependencies)
|
||||||
|
|
||||||
executable('icongen',
|
executable('icongen',
|
||||||
[ 'dbc.cpp', 'tools/icongen.cpp' ],
|
[ 'config.cpp', 'dbc.cpp', 'tools/icongen.cpp' ],
|
||||||
cpp_args: cpp_args,
|
cpp_args: cpp_args,
|
||||||
link_args: link_args,
|
link_args: link_args,
|
||||||
override_options: exe_defaults,
|
override_options: exe_defaults,
|
||||||
|
|
|
@ -131,8 +131,9 @@
|
||||||
* I am horribly bad at trigonometry and graphics algorithms, so if you've got an idea to improve them
|
* I am horribly bad at trigonometry and graphics algorithms, so if you've got an idea to improve them
|
||||||
* or find a bug shoot me an email at help@learncodethehardway.com.
|
* or find a bug shoot me an email at help@learncodethehardway.com.
|
||||||
*/
|
*/
|
||||||
namespace shiterator { using std::vector, std::queue, std::array; using
|
namespace shiterator {
|
||||||
std::min, std::max, std::floor;
|
using std::vector, std::queue, std::array;
|
||||||
|
using std::min, std::max, std::floor;
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
using BaseRow = vector<T>;
|
using BaseRow = vector<T>;
|
||||||
|
|
|
@ -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>
|
||||||
|
|
||||||
int main() {
|
namespace fs = std::filesystem;
|
||||||
unsigned int font_size = 30;
|
constexpr const int TILE_COUNT=10;
|
||||||
sf::Vector2u size{32, 32};
|
|
||||||
|
|
||||||
sf::RenderTexture render{size};
|
using namespace shiterator;
|
||||||
render.setSmooth(false);
|
|
||||||
|
|
||||||
sf::Font font{FONT_FILE_NAME};
|
using MapRow = BaseRow<wchar_t>;
|
||||||
font.setSmooth(false);
|
using MapGrid = Base<wchar_t>;
|
||||||
|
|
||||||
wchar_t display_char = 3848;
|
struct MapTileBuilder {
|
||||||
std::wstring content{display_char};
|
unsigned int $font_size = 20;
|
||||||
sf::Text icon{font, content, font_size};
|
sf::Glyph $glyph;
|
||||||
|
sf::Font $font{FONT_FILE_NAME};
|
||||||
|
sf::Vector2u $size;
|
||||||
|
sf::Vector2u $image_size;
|
||||||
|
sf::RenderTexture $render;
|
||||||
|
sf::RenderTexture $temp_render;
|
||||||
|
|
||||||
icon.setFillColor({0, 0, 0, 255});
|
MapTileBuilder(size_t x, size_t y) :
|
||||||
render.draw(icon);
|
$size(x, y),
|
||||||
render.clear({0,0,0,0});
|
$image_size($size.x * TILE_COUNT, $size.y * TILE_COUNT),
|
||||||
|
$render($image_size),
|
||||||
// fit the glyph in our box height
|
$temp_render($size)
|
||||||
auto glyph = font.getGlyph(content[0], font_size, false);
|
{
|
||||||
|
$font.setSmooth(false);
|
||||||
while(glyph.textureRect.size.y < int(size.y)-1) {
|
$render.setSmooth(false);
|
||||||
font_size++;
|
|
||||||
glyph = font.getGlyph(content[0], font_size, false);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
auto font_texture = font.getTexture(font_size);
|
void best_size(wchar_t for_char) {
|
||||||
sf::Sprite sprite{font_texture, glyph.textureRect};
|
$font_size = 20; // reset the size
|
||||||
auto t_size = glyph.textureRect.size;
|
// fit the glyph in our box height
|
||||||
|
auto temp = $font.getGlyph(for_char, $font_size, false);
|
||||||
|
auto temp_size = $font_size;
|
||||||
|
|
||||||
dbc::check(int(size.x - t_size.x) > 0, "font too big on x");
|
while(temp.textureRect.size.y < int($size.y)-1
|
||||||
dbc::check(int(size.y - t_size.y) > 0, "font too big on y");
|
&& 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;
|
||||||
|
|
||||||
|
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");
|
||||||
|
|
||||||
sf::Vector2f center{
|
sf::Vector2f center{
|
||||||
(float(size.x) - float(t_size.x)) / 2.0f,
|
(float($size.x) - float(t_size.x)) / 2.0f,
|
||||||
(float(size.y) - float(t_size.y)) / 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)};
|
sf::Vector2f scale{float($size.x) / float(t_size.x), float($size.y) / float(t_size.y)};
|
||||||
|
|
||||||
fmt::println("scale={},{}; t_size={},{}; size={},{}",
|
|
||||||
scale.x, scale.y, t_size.x, t_size.y, size.x, size.y);
|
|
||||||
|
|
||||||
sprite.setScale(scale);
|
sprite.setScale(scale);
|
||||||
|
sprite.setPosition(cell_pos);
|
||||||
sprite.setPosition({0,0});
|
|
||||||
sprite.setColor({0, 0, 0, 255});
|
sprite.setColor({0, 0, 0, 255});
|
||||||
|
|
||||||
render.draw(sprite);
|
$render.draw(sprite);
|
||||||
render.display();
|
}
|
||||||
sf::Image out_img = render.getTexture().copyToImage();
|
|
||||||
|
|
||||||
bool worked = out_img.saveToFile("./screenshot.png");
|
$render.display();
|
||||||
dbc::check(worked, "Failed to write screenshot.png");
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
void load_config(MapGrid& map, 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
each_row_t<MapGrid> it{map};
|
||||||
|
|
||||||
|
load_config(map, it, "./assets/tiles.json", [](nlohmann::json& val) -> wchar_t {
|
||||||
|
return val["display"];
|
||||||
|
});
|
||||||
|
|
||||||
|
load_config(map, it, "./assets/items.json", component_display);
|
||||||
|
load_config(map, it, "./assets/devices.json", component_display);
|
||||||
|
load_config(map, it, "./assets/enemies.json", component_display);
|
||||||
|
|
||||||
|
MapTileBuilder builder(32, 32);
|
||||||
|
builder.run(map);
|
||||||
|
|
||||||
|
builder.save_image("./assets/map_tiles.png");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue