Fixed all my pointers
This commit is contained in:
@ -16,4 +16,5 @@ target_include_directories(${DAWN_TARGET_NAME}
|
|||||||
)
|
)
|
||||||
|
|
||||||
# Subdirs
|
# Subdirs
|
||||||
|
add_subdirectory(display)
|
||||||
add_subdirectory(scene)
|
add_subdirectory(scene)
|
10
src/dawn/display/CMakeLists.txt
Normal file
10
src/dawn/display/CMakeLists.txt
Normal 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
|
||||||
|
)
|
26
src/dawn/display/RenderPipeline.cpp
Normal file
26
src/dawn/display/RenderPipeline.cpp
Normal 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() {
|
||||||
|
|
||||||
|
}
|
24
src/dawn/display/RenderPipeline.hpp
Normal file
24
src/dawn/display/RenderPipeline.hpp
Normal 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();
|
||||||
|
};
|
||||||
|
}
|
@ -8,10 +8,19 @@
|
|||||||
|
|
||||||
namespace Dawn {
|
namespace Dawn {
|
||||||
class DawnGame;
|
class DawnGame;
|
||||||
|
class RenderPipeline;
|
||||||
|
|
||||||
class IRenderManager {
|
class IRenderManager {
|
||||||
public:
|
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
|
* Returns the primary render target (the backbuffer) that draws directly
|
||||||
@ -19,10 +28,14 @@ namespace Dawn {
|
|||||||
*
|
*
|
||||||
* @return Shared pointer to the backbuffer render target.
|
* @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;
|
virtual void init() = 0;
|
||||||
|
|
||||||
|
@ -14,6 +14,6 @@ namespace Dawn {
|
|||||||
*
|
*
|
||||||
* @param mesh Mesh to initialize as a triangle.
|
* @param mesh Mesh to initialize as a triangle.
|
||||||
*/
|
*/
|
||||||
static void createTriangleMesh(std::shared_ptr<Mesh> mesh);
|
static void createTriangleMesh(Mesh &mesh);
|
||||||
};
|
};
|
||||||
}
|
}
|
@ -15,22 +15,17 @@
|
|||||||
namespace Dawn {
|
namespace Dawn {
|
||||||
class DawnHost;
|
class DawnHost;
|
||||||
|
|
||||||
class DawnGame :
|
class DawnGame {
|
||||||
public std::enable_shared_from_this<DawnGame>
|
|
||||||
{
|
|
||||||
private:
|
|
||||||
std::shared_ptr<Scene> scene;
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
std::weak_ptr<DawnHost> host;
|
std::shared_ptr<Scene> scene;
|
||||||
|
DawnHost &host;
|
||||||
RenderManager renderManager;
|
RenderManager renderManager;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Construct a new instance of the DawnGame.
|
* 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
|
* 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
|
* return an initialize result, where DAWN_GAME_INIT_RESULT_SUCCESS is
|
||||||
* the only "successful" result, anything else is deemed a failure state.
|
* 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.
|
* @return The game initialize result.
|
||||||
*/
|
*/
|
||||||
int32_t init();
|
int32_t init();
|
||||||
|
@ -29,7 +29,9 @@ namespace Dawn {
|
|||||||
*/
|
*/
|
||||||
class DawnHostData;
|
class DawnHostData;
|
||||||
|
|
||||||
class DawnHost {
|
class DawnHost :
|
||||||
|
public std::enable_shared_from_this<DawnHost>
|
||||||
|
{
|
||||||
public:
|
public:
|
||||||
std::unique_ptr<DawnHostData> data;
|
std::unique_ptr<DawnHostData> data;
|
||||||
|
|
||||||
@ -49,7 +51,7 @@ namespace Dawn {
|
|||||||
* @param game Game instance that this host is running for.
|
* @param game Game instance that this host is running for.
|
||||||
* @return A status code, where DAWN_HOST_INIT_RESULT_SUCCESS is success.
|
* @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
|
* 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.
|
* @param game Game instance that this host is running for.
|
||||||
* @return A status code, refer to DAWN_HOST_START_RESULT_{} definitions.
|
* @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.
|
* 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.
|
* @param delta How much time has passed (in seconds) since the last tick.
|
||||||
* @return A status code, refer to DAWN_HOST_UPDATE_RESULT_{} definitions.
|
* @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
|
* 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.
|
* @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.
|
* Destroy (and unload) all of the DawnHost data from memory.
|
||||||
|
@ -8,8 +8,7 @@
|
|||||||
|
|
||||||
using namespace Dawn;
|
using namespace Dawn;
|
||||||
|
|
||||||
Scene::Scene(std::weak_ptr<DawnGame> game) {
|
Scene::Scene(DawnGame &game) : game(game) {
|
||||||
this->game = game;
|
|
||||||
this->nextId = 0;
|
this->nextId = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -30,7 +29,7 @@ void Scene::update() {
|
|||||||
|
|
||||||
std::shared_ptr<SceneItem> Scene::createSceneItem() {
|
std::shared_ptr<SceneItem> Scene::createSceneItem() {
|
||||||
sceneitemid_t id = this->nextId++;
|
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;
|
this->itemsNotInitialized[id] = item;
|
||||||
return item;
|
return item;
|
||||||
}
|
}
|
||||||
|
@ -9,23 +9,21 @@
|
|||||||
namespace Dawn {
|
namespace Dawn {
|
||||||
class DawnGame;
|
class DawnGame;
|
||||||
|
|
||||||
class Scene :
|
class Scene {
|
||||||
public std::enable_shared_from_this<Scene>
|
|
||||||
{
|
|
||||||
private:
|
private:
|
||||||
sceneitemid_t nextId;
|
sceneitemid_t nextId;
|
||||||
std::map<sceneitemid_t,std::shared_ptr<SceneItem>> items;
|
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>> itemsNotInitialized;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
std::weak_ptr<DawnGame> game;
|
DawnGame &game;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Construct a new Scene instance.
|
* 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
|
* Perform a one frame synchronous tick on the current scene. This may
|
||||||
@ -59,6 +57,25 @@ namespace Dawn {
|
|||||||
return nullptr;
|
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.
|
* Destroys a previously initialized Scene.
|
||||||
*/
|
*/
|
||||||
|
@ -8,8 +8,10 @@
|
|||||||
|
|
||||||
using namespace Dawn;
|
using namespace Dawn;
|
||||||
|
|
||||||
SceneItem::SceneItem(std::weak_ptr<Scene>, sceneitemid_t id) {
|
SceneItem::SceneItem(Scene &scene, sceneitemid_t id) :
|
||||||
this->scene = scene;
|
scene(scene),
|
||||||
|
id(id)
|
||||||
|
{
|
||||||
this->id = id;
|
this->id = id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -11,14 +11,12 @@ namespace Dawn {
|
|||||||
|
|
||||||
class Scene;
|
class Scene;
|
||||||
|
|
||||||
class SceneItem :
|
class SceneItem {
|
||||||
public std::enable_shared_from_this<SceneItem>
|
|
||||||
{
|
|
||||||
private:
|
private:
|
||||||
std::vector<std::shared_ptr<SceneItemComponent>> components;
|
std::vector<std::shared_ptr<SceneItemComponent>> components;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
std::weak_ptr<Scene> scene;
|
Scene &scene;
|
||||||
sceneitemid_t id;
|
sceneitemid_t id;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -28,7 +26,7 @@ namespace Dawn {
|
|||||||
* @param scene Weak pointer to the Scene that this SceneItem belongs to.
|
* @param scene Weak pointer to the Scene that this SceneItem belongs to.
|
||||||
* @param id Scene Item ID that the Scene assigned this SceneItem.
|
* @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
|
* Called by the Scene the frame after we were constructed so we can begin
|
||||||
@ -48,7 +46,7 @@ namespace Dawn {
|
|||||||
*/
|
*/
|
||||||
template<class T>
|
template<class T>
|
||||||
std::shared_ptr<T> addComponent() {
|
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);
|
this->components.push_back(component);
|
||||||
return component;
|
return component;
|
||||||
}
|
}
|
||||||
|
@ -10,16 +10,15 @@
|
|||||||
|
|
||||||
using namespace Dawn;
|
using namespace Dawn;
|
||||||
|
|
||||||
SceneItemComponent::SceneItemComponent(std::weak_ptr<SceneItem> item) {
|
SceneItemComponent::SceneItemComponent(SceneItem &item) : item(item) {
|
||||||
this->item = item;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::shared_ptr<Scene> SceneItemComponent::getScene() {
|
Scene & SceneItemComponent::getScene() {
|
||||||
return this->item.lock()->scene.lock();
|
return this->item.scene;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::shared_ptr<DawnGame> SceneItemComponent::getGame() {
|
DawnGame & SceneItemComponent::getGame() {
|
||||||
return this->item.lock()->scene.lock()->game.lock();
|
return this->item.scene.game;
|
||||||
}
|
}
|
||||||
|
|
||||||
SceneItemComponent::~SceneItemComponent() {
|
SceneItemComponent::~SceneItemComponent() {
|
||||||
|
@ -13,14 +13,14 @@ namespace Dawn {
|
|||||||
|
|
||||||
class SceneItemComponent {
|
class SceneItemComponent {
|
||||||
public:
|
public:
|
||||||
std::weak_ptr<SceneItem> item;
|
SceneItem &item;
|
||||||
|
|
||||||
SceneItemComponent(std::weak_ptr<SceneItem> item);
|
SceneItemComponent(SceneItem &item);
|
||||||
|
|
||||||
virtual void start() = 0;
|
virtual void start() = 0;
|
||||||
|
|
||||||
std::shared_ptr<Scene> getScene();
|
Scene & getScene();
|
||||||
std::shared_ptr<DawnGame> getGame();
|
DawnGame & getGame();
|
||||||
|
|
||||||
virtual ~SceneItemComponent();
|
virtual ~SceneItemComponent();
|
||||||
};
|
};
|
||||||
|
@ -9,7 +9,9 @@
|
|||||||
namespace Dawn {
|
namespace Dawn {
|
||||||
class DummyComponent : public SceneItemComponent {
|
class DummyComponent : public SceneItemComponent {
|
||||||
public:
|
public:
|
||||||
DummyComponent(std::weak_ptr<SceneItem> item) : SceneItemComponent(item) {
|
DummyComponent(SceneItem &item) :
|
||||||
|
SceneItemComponent(item)
|
||||||
|
{
|
||||||
std::cout << "Dummy Component Initialized" << std::endl;
|
std::cout << "Dummy Component Initialized" << std::endl;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -9,7 +9,9 @@
|
|||||||
|
|
||||||
using namespace Dawn;
|
using namespace Dawn;
|
||||||
|
|
||||||
Camera::Camera(std::weak_ptr<SceneItem> item) : SceneItemComponent(item) {
|
Camera::Camera(SceneItem &item) :
|
||||||
|
SceneItemComponent(item)
|
||||||
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
void Camera::updateProjection() {
|
void Camera::updateProjection() {
|
||||||
@ -36,17 +38,16 @@ void Camera::updateProjection() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std::shared_ptr<RenderTarget> Camera::getRenderTarget() {
|
RenderTarget & Camera::getRenderTarget() {
|
||||||
if(this->target == nullptr) {
|
if(this->target == nullptr) {
|
||||||
return this->getGame()->renderManager.getBackBuffer();
|
return this->getGame().renderManager.getBackBuffer();
|
||||||
}
|
}
|
||||||
|
return *this->target;
|
||||||
return this->target;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
float_t Camera::getAspect() {
|
float_t Camera::getAspect() {
|
||||||
auto target = this->getRenderTarget();
|
RenderTarget &target = this->getRenderTarget();
|
||||||
return target->getWidth() / target->getHeight();
|
return target.getWidth() / target.getHeight();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Camera::start() {
|
void Camera::start() {
|
||||||
|
@ -37,14 +37,14 @@ namespace Dawn {
|
|||||||
*
|
*
|
||||||
* @param item SceneItem that this component belongs to.
|
* @param item SceneItem that this component belongs to.
|
||||||
*/
|
*/
|
||||||
Camera(std::weak_ptr<SceneItem> item);
|
Camera(SceneItem &item);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Updates the projection matrix.
|
* Updates the projection matrix.
|
||||||
*/
|
*/
|
||||||
void updateProjection();
|
void updateProjection();
|
||||||
|
|
||||||
std::shared_ptr<RenderTarget> getRenderTarget();
|
RenderTarget & getRenderTarget();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returs the aspect ratio of the camera.
|
* Returs the aspect ratio of the camera.
|
||||||
|
@ -7,7 +7,7 @@
|
|||||||
|
|
||||||
using namespace Dawn;
|
using namespace Dawn;
|
||||||
|
|
||||||
MeshRenderer::MeshRenderer(std::weak_ptr<SceneItem> item) :
|
MeshRenderer::MeshRenderer(SceneItem &item) :
|
||||||
SceneItemComponent(item)
|
SceneItemComponent(item)
|
||||||
{
|
{
|
||||||
|
|
||||||
|
@ -12,7 +12,7 @@ namespace Dawn {
|
|||||||
public:
|
public:
|
||||||
std::shared_ptr<Mesh> mesh = nullptr;
|
std::shared_ptr<Mesh> mesh = nullptr;
|
||||||
|
|
||||||
MeshRenderer(std::weak_ptr<SceneItem> item);
|
MeshRenderer(SceneItem &item);
|
||||||
|
|
||||||
void start() override;
|
void start() override;
|
||||||
};
|
};
|
||||||
|
@ -25,7 +25,7 @@ DawnHost::DawnHost() {
|
|||||||
this->data->window = nullptr;
|
this->data->window = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
int32_t DawnHost::init(std::weak_ptr<DawnGame> game) {
|
int32_t DawnHost::init(DawnGame &game) {
|
||||||
// Init GLFW
|
// Init GLFW
|
||||||
if(!glfwInit()) return DAWN_GLFW_INIT_RESULT_INIT_FAILED;
|
if(!glfwInit()) return DAWN_GLFW_INIT_RESULT_INIT_FAILED;
|
||||||
|
|
||||||
@ -51,22 +51,18 @@ int32_t DawnHost::init(std::weak_ptr<DawnGame> game) {
|
|||||||
gladLoadGLLoader((GLADloadproc)glfwGetProcAddress);
|
gladLoadGLLoader((GLADloadproc)glfwGetProcAddress);
|
||||||
|
|
||||||
// Initialize the game
|
// Initialize the game
|
||||||
if(auto g = game.lock()) {
|
auto result = game.init();
|
||||||
auto result = g->init();
|
if(result != DAWN_GAME_INIT_RESULT_SUCCESS) return result;
|
||||||
if(result != DAWN_GAME_INIT_RESULT_SUCCESS) return result;
|
|
||||||
|
|
||||||
g->renderManager.renderTarget->setSize(
|
game.renderManager.backBuffer.setSize(
|
||||||
DAWN_GLFW_WINDOW_WIDTH_DEFAULT,
|
DAWN_GLFW_WINDOW_WIDTH_DEFAULT,
|
||||||
DAWN_GLFW_WINDOW_HEIGHT_DEFAULT
|
DAWN_GLFW_WINDOW_HEIGHT_DEFAULT
|
||||||
);
|
);
|
||||||
} else {
|
|
||||||
return DAWN_GLFW_INIT_RESULT_GAME_LOCK_FAILED;
|
|
||||||
}
|
|
||||||
|
|
||||||
return DAWN_HOST_INIT_RESULT_SUCCESS;
|
return DAWN_HOST_INIT_RESULT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
int32_t DawnHost::start(std::weak_ptr<DawnGame> game) {
|
int32_t DawnHost::start(DawnGame &game) {
|
||||||
double_t time, newTime;
|
double_t time, newTime;
|
||||||
float_t fDelta;
|
float_t fDelta;
|
||||||
int32_t updateResult;
|
int32_t updateResult;
|
||||||
@ -99,25 +95,21 @@ int32_t DawnHost::start(std::weak_ptr<DawnGame> game) {
|
|||||||
return DAWN_HOST_START_RESULT_EXIT_SUCCESS;
|
return DAWN_HOST_START_RESULT_EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
int32_t DawnHost::update(std::weak_ptr<DawnGame> game, float_t delta) {
|
int32_t DawnHost::update(DawnGame &game, float_t delta) {
|
||||||
if(auto g = game.lock()) {
|
auto ret = game.update(delta);
|
||||||
auto ret = g->update(delta);
|
switch(ret) {
|
||||||
switch(ret) {
|
case DAWN_GAME_UPDATE_RESULT_SUCCESS:
|
||||||
case DAWN_GAME_UPDATE_RESULT_SUCCESS:
|
return DAWN_HOST_UPDATE_RESULT_SUCCESS;
|
||||||
return DAWN_HOST_UPDATE_RESULT_SUCCESS;
|
|
||||||
|
case DAWN_GAME_UPDATE_RESULT_EXIT:
|
||||||
case DAWN_GAME_UPDATE_RESULT_EXIT:
|
return DAWN_HOST_UPDATE_RESULT_EXIT;
|
||||||
return DAWN_HOST_UPDATE_RESULT_EXIT;
|
|
||||||
|
|
||||||
default:
|
default:
|
||||||
return ret;
|
return ret;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return DAWN_GLFW_UPDATE_RESULT_GAME_LOCK_FAILED;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void DawnHost::unload(std::weak_ptr<DawnGame> game) {
|
void DawnHost::unload(DawnGame &game) {
|
||||||
glfwDestroyWindow(this->data->window);
|
glfwDestroyWindow(this->data->window);
|
||||||
this->data->window = nullptr;
|
this->data->window = nullptr;
|
||||||
glfwTerminate();
|
glfwTerminate();
|
||||||
|
@ -14,12 +14,9 @@
|
|||||||
|
|
||||||
#define DAWN_GLFW_INIT_RESULT_INIT_FAILED -1
|
#define DAWN_GLFW_INIT_RESULT_INIT_FAILED -1
|
||||||
#define DAWN_GLFW_INIT_RESULT_WINDOW_CREATE_FAILED -2
|
#define DAWN_GLFW_INIT_RESULT_WINDOW_CREATE_FAILED -2
|
||||||
#define DAWN_GLFW_INIT_RESULT_GAME_LOCK_FAILED -3
|
|
||||||
|
|
||||||
#define DAWN_GLFW_START_RESULT_UPDATE_FAILED -1
|
#define DAWN_GLFW_START_RESULT_UPDATE_FAILED -1
|
||||||
|
|
||||||
#define DAWN_GLFW_UPDATE_RESULT_GAME_LOCK_FAILED -1
|
|
||||||
|
|
||||||
namespace Dawn {
|
namespace Dawn {
|
||||||
class DawnHostData {
|
class DawnHostData {
|
||||||
public:
|
public:
|
||||||
|
@ -7,10 +7,9 @@
|
|||||||
|
|
||||||
using namespace Dawn;
|
using namespace Dawn;
|
||||||
|
|
||||||
BackBufferRenderTarget::BackBufferRenderTarget(
|
BackBufferRenderTarget::BackBufferRenderTarget(RenderManager &renderManager) :
|
||||||
std::weak_ptr<RenderManager> renderManager
|
renderManager(renderManager)
|
||||||
) {
|
{
|
||||||
this->renderManager = renderManager;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
float_t BackBufferRenderTarget::getWidth() {
|
float_t BackBufferRenderTarget::getWidth() {
|
||||||
|
@ -6,18 +6,19 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include "dawnopengl.hpp"
|
#include "dawnopengl.hpp"
|
||||||
#include "display/RenderTarget.hpp"
|
#include "display/RenderTarget.hpp"
|
||||||
#include "display/RenderManager.hpp"
|
|
||||||
|
|
||||||
namespace Dawn {
|
namespace Dawn {
|
||||||
|
class RenderManager;
|
||||||
|
|
||||||
class BackBufferRenderTarget : public RenderTarget {
|
class BackBufferRenderTarget : public RenderTarget {
|
||||||
private:
|
private:
|
||||||
std::weak_ptr<RenderManager> renderManager;
|
RenderManager &renderManager;
|
||||||
float_t width = 1;
|
float_t width = 1;
|
||||||
float_t height = 1;
|
float_t height = 1;
|
||||||
struct Color clearColor = COLOR_CORNFLOWER_BLUE;
|
struct Color clearColor = COLOR_CORNFLOWER_BLUE;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
BackBufferRenderTarget(std::weak_ptr<RenderManager> renderManager);
|
BackBufferRenderTarget(RenderManager &renderManager);
|
||||||
|
|
||||||
float_t getWidth() override;
|
float_t getWidth() override;
|
||||||
float_t getHeight() override;
|
float_t getHeight() override;
|
||||||
|
@ -8,6 +8,7 @@ target_sources(${DAWN_TARGET_NAME}
|
|||||||
PRIVATE
|
PRIVATE
|
||||||
RenderManager.cpp
|
RenderManager.cpp
|
||||||
BackBufferRenderTarget.cpp
|
BackBufferRenderTarget.cpp
|
||||||
|
StandardRenderPipeline.cpp
|
||||||
)
|
)
|
||||||
|
|
||||||
# Subdirs
|
# Subdirs
|
||||||
|
@ -6,28 +6,31 @@
|
|||||||
#include "dawnopengl.hpp"
|
#include "dawnopengl.hpp"
|
||||||
#include "game/DawnGame.hpp"
|
#include "game/DawnGame.hpp"
|
||||||
#include "display/RenderManager.hpp"
|
#include "display/RenderManager.hpp"
|
||||||
#include "display/BackBufferRenderTarget.hpp"
|
#include "display/StandardRenderPipeline.hpp"
|
||||||
|
|
||||||
using namespace Dawn;
|
using namespace Dawn;
|
||||||
|
|
||||||
RenderManager::RenderManager(std::weak_ptr<DawnGame> game) {
|
RenderManager::RenderManager(DawnGame &game) :
|
||||||
this->game = game;
|
IRenderManager(game),
|
||||||
this->renderTarget=std::make_shared<BackBufferRenderTarget>(weak_from_this());
|
backBuffer(*this)
|
||||||
|
{
|
||||||
|
this->standardRenderPipeline=std::make_shared<StandardRenderPipeline>(*this);
|
||||||
}
|
}
|
||||||
|
|
||||||
void RenderManager::init() {
|
void RenderManager::init() {
|
||||||
|
this->standardRenderPipeline->init();
|
||||||
}
|
}
|
||||||
|
|
||||||
std::shared_ptr<RenderTarget> RenderManager::getBackBuffer() {
|
RenderTarget & RenderManager::getBackBuffer() {
|
||||||
return this->renderTarget;
|
return this->backBuffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
RenderPipeline & RenderManager::getRenderPipeline() {
|
||||||
|
return *this->standardRenderPipeline;
|
||||||
}
|
}
|
||||||
|
|
||||||
void RenderManager::update() {
|
void RenderManager::update() {
|
||||||
this->getBackBuffer()->bind();
|
this->getRenderPipeline().render();
|
||||||
this->getBackBuffer()->clear(
|
|
||||||
RENDER_TARGET_CLEAR_FLAG_COLOR |
|
|
||||||
RENDER_TARGET_CLEAR_FLAG_DEPTH
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
RenderManager::~RenderManager() {
|
RenderManager::~RenderManager() {
|
||||||
|
@ -5,26 +5,24 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
#include "display/_RenderManager.hpp"
|
#include "display/_RenderManager.hpp"
|
||||||
|
#include "display/BackBufferRenderTarget.hpp"
|
||||||
|
|
||||||
namespace Dawn {
|
namespace Dawn {
|
||||||
class BackBufferRenderTarget;
|
class StandardRenderPipeline;
|
||||||
|
|
||||||
class RenderManager :
|
class RenderManager : public IRenderManager {
|
||||||
public IRenderManager,
|
private:
|
||||||
public std::enable_shared_from_this<RenderManager>
|
std::shared_ptr<StandardRenderPipeline> standardRenderPipeline;
|
||||||
{
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
std::shared_ptr<BackBufferRenderTarget> renderTarget;
|
BackBufferRenderTarget backBuffer;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Construct a new RenderManager for a game instance.
|
* Construct a new RenderManager for a game instance.
|
||||||
*
|
|
||||||
* @param game Game instance this render manager belongs to.
|
|
||||||
*/
|
*/
|
||||||
RenderManager(std::weak_ptr<DawnGame> game);
|
RenderManager(DawnGame &game);
|
||||||
|
RenderTarget & getBackBuffer() override;
|
||||||
std::shared_ptr<RenderTarget> getBackBuffer() override;
|
RenderPipeline & getRenderPipeline() override;
|
||||||
void init() override;
|
void init() override;
|
||||||
void update() override;
|
void update() override;
|
||||||
|
|
||||||
|
55
src/dawnopengl/display/StandardRenderPipeline.cpp
Normal file
55
src/dawnopengl/display/StandardRenderPipeline.cpp
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
// Copyright (c) 2022 Dominic Masters
|
||||||
|
//
|
||||||
|
// This software is released under the MIT License.
|
||||||
|
// https://opensource.org/licenses/MIT
|
||||||
|
|
||||||
|
#include "StandardRenderPipeline.hpp"
|
||||||
|
#include "scene/components/Components.hpp"
|
||||||
|
|
||||||
|
using namespace Dawn;
|
||||||
|
|
||||||
|
StandardRenderPipeline::StandardRenderPipeline(RenderManager &renderManager) :
|
||||||
|
RenderPipeline(renderManager)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
void StandardRenderPipeline::renderScene(Scene &scene) {
|
||||||
|
RenderTarget &backBuffer = this->renderManager.getBackBuffer();
|
||||||
|
auto cameras = scene.findComponents<Camera>();
|
||||||
|
std::shared_ptr<Camera> backBufferCamera = nullptr;
|
||||||
|
|
||||||
|
// First, render all non-backbuffer cameras.
|
||||||
|
auto it = cameras.begin();
|
||||||
|
while(it != cameras.end()) {
|
||||||
|
RenderTarget &cameraTarget = (*it)->getRenderTarget();
|
||||||
|
|
||||||
|
// Leave the backbuffer camera(s) to last, so we skip them.
|
||||||
|
if(&cameraTarget == &backBuffer) {
|
||||||
|
backBufferCamera = *it;
|
||||||
|
} else {
|
||||||
|
this->renderSceneCamera(scene, **it);
|
||||||
|
}
|
||||||
|
|
||||||
|
++it;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Now render the backbuffer camera.
|
||||||
|
if(backBufferCamera == nullptr) return;
|
||||||
|
this->renderSceneCamera(scene, *backBufferCamera);
|
||||||
|
}
|
||||||
|
|
||||||
|
void StandardRenderPipeline::renderSceneCamera(Scene &scene, Camera &camera) {
|
||||||
|
RenderTarget &renderTarget = camera.getRenderTarget();
|
||||||
|
renderTarget.bind();
|
||||||
|
renderTarget.clear(
|
||||||
|
RENDER_TARGET_CLEAR_FLAG_DEPTH |
|
||||||
|
RENDER_TARGET_CLEAR_FLAG_COLOR
|
||||||
|
);
|
||||||
|
|
||||||
|
auto meshes = scene.findComponents<MeshRenderer>();
|
||||||
|
auto it = meshes.begin();
|
||||||
|
while(it != meshes.end()) {
|
||||||
|
(*it)->mesh->draw(MESH_DRAW_MODE_TRIANGLES, 0, -1);
|
||||||
|
++it;
|
||||||
|
}
|
||||||
|
}
|
18
src/dawnopengl/display/StandardRenderPipeline.hpp
Normal file
18
src/dawnopengl/display/StandardRenderPipeline.hpp
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
// Copyright (c) 2022 Dominic Masters
|
||||||
|
//
|
||||||
|
// This software is released under the MIT License.
|
||||||
|
// https://opensource.org/licenses/MIT
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
#include "dawnopengl.hpp"
|
||||||
|
#include "display/RenderPipeline.hpp"
|
||||||
|
|
||||||
|
namespace Dawn {
|
||||||
|
class StandardRenderPipeline : public RenderPipeline {
|
||||||
|
public:
|
||||||
|
StandardRenderPipeline(RenderManager &renderManager);
|
||||||
|
|
||||||
|
void renderScene(Scene &scene) override;
|
||||||
|
void renderSceneCamera(Scene &scene, Camera &camera) override;
|
||||||
|
};
|
||||||
|
}
|
@ -8,19 +8,19 @@
|
|||||||
|
|
||||||
using namespace Dawn;
|
using namespace Dawn;
|
||||||
|
|
||||||
void TriangleMesh::createTriangleMesh(std::shared_ptr<Mesh> mesh) {
|
void TriangleMesh::createTriangleMesh(Mesh &mesh) {
|
||||||
mesh->createBuffers(3, 3);
|
mesh.createBuffers(3, 3);
|
||||||
mesh->bufferPositions(0, std::array<glm::vec3, 3>{{
|
mesh.bufferPositions(0, std::array<glm::vec3, 3>{{
|
||||||
glm::vec3(-1, 0, 0),
|
glm::vec3(-1, 0, 0),
|
||||||
glm::vec3(0, 1, 0),
|
glm::vec3(0, 1, 0),
|
||||||
glm::vec3(1, 0, 0)
|
glm::vec3(1, 0, 0)
|
||||||
}});
|
}});
|
||||||
mesh->bufferCoordinates(0, std::array<glm::vec2, 3>{{
|
mesh.bufferCoordinates(0, std::array<glm::vec2, 3>{{
|
||||||
glm::vec2(0, 0),
|
glm::vec2(0, 0),
|
||||||
glm::vec2(0, 1),
|
glm::vec2(0, 1),
|
||||||
glm::vec2(1, 0)
|
glm::vec2(1, 0)
|
||||||
}});
|
}});
|
||||||
mesh->bufferIndices(0, std::array<meshindice_t,3>{{
|
mesh.bufferIndices(0, std::array<meshindice_t,3>{{
|
||||||
0, 1, 2
|
0, 1, 2
|
||||||
}});
|
}});
|
||||||
}
|
}
|
@ -7,16 +7,16 @@
|
|||||||
|
|
||||||
using namespace Dawn;
|
using namespace Dawn;
|
||||||
|
|
||||||
DawnGame::DawnGame(std::weak_ptr<DawnHost> host) :
|
DawnGame::DawnGame(DawnHost &host) :
|
||||||
renderManager(weak_from_this())
|
host(host),
|
||||||
|
renderManager(*this)
|
||||||
{
|
{
|
||||||
this->host = host;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int32_t DawnGame::init() {
|
int32_t DawnGame::init() {
|
||||||
this->renderManager.init();
|
this->renderManager.init();
|
||||||
|
|
||||||
this->scene = std::make_shared<Scene>(weak_from_this());
|
this->scene = std::make_shared<Scene>(*this);
|
||||||
|
|
||||||
auto cameraObject = this->scene->createSceneItem();
|
auto cameraObject = this->scene->createSceneItem();
|
||||||
auto camera = cameraObject->addComponent<Camera>();
|
auto camera = cameraObject->addComponent<Camera>();
|
||||||
@ -24,20 +24,14 @@ int32_t DawnGame::init() {
|
|||||||
auto cubeObject = this->scene->createSceneItem();
|
auto cubeObject = this->scene->createSceneItem();
|
||||||
auto cubeMeshRenderer = cubeObject->addComponent<MeshRenderer>();
|
auto cubeMeshRenderer = cubeObject->addComponent<MeshRenderer>();
|
||||||
cubeMeshRenderer->mesh = std::make_shared<Mesh>();
|
cubeMeshRenderer->mesh = std::make_shared<Mesh>();
|
||||||
TriangleMesh::createTriangleMesh(cubeMeshRenderer->mesh);
|
TriangleMesh::createTriangleMesh(*cubeMeshRenderer->mesh);
|
||||||
|
|
||||||
return DAWN_GAME_INIT_RESULT_SUCCESS;
|
return DAWN_GAME_INIT_RESULT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
int32_t DawnGame::update(float_t delta) {
|
int32_t DawnGame::update(float_t delta) {
|
||||||
this->renderManager.update();
|
|
||||||
if(this->scene != nullptr) this->scene->update();
|
if(this->scene != nullptr) this->scene->update();
|
||||||
|
this->renderManager.update();
|
||||||
auto meshRenderer = this->scene->findComponent<MeshRenderer>();
|
|
||||||
if(meshRenderer != nullptr) {
|
|
||||||
meshRenderer->mesh->draw(MESH_DRAW_MODE_TRIANGLES, 0, -1);
|
|
||||||
}
|
|
||||||
|
|
||||||
return DAWN_GAME_UPDATE_RESULT_SUCCESS;
|
return DAWN_GAME_UPDATE_RESULT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -12,10 +12,10 @@ int32_t main(int32_t argc, char **args) {
|
|||||||
|
|
||||||
// Create the host
|
// Create the host
|
||||||
auto host = std::make_shared<DawnHost>();
|
auto host = std::make_shared<DawnHost>();
|
||||||
auto game = std::make_shared<DawnGame>(host);
|
auto game = std::make_shared<DawnGame>(*host);
|
||||||
|
|
||||||
// Initialize the host and error check
|
// Initialize the host and error check
|
||||||
result = host->init(std::weak_ptr<DawnGame>(game));
|
result = host->init(*game);
|
||||||
switch(result) {
|
switch(result) {
|
||||||
case DAWN_HOST_INIT_RESULT_SUCCESS:
|
case DAWN_HOST_INIT_RESULT_SUCCESS:
|
||||||
break;
|
break;
|
||||||
@ -24,7 +24,7 @@ int32_t main(int32_t argc, char **args) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Request the main loop to start running.
|
// Request the main loop to start running.
|
||||||
result = host->start(std::weak_ptr<DawnGame>(game));
|
result = host->start(*game);
|
||||||
switch(result) {
|
switch(result) {
|
||||||
case DAWN_HOST_START_RESULT_SUCCESS:
|
case DAWN_HOST_START_RESULT_SUCCESS:
|
||||||
break;
|
break;
|
||||||
@ -35,7 +35,7 @@ int32_t main(int32_t argc, char **args) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Main loop finished without errors, cleanup
|
// Main loop finished without errors, cleanup
|
||||||
host->unload(std::weak_ptr<DawnGame>(game));
|
host->unload(*game);
|
||||||
|
|
||||||
// Success
|
// Success
|
||||||
return 0;
|
return 0;
|
||||||
|
Reference in New Issue
Block a user