From 523c9996a8992aaff85b5da5ca7fddd074675a31 Mon Sep 17 00:00:00 2001 From: Martin Felis Date: Sat, 3 Sep 2016 16:18:51 +0200 Subject: [PATCH] Wrapping runtime compiled code as a module manager --- CMakeLists.txt | 4 +- src/{Scene.cc => ModuleManager.cc} | 84 +++++++++++++++---------- src/{Scene.h => ModuleManager.h} | 15 +++-- src/Renderer.cc | 11 +--- src/SceneObject.h | 18 ------ src/{SceneObject.cpp => TestModule.cpp} | 7 +-- src/main.cc | 9 +++ 7 files changed, 77 insertions(+), 71 deletions(-) rename src/{Scene.cc => ModuleManager.cc} (66%) rename src/{Scene.h => ModuleManager.h} (62%) delete mode 100644 src/SceneObject.h rename src/{SceneObject.cpp => TestModule.cpp} (55%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 9a35546..550c280 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 ) diff --git a/src/Scene.cc b/src/ModuleManager.cc similarity index 66% rename from src/Scene.cc rename to src/ModuleManager.cc index c99a7e8..2e7348c 100644 --- a/src/Scene.cc +++ b/src/ModuleManager.cc @@ -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; +} diff --git a/src/Scene.h b/src/ModuleManager.h similarity index 62% rename from src/Scene.h rename to src/ModuleManager.h index fe1c910..d3bd904 100644 --- a/src/Scene.h +++ b/src/ModuleManager.h @@ -6,17 +6,20 @@ struct IUpdateable; struct IRuntimeObjectSystem; -struct Scene : IObjectFactoryListener { +struct ModuleManager : IObjectFactoryListener { ICompilerLogger* mCompilerLogger; IRuntimeObjectSystem* mRuntimeObjectSystem; - IUpdateable* mUpdateable; - ObjectId mObjectId; + std::vector mModules; + std::vector mModuleIds; - Scene(); - virtual ~Scene(); + ModuleManager(); + virtual ~ModuleManager(); bool init(); - void OnConstructorsAdded(); void update (); + + void OnConstructorsAdded(); + bool RegisterModule(const char* name); + }; diff --git a/src/Renderer.cc b/src/Renderer.cc index 25c44b1..cb49dd8 100644 --- a/src/Renderer.cc +++ b/src/Renderer.cc @@ -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() { diff --git a/src/SceneObject.h b/src/SceneObject.h deleted file mode 100644 index 47e9224..0000000 --- a/src/SceneObject.h +++ /dev/null @@ -1,18 +0,0 @@ -#include "RuntimeCompiledCpp/RuntimeObjectSystem/ObjectInterfacePerModule.h" -#include "RuntimeCompiledCpp/RuntimeObjectSystem/IObject.h" - -#include "IUpdateable.h" -#include "InterfaceIds.h" -#include - - -class SceneObject : public TInterface -{ -public: - virtual void Update( float deltaTime ) - { - std::cout << "Runtime Object 01231 update called!\n"; - } -}; - -REGISTERCLASS(SceneObject); diff --git a/src/SceneObject.cpp b/src/TestModule.cpp similarity index 55% rename from src/SceneObject.cpp rename to src/TestModule.cpp index 0126c89..448ac0e 100644 --- a/src/SceneObject.cpp +++ b/src/TestModule.cpp @@ -5,14 +5,13 @@ #include "rcpp/InterfaceIds.h" #include - -class SceneObject : public TInterface +class TestModule : public TInterface { 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); diff --git a/src/main.cc b/src/main.cc index a39403f..5ca1168 100644 --- a/src/main.cc +++ b/src/main.cc @@ -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