From aaa85733125a59c174929a45243ddca9b54169fa Mon Sep 17 00:00:00 2001 From: Dominic Masters Date: Fri, 10 Nov 2023 20:12:18 -0600 Subject: [PATCH] Converted a couple more things to shared pointers. --- src/dawn/display/IRenderManager.hpp | 17 +++--- src/dawn/display/RenderPipeline.cpp | 9 ++-- src/dawn/display/shader/ShaderPass.hpp | 2 +- src/dawn/event/Event.hpp | 32 ++++++++++++ src/dawn/game/DawnGame.cpp | 6 +-- src/dawn/game/DawnGame.hpp | 6 +-- src/dawn/host/DawnHost.hpp | 18 ++++--- src/dawn/scene/components/display/Camera.cpp | 4 +- src/dawn/scene/components/display/Camera.hpp | 4 +- .../components/display/CameraTexture.cpp | 6 +-- .../components/display/CameraTexture.hpp | 2 +- .../scene/components/ui/menu/UISimpleMenu.cpp | 2 +- src/dawnglfw/host/DawnGLFWHost.cpp | 52 ++++++++++--------- src/dawnlinux64/host/DawnHostTux32.cpp | 7 +-- src/dawnopengl/display/RenderManager.cpp | 16 +++--- src/dawnopengl/display/RenderManager.hpp | 10 ++-- 16 files changed, 114 insertions(+), 79 deletions(-) create mode 100644 src/dawn/event/Event.hpp diff --git a/src/dawn/display/IRenderManager.hpp b/src/dawn/display/IRenderManager.hpp index 3371eeb7..3a2fa2de 100644 --- a/src/dawn/display/IRenderManager.hpp +++ b/src/dawn/display/IRenderManager.hpp @@ -5,32 +5,29 @@ #pragma once #include "util/flag.hpp" -#include "RenderTarget.hpp" #define RENDER_MANAGER_RENDER_FLAG_DEPTH_TEST FLAG_DEFINE(0) #define RENDER_MANAGER_RENDER_FLAG_BLEND FLAG_DEFINE(1) -typedef flag_t renderflag_t; namespace Dawn { class DawnGame; class RenderPipeline; class ShaderManager; + class RenderTarget; class IRenderManager { protected: - renderflag_t renderFlags = 0; + flag_t renderFlags = 0; public: - DawnGame *game; + std::weak_ptr game; /** * Default constructor for a render manager instance. * * @param game Game that this render manager belongs to. */ - IRenderManager(DawnGame *game) { - assertNotNull(game, "IRenderManager::IRenderManager: Game cannot be null"); - this->game = game; + IRenderManager() { } /** @@ -39,7 +36,7 @@ namespace Dawn { * * @return Shared pointer to the backbuffer render target. */ - virtual RenderTarget * getBackBuffer() = 0; + virtual std::shared_ptr getBackBuffer() = 0; /** * Returns the current render pipeline intended to be used for rendering @@ -61,14 +58,14 @@ namespace Dawn { * * @param renderFlags Render flags to use. */ - virtual void setRenderFlags(renderflag_t renderFlags) = 0; + virtual void setRenderFlags(flag_t renderFlags) = 0; /** * Initialize / Start the Render Manager. * * @param game Game instance this render manager belongs to. */ - virtual void init() = 0; + virtual void init(std::weak_ptr game) = 0; /** * Perform a synchronous frame update on the render manager. diff --git a/src/dawn/display/RenderPipeline.cpp b/src/dawn/display/RenderPipeline.cpp index 8e32af96..e9f25a3b 100644 --- a/src/dawn/display/RenderPipeline.cpp +++ b/src/dawn/display/RenderPipeline.cpp @@ -23,8 +23,11 @@ void RenderPipeline::init() { } void RenderPipeline::render() { - if(this->renderManager->game->scene != nullptr) { - this->renderScene(this->renderManager->game->scene); + auto game = this->renderManager->game.lock(); + assertNotNull(game, "RenderPipeline::render: Game cannot be null"); + + if(game->scene != nullptr) { + this->renderScene(game->scene); } } @@ -58,7 +61,7 @@ void RenderPipeline::renderScene(Scene *scene) { // First, render all non-backbuffer cameras. auto it = cameras.begin(); while(it != cameras.end()) { - RenderTarget *cameraTarget = (*it)->getRenderTarget(); + auto cameraTarget = (*it)->getRenderTarget(); // Leave the backbuffer camera(s) to last, so we skip them. we do this so // that children framebuffers contain the CURRENT frame, not LAST frame. diff --git a/src/dawn/display/shader/ShaderPass.hpp b/src/dawn/display/shader/ShaderPass.hpp index 6ff85a12..edc9eef0 100644 --- a/src/dawn/display/shader/ShaderPass.hpp +++ b/src/dawn/display/shader/ShaderPass.hpp @@ -19,7 +19,7 @@ namespace Dawn { int32_t start = 0; int32_t count = -1; float_t w = 0; - renderflag_t renderFlags = RENDER_MANAGER_RENDER_FLAG_DEPTH_TEST; + flag_t renderFlags = RENDER_MANAGER_RENDER_FLAG_DEPTH_TEST; enum MeshDrawMode drawMode = MESH_DRAW_MODE_TRIANGLES; // Parameters diff --git a/src/dawn/event/Event.hpp b/src/dawn/event/Event.hpp new file mode 100644 index 00000000..7a3ad324 --- /dev/null +++ b/src/dawn/event/Event.hpp @@ -0,0 +1,32 @@ +// Copyright (c) 2023 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#pragma once +#include "dawnlibs.hpp" + +namespace Dawn { + template + class Event { + private: + std::hashmap> callback; + int32_t nextEventId = 0; + + public: + int32_t listen(const std::function &callback) { + this->callback.insert(std::make_pair(this->nextEventId, callback)); + return this->nextEventId++; + } + + void unlisten(int32_t id) { + this->callback.erase(id); + } + + void trigger(T... args) { + for(auto &it : this->callback) { + it.second(args...); + } + } + }; +} \ No newline at end of file diff --git a/src/dawn/game/DawnGame.cpp b/src/dawn/game/DawnGame.cpp index f443f5ff..dd33ed33 100644 --- a/src/dawn/game/DawnGame.cpp +++ b/src/dawn/game/DawnGame.cpp @@ -7,9 +7,9 @@ using namespace Dawn; -DawnGame::DawnGame(DawnHost *host) : +DawnGame::DawnGame(const std::weak_ptr host) : host(host), - renderManager(this), + renderManager(), inputManager(this), saveManager(this) { @@ -17,7 +17,7 @@ DawnGame::DawnGame(DawnHost *host) : int32_t DawnGame::init() { this->assetManager.init(); - this->renderManager.init(); + this->renderManager.init(weak_from_this()); this->scene = dawnGameGetInitialScene(this); diff --git a/src/dawn/game/DawnGame.hpp b/src/dawn/game/DawnGame.hpp index 90c57d74..c530fb8c 100644 --- a/src/dawn/game/DawnGame.hpp +++ b/src/dawn/game/DawnGame.hpp @@ -22,13 +22,13 @@ namespace Dawn { class DawnHost; - class DawnGame { + class DawnGame : public std::enable_shared_from_this { private: Scene *sceneToCutTo = nullptr; bool_t closeRequested = false; public: - DawnHost *host; + const std::weak_ptr host; Scene *scene = nullptr; AssetManager assetManager; TimeManager timeManager; @@ -41,7 +41,7 @@ namespace Dawn { * * @param host Host that executed this game instantiation. */ - DawnGame(DawnHost *host); + DawnGame(const std::weak_ptr host); /** * Initialize the game. This is performed by the host at a time that is diff --git a/src/dawn/host/DawnHost.hpp b/src/dawn/host/DawnHost.hpp index 6bdb3b89..47e25e50 100644 --- a/src/dawn/host/DawnHost.hpp +++ b/src/dawn/host/DawnHost.hpp @@ -29,10 +29,10 @@ namespace Dawn { */ class DawnHostData; - class DawnHost { + class DawnHost : std::enable_shared_from_this { public: - DawnHostData *data; - DawnGame *game; + std::shared_ptr data; + std::shared_ptr game; /** * Construct a new DawnHost. Hosts are set by the various dawn platform @@ -50,7 +50,7 @@ namespace Dawn { * @param game Game instance that this host is running for. * @return A status code, where DAWN_HOST_INIT_RESULT_SUCCESS is success. */ - int32_t init(DawnGame *game); + int32_t init(std::shared_ptr game); /** * Request to start the main loop. This method may not exist and may not @@ -62,7 +62,7 @@ namespace Dawn { * @param game Game instance that this host is running for. * @return A status code, refer to DAWN_HOST_START_RESULT_{} definitions. */ - virtual int32_t start(DawnGame *game); + virtual int32_t start(std::shared_ptr game); /** * Requests the host to perform a main-thread update. @@ -71,7 +71,7 @@ namespace Dawn { * @param delta How much time has passed (in seconds) since the last tick. * @return A status code, refer to DAWN_HOST_UPDATE_RESULT_{} definitions. */ - int32_t update(DawnGame *game, float_t delta); + int32_t update(std::shared_ptr game, float_t delta); /** * Request the host to be unloaded. This is a bit different from dispose @@ -80,10 +80,12 @@ namespace Dawn { * * @param game Game instance that this host is running for. */ - void unload(DawnGame *game); + void unload(std::shared_ptr game); /** - * Destroy (and unload) all of the DawnHost data from memory. + * Destroy (and unload) all of the DawnHost data from memory. Currently + * not marked as virtual as I don't really want hosts being able to have + * destructors right now. */ ~DawnHost(); }; diff --git a/src/dawn/scene/components/display/Camera.cpp b/src/dawn/scene/components/display/Camera.cpp index 6241d381..cdbe6ace 100644 --- a/src/dawn/scene/components/display/Camera.cpp +++ b/src/dawn/scene/components/display/Camera.cpp @@ -51,8 +51,8 @@ glm::mat4 Camera::getProjection() { return this->projection; } -RenderTarget * Camera::getRenderTarget() { - return (RenderTarget*)this->renderTarget; +std::shared_ptr Camera::getRenderTarget() { + return this->renderTarget; } float_t Camera::getAspect() { diff --git a/src/dawn/scene/components/display/Camera.hpp b/src/dawn/scene/components/display/Camera.hpp index ab916727..3d9b3492 100644 --- a/src/dawn/scene/components/display/Camera.hpp +++ b/src/dawn/scene/components/display/Camera.hpp @@ -29,7 +29,7 @@ namespace Dawn { } // @optional - StateProperty renderTarget; + StateProperty> renderTarget; // @optional StateProperty fov; // @optional @@ -70,7 +70,7 @@ namespace Dawn { * * @return The target render target framebuffer. */ - RenderTarget * getRenderTarget(); + std::shared_ptr getRenderTarget(); /** * Returs the aspect ratio of the camera. diff --git a/src/dawn/scene/components/display/CameraTexture.cpp b/src/dawn/scene/components/display/CameraTexture.cpp index cdbf4437..f6093804 100644 --- a/src/dawn/scene/components/display/CameraTexture.cpp +++ b/src/dawn/scene/components/display/CameraTexture.cpp @@ -9,9 +9,9 @@ using namespace Dawn; CameraTexture::CameraTexture(SceneItem *item) : SceneItemComponent(item), - camera(nullptr), - renderTarget(32.0f, 32.0f) + camera(nullptr) { + renderTarget = std::make_shared(32, 32); } void CameraTexture::onStart() { @@ -19,6 +19,6 @@ void CameraTexture::onStart() { useEffect([&]{ if(this->camera == nullptr) return; - this->camera->renderTarget = &this->renderTarget; + this->camera->renderTarget = this->renderTarget; }, camera)(); } \ No newline at end of file diff --git a/src/dawn/scene/components/display/CameraTexture.hpp b/src/dawn/scene/components/display/CameraTexture.hpp index 42e01275..b29be618 100644 --- a/src/dawn/scene/components/display/CameraTexture.hpp +++ b/src/dawn/scene/components/display/CameraTexture.hpp @@ -12,7 +12,7 @@ namespace Dawn { public: // @optional StateProperty camera; - TextureRenderTarget renderTarget; + std::shared_ptr renderTarget; CameraTexture(SceneItem *item); void onStart() override; diff --git a/src/dawn/scene/components/ui/menu/UISimpleMenu.cpp b/src/dawn/scene/components/ui/menu/UISimpleMenu.cpp index 0b19890d..1d9bc105 100644 --- a/src/dawn/scene/components/ui/menu/UISimpleMenu.cpp +++ b/src/dawn/scene/components/ui/menu/UISimpleMenu.cpp @@ -80,7 +80,7 @@ void UISimpleMenu::onStart() { break; case UI_DRAW_TYPE_WORLD_CAMERA_RELATIVE: - RenderTarget *rt = canvas->camera->getRenderTarget(); + auto rt = canvas->camera->getRenderTarget(); mouse *= glm::vec2(rt->getWidth(), rt->getHeight()); ray.origin = glm::vec3(mouse, 5); ray.direction = glm::vec3(0, 0, -10); diff --git a/src/dawnglfw/host/DawnGLFWHost.cpp b/src/dawnglfw/host/DawnGLFWHost.cpp index c7d7949e..3b9e5d58 100644 --- a/src/dawnglfw/host/DawnGLFWHost.cpp +++ b/src/dawnglfw/host/DawnGLFWHost.cpp @@ -13,18 +13,18 @@ using namespace Dawn; // Static declaration of the host, needed due to GLFW events being C-like -DawnHost *DAWN_HOST = nullptr; +std::weak_ptr DAWN_HOST; // Host DawnHost::DawnHost() { - this->data = new DawnHostData(); + this->data = std::make_shared(); this->data->window = nullptr; } -int32_t DawnHost::init(DawnGame *game) { +int32_t DawnHost::init(std::shared_ptr game) { // Update values + DAWN_HOST = weak_from_this(); this->game = game; - DAWN_HOST = this; // Init GLFW if(!glfwInit()) return DAWN_GLFW_INIT_RESULT_INIT_FAILED; @@ -65,9 +65,9 @@ int32_t DawnHost::init(DawnGame *game) { assertTrue(fbWidth > 0, "Detected framebuffer height is too small?"); assertTrue(windowWidth > 0, "Detected window width is too small?"); assertTrue(windowHeight > 0, "Detected window height is too small?"); - game->renderManager.backBuffer.setSize(fbWidth, fbHeight); - game->renderManager.backBuffer.scale = ((float_t)fbWidth) / ((float_t)windowWidth); - assertTrue(game->renderManager.backBuffer.scale > 0, "Back buffer scale is invalid"); + game->renderManager.backBuffer->setSize(fbWidth, fbHeight); + game->renderManager.backBuffer->scale = ((float_t)fbWidth) / ((float_t)windowWidth); + assertTrue(game->renderManager.backBuffer->scale > 0, "Back buffer scale is invalid"); assertNoGLError(); // Default keybinds @@ -106,7 +106,7 @@ int32_t DawnHost::init(DawnGame *game) { return DAWN_HOST_INIT_RESULT_SUCCESS; } -int32_t DawnHost::start(DawnGame *game) { +int32_t DawnHost::start(std::shared_ptr game) { double_t time, newTime; float_t fDelta; int32_t updateResult; @@ -139,7 +139,7 @@ int32_t DawnHost::start(DawnGame *game) { return DAWN_HOST_START_RESULT_EXIT_SUCCESS; } -int32_t DawnHost::update(DawnGame *game, float_t delta) { +int32_t DawnHost::update(std::shared_ptr game, float_t delta) { // Tick game. auto ret = game->update(delta); switch(ret) { @@ -154,7 +154,7 @@ int32_t DawnHost::update(DawnGame *game, float_t delta) { } } -void DawnHost::unload(DawnGame *game) { +void DawnHost::unload(std::shared_ptr game) { assertNotNull(game, "DawnHost::unload: game must not be null"); assertNotNull(this->data, "DawnHost::unload: data must not be null"); if(this->data->window != nullptr) { @@ -171,8 +171,6 @@ DawnHost::~DawnHost() { this->data->window = nullptr; glfwTerminate(); } - delete this->data; - DAWN_HOST = nullptr; } @@ -182,10 +180,11 @@ void glfwOnError(int error, const char* description) { } void glfwOnResize(GLFWwindow *window, int32_t w, int32_t h) { - if(DAWN_HOST == nullptr) return; - assertTrue(window == DAWN_HOST->data->window, "glfwOnResize: Window mismatch"); + auto host = DAWN_HOST.lock(); + if(!host) return; + assertTrue(window == host->data->window, "glfwOnResize: Window mismatch"); - auto backBuffer = ((BackBufferRenderTarget*)&DAWN_HOST->game->renderManager.backBuffer); + auto backBuffer = ((BackBufferRenderTarget*)&host->game->renderManager.backBuffer); assertNotNull(backBuffer, "glfwOnResize: Back buffer is not valid"); int32_t windowWidth, windowHeight; @@ -204,7 +203,8 @@ void glfwOnKey( int32_t action, int32_t mods ) { - if(DAWN_HOST == nullptr) return; + auto host = DAWN_HOST.lock(); + if(host == nullptr) return; // Determine Value float_t value; @@ -217,26 +217,28 @@ void glfwOnKey( } // Determine the input axis - DAWN_HOST->game->inputManager.rawInputValues[key] = value; + host->game->inputManager.rawInputValues[key] = value; } void glfwOnCursor(GLFWwindow* window, double xpos, double ypos) { - if(DAWN_HOST == nullptr) return; + auto host = DAWN_HOST.lock(); + if(host == nullptr) return; - DAWN_HOST->game->inputManager.rawInputValues[INPUT_MANAGER_AXIS_MOUSE_X] = mathClamp( - (float_t)(xpos / DAWN_HOST->game->renderManager.backBuffer.getWidth()), + host->game->inputManager.rawInputValues[INPUT_MANAGER_AXIS_MOUSE_X] = mathClamp( + (float_t)(xpos / host->game->renderManager.backBuffer->getWidth()), 0, 1 ); - DAWN_HOST->game->inputManager.rawInputValues[INPUT_MANAGER_AXIS_MOUSE_Y] = mathClamp( - (float_t)(ypos / DAWN_HOST->game->renderManager.backBuffer.getHeight()), + host->game->inputManager.rawInputValues[INPUT_MANAGER_AXIS_MOUSE_Y] = mathClamp( + (float_t)(ypos / host->game->renderManager.backBuffer->getHeight()), 0, 1 ); } void glfwOnMouseButton(GLFWwindow* window, int button, int action, int mods) { - if(DAWN_HOST == nullptr) return; - assertTrue(window == DAWN_HOST->data->window, "glfwOnMouseButton: Window mismatch"); + auto host = DAWN_HOST.lock(); + if(host == nullptr) return; + assertTrue(window == host->data->window, "glfwOnMouseButton: Window mismatch"); float_t value = action == GLFW_PRESS ? 1 : 0; - DAWN_HOST->game->inputManager.rawInputValues[INPUT_MANAGER_AXIS_MOUSE_0 + button] = value; + host->game->inputManager.rawInputValues[INPUT_MANAGER_AXIS_MOUSE_0 + button] = value; } \ No newline at end of file diff --git a/src/dawnlinux64/host/DawnHostTux32.cpp b/src/dawnlinux64/host/DawnHostTux32.cpp index 5f508bbe..2e108436 100644 --- a/src/dawnlinux64/host/DawnHostTux32.cpp +++ b/src/dawnlinux64/host/DawnHostTux32.cpp @@ -15,8 +15,8 @@ int32_t main(int32_t argc, char **args) { memoryInit(); // Create the host - auto host = new DawnHost(); - auto game = new DawnGame(host); + auto host = std::make_shared(); + auto game = std::make_shared(host); // Initialize the host and error check result = host->init(game); @@ -40,9 +40,6 @@ int32_t main(int32_t argc, char **args) { // Main loop finished without errors, cleanup host->unload(game); - - delete game; - delete host; memoryDispose(); diff --git a/src/dawnopengl/display/RenderManager.cpp b/src/dawnopengl/display/RenderManager.cpp index bce3fa68..7998c1f3 100644 --- a/src/dawnopengl/display/RenderManager.cpp +++ b/src/dawnopengl/display/RenderManager.cpp @@ -9,14 +9,16 @@ using namespace Dawn; -RenderManager::RenderManager(DawnGame *game) : - IRenderManager(game), - backBuffer(*this), +RenderManager::RenderManager() : + IRenderManager(), renderPipeline(this) { + backBuffer = std::make_shared(*this); } -void RenderManager::init() { +void RenderManager::init(std::weak_ptr game) { + this->game = game; + // Lock the common shaders lockSimpleTextured = shaderManager.lockShader(); simpleTexturedShader = shaderManager.getShader( @@ -47,8 +49,8 @@ void RenderManager::init() { assertNoGLError(); } -RenderTarget * RenderManager::getBackBuffer() { - return &backBuffer; +std::shared_ptr RenderManager::getBackBuffer() { + return backBuffer; } RenderPipeline * RenderManager::getRenderPipeline() { @@ -59,7 +61,7 @@ ShaderManager * RenderManager::getShaderManager() { return &shaderManager; } -void RenderManager::setRenderFlags(renderflag_t flags) { +void RenderManager::setRenderFlags(flag_t flags) { renderFlags = flags; if((flags & RENDER_MANAGER_RENDER_FLAG_DEPTH_TEST) == 0) { diff --git a/src/dawnopengl/display/RenderManager.hpp b/src/dawnopengl/display/RenderManager.hpp index a548eff9..8e603f36 100644 --- a/src/dawnopengl/display/RenderManager.hpp +++ b/src/dawnopengl/display/RenderManager.hpp @@ -22,7 +22,7 @@ namespace Dawn { shaderlock_t lockFontShader = -1; public: - BackBufferRenderTarget backBuffer; + std::shared_ptr backBuffer; SimpleTexturedShader *simpleTexturedShader = nullptr; UIShader *uiShader = nullptr; FontShader *fontShader = nullptr; @@ -30,13 +30,13 @@ namespace Dawn { /** * Construct a new RenderManager for a game instance. */ - RenderManager(DawnGame *game); + RenderManager(); - RenderTarget * getBackBuffer() override; + std::shared_ptr getBackBuffer() override; RenderPipeline * getRenderPipeline() override; ShaderManager * getShaderManager() override; - void setRenderFlags(renderflag_t renderFlags) override; - void init() override; + void setRenderFlags(flag_t renderFlags) override; + void init(std::weak_ptr game) override; void update() override; /**