Skip to content

Commit

Permalink
Feature/particle wren bindings (#127)
Browse files Browse the repository at this point in the history
* [ADDED] Wren bindings for particle module

* [ADDED] BindBitflagEnum [REMOVED] hacky enum casting for particle flags
  • Loading branch information
lyvanettinger authored Jan 8, 2025
1 parent 8855d9d commit f3f74dd
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 8 deletions.
1 change: 1 addition & 0 deletions engine/bindings/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ target_link_libraries(Bindings
PRIVATE ECS
PRIVATE Audio
PRIVATE Renderer
PRIVATE Particles
)

target_include_directories(Renderer
Expand Down
27 changes: 27 additions & 0 deletions engine/bindings/private/particles/particle_bindings.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include "particle_bindings.hpp"

#include "particle_module.hpp"
#include "utility/enum_bind.hpp"
#include "utility/wren_entity.hpp"

namespace bindings
{
void LoadEmitterPresets(ParticleModule& self)
{
self.LoadEmitterPresets();
}
void SpawnEmitter(ParticleModule& self, WrenEntity& entity, EmitterPresetID emitterPreset, uint8_t flags, glm::vec3 position = { 0.0f, 0.0f, 0.0f }, glm::vec3 velocity = { 0.0f, 0.0f, 0.0f })
{
self.SpawnEmitter(entity.entity, emitterPreset, static_cast<SpawnEmitterFlagBits>(flags), position, velocity);
}
}

void BindParticleAPI(wren::ForeignModule& module)
{
bindings::BindEnum<EmitterPresetID>(module, "EmitterPresetID");
bindings::BindBitflagEnum<SpawnEmitterFlagBits>(module, "SpawnEmitterFlagBits");

auto& wren_class = module.klass<ParticleModule>("Particles");
wren_class.funcExt<bindings::LoadEmitterPresets>("LoadEmitterPresets");
wren_class.funcExt<bindings::SpawnEmitter>("SpawnEmitter");
}
5 changes: 5 additions & 0 deletions engine/bindings/private/particles/particle_bindings.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#pragma once

#include "wren_common.hpp"

void BindParticleAPI(wren::ForeignModule& module);
33 changes: 26 additions & 7 deletions engine/bindings/private/utility/enum_bind.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,44 @@ namespace detail
template <auto v>
auto ReturnVal() { return v; }

template <typename T, size_t... Idx>
template <typename E, size_t... Idx>
void BindEnumSequence(wren::ForeignModule& module, const std::string& enumName, std::index_sequence<Idx...>)
{
constexpr auto names = magic_enum::enum_names<T>();
constexpr auto values = magic_enum::enum_values<T>();
constexpr auto names = magic_enum::enum_names<E>();
constexpr auto values = magic_enum::enum_values<E>();

auto& enumClass = module.klass<T>(enumName);
auto& enumClass = module.klass<E>(enumName);
((enumClass.template funcStaticExt<ReturnVal<values[Idx]>>(std::string(names[Idx]))), ...);
}

template <typename E, size_t... Idx>
void BindBitflagSequence(wren::ForeignModule& module, const std::string& enumName, std::index_sequence<Idx...>)
{
constexpr auto names = magic_enum::enum_names<E>();
constexpr auto values = magic_enum::enum_values<E>();

auto& enumClass = module.klass<E>(enumName);
using UnderType = std::underlying_type_t<E>;
((enumClass.template funcStaticExt<ReturnVal<static_cast<UnderType>(values[Idx])>>(std::string(names[Idx]))), ...);
}

}

namespace bindings
{
template <typename T>
template <typename E>
void BindEnum(wren::ForeignModule& module, const std::string& enumName)
{
constexpr auto enum_size = magic_enum::enum_count<T>();
constexpr auto enum_size = magic_enum::enum_count<E>();
auto index_sequence = std::make_index_sequence<enum_size>();
detail::BindEnumSequence<E>(module, enumName, index_sequence);
}

template <typename E>
void BindBitflagEnum(wren::ForeignModule& module, const std::string& enumName)
{
constexpr auto enum_size = magic_enum::enum_count<E>();
auto index_sequence = std::make_index_sequence<enum_size>();
detail::BindEnumSequence<T>(module, enumName, index_sequence);
detail::BindBitflagSequence<E>(module, enumName, index_sequence);
}
}
8 changes: 8 additions & 0 deletions engine/bindings/private/wren_bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
#include "input/action_manager.hpp"
#include "input/input_codes/keys.hpp"
#include "input/input_codes/mousebuttons.hpp"
#include "particle_module.hpp"
#include "particles/particle_bindings.hpp"
#include "renderer/animation_bindings.hpp"
#include "time_module.hpp"
#include "utility/enum_bind.hpp"
Expand Down Expand Up @@ -120,6 +122,7 @@ void BindEngineAPI(wren::ForeignModule& module)
engineAPI.func<&WrenEngine::GetModule<ECSModule>>("GetECS");
engineAPI.func<&WrenEngine::GetModule<ApplicationModule>>("GetInput");
engineAPI.func<&WrenEngine::GetModule<AudioModule>>("GetAudio");
engineAPI.func<&WrenEngine::GetModule<ParticleModule>>("GetParticles");
}

// Time Module
Expand Down Expand Up @@ -157,6 +160,11 @@ void BindEngineAPI(wren::ForeignModule& module)
BindAnimationAPI(module);
}

// Particles
{
BindParticleAPI(module);
}

// Components
{
// Name class
Expand Down
5 changes: 4 additions & 1 deletion game/game.wren
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import "engine_api.wren" for Engine, TimeModule, ECS, Entity, Vec3, Quat, Math, AnimationControlComponent, TransformComponent, Input, Keycode
import "engine_api.wren" for Engine, TimeModule, ECS, Entity, Vec3, Quat, Math, AnimationControlComponent, TransformComponent, Input, Keycode, SpawnEmitterFlagBits, EmitterPresetID

class Main {

Expand All @@ -24,6 +24,9 @@ class Main {

__player.AddAudioEmitterComponent()

var emitterFlags = SpawnEmitterFlagBits.eIsActive() | SpawnEmitterFlagBits.eSetCustomVelocity()
engine.GetParticles().SpawnEmitter(__player, EmitterPresetID.eTest(), emitterFlags, Vec3.new(0.0, 0.0, 0.0), Vec3.new(5.0, -1.0, -5.0))

var gunTransform = __gun.GetTransformComponent()
gunTransform.translation = Vec3.new(-0.4, -3.1, -1)
gunTransform.rotation = Math.ToQuat(Vec3.new(0.0, -Math.PI(), 0.0))
Expand Down

0 comments on commit f3f74dd

Please sign in to comment.