Wrapping runtime compiled code as a module manager

master
Martin Felis 2016-09-03 16:18:51 +02:00
parent 8095b4aef2
commit 523c9996a8
7 changed files with 77 additions and 71 deletions

View File

@ -65,8 +65,8 @@ SET ( protot_SRCS
src/shaderc_glsl.cpp
src/shaderc_hlsl.cpp
src/Scene.cc
src/SceneObject.cpp
src/ModuleManager.cc
src/TestModule.cpp
3rdparty/glfw/deps/glad.c
)

View File

@ -1,4 +1,4 @@
#include "Scene.h"
#include "ModuleManager.h"
// Runtime Compiled Cpp
#include "RuntimeCompiledCpp/RuntimeObjectSystem/IObjectFactorySystem.h"
@ -26,13 +26,13 @@
using namespace std;
Scene::Scene() :
ModuleManager::ModuleManager() :
mCompilerLogger(nullptr),
mRuntimeObjectSystem(nullptr),
mUpdateable(nullptr) {
mRuntimeObjectSystem(nullptr)
{
}
Scene::~Scene() {
ModuleManager::~ModuleManager() {
if (mRuntimeObjectSystem != nullptr) {
mRuntimeObjectSystem->CleanObjectFiles();
}
@ -40,16 +40,18 @@ Scene::~Scene() {
if (mRuntimeObjectSystem && mRuntimeObjectSystem->GetObjectFactorySystem()) {
mRuntimeObjectSystem->GetObjectFactorySystem()->RemoveListener(this);
// delete object via correct interface
IObject* obj = mRuntimeObjectSystem->GetObjectFactorySystem()->GetObject( mObjectId );
delete obj;
for (unsigned int i = 0; i < mModuleIds.size(); i++) {
// delete object via correct interface
IObject* obj = mRuntimeObjectSystem->GetObjectFactorySystem()->GetObject( mModuleIds[i] );
delete obj;
}
}
delete mRuntimeObjectSystem;
delete mCompilerLogger;
}
bool Scene::init() {
bool ModuleManager::init() {
mRuntimeObjectSystem = new RuntimeObjectSystem;
mCompilerLogger = new StdioLogSystem();
@ -60,48 +62,66 @@ bool Scene::init() {
mRuntimeObjectSystem->GetObjectFactorySystem()->AddListener(this);
// construct an object
IObjectConstructor* constructor = mRuntimeObjectSystem->GetObjectFactorySystem()->GetConstructor("SceneObject");
if (constructor) {
IObject* obj = constructor->Construct();
obj->GetInterface(&mUpdateable);
if (nullptr == mUpdateable) {
delete obj;
mCompilerLogger->LogError("Error - no updateable interface found!\n");
return false;
}
mObjectId = obj->GetObjectId();
}
return true;
}
void Scene::OnConstructorsAdded() {
if (mUpdateable) {
IObject* obj = mRuntimeObjectSystem->GetObjectFactorySystem()->GetObject(mObjectId);
obj->GetInterface(&mUpdateable);
void ModuleManager::OnConstructorsAdded() {
for (unsigned int i = 0; i < mModules.size(); i++) {
IUpdateable* module = mModules[i];
if (!module)
continue;
IObject* obj = mRuntimeObjectSystem->GetObjectFactorySystem()->GetObject(mModuleIds[i]);
obj->GetInterface(&mModules[i]);
if (nullptr == mUpdateable) {
if (nullptr == module) {
delete obj;
mCompilerLogger->LogError("Error - no updateable interface found!\n");
}
}
}
void Scene::update() {
void ModuleManager::update() {
if (mRuntimeObjectSystem->GetIsCompiledComplete()) {
mRuntimeObjectSystem->LoadCompiledModule();
}
if (!mRuntimeObjectSystem->GetIsCompiling()) {
static int num_updates = 0;
num_updates ++;
std::cout << "Main loop. num_updates = " << num_updates << "\n";
const float delta_time = 1.0f;
mRuntimeObjectSystem->GetFileChangeNotifier()->Update(delta_time);
mUpdateable->Update(delta_time);
usleep(1000 * 1000);
for (unsigned int i = 0; i < mModules.size(); i++) {
mModules[i]->Update(delta_time);
}
usleep(1000 * 100);
}
}
bool ModuleManager::RegisterModule(const char* name) {
cout << "Registering Module: " << name << endl;
// construct an object
IObjectConstructor* constructor = mRuntimeObjectSystem->GetObjectFactorySystem()->GetConstructor(name);
if (constructor) {
IObject* obj = constructor->Construct();
IUpdateable** module = new IUpdateable*[1];
obj->GetInterface(module);
if (nullptr == module) {
delete obj;
mCompilerLogger->LogError("Error - no updateable interface found!\n");
return false;
}
mModules.push_back(*module);
mModuleIds.push_back(obj->GetObjectId());
}
return true;
}

View File

@ -6,17 +6,20 @@
struct IUpdateable;
struct IRuntimeObjectSystem;
struct Scene : IObjectFactoryListener {
struct ModuleManager : IObjectFactoryListener {
ICompilerLogger* mCompilerLogger;
IRuntimeObjectSystem* mRuntimeObjectSystem;
IUpdateable* mUpdateable;
ObjectId mObjectId;
std::vector<IUpdateable*> mModules;
std::vector<ObjectId> mModuleIds;
Scene();
virtual ~Scene();
ModuleManager();
virtual ~ModuleManager();
bool init();
void OnConstructorsAdded();
void update ();
void OnConstructorsAdded();
bool RegisterModule(const char* name);
};

View File

@ -20,8 +20,6 @@
#include "math_types.h"
#include "Scene.h"
// BGFX globals
bgfx::VertexBufferHandle cube_vbh;
@ -465,8 +463,6 @@ class BGFXCallbacks: public bgfx::CallbackI {
};
};
Scene scene;
void Renderer::initialize(int width, int height) {
this->width = width;
this->height = height;
@ -534,8 +530,6 @@ void Renderer::initialize(int width, int height) {
initialized = true;
resize (width, height);
bgfx::frame();
scene.init();
}
void Renderer::shutdown() {
@ -600,16 +594,15 @@ void Renderer::paintGLSimple() {
const double toMs = 1000.0/freq;
// Use debug font to print information about this example.
bgfx::dbgTextClear();
bgfx::dbgTextPrintf(0, 1, 0x4f, "bgfx/examples/00-helloworld");
bgfx::dbgTextPrintf(0, 2, 0x6f, "Description: Initialization and debug text.");
bgfx::dbgTextPrintf(0, 3, 0x8f, "Frame: % 7.3f[ms]", double(frameTime)*toMs);
scene.update();
// Advance to next frame. Rendering thread will be kicked to
// process submitted rendering primitives.
bgfx::frame();
bgfx::dbgTextClear();
}
void Renderer::paintGL() {

View File

@ -1,18 +0,0 @@
#include "RuntimeCompiledCpp/RuntimeObjectSystem/ObjectInterfacePerModule.h"
#include "RuntimeCompiledCpp/RuntimeObjectSystem/IObject.h"
#include "IUpdateable.h"
#include "InterfaceIds.h"
#include <iostream>
class SceneObject : public TInterface<IID_IUPDATEABLE,IUpdateable>
{
public:
virtual void Update( float deltaTime )
{
std::cout << "Runtime Object 01231 update called!\n";
}
};
REGISTERCLASS(SceneObject);

View File

@ -5,14 +5,13 @@
#include "rcpp/InterfaceIds.h"
#include <iostream>
class SceneObject : public TInterface<IID_IUPDATEABLE,IUpdateable>
class TestModule : public TInterface<IID_IUPDATEABLE,IUpdateable>
{
public:
virtual void Update( float deltaTime )
{
std::cout << "Runtime Object 23 update called!\n";
std::cout << "TestModule: 1 Runtime Object 214 update called!\n";
}
};
REGISTERCLASS(SceneObject);
REGISTERCLASS(TestModule);

View File

@ -48,6 +48,7 @@
#include "bgfx/bgfxplatform.h"
#include "bx/timer.h"
#include "Renderer.h"
#include "ModuleManager.h"
using namespace std;
@ -153,6 +154,11 @@ int main(void)
// Set view 0 clear state.
// bgfx::setViewClear(0, BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH, 0x3070F0FF);
printf("Initializing ModuleManager...\n");
ModuleManager module_manager;
module_manager.init();
module_manager.RegisterModule("TestModule");
printf("Starting main loop...\n");
glfwSetKeyCallback(win, key_callback);
int64_t time_offset = bx::getHPCounter();
@ -173,7 +179,10 @@ int main(void)
renderer.resize(width, height);
}
module_manager.update();
renderer.paintGLSimple();
// bgfx::setViewRect(0, 0, 0, width, height);
// Dummy submit call to make sure view 0 is cleared