Even more shared pointers.

This commit is contained in:
2023-11-10 22:33:21 -06:00
parent aaa8573312
commit bed6e3e1f4
19 changed files with 89 additions and 80 deletions

View File

@ -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);

View File

@ -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;

View File

@ -15,7 +15,7 @@ namespace Dawn {
class ShaderManager;
class RenderTarget;
class IRenderManager {
class IRenderManager : public std::enable_shared_from_this<IRenderManager> {
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<RenderPipeline> 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<ShaderManager> getShaderManager() = 0;
/**
* Sets the render flags for the render manager to use.

View File

@ -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> 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> 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<SubSceneController>();
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>();
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<struct ShaderPassItem>::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);

View File

@ -18,22 +18,22 @@ namespace Dawn {
int_fast16_t renderId = -1;
public:
RenderManager *renderManager;
std::weak_ptr<RenderManager> 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> 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> scene);
/**
* Render a specific camera on a specific scene.

View File

@ -9,15 +9,15 @@ using namespace Dawn;
DawnGame::DawnGame(const std::weak_ptr<DawnHost> host) :
host(host),
renderManager(),
inputManager(this),
saveManager(this)
{
renderManager = std::make_shared<RenderManager>();
}
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> scene) {
if(scene == nullptr) scene = this->scene;
this->sceneToCutTo = scene;
}

View File

@ -24,15 +24,15 @@ namespace Dawn {
class DawnGame : public std::enable_shared_from_this<DawnGame> {
private:
Scene *sceneToCutTo = nullptr;
std::shared_ptr<Scene> sceneToCutTo;
bool_t closeRequested = false;
public:
const std::weak_ptr<DawnHost> host;
Scene *scene = nullptr;
std::shared_ptr<Scene> scene;
AssetManager assetManager;
TimeManager timeManager;
RenderManager renderManager;
std::shared_ptr<RenderManager> 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> 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<Scene> dawnGameGetInitialScene(DawnGame *game);
}

View File

@ -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),

View File

@ -12,5 +12,5 @@ Material::Material(SceneItem *item) : SceneItemComponent(item) {
}
ShaderManager & Material::getShaderManager() {
return *this->getGame()->renderManager.getShaderManager();
return *this->getGame()->renderManager->getShaderManager();
}

View File

@ -10,7 +10,7 @@ namespace Dawn {
class SubSceneController : public SceneItemComponent {
public:
// @optional
Scene *subScene = nullptr;
std::shared_ptr<Scene> subScene = nullptr;
// @optional
bool_t onlyUpdateUnpaused = false;

View File

@ -33,7 +33,7 @@ float_t UIBorder::getChildOffsetY() {
std::vector<struct ShaderPassItem> 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;

View File

@ -14,9 +14,12 @@ UIComponentRenderable::UIComponentRenderable(SceneItem *i) : UIComponent(i) {
std::vector<struct ShaderPassItem> 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 {};

View File

@ -28,7 +28,7 @@ float_t UIImage::getContentHeight() {
std::vector<struct ShaderPassItem> 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;

View File

@ -27,7 +27,7 @@ float_t UIMesh::getContentHeight() {
std::vector<struct ShaderPassItem> 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;

View File

@ -87,7 +87,7 @@ std::vector<struct ShaderPassItem> 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();

View File

@ -65,9 +65,9 @@ int32_t DawnHost::init(std::shared_ptr<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
@ -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>(
(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>(
(float_t)(ypos / host->game->renderManager.backBuffer->getHeight()),
(float_t)(ypos / host->game->renderManager->backBuffer->getHeight()),
0, 1
);
}

View File

@ -8,6 +8,6 @@
using namespace Dawn;
Scene * Dawn::dawnGameGetInitialScene(DawnGame *game) {
return new HelloWorldScene(game);
std::shared_ptr<Scene> Dawn::dawnGameGetInitialScene(DawnGame *game) {
return std::make_shared<HelloWorldScene>(game);
}

View File

@ -9,28 +9,29 @@
using namespace Dawn;
RenderManager::RenderManager() :
IRenderManager(),
renderPipeline(this)
{
RenderManager::RenderManager() : IRenderManager() {
renderPipeline = std::make_shared<RenderPipeline>();
shaderManager = std::make_shared<ShaderManager>();
backBuffer = std::make_shared<BackBufferRenderTarget>(*this);
}
void RenderManager::init(std::weak_ptr<DawnGame> game) {
this->game = game;
// Init the render pipeline
renderPipeline->init(std::static_pointer_cast<RenderManager>(shared_from_this()));
// Lock the common shaders
lockSimpleTextured = shaderManager.lockShader<SimpleTexturedShader>();
simpleTexturedShader = shaderManager.getShader<SimpleTexturedShader>(
lockSimpleTextured = shaderManager->lockShader<SimpleTexturedShader>();
simpleTexturedShader = shaderManager->getShader<SimpleTexturedShader>(
lockSimpleTextured
);
lockUIShaderProgram = shaderManager.lockShader<UIShader>();
uiShader = shaderManager.getShader<UIShader>(lockUIShaderProgram);
lockUIShaderProgram = shaderManager->lockShader<UIShader>();
uiShader = shaderManager->getShader<UIShader>(lockUIShaderProgram);
lockFontShader = shaderManager.lockShader<FontShader>();
fontShader = shaderManager.getShader<FontShader>(lockFontShader);
renderPipeline.init();
lockFontShader = shaderManager->lockShader<FontShader>();
fontShader = shaderManager->getShader<FontShader>(lockFontShader);
assertNoGLError();
// Prepare the initial values
@ -53,12 +54,12 @@ std::shared_ptr<RenderTarget> RenderManager::getBackBuffer() {
return backBuffer;
}
RenderPipeline * RenderManager::getRenderPipeline() {
return &renderPipeline;
std::shared_ptr<RenderPipeline> RenderManager::getRenderPipeline() {
return renderPipeline;
}
ShaderManager * RenderManager::getShaderManager() {
return &shaderManager;
std::shared_ptr<ShaderManager> RenderManager::getShaderManager() {
return shaderManager;
}
void RenderManager::setRenderFlags(flag_t flags) {
@ -84,7 +85,7 @@ void RenderManager::update() {
}
RenderManager::~RenderManager() {
shaderManager.releaseShader<SimpleTexturedShader>(lockSimpleTextured);
shaderManager.releaseShader<UIShader>(lockUIShaderProgram);
shaderManager.releaseShader<FontShader>(lockFontShader);
shaderManager->releaseShader<SimpleTexturedShader>(lockSimpleTextured);
shaderManager->releaseShader<UIShader>(lockUIShaderProgram);
shaderManager->releaseShader<FontShader>(lockFontShader);
}

View File

@ -15,8 +15,8 @@
namespace Dawn {
class RenderManager : public IRenderManager {
private:
RenderPipeline renderPipeline;
ShaderManager shaderManager;
std::shared_ptr<RenderPipeline> renderPipeline;
std::shared_ptr<ShaderManager> shaderManager;
shaderlock_t lockSimpleTextured = -1;
shaderlock_t lockUIShaderProgram = -1;
shaderlock_t lockFontShader = -1;
@ -33,8 +33,8 @@ namespace Dawn {
RenderManager();
std::shared_ptr<RenderTarget> getBackBuffer() override;
RenderPipeline * getRenderPipeline() override;
ShaderManager * getShaderManager() override;
std::shared_ptr<RenderPipeline> getRenderPipeline() override;
std::shared_ptr<ShaderManager> getShaderManager() override;
void setRenderFlags(flag_t renderFlags) override;
void init(std::weak_ptr<DawnGame> game) override;
void update() override;