-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathengine_core.cpp
More file actions
86 lines (70 loc) · 2.01 KB
/
engine_core.cpp
File metadata and controls
86 lines (70 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// engine_core.h
#include "engine_core.h"
#include "IPlugin.h" // Include full definition of IPlugin
#include "render.h"
#include <dlfcn.h> // For dynamic loading
#include <iostream>
namespace Engine
{
typedef IPlugin* (*CreatePluginFunc)();
void EngineCore::initialize()
{
// Initialize engine systems
std::cout << "Engine initialized." << std::endl;
}
void EngineCore::run()
{
// 1. Call the dynamically linked library (librender.so)
std::cout << "Calling the dynamically linked library (Render Engine):" << std::endl;
render(); // Direct call to render function
// Load the plugin
loadPlugin("./libplugin.so");
if (plugin_)
{
// Call the plugin's onLoad method
plugin_->onLoad();
// Main loop (simplified)
std::cout << "Engine running..." << std::endl;
plugin_->execute();
// Call the plugin's onUnload method
plugin_->onUnload();
}
}
void EngineCore::shutdown()
{
// Clean up and close the plugin
if (plugin_)
{
plugin_.reset();
}
if (pluginHandle_)
{
dlclose(pluginHandle_);
pluginHandle_ = nullptr;
}
std::cout << "Engine shutdown." << std::endl;
}
void EngineCore::loadPlugin(const std::string& pluginPath)
{
pluginHandle_ = dlopen(pluginPath.c_str(), RTLD_LAZY);
if (!pluginHandle_)
{
std::cerr << "Cannot open plugin library: " << dlerror() << std::endl;
return;
}
// Reset errors
dlerror();
// Load the factory function
CreatePluginFunc createPlugin = (CreatePluginFunc)dlsym(pluginHandle_, "createPlugin");
const char* dlsymError = dlerror();
if (dlsymError)
{
std::cerr << "Cannot load symbol 'createPlugin': " << dlsymError << std::endl;
dlclose(pluginHandle_);
pluginHandle_ = nullptr;
return;
}
// Create an instance of the plugin
plugin_.reset(createPlugin());
}
} // namespace Engine