seperate out tick and render update calls

This commit is contained in:
Green Sky 2024-01-07 14:37:33 +01:00
parent 96bab0200f
commit 75e7c5e3c7
No known key found for this signature in database
5 changed files with 56 additions and 10 deletions

View File

@ -21,6 +21,7 @@
#define LINUX
#endif
#include <limits>
#include <iostream>
#include <cassert>
@ -60,6 +61,7 @@ Plugin::Plugin(const char* path) {
_fn_start = loadSymbol("solana_plugin_start");
_fn_stop = loadSymbol("solana_plugin_stop");
_fn_tick = loadSymbol("solana_plugin_tick");
_fn_render = loadSymbol("solana_plugin_render"); // optional
if (!_fn_name || !_fn_version || !_fn_start || !_fn_stop || !_fn_tick) {
std::cerr << "PLG '" << path << "' misses functions\n";
@ -127,8 +129,18 @@ void Plugin::stop(void) const {
}
// tick function
void Plugin::tick(float delta) const {
float Plugin::tick(float delta) const {
assert(valid_plugin);
reinterpret_cast<decltype(&solana_plugin_tick)>(_fn_tick)(delta);
return reinterpret_cast<decltype(&solana_plugin_tick)>(_fn_tick)(delta);
}
// render function
float Plugin::render(float delta) const {
assert(valid_plugin);
if (_fn_render != nullptr) {
return reinterpret_cast<decltype(&solana_plugin_tick)>(_fn_tick)(delta);
} else {
return std::numeric_limits<float>::max();
}
}

View File

@ -19,6 +19,7 @@ struct Plugin {
void* _fn_start = nullptr;
void* _fn_stop = nullptr;
void* _fn_tick = nullptr;
void* _fn_render = nullptr;
void* loadSymbol(const char* name);
@ -37,8 +38,9 @@ struct Plugin {
// stop function
void stop(void) const;
// tick function
void tick(float delta) const;
// update functions
float tick(float delta) const;
float render(float delta) const;
operator bool(void) const {
return valid_plugin;

View File

@ -1,5 +1,7 @@
#include "./plugin_manager.hpp"
#include <limits>
#include <iostream>
// def
@ -45,9 +47,29 @@ bool PluginManager::add(const std::string& plug_path) {
return true;
}
void PluginManager::tick(float delta) {
float PluginManager::tick(float delta) {
float min_interval {std::numeric_limits<float>::max()};
for (const auto& p : _plugins) {
p.tick(delta);
const float plug_interval = p.tick(delta);
if (plug_interval < min_interval) {
min_interval = plug_interval;
}
}
return min_interval;
}
float PluginManager::render(float delta) {
float min_interval {std::numeric_limits<float>::max()};
for (const auto& p : _plugins) {
const float plug_interval = p.render(delta);
if (plug_interval < min_interval) {
min_interval = plug_interval;
}
}
return min_interval;
}

View File

@ -39,6 +39,8 @@ struct PluginManager {
bool add(const std::string& plug_path);
void tick(float delta);
// returns the minimum time until next call is wanted in seconds
float tick(float delta);
float render(float delta);
};

View File

@ -3,7 +3,7 @@
#include <stdint.h>
#define SOLANA_PLUGIN_VERSION 6
#define SOLANA_PLUGIN_VERSION 7
#if defined(_MSC_VER) || defined(__MINGW32__)
#define SOLANA_PLUGIN_EXPORT __declspec(dllexport)
@ -15,6 +15,7 @@
#if defined(SOLANA_PLUGIN_HOST)
#define SOLANA_PLUGIN_DECL
// TODO: this looks like decl should be used, but it isnt
#else
#define SOLANA_PLUGIN_DECL SOLANA_PLUGIN_EXPORT
#endif
@ -34,7 +35,9 @@ struct SolanaAPI {
// ---------- info ----------
// TODO: change to exported struct, so we dont have to execute this code
SOLANA_PLUGIN_EXPORT const char* solana_plugin_get_name(void);
// TODO: add author and other attributes
// get the SOLANA_PLUGIN_VERSION the plugin was compiled with
SOLANA_PLUGIN_EXPORT uint32_t solana_plugin_get_version(void);
@ -46,9 +49,14 @@ SOLANA_PLUGIN_EXPORT uint32_t solana_plugin_start(struct SolanaAPI* solana_api);
SOLANA_PLUGIN_EXPORT void solana_plugin_stop(void);
// called periodically
SOLANA_PLUGIN_EXPORT void solana_plugin_tick(float delta);
// ---------- called periodically ----------
// rendering needs to be called in a different interval AND needs to be garantied from mainthread
// the functions return the minimum time in seconds until the update should be called next
// for compute tasks
SOLANA_PLUGIN_EXPORT float solana_plugin_tick(float delta);
// on frame rendering
SOLANA_PLUGIN_EXPORT float solana_plugin_render(float delta);
#ifdef __cplusplus
}