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
47
src/guecs/dbc.cpp
Normal file
47
src/guecs/dbc.cpp
Normal file
|
@ -0,0 +1,47 @@
|
|||
#include "guecs/dbc.hpp"
|
||||
#include <iostream>
|
||||
|
||||
void dbc::log(const string &message, const std::source_location location) {
|
||||
std::cout << '[' << location.file_name() << ':'
|
||||
<< location.line() << "|"
|
||||
<< location.function_name() << "] "
|
||||
<< message << std::endl;
|
||||
}
|
||||
|
||||
void dbc::sentinel(const string &message, const std::source_location location) {
|
||||
string err = fmt::format("[SENTINEL!] {}", message);
|
||||
dbc::log(err, location);
|
||||
throw dbc::SentinelError{err};
|
||||
}
|
||||
|
||||
void dbc::pre(const string &message, bool test, const std::source_location location) {
|
||||
if(!test) {
|
||||
string err = fmt::format("[PRE!] {}", message);
|
||||
dbc::log(err, location);
|
||||
throw dbc::PreCondError{err};
|
||||
}
|
||||
}
|
||||
|
||||
void dbc::pre(const string &message, std::function<bool()> tester, const std::source_location location) {
|
||||
dbc::pre(message, tester(), location);
|
||||
}
|
||||
|
||||
void dbc::post(const string &message, bool test, const std::source_location location) {
|
||||
if(!test) {
|
||||
string err = fmt::format("[POST!] {}", message);
|
||||
dbc::log(err, location);
|
||||
throw dbc::PostCondError{err};
|
||||
}
|
||||
}
|
||||
|
||||
void dbc::post(const string &message, std::function<bool()> tester, const std::source_location location) {
|
||||
dbc::post(message, tester(), location);
|
||||
}
|
||||
|
||||
void dbc::check(bool test, const string &message, const std::source_location location) {
|
||||
if(!test) {
|
||||
string err = fmt::format("[CHECK!] {}\n", message);
|
||||
dbc::log(err, location);
|
||||
throw dbc::CheckError{err};
|
||||
}
|
||||
}
|
117
src/guecs/lel.cpp
Normal file
117
src/guecs/lel.cpp
Normal file
|
@ -0,0 +1,117 @@
|
|||
#include "guecs/lel.hpp"
|
||||
#include <fmt/core.h>
|
||||
#include "guecs/dbc.hpp"
|
||||
#include <numeric>
|
||||
|
||||
#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),
|
||||
cur(0, 0)
|
||||
{
|
||||
}
|
||||
|
||||
Parser::Parser() : cur(0, 0) { }
|
||||
|
||||
void Parser::position(int x, int y, int width, int height) {
|
||||
grid_x = x;
|
||||
grid_y = y;
|
||||
grid_w = width;
|
||||
grid_h = height;
|
||||
}
|
||||
|
||||
void Parser::id(std::string name) {
|
||||
if(name != "_") {
|
||||
dbc::check(!cells.contains(name),
|
||||
fmt::format("duplicate cell name {}", name));
|
||||
cells.insert_or_assign(name, cur);
|
||||
}
|
||||
|
||||
cur = {cur.col + 1, 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) {
|
||||
size_t columns = row.size();
|
||||
int cell_width = grid_w / columns;
|
||||
dbc::check(cell_width > 0, "invalid cell width calc");
|
||||
dbc::check(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);
|
||||
|
||||
dbc::check(cell.h > 0, fmt::format("invalid height cell {}", name));
|
||||
dbc::check(cell.w > 0, fmt::format("invalid width cell {}", name));
|
||||
|
||||
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 = {0, 0};
|
||||
grid.clear();
|
||||
cells.clear();
|
||||
}
|
||||
|
||||
std::optional<std::string> Parser::hit(int x, int y) {
|
||||
for(auto& [name, cell] : cells) {
|
||||
if((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;
|
||||
|
||||
copy.x = parent.mid_x - width / 2;
|
||||
copy.y = parent.mid_y - height / 2;
|
||||
copy.w = width;
|
||||
copy.h = height;
|
||||
|
||||
return copy;
|
||||
}
|
||||
}
|
261
src/guecs/lel_parser.cpp
Normal file
261
src/guecs/lel_parser.cpp
Normal file
|
@ -0,0 +1,261 @@
|
|||
|
||||
#line 1 ".\\src\\guecs\\lel_parser.rl"
|
||||
#include "guecs/lel.hpp"
|
||||
#include <fmt/core.h>
|
||||
#include <iostream>
|
||||
|
||||
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, 41,
|
||||
44, 56, 61
|
||||
};
|
||||
|
||||
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, 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, 0, 1,
|
||||
4, 3, 2
|
||||
};
|
||||
|
||||
static const char _Parser_range_lengths[] = {
|
||||
0, 1, 4, 3, 1, 1, 1, 1,
|
||||
4, 1, 1
|
||||
};
|
||||
|
||||
static const char _Parser_index_offsets[] = {
|
||||
0, 0, 4, 17, 28, 30, 34, 36,
|
||||
39, 48, 53
|
||||
};
|
||||
|
||||
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, 1, 16, 17, 1, 18,
|
||||
20, 19, 21, 18, 19, 19, 19, 1,
|
||||
22, 23, 24, 22, 1, 25, 2, 25,
|
||||
1, 0
|
||||
};
|
||||
|
||||
static const char _Parser_trans_targs[] = {
|
||||
1, 0, 2, 2, 3, 4, 3, 3,
|
||||
3, 3, 8, 5, 3, 6, 5, 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, 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(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 {
|
||||
dbc::log("error at:");
|
||||
std::cout << p;
|
||||
}
|
||||
return good;
|
||||
}
|
||||
|
||||
}
|
67
src/guecs/lel_parser.rl
Normal file
67
src/guecs/lel_parser.rl
Normal file
|
@ -0,0 +1,67 @@
|
|||
#include "guecs/lel.hpp"
|
||||
#include <fmt/core.h>
|
||||
#include <iostream>
|
||||
|
||||
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 ("," 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(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 {
|
||||
dbc::log("error at:");
|
||||
std::cout << p;
|
||||
}
|
||||
return good;
|
||||
}
|
||||
|
||||
}
|
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;
|
||||
}
|
||||
};
|
228
src/guecs/ui.cpp
Normal file
228
src/guecs/ui.cpp
Normal file
|
@ -0,0 +1,228 @@
|
|||
#include "guecs/ui.hpp"
|
||||
#include <typeinfo>
|
||||
|
||||
namespace guecs {
|
||||
using std::make_shared;
|
||||
|
||||
UI::UI() {
|
||||
$font = make_shared<sf::Font>(FONT_FILE_NAME);
|
||||
}
|
||||
|
||||
void UI::position(int x, int y, int width, int height) {
|
||||
$parser.position(x, y, width, height);
|
||||
}
|
||||
|
||||
sf::Vector2f UI::get_position() {
|
||||
return {float($parser.grid_x), float($parser.grid_y)};
|
||||
}
|
||||
|
||||
sf::Vector2f 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);
|
||||
dbc::check(good, "LEL parsing failed.");
|
||||
|
||||
for(auto& [name, cell] : $parser.cells) {
|
||||
auto ent = init_entity(name);
|
||||
set<lel::Cell>(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 makes it easier to get the name during querying
|
||||
set<CellName>(ent, {name});
|
||||
return ent;
|
||||
}
|
||||
|
||||
Entity UI::entity(const string& name) {
|
||||
dbc::check($name_ents.contains(name),
|
||||
fmt::format("GUECS entity {} does not exist. Mispelled cell name?", name));
|
||||
return $name_ents.at(name);
|
||||
}
|
||||
|
||||
Entity UI::entity(const string& name, int id) {
|
||||
return entity(fmt::format("{}{}", name, id));
|
||||
}
|
||||
|
||||
void UI::init() {
|
||||
query<Background>([](auto, auto& bg) {
|
||||
bg.init();
|
||||
});
|
||||
|
||||
query<lel::Cell, Rectangle>([](auto, auto& cell, auto& rect) {
|
||||
rect.init(cell);
|
||||
});
|
||||
|
||||
query<lel::Cell, Effect>([](auto, auto& cell, auto& shader) {
|
||||
shader.init(cell);
|
||||
});
|
||||
|
||||
query<Rectangle, Meter>([](auto, auto& bg, auto &meter) {
|
||||
bg.shape->setFillColor(meter.color);
|
||||
});
|
||||
|
||||
query<lel::Cell, Meter>([](auto, auto &cell, auto& meter) {
|
||||
meter.init(cell);
|
||||
});
|
||||
|
||||
query<lel::Cell, Textual>([this](auto, auto& cell, auto& text) {
|
||||
text.init(cell, $font);
|
||||
});
|
||||
|
||||
query<lel::Cell, Label>([this](auto, auto& cell, auto& text) {
|
||||
text.init(cell, $font);
|
||||
});
|
||||
|
||||
query<lel::Cell, Sprite>([&](auto, auto &cell, auto &sprite) {
|
||||
sprite.init(cell);
|
||||
});
|
||||
}
|
||||
|
||||
void UI::debug_layout(sf::RenderWindow& window) {
|
||||
query<lel::Cell>([&](const auto, auto &cell) {
|
||||
sf::RectangleShape rect{{float(cell.w), float(cell.h)}};
|
||||
rect.setPosition({float(cell.x), float(cell.y)});
|
||||
rect.setFillColor(sf::Color::Transparent);
|
||||
rect.setOutlineColor(sf::Color::Red);
|
||||
rect.setOutlineThickness(2.0f);
|
||||
window.draw(rect);
|
||||
});
|
||||
}
|
||||
|
||||
void UI::render(sf::RenderWindow& window) {
|
||||
if(auto bg = get_if<Background>(MAIN)) {
|
||||
window.draw(*(bg->shape));
|
||||
}
|
||||
|
||||
query<Effect>([&](auto, auto& shader) {
|
||||
if(shader.$active) shader.step();
|
||||
});
|
||||
|
||||
query<Rectangle>([&](auto ent, auto& rect) {
|
||||
render_helper(window, ent, true, rect.shape);
|
||||
});
|
||||
|
||||
query<lel::Cell, Meter>([&](auto ent, auto& cell, auto &meter) {
|
||||
meter.render(cell);
|
||||
render_helper(window, ent, true, meter.bar.shape);
|
||||
});
|
||||
|
||||
query<Sprite>([&](auto ent, auto& sprite) {
|
||||
render_helper(window, ent, false, sprite.sprite);
|
||||
});
|
||||
|
||||
query<Label>([&](auto ent, auto& text) {
|
||||
render_helper(window, ent, false, text.text);
|
||||
});
|
||||
|
||||
query<Textual>([&](auto ent, auto& text) {
|
||||
render_helper(window, ent, true, text.text);
|
||||
});
|
||||
}
|
||||
|
||||
bool UI::mouse(float x, float y, bool hover) {
|
||||
int action_count = 0;
|
||||
|
||||
query<lel::Cell>([&](auto ent, auto& cell) {
|
||||
if((x >= cell.x && x <= cell.x + cell.w) &&
|
||||
(y >= cell.y && y <= cell.y + cell.h))
|
||||
{
|
||||
do_if<Effect>(ent, [hover](auto& effect) {
|
||||
effect.$shader->setUniform("hover", hover);
|
||||
effect.run();
|
||||
});
|
||||
|
||||
do_if<Sound>(ent, [hover](auto& sound) {
|
||||
// here set that it played then only play once
|
||||
sound.play(hover);
|
||||
});
|
||||
|
||||
if(hover) return;
|
||||
|
||||
click_on(ent);
|
||||
|
||||
action_count++;
|
||||
} else {
|
||||
do_if<Effect>(ent, [hover](auto& effect) {
|
||||
effect.stop();
|
||||
});
|
||||
|
||||
do_if<Sound>(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<Sprite>(ent)) {
|
||||
sprite->update(sprite_name);
|
||||
} else {
|
||||
set_init<Sprite>(ent, {sprite_name});
|
||||
}
|
||||
}
|
||||
|
||||
void UI::show_text(const string& region, const wstring& content) {
|
||||
auto ent = entity(region);
|
||||
|
||||
if(auto tc = get_if<Textual>(ent)) {
|
||||
tc->text->setString(content);
|
||||
} else {
|
||||
auto &cell = cell_for(ent);
|
||||
Textual to_set{content, TEXT_SIZE};
|
||||
to_set.init(cell, $font);
|
||||
set<Textual>(ent, to_set);
|
||||
}
|
||||
}
|
||||
|
||||
void UI::click_on(const string& name, bool required) {
|
||||
auto ent = entity(name);
|
||||
|
||||
if(required) {
|
||||
dbc::check(has<Clickable>(ent),
|
||||
fmt::format("click_on required '{}' to exist but it doesn't", name));
|
||||
}
|
||||
|
||||
click_on(ent);
|
||||
}
|
||||
|
||||
void UI::click_on(Entity ent) {
|
||||
if(auto clicked = get_if<Clickable>(ent)) {
|
||||
if(auto action_data = get_if<ActionData>(ent)) {
|
||||
clicked->action(ent, action_data->data);
|
||||
} else {
|
||||
clicked->action(ent, {});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void UI::show_label(const string& region, const wstring& content) {
|
||||
auto ent = entity(region);
|
||||
|
||||
if(auto tc = get_if<Label>(ent)) {
|
||||
tc->text->setString(content);
|
||||
} else {
|
||||
auto &cell = cell_for(ent);
|
||||
Label to_set{content, LABEL_SIZE};
|
||||
to_set.init(cell, $font);
|
||||
to_set.text->setFillColor(TEXT_COLOR);
|
||||
set<Label>(ent, to_set);
|
||||
}
|
||||
}
|
||||
|
||||
wstring to_wstring(const string& str) {
|
||||
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> $converter;
|
||||
return $converter.from_bytes(str);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue