implement most outstanding FFI methods

This commit is contained in:
Nick Fisher
2023-09-29 18:25:37 +08:00
parent e8ba136863
commit 4b19632129
2 changed files with 157 additions and 68 deletions

View File

@@ -5,7 +5,6 @@
#include "Log.hpp" #include "Log.hpp"
#include "ThreadPool.hpp" #include "ThreadPool.hpp"
#include <thread> #include <thread>
#include <functional> #include <functional>
@@ -21,23 +20,19 @@ public:
_t = new std::thread([this]() _t = new std::thread([this]()
{ {
while(!_stop) { while(!_stop) {
if(_rendering) {
doRender();
}
std::function<void()> task; std::function<void()> task;
{ {
std::unique_lock<std::mutex> lock(_access); std::unique_lock<std::mutex> lock(_access);
if(_tasks.empty()) { if(_tasks.empty()) {
_cond.wait_for(lock, std::chrono::duration<int, std::milli>(5)); _cond.wait_for(lock, std::chrono::duration<int, std::milli>(_frameIntervalInMilliseconds));
continue; continue;
} }
task = std::move(_tasks.front()); task = std::move(_tasks.front());
_tasks.pop_front(); _tasks.pop_front();
std::this_thread::sleep_for(
std::chrono::milliseconds(_frameIntervalInMilliseconds));
if(_rendering) {
doRender();
} }
}
task(); task();
} }); } });
} }
@@ -70,6 +65,10 @@ public:
_renderCallback(_renderCallbackOwner); _renderCallback(_renderCallbackOwner);
} }
void setFrameIntervalInMilliseconds(int frameIntervalInMilliseconds) {
_frameIntervalInMilliseconds = frameIntervalInMilliseconds;
}
template <class Rt> template <class Rt>
auto add_task(std::packaged_task<Rt()> &pt) -> std::future<Rt> auto add_task(std::packaged_task<Rt()> &pt) -> std::future<Rt>
{ {
@@ -133,21 +132,30 @@ extern "C"
fut.wait(); fut.wait();
} }
FLUTTER_PLUGIN_EXPORT void set_rendering_ffi(bool rendering) FLUTTER_PLUGIN_EXPORT void set_rendering_ffi(void *const viewer, bool rendering)
{ {
if (!_rl) if (!_rl)
{ {
Log("No render loop!"); // PANIC? Log("No render loop!"); // PANIC?
} else { }
if(rendering) { else
{
if (rendering)
{
Log("Set rendering to true"); Log("Set rendering to true");
} else { }
else
{
Log("Set rendering to false"); Log("Set rendering to false");
} }
_rl->setRendering(rendering); _rl->setRendering(rendering);
} }
} }
FLUTTER_PLUGIN_EXPORT void set_frame_interval(double frameIntervalInMilliseconds) {
_rl->setFrameIntervalInMilliseconds(frameIntervalInMilliseconds);
}
FLUTTER_PLUGIN_EXPORT void render_ffi(void *const viewer) FLUTTER_PLUGIN_EXPORT void render_ffi(void *const viewer)
{ {
std::packaged_task<void()> lambda([&]() mutable std::packaged_task<void()> lambda([&]() mutable
@@ -245,6 +253,7 @@ extern "C"
fut.wait(); fut.wait();
return fut.get(); return fut.get();
} }
FLUTTER_PLUGIN_EXPORT void remove_light_ffi(void *const viewer, EntityId entityId) FLUTTER_PLUGIN_EXPORT void remove_light_ffi(void *const viewer, EntityId entityId)
{ {
std::packaged_task<void()> lambda([&] std::packaged_task<void()> lambda([&]
@@ -252,6 +261,7 @@ extern "C"
auto fut = _rl->add_task(lambda); auto fut = _rl->add_task(lambda);
fut.wait(); fut.wait();
} }
FLUTTER_PLUGIN_EXPORT void clear_lights_ffi(void *const viewer) FLUTTER_PLUGIN_EXPORT void clear_lights_ffi(void *const viewer)
{ {
std::packaged_task<void()> lambda([&] std::packaged_task<void()> lambda([&]
@@ -302,7 +312,7 @@ extern "C"
auto fut = _rl->add_task(lambda); auto fut = _rl->add_task(lambda);
fut.wait(); fut.wait();
} }
// implementations of rest of animation functions
FLUTTER_PLUGIN_EXPORT void get_morph_target_name_ffi(void *assetManager, EntityId asset, const char *meshName, char *const outPtr, int index) FLUTTER_PLUGIN_EXPORT void get_morph_target_name_ffi(void *assetManager, EntityId asset, const char *meshName, char *const outPtr, int index)
{ {
std::packaged_task<void()> lambda([&] std::packaged_task<void()> lambda([&]
@@ -321,12 +331,50 @@ extern "C"
} }
void set_morph_target_weights_ffi( void set_morph_target_weights_ffi(
void* const assetManager, void *const assetManager,
EntityId asset, EntityId asset,
const char *const entityName, const char *const entityName,
const float *const morphData, const float *const morphData,
int numWeights int numWeights)
) { {
// TODO // TODO
} }
void play_animation_ffi(void *const assetManager, EntityId asset, int index, bool loop, bool reverse, bool replaceActive, float crossfade)
{
std::packaged_task<void()> lambda([&]
{ play_animation(assetManager, asset, index, loop, reverse, replaceActive, crossfade); });
auto fut = _rl->add_task(lambda);
fut.wait();
}
void set_animation_frame_ffi(void *const assetManager, EntityId asset, int animationIndex, int animationFrame)
{
std::packaged_task<void()> lambda([&]
{ set_animation_frame(assetManager, asset, animationIndex, animationFrame); });
auto fut = _rl->add_task(lambda);
fut.wait();
}
void stop_animation_ffi(void *const assetManager, EntityId asset, int index)
{
std::packaged_task<void()> lambda([&]
{ stop_animation(assetManager, asset, index); });
auto fut = _rl->add_task(lambda);
fut.wait();
}
int get_animation_count_ffi(void *const assetManager, EntityId asset)
{
std::packaged_task<int()> lambda([&]
{ return get_animation_count(assetManager, asset); });
auto fut = _rl->add_task(lambda);
fut.wait();
return fut.get();
}
void get_animation_name_ffi(void *const assetManager, EntityId asset, char *const outPtr, int index)
{
std::packaged_task<void()> lambda([&] {
get_animation_name(assetManager, asset, outPtr, index);
});
auto fut = _rl->add_task(lambda);
fut.wait();
}
} }

View File

@@ -5,7 +5,6 @@
#include "Log.hpp" #include "Log.hpp"
#include "ThreadPool.hpp" #include "ThreadPool.hpp"
#include <thread> #include <thread>
#include <functional> #include <functional>
@@ -21,21 +20,20 @@ public:
_t = new std::thread([this]() _t = new std::thread([this]()
{ {
while(!_stop) { while(!_stop) {
if(_rendering) {
doRender();
}
std::function<void()> task; std::function<void()> task;
{ {
std::unique_lock<std::mutex> lock(_access); std::unique_lock<std::mutex> lock(_access);
if(_tasks.empty()) { if(_tasks.empty()) {
_cond.wait_for(lock, std::chrono::duration<int, std::milli>(5)); _cond.wait_for(lock, std::chrono::duration<int, std::milli>(_frameIntervalInMilliseconds));
continue; continue;
} }
task = std::move(_tasks.front()); task = std::move(_tasks.front());
_tasks.pop_front(); _tasks.pop_front();
std::this_thread::sleep_for( // std::this_thread::sleep_for(
std::chrono::milliseconds(_frameIntervalInMilliseconds)); // std::chrono::milliseconds(_frameIntervalInMilliseconds));
if(_rendering) {
doRender();
}
} }
task(); task();
@@ -133,15 +131,20 @@ extern "C"
fut.wait(); fut.wait();
} }
FLUTTER_PLUGIN_EXPORT void set_rendering_ffi(bool rendering) FLUTTER_PLUGIN_EXPORT void set_rendering_ffi(void *const viewer, bool rendering)
{ {
if (!_rl) if (!_rl)
{ {
Log("No render loop!"); // PANIC? Log("No render loop!"); // PANIC?
} else { }
if(rendering) { else
{
if (rendering)
{
Log("Set rendering to true"); Log("Set rendering to true");
} else { }
else
{
Log("Set rendering to false"); Log("Set rendering to false");
} }
_rl->setRendering(rendering); _rl->setRendering(rendering);
@@ -302,7 +305,7 @@ extern "C"
auto fut = _rl->add_task(lambda); auto fut = _rl->add_task(lambda);
fut.wait(); fut.wait();
} }
// implementations of rest of animation functions
FLUTTER_PLUGIN_EXPORT void get_morph_target_name_ffi(void *assetManager, EntityId asset, const char *meshName, char *const outPtr, int index) FLUTTER_PLUGIN_EXPORT void get_morph_target_name_ffi(void *assetManager, EntityId asset, const char *meshName, char *const outPtr, int index)
{ {
std::packaged_task<void()> lambda([&] std::packaged_task<void()> lambda([&]
@@ -321,12 +324,50 @@ extern "C"
} }
void set_morph_target_weights_ffi( void set_morph_target_weights_ffi(
void* const assetManager, void *const assetManager,
EntityId asset, EntityId asset,
const char *const entityName, const char *const entityName,
const float *const morphData, const float *const morphData,
int numWeights int numWeights)
) { {
// TODO // TODO
} }
void play_animation_ffi(void *const assetManager, EntityId asset, int index, bool loop, bool reverse, bool replaceActive, float crossfade)
{
std::packaged_task<void()> lambda([&]
{ play_animation(assetManager, asset, index, loop, reverse, replaceActive, crossfade); });
auto fut = _rl->add_task(lambda);
fut.wait();
}
void set_animation_frame_ffi(void *const assetManager, EntityId asset, int animationIndex, int animationFrame)
{
std::packaged_task<void()> lambda([&]
{ set_animation_frame(assetManager, asset, animationIndex, animationFrame); });
auto fut = _rl->add_task(lambda);
fut.wait();
}
void stop_animation_ffi(void *const assetManager, EntityId asset, int index)
{
std::packaged_task<void()> lambda([&]
{ stop_animation(assetManager, asset, index); });
auto fut = _rl->add_task(lambda);
fut.wait();
}
int get_animation_count_ffi(void *const assetManager, EntityId asset)
{
std::packaged_task<int()> lambda([&]
{ return get_animation_count(assetManager, asset); });
auto fut = _rl->add_task(lambda);
fut.wait();
return fut.get();
}
void get_animation_name_ffi(void *const assetManager, EntityId asset, char *const outPtr, int index)
{
std::packaged_task<void()> lambda([&] {
get_animation_name(assetManager, asset, outPtr, index);
});
auto fut = _rl->add_task(lambda);
fut.wait();
}
} }