First cut of pulling out the relevant parts of my original game to make a little framework.

This commit is contained in:
Zed A. Shaw 2026-03-22 10:37:45 -04:00
commit 6a0c9e8d46
177 changed files with 18197 additions and 0 deletions

347
tools/animator.cpp Normal file
View file

@ -0,0 +1,347 @@
#define FSM_DEBUG 1
#include "game/sound.hpp"
#include "ai/ai.hpp"
#include <iostream>
#include "graphics/shaders.hpp"
#include "gui/backend.hpp"
#include "constants.hpp"
#include "graphics/animation.hpp"
#include "tools/animator.hpp"
#include <unistd.h>
#include <ranges>
#include <vector>
using namespace std::chrono_literals;
bool YES_SYNC=true;
namespace animator {
void FSM::init(const std::string &sprite_name, const std::string &anim_name, const std::string &background) {
$timer.start();
$sprite_name = sprite_name;
$anim_name = anim_name;
$background = background;
// this loads the animation
reload();
sf::Vector2u new_size{(unsigned int)$anim.sheet.frame_width, (unsigned int)$anim.sheet.frame_height};
$window = sf::RenderWindow(sf::VideoMode(new_size), "Animation Crafting Tool");
$window.setPosition({0,0});
$ui.init($sprite_name, $background, new_size.x, new_size.y);
// need to keep these around
$pos = $ui.sprite->getPosition();
$scale = $ui.sprite->getScale();
if(YES_SYNC) {
$window.setVerticalSyncEnabled(VSYNC);
if(FRAME_LIMIT) $window.setFramerateLimit(FRAME_LIMIT);
}
$ui.update_status($anim);
}
void FSM::event(Event ev, std::any data) {
switch($state) {
FSM_STATE(State, START, ev);
FSM_STATE(State, ANIMATE, ev);
FSM_STATE(State, END, ev);
}
}
void FSM::START(Event ev) {
state(State::ANIMATE);
}
void FSM::ANIMATE(Event ev) {
switch(ev) {
case Event::PLAY_STOP:
if($anim.playing) {
$anim.stop();
} else {
$anim.play();
}
break;
case Event::PREV_FORM:
change_form(-1);
$ui.update_status($anim);
break;
case Event::NEXT_FORM:
change_form(1);
$ui.update_status($anim);
break;
case Event::RELOAD:
reload();
$ui.update_status($anim);
state(State::START);
break;
case Event::TEST_SHADER:
$ui.effect = $ui.effect == nullptr ? shaders::get("flame") : nullptr;
break;
default:
state(State::START);
}
}
void FSM::END(Event ev) {
}
void FSM::change_form(int direction) {
if($anim.forms.size() == 0) {
$ui.show_error("NO FORMS!");
return;
}
$cur_form_i = std::clamp($cur_form_i + direction, 0, int($anim.forms.size()) - 1);
dbc::check($cur_form_i >= 0, "CATASTROPHE! cur_form_i went below 0. How?");
// this is the dumbest shit ever
auto key_view = std::views::keys($anim.forms);
std::vector<std::string> keys(key_view.begin(), key_view.end());
dbc::check(size_t($cur_form_i) < keys.size(), "form index outside of form keys vector");
$cur_form = keys[$cur_form_i];
fmt::println("cur_form_i {}; cur_form: {}", $cur_form_i, $cur_form);
// set_form will stop the animation
$anim.set_form($cur_form);
$anim.play();
}
void FSM::update() {
if($anim.playing) {
$anim.update();
$anim.apply(*$ui.sprite);
if($ui.effect != nullptr) $anim.apply_effect($ui.effect);
$anim.motion(*$ui.sprite, $pos, $scale);
}
}
void FSM::check_changed() {
if($timer.getElapsedTime().toDuration() > 500ms) {
try {
auto mod_time = std::filesystem::last_write_time("assets/animation.json");
if($last_mod_time < mod_time) {
event(Event::RELOAD);
}
$timer.restart();
} catch(const std::filesystem::filesystem_error& err) {
fmt::println("failed to open {}: {}", err.path1().string(), err.what());
$timer.restart();
}
}
}
void FSM::reload() {
animation::Animation new_anim;
try {
new_anim = animation::load("assets/animation.json", $anim_name);
} catch(...) {
$ui.show_error("Failed to load JSON");
return;
}
if(!new_anim.has_form($cur_form)) {
$ui.show_error(fmt::format("No form {}", $cur_form));
$cur_form = "idle";
$cur_form_i = 0;
}
new_anim.set_form($cur_form);
try {
$last_mod_time = std::filesystem::last_write_time("assets/animation.json");
} catch(...) {
$ui.show_error("Filesystem error");
}
if($anim.form_name == new_anim.form_name) {
$ui.clear_error();
}
$anim = new_anim;
$anim.play();
}
void FSM::handle_keyboard_mouse() {
while(const auto ev = $window.pollEvent()) {
using enum game::Event;
using KEY = sf::Keyboard::Scan;
auto gui_ev = $router.process_event(ev);
auto mouse_pos = $window.mapPixelToCoords($router.position);
switch(gui_ev) {
case KEY_PRESS:
if($router.scancode == KEY::Space) {
event(Event::PLAY_STOP);
} else if($router.scancode == KEY::Up) {
event(Event::PREV_FORM);
} else if($router.scancode == KEY::Down) {
event(Event::NEXT_FORM);
} else if($router.scancode == KEY::M) {
$mute = !$mute;
sound::mute($mute);
} else if($router.scancode == KEY::S) {
event(Event::TEST_SHADER);
}
break;
case MOUSE_CLICK:
$ui.mouse(mouse_pos.x, mouse_pos.y, guecs::NO_MODS);
break;
case MOUSE_MOVE:
$ui.mouse(mouse_pos.x, mouse_pos.y, {1 << guecs::ModBit::hover});
break;
case QUIT:
state(State::END);
default:
break; // ignored
}
}
}
void FSM::render() {
$window.clear();
$ui.render($window, false);
$window.display();
}
bool FSM::active() {
return !in_state(State::END);
}
void UI::init(const std::string& sprite_name, const std::string& background, int width, int height) {
$ui.position(0,0, width, height);
$ui.layout("[=viewer]");
if(background != "") {
$ui.set<guecs::Background>($ui.MAIN, {$ui.$parser, guecs::THEME.TRANSPARENT});
auto& bg = $ui.get<guecs::Background>($ui.MAIN);
bg.set_sprite(background, true);
}
auto viewer = $ui.entity("viewer");
// BUG: this is some jank bullshit but it works
$ui.set<guecs::Sprite>(viewer, {sprite_name, 0, false});
$ui.init();
sprite = $ui.get<guecs::Sprite>(viewer).sprite;
dbc::check(sprite != nullptr, "failed to initialize $ui.sprite");
$ui.remove<guecs::Sprite>(viewer);
$overlay.position(0, 0, width/4, height/4);
$overlay.layout(
"[form]"
"[sequence]"
"[transform]"
"[error]");
$overlay.init();
$initialized_this_sucks_ass = true;
}
void UI::render(sf::RenderWindow& window, bool debug) {
$ui.render(window);
window.draw(*sprite, effect.get());
$overlay.render(window);
if(debug) {
$ui.debug_layout(window);
$overlay.debug_layout(window);
}
}
bool UI::mouse(float x, float y, guecs::Modifiers mods) {
return $ui.mouse(x, y, mods);
}
void UI::update_status(animation::Animation& anim) {
$overlay.show_text("form", guecs::to_wstring(anim.form_name));
$overlay.show_text("sequence", guecs::to_wstring(anim.sequence_name));
$overlay.show_text("transform", guecs::to_wstring(anim.transform_name));
}
void UI::show_error(const std::string& message) {
if($initialized_this_sucks_ass) {
$overlay.show_text("error", guecs::to_wstring(message));
} else {
dbc::log(message);
}
}
void UI::clear_error() {
if($initialized_this_sucks_ass) {
$overlay.show_text("error", L"");
}
}
std::shared_ptr<sf::Sprite> UI::get_sprite() {
auto viewer = $ui.entity("viewer");
return $ui.get<guecs::Sprite>(viewer).sprite;
}
}
int error_usage() {
fmt::println("USAGE: animator -h -b <background> -s <sprite> -a <animation>");
return 1;
}
int main(int argc, char* argv[]) {
ai::init("ai");
components::init();
gui::Backend backend;
guecs::init(&backend);
shaders::init();
std::string sprite_name;
std::string background;
std::string anim_name;
int opt = 0;
while((opt = getopt(argc, argv, "hb:s:a:")) != -1) {
switch(opt) {
case 'b':
background = optarg;
break;
case 's':
sprite_name = optarg;
break;
case 'a':
anim_name = optarg;
break;
case 'h': // fallthrough
error_usage();
break;
default:
return error_usage();
break;
}
}
if(sprite_name == "") {
return error_usage();
} else if(anim_name == "") {
anim_name = sprite_name; // default to the same
}
sound::mute(true);
animator::FSM main;
main.init(sprite_name, anim_name, background);
while(main.active()) {
main.update();
main.render();
main.check_changed();
main.handle_keyboard_mouse();
}
return 0;
}

75
tools/animator.hpp Normal file
View file

@ -0,0 +1,75 @@
#pragma once
#include <guecs/ui.hpp>
#include "gui/event_router.hpp"
#include "gui/guecstra.hpp"
#include "events.hpp"
#include <filesystem>
namespace animator {
enum class State {
START=__LINE__,
ANIMATE=__LINE__,
END=__LINE__,
};
enum class Event {
TICK=__LINE__,
PLAY_STOP=__LINE__,
NEXT_FORM=__LINE__,
PREV_FORM=__LINE__,
TEST_SHADER=__LINE__,
RELOAD=__LINE__,
};
struct FSM;
struct UI {
guecs::UI $ui;
guecs::UI $overlay;
std::shared_ptr<sf::Sprite> sprite = nullptr;
std::shared_ptr<sf::Shader> effect = nullptr;
bool $initialized_this_sucks_ass = false;
void button(const std::string& name, std::function<void(guecs::Modifiers mods)> cb);
void init(const std::string& sprite_name, const std::string& background, int width, int height);
void render(sf::RenderWindow& window, bool debug=false);
bool mouse(float x, float y, guecs::Modifiers mods);
void update_status(animation::Animation& anim);
std::shared_ptr<sf::Sprite> get_sprite();
void show_error(const std::string& message);
void clear_error();
};
struct FSM : public DeadSimpleFSM<State, Event> {
UI $ui;
gui::routing::Router $router;
sf::RenderWindow $window;
sf::Vector2f $pos{0,0};
sf::Vector2f $scale{0,0};
animation::Animation $anim;
std::string $sprite_name="";
std::string $anim_name="";
std::string $background="";
std::filesystem::file_time_type $last_mod_time;
sf::Clock $timer;
std::string $cur_form = "idle";
int $cur_form_i = 0;
bool $mute = true;
void init(const std::string &sprite_name, const std::string& background, const std::string &anim_name);
void event(Event ev, std::any data={});
void START(Event ev);
void ANIMATE(Event ev);
void END(Event ev);
void handle_keyboard_mouse();
void render();
bool active();
void update();
void reload();
void check_changed();
void change_form(int direction);
};
}

123
tools/fragviewer.cpp Normal file
View file

@ -0,0 +1,123 @@
#include "graphics/textures.hpp"
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <SFML/Audio.hpp>
#include <SFML/Window/Event.hpp>
#include "dbc.hpp"
#include <fmt/core.h>
#include <unistd.h>
bool SHADER_RELOAD = true;
void Handle_events(sf::RenderWindow &window) {
// is this a main event loop
while (const auto event = window.pollEvent()) {
if(event->is<sf::Event::Closed>()) {
window.close();
} else if(const auto* key = event->getIf<sf::Event::KeyPressed>()) {
using KEY = sf::Keyboard::Scan;
if(key->scancode == KEY::Escape || key->scancode == KEY::Space) {
SHADER_RELOAD = true;
}
}
}
}
int main(int argc, char *argv[]) {
int opt = 0;
bool load_sprite = false;
std::string sprite_name;
std::string frag_name;
sf::Clock clock;
sf::Shader shader;
sf::RectangleShape rect;
textures::SpriteTexture sprite_texture;
sf::Vector2f u_resolution{720.0, 720.0};
sf::Vector2u screen_resolution{720, 720};
textures::init();
dbc::check(sf::Shader::isAvailable(), "You apparently are a time traveler from the 80s who doesn't have shaders.");
while((opt = getopt(argc, argv, "hs:f:x:y:")) != -1) {
switch(opt) {
case 's':
sprite_name = optarg;
load_sprite = true;
break;
case 'f':
frag_name = optarg;
break;
case 'x':
screen_resolution.x = std::atoi(optarg);
u_resolution.x = screen_resolution.x;
break;
case 'y':
screen_resolution.y = std::atoi(optarg);
u_resolution.y = screen_resolution.y;
break;
case 'h':
fmt::println(
"fragviewer is a simple tool to play fragment shaders.\n"
"USAGE: fragviewer [-x size_x -y size_y] [-s sprite_name] -f shader.frag");
return 0;
default:
fmt::println("USAGE: fragviewer [-x size_x -y size_y] [-s sprite_name] -f shader.frag");
return -1;
}
}
fmt::println("sprite is", sprite_name);
dbc::check(frag_name != "", "You must set the -f shader.frag option.");
sf::RenderWindow window(sf::VideoMode(screen_resolution), "SFML Frag Shader Viewer");
window.setFramerateLimit(60);
window.setVerticalSyncEnabled(true);
if(load_sprite) {
sprite_texture = textures::get_sprite(sprite_name);
sprite_texture.sprite->setPosition({0,0});
auto bounds = sprite_texture.sprite->getLocalBounds();
sf::Vector2f scale{u_resolution.x / bounds.size.x,
u_resolution.y / bounds.size.y};
sprite_texture.sprite->setScale(scale);
shader.setUniform("source", sf::Shader::CurrentTexture);
} else {
rect.setPosition({0,0});
rect.setSize(u_resolution);
}
while(window.isOpen()) {
Handle_events(window);
if(SHADER_RELOAD) {
bool good_shader = shader.loadFromFile(frag_name, sf::Shader::Type::Fragment);
if(!good_shader) {
fmt::print("!!!!!! failed to load shader {}\n", frag_name);
}
shader.setUniform("u_resolution", u_resolution);
SHADER_RELOAD = false;
}
sf::Time u_time = clock.getElapsedTime();
sf::Vector2i mouse_at = sf::Mouse::getPosition(window);
sf::Vector2f u_mouse{float(mouse_at.x), float(mouse_at.y)};
shader.setUniform("u_mouse", u_mouse);
shader.setUniform("u_time", u_time.asSeconds());
window.clear();
if(load_sprite) {
window.draw(*sprite_texture.sprite, &shader);
} else {
window.draw(rect, &shader);
}
window.display();
}
}

266
tools/icongen.cpp Normal file
View file

@ -0,0 +1,266 @@
#include <fmt/core.h>
#include "dbc.hpp"
#include <SFML/Graphics.hpp>
#include <SFML/Graphics/RenderWindow.hpp>
#include "constants.hpp"
#include "game/config.hpp"
#include <filesystem>
#include "algos/shiterator.hpp"
#include <functional>
#include <iostream>
#include "graphics/textures.hpp"
#include "graphics/palette.hpp"
namespace fs = std::filesystem;
constexpr const int TILE_COUNT=10;
constexpr const sf::Color DEFAULT_COLOR{255, 255, 255, 255};
using namespace nlohmann;
using namespace shiterator;
using MapRow = BaseRow<wchar_t>;
using MapGrid = Base<wchar_t>;
using BoolRow = BaseRow<bool>;
using BoolGrid = Base<bool>;
struct MapConfig {
MapGrid map = make<wchar_t>(TILE_COUNT, TILE_COUNT);
BoolGrid centered = make<bool>(TILE_COUNT, TILE_COUNT);
std::unordered_map<wchar_t, sf::Color> colors;
std::unordered_map<wchar_t, sf::Color> backgrounds;
std::unordered_map<wchar_t, std::string> names;
each_row_t<MapGrid> it{map};
};
struct MapTileBuilder {
unsigned int $font_size = 20;
sf::Glyph $glyph;
sf::Font $font{FONT_FILE_NAME};
std::shared_ptr<sf::RenderTexture> $render = nullptr;
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({(unsigned int)$size.x, (unsigned int)$size.y})
{
$font.setSmooth(false);
}
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 <= adjusted_size.y
&& temp.textureRect.size.x <= adjusted_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_real_textures(MapConfig &config) {
textures::init();
sf::Vector2u crop{$size.x * (unsigned int)config.it.width, ($size.y) * ((unsigned int)config.it.y + 1)};
$render = std::make_shared<sf::RenderTexture>(crop);
$render->clear({0,0,0,0});
$render->setSmooth(false);
sf::Vector2f cell_pos{0.0f,0.0f};
for(each_row_t<MapGrid> it{config.map}; it.next();) {
wchar_t display_char = config.map[it.y][it.x];
// stop when there's no more cells set
if(display_char == 0) break;
cell_pos.x = it.x * $size.x;
cell_pos.y = it.y * $size.y;
auto& name = config.names.at(display_char);
auto id = textures::get_id(name);
auto& img = textures::get_surface_img(id);
auto img_size = img.getSize();
sf::Texture surface{img};
sf::Vector2f scale{float($size.x) / float(img_size.x),
float($size.y) / float(img_size.y)};
sf::Sprite sprite{surface};
sprite.setScale(scale);
sprite.setPosition(cell_pos);
$render->draw(sprite);
$render->display();
}
}
void run(MapConfig& config) {
sf::Vector2u crop{$size.x * (unsigned int)config.it.width, $size.y * ((unsigned int)config.it.y+1)};
$render = std::make_shared<sf::RenderTexture>(crop);
$render->clear({0,0,0,0});
$render->setSmooth(false);
sf::Vector2f cell_pos{0.0f,0.0f};
sf::RectangleShape background({(float)$size.x, (float)$size.y});
for(each_row_t<MapGrid> it{config.map}; it.next();) {
// a 0 slot means we're done
if(config.map[it.y][it.x] == 0) break;
cell_pos.x = it.x * $size.x;
cell_pos.y = it.y * $size.y;
bool is_centered = config.centered[it.y][it.x];
wchar_t display_char = config.map[it.y][it.x];
std::wstring content{display_char};
auto bg = config.backgrounds.at(display_char);
auto fg = config.colors.at(display_char);
best_size(display_char, is_centered);
sf::Text icon{$font, content, $font_size};
icon.setFillColor({255, 255, 255, 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($size.x - t_size.x >= 0, "font too big on x");
dbc::check($size.y - t_size.y >= 0, "font too big on y");
// draw the background first
background.setFillColor(bg);
if(is_centered) {
sf::Vector2f center{
float(($size.x - t_size.x) / 2),
float(($size.y - t_size.y) / 2)};
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)};
sprite.setScale(scale);
sprite.setPosition(cell_pos);
background.setPosition(cell_pos);
}
sprite.setColor(fg);
$render->draw(background);
$render->draw(sprite);
$render->display();
}
}
void save_config(MapConfig& config, const std::string &path) {
(void)path;
json result = json::array();
for(each_row_t<MapGrid> it{config.map}; it.next();) {
if(config.map[it.y][it.x] == 0) break;
json val;
val["x"] = $size.x * it.x;
val["y"] = $size.y * it.y;
val["display"] = (int)config.map[it.y][it.x];
val["centered"] = config.centered[it.y][it.x];
result.push_back(val);
}
std::ofstream o(path, std::ios::out | std::ios::binary);
o << std::setw(4) << result << std::endl;
}
};
void load_config(MapConfig& config, bool is_centered, std::string path, std::function<json&(json&)> finder)
{
auto tiles = settings::get(path);
for(auto [key, val] : tiles.json().items()) {
config.it.next();
auto data = finder(val);
wchar_t display = data["display"];
config.map[config.it.y][config.it.x] = display;
config.centered[config.it.y][config.it.x] = is_centered;
config.names.insert_or_assign(display, key);
dbc::check(!config.colors.contains(display),
fmt::format("duplicate icon for display={} key={}",
(int)display, (std::string)key));
dbc::check(data.contains("foreground"),
fmt::format("{} has no foreground", std::string(key)));
auto fg = palette::get(data["foreground"]);
config.colors.insert_or_assign(display, fg);
dbc::check(data.contains("background"),
fmt::format("{} has no background", std::string(key)));
auto bg = palette::get(data["background"]);
config.backgrounds.insert_or_assign(display, bg);
}
}
json& component_display(json& val) {
auto& components = val["components"];
for(auto& comp : components) {
if(comp["_type"] == "Tile") {
return comp;
}
}
dbc::log("BAD CHAR");
return val;
}
int main() {
palette::init();
MapConfig config;
load_config(config, false, "tiles", [](json& val) -> json& {
return val;
});
load_config(config, true, "items", component_display);
load_config(config, true, "devices", component_display);
load_config(config, true, "enemies", component_display);
fmt::println("-----------------------------------------");
MapTileBuilder builder(ICONGEN_MAP_TILE_DIM, ICONGEN_MAP_TILE_DIM);
builder.run(config);
builder.save_image("./assets/map_tiles.png");
builder.save_config(config, "./assets/map_tiles.json");
return 0;
}