From 29ec390478354d160ad89d099ea4d3caab94f261 Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Wed, 4 Nov 2026 02:11:33 -0500 Subject: [PATCH] Brought over the lel-guecs code so I could make it work directly with opengl. --- .gitignore | 1 + Makefile | 5 +- meson.build | 31 ++-- src/document.hpp | 12 +- src/guecs/lel.cpp | 130 +++++++++++++++++ src/guecs/lel.hpp | 52 +++++++ src/guecs/lel_parser.cpp | 261 ++++++++++++++++++++++++++++++++++ src/guecs/lel_parser.rl | 67 +++++++++ src/guecs/sfml/backend.cpp | 59 ++++++++ src/guecs/sfml/backend.hpp | 21 +++ src/guecs/sfml/components.cpp | 196 +++++++++++++++++++++++++ src/guecs/sfml/components.hpp | 136 ++++++++++++++++++ src/guecs/sfml/config.cpp | 46 ++++++ src/guecs/sfml/config.hpp | 24 ++++ src/guecs/sfml/shaders.cpp | 77 ++++++++++ src/guecs/sfml/shaders.hpp | 27 ++++ src/guecs/sfml/sound.cpp | 81 +++++++++++ src/guecs/sfml/sound.hpp | 26 ++++ src/guecs/sfml/textures.cpp | 62 ++++++++ src/guecs/sfml/textures.hpp | 28 ++++ src/guecs/theme.cpp | 11 ++ src/guecs/theme.hpp | 57 ++++++++ src/guecs/ui.cpp | 243 +++++++++++++++++++++++++++++++ src/guecs/ui.hpp | 238 +++++++++++++++++++++++++++++++ src/guecs/uistack.hpp | 97 +++++++++++++ src/meson.build | 20 +++ tests/meson.build | 5 + wraps/miniaudio.wrap | 13 ++ wraps/utf8cpp.wrap | 9 ++ 29 files changed, 2004 insertions(+), 31 deletions(-) create mode 100644 src/guecs/lel.cpp create mode 100644 src/guecs/lel.hpp create mode 100644 src/guecs/lel_parser.cpp create mode 100644 src/guecs/lel_parser.rl create mode 100644 src/guecs/sfml/backend.cpp create mode 100644 src/guecs/sfml/backend.hpp create mode 100644 src/guecs/sfml/components.cpp create mode 100644 src/guecs/sfml/components.hpp create mode 100644 src/guecs/sfml/config.cpp create mode 100644 src/guecs/sfml/config.hpp create mode 100644 src/guecs/sfml/shaders.cpp create mode 100644 src/guecs/sfml/shaders.hpp create mode 100644 src/guecs/sfml/sound.cpp create mode 100644 src/guecs/sfml/sound.hpp create mode 100644 src/guecs/sfml/textures.cpp create mode 100644 src/guecs/sfml/textures.hpp create mode 100644 src/guecs/theme.cpp create mode 100644 src/guecs/theme.hpp create mode 100644 src/guecs/ui.cpp create mode 100644 src/guecs/ui.hpp create mode 100644 src/guecs/uistack.hpp create mode 100644 src/meson.build create mode 100644 tests/meson.build create mode 100644 wraps/miniaudio.wrap create mode 100644 wraps/utf8cpp.wrap diff --git a/.gitignore b/.gitignore index ca37570..6d74c4a 100644 --- a/.gitignore +++ b/.gitignore @@ -29,3 +29,4 @@ backup coverage coverage/* .venv +*.cap diff --git a/Makefile b/Makefile index adcab3a..6c6b11f 100644 --- a/Makefile +++ b/Makefile @@ -9,7 +9,10 @@ else sh -x ./scripts/reset_build.sh endif -build: +%.cpp : %.rl + ragel -o $@ $< + +build: src/guecs/lel_parser.cpp meson compile -j 10 -C builddir release_build: diff --git a/meson.build b/meson.build index 4d56e0f..5c18762 100644 --- a/meson.build +++ b/meson.build @@ -7,6 +7,7 @@ project('sol2_test', 'cpp', # use this for common options only for our executables cpp_args=[ + '-I../subprojects/utf8cpp-4.0.9/source', '-Wno-unused-parameter', '-Wno-unused-function', '-Wno-unused-variable', @@ -24,36 +25,22 @@ sol2 = subproject('sol2').get_variable('sol2_dep') glfw3 = subproject('glfw').get_variable('glfw_dep') glm = subproject('glm').get_variable('glm_dep') freetype2 = subproject('freetype2').get_variable('freetype_dep') +miniaudio = subproject('miniaudio').get_variable('ma_dep') +utf8cpp = subproject('utf8cpp').get_variable('utf8cpp_dep') # have to disable tests because this dude's using catch2 from the 1890s magic_enum_proj = subproject('magic_enum', default_options: { 'test': false}) magic_enum = magic_enum_proj.get_variable('magic_enum_dep') -dependencies = [fmt, fuc2, sol2, glfw3, glm, freetype2, magic_enum] - -sources = [ - 'src/glad/gl.cpp', - 'src/stb_image.cpp', - 'src/shader.cpp', - 'src/text.cpp', - 'src/stats.cpp', - 'src/dbc.cpp', - 'src/builder.cpp', - 'src/bezos.cpp', - 'src/document.cpp', - 'src/framebuffer.cpp', - 'src/screen.cpp', - 'src/engine.cpp', - 'src/image.cpp', - 'src/options.cpp', +dependencies = [ + fmt, fuc2, sol2, + glfw3, glm, freetype2, + magic_enum, miniaudio ] -tests = [ - 'tests/main.cpp', - 'tests/sol2_test.cpp', - 'tests/builder_test.cpp', -] +subdir('src') +subdir('tests') executable('bezos_tests', sources + tests, cpp_args: cpp_args, diff --git a/src/document.hpp b/src/document.hpp index c05eaef..79f406a 100644 --- a/src/document.hpp +++ b/src/document.hpp @@ -16,14 +16,10 @@ enum class NodeType { }; struct Color { - uint8_t r; - uint8_t g; - uint8_t b; - uint8_t a; - - Color(uint8_t r, uint8_t g, uint8_t b, uint8_t a) : - r(r), g(g), b(g), a(a) - {} + uint8_t r=0; + uint8_t g=0; + uint8_t b=0; + uint8_t a=255; }; struct Node { diff --git a/src/guecs/lel.cpp b/src/guecs/lel.cpp new file mode 100644 index 0000000..c8efccc --- /dev/null +++ b/src/guecs/lel.cpp @@ -0,0 +1,130 @@ +#include "guecs/lel.hpp" +#include +#include +#include + +#include "./lel_parser.cpp" + +namespace lel { + + Parser::Parser(int x, int y, int width, int height) : + grid_x(x), + grid_y(y), + grid_w(width), + grid_h(height) + { + } + + void Parser::position(int x, int y, int width, int height) { + grid_x = x; + grid_y = y; + grid_w = width; + grid_h = height; + + // used in GUIs for setting backgrounds + main = { + .x=grid_x, + .y=grid_y, + .w=grid_w, + .h=grid_h, + .mid_x=std::midpoint(grid_x, grid_x + grid_w), + .mid_y=std::midpoint(grid_y, grid_y + grid_h), + .max_w=grid_w, + .max_h=grid_h + }; + } + + void Parser::id(const std::string& name) { + if(name != "_") { + assert(!cells.contains(name) && "duplicate cell name"); + cells.insert_or_assign(name, cur); + } + + cur = {.col=cur.col + 1, .row=cur.row}; + auto& row = grid.back(); + row.push_back(name); + } + + void Parser::finalize() { + size_t rows = grid.size(); + int cell_height = grid_h / rows; + + for(auto& row : grid) { + // BUG: see issue #16 + size_t columns = row.size(); + int cell_width = grid_w / columns; + assert(cell_width > 0 && "invalid cell width calc"); + assert(cell_height > 0 && "invalid cell height calc"); + + for(auto& name : row) { + if(name == "_") continue; + auto& cell = cells.at(name); + + // ZED: getting a bit hairy but this should work + if(cell.percent) { + // when percent mode we have to take unset to 100% + if(cell.max_w == 0) cell.max_w = 100; + if(cell.max_h == 0) cell.max_h = 100; + cell.max_w *= cell_width * 0.01; + cell.max_h *= cell_height * 0.01; + } else { + if(cell.max_w == 0) cell.max_w = cell_width; + if(cell.max_h == 0) cell.max_h = cell_height; + } + + cell.w = cell.expand ? std::min(cell.max_w, grid_w) : std::min(cell_width, cell.max_w); + cell.h = cell.expand ? std::min(cell.max_h, grid_h) : std::min(cell_height, cell.max_h); + + assert(cell.h > 0 && "invalid height cell is <= 0"); + assert(cell.w > 0 && "invalid width cell is <= 0"); + + cell.x = grid_x + (cell.col * cell_width); + cell.y = grid_y + (cell.row * cell_height); + + // keep the midpoint since it is used a lot + cell.mid_x = std::midpoint(cell.x, cell.x + cell.w); + cell.mid_y = std::midpoint(cell.y, cell.y + cell.h); + + // perform alignments + if(cell.right) cell.x += cell_width - cell.w; + if(cell.bottom) cell.y += cell_height - cell.h; + if(cell.center) { + cell.x = cell.mid_x - cell.w / 2; + cell.y = cell.mid_y - cell.h / 2; + } + } + } + } + + void Parser::reset() { + cur = {.col=0, .row=0}; + main = {.col=0, .row=0}; + grid.clear(); + cells.clear(); + } + + std::optional Parser::hit(int x, int y) { + for(auto& [name, cell] : cells) { + if(name != "_MAIN" && + (x >= cell.x && x <= cell.x + cell.w) && + (y >= cell.y && y <= cell.y + cell.h)) + { + return name; + } + } + + return std::nullopt; + } + + Cell center(int width, int height, Cell &parent) { + Cell copy = parent; + + // BUG: could I use std::midpoint here or am I even using this? + copy.x = parent.mid_x - width / 2; + copy.y = parent.mid_y - height / 2; + copy.w = width; + copy.h = height; + + return copy; + } +} diff --git a/src/guecs/lel.hpp b/src/guecs/lel.hpp new file mode 100644 index 0000000..3850126 --- /dev/null +++ b/src/guecs/lel.hpp @@ -0,0 +1,52 @@ +#pragma once +#include +#include +#include +#include + +namespace lel { + + struct Cell { + int col = 0; + int row = 0; + 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; + bool right = false; + bool bottom = false; + bool expand = false; + bool center = false; + bool percent = false; + }; + + using Row = std::vector; + using CellMap = std::unordered_map; + + struct Parser { + int grid_x = 0; + int grid_y = 0; + int grid_w = 0; + int grid_h = 0; + Cell cur; + Cell main; + std::vector grid; + CellMap cells; + + Parser(int x, int y, int width, int height); + Parser() : cur{.col=0, .row=0} {}; + + void position(int x, int y, int width, int height); + void id(const std::string& name); + void reset(); + bool parse(const std::string& input); + void finalize(); + std::optional hit(int x, int y); + }; + + Cell center(int width, int height, Cell &parent); +} diff --git a/src/guecs/lel_parser.cpp b/src/guecs/lel_parser.cpp new file mode 100644 index 0000000..ba99a2b --- /dev/null +++ b/src/guecs/lel_parser.cpp @@ -0,0 +1,261 @@ + +#line 1 "src/guecs/lel_parser.rl" +#include "guecs/lel.hpp" +#include +#include + +namespace lel { + + +#line 41 "src/guecs/lel_parser.rl" + + + +#line 10 "src/guecs/lel_parser.cpp" +static const char _Parser_actions[] = { + 0, 1, 1, 1, 2, 1, 3, 1, + 4, 1, 5, 1, 6, 1, 9, 1, + 10, 1, 11, 1, 12, 1, 13, 2, + 0, 7, 2, 0, 8, 2, 4, 1, + 2, 4, 5 +}; + +static const char _Parser_key_offsets[] = { + 0, 0, 4, 20, 33, 35, 39, 44, + 47, 59, 64 +}; + +static const char _Parser_trans_keys[] = { + 32, 91, 9, 13, 32, 37, 40, 42, + 46, 61, 94, 95, 9, 13, 60, 62, + 65, 90, 97, 122, 37, 40, 42, 46, + 61, 94, 95, 60, 62, 65, 90, 97, + 122, 48, 57, 41, 44, 48, 57, 32, + 9, 13, 48, 57, 41, 48, 57, 32, + 93, 95, 124, 9, 13, 48, 57, 65, + 90, 97, 122, 32, 93, 124, 9, 13, + 32, 91, 9, 13, 0 +}; + +static const char _Parser_single_lengths[] = { + 0, 2, 8, 7, 0, 2, 1, 1, + 4, 3, 2 +}; + +static const char _Parser_range_lengths[] = { + 0, 1, 4, 3, 1, 1, 2, 1, + 4, 1, 1 +}; + +static const char _Parser_index_offsets[] = { + 0, 0, 4, 17, 28, 30, 34, 38, + 41, 50, 55 +}; + +static const char _Parser_indicies[] = { + 0, 2, 0, 1, 3, 4, 5, 6, + 7, 9, 7, 10, 3, 8, 10, 10, + 1, 4, 5, 6, 7, 9, 7, 10, + 8, 10, 10, 1, 11, 1, 12, 13, + 14, 1, 15, 15, 16, 1, 17, 18, + 1, 19, 21, 20, 22, 19, 20, 20, + 20, 1, 23, 24, 25, 23, 1, 26, + 2, 26, 1, 0 +}; + +static const char _Parser_trans_targs[] = { + 1, 0, 2, 2, 3, 4, 3, 3, + 3, 3, 8, 5, 3, 6, 5, 6, + 7, 3, 7, 9, 8, 10, 2, 9, + 10, 2, 10 +}; + +static const char _Parser_trans_actions[] = { + 0, 0, 3, 0, 17, 0, 13, 5, + 11, 15, 21, 19, 23, 23, 0, 0, + 19, 26, 0, 7, 0, 32, 29, 0, + 9, 1, 0 +}; + +static const int Parser_start = 1; +static const int Parser_first_final = 10; +static const int Parser_error = 0; + +static const int Parser_en_main = 1; + + +#line 44 "src/guecs/lel_parser.rl" + +bool Parser::parse(const std::string& input) { + reset(); + int cs = 0; + const char *start = nullptr; + const char *begin = input.data(); + const char *p = input.data(); + const char *pe = p + input.size(); + std::string tk; + + +#line 91 "src/guecs/lel_parser.cpp" + { + cs = Parser_start; + } + +#line 55 "src/guecs/lel_parser.rl" + +#line 94 "src/guecs/lel_parser.cpp" + { + int _klen; + unsigned int _trans; + const char *_acts; + unsigned int _nacts; + const char *_keys; + + if ( p == pe ) + goto _test_eof; + if ( cs == 0 ) + goto _out; +_resume: + _keys = _Parser_trans_keys + _Parser_key_offsets[cs]; + _trans = _Parser_index_offsets[cs]; + + _klen = _Parser_single_lengths[cs]; + if ( _klen > 0 ) { + const char *_lower = _keys; + const char *_mid; + const char *_upper = _keys + _klen - 1; + while (1) { + if ( _upper < _lower ) + break; + + _mid = _lower + ((_upper-_lower) >> 1); + if ( (*p) < *_mid ) + _upper = _mid - 1; + else if ( (*p) > *_mid ) + _lower = _mid + 1; + else { + _trans += (unsigned int)(_mid - _keys); + goto _match; + } + } + _keys += _klen; + _trans += _klen; + } + + _klen = _Parser_range_lengths[cs]; + if ( _klen > 0 ) { + const char *_lower = _keys; + const char *_mid; + const char *_upper = _keys + (_klen<<1) - 2; + while (1) { + if ( _upper < _lower ) + break; + + _mid = _lower + (((_upper-_lower) >> 1) & ~1); + if ( (*p) < _mid[0] ) + _upper = _mid - 2; + else if ( (*p) > _mid[1] ) + _lower = _mid + 2; + else { + _trans += (unsigned int)((_mid - _keys)>>1); + goto _match; + } + } + _trans += _klen; + } + +_match: + _trans = _Parser_indicies[_trans]; + cs = _Parser_trans_targs[_trans]; + + if ( _Parser_trans_actions[_trans] == 0 ) + goto _again; + + _acts = _Parser_actions + _Parser_trans_actions[_trans]; + _nacts = (unsigned int) *_acts++; + while ( _nacts-- > 0 ) + { + switch ( *_acts++ ) + { + case 0: +#line 11 "src/guecs/lel_parser.rl" + {tk = input.substr(start - begin, p - start); } + break; + case 1: +#line 13 "src/guecs/lel_parser.rl" + {} + break; + case 2: +#line 14 "src/guecs/lel_parser.rl" + { grid.push_back(Row()); } + break; + case 3: +#line 15 "src/guecs/lel_parser.rl" + { cur.bottom = (*p) == '.'; } + break; + case 4: +#line 16 "src/guecs/lel_parser.rl" + { id(input.substr(start - begin, p - start)); } + break; + case 5: +#line 17 "src/guecs/lel_parser.rl" + { cur.col = 0; cur.row++; } + break; + case 6: +#line 18 "src/guecs/lel_parser.rl" + { cur.right = (*p) == '>'; } + break; + case 7: +#line 19 "src/guecs/lel_parser.rl" + {cur.max_w = std::stoi(tk); } + break; + case 8: +#line 20 "src/guecs/lel_parser.rl" + { cur.max_h = std::stoi(tk); } + break; + case 9: +#line 21 "src/guecs/lel_parser.rl" + { cur.expand = true; } + break; + case 10: +#line 22 "src/guecs/lel_parser.rl" + { cur.center = true; } + break; + case 11: +#line 23 "src/guecs/lel_parser.rl" + { cur.percent = true; } + break; + case 12: +#line 33 "src/guecs/lel_parser.rl" + { start = p; } + break; + case 13: +#line 36 "src/guecs/lel_parser.rl" + {start = p;} + break; +#line 209 "src/guecs/lel_parser.cpp" + } + } + +_again: + if ( cs == 0 ) + goto _out; + if ( ++p != pe ) + goto _resume; + _test_eof: {} + _out: {} + } + +#line 56 "src/guecs/lel_parser.rl" + + bool good = pe - p == 0; + if(good) { + finalize(); + } else { + std::cout << "error at:"; + std::cout << p; + } + return good; +} + +} diff --git a/src/guecs/lel_parser.rl b/src/guecs/lel_parser.rl new file mode 100644 index 0000000..a123bfc --- /dev/null +++ b/src/guecs/lel_parser.rl @@ -0,0 +1,67 @@ +#include "guecs/lel.hpp" +#include +#include + +namespace lel { + +%%{ + machine Parser; + alphtype char; + + action token {tk = input.substr(start - begin, fpc - start); } + + action col {} + action ltab { grid.push_back(Row()); } + action valign { cur.bottom = fc == '.'; } + action id { id(input.substr(start - begin, fpc - start)); } + action row { cur.col = 0; cur.row++; } + action align { cur.right = fc == '>'; } + action setwidth {cur.max_w = std::stoi(tk); } + action setheight { cur.max_h = std::stoi(tk); } + action expand { cur.expand = true; } + action center { cur.center = true; } + action percent { cur.percent = true; } + + col = "|" $col; + ltab = "[" $ltab; + rtab = "]" $row; + valign = ("^" | ".") $valign; + expand = "*" $expand; + center = "=" $center; + percent = "%" $percent; + halign = ("<" | ">") $align; + number = digit+ >{ start = fpc; } %token; + setw = ("(" number %setwidth ("," space* number %setheight)? ")") ; + modifiers = (percent | center | expand | valign | halign | setw); + id = ((alpha | '_')+ :>> (alnum | '_')*) >{start = fpc;} %id; + cell = modifiers* id; + row = space* ltab space* cell space* (col space* cell space*)* space* rtab space*; + + main := row+; +}%% + +%% write data; + +bool Parser::parse(const std::string& input) { + reset(); + int cs = 0; + const char *start = nullptr; + const char *begin = input.data(); + const char *p = input.data(); + const char *pe = p + input.size(); + std::string tk; + + %% write init; + %% write exec; + + bool good = pe - p == 0; + if(good) { + finalize(); + } else { + std::cout << "error at:"; + std::cout << p; + } + return good; +} + +} diff --git a/src/guecs/sfml/backend.cpp b/src/guecs/sfml/backend.cpp new file mode 100644 index 0000000..f90ee12 --- /dev/null +++ b/src/guecs/sfml/backend.cpp @@ -0,0 +1,59 @@ +#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::get_sprite(const string& name) { + auto sp = textures::get(name); + return {sp.sprite, sp.texture, sp.frame_size}; + } + + guecs::SpriteTexture Backend::get_icon(const string& name) { + return get_sprite(name); + } + + 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 Backend::get_shader(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; + } + } + + guecs::Theme Backend::theme() { + guecs::Theme theme; + + theme.PADDING = 3; + theme.BORDER_PX = 1; + theme.TEXT_SIZE = 30; + theme.LABEL_SIZE = 20; + theme.FILL_COLOR = theme.DARK_MID; + theme.TEXT_COLOR = theme.LIGHT_LIGHT; + theme.BG_COLOR = theme.DARK_DARK; + theme.BORDER_COLOR = theme.DARK_LIGHT; + theme.BG_COLOR_DARK = theme.BLACK; + theme.FONT_FILE_NAME = "assets/text.otf"; + + return theme; + } +} diff --git a/src/guecs/sfml/backend.hpp b/src/guecs/sfml/backend.hpp new file mode 100644 index 0000000..20010fe --- /dev/null +++ b/src/guecs/sfml/backend.hpp @@ -0,0 +1,21 @@ +#pragma once +#include "guecs/ui.hpp" + +namespace sfml { + using std::string; + + class Backend : public guecs::Backend { + int $shaders_version = 0; + + public: + + Backend(); + guecs::SpriteTexture get_sprite(const string& name); + guecs::SpriteTexture get_icon(const string& name); + void sound_play(const string& name); + void sound_stop(const string& name); + std::shared_ptr get_shader(const std::string& name); + bool shader_updated(); + guecs::Theme theme(); + }; +} diff --git a/src/guecs/sfml/components.cpp b/src/guecs/sfml/components.cpp new file mode 100644 index 0000000..1d63c54 --- /dev/null +++ b/src/guecs/sfml/components.cpp @@ -0,0 +1,196 @@ +#include "guecs/ui.hpp" +#include "guecs/sfml/backend.hpp" +#include +#include +#include +#include + +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; + } +} diff --git a/src/guecs/sfml/components.hpp b/src/guecs/sfml/components.hpp new file mode 100644 index 0000000..fcb0c1d --- /dev/null +++ b/src/guecs/sfml/components.hpp @@ -0,0 +1,136 @@ +#pragma once +#include "guecs/lel.hpp" +#include +#include +#include +#include +#include "guecs/theme.hpp" +#include +#include "text.hpp" +#include "shader.hpp" + +namespace guecs { + using std::shared_ptr, std::wstring, std::string; + + struct Text { + std::wstring content; + unsigned int size = THEME.TEXT_SIZE; + Color color = THEME.TEXT_COLOR; + int padding = THEME.PADDING; + bool centered = false; + shared_ptr text = nullptr; + + void init(lel::Cell &cell, const std::string& font_file); + void update(const std::wstring& new_content); + void render(); + }; + + struct Sprite { + string name; + int padding = THEME.PADDING; + bool stretch = true; + bool is_icon = false; + std::shared_ptr image = nullptr; + + void init(lel::Cell &cell); + void update(const string& new_name); + void render(); + }; + + struct Icon : public Sprite { + template + Icon(Args... args) : Sprite(args...) + { + stretch = false; + is_icon = true; + } + + Icon() { + stretch = false; + is_icon = true; + }; + }; + + struct Rectangle { + // BUG: confirm that padding is being used correctly here + int padding = THEME.PADDING; + Color color = THEME.FILL_COLOR; + Color border_color = THEME.BORDER_COLOR; + int border_px = THEME.BORDER_PX; + void* shape = nullptr; // CHANGE ME BRO + + void init(lel::Cell& cell); + void render(); + void update(); + }; + + // BUG: meter needs a backing rectangle of one color for the full bar, then + // another rectangle for the current setting. + // BUG: the meter will be past the end, probably a floating point error + struct Meter { + float percent = 1.0f; + Color color = THEME.BG_COLOR_DARK; + int padding = THEME.PADDING; + bool vertical = false; + Rectangle bar{padding, color}; + + // this is set automatically if a Rectangle is configured for the cell + size_t $cell_x = 0; + size_t $cell_y = 0; + size_t $cell_w = 0; + size_t $cell_h = 0; + + void init(lel::Cell& cell); + void render(); + void update(); + }; + + struct Effect { + double duration = 0.1; + string name{"ui_shader"}; + double $u_time_end = 0.0; + bool $active = false; + std::shared_ptr $shader = nullptr; + int $shader_version = 0; + + void init(lel::Cell &cell); + void run(); + void stop(); + void render(); + Shader* get_shader(bool is_shape); + + }; + + struct Sound { + string on_click{"ui_click"}; + void play(bool hover); + void stop(bool hover); + }; + + // BUG: it should be _way_ easier to set the background and change it + struct Background { + float x = 0.0f; + float y = 0.0f; + float w = 0.0f; + float h = 0.0f; + Color color=THEME.BG_COLOR; + void* shape = nullptr; // CHANGE ME BRO + shared_ptr sprite = nullptr; + + Background(lel::Parser& parser, Color& bg_color=THEME.BG_COLOR) : + x(parser.grid_x), + y(parser.grid_y), + w(parser.grid_w), + h(parser.grid_h), + color(bg_color) + {} + + Background() {} + + void init(); + void render(); + + void set_color(Color& c); + void set_sprite(const std::string& name, bool stretch=false); + }; +} diff --git a/src/guecs/sfml/config.cpp b/src/guecs/sfml/config.cpp new file mode 100644 index 0000000..8ecd101 --- /dev/null +++ b/src/guecs/sfml/config.cpp @@ -0,0 +1,46 @@ +#include "guecs/sfml/config.hpp" +#include + +using nlohmann::json; +using fmt::format; + +std::filesystem::path Config::BASE_DIR{"."}; + +Config::Config(const std::string src_path) : $src_path(src_path) { + std::ifstream infile(Config::path_to($src_path)); + $config = json::parse(infile); +} + +json &Config::operator[](const std::string &key) { + assert($config.contains(key) && + "ERROR in config, key doesn't exist."); + return $config[key]; +} + +std::wstring Config::wstring(const std::string main_key, const std::string sub_key) { + assert($config.contains(main_key) && "ERROR wstring main/key in config, main_key doesn't exist."); + assert($config[main_key].contains(sub_key) && "ERROR wstring in config, main_key/key doesn't exist."); + + const std::string& str_val = $config[main_key][sub_key]; + std::wstring_convert> $converter; + return $converter.from_bytes(str_val); +} + +std::vector Config::keys() { + std::vector the_fucking_keys; + + for(auto& [key, value] : $config.items()) { + the_fucking_keys.push_back(key); + } + + return the_fucking_keys; +} + + +void Config::set_base_dir(const char *optarg) { + Config::BASE_DIR.assign(optarg); +} + +std::filesystem::path Config::path_to(const std::string& path) { + return Config::BASE_DIR / path; +} diff --git a/src/guecs/sfml/config.hpp b/src/guecs/sfml/config.hpp new file mode 100644 index 0000000..2ca8d52 --- /dev/null +++ b/src/guecs/sfml/config.hpp @@ -0,0 +1,24 @@ +#pragma once +#include +#include +#include +#include + +struct Config { + static std::filesystem::path BASE_DIR; + 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 keys(); + + static void set_base_dir(const char *optarg); + static std::filesystem::path path_to(const std::string& path); +}; diff --git a/src/guecs/sfml/shaders.cpp b/src/guecs/sfml/shaders.cpp new file mode 100644 index 0000000..a235454 --- /dev/null +++ b/src/guecs/sfml/shaders.cpp @@ -0,0 +1,77 @@ +#include "guecs/sfml/shaders.hpp" +#include "guecs/sfml/config.hpp" +#include +#include +#include + +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 ptr) { + ptr->setUniform("source", sf::Shader::CurrentTexture); + } + + bool load_shader(std::string name, nlohmann::json& settings) { + auto file_name = settings["file_name"]; + auto file_path = Config::path_to(file_name); + auto ptr = std::make_shared(); + bool good = ptr->loadFromFile(file_path, 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) { + assert(sf::Shader::isAvailable() && "no shaders?!"); + INITIALIZED = true; + Config config("assets/shaders.json"); + bool good = load_shader("ERROR", config["ERROR"]); + assert(good && "Failed to load ERROR shader. Look in assets/shaders.json"); + + for(auto& [name, settings] : config.json().items()) { + if(name == "ERROR") continue; + + assert(!SMGR.shaders.contains(name) && + "shader name duplicated in assets/shaders.json"); + good = load_shader(name, settings); + + if(!good) { + fmt::println("[SOUND] failed to load shader {}", name); + SMGR.shaders.insert_or_assign(name, SMGR.shaders.at("ERROR")); + } + } + } + } + + std::shared_ptr get(const std::string& name) { + assert(INITIALIZED && "you forgot to shaders::init()"); + assert(SMGR.shaders.contains(name) && + "shader name not in assets/shaders.json"); + 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; + } +}; diff --git a/src/guecs/sfml/shaders.hpp b/src/guecs/sfml/shaders.hpp new file mode 100644 index 0000000..33791d9 --- /dev/null +++ b/src/guecs/sfml/shaders.hpp @@ -0,0 +1,27 @@ +#pragma once +#include +#include +#include +#include +#include +#include +#include + +namespace shaders { + struct Record { + std::string name; + std::string file_name; + std::shared_ptr ptr = nullptr; + }; + + struct ShaderManager { + std::unordered_map shaders; + }; + + std::shared_ptr 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(); +} diff --git a/src/guecs/sfml/sound.cpp b/src/guecs/sfml/sound.cpp new file mode 100644 index 0000000..3fcb2e3 --- /dev/null +++ b/src/guecs/sfml/sound.cpp @@ -0,0 +1,81 @@ +#include "guecs/sfml/sound.hpp" +#include "guecs/sfml/config.hpp" +#include +#include + +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) { + assert(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 { + fmt::println("[SOUND] 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(), Config::path_to(el.value())); + } + initialized = true; + } + } + + void load(const std::string& name, const std::filesystem::path& sound_path) { + assert(fs::exists(sound_path) && "sound file does not exist"); + + // create the buffer and keep in the buffer map + auto buffer = make_shared(sound_path); + + // set it on the sound and keep in the sound map + auto sound = make_shared(*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; + } +} diff --git a/src/guecs/sfml/sound.hpp b/src/guecs/sfml/sound.hpp new file mode 100644 index 0000000..80241cf --- /dev/null +++ b/src/guecs/sfml/sound.hpp @@ -0,0 +1,26 @@ +#pragma once +#include +#include +#include +#include +#include + +namespace sound { + struct SoundPair { + std::shared_ptr buffer; + std::shared_ptr sound; + }; + + struct SoundManager { + std::unordered_map sounds; + }; + + void init(); + void load(const std::string& name, const std::filesystem::path& 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); +} diff --git a/src/guecs/sfml/textures.cpp b/src/guecs/sfml/textures.cpp new file mode 100644 index 0000000..9a90105 --- /dev/null +++ b/src/guecs/sfml/textures.cpp @@ -0,0 +1,62 @@ +#include "guecs/sfml/textures.hpp" +#include "guecs/sfml/config.hpp" +#include +#include +#include + +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 file_name = settings["path"]; + auto file_path = Config::path_to(file_name); + auto texture = make_shared(file_path); + + texture->setSmooth(assets["graphics"]["smooth_textures"]); + auto sprite = make_shared(*texture); + + int width = settings["frame_width"]; + int height = settings["frame_height"]; + + sf::Vector2i frame_size{width, height}; + sprite->setTextureRect({{0,0}, frame_size}); + TMGR.sprite_textures.try_emplace(name, sprite, texture, frame_size); + } + } + + void init() { + if(!initialized) { + load_sprites(); + initialized = true; + } + } + + SpriteTexture get(const std::string& name) { + assert(initialized && "you forgot to call textures::init()"); + assert(TMGR.sprite_textures.contains(name) && + "!!!!! texture pack does not contain sprite"); + + auto result = TMGR.sprite_textures.at(name); + + assert(result.sprite != nullptr && + "bad sprite from textures::get named can't be null"); + assert(result.texture != nullptr && + "bad texture from textures::get named can't be null"); + + return result; + } + + sf::Image load_image(const std::string& filename) { + sf::Image texture; + auto file_path = Config::path_to(filename); + bool good = texture.loadFromFile(file_path); + assert(good && "failed to load image file"); + return texture; + } +}; diff --git a/src/guecs/sfml/textures.hpp b/src/guecs/sfml/textures.hpp new file mode 100644 index 0000000..d0d3c2b --- /dev/null +++ b/src/guecs/sfml/textures.hpp @@ -0,0 +1,28 @@ +#pragma once +#include +#include +#include +#include +#include +#include + +namespace textures { + + struct SpriteTexture { + std::shared_ptr sprite = nullptr; + std::shared_ptr texture = nullptr; + sf::Vector2i frame_size; + }; + + struct TextureManager { + std::vector surfaces; + std::unordered_map sprite_textures; + std::unordered_map char_to_texture; + }; + + void init(); + + SpriteTexture get(const std::string& name); + + sf::Image load_image(const std::string& filename); +} diff --git a/src/guecs/theme.cpp b/src/guecs/theme.cpp new file mode 100644 index 0000000..5f634ec --- /dev/null +++ b/src/guecs/theme.cpp @@ -0,0 +1,11 @@ +#include "guecs/theme.hpp" + +namespace guecs { + Backend* BACKEND = nullptr; + Theme THEME; + + void init(Backend* backend) { + BACKEND = backend; + THEME = BACKEND->theme(); + } +} diff --git a/src/guecs/theme.hpp b/src/guecs/theme.hpp new file mode 100644 index 0000000..c23829e --- /dev/null +++ b/src/guecs/theme.hpp @@ -0,0 +1,57 @@ +#pragma once +#include +#include +#include +#include "document.hpp" + +namespace guecs { + using std::string; + + struct Theme { + Color BLACK{0, 0, 0}; + Color DARK_DARK{10, 10, 10}; + Color DARK_MID{30, 30, 30}; + Color DARK_LIGHT{60, 60, 60}; + Color MID{100, 100, 100}; + Color LIGHT_DARK{150, 150, 150}; + Color LIGHT_MID{200, 200, 200}; + Color LIGHT_LIGHT{230, 230, 230}; + Color WHITE{255, 255, 255}; + Color TRANSPARENT = {0, 0, 0}; + + int PADDING = 3; + int BORDER_PX = 1; + unsigned int TEXT_SIZE = 30; + int LABEL_SIZE = 20; + Color FILL_COLOR{255,0,0,255}; + Color TEXT_COLOR{255,0,0,255}; + Color BG_COLOR{255,0,0,255}; + Color BORDER_COLOR{255,0,0,255}; + Color BG_COLOR_DARK{0,0,0,255}; + std::string FONT_FILE_NAME{"you_forgot_to_init.otf"}; + }; + + struct SpriteTexture { + }; + + class Backend { + public: + virtual SpriteTexture get_sprite(const string& name) = 0; + + virtual SpriteTexture get_icon(const string& name) = 0; + + virtual void sound_play(const string& name) = 0; + + virtual void sound_stop(const string& name) = 0; + + virtual std::shared_ptr get_shader(const std::string& name) = 0; + + virtual bool shader_updated() = 0; + virtual Theme theme() = 0; + }; + + extern Backend* BACKEND; + extern Theme THEME; + + void init(Backend* backend); +} diff --git a/src/guecs/ui.cpp b/src/guecs/ui.cpp new file mode 100644 index 0000000..99c7e61 --- /dev/null +++ b/src/guecs/ui.cpp @@ -0,0 +1,243 @@ +#include "guecs/ui.hpp" +#include +#include +#include +#include +#include + +namespace guecs { + using std::make_shared; + + UI::UI() { + } + + void UI::position(int x, int y, int width, int height) { + $parser.position(x, y, width, height); + } + + glm::vec2 UI::get_position() { + return {float($parser.grid_x), float($parser.grid_y)}; + } + + glm::vec2 UI::get_size() { + return {float($parser.grid_w), float($parser.grid_h)}; + } + + void UI::layout(const string& grid) { + $grid = grid; + bool good = $parser.parse($grid); + assert(good && "LEL parsing failed."); + + for(auto& [name, cell] : $parser.cells) { + auto ent = init_entity(name); + set(ent, cell); + } + } + + Entity UI::init_entity(const string& name) { + auto ent = entity(); + // this lets you look up an entity by name + $name_ents.insert_or_assign(name, ent); + // this lets you get a name by entity + $ents_name.insert_or_assign(ent, name); + // this makes it easier to get the name during querying + set(ent, {name}); + return ent; + } + + const std::string& UI::name_for(Entity gui_id) { + assert($ents_name.contains(gui_id) && + "Attempt to get name_for an Entity but that's not a valid ID."); + return $ents_name.at(gui_id); + } + + bool UI::contains(const string& name) { + return $name_ents.contains(name); + } + + Entity UI::entity(const string& name) { + assert($name_ents.contains(name) && + "GUECS entity does not exist. Mispelled cell name?"); + return $name_ents.at(name); + } + + Entity UI::entity(const string& name, int id) { + return entity(fmt::format("{}{}", name, id)); + } + + void UI::init() { + query([](auto, auto& bg) { + bg.init(); + }); + + query([](auto, auto& cell, auto& rect) { + rect.init(cell); + }); + + query([](auto, auto& cell, auto& shader) { + shader.init(cell); + }); + + query([this](auto ent, auto &cell, auto& meter) { + meter.init(cell); + }); + + query([this](auto, auto& cell, auto& text) { + // CHANGE ME font needs to be loaded + text.init(cell, $font); + }); + + query([&](auto, auto &cell, auto &sprite) { + sprite.init(cell); + }); + + query([&](auto, auto &cell, auto &icon) { + icon.init(cell); + }); + } + + void UI::debug_layout() { + query([&](const auto, auto &cell) { + }); + } + + Shader* UI::find_shader(Entity ent, bool is_shape) { + if(auto shader = get_if(ent)) { + return shader->get_shader(is_shape); + } else { + return nullptr; + } + } + + void UI::update() { + query([&](auto ent, auto& rect) { + rect.update(); + }); + + query([&](auto ent, auto &meter) { + meter.update(); + }); + } + + // BUG: either render detects that the things are initialized or there's + // another validator function I can call in debug modes to confirm they are + void UI::render() { + if(auto bg = get_if(MAIN)) { + bg->render(); + } + + query([&](auto, auto& shader) { + if(shader.$active) shader.render(); + }); + + query([&](auto ent, auto& rect) { + auto shader_ptr = find_shader(ent, true); + rect.render(); + }); + + query([&](auto ent, auto &meter) { + auto shader_ptr = find_shader(ent, true); + meter.render(); + }); + + query([&](auto ent, auto& sprite) { + auto shader_ptr = find_shader(ent, false); + sprite.render(); + }); + + query([&](auto ent, auto& icon) { + auto shader_ptr = find_shader(ent, false); + icon.render(); + }); + + query([&](auto ent, auto& text) { + auto shader_ptr = find_shader(ent, false); + text.render(); + }); + } + + bool UI::mouse(float x, float y, Modifiers mods) { + int action_count = 0; + bool hover = mods.test(size_t(ModBit::hover)); + + query([&](auto ent, auto& cell) { + if((x >= cell.x && x <= cell.x + cell.w) && + (y >= cell.y && y <= cell.y + cell.h)) + { + do_if(ent, [hover](auto& effect) { + effect.$shader->setBool("hover", hover); + effect.run(); + }); + + do_if(ent, [hover](auto& sound) { + // here set that it played then only play once + sound.play(hover); + }); + + if(hover) return; + + click_on(ent, mods); + + action_count++; + } else { + do_if(ent, [](auto& effect) { + effect.stop(); + }); + + do_if(ent, [hover](auto& sound) { + // here set that it played then only play once + sound.stop(hover); + }); + } + }); + + return action_count > 0; + } + + void UI::show_sprite(const string& region, const string& sprite_name) { + auto ent = entity(region); + + if(auto sprite = get_if(ent)) { + sprite->update(sprite_name); + } else { + set_init(ent, {sprite_name}); + } + } + + void UI::show_icon(const string& region, const string& sprite_name) { + auto ent = entity(region); + + if(auto sprite = get_if(ent)) { + sprite->update(sprite_name); + } else { + set_init(ent, {sprite_name}); + } + } + + void UI::show_text(const string& region, const wstring& content) { + auto ent = entity(region); + // REWRITE ME + } + + void UI::click_on(const std::string& name, Modifiers mods) { + click_on(entity(name), mods); + } + + void UI::click_on(Entity gui_id, Modifiers mods) { + if(auto to_click = get_if(gui_id)) { + to_click->action(mods); + }; + } + + wstring to_wstring(const string& str) { + std::wstring result = L""; + + try { + utf8::utf8to32(str.begin(), str.end(), std::back_inserter(result)); + } catch(const utf8::exception& e) { + std::cerr << e.what() << std::endl; + } + + return result; + } +} diff --git a/src/guecs/ui.hpp b/src/guecs/ui.hpp new file mode 100644 index 0000000..66be5b4 --- /dev/null +++ b/src/guecs/ui.hpp @@ -0,0 +1,238 @@ +#pragma once +#include "guecs/lel.hpp" +#include +#include +#include +#include +#include +#include +#include +#include "guecs/theme.hpp" +#include "guecs/sfml/components.hpp" +#include +#include + +namespace guecs { + using std::shared_ptr, std::wstring, std::string; + + using Entity = unsigned long; + + using EntityMap = std::unordered_map; + + template + struct ComponentStorage { + std::vector data; + std::queue free_indices; + }; + + namespace ModBit { + enum { + NONE=0, + hover=1, + left=2, + right=3 + }; + } + + using Modifiers = std::bitset<16>; + + constexpr const Modifiers NO_MODS{0}; + + 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 action; + }; + + struct CellName { + string name; + }; + + class UI { + public: + Entity MAIN = 0; + unsigned long entity_count = 1; + std::unordered_map $components; + std::unordered_map $component_storages; + std::unordered_map $name_ents; + std::unordered_map $ents_name; + // change this to use the new text? + std::string $font = THEME.FONT_FILE_NAME; + lel::Parser $parser; + string $grid = ""; + // BUG: check for this to prevent bugs, or find a way to shape the API so you + // simply can't get it wrong + bool initialized = false; + + UI(); + + void position(int x, int y, int width, int height); + glm::vec2 get_position(); + glm::vec2 get_size(); + void layout(const string& grid); + Entity init_entity(const string& name); + bool contains(const string& name); + Entity entity(const string& name); + Entity entity(const string& name, int id); + const std::string& name_for(Entity gui_id); + + inline lel::CellMap& cells() { + return $parser.cells; + } + + void init(); + void render(); + void update(); + bool mouse(float x, float y, Modifiers mods); + void click_on(const std::string& name, Modifiers mods=NO_MODS); + void click_on(Entity slot_id, Modifiers mods=NO_MODS); + void debug_layout(); + + Entity entity() { return ++entity_count; } + + template + size_t make_component() { + auto &storage = component_storage_for(); + 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 + ComponentStorage &component_storage_for() { + auto type_index = std::type_index(typeid(Comp)); + $component_storages.try_emplace(type_index, ComponentStorage{}); + return std::any_cast &>( + $component_storages.at(type_index)); + } + + template + EntityMap &entity_map_for() { + return $components[std::type_index(typeid(Comp))]; + } + + template + void set(Entity ent, Comp val) { + EntityMap &map = entity_map_for(); + + if(has(ent)) { + get(ent) = val; + return; + } + + map.insert_or_assign(ent, make_component()); + get(ent) = val; + } + + template + Comp& get(Entity ent) { + EntityMap &map = entity_map_for(); + auto &storage = component_storage_for(); + auto index = map.at(ent); + return storage.data[index]; + } + + template + Comp* get_if(Entity entity) { + EntityMap &map = entity_map_for(); + auto &storage = component_storage_for(); + if(map.contains(entity)) { + auto index = map.at(entity); + return &storage.data[index]; + } else { + return nullptr; + } + } + + template + bool has(Entity ent) { + EntityMap &map = entity_map_for(); + return map.contains(ent); + } + + template + void remove(Entity ent) { + EntityMap &map = entity_map_for(); + + if(map.contains(ent)) { + size_t index = map.at(ent); + component_storage_for().free_indices.push(index); + } + + map.erase(ent); + } + + template + void query(std::function cb) { + EntityMap &map = entity_map_for(); + + for(auto &[entity, index] : map) { + cb(entity, get(entity)); + } + } + + template + void query(std::function cb) { + EntityMap &map_a = entity_map_for(); + EntityMap &map_b = entity_map_for(); + + for(auto &[entity, index_a] : map_a) { + if(map_b.contains(entity)) { + cb(entity, get(entity), get(entity)); + } + } + } + + template + void set_init(Entity ent, Comp val) { + assert(has(ent) && "WRONG! slot is missing its cell?!"); + auto& cell = get(ent); + val.init(cell); + set(ent, val); + } + + template + void do_if(Entity ent, std::function cb) { + if(has(ent)) { + cb(get(ent)); + } + } + + lel::Cell& cell_for(Entity ent) { + return get(ent); + } + + lel::Cell& cell_for(const string& name) { + Entity ent = entity(name); + return get(ent); + } + + // BUG: close could just be remove with overload + template + void close(string region) { + auto ent = entity(region); + if(has(ent)) { + remove(ent); + } + } + + Shader* find_shader(Entity ent, bool is_shape); + + void show_sprite(const string& region, const string& sprite_name); + void show_icon(const string& region, const string& sprite_name); + void show_text(const string& region, const wstring& content); + }; + + + wstring to_wstring(const string& str); + +} diff --git a/src/guecs/uistack.hpp b/src/guecs/uistack.hpp new file mode 100644 index 0000000..d451ea0 --- /dev/null +++ b/src/guecs/uistack.hpp @@ -0,0 +1,97 @@ +#include +#include +#include + +namespace gui { + template + struct UIStack { + using UIStackMap = std::flat_map>; + + UIStackMap screens{}; + bool visible = false; + + UIStackMap::iterator $current{screens.begin()}; + SCREEN_TYPE* $active = nullptr; + + void add(const std::string& name, std::shared_ptr screen) { + screens.insert_or_assign(name, screen); + update_active(screens.begin()); + } + + void update_active(const UIStackMap::iterator& itr) { + $current = itr; + $active = (*$current).second.get(); + } + + void set_active(const std::string& name) { + assert(screens.contains(name) && "no screen with that name"); + update_active(screens.find(name)); + } + + void set_visible(bool new_value) { + visible = new_value; + } + + bool is_visible() { + return visible; + } + + void render(sf::RenderTarget& target) { + assert($active != nullptr && "you didn't set active"); + $active->render(target); + } + + void update() { + assert($active != nullptr && "you didn't set active"); + $active->update(); + } + + bool mouse(float x, float y, guecs::Modifiers mods = guecs::NO_MODS) { + assert($active != nullptr && "you didn't set active"); + return $active->mouse(x, y, mods); + } + + bool move(const UIStackMap::iterator& itr, const UIStackMap::iterator& avoid, const UIStackMap::iterator& result) { + if(itr != avoid) { + update_active(result); + return true; + } else { + return false; + } + } + + bool next() { + return move($current + 1, screens.end(), $current + 1); + } + + bool prev() { + return move($current, screens.begin(), $current - 1); + } + + void first() { + update_active(screens.begin()); + } + + void last() { + update_active(screens.end() - 1); + } + + const std::string& active_name() { + assert($active != nullptr && "you didn't set active"); + return (*$current).first; + } + + std::shared_ptr active_ui() { + assert($active != nullptr && "you didn't set active"); + return (*$current).second; + } + + std::shared_ptr at(const std::string& name) { + return screens.at(name); + } + + const UIStackMap::key_container_type& names() { + return screens.keys(); + } + }; +} diff --git a/src/meson.build b/src/meson.build new file mode 100644 index 0000000..ebfd6d6 --- /dev/null +++ b/src/meson.build @@ -0,0 +1,20 @@ +sources = [ + 'src/glad/gl.cpp', + 'src/stb_image.cpp', + 'src/shader.cpp', + 'src/text.cpp', + 'src/stats.cpp', + 'src/dbc.cpp', + 'src/builder.cpp', + 'src/bezos.cpp', + 'src/document.cpp', + 'src/framebuffer.cpp', + 'src/screen.cpp', + 'src/engine.cpp', + 'src/image.cpp', + 'src/options.cpp', + 'src/guecs/ui.cpp', + 'src/guecs/lel.cpp', + 'src/guecs/theme.cpp', + 'src/guecs/sfml/components.cpp', +] diff --git a/tests/meson.build b/tests/meson.build new file mode 100644 index 0000000..bcb7cbc --- /dev/null +++ b/tests/meson.build @@ -0,0 +1,5 @@ +tests = [ + 'tests/main.cpp', + 'tests/sol2_test.cpp', + 'tests/builder_test.cpp', +] diff --git a/wraps/miniaudio.wrap b/wraps/miniaudio.wrap new file mode 100644 index 0000000..23dac1e --- /dev/null +++ b/wraps/miniaudio.wrap @@ -0,0 +1,13 @@ +[wrap-file] +directory = miniaudio-0.11.22 +source_url = https://github.com/mackron/miniaudio/archive/refs/tags/0.11.22.tar.gz +source_filename = miniaudio-0.11.22.tar.gz +source_hash = bcb07bfb27e6fa94d34da73ba2d5642d4940b208ec2a660dbf4e52e6b7cd492f +patch_filename = miniaudio_0.11.22-2_patch.zip +patch_url = https://wrapdb.mesonbuild.com/v2/miniaudio_0.11.22-2/get_patch +patch_hash = 345bc40914588b9901aff3d95213e25fdd678c24e976588a63442ad2c5171c77 +source_fallback_url = https://github.com/mesonbuild/wrapdb/releases/download/miniaudio_0.11.22-2/miniaudio-0.11.22.tar.gz +wrapdb_version = 0.11.22-2 + +[provide] +dependency_names = miniaudio diff --git a/wraps/utf8cpp.wrap b/wraps/utf8cpp.wrap new file mode 100644 index 0000000..c1ae900 --- /dev/null +++ b/wraps/utf8cpp.wrap @@ -0,0 +1,9 @@ +[wrap-git] +directory=utf8cpp-4.0.9 +url=https://github.com/nemtrif/utfcpp.git +revision=v4.0.9 +depth=1 +method=cmake + +[provide] +utf8cpp = utf8cpp_dep