diff --git a/src/dawn/display/Color.cpp b/src/dawn/display/Color.cpp index 796aca53..b06574aa 100644 --- a/src/dawn/display/Color.cpp +++ b/src/dawn/display/Color.cpp @@ -8,7 +8,7 @@ using namespace Dawn; -struct Color Color::fromString(std::string str) { +struct Color Color::fromString(const std::string str) { // Convert to lowercase auto lower = stringToLowercase(str); diff --git a/src/dawn/display/Color.hpp b/src/dawn/display/Color.hpp index 59e6f41f..2b2bf4f6 100644 --- a/src/dawn/display/Color.hpp +++ b/src/dawn/display/Color.hpp @@ -20,7 +20,7 @@ namespace Dawn { * @param str String to parse. * @return Color parsed. */ - static struct Color fromString(std::string str); + static struct Color fromString(const std::string str); float_t r, g, b, a; diff --git a/src/dawn/display/IRenderManager.hpp b/src/dawn/display/IRenderManager.hpp index 3a2fa2de..2dd0c3a4 100644 --- a/src/dawn/display/IRenderManager.hpp +++ b/src/dawn/display/IRenderManager.hpp @@ -15,7 +15,7 @@ namespace Dawn { class ShaderManager; class RenderTarget; - class IRenderManager { + class IRenderManager : public std::enable_shared_from_this { protected: flag_t renderFlags = 0; @@ -44,14 +44,14 @@ namespace Dawn { * * @return Reference to the currently active main scene render pipeline. */ - virtual RenderPipeline * getRenderPipeline() = 0; + virtual std::shared_ptr getRenderPipeline() = 0; /** * Returns the shader manager that this render manager uses. * * @return Reference to the shader manager. */ - virtual ShaderManager * getShaderManager() = 0; + virtual std::shared_ptr getShaderManager() = 0; /** * Sets the render flags for the render manager to use. diff --git a/src/dawn/display/RenderPipeline.cpp b/src/dawn/display/RenderPipeline.cpp index e9f25a3b..92b3aa9d 100644 --- a/src/dawn/display/RenderPipeline.cpp +++ b/src/dawn/display/RenderPipeline.cpp @@ -13,32 +13,35 @@ using namespace Dawn; -RenderPipeline::RenderPipeline(RenderManager *renderManager) { - assertNotNull(renderManager, "RenderPipeline::RenderPipeline: RenderManager cannot be null"); - this->renderManager = renderManager; +RenderPipeline::RenderPipeline() { } -void RenderPipeline::init() { - this->shaderBuffer.init(); +void RenderPipeline::init(const std::weak_ptr renderManager) { + this->renderManager = renderManager; + shaderBuffer.init(); } void RenderPipeline::render() { - auto game = this->renderManager->game.lock(); + auto rm = renderManager.lock(); + assertNotNull(rm, "RenderPipeline::render: RenderManager cannot be null"); + auto game = rm->game.lock(); assertNotNull(game, "RenderPipeline::render: Game cannot be null"); if(game->scene != nullptr) { - this->renderScene(game->scene); + renderScene(game->scene); } } -void RenderPipeline::renderScene(Scene *scene) { +void RenderPipeline::renderScene(std::shared_ptr scene) { assertNotNull(scene, "RenderPipeline::renderScene: Scene cannot be null"); + auto rm = renderManager.lock(); + assertNotNull(rm, "RenderPipeline::renderScene: RenderManager cannot be null"); // Render subscenes first. auto subSceneControllers = scene->findComponents(); auto itSubScene = subSceneControllers.begin(); while(itSubScene != subSceneControllers.end()) { - Scene *subScene = (Scene *)((*itSubScene)->subScene); + auto subScene = (*itSubScene)->subScene; if(subScene == nullptr) { ++itSubScene; continue; @@ -49,12 +52,12 @@ void RenderPipeline::renderScene(Scene *scene) { continue; } - this->renderScene(subScene); + renderScene(subScene); ++itSubScene; } // Now render backbuffers. - auto backBuffer = this->renderManager->getBackBuffer(); + auto backBuffer = rm->getBackBuffer(); auto cameras = scene->findComponents(); Camera *backBufferCamera = nullptr; @@ -71,7 +74,7 @@ void RenderPipeline::renderScene(Scene *scene) { } else if(cameraTarget == backBuffer) { backBufferCamera = *it; } else { - this->renderSceneCamera(scene, *it); + renderSceneCamera(&(*scene), *it); } ++it; @@ -79,10 +82,13 @@ void RenderPipeline::renderScene(Scene *scene) { // Now render the backbuffer camera. if(backBufferCamera == nullptr) return; - this->renderSceneCamera(scene, backBufferCamera); + renderSceneCamera(&(*scene), backBufferCamera); } void RenderPipeline::renderSceneCamera(Scene *scene, Camera *camera) { + auto rm = renderManager.lock(); + assertNotNull(rm, "RenderPipeline::renderSceneCamera: RenderManager cannot be null"); + std::vector::iterator itPassItem; assertNotNull(scene, "RenderPipeline::renderSceneCamera: Scene cannot be null"); @@ -90,7 +96,7 @@ void RenderPipeline::renderSceneCamera(Scene *scene, Camera *camera) { // Create a new render ID. Long story short this is a really dirty way of // not sending parameters to shaders more than we need. - this->renderId--; + renderId--; // Get the render target. auto renderTarget = camera->getRenderTarget(); @@ -100,7 +106,7 @@ void RenderPipeline::renderSceneCamera(Scene *scene, Camera *camera) { struct RenderPipelineShaderBufferData shaderBufferData; shaderBufferData.projection = camera->getProjection(); shaderBufferData.view = camera->transform->getWorldTransform(); - this->shaderBuffer.buffer(&shaderBufferData); + shaderBuffer.buffer(&shaderBufferData); // Prepare a render context. This is just a nice way of letting renderables // know a bit about what is happening, they can choose to use this data to @@ -137,7 +143,7 @@ void RenderPipeline::renderSceneCamera(Scene *scene, Camera *camera) { &lineMesh, &lineIndex, camera, - this->renderManager->simpleTexturedShader + renderManager->simpleTexturedShader ); shaderPassItems.push_back(item); itDebugLine = scene->debugLines.erase(itDebugLine); @@ -260,7 +266,7 @@ void RenderPipeline::renderSceneCamera(Scene *scene, Camera *camera) { } // Set Render flags - this->renderManager->setRenderFlags(item.renderFlags); + rm->setRenderFlags(item.renderFlags); // Thank god that's done, now just draw the damn mesh. item.mesh->draw(item.drawMode, item.start, item.count); diff --git a/src/dawn/display/RenderPipeline.hpp b/src/dawn/display/RenderPipeline.hpp index df9f0e25..e7835858 100644 --- a/src/dawn/display/RenderPipeline.hpp +++ b/src/dawn/display/RenderPipeline.hpp @@ -18,22 +18,22 @@ namespace Dawn { int_fast16_t renderId = -1; public: - RenderManager *renderManager; + std::weak_ptr renderManager; RenderPipelineShaderBuffer shaderBuffer; /** * Constructs a new RenderPipeline. Render Pipelines are my attempt to * create both a flexible, but standard way to allow the individual games * to decide how they want to render the common scene-item models. - * - * @param renderManager Parent render manager this pipeline belongs to. */ - RenderPipeline(RenderManager *renderManager); + RenderPipeline(); /** * Initialize the render pipeline. + * + * @param renderManager Parent render manager this pipeline belongs to. */ - virtual void init(); + virtual void init(const std::weak_ptr renderManager); /** * Renders the games' currently active scene, and all of its' cameras. @@ -46,7 +46,7 @@ namespace Dawn { * * @param scene Scene to render. */ - virtual void renderScene(Scene *scene); + virtual void renderScene(std::shared_ptr scene); /** * Render a specific camera on a specific scene. diff --git a/src/dawn/game/DawnGame.cpp b/src/dawn/game/DawnGame.cpp index dd33ed33..687c9d36 100644 --- a/src/dawn/game/DawnGame.cpp +++ b/src/dawn/game/DawnGame.cpp @@ -9,15 +9,15 @@ using namespace Dawn; DawnGame::DawnGame(const std::weak_ptr host) : host(host), - renderManager(), inputManager(this), saveManager(this) { + renderManager = std::make_shared(); } int32_t DawnGame::init() { this->assetManager.init(); - this->renderManager.init(weak_from_this()); + this->renderManager->init(weak_from_this()); this->scene = dawnGameGetInitialScene(this); @@ -31,10 +31,9 @@ int32_t DawnGame::update(float_t delta) { if(this->scene != nullptr) this->scene->update(); - this->renderManager.update(); + this->renderManager->update(); if(this->sceneToCutTo != nullptr) { - delete this->scene; this->scene = nullptr; this->scene = this->sceneToCutTo; this->sceneToCutTo = nullptr; @@ -47,7 +46,7 @@ int32_t DawnGame::update(float_t delta) { return DAWN_GAME_UPDATE_RESULT_SUCCESS; } -void DawnGame::sceneCutover(Scene *scene) { +void DawnGame::sceneCutover(std::shared_ptr scene) { if(scene == nullptr) scene = this->scene; this->sceneToCutTo = scene; } diff --git a/src/dawn/game/DawnGame.hpp b/src/dawn/game/DawnGame.hpp index c530fb8c..1c240d78 100644 --- a/src/dawn/game/DawnGame.hpp +++ b/src/dawn/game/DawnGame.hpp @@ -24,15 +24,15 @@ namespace Dawn { class DawnGame : public std::enable_shared_from_this { private: - Scene *sceneToCutTo = nullptr; + std::shared_ptr sceneToCutTo; bool_t closeRequested = false; public: const std::weak_ptr host; - Scene *scene = nullptr; + std::shared_ptr scene; AssetManager assetManager; TimeManager timeManager; - RenderManager renderManager; + std::shared_ptr renderManager; InputManager inputManager; SaveManager saveManager; @@ -75,7 +75,7 @@ namespace Dawn { * * @param scene Scene to cut over to. */ - void sceneCutover(Scene *scene); + void sceneCutover(std::shared_ptr scene); /** * Gracefully requests that the game should be closed as soon as possible. @@ -90,5 +90,5 @@ namespace Dawn { * @param game Game that is requesting this scene. * @return Pointer to a scene that you wish to have as the default scene. */ - Scene * dawnGameGetInitialScene(DawnGame *game); + std::shared_ptr dawnGameGetInitialScene(DawnGame *game); } \ No newline at end of file diff --git a/src/dawn/scene/components/display/Camera.cpp b/src/dawn/scene/components/display/Camera.cpp index cdbe6ace..3efa64d0 100644 --- a/src/dawn/scene/components/display/Camera.cpp +++ b/src/dawn/scene/components/display/Camera.cpp @@ -10,7 +10,7 @@ using namespace Dawn; Camera::Camera(SceneItem *item) : SceneItemComponent(item), - renderTarget(item->scene->game->renderManager.getBackBuffer()), + renderTarget(item->scene->game->renderManager->getBackBuffer()), fov(0.785398f),// 45 degrees, type(CAMERA_TYPE_PERSPECTIVE), orthoLeft(-0.5f), diff --git a/src/dawn/scene/components/display/Material.cpp b/src/dawn/scene/components/display/Material.cpp index 78389b43..5d3e33c5 100644 --- a/src/dawn/scene/components/display/Material.cpp +++ b/src/dawn/scene/components/display/Material.cpp @@ -12,5 +12,5 @@ Material::Material(SceneItem *item) : SceneItemComponent(item) { } ShaderManager & Material::getShaderManager() { - return *this->getGame()->renderManager.getShaderManager(); + return *this->getGame()->renderManager->getShaderManager(); } \ No newline at end of file diff --git a/src/dawn/scene/components/scene/SubSceneController.hpp b/src/dawn/scene/components/scene/SubSceneController.hpp index 6301cdd0..ec7c1270 100644 --- a/src/dawn/scene/components/scene/SubSceneController.hpp +++ b/src/dawn/scene/components/scene/SubSceneController.hpp @@ -10,7 +10,7 @@ namespace Dawn { class SubSceneController : public SceneItemComponent { public: // @optional - Scene *subScene = nullptr; + std::shared_ptr subScene = nullptr; // @optional bool_t onlyUpdateUnpaused = false; diff --git a/src/dawn/scene/components/ui/UIBorder.cpp b/src/dawn/scene/components/ui/UIBorder.cpp index f9b0cd12..e3a95974 100644 --- a/src/dawn/scene/components/ui/UIBorder.cpp +++ b/src/dawn/scene/components/ui/UIBorder.cpp @@ -33,7 +33,7 @@ float_t UIBorder::getChildOffsetY() { std::vector UIBorder::getUIRenderPasses() { struct ShaderPassItem item; - auto shader = getGame()->renderManager.uiShader; + auto shader = getGame()->renderManager->uiShader; item.shader = shader; item.colorValues[shader->paramColor] = COLOR_WHITE; item.parameterBuffers[shader->bufferUiCanvas] = &this->getCanvas()->shaderBuffer; diff --git a/src/dawn/scene/components/ui/UIComponentRenderable.cpp b/src/dawn/scene/components/ui/UIComponentRenderable.cpp index c8783288..47440076 100644 --- a/src/dawn/scene/components/ui/UIComponentRenderable.cpp +++ b/src/dawn/scene/components/ui/UIComponentRenderable.cpp @@ -14,9 +14,12 @@ UIComponentRenderable::UIComponentRenderable(SceneItem *i) : UIComponent(i) { std::vector UIComponentRenderable::getRenderPasses( struct IRenderableContext &context ) { + auto renderManager = context.renderPipeline->renderManager.lock(); + assertNotNull(renderManager, "RenderManager cannot be null"); + if( context.camera->getRenderTarget() != - context.renderPipeline->renderManager->getBackBuffer() + renderManager->getBackBuffer() ) { auto canvas = this->getCanvas(); if(canvas->drawType == UI_DRAW_TYPE_WORLD_CAMERA_RELATIVE) return {}; diff --git a/src/dawn/scene/components/ui/UIImage.cpp b/src/dawn/scene/components/ui/UIImage.cpp index 71b36049..a4974b36 100644 --- a/src/dawn/scene/components/ui/UIImage.cpp +++ b/src/dawn/scene/components/ui/UIImage.cpp @@ -28,7 +28,7 @@ float_t UIImage::getContentHeight() { std::vector UIImage::getUIRenderPasses() { struct ShaderPassItem item; - auto shader = getGame()->renderManager.uiShader; + auto shader = getGame()->renderManager->uiShader; item.shader = shader; item.colorValues[shader->paramColor] = this->color; item.parameterBuffers[shader->bufferUiCanvas] = &getCanvas()->shaderBuffer; diff --git a/src/dawn/scene/components/ui/UIMesh.cpp b/src/dawn/scene/components/ui/UIMesh.cpp index 5cf0423c..25cf81b9 100644 --- a/src/dawn/scene/components/ui/UIMesh.cpp +++ b/src/dawn/scene/components/ui/UIMesh.cpp @@ -27,7 +27,7 @@ float_t UIMesh::getContentHeight() { std::vector UIMesh::getUIRenderPasses() { struct ShaderPassItem item; - auto shader = getGame()->renderManager.uiShader; + auto shader = getGame()->renderManager->uiShader; item.shader = shader; item.colorValues[shader->paramColor] = this->color; item.parameterBuffers[shader->bufferUiCanvas] = &getCanvas()->shaderBuffer; diff --git a/src/dawn/scene/components/ui/text/UILabel.cpp b/src/dawn/scene/components/ui/text/UILabel.cpp index a9a3bc93..539986a9 100644 --- a/src/dawn/scene/components/ui/text/UILabel.cpp +++ b/src/dawn/scene/components/ui/text/UILabel.cpp @@ -87,7 +87,7 @@ std::vector UILabel::getUIRenderPasses() { if(this->textsBuffered.empty()) return {}; auto canvas = this->getCanvas(); - auto shader = getGame()->renderManager.fontShader; + auto shader = getGame()->renderManager->fontShader; // Translate glm::mat4 model = transform->getWorldTransform(); diff --git a/src/dawnglfw/host/DawnGLFWHost.cpp b/src/dawnglfw/host/DawnGLFWHost.cpp index 3b9e5d58..728b5474 100644 --- a/src/dawnglfw/host/DawnGLFWHost.cpp +++ b/src/dawnglfw/host/DawnGLFWHost.cpp @@ -65,9 +65,9 @@ int32_t DawnHost::init(std::shared_ptr 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 @@ -184,16 +184,16 @@ void glfwOnResize(GLFWwindow *window, int32_t w, int32_t h) { if(!host) return; assertTrue(window == host->data->window, "glfwOnResize: Window mismatch"); - auto backBuffer = ((BackBufferRenderTarget*)&host->game->renderManager.backBuffer); - assertNotNull(backBuffer, "glfwOnResize: Back buffer is not valid"); + auto bb = host->game->renderManager->backBuffer; + assertNotNull(bb, "glfwOnResize: Back buffer is not valid"); int32_t windowWidth, windowHeight; glfwGetWindowSize(window, &windowWidth, &windowHeight); // TODO: I may throttle this, it calls for every frame the window's resized. - backBuffer->setSize((float_t)w, (float_t)h); - backBuffer->scale = ((float_t)w) / ((float_t)windowWidth); - assertTrue(backBuffer->scale > 0, "glfwOnResize: Back buffer scale is invalid"); + bb->setSize((float_t)w, (float_t)h); + bb->scale = ((float_t)w) / ((float_t)windowWidth); + assertTrue(bb->scale > 0, "glfwOnResize: Back buffer scale is invalid"); } void glfwOnKey( @@ -225,11 +225,11 @@ void glfwOnCursor(GLFWwindow* window, double xpos, double ypos) { if(host == nullptr) return; host->game->inputManager.rawInputValues[INPUT_MANAGER_AXIS_MOUSE_X] = mathClamp( - (float_t)(xpos / host->game->renderManager.backBuffer->getWidth()), + (float_t)(xpos / host->game->renderManager->backBuffer->getWidth()), 0, 1 ); host->game->inputManager.rawInputValues[INPUT_MANAGER_AXIS_MOUSE_Y] = mathClamp( - (float_t)(ypos / host->game->renderManager.backBuffer->getHeight()), + (float_t)(ypos / host->game->renderManager->backBuffer->getHeight()), 0, 1 ); } diff --git a/src/dawnhelloworld/game/HelloGame.cpp b/src/dawnhelloworld/game/HelloGame.cpp index 105be5d3..d402a2e6 100644 --- a/src/dawnhelloworld/game/HelloGame.cpp +++ b/src/dawnhelloworld/game/HelloGame.cpp @@ -8,6 +8,6 @@ using namespace Dawn; -Scene * Dawn::dawnGameGetInitialScene(DawnGame *game) { - return new HelloWorldScene(game); +std::shared_ptr Dawn::dawnGameGetInitialScene(DawnGame *game) { + return std::make_shared(game); } \ No newline at end of file diff --git a/src/dawnopengl/display/RenderManager.cpp b/src/dawnopengl/display/RenderManager.cpp index 7998c1f3..82f35a32 100644 --- a/src/dawnopengl/display/RenderManager.cpp +++ b/src/dawnopengl/display/RenderManager.cpp @@ -9,28 +9,29 @@ using namespace Dawn; -RenderManager::RenderManager() : - IRenderManager(), - renderPipeline(this) -{ +RenderManager::RenderManager() : IRenderManager() { + renderPipeline = std::make_shared(); + shaderManager = std::make_shared(); backBuffer = std::make_shared(*this); } void RenderManager::init(std::weak_ptr game) { this->game = game; + + // Init the render pipeline + renderPipeline->init(std::static_pointer_cast(shared_from_this())); // Lock the common shaders - lockSimpleTextured = shaderManager.lockShader(); - simpleTexturedShader = shaderManager.getShader( + lockSimpleTextured = shaderManager->lockShader(); + simpleTexturedShader = shaderManager->getShader( lockSimpleTextured ); - lockUIShaderProgram = shaderManager.lockShader(); - uiShader = shaderManager.getShader(lockUIShaderProgram); + lockUIShaderProgram = shaderManager->lockShader(); + uiShader = shaderManager->getShader(lockUIShaderProgram); - lockFontShader = shaderManager.lockShader(); - fontShader = shaderManager.getShader(lockFontShader); - renderPipeline.init(); + lockFontShader = shaderManager->lockShader(); + fontShader = shaderManager->getShader(lockFontShader); assertNoGLError(); // Prepare the initial values @@ -53,12 +54,12 @@ std::shared_ptr RenderManager::getBackBuffer() { return backBuffer; } -RenderPipeline * RenderManager::getRenderPipeline() { - return &renderPipeline; +std::shared_ptr RenderManager::getRenderPipeline() { + return renderPipeline; } -ShaderManager * RenderManager::getShaderManager() { - return &shaderManager; +std::shared_ptr RenderManager::getShaderManager() { + return shaderManager; } void RenderManager::setRenderFlags(flag_t flags) { @@ -84,7 +85,7 @@ void RenderManager::update() { } RenderManager::~RenderManager() { - shaderManager.releaseShader(lockSimpleTextured); - shaderManager.releaseShader(lockUIShaderProgram); - shaderManager.releaseShader(lockFontShader); + shaderManager->releaseShader(lockSimpleTextured); + shaderManager->releaseShader(lockUIShaderProgram); + shaderManager->releaseShader(lockFontShader); } \ No newline at end of file diff --git a/src/dawnopengl/display/RenderManager.hpp b/src/dawnopengl/display/RenderManager.hpp index 8e603f36..657adf06 100644 --- a/src/dawnopengl/display/RenderManager.hpp +++ b/src/dawnopengl/display/RenderManager.hpp @@ -15,8 +15,8 @@ namespace Dawn { class RenderManager : public IRenderManager { private: - RenderPipeline renderPipeline; - ShaderManager shaderManager; + std::shared_ptr renderPipeline; + std::shared_ptr shaderManager; shaderlock_t lockSimpleTextured = -1; shaderlock_t lockUIShaderProgram = -1; shaderlock_t lockFontShader = -1; @@ -33,8 +33,8 @@ namespace Dawn { RenderManager(); std::shared_ptr getBackBuffer() override; - RenderPipeline * getRenderPipeline() override; - ShaderManager * getShaderManager() override; + std::shared_ptr getRenderPipeline() override; + std::shared_ptr getShaderManager() override; void setRenderFlags(flag_t renderFlags) override; void init(std::weak_ptr game) override; void update() override;