Moved everything around to let meson build the libraries, but I suspect I have too much SFML support gear for it to be useable.

This commit is contained in:
Zed A. Shaw 2025-05-07 12:21:34 -04:00
parent 560f506733
commit 838f54a4f4
22 changed files with 36 additions and 26 deletions

19
include/config.hpp Normal file
View file

@ -0,0 +1,19 @@
#pragma once
#include <nlohmann/json.hpp>
#include <fstream>
#include <codecvt>
struct Config {
nlohmann::json $config;
std::string $src_path;
Config(const std::string src_path);
Config(nlohmann::json config, std::string src_path)
: $config(config), $src_path(src_path) {}
nlohmann::json &operator[](const std::string &key);
nlohmann::json &json() { return $config; };
std::wstring wstring(const std::string main_key, const std::string sub_key);
std::vector<std::string> keys();
};

16
include/constants.hpp Normal file
View file

@ -0,0 +1,16 @@
#pragma once
#include <string>
#include <array>
constexpr const int SCREEN_WIDTH=1280;
constexpr const int SCREEN_HEIGHT=720;
constexpr const bool VSYNC=false;
constexpr const int FRAME_LIMIT=60;
#ifdef NDEBUG
constexpr const bool DEBUG_BUILD=false;
#else
constexpr const bool DEBUG_BUILD=true;
#endif

50
include/dbc.hpp Normal file
View file

@ -0,0 +1,50 @@
#pragma once
#include <string>
#include <fmt/core.h>
#include <functional>
#include <source_location>
using std::string;
namespace dbc {
class Error {
public:
const string message;
Error(string m) : message{m} {}
Error(const char *m) : message{m} {}
};
class CheckError : public Error {};
class SentinelError : public Error {};
class PreCondError : public Error {};
class PostCondError : public Error {};
void log(const string &message,
const std::source_location location =
std::source_location::current());
[[noreturn]] void sentinel(const string &message,
const std::source_location location =
std::source_location::current());
void pre(const string &message, bool test,
const std::source_location location =
std::source_location::current());
void pre(const string &message, std::function<bool()> tester,
const std::source_location location =
std::source_location::current());
void post(const string &message, bool test,
const std::source_location location =
std::source_location::current());
void post(const string &message, std::function<bool()> tester,
const std::source_location location =
std::source_location::current());
void check(bool test, const string &message,
const std::source_location location =
std::source_location::current());
}

233
include/guecs.hpp Normal file
View file

@ -0,0 +1,233 @@
#pragma once
#include "dbc.hpp"
#include "lel.hpp"
#include <string>
#include <memory>
#include <functional>
#include <any>
#include <queue>
#include <typeindex>
#include <unordered_map>
#include "sfml/components.hpp"
namespace guecs {
using std::shared_ptr, std::wstring, std::string;
using Entity = unsigned long;
using EntityMap = std::unordered_map<Entity, size_t>;
template <typename T>
struct ComponentStorage {
std::vector<T> data;
std::queue<size_t> free_indices;
};
struct Clickable {
/* This is actually called by UI::mouse and passed the entity ID of the
* button pressed so you can interact with it in the event handler.
*/
std::function<void(Entity ent, std::any data)> action;
};
struct ActionData {
std::any data;
};
struct CellName {
string name;
};
class UI {
public:
Entity MAIN = 0;
unsigned long entity_count = 1;
std::unordered_map<std::type_index, EntityMap> $components;
std::unordered_map<std::type_index, std::any> $component_storages;
std::unordered_map<string, Entity> $name_ents;
shared_ptr<sf::Font> $font = nullptr;
lel::Parser $parser;
string $grid = "";
UI();
void position(int x, int y, int width, int height);
sf::Vector2f get_position();
sf::Vector2f get_size();
void layout(const string& grid);
Entity init_entity(const string& name);
Entity entity(const string& name);
Entity entity(const string& name, int id);
inline lel::CellMap& cells() {
return $parser.cells;
}
void init();
void render(sf::RenderWindow& window);
bool mouse(float x, float y, bool hover);
void click_on(const string& name, bool required=false);
void click_on(Entity slot_id);
void debug_layout(sf::RenderWindow& window);
Entity entity() { return ++entity_count; }
template <typename Comp>
size_t make_component() {
auto &storage = component_storage_for<Comp>();
size_t index;
if(!storage.free_indices.empty()) {
index = storage.free_indices.front();
storage.free_indices.pop();
} else {
storage.data.emplace_back();
index = storage.data.size() - 1;
}
return index;
}
template <typename Comp>
ComponentStorage<Comp> &component_storage_for() {
auto type_index = std::type_index(typeid(Comp));
$component_storages.try_emplace(type_index, ComponentStorage<Comp>{});
return std::any_cast<ComponentStorage<Comp> &>(
$component_storages.at(type_index));
}
template <typename Comp>
EntityMap &entity_map_for() {
return $components[std::type_index(typeid(Comp))];
}
template <typename Comp>
void set(Entity ent, Comp val) {
EntityMap &map = entity_map_for<Comp>();
if(has<Comp>(ent)) {
get<Comp>(ent) = val;
return;
}
map.insert_or_assign(ent, make_component<Comp>());
get<Comp>(ent) = val;
}
template <typename Comp>
Comp& get(Entity ent) {
EntityMap &map = entity_map_for<Comp>();
auto &storage = component_storage_for<Comp>();
auto index = map.at(ent);
return storage.data[index];
}
template <typename Comp>
Comp* get_if(Entity entity) {
EntityMap &map = entity_map_for<Comp>();
auto &storage = component_storage_for<Comp>();
if(map.contains(entity)) {
auto index = map.at(entity);
return &storage.data[index];
} else {
return nullptr;
}
}
template <typename Comp>
bool has(Entity ent) {
EntityMap &map = entity_map_for<Comp>();
return map.contains(ent);
}
template <typename Comp>
void remove(Entity ent) {
EntityMap &map = entity_map_for<Comp>();
if(map.contains(ent)) {
size_t index = map.at(ent);
component_storage_for<Comp>().free_indices.push(index);
}
map.erase(ent);
}
template <typename Comp>
void query(std::function<void(Entity, Comp &)> cb) {
EntityMap &map = entity_map_for<Comp>();
for(auto &[entity, index] : map) {
cb(entity, get<Comp>(entity));
}
}
template <typename CompA, typename CompB>
void query(std::function<void(Entity, CompA &, CompB &)> cb) {
EntityMap &map_a = entity_map_for<CompA>();
EntityMap &map_b = entity_map_for<CompB>();
for(auto &[entity, index_a] : map_a) {
if(map_b.contains(entity)) {
cb(entity, get<CompA>(entity), get<CompB>(entity));
}
}
}
template <typename Comp>
void set_init(Entity ent, Comp val) {
dbc::check(has<lel::Cell>(ent),"WRONG! slot is missing its cell?!");
auto& cell = get<lel::Cell>(ent);
val.init(cell);
set<Comp>(ent, val);
}
template <typename Comp>
void do_if(Entity ent, std::function<void(Comp &)> cb) {
if(has<Comp>(ent)) {
cb(get<Comp>(ent));
}
}
lel::Cell& cell_for(Entity ent) {
return get<lel::Cell>(ent);
}
lel::Cell& cell_for(const string& name) {
Entity ent = entity(name);
return get<lel::Cell>(ent);
}
// BUG: close could just be remove with overload
template <typename Comp>
void close(string region) {
auto ent = entity(region);
if(has<Comp>(ent)) {
remove<Comp>(ent);
}
}
template<typename T>
void render_helper(sf::RenderWindow& window, Entity ent, bool is_shape, T& target) {
sf::Shader *shader_ptr = nullptr;
if(auto shader = get_if<Effect>(ent)) {
if(shader->$active && !is_shape) {
auto ptr = shader->checkout_ptr();
ptr->setUniform("is_shape", is_shape);
// NOTE: this is needed because SFML doesn't handle shared_ptr
shader_ptr = ptr.get();
}
}
window.draw(*target, shader_ptr);
}
void show_sprite(const string& region, const string& sprite_name);
void show_text(const string& region, const wstring& content);
void show_label(const string& region, const wstring& content);
};
wstring to_wstring(const string& str);
}

54
include/lel.hpp Normal file
View file

@ -0,0 +1,54 @@
#pragma once
#include <string>
#include <unordered_map>
#include <optional>
#include <vector>
namespace lel {
struct Cell {
int x = 0;
int y = 0;
int w = 0;
int h = 0;
int mid_x = 0;
int mid_y = 0;
int max_w = 0;
int max_h = 0;
int col = 0;
int row = 0;
bool right = false;
bool bottom = false;
bool expand = false;
bool center = false;
bool percent = false;
Cell(int col, int row) : col(col), row(row) {}
Cell() {}
};
using Row = std::vector<std::string>;
using CellMap = std::unordered_map<std::string, Cell>;
struct Parser {
int grid_x = 0;
int grid_y = 0;
int grid_w = 0;
int grid_h = 0;
Cell cur;
std::vector<Row> grid;
CellMap cells;
Parser(int x, int y, int width, int height);
Parser();
void position(int x, int y, int width, int height);
void id(std::string name);
void reset();
bool parse(std::string input);
void finalize();
std::optional<std::string> hit(int x, int y);
};
Cell center(int width, int height, Cell &parent);
}

15
include/sfml/color.hpp Normal file
View file

@ -0,0 +1,15 @@
#pragma once
#include <SFML/Graphics/Color.hpp>
namespace ColorValue {
constexpr const sf::Color BLACK{0, 0, 0};
constexpr const sf::Color DARK_DARK{10, 10, 10};
constexpr const sf::Color DARK_MID{30, 30, 30};
constexpr const sf::Color DARK_LIGHT{60, 60, 60};
constexpr const sf::Color MID{100, 100, 100};
constexpr const sf::Color LIGHT_DARK{150, 150, 150};
constexpr const sf::Color LIGHT_MID{200, 200, 200};
constexpr const sf::Color LIGHT_LIGHT{230, 230, 230};
constexpr const sf::Color WHITE{255, 255, 255};
constexpr const sf::Color TRANSPARENT = sf::Color::Transparent;
}

120
include/sfml/components.hpp Normal file
View file

@ -0,0 +1,120 @@
#pragma once
#include "dbc.hpp"
#include "sfml/color.hpp"
#include "lel.hpp"
#include <string>
#include <memory>
#include <SFML/Graphics.hpp>
#include <functional>
#include <any>
namespace guecs {
using std::shared_ptr, std::wstring, std::string;
constexpr const int PADDING = 3;
constexpr const int BORDER_PX = 1;
constexpr const int TEXT_SIZE = 30;
constexpr const int LABEL_SIZE = 20;
constexpr const sf::Color FILL_COLOR = ColorValue::DARK_MID;
constexpr const sf::Color TEXT_COLOR = ColorValue::LIGHT_LIGHT;
constexpr const sf::Color BG_COLOR = ColorValue::MID;
constexpr const sf::Color BORDER_COLOR = ColorValue::MID;
constexpr const char *FONT_FILE_NAME="assets/text.otf";
struct Textual {
std::wstring content;
unsigned int size = TEXT_SIZE;
sf::Color color = TEXT_COLOR;
int padding = PADDING;
bool centered = false;
shared_ptr<sf::Font> font = nullptr;
shared_ptr<sf::Text> text = nullptr;
void init(lel::Cell &cell, shared_ptr<sf::Font> font_ptr);
void update(const std::wstring& new_content);
};
struct Label : public Textual {
template<typename... Args>
Label(Args... args) : Textual(args...)
{
centered = true;
size = LABEL_SIZE;
}
Label() {
centered = true;
};
};
struct Sprite {
string name;
int padding = PADDING;
std::shared_ptr<sf::Sprite> sprite = nullptr;
void init(lel::Cell &cell);
void update(const string& new_name);
};
struct Rectangle {
int padding = PADDING;
sf::Color color = FILL_COLOR;
sf::Color border_color = BORDER_COLOR;
int border_px = BORDER_PX;
shared_ptr<sf::RectangleShape> shape = nullptr;
void init(lel::Cell& cell);
};
struct Meter {
float percent = 1.0f;
sf::Color color = ColorValue::BLACK;
Rectangle bar;
void init(lel::Cell& cell);
void render(lel::Cell& cell);
};
struct Effect {
float duration = 0.1f;
string name{"ui_shader"};
float $u_time_end = 0.0;
bool $active = false;
std::shared_ptr<sf::Clock> $clock = nullptr;
std::shared_ptr<sf::Shader> $shader = nullptr;
int $shader_version = 0;
void init(lel::Cell &cell);
void run();
void stop();
void step();
shared_ptr<sf::Shader> checkout_ptr();
};
struct Sound {
string on_click{"ui_click"};
void play(bool hover);
void stop(bool hover);
};
struct Background {
float x = 0.0f;
float y = 0.0f;
float w = 0.0f;
float h = 0.0f;
sf::Color color = BG_COLOR;
shared_ptr<sf::RectangleShape> shape = nullptr;
Background(lel::Parser& parser, sf::Color bg_color=BG_COLOR) :
x(parser.grid_x),
y(parser.grid_y),
w(parser.grid_w),
h(parser.grid_h),
color(bg_color)
{}
Background() {}
void init();
};
}

27
include/sfml/shaders.hpp Normal file
View file

@ -0,0 +1,27 @@
#pragma once
#include <cstdint>
#include <vector>
#include <string>
#include <SFML/Graphics.hpp>
#include <unordered_map>
#include <memory>
#include <nlohmann/json.hpp>
namespace shaders {
struct Record {
std::string name;
std::string file_name;
std::shared_ptr<sf::Shader> ptr = nullptr;
};
struct ShaderManager {
std::unordered_map<std::string, Record> shaders;
};
std::shared_ptr<sf::Shader> get(const std::string& name);
void init();
bool load_shader(std::string& name, nlohmann::json& settings);
bool updated(int my_version);
int reload();
int version();
}

26
include/sfml/sound.hpp Normal file
View file

@ -0,0 +1,26 @@
#pragma once
#include <string>
#include <filesystem>
#include <memory>
#include <unordered_map>
#include <SFML/Audio.hpp>
namespace sound {
struct SoundPair {
std::shared_ptr<sf::SoundBuffer> buffer;
std::shared_ptr<sf::Sound> sound;
};
struct SoundManager {
std::unordered_map<std::string, SoundPair> sounds;
};
void init();
void load(const std::string& name, const std::string& path);
void play(const std::string& name, bool loop=false);
void play_at(const std::string& name, float x, float y, float z);
void stop(const std::string& name);
void mute(bool setting);
bool playing(const std::string& name);
SoundPair& get_sound_pair(const std::string& name);
}

27
include/sfml/textures.hpp Normal file
View file

@ -0,0 +1,27 @@
#pragma once
#include <cstdint>
#include <vector>
#include <string>
#include <SFML/Graphics.hpp>
#include <unordered_map>
#include <memory>
namespace textures {
struct SpriteTexture {
std::shared_ptr<sf::Sprite> sprite = nullptr;
std::shared_ptr<sf::Texture> texture = nullptr;
};
struct TextureManager {
std::vector<sf::Image> surfaces;
std::unordered_map<std::string, SpriteTexture> sprite_textures;
std::unordered_map<wchar_t, int> char_to_texture;
};
void init();
SpriteTexture get(const std::string& name);
sf::Image load_image(const std::string& filename);
}