summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJonathan Bradley <jcb@pikum.xyz>2023-12-05 13:07:57 -0500
committerJonathan Bradley <jcb@pikum.xyz>2023-12-05 13:07:57 -0500
commitc349359e71170c2fa78ac0fa22df07932ab47210 (patch)
tree74795fee5b64fe94683c6078d3b50ec5112a4707 /src
parent6e498a3781f5ccbbaa6cf5e407cf67722624760f (diff)
minor refactor for plugins
Diffstat (limited to 'src')
-rw-r--r--src/game-settings.cpp1
-rw-r--r--src/game-settings.hpp9
-rw-r--r--src/game.cpp17
-rw-r--r--src/plugins.cpp51
-rw-r--r--src/plugins.hpp16
-rw-r--r--src/window.cpp9
6 files changed, 81 insertions, 22 deletions
diff --git a/src/game-settings.cpp b/src/game-settings.cpp
index 54699dd..6f09432 100644
--- a/src/game-settings.cpp
+++ b/src/game-settings.cpp
@@ -1,4 +1,3 @@
#include "game-settings.hpp"
GameSettings pkeSettings{};
-PKEGameCallbacks pkeGameCallbacks{};
diff --git a/src/game-settings.hpp b/src/game-settings.hpp
index 480ae8e..3456725 100644
--- a/src/game-settings.hpp
+++ b/src/game-settings.hpp
@@ -33,15 +33,6 @@ struct GameSettings {
} mem;
};
-struct PKEGameCallbacks {
- void (*OnInit)() = nullptr;
- void (*OnTick)(double delta) = nullptr;
- void (*OnTeardown)() = nullptr;
- void (*OnImGuiRender)() = nullptr;
-};
-
extern GameSettings pkeSettings;
-extern PKEGameCallbacks pkeGameCallbacks;
-
#endif /* PKE_GAME_SETTINGS_HPP */
diff --git a/src/game.cpp b/src/game.cpp
index 72228bf..5d39d1b 100644
--- a/src/game.cpp
+++ b/src/game.cpp
@@ -11,6 +11,7 @@
#include "math-helpers.hpp"
#include "physics.hpp"
#include "player-input.hpp"
+#include "plugins.hpp"
#include "vendor/glm_include.hpp"
#include "window.hpp"
@@ -484,8 +485,8 @@ void Game_Tick(double delta) {
PkeInput_Tick(delta);
// TODO invoke external ticks here
- if (pkeGameCallbacks.OnTick) {
- pkeGameCallbacks.OnTick(delta);
+ if (pkePlugin.OnTick) {
+ pkePlugin.OnTick(delta);
}
EntityType_Tick_Late(delta);
@@ -502,8 +503,8 @@ void Game_Main(PKEWindowProperties windowProps) {
CreateWindow(windowProps);
PkeInput_Init();
EntityType_Init();
- if (pkeGameCallbacks.OnInit) {
- pkeGameCallbacks.OnInit();
+ if (pkePlugin.OnInit) {
+ pkePlugin.OnInit();
}
GameTimePoint lastTimePoint = pkeSettings.steadyClock.now();
@@ -593,8 +594,8 @@ void Game_Main(PKEWindowProperties windowProps) {
#ifndef NDEBUG
Pke_DebugPrint();
#endif
- if (pkeGameCallbacks.OnTeardown) {
- pkeGameCallbacks.OnTeardown();
+ if (pkePlugin.OnTeardown) {
+ pkePlugin.OnTeardown();
}
Game_Teardown();
Event_Teardown();
@@ -615,8 +616,8 @@ void Game_Init() {
for (long i = 0; i < consoleBufferCount; ++i) {
memset(consoleBuffer[i], '\0', consoleLineLength);
}
- if (!pkeGameCallbacks.OnImGuiRender) {
- pkeGameCallbacks.OnImGuiRender = Game_RecordImGui;
+ if (!pkePlugin.OnImGuiRender) {
+ pkePlugin.OnImGuiRender = Game_RecordImGui;
}
}
diff --git a/src/plugins.cpp b/src/plugins.cpp
new file mode 100644
index 0000000..aa7efd5
--- /dev/null
+++ b/src/plugins.cpp
@@ -0,0 +1,51 @@
+
+#include "macros.hpp"
+#include "plugins.hpp"
+
+#include <cstdio>
+#include <dlfcn.h>
+
+PKEPluginInterface pkePlugin{};
+
+void *loadedPlugin = nullptr;
+
+void Pke_UpdatePlugin(const PKEPluginInterface &plugin) {
+ pkePlugin.OnInit = plugin.OnInit;
+ pkePlugin.OnTick = plugin.OnTick;
+ pkePlugin.OnTeardown = plugin.OnTeardown;
+ pkePlugin.OnImGuiRender = plugin.OnImGuiRender;
+}
+
+void Pke_LoadPlugin(const char *path) {
+ if (loadedPlugin != nullptr || loadedPlugin != CAFE_BABE(void)) {
+ return;
+ }
+ void *extension = dlopen(path, RTLD_NOW);
+ char *err = dlerror();
+ if (!extension) {
+ fprintf(stderr, "Given plugin library (%s) failed to load: %s\n", path, err);
+ return;
+ }
+ PKEPluginInterface *interface = reinterpret_cast<PKEPluginInterface *>(dlsym(extension, "pkePluginInterface"));
+ err = dlerror();
+ if (err != NULL) {
+ fprintf(stderr, "Given plugin library (%s) did not contain 'pkePluginInterface': %s\n", path, err);
+ dlclose(extension);
+ return;
+ }
+ Pke_UpdatePlugin(*interface);
+ loadedPlugin = interface;
+}
+
+void Pke_UnloadPlugin() {
+ pkePlugin = {
+ .OnInit = nullptr,
+ .OnTick= nullptr,
+ .OnTeardown= nullptr,
+ .OnImGuiRender= nullptr,
+ };
+ if (loadedPlugin != nullptr && loadedPlugin != CAFE_BABE(void)) {
+ dlclose(loadedPlugin);
+ loadedPlugin = CAFE_BABE(void);
+ }
+}
diff --git a/src/plugins.hpp b/src/plugins.hpp
new file mode 100644
index 0000000..2f34c12
--- /dev/null
+++ b/src/plugins.hpp
@@ -0,0 +1,16 @@
+#ifndef PKE_PLUGINS_HPP
+#define PKE_PLUGINS_HPP
+
+struct PKEPluginInterface {
+ void (*OnInit)() = nullptr;
+ void (*OnTick)(double delta) = nullptr;
+ void (*OnTeardown)() = nullptr;
+ void (*OnImGuiRender)() = nullptr;
+};
+
+extern PKEPluginInterface pkePlugin;
+
+void Pke_LoadPlugin(const char *path);
+void Pke_UnloadPlugin();
+
+#endif /* PKE_PLUGINS_HPP */
diff --git a/src/window.cpp b/src/window.cpp
index 8d40221..f9b6ff2 100644
--- a/src/window.cpp
+++ b/src/window.cpp
@@ -1,6 +1,8 @@
#define GLFW_INCLUDE_NONE
#define GLFW_INCLUDE_VULKAN
+#include "window.hpp"
+
#include "asset-manager.hpp"
#include "camera.hpp"
#include "ecs.hpp"
@@ -9,8 +11,8 @@
#include "game-settings.hpp"
#include "helpers.hpp"
#include "memory.hpp"
+#include "plugins.hpp"
#include "static/cube.hpp"
-#include "window.hpp"
#include "window-types.hpp"
#include "glm/ext/matrix_transform.hpp"
@@ -111,7 +113,6 @@ ImplementedPipelines pkePipelines{};
/*
* ImGui
*/
-bool isImGuiRenderActive = true;
VkDescriptorPool imGuiDescriptorPool;
const std::vector<const char *> REQUIRED_EXTENSIONS = std::vector<const char *> {
@@ -2427,12 +2428,12 @@ void RecordCommandBuffer(VkCommandBuffer commandBuffer, uint32_t imageIndex) {
vkCmdDraw(commandBuffer, 3, 1, 0, 0);
// ImGui
- if (isImGuiRenderActive && pkeGameCallbacks.OnImGuiRender) {
+ if (pkePlugin.OnImGuiRender) {
ImGui_ImplVulkan_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
- pkeGameCallbacks.OnImGuiRender();
+ pkePlugin.OnImGuiRender();
ImGui::Render();
auto drawData = ImGui::GetDrawData();