Part one - removed references and smart pointers

This commit is contained in:
2022-11-11 19:08:46 -08:00
parent 4c2fc4cfcf
commit 42645883cd
76 changed files with 3899 additions and 3707 deletions

View File

@ -1,40 +1,42 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "Scene.hpp"
#include "game/DawnGame.hpp"
using namespace Dawn;
Scene::Scene(DawnGame &game) : game(game) {
this->nextId = 0;
}
void Scene::update() {
// Finsh adding scene items that were trying to add from the last frame.
auto it = this->itemsNotInitialized.begin();
while(it != this->itemsNotInitialized.end()) {
this->items[it->first] = it->second;
it->second->init();
++it;
}
this->itemsNotInitialized.clear();
// TODO: Cleanup old scene items
// TODO: Tick scene items(?)
this->eventSceneUpdate.invoke();
if(!this->game.timeManager.isPaused) this->eventSceneUnpausedUpdate.invoke();
}
std::shared_ptr<SceneItem> Scene::createSceneItem() {
sceneitemid_t id = this->nextId++;
auto item = std::make_shared<SceneItem>(*this, id);
this->itemsNotInitialized[id] = item;
return item;
}
Scene::~Scene() {
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "Scene.hpp"
#include "game/DawnGame.hpp"
using namespace Dawn;
Scene::Scene(DawnGame *game) {
this->game = game;
this->nextId = 0;
}
void Scene::update() {
// Finsh adding scene items that were trying to add from the last frame.
auto it = this->itemsNotInitialized.begin();
while(it != this->itemsNotInitialized.end()) {
this->items[it->first] = it->second;
it->second->init();
++it;
}
this->itemsNotInitialized.clear();
// TODO: Cleanup old scene items
// TODO: Tick scene items(?)
this->eventSceneUpdate.invoke();
if(!this->game->timeManager.isPaused) this->eventSceneUnpausedUpdate.invoke();
}
SceneItem * Scene::createSceneItem() {
sceneitemid_t id = this->nextId++;
auto item = new SceneItem(this, id);
this->itemsNotInitialized[id] = item;
return item;
}
Scene::~Scene() {
}

View File

@ -1,87 +1,87 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "SceneItem.hpp"
#include "event/Event.hpp"
namespace Dawn {
class DawnGame;
class Scene {
private:
sceneitemid_t nextId;
std::map<sceneitemid_t, std::shared_ptr<SceneItem>> items;
std::map<sceneitemid_t, std::shared_ptr<SceneItem>> itemsNotInitialized;
public:
DawnGame &game;
Event<> eventSceneUpdate;
Event<> eventSceneUnpausedUpdate;
/**
* Construct a new Scene instance.
*
* @param game Reference to the game that this scene belongs to.
*/
Scene(DawnGame &game);
/**
* Perform a one frame synchronous tick on the current scene. This may
* change in future to be more event-like.
*/
void update();
/**
* Create a Scene Item object and add to the current scene.
*
* @return A shared pointer to the created SceneItem.
*/
std::shared_ptr<SceneItem> createSceneItem();
/**
* Finds an existing component on the scene (Root Level Only) that has a
* component matching the given component type. Returns nullptr if no item
* with the specified component could be found.
*
* @tparam Component type to look for.
* @return Pointer to the found component (and by extension the item).
*/
template<class T>
std::shared_ptr<T> findComponent() {
auto it = this->items.begin();
while(it != this->items.end()) {
auto component = it->second->getComponent<T>();
if(component != nullptr) return component;
++it;
}
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.
*/
~Scene();
};
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "SceneItem.hpp"
#include "event/Event.hpp"
namespace Dawn {
class DawnGame;
class Scene {
private:
sceneitemid_t nextId;
std::map<sceneitemid_t, SceneItem*> items;
std::map<sceneitemid_t, SceneItem*> itemsNotInitialized;
public:
DawnGame *game;
Event<> eventSceneUpdate;
Event<> eventSceneUnpausedUpdate;
/**
* Construct a new Scene instance.
*
* @param game Reference to the game that this scene belongs to.
*/
Scene(DawnGame *game);
/**
* Perform a one frame synchronous tick on the current scene. This may
* change in future to be more event-like.
*/
void update();
/**
* Create a Scene Item object and add to the current scene.
*
* @return A shared pointer to the created SceneItem.
*/
SceneItem * createSceneItem();
/**
* Finds an existing component on the scene (Root Level Only) that has a
* component matching the given component type. Returns nullptr if no item
* with the specified component could be found.
*
* @tparam Component type to look for.
* @return Pointer to the found component (and by extension the item).
*/
template<class T>
T * findComponent() {
auto it = this->items.begin();
while(it != this->items.end()) {
auto component = it->second->getComponent<T>();
if(component != nullptr) return component;
++it;
}
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<T*> findComponents() {
std::vector<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.
*/
~Scene();
};
}

View File

@ -1,59 +1,57 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "SceneItem.hpp"
#include "Scene.hpp"
using namespace Dawn;
SceneItem::SceneItem(Scene &scene, sceneitemid_t id) :
scene(scene),
id(id),
transform(*this)
{
}
void SceneItem::init() {
// Keep checking all components until they have all inited
int32_t waitingOn;
do {
waitingOn = 0;
// For each component
auto it = this->components.begin();
while(it != this->components.end()) {
// Has this component already inited?
if((*it)->hasInitialized) {
++it;
continue;
}
// For each dependency.
auto deps = (*it)->getDependencies();
bool_t waiting = false;
auto it2 = deps.begin();
while(it2 != deps.end()) {
// Has the dep not yet inited?
if(!(*it2)->hasInitialized) {
waiting = true;
break;
}
++it2;
}
// Are we waiting for a dep?
if(waiting) {
waitingOn++;
} else {
(*it)->init();
}
++it;
}
} while(waitingOn != 0);
}
SceneItem::~SceneItem() {
std::cout << "Scene item disposed" << std::endl;
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "SceneItem.hpp"
#include "Scene.hpp"
using namespace Dawn;
SceneItem::SceneItem(Scene *scene, sceneitemid_t id) : transform(this) {
this->id = id;
this->scene = scene;
}
void SceneItem::init() {
// Keep checking all components until they have all inited
int32_t waitingOn;
do {
waitingOn = 0;
// For each component
auto it = this->components.begin();
while(it != this->components.end()) {
// Has this component already inited?
if((*it)->hasInitialized) {
++it;
continue;
}
// For each dependency.
auto deps = (*it)->getDependencies();
bool_t waiting = false;
auto it2 = deps.begin();
while(it2 != deps.end()) {
// Has the dep not yet inited?
if(!(*it2)->hasInitialized) {
waiting = true;
break;
}
++it2;
}
// Are we waiting for a dep?
if(waiting) {
waitingOn++;
} else {
(*it)->init();
}
++it;
}
} while(waitingOn != 0);
}
SceneItem::~SceneItem() {
std::cout << "Scene item disposed" << std::endl;
}

View File

@ -1,117 +1,117 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "SceneItemComponent.hpp"
#include "display/Transform.hpp"
#include "event/Event.hpp"
namespace Dawn {
typedef int32_t sceneitemid_t;
class Scene;
class SceneItem {
private:
std::vector<std::shared_ptr<SceneItemComponent>> components;
public:
Scene &scene;
sceneitemid_t id;
Transform transform;
/**
* Constructor for a SceneItem. Scene Items should only be called and
* initialized by the scene itself.
*
* @param scene Weak pointer to the Scene that this SceneItem belongs to.
* @param id Scene Item ID that the Scene assigned this SceneItem.
*/
SceneItem(Scene &scene, sceneitemid_t id);
/**
* Called by the Scene the frame after we were constructed so we can begin
* existing.
*/
void init();
/**
* Adds a component to this scene item. Components will only have their
* init methods invoked on the first frame we enter the scene. If you add
* a component to this item after this time you will either need to try
* manually invoking its' init method, or ensure the component is aware of
* the entire SceneItem lifecycle and listen for added callbacks.
*
* @tparam T Type of component being added to this scene item.
* @return A shared pointer to the newly added component.
*/
template<class T>
std::shared_ptr<T> addComponent() {
auto component = std::make_shared<T>(*this);
this->components.push_back(component);
return component;
}
/**
* Returns a component attached to this SceneItem. This method will return
* either a shared pointer to the component, or nullptr if the item does
* not have the queried component.
*
* @tparam T Type of component to be fetched.
* @return A shared pointer to the component, or nullptr if not found.
*/
template<class T>
std::shared_ptr<T> getComponent() {
auto it = this->components.begin();
while(it != this->components.end()) {
auto castedAs = std::dynamic_pointer_cast<T>(*it);
if(castedAs != nullptr) return castedAs;
++it;
}
return nullptr;
}
/**
* Finds a (direct) child of this component that has a matching component.
*
* @tparam T Component to find child of.
* @return Pointer to the child, or nullptr if not found.
*/
template<class T>
std::shared_ptr<T> findChild() {
auto it = this->transform.children.begin();
while(it != this->transform.children.end()) {
auto child = (*it)->item.getComponent<T>();
if(child != nullptr) return child;
++it;
}
return nullptr;
}
/**
* Finds all (direct) children of this component that match the queried
* component.
*
* @tparam T Component to find children for.
* @return Array of pointers to matching children.
*/
template<class T>
std::vector<std::shared_ptr<T>> findChildren() {
auto it = this->transform.children.begin();
std::vector<std::shared_ptr<T>> children;
while(it != this->transform.children.end()) {
auto child = (*it)->item.getComponent<T>();
if(child != nullptr) children.push_back(child);
++it;
}
return children;
}
/**
* Destroy this SceneItem.
*/
~SceneItem();
};
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "SceneItemComponent.hpp"
#include "display/Transform.hpp"
#include "event/Event.hpp"
namespace Dawn {
typedef int32_t sceneitemid_t;
class Scene;
class SceneItem {
private:
std::vector<SceneItemComponent*> components;
public:
Scene *scene;
sceneitemid_t id;
Transform transform;
/**
* Constructor for a SceneItem. Scene Items should only be called and
* initialized by the scene itself.
*
* @param scene Weak pointer to the Scene that this SceneItem belongs to.
* @param id Scene Item ID that the Scene assigned this SceneItem.
*/
SceneItem(Scene *scene, sceneitemid_t id);
/**
* Called by the Scene the frame after we were constructed so we can begin
* existing.
*/
void init();
/**
* Adds a component to this scene item. Components will only have their
* init methods invoked on the first frame we enter the scene. If you add
* a component to this item after this time you will either need to try
* manually invoking its' init method, or ensure the component is aware of
* the entire SceneItem lifecycle and listen for added callbacks.
*
* @tparam T Type of component being added to this scene item.
* @return A shared pointer to the newly added component.
*/
template<class T>
T * addComponent() {
auto component = new T(this);
this->components.push_back(component);
return component;
}
/**
* Returns a component attached to this SceneItem. This method will return
* either a shared pointer to the component, or nullptr if the item does
* not have the queried component.
*
* @tparam T Type of component to be fetched.
* @return A shared pointer to the component, or nullptr if not found.
*/
template<class T>
T * getComponent() {
auto it = this->components.begin();
while(it != this->components.end()) {
T *castedAs = dynamic_cast<T*>(*it);
if(castedAs != nullptr) return castedAs;
++it;
}
return nullptr;
}
/**
* Finds a (direct) child of this component that has a matching component.
*
* @tparam T Component to find child of.
* @return Pointer to the child, or nullptr if not found.
*/
template<class T>
T * findChild() {
auto it = this->transform.children.begin();
while(it != this->transform.children.end()) {
auto child = (*it)->item->getComponent<T>();
if(child != nullptr) return child;
++it;
}
return nullptr;
}
/**
* Finds all (direct) children of this component that match the queried
* component.
*
* @tparam T Component to find children for.
* @return Array of pointers to matching children.
*/
template<class T>
std::vector<T*> findChildren() {
auto it = this->transform.children.begin();
std::vector<T*> children;
while(it != this->transform.children.end()) {
auto child = (*it)->item->getComponent<T>();
if(child != nullptr) children.push_back(child);
++it;
}
return children;
}
/**
* Destroy this SceneItem.
*/
~SceneItem();
};
}

View File

@ -1,42 +1,41 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "SceneItemComponent.hpp"
#include "SceneItem.hpp"
#include "Scene.hpp"
#include "game/DawnGame.hpp"
using namespace Dawn;
SceneItemComponent::SceneItemComponent(SceneItem &item) :
item(item),
transform(item.transform)
{
}
void SceneItemComponent::init() {
this->onStart();
this->hasInitialized = true;
}
std::vector<SceneItemComponent*> SceneItemComponent::getDependencies() {
return std::vector<SceneItemComponent*>();
}
Scene & SceneItemComponent::getScene() {
return this->item.scene;
}
DawnGame & SceneItemComponent::getGame() {
return this->item.scene.game;
}
void SceneItemComponent::onStart() {
}
SceneItemComponent::~SceneItemComponent() {
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "SceneItemComponent.hpp"
#include "SceneItem.hpp"
#include "Scene.hpp"
#include "game/DawnGame.hpp"
using namespace Dawn;
SceneItemComponent::SceneItemComponent(SceneItem *item) {
this->item = item;
this->transform = &item->transform;
}
void SceneItemComponent::init() {
this->onStart();
this->hasInitialized = true;
}
std::vector<SceneItemComponent*> SceneItemComponent::getDependencies() {
return std::vector<SceneItemComponent*>();
}
Scene * SceneItemComponent::getScene() {
return this->item->scene;
}
DawnGame * SceneItemComponent::getGame() {
return this->item->scene->game;
}
void SceneItemComponent::onStart() {
}
SceneItemComponent::~SceneItemComponent() {
}

View File

@ -1,66 +1,66 @@
// 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/Transform.hpp"
namespace Dawn {
class SceneItem;
class Scene;
class DawnGame;
class SceneItemComponent {
public:
SceneItem &item;
Transform &transform;
bool_t hasInitialized = false;
/**
* Constructs a new SceneItemComponent. Components are attached to
* SceneItems and will be individual responsibility components, and must
* communicate to other items/components using methods and events.
*
* @param item Scene Item thsi component belongs to.
*/
SceneItemComponent(SceneItem &item);
/**
* Requested on the first frame that the parent scene item has become
* active, after all of my dependencies are ready.
*/
void init();
/**
* Optionally return all dependencies for this component to wait for init
* for before the component will be initialized.
*
* @return Array of dependencies.
*/
virtual std::vector<SceneItemComponent*> getDependencies();
/**
* Shorthand to return the scene that this component's item belongs to.
* @return The current scene.
*/
Scene & getScene();
/**
* Shorthand to return the game that this scene belongs to.
* @return The current game.
*/
DawnGame & getGame();
/**
* Same as init, but intended for your subclass to override.
*/
virtual void onStart();
/**
* Cleanup the SceneItemComponent.
*/
virtual ~SceneItemComponent();
};
// 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/Transform.hpp"
namespace Dawn {
class SceneItem;
class Scene;
class DawnGame;
class SceneItemComponent {
public:
SceneItem *item;
Transform *transform;
bool_t hasInitialized = false;
/**
* Constructs a new SceneItemComponent. Components are attached to
* SceneItems and will be individual responsibility components, and must
* communicate to other items/components using methods and events.
*
* @param item Scene Item thsi component belongs to.
*/
SceneItemComponent(SceneItem *item);
/**
* Requested on the first frame that the parent scene item has become
* active, after all of my dependencies are ready.
*/
void init();
/**
* Optionally return all dependencies for this component to wait for init
* for before the component will be initialized.
*
* @return Array of dependencies.
*/
virtual std::vector<SceneItemComponent*> getDependencies();
/**
* Shorthand to return the scene that this component's item belongs to.
* @return The current scene.
*/
Scene * getScene();
/**
* Shorthand to return the game that this scene belongs to.
* @return The current game.
*/
DawnGame * getGame();
/**
* Same as init, but intended for your subclass to override.
*/
virtual void onStart();
/**
* Cleanup the SceneItemComponent.
*/
virtual ~SceneItemComponent();
};
}

View File

@ -1,80 +1,80 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "Camera.hpp"
#include "scene/Scene.hpp"
#include "game/DawnGame.hpp"
using namespace Dawn;
Camera::Camera(SceneItem &item) :
SceneItemComponent(item)
{
this->getRenderTarget().eventRenderTargetResized.addListener(
this, &Camera::onRenderTargetResize
);
}
void Camera::updateProjection() {
switch(this->type) {
case CAMERA_TYPE_ORTHONOGRAPHIC:
this->projection = glm::ortho(
this->orthoLeft,
this->orthoRight,
this->orthoBottom,
this->orthoTop,
this->clipNear,
this->clipFar
);
break;
case CAMERA_TYPE_PERSPECTIVE:
this->projection = glm::perspective(
this->fov,
this->getAspect(),
this->clipNear,
this->clipFar
);
break;
}
}
RenderTarget & Camera::getRenderTarget() {
if(this->target == nullptr) {
return this->getGame().renderManager.getBackBuffer();
}
return *this->target;
}
void Camera::setRenderTarget(std::shared_ptr<RenderTarget> renderTarget) {
if(renderTarget == this->target) return;
this->getRenderTarget().eventRenderTargetResized.removeListener(
this, &Camera::onRenderTargetResize
);
this->target = renderTarget;
this->getRenderTarget().eventRenderTargetResized.addListener(
this, &Camera::onRenderTargetResize
);
this->updateProjection();
}
float_t Camera::getAspect() {
RenderTarget &target = this->getRenderTarget();
return target.getWidth() / target.getHeight();
}
void Camera::onStart() {
this->updateProjection();
}
void Camera::onRenderTargetResize(RenderTarget &target, float_t w, float_t h) {
this->updateProjection();
}
Camera::~Camera() {
this->getRenderTarget().eventRenderTargetResized.removeListener(
this, &Camera::onRenderTargetResize
);
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "Camera.hpp"
#include "scene/Scene.hpp"
#include "game/DawnGame.hpp"
using namespace Dawn;
Camera::Camera(SceneItem *item) :
SceneItemComponent(item)
{
this->getRenderTarget()->eventRenderTargetResized.addListener(
this, &Camera::onRenderTargetResize
);
}
void Camera::updateProjection() {
switch(this->type) {
case CAMERA_TYPE_ORTHONOGRAPHIC:
this->projection = glm::ortho(
this->orthoLeft,
this->orthoRight,
this->orthoBottom,
this->orthoTop,
this->clipNear,
this->clipFar
);
break;
case CAMERA_TYPE_PERSPECTIVE:
this->projection = glm::perspective(
this->fov,
this->getAspect(),
this->clipNear,
this->clipFar
);
break;
}
}
RenderTarget * Camera::getRenderTarget() {
if(this->target == nullptr) {
return this->getGame()->renderManager.getBackBuffer();
}
return this->target;
}
void Camera::setRenderTarget(RenderTarget *renderTarget) {
if(renderTarget == this->target) return;
this->getRenderTarget()->eventRenderTargetResized.removeListener(
this, &Camera::onRenderTargetResize
);
this->target = renderTarget;
this->getRenderTarget()->eventRenderTargetResized.addListener(
this, &Camera::onRenderTargetResize
);
this->updateProjection();
}
float_t Camera::getAspect() {
RenderTarget *target = this->getRenderTarget();
return target->getWidth() / target->getHeight();
}
void Camera::onStart() {
this->updateProjection();
}
void Camera::onRenderTargetResize(RenderTarget *target, float_t w, float_t h) {
this->updateProjection();
}
Camera::~Camera() {
this->getRenderTarget()->eventRenderTargetResized.removeListener(
this, &Camera::onRenderTargetResize
);
}

View File

@ -1,83 +1,83 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "scene/SceneItemComponent.hpp"
#include "display/RenderTarget.hpp"
namespace Dawn {
enum CameraType {
CAMERA_TYPE_ORTHONOGRAPHIC,
CAMERA_TYPE_PERSPECTIVE
};
class Camera : public SceneItemComponent {
protected:
std::shared_ptr<RenderTarget> target = nullptr;
void onRenderTargetResize(RenderTarget &target, float_t w, float_t h);
public:
glm::mat4 projection;
// Perspective
enum CameraType type = CAMERA_TYPE_PERSPECTIVE;
float_t fov = 0.785398f;// 45 degrees
// Ortho
float_t orthoLeft = 0.0f;
float_t orthoRight = 1.0f;
float_t orthoBottom = 0.0f;
float_t orthoTop = 1.0f;
// Shared
float_t clipNear = 0.001f;
float_t clipFar = 100.0f;
/**
* Create a new Camera Component.
*
* @param item SceneItem that this component belongs to.
*/
Camera(SceneItem &item);
/**
* Updates the projection matrix.
*/
void updateProjection();
/**
* Returns the intended render target for this camera to render to, will
* automatically revert to the back buffer if no frame buffer is provided.
*
* @return The target render target framebuffer.
*/
RenderTarget & getRenderTarget();
/**
* Updates the render target for the camera to use.
*
* @param renderTarget Render target for this camera to draw to.
*/
void setRenderTarget(std::shared_ptr<RenderTarget> renderTarget);
/**
* Returs the aspect ratio of the camera.
*
* @return The aspect ratio of the camera.
*/
float_t getAspect();
/**
* Event triggered by the scene item when the item is added to the scene.
*/
void onStart() override;
/**
* Disposes a previously initialized camera.
*/
~Camera();
};
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "scene/SceneItemComponent.hpp"
#include "display/RenderTarget.hpp"
namespace Dawn {
enum CameraType {
CAMERA_TYPE_ORTHONOGRAPHIC,
CAMERA_TYPE_PERSPECTIVE
};
class Camera : public SceneItemComponent {
protected:
RenderTarget *target = nullptr;
void onRenderTargetResize(RenderTarget *target, float_t w, float_t h);
public:
glm::mat4 projection;
// Perspective
enum CameraType type = CAMERA_TYPE_PERSPECTIVE;
float_t fov = 0.785398f;// 45 degrees
// Ortho
float_t orthoLeft = 0.0f;
float_t orthoRight = 1.0f;
float_t orthoBottom = 0.0f;
float_t orthoTop = 1.0f;
// Shared
float_t clipNear = 0.001f;
float_t clipFar = 100.0f;
/**
* Create a new Camera Component.
*
* @param item SceneItem that this component belongs to.
*/
Camera(SceneItem *item);
/**
* Updates the projection matrix.
*/
void updateProjection();
/**
* Returns the intended render target for this camera to render to, will
* automatically revert to the back buffer if no frame buffer is provided.
*
* @return The target render target framebuffer.
*/
RenderTarget * getRenderTarget();
/**
* Updates the render target for the camera to use.
*
* @param renderTarget Render target for this camera to draw to.
*/
void setRenderTarget(RenderTarget *renderTarget);
/**
* Returs the aspect ratio of the camera.
*
* @return The aspect ratio of the camera.
*/
float_t getAspect();
/**
* Event triggered by the scene item when the item is added to the scene.
*/
void onStart() override;
/**
* Disposes a previously initialized camera.
*/
~Camera();
};
}

View File

@ -1,72 +1,70 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "Material.hpp"
#include "scene/Scene.hpp"
#include "game/DawnGame.hpp"
using namespace Dawn;
Material::Material(SceneItem &item) :
SceneItemComponent(item),
shader(item.scene.game.renderManager.getDefaultShader())
{
this->updateShaderParameters();
}
void Material::updateShaderParameters() {
this->colorValues.clear();
this->boolValues.clear();
this->parameters = this->shader->getParameters();
this->shader->setDefaultParameters(*this);
// We do need to validate these params at some point to make sure that the
// shader has actually bound them.
}
void Material::setShaderParameters() {
auto it = this->parameters.begin();
while(it != this->parameters.end()) {
switch(it->second) {
case SHADER_PARAMETER_TYPE_COLOR:
this->shader->setColor(it->first, this->colorValues[it->first]);
break;
case SHADER_PARAMETER_TYPE_MATRIX:
this->shader->setMatrix(it->first, this->matrixValues[it->first]);
break;
case SHADER_PARAMETER_TYPE_BOOLEAN:
this->shader->setBoolean(it->first, this->boolValues[it->first]);
break;
case SHADER_PARAMETER_TYPE_VECTOR3:
this->shader->setVector3(it->first, this->vec3Values[it->first]);
break;
case SHADER_PARAMETER_TYPE_TEXTURE:
this->shader->setTexture(it->first, this->textureValues[it->first]);
break;
case SHADER_PARAMETER_TYPE_FLOAT:
this->shader->setFloat(it->first, this->floatValues[it->first]);
break;
default:
throw "An unsupported or invalid shader parameter type was supplied.";
}
++it;
}
}
std::shared_ptr<Shader> Material::getShader() {
return this->shader;
}
void Material::setShader(std::shared_ptr<Shader> shader) {
this->shader = shader;
this->updateShaderParameters();
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "Material.hpp"
#include "scene/Scene.hpp"
#include "game/DawnGame.hpp"
using namespace Dawn;
Material::Material(SceneItem *item) : SceneItemComponent(item) {
this->shader = item->scene->game->renderManager.getDefaultShader();
this->updateShaderParameters();
}
void Material::updateShaderParameters() {
this->colorValues.clear();
this->boolValues.clear();
this->parameters = this->shader->getParameters();
this->shader->setDefaultParameters(this);
// We do need to validate these params at some point to make sure that the
// shader has actually bound them.
}
void Material::setShaderParameters() {
auto it = this->parameters.begin();
while(it != this->parameters.end()) {
switch(it->second) {
case SHADER_PARAMETER_TYPE_COLOR:
this->shader->setColor(it->first, this->colorValues[it->first]);
break;
case SHADER_PARAMETER_TYPE_MATRIX:
this->shader->setMatrix(it->first, this->matrixValues[it->first]);
break;
case SHADER_PARAMETER_TYPE_BOOLEAN:
this->shader->setBoolean(it->first, this->boolValues[it->first]);
break;
case SHADER_PARAMETER_TYPE_VECTOR3:
this->shader->setVector3(it->first, this->vec3Values[it->first]);
break;
case SHADER_PARAMETER_TYPE_TEXTURE:
this->shader->setTexture(it->first, this->textureValues[it->first]);
break;
case SHADER_PARAMETER_TYPE_FLOAT:
this->shader->setFloat(it->first, this->floatValues[it->first]);
break;
default:
throw "An unsupported or invalid shader parameter type was supplied.";
}
++it;
}
}
Shader * Material::getShader() {
return this->shader;
}
void Material::setShader(Shader * shader) {
this->shader = shader;
this->updateShaderParameters();
}

View File

@ -1,63 +1,63 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "scene/SceneItemComponent.hpp"
#include "display/shader/Shader.hpp"
namespace Dawn {
class Shader;
class Material : public SceneItemComponent {
private:
std::shared_ptr<Shader> shader;
/**
* Internal method that will be invoked to go through and update all of
* the shader parameters whenever the shader is swapped out.
*/
void updateShaderParameters();
public:
std::map<shaderparameter_t, enum ShaderParameterType> parameters;
std::map<shaderparameter_t, struct Color> colorValues;
std::map<shaderparameter_t, bool_t> boolValues;
std::map<shaderparameter_t, glm::mat4> matrixValues;
std::map<shaderparameter_t, glm::vec3> vec3Values;
std::map<shaderparameter_t, Texture*> textureValues;
std::map<shaderparameter_t, float_t> floatValues;
/**
* Material component constructor.
*
* @param item Scene Item this component belongs to.
*/
Material(SceneItem &item);
/**
* Return the shader this material is currently using.
*
* @return Shader pointer to the currently bound shader.
*/
std::shared_ptr<Shader> getShader();
/**
* Sets the shader for the material to use. This will also clear and
* update all of the parameters from the shaders' default parameter list.
*
* @param shader Shader to set.
*/
void setShader(std::shared_ptr<Shader> shader);
/**
* Protected method that can be called, likely by the render pipeline, to
* set and update all of the shader parameters from this material on to
* the shader.
*
* This method assumes that the shader has already been bound.
*/
void setShaderParameters();
};
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "scene/SceneItemComponent.hpp"
#include "display/shader/Shader.hpp"
namespace Dawn {
class Shader;
class Material : public SceneItemComponent {
private:
Shader *shader;
/**
* Internal method that will be invoked to go through and update all of
* the shader parameters whenever the shader is swapped out.
*/
void updateShaderParameters();
public:
std::map<shaderparameter_t, enum ShaderParameterType> parameters;
std::map<shaderparameter_t, struct Color> colorValues;
std::map<shaderparameter_t, bool_t> boolValues;
std::map<shaderparameter_t, glm::mat4> matrixValues;
std::map<shaderparameter_t, glm::vec3> vec3Values;
std::map<shaderparameter_t, Texture*> textureValues;
std::map<shaderparameter_t, float_t> floatValues;
/**
* Material component constructor.
*
* @param item Scene Item this component belongs to.
*/
Material(SceneItem *item);
/**
* Return the shader this material is currently using.
*
* @return Shader pointer to the currently bound shader.
*/
Shader * getShader();
/**
* Sets the shader for the material to use. This will also clear and
* update all of the parameters from the shaders' default parameter list.
*
* @param shader Shader to set.
*/
void setShader(Shader * shader);
/**
* Protected method that can be called, likely by the render pipeline, to
* set and update all of the shader parameters from this material on to
* the shader.
*
* This method assumes that the shader has already been bound.
*/
void setShaderParameters();
};
}

View File

@ -1,14 +1,11 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "MeshRenderer.hpp"
using namespace Dawn;
MeshRenderer::MeshRenderer(SceneItem &item) :
SceneItemComponent(item)
{
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "MeshRenderer.hpp"
using namespace Dawn;
MeshRenderer::MeshRenderer(SceneItem *item) : SceneItemComponent(item) {
}

View File

@ -1,22 +1,22 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "scene/SceneItemComponent.hpp"
#include "display/mesh/Mesh.hpp"
namespace Dawn {
class MeshRenderer : public SceneItemComponent {
public:
std::shared_ptr<Mesh> mesh = nullptr;
/**
* Constructs a MeshRenderer scene item component.
*
* @param item Scene Item this mesh renderer belongs to.
*/
MeshRenderer(SceneItem &item);
};
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "scene/SceneItemComponent.hpp"
#include "display/mesh/Mesh.hpp"
namespace Dawn {
class MeshRenderer : public SceneItemComponent {
public:
Mesh * mesh = nullptr;
/**
* Constructs a MeshRenderer scene item component.
*
* @param item Scene Item this mesh renderer belongs to.
*/
MeshRenderer(SceneItem *item);
};
}

View File

@ -1,52 +1,51 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "UICanvas.hpp"
#include "scene/Scene.hpp"
#include "ui/UIComponent.hpp"
#include "game/DawnGame.hpp"
using namespace Dawn;
std::shared_ptr<UICanvas> UICanvas::createCanvas(std::shared_ptr<Scene> scene) {
auto item = scene->createSceneItem();
return item->addComponent<UICanvas>();
}
UICanvas::UICanvas(SceneItem &item) : SceneItemComponent(item) {
}
float_t UICanvas::getWidth() {
return this->getGame().renderManager.getBackBuffer().getWidth();
}
float_t UICanvas::getHeight() {
return this->getGame().renderManager.getBackBuffer().getHeight();
}
void UICanvas::onStart() {
std::cout << "Canvas event" << std::endl;
this->getGame().renderManager.getBackBuffer()
.eventRenderTargetResized.addListener(this, &UICanvas::onBackBufferResize)
;
}
void UICanvas::onBackBufferResize(
RenderTarget &target,
float_t width,
float_t height
) {
auto it = this->children.begin();
while(it != this->children.end()) {
(*it)->updatePositions();
++it;
}
}
UICanvas::~UICanvas() {
this->getGame().renderManager.getBackBuffer()
.eventRenderTargetResized.removeListener(this, &UICanvas::onBackBufferResize)
;
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "UICanvas.hpp"
#include "scene/Scene.hpp"
#include "ui/UIComponent.hpp"
#include "game/DawnGame.hpp"
using namespace Dawn;
UICanvas * UICanvas::createCanvas(Scene *scene) {
auto item = scene->createSceneItem();
return item->addComponent<UICanvas>();
}
UICanvas::UICanvas(SceneItem *item) : SceneItemComponent(item) {
}
float_t UICanvas::getWidth() {
return this->getGame()->renderManager.getBackBuffer()->getWidth();
}
float_t UICanvas::getHeight() {
return this->getGame()->renderManager.getBackBuffer()->getHeight();
}
void UICanvas::onStart() {
this->getGame()->renderManager.getBackBuffer()->eventRenderTargetResized
.addListener(this, &UICanvas::onBackBufferResize)
;
}
void UICanvas::onBackBufferResize(
RenderTarget *target,
float_t width,
float_t height
) {
auto it = this->children.begin();
while(it != this->children.end()) {
(*it)->updatePositions();
++it;
}
}
UICanvas::~UICanvas() {
this->getGame()->renderManager.getBackBuffer()->eventRenderTargetResized
.removeListener(this, &UICanvas::onBackBufferResize)
;
}

View File

@ -1,52 +1,80 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "scene/SceneItemComponent.hpp"
#include "display/RenderTarget.hpp"
namespace Dawn {
enum UIDrawType {
UI_DRAW_TYPE_WORLD_ABSOLUTE,
UI_DRAW_TYPE_WORLD_CAMERA_RELATIVE,
UI_DRAW_TYPE_CAMERA_OVERLAY
};
class UIComponent;
class UICanvas : public SceneItemComponent {
protected:
void onBackBufferResize(
RenderTarget &target,
float_t width,
float_t height
);
public:
static std::shared_ptr<UICanvas> createCanvas(
std::shared_ptr<Scene> scene
);
//
std::vector<std::shared_ptr<UIComponent>> children;
UIDrawType drawType = UI_DRAW_TYPE_WORLD_CAMERA_RELATIVE;
UICanvas(SceneItem &item);
template<class T>
std::shared_ptr<T> addElement() {
auto item = std::make_shared<T>(*this);
this->children.push_back(item);
return item;
}
float_t getWidth();
float_t getHeight();
void onStart() override;
~UICanvas();
};
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "scene/SceneItemComponent.hpp"
#include "display/RenderTarget.hpp"
namespace Dawn {
enum UIDrawType {
UI_DRAW_TYPE_WORLD_ABSOLUTE,
UI_DRAW_TYPE_WORLD_CAMERA_RELATIVE,
UI_DRAW_TYPE_CAMERA_OVERLAY
};
class UIComponent;
class UICanvas : public SceneItemComponent {
protected:
void onBackBufferResize(
RenderTarget *target,
float_t width,
float_t height
);
public:
/**
* Creates a UI Canvas Scene Item Element, and attaches it to the provided
* scene.
*
* @param scene Scene to create the UI Canvas for.
* @return Created UI Canvas.
*/
static UICanvas * createCanvas(Scene *scene);
//
std::vector<UIComponent*> children;
UIDrawType drawType = UI_DRAW_TYPE_WORLD_CAMERA_RELATIVE;
/**
* Constructs the UI Canvas Scene Item Component.
*
* @param item Item that this canvas item belongs to.
*/
UICanvas(SceneItem *item);
/**
* Construct and append a UI item to this UI Canvas.
*
* @tparam Type of the UI Item.
* @return Pointer to the created UI Item.
*/
template<class T>
T * addElement() {
auto item = new T(this);
this->children.push_back(item);
return item;
}
/**
* Returns the width of the root UI Canvas size. In future I may allow
* this to be dynamic, right now it uses the render canvas however.
*
* @return Width of the UI Canvas.
*/
float_t getWidth();
/**
* Returns the height of this UI Canvas element.
*
* @return Height of the UI Canvas.
*/
float_t getHeight();
void onStart() override;
~UICanvas();
};
}