Moved Scene implementation around

This commit is contained in:
2023-02-21 20:59:42 -08:00
parent cd686c2bd6
commit c065ab08b3
42 changed files with 540 additions and 459 deletions

View File

@ -4,6 +4,8 @@
// https://opensource.org/licenses/MIT
#include "Scene.hpp"
#include "SceneItem.hpp"
#include "SceneItemComponent.hpp"
#include "game/DawnGame.hpp"
using namespace Dawn;

View File

@ -4,13 +4,19 @@
// https://opensource.org/licenses/MIT
#pragma once
#include "SceneItem.hpp"
#include "event/Event.hpp"
#include "asset/Asset.hpp"
namespace Dawn {
class DawnGame;
class RenderPipeline;
class SceneItem;
class SceneItemComponent;
typedef int32_t sceneitemid_t;
template<class T>
T * _sceneForwardGetComponent(SceneItem *item);
class Scene {
private:
@ -84,13 +90,13 @@ namespace Dawn {
T * findComponent() {
auto it = this->itemsNotInitialized.begin();
while(it != this->itemsNotInitialized.end()) {
auto component = it->second->getComponent<T>();
auto component = _sceneForwardGetComponent<T>(it->second);
if(component != nullptr) return component;
++it;
}
auto it2 = this->items.begin();
while(it2 != this->items.end()) {
auto component = it2->second->getComponent<T>();
auto component = _sceneForwardGetComponent<T>(it2->second);
if(component != nullptr) return component;
++it2;
}
@ -110,14 +116,14 @@ namespace Dawn {
auto it = this->itemsNotInitialized.begin();
while(it != this->itemsNotInitialized.end()) {
auto component = it->second->getComponent<T>();
auto component = _sceneForwardGetComponent<T>(it->second);
if(component != nullptr) components.push_back(component);
++it;
}
auto it2 = this->items.begin();
while(it2 != this->items.end()) {
auto component = it2->second->getComponent<T>();
auto component = _sceneForwardGetComponent<T>(it2->second);
if(component != nullptr) components.push_back(component);
++it2;
}

View File

@ -4,7 +4,7 @@
// https://opensource.org/licenses/MIT
#include "SceneItem.hpp"
#include "Scene.hpp"
#include "SceneItemComponent.hpp"
using namespace Dawn;

View File

@ -4,14 +4,12 @@
// https://opensource.org/licenses/MIT
#pragma once
#include "SceneItemComponent.hpp"
#include "display/Transform.hpp"
#include "event/Event.hpp"
#include "scene/Scene.hpp"
namespace Dawn {
typedef int32_t sceneitemid_t;
class Scene;
class SceneItemComponent;
class SceneItem {
private:

View File

@ -6,10 +6,9 @@
#pragma once
#include "dawnlibs.hpp"
#include "display/Transform.hpp"
#include "scene/SceneItem.hpp"
namespace Dawn {
class SceneItem;
class Scene;
class DawnGame;
class SceneItemComponent {
@ -70,4 +69,11 @@ namespace Dawn {
*/
~SceneItemComponent();
};
template<class T>
T * _sceneForwardGetComponent(SceneItem *item) {
return item->getComponent<T>();
}
}

View File

@ -1,38 +1,37 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "AnimationController.hpp"
#include "scene/Scene.hpp"
#include "game/DawnGame.hpp"
using namespace Dawn;
AnimationController::AnimationController(SceneItem *item) :
SceneItemComponent(item)
{
}
void AnimationController::addAnimation(Animation *animation) {
assertNotNull(animation);
this->animations.push_back(animation);
}
void AnimationController::onSceneUpdate() {
auto it = this->animations.begin();
while(it != this->animations.end()) {
(*it)->tick(this->getGame()->timeManager.delta);
++it;
}
}
void AnimationController::onStart() {
SceneItemComponent::onStart();
getScene()->eventSceneUnpausedUpdate.addListener(this, &AnimationController::onSceneUpdate);
}
void AnimationController::onDispose() {
getScene()->eventSceneUnpausedUpdate.removeListener(this, &AnimationController::onSceneUpdate);
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "AnimationController.hpp"
#include "game/DawnGame.hpp"
using namespace Dawn;
AnimationController::AnimationController(SceneItem *item) :
SceneItemComponent(item)
{
}
void AnimationController::addAnimation(Animation *animation) {
assertNotNull(animation);
this->animations.push_back(animation);
}
void AnimationController::onSceneUpdate() {
auto it = this->animations.begin();
while(it != this->animations.end()) {
(*it)->tick(this->getGame()->timeManager.delta);
++it;
}
}
void AnimationController::onStart() {
SceneItemComponent::onStart();
getScene()->eventSceneUnpausedUpdate.addListener(this, &AnimationController::onSceneUpdate);
}
void AnimationController::onDispose() {
getScene()->eventSceneUnpausedUpdate.removeListener(this, &AnimationController::onSceneUpdate);
}

View File

@ -4,7 +4,6 @@
// https://opensource.org/licenses/MIT
#include "Camera.hpp"
#include "scene/Scene.hpp"
#include "game/DawnGame.hpp"
using namespace Dawn;

View File

@ -4,7 +4,6 @@
// https://opensource.org/licenses/MIT
#include "Material.hpp"
#include "scene/Scene.hpp"
#include "game/DawnGame.hpp"
using namespace Dawn;

View File

@ -1,81 +1,80 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "SimpleRenderTargetQuad.hpp"
#include "scene/Scene.hpp"
using namespace Dawn;
SimpleRenderTargetQuad::SimpleRenderTargetQuad(SceneItem *i) :
SceneItemComponent(i)
{
}
void SimpleRenderTargetQuad::onRenderTargetResized(
RenderTarget *target, float_t w, float_t h
) {
assertTrue(target == this->renderTarget);
// Update mesh
QuadMesh::bufferQuadMesh(
&this->meshHost->mesh,
glm::vec2(0, 0), glm::vec2(0, 0),
glm::vec2(w, h), glm::vec2(1, 1),
0, 0
);
}
void SimpleRenderTargetQuad::setRenderTarget(RenderTarget *rt) {
assertTrue(rt != this->renderTarget);
// Remove old event listener
if(this->renderTarget != nullptr) {
this->renderTarget->eventRenderTargetResized.removeListener(
this, &SimpleRenderTargetQuad::onRenderTargetResized
);
}
this->renderTarget = rt;
// Add new event listener.
if(rt != nullptr) {
rt->eventRenderTargetResized.addListener(
this, &SimpleRenderTargetQuad::onRenderTargetResized
);
}
}
std::vector<SceneItemComponent*> SimpleRenderTargetQuad::getDependencies() {
return std::vector<SceneItemComponent*>{
(this->meshHost = this->item->getComponent<MeshHost>())
};
}
void SimpleRenderTargetQuad::onStart() {
assertNotNull(this->meshHost);
// Create quad mesh
this->meshHost->mesh.createBuffers(QUAD_VERTICE_COUNT, QUAD_INDICE_COUNT);
// Perform first resize.
if(this->renderTarget != nullptr) {
QuadMesh::bufferQuadMesh(
&this->meshHost->mesh,
glm::vec2(0, 0),
glm::vec2(0, 0),
glm::vec2(this->renderTarget->getWidth(), this->renderTarget->getHeight()),
glm::vec2(1,1),
0, 0
);
}
}
void SimpleRenderTargetQuad::onDispose() {
if(this->renderTarget != nullptr) {
this->renderTarget->eventRenderTargetResized.removeListener(
this, &SimpleRenderTargetQuad::onRenderTargetResized
);
}
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "SimpleRenderTargetQuad.hpp"
using namespace Dawn;
SimpleRenderTargetQuad::SimpleRenderTargetQuad(SceneItem *i) :
SceneItemComponent(i)
{
}
void SimpleRenderTargetQuad::onRenderTargetResized(
RenderTarget *target, float_t w, float_t h
) {
assertTrue(target == this->renderTarget);
// Update mesh
QuadMesh::bufferQuadMesh(
&this->meshHost->mesh,
glm::vec2(0, 0), glm::vec2(0, 0),
glm::vec2(w, h), glm::vec2(1, 1),
0, 0
);
}
void SimpleRenderTargetQuad::setRenderTarget(RenderTarget *rt) {
assertTrue(rt != this->renderTarget);
// Remove old event listener
if(this->renderTarget != nullptr) {
this->renderTarget->eventRenderTargetResized.removeListener(
this, &SimpleRenderTargetQuad::onRenderTargetResized
);
}
this->renderTarget = rt;
// Add new event listener.
if(rt != nullptr) {
rt->eventRenderTargetResized.addListener(
this, &SimpleRenderTargetQuad::onRenderTargetResized
);
}
}
std::vector<SceneItemComponent*> SimpleRenderTargetQuad::getDependencies() {
return std::vector<SceneItemComponent*>{
(this->meshHost = this->item->getComponent<MeshHost>())
};
}
void SimpleRenderTargetQuad::onStart() {
assertNotNull(this->meshHost);
// Create quad mesh
this->meshHost->mesh.createBuffers(QUAD_VERTICE_COUNT, QUAD_INDICE_COUNT);
// Perform first resize.
if(this->renderTarget != nullptr) {
QuadMesh::bufferQuadMesh(
&this->meshHost->mesh,
glm::vec2(0, 0),
glm::vec2(0, 0),
glm::vec2(this->renderTarget->getWidth(), this->renderTarget->getHeight()),
glm::vec2(1,1),
0, 0
);
}
}
void SimpleRenderTargetQuad::onDispose() {
if(this->renderTarget != nullptr) {
this->renderTarget->eventRenderTargetResized.removeListener(
this, &SimpleRenderTargetQuad::onRenderTargetResized
);
}
}

View File

@ -1,45 +1,44 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "ExampleSpin.hpp"
#include "scene/Scene.hpp"
#include "game/DawnGame.hpp"
#include "scene/components/display/MeshRenderer.hpp"
#include "display/mesh/CubeMesh.hpp"
#include "scene/components/display/material/SimpleTexturedMaterial.hpp"
using namespace Dawn;
SceneItem * ExampleSpin::create(Scene *scene) {
auto item = scene->createSceneItem();
auto mr = item->addComponent<MeshRenderer>();
mr->mesh = new Mesh();
mr->mesh->createBuffers(CUBE_VERTICE_COUNT, CUBE_INDICE_COUNT);
CubeMesh::buffer(mr->mesh, glm::vec3(-0.5f, -0.5f, -0.5f), glm::vec3(1, 1, 1), 0, 0);
auto mat = item->addComponent<SimpleTexturedMaterial>();
item->addComponent<ExampleSpin>();
return item;
}
ExampleSpin::ExampleSpin(SceneItem *item) :
SceneItemComponent(item)
{
getScene()->eventSceneUnpausedUpdate.addListener(this, &ExampleSpin::onUnpausedUpdate);
}
void ExampleSpin::onUnpausedUpdate() {
auto quat = this->transform->getLocalRotation();
quat = glm::rotate(quat, getGame()->timeManager.delta, glm::vec3(0, 1, 0));
quat = glm::rotate(quat, getGame()->timeManager.delta / 2.0f, glm::vec3(1, 0, 0));
quat = glm::rotate(quat, getGame()->timeManager.delta / 4.0f, glm::vec3(0, 0, 1));
this->transform->setLocalRotation(quat);
}
void ExampleSpin::onDispose() {
getScene()->eventSceneUnpausedUpdate.removeListener(this, &ExampleSpin::onUnpausedUpdate);
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "ExampleSpin.hpp"
#include "game/DawnGame.hpp"
#include "scene/components/display/MeshRenderer.hpp"
#include "display/mesh/CubeMesh.hpp"
#include "scene/components/display/material/SimpleTexturedMaterial.hpp"
using namespace Dawn;
SceneItem * ExampleSpin::create(Scene *scene) {
auto item = scene->createSceneItem();
auto mr = item->addComponent<MeshRenderer>();
mr->mesh = new Mesh();
mr->mesh->createBuffers(CUBE_VERTICE_COUNT, CUBE_INDICE_COUNT);
CubeMesh::buffer(mr->mesh, glm::vec3(-0.5f, -0.5f, -0.5f), glm::vec3(1, 1, 1), 0, 0);
auto mat = item->addComponent<SimpleTexturedMaterial>();
item->addComponent<ExampleSpin>();
return item;
}
ExampleSpin::ExampleSpin(SceneItem *item) :
SceneItemComponent(item)
{
getScene()->eventSceneUnpausedUpdate.addListener(this, &ExampleSpin::onUnpausedUpdate);
}
void ExampleSpin::onUnpausedUpdate() {
auto quat = this->transform->getLocalRotation();
quat = glm::rotate(quat, getGame()->timeManager.delta, glm::vec3(0, 1, 0));
quat = glm::rotate(quat, getGame()->timeManager.delta / 2.0f, glm::vec3(1, 0, 0));
quat = glm::rotate(quat, getGame()->timeManager.delta / 4.0f, glm::vec3(0, 0, 1));
this->transform->setLocalRotation(quat);
}
void ExampleSpin::onDispose() {
getScene()->eventSceneUnpausedUpdate.removeListener(this, &ExampleSpin::onUnpausedUpdate);
}

View File

@ -0,0 +1,12 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "BoxCollider.hpp"
using namespace Dawn;
BoxCollider::BoxCollider(SceneItem *i) : Collider2D(i) {
}

View File

@ -0,0 +1,18 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "Collider2D.hpp"
#include "physics/2d/Box.hpp"
namespace Dawn {
class BoxCollider : public Collider2D {
public:
glm::vec2 min = glm::vec2(-0.5f, -0.5f);
glm::vec2 max = glm::vec2( 0.5f, 0.5f);
BoxCollider(SceneItem *item);
};
}

View File

@ -1,11 +1,11 @@
# Copyright (c) 2023 Dominic Masters
#
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT
# Sources
target_sources(${DAWN_TARGET_NAME}
PRIVATE
Collider2D.cpp
Ray2D.cpp
# Copyright (c) 2023 Dominic Masters
#
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT
# Sources
target_sources(${DAWN_TARGET_NAME}
PRIVATE
BoxCollider.cpp
Collider2D.cpp
)

View File

@ -1,27 +0,0 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "Ray2D.hpp"
using namespace Dawn;
Ray2D::Ray2D(glm::vec2 pos, glm::vec2 dir) :
position(pos),
direction(dir)
{
}
bool_t Ray2D::intersects(struct Ray2D ray, glm::vec2 *out) {
glm::vec2 j = this->position - ray.position;
float_t f = -ray.direction.x * direction.y + direction.x * ray.direction.y;
float_t s = (-direction.y * j.x + direction.x * j.y) / f;
float_t t = (ray.direction.x * j.y - ray.direction.y * j.x) / f;
if (s >= 0 && s <= 1 && t >= 0 && t <= 1) {
*out = position + (t * direction);
return true;
}
return false;
}

View File

@ -1,31 +0,0 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "dawnlibs.hpp"
namespace Dawn {
struct Ray2D {
const glm::vec2 position;
const glm::vec2 direction;
/**
* Constructs a new Ray 2D.
*
* @param position Position of this ray in 2D space.
* @param direction Direction from the origin in 2D space of this ray.
*/
Ray2D(glm::vec2 position, glm::vec2 direction);
/**
* Checks whether one ray is intersecting with another ray.
*
* @param ray The ray to check if this ray is intersecting.
* @param out The point in space where the two rays intersect.
* @return True if they intersect, otherwise false.
*/
bool_t intersects(struct Ray2D ray, glm::vec2 *out);
};
}

View File

@ -4,10 +4,10 @@
# https://opensource.org/licenses/MIT
# Sources
target_sources(${DAWN_TARGET_NAME}
PRIVATE
RayTester3D.cpp
)
# target_sources(${DAWN_TARGET_NAME}
# PRIVATE
# RayTester3D.cpp
# )
# Subdirs
add_subdirectory(collider)

View File

@ -1,44 +0,0 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "RayTester3D.hpp"
#include "scene/Scene.hpp"
using namespace Dawn;
RayTester3D::RayTester3D(SceneItem *item) : SceneItemComponent(item) {
}
void RayTester3D::onStart() {
this->getScene()->eventSceneUpdate.addListener(this, &RayTester3D::onUpdate);
}
void RayTester3D::onUpdate() {
prefab->transform.setLocalPosition(glm::vec3(0, 0, 0));
triangle.v0 = glm::vec3(-0.5f, -0.5f, 0);
triangle.v1 = glm::vec3(0.5f, -0.5f, 0);
triangle.v2 = glm::vec3(0, 0.5f, 0);
auto mouse = this->getGame()->inputManager.getAxis2D(INPUT_BIND_MOUSE_X, INPUT_BIND_MOUSE_Y);
mouse *= 2.0f;
mouse -= glm::vec2(1, 1);
glm::vec3 pos = glm::vec3(mouse.x * camera->orthoRight, mouse.y * camera->orthoBottom, 0.0f);
ray.direction = glm::vec3(0, 0, -15);
ray.origin = pos;
// prefab->transform.setLocalPosition(pos);
// ray.origin = glm::vec3(0, 0, 5);
bool_t x = raytestTriangle(ray, triangle, &hit, &normal, &distance);
if(x) {
prefab->material->color = COLOR_RED;
} else {
prefab->material->color = COLOR_WHITE;
}
}

View File

@ -1,27 +0,0 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "scene/SceneItemComponent.hpp"
#include "prefabs/SimpleSpinningCubePrefab.hpp"
#include "physics/3d/Ray3D.hpp"
#include "scene/components/display/Camera.hpp"
namespace Dawn {
class RayTester3D : public SceneItemComponent {
public:
SimpleSpinningCubePrefab *prefab;
Camera *camera;
struct PhysicsTriangle triangle;
struct Ray3D ray;
glm::vec3 hit;
glm::vec3 normal;
float_t distance;
RayTester3D(SceneItem *item);
void onStart() override;
void onUpdate();
};
}

View File

@ -1,28 +1,28 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "scene/Scene.hpp"
namespace Dawn {
class SubSceneController : public SceneItemComponent {
protected:
Scene *subScene = nullptr;
void onSceneUpdate();
void onSceneUnpausedUpdate();
public:
bool_t onlyUpdateUnpaused = false;
SubSceneController(SceneItem *item);
Scene * getSubScene();
void setSubScene(Scene *scene);
void onStart() override;
void onDispose() override;
};
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "scene/SceneItemComponent.hpp"
namespace Dawn {
class SubSceneController : public SceneItemComponent {
protected:
Scene *subScene = nullptr;
void onSceneUpdate();
void onSceneUnpausedUpdate();
public:
bool_t onlyUpdateUnpaused = false;
SubSceneController(SceneItem *item);
Scene * getSubScene();
void setSubScene(Scene *scene);
void onStart() override;
void onDispose() override;
};
}

View File

@ -4,7 +4,6 @@
// https://opensource.org/licenses/MIT
#include "UICanvas.hpp"
#include "scene/Scene.hpp"
#include "ui/UIComponent.hpp"
#include "game/DawnGame.hpp"
#include "ui/UIMenu.hpp"