Fixed all my pointers

This commit is contained in:
2022-10-19 19:44:47 -07:00
parent f0cbae4cf8
commit acc9d798cb
31 changed files with 289 additions and 141 deletions

View File

@ -16,4 +16,5 @@ target_include_directories(${DAWN_TARGET_NAME}
)
# Subdirs
add_subdirectory(display)
add_subdirectory(scene)

View File

@ -0,0 +1,10 @@
# Copyright (c) 2022 Dominic Masters
#
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT
# Sources
target_sources(${DAWN_TARGET_NAME}
PRIVATE
RenderPipeline.cpp
)

View File

@ -0,0 +1,26 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "RenderPipeline.hpp"
#include "game/DawnGame.hpp"
using namespace Dawn;
RenderPipeline::RenderPipeline(RenderManager &renderManager) :
renderManager(renderManager)
{
}
void RenderPipeline::init() {
}
void RenderPipeline::render() {
this->renderScene(*this->renderManager.game.scene);
}
RenderPipeline::~RenderPipeline() {
}

View File

@ -0,0 +1,24 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "dawnlibs.hpp"
#include "display/RenderManager.hpp"
#include "scene/Scene.hpp"
#include "scene/components/Components.hpp"
namespace Dawn {
class RenderPipeline {
public:
RenderManager &renderManager;
RenderPipeline(RenderManager &renderManager);
virtual void init();
void render();
virtual void renderScene(Scene &scene) = 0;
virtual void renderSceneCamera(Scene &scene, Camera &camera) = 0;
virtual ~RenderPipeline();
};
}

View File

@ -8,10 +8,19 @@
namespace Dawn {
class DawnGame;
class RenderPipeline;
class IRenderManager {
public:
std::weak_ptr<DawnGame> game;
DawnGame &game;
std::shared_ptr<RenderPipeline> renderPipeline;
/**
* Default constructor for a render manager instance.
*
* @param game Game that this render manager belongs to.
*/
IRenderManager(DawnGame &game) : game(game) {}
/**
* Returns the primary render target (the backbuffer) that draws directly
@ -19,10 +28,14 @@ namespace Dawn {
*
* @return Shared pointer to the backbuffer render target.
*/
virtual std::shared_ptr<RenderTarget> getBackBuffer() = 0;
virtual RenderTarget & getBackBuffer() = 0;
virtual RenderPipeline & getRenderPipeline() = 0;
/**
* Initialize this render manager.
* Initialize / Start the Render Manager.
*
* @param game Game instance this render manager belongs to.
*/
virtual void init() = 0;

View File

@ -14,6 +14,6 @@ namespace Dawn {
*
* @param mesh Mesh to initialize as a triangle.
*/
static void createTriangleMesh(std::shared_ptr<Mesh> mesh);
static void createTriangleMesh(Mesh &mesh);
};
}

View File

@ -15,22 +15,17 @@
namespace Dawn {
class DawnHost;
class DawnGame :
public std::enable_shared_from_this<DawnGame>
{
private:
std::shared_ptr<Scene> scene;
class DawnGame {
public:
std::weak_ptr<DawnHost> host;
std::shared_ptr<Scene> scene;
DawnHost &host;
RenderManager renderManager;
/**
* Construct a new instance of the DawnGame.
*
* @param host Weak pointer to the host that is running this game.
*/
DawnGame(std::weak_ptr<DawnHost> host);
DawnGame(DawnHost &host);
/**
* Initialize the game. This is performed by the host at a time that is
@ -38,6 +33,7 @@ namespace Dawn {
* return an initialize result, where DAWN_GAME_INIT_RESULT_SUCCESS is
* the only "successful" result, anything else is deemed a failure state.
*
* @param host Pointer to the host that is running this game.
* @return The game initialize result.
*/
int32_t init();

View File

@ -29,7 +29,9 @@ namespace Dawn {
*/
class DawnHostData;
class DawnHost {
class DawnHost :
public std::enable_shared_from_this<DawnHost>
{
public:
std::unique_ptr<DawnHostData> data;
@ -49,7 +51,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(std::weak_ptr<DawnGame> game);
int32_t init(DawnGame &game);
/**
* Request to start the main loop. This method may not exist and may not
@ -61,7 +63,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(std::weak_ptr<DawnGame> game);
virtual int32_t start(DawnGame &game);
/**
* Requests the host to perform a main-thread update.
@ -70,7 +72,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(std::weak_ptr<DawnGame> game, float_t delta);
int32_t update(DawnGame &game, float_t delta);
/**
* Request the host to be unloaded. This is a bit different from dispose
@ -79,7 +81,7 @@ namespace Dawn {
*
* @param game Game instance that this host is running for.
*/
void unload(std::weak_ptr<DawnGame> game);
void unload(DawnGame &game);
/**
* Destroy (and unload) all of the DawnHost data from memory.

View File

@ -8,8 +8,7 @@
using namespace Dawn;
Scene::Scene(std::weak_ptr<DawnGame> game) {
this->game = game;
Scene::Scene(DawnGame &game) : game(game) {
this->nextId = 0;
}
@ -30,7 +29,7 @@ void Scene::update() {
std::shared_ptr<SceneItem> Scene::createSceneItem() {
sceneitemid_t id = this->nextId++;
auto item = std::make_shared<SceneItem>(weak_from_this(), id);
auto item = std::make_shared<SceneItem>(*this, id);
this->itemsNotInitialized[id] = item;
return item;
}

View File

@ -9,23 +9,21 @@
namespace Dawn {
class DawnGame;
class Scene :
public std::enable_shared_from_this<Scene>
{
class Scene {
private:
sceneitemid_t nextId;
std::map<sceneitemid_t,std::shared_ptr<SceneItem>> items;
std::map<sceneitemid_t,std::shared_ptr<SceneItem>> itemsNotInitialized;
std::map<sceneitemid_t, std::shared_ptr<SceneItem>> items;
std::map<sceneitemid_t, std::shared_ptr<SceneItem>> itemsNotInitialized;
public:
std::weak_ptr<DawnGame> game;
DawnGame &game;
/**
* Construct a new Scene instance.
*
* @param game Weak pointer to the game that this scene belongs to.
* @param game Reference to the game that this scene belongs to.
*/
Scene(std::weak_ptr<DawnGame> game);
Scene(DawnGame &game);
/**
* Perform a one frame synchronous tick on the current scene. This may
@ -59,6 +57,25 @@ namespace Dawn {
return nullptr;
}
/**
* Finds all exisitng components on the scene (Root Level Only) that has a
* matching component of the given component type.
*
* @tparam Component type to look for.
* @return List of matching compnoents.
*/
template<class T>
std::vector<std::shared_ptr<T>> findComponents() {
std::vector<std::shared_ptr<T>> components;
auto it = this->items.begin();
while(it != this->items.end()) {
auto component = it->second->getComponent<T>();
if(component != nullptr) components.push_back(component);
++it;
}
return components;
}
/**
* Destroys a previously initialized Scene.
*/

View File

@ -8,8 +8,10 @@
using namespace Dawn;
SceneItem::SceneItem(std::weak_ptr<Scene>, sceneitemid_t id) {
this->scene = scene;
SceneItem::SceneItem(Scene &scene, sceneitemid_t id) :
scene(scene),
id(id)
{
this->id = id;
}

View File

@ -11,14 +11,12 @@ namespace Dawn {
class Scene;
class SceneItem :
public std::enable_shared_from_this<SceneItem>
{
class SceneItem {
private:
std::vector<std::shared_ptr<SceneItemComponent>> components;
public:
std::weak_ptr<Scene> scene;
Scene &scene;
sceneitemid_t id;
/**
@ -28,7 +26,7 @@ namespace Dawn {
* @param scene Weak pointer to the Scene that this SceneItem belongs to.
* @param id Scene Item ID that the Scene assigned this SceneItem.
*/
SceneItem(std::weak_ptr<Scene> scene, sceneitemid_t id);
SceneItem(Scene &scene, sceneitemid_t id);
/**
* Called by the Scene the frame after we were constructed so we can begin
@ -48,7 +46,7 @@ namespace Dawn {
*/
template<class T>
std::shared_ptr<T> addComponent() {
auto component = std::make_shared<T>(weak_from_this());
auto component = std::make_shared<T>(*this);
this->components.push_back(component);
return component;
}

View File

@ -10,16 +10,15 @@
using namespace Dawn;
SceneItemComponent::SceneItemComponent(std::weak_ptr<SceneItem> item) {
this->item = item;
SceneItemComponent::SceneItemComponent(SceneItem &item) : item(item) {
}
std::shared_ptr<Scene> SceneItemComponent::getScene() {
return this->item.lock()->scene.lock();
Scene & SceneItemComponent::getScene() {
return this->item.scene;
}
std::shared_ptr<DawnGame> SceneItemComponent::getGame() {
return this->item.lock()->scene.lock()->game.lock();
DawnGame & SceneItemComponent::getGame() {
return this->item.scene.game;
}
SceneItemComponent::~SceneItemComponent() {

View File

@ -13,14 +13,14 @@ namespace Dawn {
class SceneItemComponent {
public:
std::weak_ptr<SceneItem> item;
SceneItem &item;
SceneItemComponent(std::weak_ptr<SceneItem> item);
SceneItemComponent(SceneItem &item);
virtual void start() = 0;
std::shared_ptr<Scene> getScene();
std::shared_ptr<DawnGame> getGame();
Scene & getScene();
DawnGame & getGame();
virtual ~SceneItemComponent();
};

View File

@ -9,7 +9,9 @@
namespace Dawn {
class DummyComponent : public SceneItemComponent {
public:
DummyComponent(std::weak_ptr<SceneItem> item) : SceneItemComponent(item) {
DummyComponent(SceneItem &item) :
SceneItemComponent(item)
{
std::cout << "Dummy Component Initialized" << std::endl;
};

View File

@ -9,7 +9,9 @@
using namespace Dawn;
Camera::Camera(std::weak_ptr<SceneItem> item) : SceneItemComponent(item) {
Camera::Camera(SceneItem &item) :
SceneItemComponent(item)
{
}
void Camera::updateProjection() {
@ -36,17 +38,16 @@ void Camera::updateProjection() {
}
}
std::shared_ptr<RenderTarget> Camera::getRenderTarget() {
RenderTarget & Camera::getRenderTarget() {
if(this->target == nullptr) {
return this->getGame()->renderManager.getBackBuffer();
return this->getGame().renderManager.getBackBuffer();
}
return this->target;
return *this->target;
}
float_t Camera::getAspect() {
auto target = this->getRenderTarget();
return target->getWidth() / target->getHeight();
RenderTarget &target = this->getRenderTarget();
return target.getWidth() / target.getHeight();
}
void Camera::start() {

View File

@ -37,14 +37,14 @@ namespace Dawn {
*
* @param item SceneItem that this component belongs to.
*/
Camera(std::weak_ptr<SceneItem> item);
Camera(SceneItem &item);
/**
* Updates the projection matrix.
*/
void updateProjection();
std::shared_ptr<RenderTarget> getRenderTarget();
RenderTarget & getRenderTarget();
/**
* Returs the aspect ratio of the camera.

View File

@ -7,7 +7,7 @@
using namespace Dawn;
MeshRenderer::MeshRenderer(std::weak_ptr<SceneItem> item) :
MeshRenderer::MeshRenderer(SceneItem &item) :
SceneItemComponent(item)
{

View File

@ -12,7 +12,7 @@ namespace Dawn {
public:
std::shared_ptr<Mesh> mesh = nullptr;
MeshRenderer(std::weak_ptr<SceneItem> item);
MeshRenderer(SceneItem &item);
void start() override;
};