Experiments in using Lua with sol2 and how best to structure the data. Contains a shoot out of SOA, vs AOS vs. MOA. SOA = Struct of Arrays AOS = Array of Structs MOA = Many Array of Structs (kind of a hybrid)
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.
sol2_test/src/guecs/sfml/components.cpp

197 lines
4.6 KiB

#include "guecs/ui.hpp"
#include "guecs/sfml/backend.hpp"
#include <cassert>
#include <iostream>
#include <fmt/core.h>
#include <GLFW/glfw3.h>
namespace guecs {
using std::make_shared;
inline SpriteTexture load_texture(const string& name, bool is_icon) {
return {};
}
void Text::init(lel::Cell &cell, const std::string& font_file) {
// make the text here
if(centered || cell.center) {
// get the text bounds and use lel::center
// auto bounds = text->getLocalBounds();
// auto text_cell = lel::center(bounds.size.x, bounds.size.y, cell);
// this stupid / 2 is because SFML renders from baseline rather than from the claimed bounding box
// text->setPosition({float(text_cell.x), float(text_cell.y) - text_cell.h / 2});
} else {
// CHANGE ME
// text->setPosition({float(cell.x + padding * 2), float(cell.y + padding * 2)});
}
}
void Text::render() {
}
void Text::update(const wstring& new_content) {
content = new_content;
}
void Sprite::update(const string& new_name) {
if(new_name != name) {
name = new_name;
// CHANGE: get the image and save it
}
}
void Sprite::init(lel::Cell &cell) {
if(cell.center) stretch = false;
auto sprite_texture = load_texture(name, is_icon);
// setup the image size and such
glm::vec2 bounds{0.0, 0.0}; // get this from the image
if(stretch) {
// handle stretch
} else {
float box_width = float(cell.w - padding * 2);
float box_height = float(cell.h - padding * 2);
float scale = std::min(box_width / float(bounds.x), box_height / float(bounds.y));
// no stretch so scale it
}
// used the center helper here
}
void Sprite::render() {
}
void Rectangle::init(lel::Cell& cell) {
glm::vec2 size{float(cell.w) - padding * 2.0f, float(cell.h) - padding * 2.0f};
update();
// sfml_center_helper(shape, cell, padding);
}
void Rectangle::render() {
}
void Rectangle::update() {
}
void Meter::init(lel::Cell& cell) {
$cell_x = cell.x;
$cell_y = cell.y;
$cell_w = cell.w;
$cell_h = cell.h;
bar.init(cell);
update();
}
void Meter::render() {
}
void Meter::update() {
bar.color = color;
bar.padding = padding;
bar.update();
if(vertical) {
float level = std::clamp(percent, 0.0f, 1.0f) * float($cell_h);
glm::vec2 size{float($cell_w) - padding * 2.0f, std::max(level, 0.0f) - padding};
// set bar size and position
} else {
float level = std::clamp(percent, 0.0f, 1.0f) * float($cell_w);
// set bar size only
}
}
void Sound::play(bool hover) {
if(!hover) {
BACKEND->sound_play(on_click);
}
}
void Sound::stop(bool hover) {
if(!hover) {
BACKEND->sound_stop(on_click);
}
}
void Background::init() {
assert(w > 0.0f && "Background.w must be > 0.0f. Forgot $gui.$parser?");
assert(h > 0.0f && "Background.h must be > 0.0f. Forgot $gui.$parser?");
glm::vec2 size{float(w), float(h)};
if(shape == nullptr) {
// need a shape thing
} else {
// need a shape thing
}
// set the shape psitions and color
// shape->pos({float(x), float(y)});
// shape->color(color);
}
void Background::set_color(Color& c) {
color = c;
}
void Background::set_sprite(const std::string& name, bool stretch) {
auto sprite_texture = BACKEND->get_sprite(name);
// create the sprite and position/scale
if(stretch) {
// if it's stretched then fill the cell
}
}
void Background::render() {
}
void Effect::init(lel::Cell &cell) {
$shader = BACKEND->get_shader(name);
$shader->setVec2("u_resolution", glm::vec2({float(cell.w), float(cell.h)}));
// do I need a clock?
}
void Effect::render() {
double u_time = glfwGetTime(); // THIS IS WRONG, IT'S WALL TIME SINCE PROGRAM RUN
if(u_time < $u_time_end) {
$shader->setFloat("u_duration", duration);
$shader->setFloat("u_time_end", $u_time_end);
$shader->setFloat("u_time", u_time);
} else {
$active = false;
}
}
void Effect::run() {
$active = true;
// THIS IS WRONG
double u_time = glfwGetTime();
$u_time_end = u_time + duration;
}
Shader* Effect::get_shader(bool is_shape) {
Shader *shader_ptr = nullptr;
if(BACKEND->shader_updated()) {
$shader = BACKEND->get_shader(name);
}
if($active) {
$shader->setBool("is_shape", is_shape);
// NOTE: this is needed because SFML doesn't handle shared_ptr
shader_ptr = $shader.get();
}
return shader_ptr;
}
void Effect::stop() {
$active = false;
}
}