Converted a couple more things to shared pointers.

This commit is contained in:
2023-11-10 20:12:18 -06:00
parent 0beb1d9cb7
commit 732a90931c
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);