split engine code into modules
This commit is contained in:
parent
8fd6cee223
commit
a288d0961f
152
src/Engine.cpp
152
src/Engine.cpp
@ -1,23 +1,27 @@
|
||||
#include "Engine.hpp"
|
||||
#include "gui/gui.hpp"
|
||||
#include "common/UsefulException.hpp"
|
||||
#include "common/ougge_format.hpp"
|
||||
#include "common/time.hpp"
|
||||
#include <SDL2/SDL.h>
|
||||
#include <iostream>
|
||||
|
||||
namespace ougge {
|
||||
|
||||
Engine::Engine(u32 game_object_pool_size)
|
||||
: gameObjectPool(game_object_pool_size), textures(&resourceManager)
|
||||
EngineModule::EngineModule(Engine& engine, const std::string& name)
|
||||
: engine(engine), name(name)
|
||||
{}
|
||||
|
||||
void EngineModule::beginFrame() {}
|
||||
void EngineModule::endFrame() {}
|
||||
|
||||
|
||||
|
||||
void Engine::addModule(EngineModule* m){
|
||||
modules.push_back(m);
|
||||
}
|
||||
|
||||
void Engine::startLoop()
|
||||
{
|
||||
engineManagedAssembly = mono.loadAssembly("Ougge.dll");
|
||||
gameObjectClass = engineManagedAssembly->getClass("Ougge", "GameObject");
|
||||
gameObjectCtor = Mono::Method<void(u64, u32)>(gameObjectClass, ".ctor");
|
||||
gameObjectInvokeUpdate = Mono::Method<void(f64)>(gameObjectClass, "InvokeUpdate");
|
||||
gameObjectTryCreateComponent = Mono::Method<Mono::Bool(Mono::String)>(gameObjectClass, "TryCreateComponent_internal");
|
||||
}
|
||||
|
||||
void Engine::openMainWindow(const std::string& window_title){
|
||||
mainWindow.open(window_title, resourceManager);
|
||||
}
|
||||
|
||||
void Engine::startLoop(){
|
||||
if(loop_running)
|
||||
throw UsefulException("loop is running already");
|
||||
|
||||
@ -25,87 +29,83 @@ void Engine::startLoop(){
|
||||
loop_running=true;
|
||||
// main loop
|
||||
while(loop_running){
|
||||
mainWindow.pollEvents(&loop_running);
|
||||
if(!loop_running)
|
||||
break;
|
||||
|
||||
nsec_t update_time_ns = getMonotonicTimeNsec();
|
||||
if(update_time_ns < prev_update_time_ns)
|
||||
throw UsefulException("monotonic clock returned unexpected value");
|
||||
f64 delta_time_s = (f64)(update_time_ns - prev_update_time_ns) / 1e9;
|
||||
prev_update_time_ns = update_time_ns;
|
||||
|
||||
tryDrawFrame(delta_time_s);
|
||||
deltaTime = delta_time_s;
|
||||
|
||||
tryDrawFrame();
|
||||
|
||||
nsec_t after_update_time_ns = getMonotonicTimeNsec();
|
||||
nsec_t frame_delay_ns = (nsec_t)1e9 / mainWindow.fps_max - (after_update_time_ns - update_time_ns);
|
||||
nsec_t frame_delay_ns = (nsec_t)1e9 / fps_max - (after_update_time_ns - update_time_ns);
|
||||
if(frame_delay_ns > 0){
|
||||
SDL_Delay(frame_delay_ns / 1e6);
|
||||
}
|
||||
}
|
||||
|
||||
mainWindow.close();
|
||||
}
|
||||
|
||||
void Engine::stopLoop(){
|
||||
loop_running = false;
|
||||
}
|
||||
|
||||
void Engine::tryDrawFrame(f64 deltaTime){
|
||||
static std::string error_message;
|
||||
static bool draw_error_window = false;
|
||||
try {
|
||||
mainWindow.beginFrame();
|
||||
|
||||
if(draw_error_window)
|
||||
gui::drawErrorWindow(error_message, &draw_error_window);
|
||||
else {
|
||||
updateGameObjects(deltaTime);
|
||||
if(!updateCallback.isNull())
|
||||
updateCallback(deltaTime);
|
||||
void Engine::handleModuleError(EngineModule* module, const char* type, const char* method, const char* error){
|
||||
std::string error_message = ougge_format(
|
||||
"Catched %s at %s.%s(): %s",
|
||||
type, module->name.c_str(), method, error);
|
||||
std::cerr<<error_message<<std::endl;
|
||||
error_messages.push_back(error_message);
|
||||
}
|
||||
|
||||
void Engine::tryDrawFrame(){
|
||||
auto it = modules.begin();
|
||||
while(it != modules.end())
|
||||
{
|
||||
EngineModule* module = *it;
|
||||
it++;
|
||||
try {
|
||||
module->beginFrame();
|
||||
}
|
||||
catch(const std::exception& e){
|
||||
handleModuleError(module, "exception", "beginFrame", e.what());
|
||||
}
|
||||
catch(const char* cstr){
|
||||
handleModuleError(module, "error message (const char*)", "beginFrame", cstr);
|
||||
}
|
||||
catch(const std::string& str){
|
||||
handleModuleError(module, "error message (std::string)", "beginFrame", str.c_str());
|
||||
}
|
||||
catch(...){
|
||||
handleModuleError(module, "unknown", "beginFrame", "unknown");
|
||||
}
|
||||
mainWindow.endFrame();
|
||||
}
|
||||
catch(const std::exception& e){
|
||||
error_message = "Catched exception: " + std::string(e.what());
|
||||
draw_error_window = true;
|
||||
std::cerr<<error_message<<std::endl;
|
||||
}
|
||||
catch(const char* cstr){
|
||||
error_message = "Catched error message (const char*): " + std::string(cstr);
|
||||
draw_error_window = true;
|
||||
std::cerr<<error_message<<std::endl;
|
||||
}
|
||||
catch(const std::string& str){
|
||||
error_message = "Catched error message (std::string): " + str;
|
||||
draw_error_window = true;
|
||||
std::cerr<<error_message<<std::endl;
|
||||
}
|
||||
catch(...){
|
||||
error_message = "Catched unknown error";
|
||||
draw_error_window = true;
|
||||
std::cerr<<error_message<<std::endl;
|
||||
|
||||
//TODO: frame expeption display
|
||||
// if(draw_error_window)
|
||||
// gui::drawErrorWindow(error_message, &draw_error_window);
|
||||
|
||||
it = modules.end();
|
||||
while(it != modules.begin())
|
||||
{
|
||||
it--;
|
||||
EngineModule* module = *it;
|
||||
try {
|
||||
module->endFrame();
|
||||
}
|
||||
catch(const std::exception& e){
|
||||
handleModuleError(module, "exception", "endFrame", e.what());
|
||||
}
|
||||
catch(const char* cstr){
|
||||
handleModuleError(module, "error message (const char*)", "endFrame", cstr);
|
||||
}
|
||||
catch(const std::string& str){
|
||||
handleModuleError(module, "error message (std::string)", "endFrame", str.c_str());
|
||||
}
|
||||
catch(...){
|
||||
handleModuleError(module, "unknown", "endFrame", "unknown");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void Engine::updateGameObjects(f64 deltaTime){
|
||||
for(auto pair : gameObjectPool){
|
||||
gameObjectInvokeUpdate(pair.second.getObjectHandle().getObject(), deltaTime);
|
||||
}
|
||||
}
|
||||
|
||||
game::GameObject& Engine::createGameObject(){
|
||||
auto pair = gameObjectPool.emplace(game::GameObject(mono.createObject(gameObjectClass)));
|
||||
game::GameObject& obj = pair.second;
|
||||
gameObjectCtor(obj.getObjectHandle().getObject(), ++obj_id, pair.first);
|
||||
return obj;
|
||||
}
|
||||
|
||||
bool Engine::tryCreateComponent(game::GameObject& obj, const std::string& componentClassName){
|
||||
Mono::String componentClassNameManaged = mono.createString(componentClassName);
|
||||
Mono::Bool created = gameObjectTryCreateComponent(obj.getObjectHandle().getObject(), componentClassNameManaged);
|
||||
return created.wide_bool;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -1,49 +1,56 @@
|
||||
#pragma once
|
||||
|
||||
#include "common/function_shared_ptr.hpp"
|
||||
#include "mono/mono.hpp"
|
||||
#include "game/GameObjectPool.hpp"
|
||||
#include "gui/MainWindow.hpp"
|
||||
#include "resources/textures.hpp"
|
||||
#include <vector>
|
||||
#include "common/std.hpp"
|
||||
#include "common/UsefulException.hpp"
|
||||
#include "common/ougge_format.hpp"
|
||||
|
||||
namespace ougge {
|
||||
|
||||
using UpdateFunc_t = function_shared_ptr<void(f64)>;
|
||||
class Engine;
|
||||
|
||||
class EngineModule {
|
||||
public:
|
||||
EngineModule() = delete;
|
||||
EngineModule(const EngineModule&) = delete;
|
||||
EngineModule& operator=(const EngineModule&) = delete;
|
||||
EngineModule(EngineModule&&) = delete;
|
||||
EngineModule& operator=(EngineModule&&) = delete;
|
||||
|
||||
Engine& engine;
|
||||
const std::string name;
|
||||
|
||||
virtual void beginFrame();
|
||||
virtual void endFrame();
|
||||
|
||||
protected:
|
||||
EngineModule(Engine& engine, const std::string& name);
|
||||
};
|
||||
|
||||
|
||||
class Engine {
|
||||
std::vector<EngineModule*> modules;
|
||||
bool loop_running = false;
|
||||
|
||||
game::GameObjectPool gameObjectPool;
|
||||
u64 obj_id = 0;
|
||||
MonoClass* gameObjectClass;
|
||||
Mono::Method<void(u64, u32)> gameObjectCtor;
|
||||
Mono::Method<void(f64)> gameObjectInvokeUpdate;
|
||||
Mono::Method<Mono::Bool(Mono::String)> gameObjectTryCreateComponent;
|
||||
|
||||
public:
|
||||
gui::MainWindow mainWindow;
|
||||
UpdateFunc_t updateCallback;
|
||||
u32 fps_max = 60;
|
||||
f64 deltaTime;
|
||||
std::vector<std::string> error_messages;
|
||||
|
||||
Engine() = default;
|
||||
Engine(const Engine&) = delete;
|
||||
Engine& operator=(const Engine&) = delete;
|
||||
Engine(Engine&&) = delete;
|
||||
Engine& operator=(Engine&&) = delete;
|
||||
|
||||
Mono::RuntimeJIT mono;
|
||||
std::shared_ptr<Mono::Assembly> engineManagedAssembly;
|
||||
void addModule(EngineModule* m);
|
||||
|
||||
resources::ResourceManager resourceManager;
|
||||
resources::CacheStorage<resources::Texture> textures;
|
||||
|
||||
Engine(u32 game_object_pool_size = 64*1024);
|
||||
|
||||
void openMainWindow(const std::string& window_title);
|
||||
// start game loop on the current thread
|
||||
void startLoop();
|
||||
void stopLoop();
|
||||
|
||||
game::GameObject& createGameObject();
|
||||
bool tryCreateComponent(game::GameObject& obj, const std::string& componentClassName);
|
||||
|
||||
|
||||
private:
|
||||
void tryDrawFrame(f64 deltaTime);
|
||||
void updateGameObjects(f64 deltaTime);
|
||||
void tryDrawFrame();
|
||||
void handleModuleError(EngineModule *module, const char *type, const char *method, const char *error);
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@ -30,8 +30,6 @@ public:
|
||||
|
||||
bool isNull() const { return func_ptr == nullptr; }
|
||||
|
||||
//TODO: make inline
|
||||
|
||||
// ReturnT is not void
|
||||
template<typename RT = ReturnT>
|
||||
std::enable_if_t<!std::is_void<RT>::value, RT> operator()(ArgTypes... args){
|
||||
|
||||
@ -1,11 +0,0 @@
|
||||
#include "gui.hpp"
|
||||
|
||||
namespace ougge::gui {
|
||||
|
||||
void drawErrorWindow(const std::string& msg, bool* draw_error_window){
|
||||
ImGui::Begin("ERROR", draw_error_window);
|
||||
ImGui::Text("%s", msg.c_str());
|
||||
ImGui::End();
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,11 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL.h>
|
||||
#include <imgui.h>
|
||||
#include "gui_exceptions.hpp"
|
||||
|
||||
namespace ougge::gui {
|
||||
|
||||
void drawErrorWindow(const std::string& msg, bool* draw_error_window);
|
||||
|
||||
}
|
||||
71
src/main.cpp
71
src/main.cpp
@ -1,46 +1,67 @@
|
||||
#define SDL_MAIN_HANDLED
|
||||
#include <iostream>
|
||||
#include <iomanip>
|
||||
#include "common/UsefulException.hpp"
|
||||
#include "common/ougge_format.hpp"
|
||||
#include "Engine.hpp"
|
||||
#include "gui/gui.hpp"
|
||||
#include "modules/MainWindowSDL2.hpp"
|
||||
#include "modules/MonoGameObjectSystem.hpp"
|
||||
#include "resources/textures.hpp"
|
||||
|
||||
using namespace ougge;
|
||||
|
||||
void drawTutel(Engine& engine){
|
||||
resources::Texture* tutel = engine.textures.tryGetOrCreate("tutel.png", engine.mainWindow.sdl_renderer);
|
||||
if(tutel == nullptr){
|
||||
throw new UsefulException("couldn't find resource 'tutel.png'");
|
||||
class TutelModule : public EngineModule {
|
||||
resources::CacheStorage<resources::Texture> textures;
|
||||
modules::MainWindowSDL2& mainWindow;
|
||||
public:
|
||||
TutelModule(Engine& engine, resources::ResourceManager& resourceManager, modules::MainWindowSDL2& mainWindow) :
|
||||
EngineModule(engine, nameof(TutelModule)),
|
||||
textures(&resourceManager),
|
||||
mainWindow(mainWindow)
|
||||
{
|
||||
}
|
||||
resources::SDL_RenderCopyExF_Params params;
|
||||
params.target_section = SDL_FRectConstruct(100, 100, 400, 400);
|
||||
static i32 si = 1;
|
||||
params.rotation_angle = M_PI_4 * (si++ / engine.mainWindow.fps_max);
|
||||
tutel->render(params);
|
||||
}
|
||||
|
||||
virtual void beginFrame(){
|
||||
resources::Texture* tutel = textures.tryGetOrCreate("tutel.png", mainWindow.sdl_renderer);
|
||||
if(tutel == nullptr){
|
||||
throw new UsefulException("couldn't find resource 'tutel.png'");
|
||||
}
|
||||
resources::SDL_RenderCopyExF_Params params;
|
||||
params.target_section = SDL_FRectConstruct(100, 100, 400, 400);
|
||||
static i32 si = 1;
|
||||
params.rotation_angle = M_PI_4 * (si++ / engine.fps_max);
|
||||
tutel->render(params);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
int main(int argc, const char** argv){
|
||||
try {
|
||||
std::cout<<"initializing game engine... ";
|
||||
std::cout<<"initializing ResourceManager";
|
||||
resources::ResourceManager resourceManager;
|
||||
|
||||
std::cout<<"initializing Engine";
|
||||
Engine engine;
|
||||
std::cout<<"done"<<std::endl;
|
||||
|
||||
game::GameObject& exampleObj = engine.createGameObject();
|
||||
std::cout<<"initializing MainWindowSDL2"<<std::endl;
|
||||
modules::MainWindowSDL2 mainWindow (engine, "ougge", resourceManager);
|
||||
engine.addModule(&mainWindow);
|
||||
|
||||
std::cout<<"initializing MonoGameObjectSystem"<<std::endl;
|
||||
modules::MonoGameObjectSystem monoSystem (engine, 64*1024);
|
||||
engine.addModule(&monoSystem);
|
||||
|
||||
std::cout<<"createing ExampleObject"<<std::endl;
|
||||
game::GameObject& exampleObj = monoSystem.createGameObject();
|
||||
std::string componentClassName = "Ougge.ExampleComponent";
|
||||
if(!engine.tryCreateComponent(exampleObj, componentClassName))
|
||||
if(!monoSystem.tryCreateComponent(exampleObj, componentClassName))
|
||||
throw UsefulException(ougge_format("couldn't create component '%s'", componentClassName.c_str()));
|
||||
|
||||
std::cout<<"created ExampleObject"<<std::endl;
|
||||
std::cout<<"initializing TutelModule"<<std::endl;
|
||||
TutelModule tutel (engine, resourceManager, mainWindow);
|
||||
engine.addModule(&tutel);
|
||||
|
||||
engine.updateCallback = [&engine](f64 deltaTime) -> void {
|
||||
drawTutel(engine);
|
||||
};
|
||||
|
||||
engine.openMainWindow("ougge");
|
||||
std::cout<<"created sdl window"<<std::endl;
|
||||
std::cout<<"main loop start"<<std::endl;
|
||||
engine.startLoop();
|
||||
std::cout<<"sdl window has been cosed"<<std::endl;
|
||||
std::cout<<"main loop stop"<<std::endl;
|
||||
}
|
||||
catch(const std::exception& e){
|
||||
std::cerr<<"Catched exception: "<<e.what()<<std::endl;
|
||||
|
||||
@ -1,14 +1,16 @@
|
||||
#include <backends/imgui_impl_sdl2.h>
|
||||
#include <backends/imgui_impl_sdlrenderer2.h>
|
||||
#include <iostream>
|
||||
#include "MainWindow.hpp"
|
||||
#include "gui_exceptions.hpp"
|
||||
#include "MainWindowSDL2.hpp"
|
||||
#include "../gui/gui_exceptions.hpp"
|
||||
#include "../common/ougge_format.hpp"
|
||||
#include "../common/math.hpp"
|
||||
|
||||
namespace ougge::gui {
|
||||
using namespace ougge::gui;
|
||||
|
||||
f32 MainWindow::getDPI(){
|
||||
namespace ougge::modules {
|
||||
|
||||
f32 MainWindowSDL2::getDPI(){
|
||||
i32 w=0, h=0;
|
||||
SDL_GetRendererOutputSize(sdl_renderer, &w, &h);
|
||||
i32 sim_w=0, sim_h=0;
|
||||
@ -19,7 +21,11 @@ f32 MainWindow::getDPI(){
|
||||
return dpi;
|
||||
}
|
||||
|
||||
void MainWindow::open(const std::string& window_title, resources::ResourceManager& resourceManager){
|
||||
MainWindowSDL2::MainWindowSDL2(Engine& engine,
|
||||
const std::string& window_title,
|
||||
resources::ResourceManager& resourceManager)
|
||||
: EngineModule(engine, nameof(MainWindowSDL2))
|
||||
{
|
||||
SDL_TRY(SDL_Init(SDL_INIT_EVERYTHING));
|
||||
SDL_version v;
|
||||
SDL_GetVersion(&v);
|
||||
@ -78,7 +84,7 @@ void MainWindow::open(const std::string& window_title, resources::ResourceManage
|
||||
io.FontDefault = resources::ImFont_LoadFromResource(font_res, default_font_size, dpi);
|
||||
}
|
||||
|
||||
void MainWindow::close(){
|
||||
MainWindowSDL2::~MainWindowSDL2(){
|
||||
ImGui_ImplSDLRenderer2_Shutdown();
|
||||
ImGui_ImplSDL2_Shutdown();
|
||||
ImGui::DestroyContext();
|
||||
@ -87,20 +93,22 @@ void MainWindow::close(){
|
||||
SDL_Quit();
|
||||
}
|
||||
|
||||
void MainWindow::pollEvents(bool* loopRunning){
|
||||
void MainWindowSDL2::pollEvents(){
|
||||
SDL_Event event;
|
||||
while (SDL_PollEvent(&event)) {
|
||||
ImGui_ImplSDL2_ProcessEvent(&event);
|
||||
switch(event.type){
|
||||
case SDL_QUIT: {
|
||||
*loopRunning = false;
|
||||
//TODO: log SDL_QUIT event
|
||||
engine.stopLoop();
|
||||
break;
|
||||
}
|
||||
case SDL_WINDOWEVENT: {
|
||||
if(event.window.event == SDL_WINDOWEVENT_CLOSE
|
||||
&& event.window.windowID == SDL_GetWindowID(sdl_window))
|
||||
{
|
||||
*loopRunning = false;
|
||||
//TODO: log SDL_WINDOWEVENT event
|
||||
engine.stopLoop();
|
||||
}
|
||||
break;
|
||||
}
|
||||
@ -108,8 +116,10 @@ void MainWindow::pollEvents(bool* loopRunning){
|
||||
}
|
||||
}
|
||||
|
||||
void MainWindowSDL2::beginFrame(){
|
||||
// process events happend since previous frame
|
||||
pollEvents();
|
||||
|
||||
void MainWindow::beginFrame(){
|
||||
// Start the Dear ImGui frame
|
||||
ImGui_ImplSDLRenderer2_NewFrame();
|
||||
ImGui_ImplSDL2_NewFrame();
|
||||
@ -126,16 +136,17 @@ void MainWindow::beginFrame(){
|
||||
|
||||
draw_bg_window();
|
||||
draw_debug_window();
|
||||
drawErrorWindow();
|
||||
}
|
||||
|
||||
void MainWindow::endFrame(){
|
||||
void MainWindowSDL2::endFrame(){
|
||||
ImGui::Render();
|
||||
ImGui_ImplSDLRenderer2_RenderDrawData(ImGui::GetDrawData(), sdl_renderer);
|
||||
// Swap buffers
|
||||
SDL_RenderPresent(sdl_renderer);
|
||||
}
|
||||
|
||||
void MainWindow::draw_bg_window(){
|
||||
void MainWindowSDL2::draw_bg_window(){
|
||||
const ImGuiDockNodeFlags dockspace_flags =
|
||||
ImGuiDockNodeFlags_PassthruCentralNode;
|
||||
const ImGuiWindowFlags window_flags =
|
||||
@ -199,11 +210,11 @@ void MainWindow::draw_bg_window(){
|
||||
ImGui::End();
|
||||
}
|
||||
|
||||
void MainWindow::draw_debug_window(){
|
||||
void MainWindowSDL2::draw_debug_window(){
|
||||
ImGuiIO& io = ImGui::GetIO();
|
||||
if(ImGui::Begin("Debug Options", &show_debug_window)){
|
||||
ImGui::ColorEdit3("clear_color", (float*)&clear_color);
|
||||
ImGui::InputInt("fps_max", &fps_max);
|
||||
ImGui::InputInt("fps_max", (int*)&engine.fps_max);
|
||||
ImGui::Text("Application average %.3f ms/frame (%.2f FPS)", 1000.0f / io.Framerate, io.Framerate);
|
||||
ImGui::Checkbox("Demo Window", &show_demo_window);
|
||||
ImGui::Checkbox("Metrics/Debug Window", &show_metrics_window);
|
||||
@ -217,4 +228,16 @@ void MainWindow::draw_debug_window(){
|
||||
ImGui::ShowMetricsWindow(&show_metrics_window);
|
||||
}
|
||||
|
||||
void MainWindowSDL2::drawErrorWindow(){
|
||||
if(engine.error_messages.size() < 1)
|
||||
return;
|
||||
ImGui::Begin("ERRORS");
|
||||
if(ImGui::Button("Clear"))
|
||||
engine.error_messages.clear();
|
||||
for(auto& msg : engine.error_messages){
|
||||
ImGui::TextWrapped("%s", msg.c_str());
|
||||
}
|
||||
ImGui::End();
|
||||
}
|
||||
|
||||
}
|
||||
@ -2,23 +2,23 @@
|
||||
|
||||
#include <SDL.h>
|
||||
#include <imgui.h>
|
||||
#include <vector>
|
||||
#include "../common/std.hpp"
|
||||
#include "../common/time.hpp"
|
||||
#include "../resources/resources.hpp"
|
||||
#include "../resources/fonts.hpp"
|
||||
#include "../Engine.hpp"
|
||||
|
||||
namespace ougge::modules {
|
||||
|
||||
/// converts hex color to float vector
|
||||
#define RGBAHexToF(R8,G8,B8,A8) ImVec4(((u8)35)/255.0f, ((u8)35)/255.0f, ((u8)50)/255.0f, ((u8)255)/255.0f)
|
||||
/// converts float vector to hex color
|
||||
#define RGBAFToHex(VEC4) {(u8)(VEC4.x*255), (u8)(VEC4.y*255), (u8)(VEC4.z*255), (u8)(VEC4.w*255)}
|
||||
|
||||
namespace ougge::gui {
|
||||
|
||||
#define default_font_path "fonts/DroidSans.ttf"
|
||||
|
||||
class MainWindow {
|
||||
class MainWindowSDL2 : public EngineModule {
|
||||
public:
|
||||
i32 fps_max = 60;
|
||||
f32 default_font_size = 14.0f;
|
||||
ImVec4 clear_color = RGBAHexToF(35,35,50,255);
|
||||
SDL_Window* sdl_window = nullptr;
|
||||
@ -30,18 +30,19 @@ private:
|
||||
bool show_metrics_window = false;
|
||||
|
||||
public:
|
||||
void open(const std::string& window_title, resources::ResourceManager& resourceManager);
|
||||
void close();
|
||||
MainWindowSDL2(Engine& engine, const std::string& window_title,
|
||||
resources::ResourceManager& resourceManager);
|
||||
~MainWindowSDL2();
|
||||
|
||||
/// process io events happened since previous frame
|
||||
void pollEvents(bool* loopRunning);
|
||||
void beginFrame();
|
||||
void endFrame();
|
||||
virtual void beginFrame();
|
||||
virtual void endFrame();
|
||||
|
||||
f32 getDPI();
|
||||
private:
|
||||
void pollEvents();
|
||||
void draw_debug_window();
|
||||
void draw_bg_window();
|
||||
void drawErrorWindow();
|
||||
};
|
||||
|
||||
}
|
||||
35
src/modules/MonoGameObjectSystem.cpp
Normal file
35
src/modules/MonoGameObjectSystem.cpp
Normal file
@ -0,0 +1,35 @@
|
||||
#include "MonoGameObjectSystem.hpp"
|
||||
|
||||
namespace ougge::modules {
|
||||
|
||||
MonoGameObjectSystem::MonoGameObjectSystem(Engine& engine, u32 max_game_objects) :
|
||||
EngineModule(engine, nameof(MonoGameObjectSystem)),
|
||||
gameObjectPool(max_game_objects)
|
||||
{
|
||||
engineManagedAssembly = mono.loadAssembly("Ougge.dll");
|
||||
gameObjectClass = engineManagedAssembly->getClass("Ougge", "GameObject");
|
||||
gameObjectCtor = Mono::Method<void(u64, u32)>(gameObjectClass, ".ctor");
|
||||
gameObjectInvokeUpdate = Mono::Method<void(f64)>(gameObjectClass, "InvokeUpdate");
|
||||
gameObjectTryCreateComponent = Mono::Method<Mono::Bool(Mono::String)>(gameObjectClass, "TryCreateComponent_internal");
|
||||
}
|
||||
|
||||
void MonoGameObjectSystem::beginFrame(){
|
||||
for(auto pair : gameObjectPool){
|
||||
gameObjectInvokeUpdate(pair.second.getObjectHandle().getObject(), engine.deltaTime);
|
||||
}
|
||||
}
|
||||
|
||||
game::GameObject& MonoGameObjectSystem::createGameObject(){
|
||||
auto pair = gameObjectPool.emplace(game::GameObject(mono.createObject(gameObjectClass)));
|
||||
game::GameObject& obj = pair.second;
|
||||
gameObjectCtor(obj.getObjectHandle().getObject(), ++obj_id, pair.first);
|
||||
return obj;
|
||||
}
|
||||
|
||||
bool MonoGameObjectSystem::tryCreateComponent(game::GameObject& obj, const std::string& componentClassName){
|
||||
Mono::String componentClassNameManaged = mono.createString(componentClassName);
|
||||
Mono::Bool created = gameObjectTryCreateComponent(obj.getObjectHandle().getObject(), componentClassNameManaged);
|
||||
return created.wide_bool;
|
||||
}
|
||||
|
||||
}
|
||||
30
src/modules/MonoGameObjectSystem.hpp
Normal file
30
src/modules/MonoGameObjectSystem.hpp
Normal file
@ -0,0 +1,30 @@
|
||||
#pragma once
|
||||
|
||||
#include "../Engine.hpp"
|
||||
#include "../common/function_shared_ptr.hpp"
|
||||
#include "../mono/mono.hpp"
|
||||
#include "../game/GameObjectPool.hpp"
|
||||
|
||||
namespace ougge::modules {
|
||||
|
||||
class MonoGameObjectSystem : public EngineModule {
|
||||
Mono::RuntimeJIT mono;
|
||||
game::GameObjectPool gameObjectPool;
|
||||
u64 obj_id = 0;
|
||||
MonoClass* gameObjectClass;
|
||||
Mono::Method<void(u64, u32)> gameObjectCtor;
|
||||
Mono::Method<void(f64)> gameObjectInvokeUpdate;
|
||||
Mono::Method<Mono::Bool(Mono::String)> gameObjectTryCreateComponent;
|
||||
|
||||
public:
|
||||
std::shared_ptr<Mono::Assembly> engineManagedAssembly;
|
||||
|
||||
MonoGameObjectSystem(Engine& engine, u32 max_game_objects);
|
||||
|
||||
virtual void beginFrame();
|
||||
|
||||
game::GameObject& createGameObject();
|
||||
bool tryCreateComponent(game::GameObject& obj, const std::string& componentClassName);
|
||||
};
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user