I can now apply shaders to any GUI element, but I need a shader manager that will allow for hot reloading and tracking input/output variables.

This commit is contained in:
Zed A. Shaw 2025-04-13 15:17:08 -04:00
parent 80b4faf940
commit a5b8e411e3
8 changed files with 121 additions and 16 deletions

View file

@ -65,6 +65,33 @@ namespace guecs {
shape->setFillColor(color);
}
void Shader::init(lel::Cell &cell) {
ptr = std::make_shared<sf::Shader>();
bool good = ptr->loadFromFile(name, sf::Shader::Type::Fragment);
dbc::check(good, fmt::format("failed to load shader {}", name));
ptr->setUniform("u_resolution", sf::Vector2f({float(cell.w), float(cell.h)}));
clock = std::make_shared<sf::Clock>();
}
void Shader::step() {
sf::Time u_time = clock->getElapsedTime();
float current_time = u_time.asSeconds();
if(current_time < u_time_end) {
ptr->setUniform("u_time", current_time);
} else {
active = false;
}
}
void Shader::run() {
active = true;
sf::Time u_time = clock->getElapsedTime();
u_time_end = u_time.asSeconds() + duration;
ptr->setUniform("u_duration", duration);
ptr->setUniform("u_time_end", u_time_end);
}
UI::UI() {
$font = make_shared<sf::Font>(FONT_FILE_NAME);
@ -114,6 +141,10 @@ namespace guecs {
rect.init(cell);
});
$world.query<lel::Cell, Shader>([](auto, auto& cell, auto& shader) {
shader.init(cell);
});
$world.query<Rectangle, Meter>([](auto, auto& bg, auto &) {
bg.shape->setFillColor(ColorValue::BLACK);
});
@ -152,27 +183,31 @@ namespace guecs {
window.draw(*bg.shape);
}
$world.query<Rectangle>([&](auto, auto& rect) {
window.draw(*rect.shape);
$world.query<Shader>([&](auto, auto& shader) {
if(shader.active) shader.step();
});
$world.query<lel::Cell, Meter>([&](auto, auto& cell, const auto &meter) {
$world.query<Rectangle>([&](auto ent, auto& rect) {
render_helper(window, ent, true, rect.shape);
});
$world.query<lel::Cell, Meter>([&](auto ent, auto& cell, const auto &meter) {
float level = std::clamp(meter.percent, 0.0f, 1.0f) * float(cell.w);
// ZED: this 6 is a border width, make it a thing
meter.bar.shape->setSize({std::max(level, 0.0f), float(cell.h - 6)});
window.draw(*meter.bar.shape);
render_helper(window, ent, true, meter.bar.shape);
});
$world.query<Sprite>([&](auto, auto& sprite) {
window.draw(*sprite.sprite);
$world.query<Sprite>([&](auto ent, auto& sprite) {
render_helper(window, ent, false, sprite.sprite);
});
$world.query<Label>([&](auto, auto& text) {
window.draw(*text.text);
$world.query<Label>([&](auto ent, auto& text) {
render_helper(window, ent, false, text.text);
});
$world.query<Textual>([&](auto, auto& text) {
window.draw(*text.text);
$world.query<Textual>([&](auto ent, auto& text) {
render_helper(window, ent, true, text.text);
});
}
@ -183,6 +218,11 @@ namespace guecs {
if((x >= cell.x && x <= cell.x + cell.w) &&
(y >= cell.y && y <= cell.y + cell.h))
{
if($world.has<Shader>(ent)) {
auto& shader = $world.get<Shader>(ent);
shader.run();
}
if(auto action_data = get_if<ActionData>(ent)) {
clicked.action(ent, action_data->data);
} else {