minor update

This commit is contained in:
2026-07-22 12:34:18 +01:00
parent 51485e980e
commit f4ad43b05f
5 changed files with 142 additions and 104 deletions
@@ -2,7 +2,11 @@
#include "engine-variant.hpp" #include "engine-variant.hpp"
template<class... Ts> struct overloaded : Ts... { using Ts::operator()...; }; namespace varicle {
template <class... Ts> struct overloaded : Ts... {
using Ts::operator()...;
};
template <class... Ts> overloaded(Ts...) -> overloaded<Ts...>; template <class... Ts> overloaded(Ts...) -> overloaded<Ts...>;
enum class OpType { enum class OpType {
@@ -25,50 +29,83 @@ class VariantOpManager{
VariantOpManager() {} VariantOpManager() {}
void ExecuteOperation(const VariantOpRequest &req) { void ExecuteOperation(const VariantOpRequest &req) {
if (req.target == nullptr) return; if (req.target == nullptr)
return;
switch (req.operation) { switch (req.operation) {
case OpType::Assign: case OpType::Assign:
req.target->data = req.operand.data; req.target->data = req.operand.data;
req.target->type = req.operand.type;
break; break;
case OpType::Add: case OpType::Add:
req.target->data = std::visit( req.target->data = std::visit(
overloaded{ overloaded{
[](float p, float n) -> EngineVariant::InternalVariant {return p + n;}, [](float p, float n) -> EngineVariant::InternalVariant {
[](Vec2 p, Vec2 n) -> EngineVariant::InternalVariant {return p + n;}, return p + n;
[](Vec3 p, Vec3 n) -> EngineVariant::InternalVariant {return p + n;}, },
[](Vec4 p, Vec4 n) -> EngineVariant::InternalVariant {return p + n;}, [](Vec2 p, Vec2 n) -> EngineVariant::InternalVariant {
return p + n;
// Fallback },
[](auto p, auto n) -> EngineVariant::InternalVariant {return p;}, [](Vec3 p, Vec3 n) -> EngineVariant::InternalVariant {
},req.target->data,req.operand.data); return p + n;
break; },
[](Vec4 p, Vec4 n) -> EngineVariant::InternalVariant {
case OpType::Lerp: return p + n;
req.target->data = std::visit(overloaded{
[&](float p, float n) -> EngineVariant::InternalVariant { return p + (n - p) * req.alpha; },
[&](Vec2 p, Vec2 n) -> EngineVariant::InternalVariant { return Vec2Lerp(p, n, req.alpha); },
[&](Vec3 p, Vec3 n) -> EngineVariant::InternalVariant { return Vec3Lerp(p, n, req.alpha); },
[&](Vec4 p, Vec4 n) -> EngineVariant::InternalVariant { return Vec4Lerp(p, n, req.alpha); },
[&](const std::string& p, const std::string& n) -> EngineVariant::InternalVariant {
return req.alpha >= 0.5f ? n : p; // Discrete switch for strings
}, },
// Fallback // Fallback
[](auto p, auto n) -> EngineVariant::InternalVariant { return p; } [](auto p, auto n) -> EngineVariant::InternalVariant {
}, req.target->data, req.operand.data); return p;
},
},
req.target->data, req.operand.data);
break;
case OpType::Lerp:
req.target->data = std::visit(
overloaded{
[&](float p, float n) -> EngineVariant::InternalVariant {
return p + (n - p) * req.alpha;
},
[&](Vec2 p, Vec2 n) -> EngineVariant::InternalVariant {
return Vec2Lerp(p, n, req.alpha);
},
[&](Vec3 p, Vec3 n) -> EngineVariant::InternalVariant {
return Vec3Lerp(p, n, req.alpha);
},
[&](Vec4 p, Vec4 n) -> EngineVariant::InternalVariant {
return Vec4Lerp(p, n, req.alpha);
},
[&](const std::string &p, const std::string &n)
-> EngineVariant::InternalVariant {
return req.alpha >= 0.5f
? n
: p; // Discrete switch for strings
},
// Fallback
[](auto p, auto n) -> EngineVariant::InternalVariant {
return p;
}},
req.target->data, req.operand.data);
break; break;
case OpType::Multiply: case OpType::Multiply:
req.target->data = std::visit(overloaded{ req.target->data = std::visit(
[](float p, float n) -> EngineVariant::InternalVariant { return p * n; }, overloaded{
[](Vec2 p, float n) -> EngineVariant::InternalVariant { return p * n; }, // Scale a vector! [](float p, float n) -> EngineVariant::InternalVariant {
[](auto p, auto n) -> EngineVariant::InternalVariant { return p; } return p * n;
}, req.target->data, req.operand.data); },
[](Vec2 p, float n) -> EngineVariant::InternalVariant {
return p * n;
}, // Scale a vector!
[](auto p, auto n) -> EngineVariant::InternalVariant {
return p;
}},
req.target->data, req.operand.data);
break; break;
} }
} }
}; };
} // namespace varicle
@@ -6,10 +6,12 @@
#include <functional> #include <functional>
#include <unordered_map> #include <unordered_map>
namespace varicle {
using VariantGetter = using VariantGetter =
std::function<EngineVariant(const entt::registry &, entt::entity)>; std::function<EngineVariant(const entt::registry &, entt::entity)>;
using VariantSetter = std::function<void(entt::registry &, entt::entity, using VariantSetter =
const EngineVariant &)>; std::function<void(entt::registry &, entt::entity, const EngineVariant &)>;
struct PropertyRef { struct PropertyRef {
uint32_t property_id; uint32_t property_id;
@@ -47,8 +49,7 @@ class PropertyRegistry {
return EngineVariant((*component).*member); return EngineVariant((*component).*member);
} }
return EngineVariant(); return EngineVariant();
} }};
};
} }
}; };
@@ -70,8 +71,8 @@ class PropertyDatabase {
return EngineVariant(); return EngineVariant();
} }
bool set_value(entt::registry &reg, entt::entity e, bool set_value(entt::registry &reg, entt::entity e, uint32_t prop_hash,
uint32_t prop_hash, const EngineVariant &value) const { const EngineVariant &value) const {
auto it = properites.find(prop_hash); auto it = properites.find(prop_hash);
if (it != properites.end()) { if (it != properites.end()) {
it->second.set(reg, e, value); it->second.set(reg, e, value);
@@ -80,3 +81,4 @@ class PropertyDatabase {
return false; return false;
} }
}; };
} // namespace varicle
@@ -3,6 +3,7 @@
#include <string> #include <string>
#include <variant> #include <variant>
namespace varicle {
// Overload helper pattern // Overload helper pattern
template <class... Ts> struct Overloaded : Ts... { template <class... Ts> struct Overloaded : Ts... {
using Ts::operator()...; using Ts::operator()...;
@@ -75,8 +76,8 @@ enum class VariantType { Null, Float, Vector2, Vector3, Vector4, String };
class EngineVariant { class EngineVariant {
public: public:
using InternalVariant = using InternalVariant = std::variant<std::monostate, float, Vec2, Vec3,
std::variant<std::monostate, float, Vec2, Vec3, Vec4, std::string, bool>; Vec4, std::string, bool>;
EngineVariant() : data(std::monostate{}) {} EngineVariant() : data(std::monostate{}) {}
EngineVariant(float v) : data(v) {} EngineVariant(float v) : data(v) {}
@@ -109,7 +110,9 @@ class EngineVariant {
std::to_string(v.w) + ")"; std::to_string(v.w) + ")";
}, },
[](const std::string &s) { return s; }, [](const std::string &s) { return s; },
[](const bool &b) { return std::string(b ? "true" : "false"); }}, [](const bool &b) {
return std::string(b ? "true" : "false");
}},
data); data);
} }
@@ -147,3 +150,4 @@ class EngineVariant {
friend class VariantOpManager; friend class VariantOpManager;
}; };
} // namespace varicle
+17 -22
View File
@@ -3,8 +3,8 @@
#include "engine/core/service-locator.hpp" #include "engine/core/service-locator.hpp"
#include "engine/ecs/components.hpp" #include "engine/ecs/components.hpp"
#include<raylib.h>
#include <entt/entt.hpp> #include <entt/entt.hpp>
#include <raylib.h>
namespace varicle { namespace varicle {
@@ -13,8 +13,10 @@ namespace varicle {
for (entt::entity entity : view) { for (entt::entity entity : view) {
const Sprite &sprite = view.get<Sprite>(entity); const Sprite &sprite = view.get<Sprite>(entity);
auto [global_position,global_scale,global_rotation] = view.get<GlobalTransform2D>(entity); auto [global_position, global_scale, global_rotation] =
auto texture = ServiceLocator::get<RaylibAssetLoader>().get_texture(sprite.texture_path); view.get<GlobalTransform2D>(entity);
auto texture = ServiceLocator::get<RaylibAssetLoader>().get_texture(
sprite.texture_path);
float width = sprite.width * global_scale; float width = sprite.width * global_scale;
float height = sprite.height * global_scale; float height = sprite.height * global_scale;
@@ -24,10 +26,12 @@ namespace varicle {
DrawTexturePro( DrawTexturePro(
*(texture), *(texture),
Rectangle{ Rectangle{
0, 0.0f,
0, 0.0f,
(float)texture->width, static_cast<float>(sprite.flip_h ? -texture->width
(float)texture->height, : texture->width),
static_cast<float>(sprite.flip_v ? -texture->height
: texture->height),
}, },
Rectangle{ Rectangle{
sprite.offset_x + global_position.x, sprite.offset_x + global_position.x,
@@ -35,23 +39,14 @@ namespace varicle {
width, width,
height, height,
}, },
Vector2 {width * 0.5f, height*0.5f}, Vector2{width * 0.5f, height * 0.5f}, rotation, WHITE);
rotation,
WHITE);
} else { } else {
DrawRectanglePro(Rectangle { DrawRectanglePro(
sprite.offset_x + global_position.x, Rectangle{sprite.offset_x + global_position.x,
sprite.offset_y + global_position.y, sprite.offset_y + global_position.y, width, height},
width, Vector2{width * 0.5f, height * 0.5f}, rotation, PURPLE);
height
},
Vector2 {width * 0.5f, height*0.5f},
rotation,
PURPLE
);
} }
} }
}
} }
} // namespace varicle
+1 -1
View File
@@ -28,7 +28,7 @@ void MenuScene::init() {
registry.emplace<varicle::GlobalTransform2D>(e, 650.0f, 150.0f, 1.0f,0.0f); registry.emplace<varicle::GlobalTransform2D>(e, 650.0f, 150.0f, 1.0f,0.0f);
// registry.emplace<Position>(e, 650.0f, 150.0f); // registry.emplace<Position>(e, 650.0f, 150.0f);
registry.emplace<Sprite>(e, "assets/bird.png", 0.0f, 0.f, 100.f, 100.f, registry.emplace<Sprite>(e, "assets/bird.png", 0.0f, 0.f, 100.f, 100.f,
false, false, 0.f); true, false, 0.f);
auto img = auto img =
LoadImageFromTexture(*asset_loader.get_texture("assets/bird.png")); LoadImageFromTexture(*asset_loader.get_texture("assets/bird.png"));
SetWindowIcon(img); SetWindowIcon(img);