Example Triangle Pog
This commit is contained in:
@ -18,8 +18,8 @@ void Scene::update() {
|
||||
auto it = this->itemsNotInitialized.begin();
|
||||
while(it != this->itemsNotInitialized.end()) {
|
||||
this->items[it->first] = it->second;
|
||||
it++;
|
||||
it->second->init();
|
||||
++it;
|
||||
}
|
||||
this->itemsNotInitialized.clear();
|
||||
|
||||
|
@ -28,6 +28,17 @@ namespace Dawn {
|
||||
|
||||
void removeSceneItem(SceneItem *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;
|
||||
}
|
||||
|
||||
~Scene();
|
||||
};
|
||||
}
|
@ -11,7 +11,9 @@ namespace Dawn {
|
||||
|
||||
class Scene;
|
||||
|
||||
class SceneItem {
|
||||
class SceneItem :
|
||||
public std::enable_shared_from_this<SceneItem>
|
||||
{
|
||||
private:
|
||||
std::vector<std::shared_ptr<SceneItemComponent>> components;
|
||||
|
||||
@ -25,7 +27,7 @@ namespace Dawn {
|
||||
|
||||
template<class T>
|
||||
std::shared_ptr<T> addComponent() {
|
||||
auto component = std::make_unique<T>(this);
|
||||
auto component = std::make_shared<T>(weak_from_this());
|
||||
this->components.push_back(component);
|
||||
return component;
|
||||
}
|
||||
@ -34,10 +36,9 @@ namespace Dawn {
|
||||
std::shared_ptr<T> getComponent() {
|
||||
auto it = this->components.begin();
|
||||
while(it != this->components.end()) {
|
||||
std::shared_ptr<T> castedAs = dynamic_cast<std::shared_ptr<T>>(*it);
|
||||
auto castedAs = std::dynamic_pointer_cast<T>(*it);
|
||||
if(castedAs != nullptr) return castedAs;
|
||||
it++;
|
||||
continue;
|
||||
++it;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
@ -7,4 +7,7 @@
|
||||
target_sources(${DAWN_TARGET_NAME}
|
||||
PRIVATE
|
||||
DummyComponent.cpp
|
||||
)
|
||||
)
|
||||
|
||||
# Subdirs
|
||||
add_subdirectory(display)
|
9
src/dawn/scene/components/Components.hpp
Normal file
9
src/dawn/scene/components/Components.hpp
Normal file
@ -0,0 +1,9 @@
|
||||
// Copyright (c) 2022 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#pragma once
|
||||
#include "scene/components/DummyComponent.hpp"
|
||||
#include "scene/components/display/Camera.hpp"
|
||||
#include "scene/components/display/MeshRenderer.hpp"
|
11
src/dawn/scene/components/display/CMakeLists.txt
Normal file
11
src/dawn/scene/components/display/CMakeLists.txt
Normal file
@ -0,0 +1,11 @@
|
||||
# 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
|
||||
Camera.cpp
|
||||
MeshRenderer.cpp
|
||||
)
|
43
src/dawn/scene/components/display/Camera.cpp
Normal file
43
src/dawn/scene/components/display/Camera.cpp
Normal file
@ -0,0 +1,43 @@
|
||||
// Copyright (c) 2022 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#include "Camera.hpp"
|
||||
|
||||
using namespace Dawn;
|
||||
|
||||
Camera::Camera(std::weak_ptr<SceneItem> item) : SceneItemComponent(item) {
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
float_t Camera::getAspect() {
|
||||
return 16.0f / 9.0f;
|
||||
}
|
||||
|
||||
void Camera::start() {
|
||||
this->updateProjection();
|
||||
}
|
57
src/dawn/scene/components/display/Camera.hpp
Normal file
57
src/dawn/scene/components/display/Camera.hpp
Normal file
@ -0,0 +1,57 @@
|
||||
// Copyright (c) 2022 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#pragma once
|
||||
#include "scene/SceneItemComponent.hpp"
|
||||
|
||||
namespace Dawn {
|
||||
enum CameraType {
|
||||
CAMERA_TYPE_ORTHONOGRAPHIC,
|
||||
CAMERA_TYPE_PERSPECTIVE
|
||||
};
|
||||
|
||||
class Camera : public SceneItemComponent {
|
||||
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(std::weak_ptr<SceneItem> item);
|
||||
|
||||
/**
|
||||
* Updates the projection matrix.
|
||||
*/
|
||||
void updateProjection();
|
||||
|
||||
/**
|
||||
* 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 start() override;
|
||||
};
|
||||
}
|
18
src/dawn/scene/components/display/MeshRenderer.cpp
Normal file
18
src/dawn/scene/components/display/MeshRenderer.cpp
Normal file
@ -0,0 +1,18 @@
|
||||
// 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(std::weak_ptr<SceneItem> item) :
|
||||
SceneItemComponent(item)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void MeshRenderer::start() {
|
||||
|
||||
}
|
19
src/dawn/scene/components/display/MeshRenderer.hpp
Normal file
19
src/dawn/scene/components/display/MeshRenderer.hpp
Normal file
@ -0,0 +1,19 @@
|
||||
// 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;
|
||||
|
||||
MeshRenderer(std::weak_ptr<SceneItem> item);
|
||||
|
||||
void start() override;
|
||||
};
|
||||
}
|
Reference in New Issue
Block a user