Converted a couple more things to shared pointers.

This commit is contained in:
2023-11-10 20:12:18 -06:00
parent b1088eeab7
commit aaa8573312
16 changed files with 114 additions and 79 deletions

View File

@ -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<DawnGame> 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<RenderTarget> 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<DawnGame> game) = 0;
/**
* Perform a synchronous frame update on the render manager.

View File

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

View File

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

32
src/dawn/event/Event.hpp Normal file
View File

@ -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<typename ...T>
class Event {
private:
std::hashmap<int32_t, std::function<void(T...)>> callback;
int32_t nextEventId = 0;
public:
int32_t listen(const std::function <void(T...)> &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...);
}
}
};
}

View File

@ -7,9 +7,9 @@
using namespace Dawn;
DawnGame::DawnGame(DawnHost *host) :
DawnGame::DawnGame(const std::weak_ptr<DawnHost> 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);

View File

@ -22,13 +22,13 @@
namespace Dawn {
class DawnHost;
class DawnGame {
class DawnGame : public std::enable_shared_from_this<DawnGame> {
private:
Scene *sceneToCutTo = nullptr;
bool_t closeRequested = false;
public:
DawnHost *host;
const std::weak_ptr<DawnHost> 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<DawnHost> host);
/**
* Initialize the game. This is performed by the host at a time that is

View File

@ -29,10 +29,10 @@ namespace Dawn {
*/
class DawnHostData;
class DawnHost {
class DawnHost : std::enable_shared_from_this<DawnHost> {
public:
DawnHostData *data;
DawnGame *game;
std::shared_ptr<DawnHostData> data;
std::shared_ptr<DawnGame> 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<DawnGame> 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<DawnGame> 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<DawnGame> 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<DawnGame> 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();
};

View File

@ -51,8 +51,8 @@ glm::mat4 Camera::getProjection() {
return this->projection;
}
RenderTarget * Camera::getRenderTarget() {
return (RenderTarget*)this->renderTarget;
std::shared_ptr<RenderTarget> Camera::getRenderTarget() {
return this->renderTarget;
}
float_t Camera::getAspect() {

View File

@ -29,7 +29,7 @@ namespace Dawn {
}
// @optional
StateProperty<RenderTarget*> renderTarget;
StateProperty<std::shared_ptr<RenderTarget>> renderTarget;
// @optional
StateProperty<float_t> fov;
// @optional
@ -70,7 +70,7 @@ namespace Dawn {
*
* @return The target render target framebuffer.
*/
RenderTarget * getRenderTarget();
std::shared_ptr<RenderTarget> getRenderTarget();
/**
* Returs the aspect ratio of the camera.

View File

@ -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<TextureRenderTarget>(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)();
}

View File

@ -12,7 +12,7 @@ namespace Dawn {
public:
// @optional
StateProperty<Camera*> camera;
TextureRenderTarget renderTarget;
std::shared_ptr<TextureRenderTarget> renderTarget;
CameraTexture(SceneItem *item);
void onStart() override;

View File

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

View File

@ -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<DawnHost> DAWN_HOST;
// Host
DawnHost::DawnHost() {
this->data = new DawnHostData();
this->data = std::make_shared<DawnHostData>();
this->data->window = nullptr;
}
int32_t DawnHost::init(DawnGame *game) {
int32_t DawnHost::init(std::shared_ptr<DawnGame> 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<DawnGame> 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<DawnGame> 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<DawnGame> 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>(
(float_t)(xpos / DAWN_HOST->game->renderManager.backBuffer.getWidth()),
host->game->inputManager.rawInputValues[INPUT_MANAGER_AXIS_MOUSE_X] = mathClamp<float_t>(
(float_t)(xpos / host->game->renderManager.backBuffer->getWidth()),
0, 1
);
DAWN_HOST->game->inputManager.rawInputValues[INPUT_MANAGER_AXIS_MOUSE_Y] = mathClamp<float_t>(
(float_t)(ypos / DAWN_HOST->game->renderManager.backBuffer.getHeight()),
host->game->inputManager.rawInputValues[INPUT_MANAGER_AXIS_MOUSE_Y] = mathClamp<float_t>(
(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;
}

View File

@ -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<DawnHost>();
auto game = std::make_shared<DawnGame>(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();

View File

@ -9,14 +9,16 @@
using namespace Dawn;
RenderManager::RenderManager(DawnGame *game) :
IRenderManager(game),
backBuffer(*this),
RenderManager::RenderManager() :
IRenderManager(),
renderPipeline(this)
{
backBuffer = std::make_shared<BackBufferRenderTarget>(*this);
}
void RenderManager::init() {
void RenderManager::init(std::weak_ptr<DawnGame> game) {
this->game = game;
// Lock the common shaders
lockSimpleTextured = shaderManager.lockShader<SimpleTexturedShader>();
simpleTexturedShader = shaderManager.getShader<SimpleTexturedShader>(
@ -47,8 +49,8 @@ void RenderManager::init() {
assertNoGLError();
}
RenderTarget * RenderManager::getBackBuffer() {
return &backBuffer;
std::shared_ptr<RenderTarget> 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) {

View File

@ -22,7 +22,7 @@ namespace Dawn {
shaderlock_t lockFontShader = -1;
public:
BackBufferRenderTarget backBuffer;
std::shared_ptr<BackBufferRenderTarget> 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<RenderTarget> 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<DawnGame> game) override;
void update() override;
/**