Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/tumble1999/cinnabar
Browse files Browse the repository at this point in the history
  • Loading branch information
SArpnt committed Feb 18, 2021
2 parents c4592d1 + 73849e6 commit 65e493b
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 8 deletions.
15 changes: 15 additions & 0 deletions engine/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,19 @@ add_test(
NAME run
COMMAND ${NAME}
WORKING_DIRECTORY "$<TARGET_FILE_DIR:${NAME}>"
)


set(LIBNAME ce_module)

add_library(${LIBNAME}
SHARED
"./ce_module_api.cpp"
)
target_link_libraries(${LIBNAME}
${CE_LIBS}
)
include_directories(${LIBNAME}
${CMAKE_CURRENT_SOURCE_DIR}
${CE_INCLUDES}
)
9 changes: 9 additions & 0 deletions engine/ce_module_api.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* Cinnabar Module API
* Author: Cameron Trow <[email protected]>
*
* Entry point for the version of the module api for use during module development
*
*/

#include "core/module.h"
18 changes: 18 additions & 0 deletions engine/core/module.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* API FOR CREATING MODULES FOR CINNABAR ENGINE
*/

#pragma once

namespace ce {
class Module {
private:
public:
virtual void tick(float deltaTime) = 0;
};
typedef Module* init_module_t();
typedef void delete_module_t(Module*);
}
#define CE_MODULE(X) \
extern "C" ce::Module* init_module() { return new X; } \
extern "C" void delete_module(ce::Module* m) { delete m; }
3 changes: 3 additions & 0 deletions engine/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,9 @@ int main(int argc, char* argv[]) {
}
}
}

moduleManager->tickModules(time->getDeltaTime());

// Rotate cube
cubePos->roll(25.0f * time->getDeltaTime());
cubePos->yaw(50.0f * time->getDeltaTime());
Expand Down
32 changes: 26 additions & 6 deletions engine/managers/module_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,28 @@ void ce::ModuleManger::loadModules() {
}
//reset Errors
dlerror();
const char* error;
LOG_ERROR("Loading Symbols for: " + path);
module_t module = (module_t)dlsym(lib, "hello");
const char* error = dlerror();
// Get a function called "Hello"
init_module_t* init_module = (init_module_t*)dlsym(lib, "init_module");
// Handle any errors
error = dlerror();
if (error) {
LOG_ERROR(dlerror());
dlclose(lib);
continue;
}

module();

dlclose(lib);
delete_module_t* delete_module = (delete_module_t*)dlsym(lib, "delete_module");
error = dlerror();
if (error) {
LOG_ERROR(dlerror());
dlclose(lib);
continue;
}
Module* module = init_module();
m_modules.push_back({module, lib, init_module, delete_module});
//delete_module(module);
//dlclose(lib);
}
}

Expand All @@ -40,4 +50,14 @@ ce::ModuleManger::ModuleManger() {
}

ce::ModuleManger::~ModuleManger() {
for (int i = 0; i < m_modules.size(); i++) {
ModuleRef module = m_modules[i];
module.delete_module(module.module);
dlclose(module.lib);
}
}

void ce::ModuleManger::tickModules(float deltaTime) {
for (int i = 0; i < m_modules.size(); i++)
m_modules[i].module->tick(deltaTime);
}
16 changes: 14 additions & 2 deletions engine/managers/module_manager.h
Original file line number Diff line number Diff line change
@@ -1,18 +1,30 @@
#pragma once

#include <string>
#include <vector>

#include <core/module.h>

namespace ce {
typedef void (*module_t)();
class ModuleManger {

private:
struct ModuleRef {
Module* module;
void* lib;
init_module_t* inti_module;
delete_module_t* delete_module;
};

inline static const std::string MODULE_FOLDER = "modules";

std::vector<ModuleRef> m_modules;

void loadModules();

public:
ModuleManger();
~ModuleManger();

void tickModules(float deltaTime);
};
}
Binary file removed res/modules/libhello.so
Binary file not shown.
Binary file added res/modules/libsample_module.so
Binary file not shown.

0 comments on commit 65e493b

Please sign in to comment.