feat: engine variant
integrate engine variant add property registry and database modify render system to use new transform components
This commit is contained in:
@@ -5,6 +5,7 @@ build-web/
|
||||
bin/
|
||||
web/
|
||||
debug/
|
||||
vendor/
|
||||
|
||||
# artifacts
|
||||
compile_commands.json
|
||||
|
||||
@@ -1,87 +1,89 @@
|
||||
// src/engine/core/application.cpp
|
||||
|
||||
#include "engine/core/application.hpp"
|
||||
#include "engine/core/service-locator.hpp"
|
||||
#include "engine/asset/raylib-asset.hpp"
|
||||
#include "engine/core/engine-variant/engine-variant-property.hpp"
|
||||
#include "engine/core/service-locator.hpp"
|
||||
|
||||
#include "engine/ecs/bindings.hpp"
|
||||
#include "engine/scene/scene.hpp"
|
||||
#include "imgui.h"
|
||||
#include "raylib.h"
|
||||
#include "rlImGui.h"
|
||||
#include <memory>
|
||||
|
||||
|
||||
#ifdef __EMSCRIPTEN__
|
||||
#include <emscripten.h>
|
||||
#endif
|
||||
|
||||
namespace varicle {
|
||||
|
||||
void game_loop(void){
|
||||
void game_loop(void) {
|
||||
|
||||
auto& scene_manager = ServiceLocator::get<SceneManager>();
|
||||
auto &scene_manager = ServiceLocator::get<SceneManager>();
|
||||
|
||||
float dt = GetFrameTime();
|
||||
float dt = GetFrameTime();
|
||||
|
||||
// Update
|
||||
scene_manager.update(dt);
|
||||
// Update
|
||||
scene_manager.update(dt);
|
||||
|
||||
// Draw
|
||||
BeginDrawing();
|
||||
ClearBackground(RAYWHITE); // Or a configurable engine default color
|
||||
// Draw
|
||||
BeginDrawing();
|
||||
ClearBackground(RAYWHITE); // Or a configurable engine default color
|
||||
|
||||
scene_manager.render();
|
||||
scene_manager.render();
|
||||
|
||||
rlImGuiBegin();
|
||||
scene_manager.ui();
|
||||
rlImGuiEnd();
|
||||
rlImGuiBegin();
|
||||
scene_manager.ui();
|
||||
rlImGuiEnd();
|
||||
|
||||
EndDrawing();
|
||||
EndDrawing();
|
||||
|
||||
scene_manager.process_scene_switch();
|
||||
}
|
||||
scene_manager.process_scene_switch();
|
||||
}
|
||||
|
||||
void Application::run() {
|
||||
// 1. Initialize Raylib
|
||||
SetTraceLogLevel(LOG_NONE);
|
||||
SetConfigFlags(FLAG_VSYNC_HINT);
|
||||
InitWindow(m_window_width, m_window_height, m_window_title);
|
||||
SetTargetFPS(60);
|
||||
rlImGuiSetup(true); // initialize gui
|
||||
void Application::run() {
|
||||
// 1. Initialize Raylib
|
||||
SetTraceLogLevel(LOG_NONE);
|
||||
SetConfigFlags(FLAG_VSYNC_HINT);
|
||||
InitWindow(m_window_width, m_window_height, m_window_title);
|
||||
SetTargetFPS(60);
|
||||
rlImGuiSetup(true); // initialize gui
|
||||
|
||||
ServiceLocator::provide(std::make_unique<RaylibAssetLoader>());
|
||||
ServiceLocator::provide(std::make_unique<SceneManager>());
|
||||
// 2. Setup Service Locators
|
||||
ServiceLocator::provide(std::make_unique<PropertyDatabase>());
|
||||
ServiceLocator::provide(std::make_unique<RaylibAssetLoader>());
|
||||
ServiceLocator::provide(std::make_unique<SceneManager>());
|
||||
|
||||
// 2. Call game-specific startup (where the game creates its first scene)
|
||||
on_init();
|
||||
register_all_component_properties(ServiceLocator::get<PropertyDatabase>());
|
||||
|
||||
auto& scene_manager = ServiceLocator::get<SceneManager>();
|
||||
// 3. Call game-specific startup (where the game creates its first scene)
|
||||
on_init();
|
||||
|
||||
auto &scene_manager = ServiceLocator::get<SceneManager>();
|
||||
|
||||
#ifdef __EMSCRIPTEN__
|
||||
emscripten_set_main_loop(game_loop, 0, 1);
|
||||
emscripten_set_main_loop(game_loop, 0, 1);
|
||||
#else
|
||||
while (!scene_manager.should_game_close()){
|
||||
game_loop();
|
||||
}
|
||||
while (!scene_manager.should_game_close()) {
|
||||
game_loop();
|
||||
}
|
||||
#endif
|
||||
|
||||
// 4. Call game-specific cleanup
|
||||
on_shutdown();
|
||||
// 4. Call game-specific cleanup
|
||||
on_shutdown();
|
||||
|
||||
ServiceLocator::shutdown(); // Clean up services
|
||||
|
||||
// 5. Close Raylib
|
||||
rlImGuiShutdown();
|
||||
CloseWindow();
|
||||
}
|
||||
|
||||
void Application::change_scene(std::string scene_id) {
|
||||
ServiceLocator::get<SceneManager>().switch_to_scene(scene_id);
|
||||
}
|
||||
|
||||
void Application::quit() {
|
||||
ServiceLocator::get<SceneManager>().quit();
|
||||
}
|
||||
ServiceLocator::shutdown(); // Clean up services
|
||||
|
||||
// 5. Close Raylib
|
||||
rlImGuiShutdown();
|
||||
CloseWindow();
|
||||
}
|
||||
|
||||
void Application::change_scene(std::string scene_id) {
|
||||
ServiceLocator::get<SceneManager>().switch_to_scene(scene_id);
|
||||
}
|
||||
|
||||
void Application::quit() { ServiceLocator::get<SceneManager>().quit(); }
|
||||
|
||||
} // namespace varicle
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include <engine_variant.hpp>
|
||||
#include "engine-variant.hpp"
|
||||
|
||||
template<class... Ts> struct overloaded : Ts... { using Ts::operator()...; };
|
||||
template<class... Ts> overloaded(Ts...) -> overloaded<Ts...>;
|
||||
@@ -0,0 +1,82 @@
|
||||
#pragma once
|
||||
|
||||
#include "engine-variant.hpp"
|
||||
|
||||
#include <entt/entt.hpp>
|
||||
#include <functional>
|
||||
#include <unordered_map>
|
||||
|
||||
using VariantGetter =
|
||||
std::function<EngineVariant(const entt::registry &, entt::entity)>;
|
||||
using VariantSetter = std::function<void(entt::registry &, entt::entity,
|
||||
const EngineVariant &)>;
|
||||
|
||||
struct PropertyRef {
|
||||
uint32_t property_id;
|
||||
VariantSetter setter;
|
||||
VariantGetter getter;
|
||||
|
||||
EngineVariant get(const entt::registry ®, entt::entity e) const {
|
||||
return getter ? getter(reg, e) : EngineVariant();
|
||||
}
|
||||
|
||||
void set(entt::registry ®, entt::entity e,
|
||||
const EngineVariant &value) const {
|
||||
if (setter)
|
||||
setter(reg, e, value);
|
||||
}
|
||||
};
|
||||
|
||||
class PropertyRegistry {
|
||||
public:
|
||||
template <typename Component, typename T>
|
||||
static PropertyRef bind_field(uint32_t prop_hash, T Component::*member) {
|
||||
return PropertyRef{.property_id = prop_hash,
|
||||
|
||||
.setter =
|
||||
[member](entt::registry ®, entt::entity e,
|
||||
const EngineVariant &val) {
|
||||
if (auto *comp = reg.try_get<Component>(e)) {
|
||||
(*comp).*member = val.Get<T>();
|
||||
}
|
||||
},
|
||||
.getter = [member](const entt::registry ®,
|
||||
entt::entity e) -> EngineVariant {
|
||||
if (auto *component =
|
||||
reg.try_get<Component>(e)) {
|
||||
return EngineVariant((*component).*member);
|
||||
}
|
||||
return EngineVariant();
|
||||
}
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
class PropertyDatabase {
|
||||
private:
|
||||
std::unordered_map<uint32_t, PropertyRef> properites;
|
||||
|
||||
public:
|
||||
void register_property(uint32_t prop_hash, PropertyRef ref) {
|
||||
properites[prop_hash] = std::move(ref);
|
||||
}
|
||||
|
||||
EngineVariant get_value(const entt::registry ®, entt::entity e,
|
||||
uint32_t prop_hash) const {
|
||||
auto it = properites.find(prop_hash);
|
||||
if (it != properites.end()) {
|
||||
return it->second.get(reg, e);
|
||||
}
|
||||
return EngineVariant();
|
||||
}
|
||||
|
||||
bool set_value(entt::registry ®, entt::entity e,
|
||||
uint32_t prop_hash, const EngineVariant &value) const {
|
||||
auto it = properites.find(prop_hash);
|
||||
if (it != properites.end()) {
|
||||
it->second.set(reg, e, value);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,149 @@
|
||||
#pragma once
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <variant>
|
||||
|
||||
// Overload helper pattern
|
||||
template <class... Ts> struct Overloaded : Ts... {
|
||||
using Ts::operator()...;
|
||||
};
|
||||
template <class... Ts> Overloaded(Ts...) -> Overloaded<Ts...>;
|
||||
|
||||
struct Vec2 {
|
||||
float x = 0.0f;
|
||||
float y = 0.0f;
|
||||
|
||||
bool operator==(const Vec2 &o) const { return x == o.x && y == o.y; }
|
||||
bool operator!=(const Vec2 &o) const { return !(*this == o); }
|
||||
Vec2 operator+(const Vec2 &o) const { return {x + o.x, y + o.y}; }
|
||||
Vec2 operator-(const Vec2 &o) const { return {x - o.x, y - o.y}; }
|
||||
Vec2 operator*(float scalar) const { return {x * scalar, y * scalar}; }
|
||||
};
|
||||
|
||||
struct Vec3 {
|
||||
float x = 0.0f;
|
||||
float y = 0.0f;
|
||||
float z = 0.0f;
|
||||
|
||||
bool operator==(const Vec3 &o) const {
|
||||
return x == o.x && y == o.y && z == o.z;
|
||||
}
|
||||
bool operator!=(const Vec3 &o) const { return !(*this == o); }
|
||||
Vec3 operator+(const Vec3 &o) const { return {x + o.x, y + o.y, z + o.z}; }
|
||||
Vec3 operator-(const Vec3 &o) const { return {x - o.x, y - o.y, z - o.z}; }
|
||||
Vec3 operator*(float scalar) const {
|
||||
return {x * scalar, y * scalar, z * scalar};
|
||||
}
|
||||
};
|
||||
|
||||
struct Vec4 {
|
||||
float x = 0.0f;
|
||||
float y = 0.0f;
|
||||
float z = 0.0f;
|
||||
float w = 1.0f;
|
||||
|
||||
bool operator==(const Vec4 &o) const {
|
||||
return x == o.x && y == o.y && z == o.z && w == o.w;
|
||||
}
|
||||
bool operator!=(const Vec4 &o) const { return !(*this == o); }
|
||||
Vec4 operator+(const Vec4 &o) const {
|
||||
return {x + o.x, y + o.y, z + o.z, w + o.w};
|
||||
}
|
||||
Vec4 operator-(const Vec4 &o) const {
|
||||
return {x - o.x, y - o.y, z - o.z, w - o.w};
|
||||
}
|
||||
Vec4 operator*(float scalar) const {
|
||||
return {x * scalar, y * scalar, z * scalar, w * scalar};
|
||||
}
|
||||
};
|
||||
|
||||
// Standalone Interpolation Helpers
|
||||
inline Vec2 Vec2Lerp(const Vec2 &s, const Vec2 &e, float a) {
|
||||
return {s.x + (e.x - s.x) * a, s.y + (e.y - s.y) * a};
|
||||
}
|
||||
inline Vec3 Vec3Lerp(const Vec3 &s, const Vec3 &e, float a) {
|
||||
return {s.x + (e.x - s.x) * a, s.y + (e.y - s.y) * a,
|
||||
s.z + (e.z - s.z) * a};
|
||||
}
|
||||
inline Vec4 Vec4Lerp(const Vec4 &s, const Vec4 &e, float a) {
|
||||
return {s.x + (e.x - s.x) * a, s.y + (e.y - s.y) * a, s.z + (e.z - s.z) * a,
|
||||
s.w + (e.w - s.w) * a};
|
||||
}
|
||||
|
||||
enum class VariantType { Null, Float, Vector2, Vector3, Vector4, String };
|
||||
|
||||
class EngineVariant {
|
||||
|
||||
public:
|
||||
using InternalVariant =
|
||||
std::variant<std::monostate, float, Vec2, Vec3, Vec4, std::string, bool>;
|
||||
|
||||
EngineVariant() : data(std::monostate{}) {}
|
||||
EngineVariant(float v) : data(v) {}
|
||||
EngineVariant(Vec2 v) : data(v) {}
|
||||
EngineVariant(Vec3 v) : data(v) {}
|
||||
EngineVariant(Vec4 v) : data(v) {}
|
||||
EngineVariant(std::string v) : data(v) {}
|
||||
EngineVariant(bool v) : data(v) {}
|
||||
|
||||
template <typename T> T Get() const { return std::get<T>(data); }
|
||||
template <typename T> T *TryGet() { return std::get_if<T>(&data); }
|
||||
|
||||
std::string ToString() const {
|
||||
return std::visit(
|
||||
Overloaded{[](std::monostate) { return std::string("!"); },
|
||||
[](float v) { return std::to_string(v); },
|
||||
[](const Vec2 &v) {
|
||||
return "(" + std::to_string(v.x) + ", " +
|
||||
std::to_string(v.y) + ")";
|
||||
},
|
||||
[](const Vec3 &v) {
|
||||
return "(" + std::to_string(v.x) + ", " +
|
||||
std::to_string(v.y) + ", " +
|
||||
std::to_string(v.z) + ")";
|
||||
},
|
||||
[](const Vec4 &v) {
|
||||
return "(" + std::to_string(v.x) + ", " +
|
||||
std::to_string(v.y) + ", " +
|
||||
std::to_string(v.z) + ", " +
|
||||
std::to_string(v.w) + ")";
|
||||
},
|
||||
[](const std::string &s) { return s; },
|
||||
[](const bool &b) { return std::string(b ? "true" : "false"); }},
|
||||
data);
|
||||
}
|
||||
|
||||
friend std::ostream &operator<<(std::ostream &os,
|
||||
const EngineVariant &variant) {
|
||||
return os << variant.GetTypeName() << variant.ToString();
|
||||
}
|
||||
|
||||
private:
|
||||
InternalVariant data;
|
||||
|
||||
std::string GetTypeName() const {
|
||||
return std::visit(
|
||||
[](auto &&arg) -> std::string {
|
||||
using T = std::decay_t<decltype(arg)>;
|
||||
if constexpr (std::is_same_v<T, std::monostate>)
|
||||
return "Null";
|
||||
else if constexpr (std::is_same_v<T, float>)
|
||||
return "Float";
|
||||
else if constexpr (std::is_same_v<T, Vec2>)
|
||||
return "Vec2";
|
||||
else if constexpr (std::is_same_v<T, Vec3>)
|
||||
return "Vec3";
|
||||
else if constexpr (std::is_same_v<T, Vec4>)
|
||||
return "Vec4";
|
||||
else if constexpr (std::is_same_v<T, std::string>)
|
||||
return "String";
|
||||
else if constexpr (std::is_same_v<T, bool>)
|
||||
return "Bool";
|
||||
else
|
||||
return "Unknown";
|
||||
},
|
||||
data);
|
||||
}
|
||||
|
||||
friend class VariantOpManager;
|
||||
};
|
||||
@@ -1,109 +0,0 @@
|
||||
#pragma once
|
||||
#include <variant>
|
||||
#include <string>
|
||||
#include <mutex>
|
||||
#include <iostream>
|
||||
|
||||
struct Vec2 {
|
||||
float x = 0.0f;
|
||||
float y = 0.0f;
|
||||
|
||||
bool operator==(const Vec2& o) const { return x == o.x && y == o.y; }
|
||||
bool operator!=(const Vec2& o) const { return !(*this == o); }
|
||||
Vec2 operator+(const Vec2& o) const { return { x + o.x, y + o.y }; }
|
||||
Vec2 operator-(const Vec2& o) const { return { x - o.x, y - o.y }; }
|
||||
Vec2 operator*(float scalar) const { return { x * scalar, y * scalar }; }
|
||||
};
|
||||
|
||||
struct Vec3 {
|
||||
float x = 0.0f;
|
||||
float y = 0.0f;
|
||||
float z = 0.0f;
|
||||
|
||||
bool operator==(const Vec3& o) const { return x == o.x && y == o.y && z == o.z; }
|
||||
bool operator!=(const Vec3& o) const { return !(*this == o); }
|
||||
Vec3 operator+(const Vec3& o) const { return { x + o.x, y + o.y, z + o.z }; }
|
||||
Vec3 operator-(const Vec3& o) const { return { x - o.x, y - o.y, z - o.z }; }
|
||||
Vec3 operator*(float scalar) const { return { x * scalar, y * scalar, z * scalar }; }
|
||||
};
|
||||
|
||||
struct Vec4 {
|
||||
float x = 0.0f;
|
||||
float y = 0.0f;
|
||||
float z = 0.0f;
|
||||
float w = 1.0f;
|
||||
|
||||
bool operator==(const Vec4& o) const { return x == o.x && y == o.y && z == o.z && w == o.w; }
|
||||
bool operator!=(const Vec4& o) const { return !(*this == o); }
|
||||
Vec4 operator+(const Vec4& o) const { return { x + o.x, y + o.y, z + o.z, w + o.w }; }
|
||||
Vec4 operator-(const Vec4& o) const { return { x - o.x, y - o.y, z - o.z, w - o.w }; }
|
||||
Vec4 operator*(float scalar) const { return { x * scalar, y * scalar, z * scalar, w * scalar }; }
|
||||
};
|
||||
|
||||
|
||||
// Standalone Interpolation Helpers
|
||||
inline Vec2 Vec2Lerp(const Vec2& s, const Vec2& e, float a) { return { s.x + (e.x - s.x) * a, s.y + (e.y - s.y) * a }; }
|
||||
inline Vec3 Vec3Lerp(const Vec3& s, const Vec3& e, float a) { return { s.x + (e.x - s.x) * a, s.y + (e.y - s.y) * a, s.z + (e.z - s.z) * a }; }
|
||||
inline Vec4 Vec4Lerp(const Vec4& s, const Vec4& e, float a) { return { s.x + (e.x - s.x) * a, s.y + (e.y - s.y) * a, s.z + (e.z - s.z) * a, s.w + (e.w - s.w) * a }; }
|
||||
|
||||
|
||||
|
||||
|
||||
enum class VariantType { Null, Float, Vector2, Vector3, Vector4, String };
|
||||
|
||||
class EngineVariant{
|
||||
|
||||
public:
|
||||
using InternalVariant = std::variant<std::monostate,float,Vec2,Vec3,Vec4,std::string>;
|
||||
|
||||
EngineVariant() : data(std::monostate{}), type(VariantType::Null){}
|
||||
EngineVariant(float v) : data(v), type(VariantType::Float){}
|
||||
EngineVariant(Vec2 v) : data(v), type(VariantType::Vector2){}
|
||||
EngineVariant(Vec3 v) : data(v), type(VariantType::Vector3){}
|
||||
EngineVariant(Vec4 v) : data(v), type(VariantType::Vector4){}
|
||||
EngineVariant(std::string v) : data(v), type(VariantType::String){}
|
||||
|
||||
VariantType GetType() const { return type; }
|
||||
|
||||
template<typename T>
|
||||
T Get() const {
|
||||
std::lock_guard<std::mutex> lock(v_mutex);
|
||||
return std::get<T>(data);
|
||||
}
|
||||
|
||||
void Print() const {
|
||||
|
||||
std::lock_guard<std::mutex> lock(v_mutex);
|
||||
std::cout << "[Variant " << TypeToString(type) << "]: ";
|
||||
std::visit([](auto &&arg){
|
||||
using T = std::decay_t<decltype(arg)>;
|
||||
if constexpr (std::is_same_v<T, std::monostate>) std::cout << "Null";
|
||||
else if constexpr (std::is_same_v<T, float>) std::cout << arg;
|
||||
else if constexpr (std::is_same_v<T, Vec2>) std::cout << "(" << arg.x << ", " << arg.y << ")";
|
||||
else if constexpr (std::is_same_v<T, Vec3>) std::cout << "(" << arg.x << ", " << arg.y << ", " << arg.z << ")";
|
||||
else if constexpr (std::is_same_v<T, Vec4>) std::cout << "(" << arg.x << ", " << arg.y << ", " << arg.z << ", " << arg.w << ")";
|
||||
else if constexpr (std::is_same_v<T, std::string>) std::cout << "\"" << arg << "\"";
|
||||
|
||||
},data);
|
||||
|
||||
std::cout << "\n";
|
||||
}
|
||||
|
||||
private:
|
||||
InternalVariant data;
|
||||
VariantType type;
|
||||
mutable std::mutex v_mutex;
|
||||
|
||||
static std::string TypeToString(VariantType t) {
|
||||
switch(t) {
|
||||
case VariantType::Float: return "Float";
|
||||
case VariantType::Vector2: return "Vector2";
|
||||
case VariantType::Vector3: return "Vector3";
|
||||
case VariantType::Vector4: return "Vector4";
|
||||
case VariantType::String: return "String";
|
||||
default: return "Null";
|
||||
}
|
||||
}
|
||||
|
||||
friend class VariantOpManager;
|
||||
};
|
||||
@@ -0,0 +1,92 @@
|
||||
#include "bindings.hpp"
|
||||
#include "components.hpp"
|
||||
|
||||
using namespace entt::literals;
|
||||
|
||||
void varicle::register_all_component_properties(PropertyDatabase &db) {
|
||||
|
||||
//----------------------------------------------------------
|
||||
// LocalTransform2D
|
||||
//----------------------------------------------------------
|
||||
db.register_property(
|
||||
"position"_hs,
|
||||
PropertyRegistry::bind_field("position"_hs,
|
||||
&varicle::LocalTransform2D::position));
|
||||
|
||||
db.register_property("scale"_hs,
|
||||
PropertyRegistry::bind_field(
|
||||
"scale"_hs, &varicle::LocalTransform2D::scale));
|
||||
|
||||
db.register_property(
|
||||
"rotation"_hs,
|
||||
PropertyRegistry::bind_field("rotation"_hs,
|
||||
&varicle::LocalTransform2D::rotation));
|
||||
|
||||
//----------------------------------------------------------
|
||||
// GlobalTransform2D
|
||||
//----------------------------------------------------------
|
||||
db.register_property(
|
||||
"global_position"_hs,
|
||||
PropertyRegistry::bind_field("global_position"_hs,
|
||||
&varicle::GlobalTransform2D::position));
|
||||
|
||||
db.register_property("global_scale"_hs,
|
||||
PropertyRegistry::bind_field(
|
||||
"global_scale"_hs, &varicle::GlobalTransform2D::scale));
|
||||
|
||||
db.register_property(
|
||||
"global_rotation"_hs,
|
||||
PropertyRegistry::bind_field("global_rotation"_hs,
|
||||
&varicle::GlobalTransform2D::rotation));
|
||||
|
||||
//----------------------------------------------------------
|
||||
// Velocity
|
||||
//----------------------------------------------------------
|
||||
|
||||
db.register_property(
|
||||
"velocity:x"_hs,
|
||||
PropertyRegistry::bind_field("velocity:x"_hs, &varicle::Velocity::dx));
|
||||
|
||||
db.register_property(
|
||||
"velocity:y"_hs,
|
||||
PropertyRegistry::bind_field("velocity:y"_hs, &varicle::Velocity::dy));
|
||||
|
||||
//----------------------------------------------------------
|
||||
// Sprite
|
||||
//----------------------------------------------------------
|
||||
|
||||
db.register_property(
|
||||
"sprite:texture_path"_hs,
|
||||
PropertyRegistry::bind_field("sprite:texture_path"_hs, &varicle::Sprite::texture_path));
|
||||
|
||||
db.register_property(
|
||||
"sprite:offset:y"_hs,
|
||||
PropertyRegistry::bind_field("sprite:offset:y"_hs, &varicle::Sprite::offset_y));
|
||||
|
||||
db.register_property(
|
||||
"sprite:offset:x"_hs,
|
||||
PropertyRegistry::bind_field("sprite:offset:x"_hs, &varicle::Sprite::offset_x));
|
||||
|
||||
|
||||
db.register_property(
|
||||
"sprite:width"_hs,
|
||||
PropertyRegistry::bind_field("sprite:width"_hs, &varicle::Sprite::width));
|
||||
|
||||
|
||||
db.register_property(
|
||||
"sprite:height"_hs,
|
||||
PropertyRegistry::bind_field("sprite:height"_hs, &varicle::Sprite::height));
|
||||
|
||||
db.register_property(
|
||||
"sprite:flip_h"_hs,
|
||||
PropertyRegistry::bind_field("sprite:flip_h"_hs, &varicle::Sprite::flip_h));
|
||||
|
||||
db.register_property(
|
||||
"sprite:flip_v"_hs,
|
||||
PropertyRegistry::bind_field("sprite:flip_v"_hs, &varicle::Sprite::flip_v));
|
||||
|
||||
db.register_property(
|
||||
"sprite:rotation"_hs,
|
||||
PropertyRegistry::bind_field("sprite:rotation"_hs, &varicle::Sprite::rotation));
|
||||
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
#pragma once
|
||||
|
||||
#include "engine/core/engine-variant/engine-variant-property.hpp"
|
||||
|
||||
namespace varicle {
|
||||
// Single point of initialization for all engine component properties
|
||||
void register_all_component_properties(PropertyDatabase& db);
|
||||
}
|
||||
@@ -1,28 +1,43 @@
|
||||
#pragma once
|
||||
|
||||
#include "engine/core/engine-variant/engine-variant.hpp"
|
||||
#include <entt/entt.hpp>
|
||||
#include <raylib.h>
|
||||
|
||||
namespace varicle{
|
||||
namespace varicle {
|
||||
|
||||
struct Position{
|
||||
float x;
|
||||
float y;
|
||||
};
|
||||
struct Position {
|
||||
float x;
|
||||
float y;
|
||||
};
|
||||
|
||||
struct Velocity{
|
||||
float dx;
|
||||
float dy;
|
||||
};
|
||||
struct Velocity {
|
||||
float dx;
|
||||
float dy;
|
||||
};
|
||||
|
||||
struct Sprite{
|
||||
Texture *texture;
|
||||
float offset_x;
|
||||
float offset_y;
|
||||
float width;
|
||||
float height;
|
||||
bool flip_h;
|
||||
bool flip_v;
|
||||
float rotation;
|
||||
};
|
||||
struct LocalTransform2D {
|
||||
Vec2 position;
|
||||
float scale;
|
||||
float rotation;
|
||||
};
|
||||
|
||||
}
|
||||
struct GlobalTransform2D {
|
||||
Vec2 position;
|
||||
float scale;
|
||||
float rotation;
|
||||
};
|
||||
|
||||
struct Sprite {
|
||||
std::string texture_path;
|
||||
float offset_x;
|
||||
float offset_y;
|
||||
float width;
|
||||
float height;
|
||||
bool flip_h;
|
||||
bool flip_v;
|
||||
float rotation;
|
||||
};
|
||||
|
||||
|
||||
} // namespace varicle
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
#include "engine/render/render-system.hpp"
|
||||
#include "engine/asset/raylib-asset.hpp"
|
||||
#include "engine/core/service-locator.hpp"
|
||||
#include "engine/ecs/components.hpp"
|
||||
|
||||
#include<raylib.h>
|
||||
@@ -7,40 +9,45 @@
|
||||
namespace varicle {
|
||||
|
||||
void update_render_system(entt::registry ®istry){
|
||||
auto view = registry.view<const Sprite, const Position>();
|
||||
auto view = registry.view<const Sprite, const GlobalTransform2D>();
|
||||
|
||||
for (entt::entity entity : view){
|
||||
const Sprite &sprite = view.get<Sprite>(entity);
|
||||
const Position &pos = view.get<Position>(entity);
|
||||
auto [global_position,global_scale,global_rotation] = view.get<GlobalTransform2D>(entity);
|
||||
auto texture = ServiceLocator::get<RaylibAssetLoader>().get_texture(sprite.texture_path);
|
||||
|
||||
if (sprite.texture){
|
||||
float width = sprite.width * global_scale;
|
||||
float height = sprite.height * global_scale;
|
||||
float rotation = sprite.rotation + global_rotation;
|
||||
|
||||
if (texture){
|
||||
DrawTexturePro(
|
||||
*(sprite.texture),
|
||||
*(texture),
|
||||
Rectangle{
|
||||
0,
|
||||
0,
|
||||
(float)sprite.texture->width,
|
||||
(float)sprite.texture->height,
|
||||
(float)texture->width,
|
||||
(float)texture->height,
|
||||
},
|
||||
Rectangle {
|
||||
sprite.offset_x + pos.x,
|
||||
sprite.offset_y + pos.y,
|
||||
sprite.width,
|
||||
sprite.height
|
||||
sprite.offset_x + global_position.x,
|
||||
sprite.offset_y + global_position.y,
|
||||
width,
|
||||
height,
|
||||
},
|
||||
Vector2 {sprite.width * 0.5f, sprite.height*0.5f},
|
||||
sprite.rotation,
|
||||
Vector2 {width * 0.5f, height*0.5f},
|
||||
rotation,
|
||||
WHITE);
|
||||
|
||||
}else{
|
||||
DrawRectanglePro(Rectangle {
|
||||
sprite.offset_x + pos.x,
|
||||
sprite.offset_y + pos.y,
|
||||
sprite.width,
|
||||
sprite.height
|
||||
sprite.offset_x + global_position.x,
|
||||
sprite.offset_y + global_position.y,
|
||||
width,
|
||||
height
|
||||
},
|
||||
Vector2 {sprite.width * 0.5f, sprite.height*0.5f},
|
||||
sprite.rotation,
|
||||
Vector2 {width * 0.5f, height*0.5f},
|
||||
rotation,
|
||||
PURPLE
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,9 +1,13 @@
|
||||
#include "engine/core/application.hpp"
|
||||
#include "engine/core/engine-variant/engine-variant-property.hpp"
|
||||
#include "engine/core/engine-variant/engine-variant.hpp"
|
||||
#include "engine/core/service-locator.hpp"
|
||||
#include "engine/ecs/components.hpp"
|
||||
#include "engine/scene/scene.hpp"
|
||||
|
||||
#include "game/scenes/menu-scene/menu.hpp"
|
||||
|
||||
#include <entt/entt.hpp>
|
||||
#include <raylib.h>
|
||||
|
||||
class Game : public varicle::Application {
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
#include "menu.hpp"
|
||||
|
||||
#include "engine/asset/raylib-asset.hpp"
|
||||
#include "engine/core/engine-variant/engine-variant-property.hpp"
|
||||
#include "engine/core/engine-variant/engine-variant.hpp"
|
||||
#include "engine/core/service-locator.hpp"
|
||||
#include "engine/ecs/components.hpp"
|
||||
#include "engine/render/render-system.hpp"
|
||||
@@ -9,25 +11,42 @@
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
|
||||
using namespace entt::literals;
|
||||
|
||||
void MenuScene::init() {
|
||||
using namespace varicle;
|
||||
ServiceLocator::provide(std::make_unique<RaylibAssetLoader>());
|
||||
|
||||
|
||||
auto& asset_loader =ServiceLocator::get<RaylibAssetLoader>(); asset_loader.load_asset("assets/bird.png");
|
||||
auto &asset_loader = ServiceLocator::get<RaylibAssetLoader>();
|
||||
asset_loader.load_asset("assets/bird.png");
|
||||
auto x = asset_loader.get_reader().get_asset_list();
|
||||
for (auto i :x) {
|
||||
for (auto i : x) {
|
||||
std::cout << i << std::endl;
|
||||
}
|
||||
|
||||
auto e = registry.create();
|
||||
registry.emplace<Position>(e,650.0f,150.0f);
|
||||
auto bird_texture = asset_loader.get_texture("assets/bird.png");
|
||||
registry.emplace<Sprite>(e,bird_texture,0.0f,0.f,100.f,100.f,false,false,0.f);
|
||||
auto img = LoadImageFromTexture(*bird_texture);
|
||||
registry.emplace<varicle::GlobalTransform2D>(e, 650.0f, 150.0f, 1.0f,0.0f);
|
||||
// registry.emplace<Position>(e, 650.0f, 150.0f);
|
||||
registry.emplace<Sprite>(e, "assets/bird.png", 0.0f, 0.f, 100.f, 100.f,
|
||||
false, false, 0.f);
|
||||
auto img =
|
||||
LoadImageFromTexture(*asset_loader.get_texture("assets/bird.png"));
|
||||
SetWindowIcon(img);
|
||||
UnloadImage(img);
|
||||
|
||||
auto &p_database = varicle::ServiceLocator::get<PropertyDatabase>();
|
||||
|
||||
registry.emplace<varicle::LocalTransform2D>(e, 100.0f, 100.0f, 10.0f,
|
||||
5.0f);
|
||||
|
||||
EngineVariant current_pos =
|
||||
p_database.get_value(registry, e, "global_position"_hs);
|
||||
EngineVariant current_scale =
|
||||
p_database.get_value(registry, e, "scale"_hs);
|
||||
EngineVariant current_rot =
|
||||
p_database.get_value(registry, e, "rotation"_hs);
|
||||
|
||||
std::cout << current_pos << current_scale << current_rot << std::endl;
|
||||
}
|
||||
|
||||
void MenuScene::update(float dt) {}
|
||||
|
||||
Reference in New Issue
Block a user