Move everything under the guecs/ directory in src/ so that it meshes better with other projects.
This commit is contained in:
parent
f520f0bade
commit
3bc05ad164
30 changed files with 74 additions and 73 deletions
38
src/guecs/sfml/backend.cpp
Normal file
38
src/guecs/sfml/backend.cpp
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
#include "guecs/sfml/backend.hpp"
|
||||
#include "guecs/sfml/shaders.hpp"
|
||||
#include "guecs/sfml/sound.hpp"
|
||||
#include "guecs/sfml/textures.hpp"
|
||||
|
||||
namespace sfml {
|
||||
guecs::SpriteTexture Backend::texture_get(const string& name) {
|
||||
auto sp = textures::get(name);
|
||||
return {sp.sprite, sp.texture};
|
||||
}
|
||||
|
||||
Backend::Backend() {
|
||||
sound::init();
|
||||
shaders::init();
|
||||
textures::init();
|
||||
}
|
||||
|
||||
void Backend::sound_play(const string& name) {
|
||||
sound::play(name);
|
||||
}
|
||||
|
||||
void Backend::sound_stop(const string& name) {
|
||||
sound::stop(name);
|
||||
}
|
||||
|
||||
std::shared_ptr<sf::Shader> Backend::shader_get(const std::string& name) {
|
||||
return shaders::get(name);
|
||||
}
|
||||
|
||||
bool Backend::shader_updated() {
|
||||
if(shaders::updated($shaders_version)) {
|
||||
$shaders_version = shaders::version();
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
138
src/guecs/sfml/components.cpp
Normal file
138
src/guecs/sfml/components.cpp
Normal file
|
|
@ -0,0 +1,138 @@
|
|||
#include "guecs/ui.hpp"
|
||||
#include "guecs/sfml/backend.hpp"
|
||||
|
||||
namespace guecs {
|
||||
static Backend* BACKEND = nullptr;
|
||||
|
||||
using std::make_shared;
|
||||
|
||||
void init(Backend* backend) {
|
||||
BACKEND = backend;
|
||||
}
|
||||
|
||||
void Textual::init(lel::Cell &cell, shared_ptr<sf::Font> font_ptr) {
|
||||
dbc::check(font_ptr != nullptr, "you failed to initialize this WideText");
|
||||
if(font == nullptr) font = font_ptr;
|
||||
if(text == nullptr) text = make_shared<sf::Text>(*font, content, size);
|
||||
text->setFillColor(color);
|
||||
|
||||
if(centered) {
|
||||
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 {
|
||||
text->setPosition({float(cell.x + padding * 2), float(cell.y + padding * 2)});
|
||||
}
|
||||
|
||||
text->setCharacterSize(size);
|
||||
}
|
||||
|
||||
void Textual::update(const wstring& new_content) {
|
||||
content = new_content;
|
||||
text->setString(content);
|
||||
}
|
||||
|
||||
void Sprite::update(const string& new_name) {
|
||||
if(new_name != name) {
|
||||
name = new_name;
|
||||
auto sprite_texture = BACKEND->texture_get(name);
|
||||
sprite->setTexture(*sprite_texture.texture);
|
||||
sprite->setTextureRect(sprite_texture.sprite->getTextureRect());
|
||||
}
|
||||
}
|
||||
|
||||
void Sprite::init(lel::Cell &cell) {
|
||||
auto sprite_texture = BACKEND->texture_get(name);
|
||||
|
||||
sprite = make_shared<sf::Sprite>(
|
||||
*sprite_texture.texture,
|
||||
sprite_texture.sprite->getTextureRect());
|
||||
|
||||
sprite->setPosition({
|
||||
float(cell.x + padding),
|
||||
float(cell.y + padding)});
|
||||
|
||||
auto bounds = sprite->getLocalBounds();
|
||||
|
||||
sprite->setScale({
|
||||
float(cell.w - padding * 2) / bounds.size.x,
|
||||
float(cell.h - padding * 2) / bounds.size.y});
|
||||
}
|
||||
|
||||
void Rectangle::init(lel::Cell& cell) {
|
||||
sf::Vector2f size{float(cell.w) - padding * 2, float(cell.h) - padding * 2};
|
||||
if(shape == nullptr) shape = make_shared<sf::RectangleShape>(size);
|
||||
shape->setPosition({float(cell.x + padding), float(cell.y + padding)});
|
||||
shape->setFillColor(color);
|
||||
shape->setOutlineColor(border_color);
|
||||
shape->setOutlineThickness(border_px);
|
||||
}
|
||||
|
||||
|
||||
void Meter::init(lel::Cell& cell) {
|
||||
bar.init(cell);
|
||||
}
|
||||
|
||||
void Meter::render(lel::Cell& cell) {
|
||||
float level = std::clamp(percent, 0.0f, 1.0f) * float(cell.w);
|
||||
// ZED: this 6 is a border width, make it a thing
|
||||
bar.shape->setSize({std::max(level, 0.0f), float(cell.h - 6)});
|
||||
}
|
||||
|
||||
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() {
|
||||
sf::Vector2f size{float(w), float(h)};
|
||||
if(shape == nullptr) shape = make_shared<sf::RectangleShape>(size);
|
||||
shape->setPosition({float(x), float(y)});
|
||||
shape->setFillColor(color);
|
||||
}
|
||||
|
||||
void Effect::init(lel::Cell &cell) {
|
||||
$shader = BACKEND->shader_get(name);
|
||||
$shader->setUniform("u_resolution", sf::Vector2f({float(cell.w), float(cell.h)}));
|
||||
$clock = std::make_shared<sf::Clock>();
|
||||
}
|
||||
|
||||
void Effect::step() {
|
||||
sf::Time cur_time = $clock->getElapsedTime();
|
||||
float u_time = cur_time.asSeconds();
|
||||
|
||||
if(u_time < $u_time_end) {
|
||||
$shader->setUniform("u_duration", duration);
|
||||
$shader->setUniform("u_time_end", $u_time_end);
|
||||
$shader->setUniform("u_time", u_time);
|
||||
} else {
|
||||
$active = false;
|
||||
}
|
||||
}
|
||||
|
||||
void Effect::run() {
|
||||
$active = true;
|
||||
sf::Time u_time = $clock->getElapsedTime();
|
||||
$u_time_end = u_time.asSeconds() + duration;
|
||||
}
|
||||
|
||||
shared_ptr<sf::Shader> Effect::checkout_ptr() {
|
||||
if(BACKEND->shader_updated()) {
|
||||
$shader = BACKEND->shader_get(name);
|
||||
}
|
||||
|
||||
return $shader;
|
||||
}
|
||||
|
||||
void Effect::stop() {
|
||||
$active = false;
|
||||
}
|
||||
}
|
||||
35
src/guecs/sfml/config.cpp
Normal file
35
src/guecs/sfml/config.cpp
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
#include "guecs/sfml/config.hpp"
|
||||
#include "guecs/dbc.hpp"
|
||||
#include <fmt/core.h>
|
||||
|
||||
using nlohmann::json;
|
||||
using fmt::format;
|
||||
|
||||
Config::Config(const std::string src_path) : $src_path(src_path) {
|
||||
std::ifstream infile($src_path);
|
||||
$config = json::parse(infile);
|
||||
}
|
||||
|
||||
json &Config::operator[](const std::string &key) {
|
||||
dbc::check($config.contains(key), fmt::format("ERROR in config, key {} doesn't exist.", key));
|
||||
return $config[key];
|
||||
}
|
||||
|
||||
std::wstring Config::wstring(const std::string main_key, const std::string sub_key) {
|
||||
dbc::check($config.contains(main_key), fmt::format("ERROR wstring main/key in config, main_key {} doesn't exist.", main_key));
|
||||
dbc::check($config[main_key].contains(sub_key), fmt::format("ERROR wstring in config, main_key/key {}/{} doesn't exist.", main_key, sub_key));
|
||||
|
||||
const std::string& str_val = $config[main_key][sub_key];
|
||||
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> $converter;
|
||||
return $converter.from_bytes(str_val);
|
||||
}
|
||||
|
||||
std::vector<std::string> Config::keys() {
|
||||
std::vector<std::string> the_fucking_keys;
|
||||
|
||||
for(auto& [key, value] : $config.items()) {
|
||||
the_fucking_keys.push_back(key);
|
||||
}
|
||||
|
||||
return the_fucking_keys;
|
||||
}
|
||||
77
src/guecs/sfml/shaders.cpp
Normal file
77
src/guecs/sfml/shaders.cpp
Normal file
|
|
@ -0,0 +1,77 @@
|
|||
#include "guecs/sfml/shaders.hpp"
|
||||
#include "guecs/sfml/config.hpp"
|
||||
#include "guecs/dbc.hpp"
|
||||
#include <SFML/Graphics/Image.hpp>
|
||||
#include <fmt/core.h>
|
||||
#include <memory>
|
||||
|
||||
namespace shaders {
|
||||
using std::shared_ptr, std::make_shared;
|
||||
|
||||
static ShaderManager SMGR;
|
||||
static bool INITIALIZED = false;
|
||||
static int VERSION = 0;
|
||||
|
||||
inline void configure_shader_defaults(std::shared_ptr<sf::Shader> ptr) {
|
||||
ptr->setUniform("source", sf::Shader::CurrentTexture);
|
||||
}
|
||||
|
||||
bool load_shader(std::string name, nlohmann::json& settings) {
|
||||
std::string file_name = settings["file_name"];
|
||||
auto ptr = std::make_shared<sf::Shader>();
|
||||
bool good = ptr->loadFromFile(file_name, sf::Shader::Type::Fragment);
|
||||
|
||||
if(good) {
|
||||
configure_shader_defaults(ptr);
|
||||
SMGR.shaders.try_emplace(name, name, file_name, ptr);
|
||||
}
|
||||
return good;
|
||||
}
|
||||
|
||||
void init() {
|
||||
if(!INITIALIZED) {
|
||||
dbc::check(sf::Shader::isAvailable(), "no shaders?!");
|
||||
INITIALIZED = true;
|
||||
Config config("assets/shaders.json");
|
||||
bool good = load_shader("ERROR", config["ERROR"]);
|
||||
dbc::check(good, "Failed to load ERROR shader. Look in assets/shaders.json");
|
||||
|
||||
for(auto& [name, settings] : config.json().items()) {
|
||||
if(name == "ERROR") continue;
|
||||
|
||||
dbc::check(!SMGR.shaders.contains(name),
|
||||
fmt::format("shader name '{}' duplicated in assets/shaders.json", name));
|
||||
good = load_shader(name, settings);
|
||||
|
||||
if(!good) {
|
||||
dbc::log(fmt::format("failed to load shader {}", name));
|
||||
SMGR.shaders.insert_or_assign(name, SMGR.shaders.at("ERROR"));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::shared_ptr<sf::Shader> get(const std::string& name) {
|
||||
dbc::check(INITIALIZED, "you forgot to shaders::init()");
|
||||
dbc::check(SMGR.shaders.contains(name),
|
||||
fmt::format("shader name '{}' not in assets/shaders.json", name));
|
||||
auto& rec = SMGR.shaders.at(name);
|
||||
return rec.ptr;
|
||||
}
|
||||
|
||||
int reload() {
|
||||
VERSION++;
|
||||
INITIALIZED = false;
|
||||
SMGR.shaders.clear();
|
||||
init();
|
||||
return VERSION;
|
||||
}
|
||||
|
||||
bool updated(int my_version) {
|
||||
return my_version != VERSION;
|
||||
}
|
||||
|
||||
int version() {
|
||||
return VERSION;
|
||||
}
|
||||
};
|
||||
82
src/guecs/sfml/sound.cpp
Normal file
82
src/guecs/sfml/sound.cpp
Normal file
|
|
@ -0,0 +1,82 @@
|
|||
#include "guecs/sfml/sound.hpp"
|
||||
#include "guecs/sfml/config.hpp"
|
||||
#include "guecs/dbc.hpp"
|
||||
#include <fmt/core.h>
|
||||
|
||||
namespace sound {
|
||||
static SoundManager SMGR;
|
||||
static bool initialized = false;
|
||||
static bool muted = false;
|
||||
|
||||
using namespace fmt;
|
||||
using std::make_shared;
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
SoundPair& get_sound_pair(const std::string& name) {
|
||||
dbc::check(initialized, "You need to call sound::init() first");
|
||||
|
||||
if(SMGR.sounds.contains(name)) {
|
||||
// get the sound from the sound map
|
||||
return SMGR.sounds.at(name);
|
||||
} else {
|
||||
dbc::log(fmt::format("Attempted to stop {} sound but not available.",
|
||||
name));
|
||||
return SMGR.sounds.at("blank");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void init() {
|
||||
if(!initialized) {
|
||||
Config assets("assets/config.json");
|
||||
|
||||
for(auto& el : assets["sounds"].items()) {
|
||||
load(el.key(), el.value());
|
||||
}
|
||||
initialized = true;
|
||||
}
|
||||
}
|
||||
|
||||
void load(const std::string& name, const std::string& sound_path) {
|
||||
dbc::check(fs::exists(sound_path), fmt::format("sound file {} does not exist", sound_path));
|
||||
|
||||
// create the buffer and keep in the buffer map
|
||||
auto buffer = make_shared<sf::SoundBuffer>(sound_path);
|
||||
|
||||
// set it on the sound and keep in the sound map
|
||||
auto sound = make_shared<sf::Sound>(*buffer);
|
||||
sound->setRelativeToListener(false);
|
||||
sound->setPosition({0.0f, 0.0f, 1.0f});
|
||||
|
||||
SMGR.sounds.try_emplace(name, buffer, sound);
|
||||
}
|
||||
|
||||
void play(const std::string& name, bool loop) {
|
||||
if(muted) return;
|
||||
auto& pair = get_sound_pair(name);
|
||||
pair.sound->setLooping(loop);
|
||||
// play it
|
||||
pair.sound->play();
|
||||
}
|
||||
|
||||
void stop(const std::string& name) {
|
||||
auto& pair = get_sound_pair(name);
|
||||
pair.sound->stop();
|
||||
}
|
||||
|
||||
bool playing(const std::string& name) {
|
||||
auto& pair = get_sound_pair(name);
|
||||
auto status = pair.sound->getStatus();
|
||||
return status == sf::SoundSource::Status::Playing;
|
||||
}
|
||||
|
||||
void play_at(const std::string& name, float x, float y, float z) {
|
||||
auto& pair = get_sound_pair(name);
|
||||
pair.sound->setPosition({x, y, z});
|
||||
pair.sound->play();
|
||||
}
|
||||
|
||||
void mute(bool setting) {
|
||||
muted = setting;
|
||||
}
|
||||
}
|
||||
59
src/guecs/sfml/textures.cpp
Normal file
59
src/guecs/sfml/textures.cpp
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
#include "guecs/dbc.hpp"
|
||||
#include "guecs/sfml/textures.hpp"
|
||||
#include "guecs/sfml/config.hpp"
|
||||
#include <SFML/Graphics/Image.hpp>
|
||||
#include <fmt/core.h>
|
||||
#include <memory>
|
||||
|
||||
namespace textures {
|
||||
using std::shared_ptr, std::make_shared;
|
||||
|
||||
static TextureManager TMGR;
|
||||
static bool initialized = false;
|
||||
|
||||
void load_sprites() {
|
||||
Config assets("assets/config.json");
|
||||
|
||||
for(auto& [name, settings] : assets["sprites"].items()) {
|
||||
auto texture = make_shared<sf::Texture>(settings["path"]);
|
||||
|
||||
texture->setSmooth(assets["graphics"]["smooth_textures"]);
|
||||
auto sprite = make_shared<sf::Sprite>(*texture);
|
||||
|
||||
int width = settings["frame_width"];
|
||||
int height = settings["frame_height"];
|
||||
sprite->setTextureRect({{0,0}, {width, height}});
|
||||
|
||||
TMGR.sprite_textures.try_emplace(name, sprite, texture);
|
||||
}
|
||||
}
|
||||
|
||||
void init() {
|
||||
if(!initialized) {
|
||||
load_sprites();
|
||||
initialized = true;
|
||||
}
|
||||
}
|
||||
|
||||
SpriteTexture get(const std::string& name) {
|
||||
dbc::check(initialized, "you forgot to call textures::init()");
|
||||
dbc::check(TMGR.sprite_textures.contains(name),
|
||||
fmt::format("!!!!! texture pack does not contain {} sprite", name));
|
||||
|
||||
auto result = TMGR.sprite_textures.at(name);
|
||||
|
||||
dbc::check(result.sprite != nullptr,
|
||||
fmt::format("bad sprite from textures::get named {}", name));
|
||||
dbc::check(result.texture != nullptr,
|
||||
fmt::format("bad texture from textures::get named {}", name));
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
sf::Image load_image(const std::string& filename) {
|
||||
sf::Image texture;
|
||||
bool good = texture.loadFromFile(filename);
|
||||
dbc::check(good, fmt::format("failed to load {}", filename));
|
||||
return texture;
|
||||
}
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue