Part one - removed references and smart pointers
This commit is contained in:
@ -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
|
||||
);
|
||||
}
|
@ -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();
|
||||
};
|
||||
}
|
@ -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();
|
||||
}
|
@ -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();
|
||||
};
|
||||
}
|
@ -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) {
|
||||
}
|
@ -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);
|
||||
};
|
||||
}
|
@ -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)
|
||||
;
|
||||
}
|
@ -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();
|
||||
};
|
||||
}
|
Reference in New Issue
Block a user