GUECS refactor part 1.

master
Zed A. Shaw 2 days ago
parent 4e7f837240
commit 20176cf54a
  1. 58
      guecs.cpp
  2. 110
      guecs.hpp

@ -155,70 +155,70 @@ namespace guecs {
for(auto& [name, cell] : $parser.cells) { for(auto& [name, cell] : $parser.cells) {
auto ent = init_entity(name); auto ent = init_entity(name);
$world.set<lel::Cell>(ent, cell); set<lel::Cell>(ent, cell);
} }
} }
DinkyECS::Entity UI::init_entity(const string& name) { Entity UI::init_entity(const string& name) {
auto entity = $world.entity(); auto ent = entity();
// this lets you look up an entity by name // this lets you look up an entity by name
$name_ents.insert_or_assign(name, entity); $name_ents.insert_or_assign(name, ent);
// this makes it easier to get the name during querying // this makes it easier to get the name during querying
$world.set<CellName>(entity, {name}); set<CellName>(ent, {name});
return entity; return ent;
} }
DinkyECS::Entity UI::entity(const string& name) { Entity UI::entity(const string& name) {
dbc::check($name_ents.contains(name), dbc::check($name_ents.contains(name),
fmt::format("GUECS entity {} does not exist. Mispelled cell name?", name)); fmt::format("GUECS entity {} does not exist. Mispelled cell name?", name));
return $name_ents.at(name); return $name_ents.at(name);
} }
DinkyECS::Entity UI::entity(const string& name, int id) { Entity UI::entity(const string& name, int id) {
return entity(fmt::format("{}{}", name, id)); return entity(fmt::format("{}{}", name, id));
} }
void UI::init() { void UI::init() {
if($world.has_the<Background>()) { if(has_the<Background>()) {
auto& bg = $world.get_the<Background>(); auto& bg = get_the<Background>();
bg.init(); bg.init();
} }
$world.query<Background>([](auto, auto& bg) { query<Background>([](auto, auto& bg) {
bg.init(); bg.init();
}); });
$world.query<lel::Cell, Rectangle>([](auto, auto& cell, auto& rect) { query<lel::Cell, Rectangle>([](auto, auto& cell, auto& rect) {
rect.init(cell); rect.init(cell);
}); });
$world.query<lel::Cell, Effect>([](auto, auto& cell, auto& shader) { query<lel::Cell, Effect>([](auto, auto& cell, auto& shader) {
shader.init(cell); shader.init(cell);
}); });
$world.query<Rectangle, Meter>([](auto, auto& bg, auto &meter) { query<Rectangle, Meter>([](auto, auto& bg, auto &meter) {
bg.shape->setFillColor(meter.color); bg.shape->setFillColor(meter.color);
}); });
$world.query<lel::Cell, Meter>([](auto, auto &cell, auto& meter) { query<lel::Cell, Meter>([](auto, auto &cell, auto& meter) {
meter.init(cell); meter.init(cell);
}); });
$world.query<lel::Cell, Textual>([this](auto, auto& cell, auto& text) { query<lel::Cell, Textual>([this](auto, auto& cell, auto& text) {
text.init(cell, $font); text.init(cell, $font);
}); });
$world.query<lel::Cell, Label>([this](auto, auto& cell, auto& text) { query<lel::Cell, Label>([this](auto, auto& cell, auto& text) {
text.init(cell, $font); text.init(cell, $font);
}); });
$world.query<lel::Cell, Sprite>([&](auto, auto &cell, auto &sprite) { query<lel::Cell, Sprite>([&](auto, auto &cell, auto &sprite) {
sprite.init(cell); sprite.init(cell);
}); });
} }
void UI::debug_layout(sf::RenderWindow& window) { void UI::debug_layout(sf::RenderWindow& window) {
$world.query<lel::Cell>([&](const auto, auto &cell) { query<lel::Cell>([&](const auto, auto &cell) {
sf::RectangleShape rect{{float(cell.w), float(cell.h)}}; sf::RectangleShape rect{{float(cell.w), float(cell.h)}};
rect.setPosition({float(cell.x), float(cell.y)}); rect.setPosition({float(cell.x), float(cell.y)});
rect.setFillColor(sf::Color::Transparent); rect.setFillColor(sf::Color::Transparent);
@ -229,33 +229,33 @@ namespace guecs {
} }
void UI::render(sf::RenderWindow& window) { void UI::render(sf::RenderWindow& window) {
if($world.has_the<Background>()) { if(has_the<Background>()) {
auto& bg = $world.get_the<Background>(); auto& bg = get_the<Background>();
window.draw(*bg.shape); window.draw(*bg.shape);
} }
$world.query<Effect>([&](auto, auto& shader) { query<Effect>([&](auto, auto& shader) {
if(shader.$active) shader.step(); if(shader.$active) shader.step();
}); });
$world.query<Rectangle>([&](auto ent, auto& rect) { query<Rectangle>([&](auto ent, auto& rect) {
render_helper(window, ent, true, rect.shape); render_helper(window, ent, true, rect.shape);
}); });
$world.query<lel::Cell, Meter>([&](auto ent, auto& cell, auto &meter) { query<lel::Cell, Meter>([&](auto ent, auto& cell, auto &meter) {
meter.render(cell); meter.render(cell);
render_helper(window, ent, true, meter.bar.shape); render_helper(window, ent, true, meter.bar.shape);
}); });
$world.query<Sprite>([&](auto ent, auto& sprite) { query<Sprite>([&](auto ent, auto& sprite) {
render_helper(window, ent, false, sprite.sprite); render_helper(window, ent, false, sprite.sprite);
}); });
$world.query<Label>([&](auto ent, auto& text) { query<Label>([&](auto ent, auto& text) {
render_helper(window, ent, false, text.text); render_helper(window, ent, false, text.text);
}); });
$world.query<Textual>([&](auto ent, auto& text) { query<Textual>([&](auto ent, auto& text) {
render_helper(window, ent, true, text.text); render_helper(window, ent, true, text.text);
}); });
} }
@ -263,7 +263,7 @@ namespace guecs {
bool UI::mouse(float x, float y, bool hover) { bool UI::mouse(float x, float y, bool hover) {
int action_count = 0; int action_count = 0;
$world.query<lel::Cell>([&](auto ent, auto& cell) { query<lel::Cell>([&](auto ent, auto& cell) {
if((x >= cell.x && x <= cell.x + cell.w) && if((x >= cell.x && x <= cell.x + cell.w) &&
(y >= cell.y && y <= cell.y + cell.h)) (y >= cell.y && y <= cell.y + cell.h))
{ {
@ -331,7 +331,7 @@ namespace guecs {
click_on(ent); click_on(ent);
} }
void UI::click_on(DinkyECS::Entity ent) { void UI::click_on(Entity ent) {
if(auto clicked = get_if<Clickable>(ent)) { if(auto clicked = get_if<Clickable>(ent)) {
if(auto action_data = get_if<ActionData>(ent)) { if(auto action_data = get_if<ActionData>(ent)) {
clicked->action(ent, action_data->data); clicked->action(ent, action_data->data);

@ -16,6 +16,16 @@
namespace guecs { namespace guecs {
using std::shared_ptr, std::make_shared, std::wstring, std::string; using std::shared_ptr, std::make_shared, std::wstring, std::string;
using Entity = DinkyECS::Entity;
using EntityMap = std::unordered_map<Entity, size_t>;
template <typename T>
struct ComponentStorage {
std::vector<T> data;
std::queue<size_t> free_indices;
};
struct Textual { struct Textual {
std::wstring content; std::wstring content;
unsigned int size = GUECS_FONT_SIZE; unsigned int size = GUECS_FONT_SIZE;
@ -45,7 +55,7 @@ namespace guecs {
/* This is actually called by UI::mouse and passed the entity ID of the /* 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. * button pressed so you can interact with it in the event handler.
*/ */
std::function<void(DinkyECS::Entity ent, std::any data)> action; std::function<void(Entity ent, std::any data)> action;
}; };
struct Sprite { struct Sprite {
@ -127,10 +137,16 @@ namespace guecs {
void init(); void init();
}; };
class UI { class UI {
public: public:
DinkyECS::World $world; DinkyECS::World $world;
std::unordered_map<string, DinkyECS::Entity> $name_ents; unsigned long entity_count = 0;
std::unordered_map<std::type_index, EntityMap> $components;
std::unordered_map<std::type_index, std::any> $facts;
std::unordered_map<std::type_index, std::any> $component_storages;
std::unordered_map<string, Entity> $name_ents;
shared_ptr<sf::Font> $font = nullptr; shared_ptr<sf::Font> $font = nullptr;
lel::Parser $parser; lel::Parser $parser;
string $grid = ""; string $grid = "";
@ -141,9 +157,9 @@ namespace guecs {
sf::Vector2f get_position(); sf::Vector2f get_position();
sf::Vector2f get_size(); sf::Vector2f get_size();
void layout(const string& grid); void layout(const string& grid);
DinkyECS::Entity init_entity(const string& name); Entity init_entity(const string& name);
DinkyECS::Entity entity(const string& name); Entity entity(const string& name);
DinkyECS::Entity entity(const string& name, int id); Entity entity(const string& name, int id);
inline lel::CellMap& cells() { inline lel::CellMap& cells() {
return $parser.cells; return $parser.cells;
@ -157,58 +173,90 @@ namespace guecs {
void render(sf::RenderWindow& window); void render(sf::RenderWindow& window);
bool mouse(float x, float y, bool hover); bool mouse(float x, float y, bool hover);
void click_on(const string& name, bool required=false); void click_on(const string& name, bool required=false);
void click_on(DinkyECS::Entity slot_id); void click_on(Entity slot_id);
void debug_layout(sf::RenderWindow& window); void debug_layout(sf::RenderWindow& window);
template <typename Comp>
void set(DinkyECS::Entity ent, Comp val) {
$world.set<Comp>(ent, val);
}
template <typename Comp>
void set_init(DinkyECS::Entity ent, Comp val) {
dbc::check(has<lel::Cell>(ent),"WRONG! slot is missing its cell?!"); Entity entity() { return $world.entity(); }
auto& cell = get<lel::Cell>(ent);
val.init(cell);
$world.set<Comp>(ent, val); template <typename Comp>
bool has_the() {
return $world.has_the<Comp>();
} }
template <typename Comp> template <typename Comp>
void do_if(DinkyECS::Entity ent, std::function<void(Comp &)> cb) { void set_the(Comp val) {
if($world.has<Comp>(ent)) { return $world.set_the<Comp>(val);
cb($world.get<Comp>(ent));
}
} }
lel::Cell& cell_for(DinkyECS::Entity ent) { template <typename Comp>
return $world.get<lel::Cell>(ent); Comp &get_the() {
return $world.get_the<Comp>();
} }
lel::Cell& cell_for(const string& name) { template <typename Comp>
DinkyECS::Entity ent = entity(name); void set(Entity ent, Comp val) {
return $world.get<lel::Cell>(ent); $world.set<Comp>(ent, val);
} }
template <typename Comp> template <typename Comp>
Comp& get(DinkyECS::Entity entity) { Comp& get(Entity entity) {
return $world.get<Comp>(entity); return $world.get<Comp>(entity);
} }
template <typename Comp> template <typename Comp>
Comp* get_if(DinkyECS::Entity entity) { Comp* get_if(Entity entity) {
return $world.get_if<Comp>(entity); return $world.get_if<Comp>(entity);
} }
template <typename Comp> template <typename Comp>
bool has(DinkyECS::Entity entity) { bool has(Entity entity) {
return $world.has<Comp>(entity); return $world.has<Comp>(entity);
} }
template <typename Comp> template <typename Comp>
void remove(DinkyECS::Entity ent) { void remove(Entity ent) {
$world.remove<Comp>(ent); $world.remove<Comp>(ent);
} }
template <typename Comp>
void query(std::function<void(Entity, Comp &)> cb) {
$world.query<Comp>(cb);
}
template <typename CompA, typename CompB>
void query(std::function<void(Entity, CompA &, CompB &)> cb) {
$world.query<CompA, CompB>(cb);
}
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);
$world.set<Comp>(ent, val);
}
template <typename Comp>
void do_if(Entity ent, std::function<void(Comp &)> cb) {
if($world.has<Comp>(ent)) {
cb($world.get<Comp>(ent));
}
}
lel::Cell& cell_for(Entity ent) {
return $world.get<lel::Cell>(ent);
}
lel::Cell& cell_for(const string& name) {
Entity ent = entity(name);
return $world.get<lel::Cell>(ent);
}
// BUG: close could just be remove with overload // BUG: close could just be remove with overload
template <typename Comp> template <typename Comp>
void close(string region) { void close(string region) {
@ -219,7 +267,7 @@ namespace guecs {
} }
template<typename T> template<typename T>
void render_helper(sf::RenderWindow& window, DinkyECS::Entity ent, bool is_shape, T& target) { void render_helper(sf::RenderWindow& window, Entity ent, bool is_shape, T& target) {
sf::Shader *shader_ptr = nullptr; sf::Shader *shader_ptr = nullptr;
if(auto shader = $world.get_if<Effect>(ent)) { if(auto shader = $world.get_if<Effect>(ent)) {

Loading…
Cancel
Save