From d1d7d468264f55cf1149b4b4f6e22415eacbe200 Mon Sep 17 00:00:00 2001 From: Dominic Masters Date: Sat, 7 Dec 2024 13:25:53 -0600 Subject: [PATCH] Only buffer mesh each frame. --- .../display/mesh/CubeMeshComponent.cpp | 5 ++++ .../display/mesh/CubeMeshComponent.hpp | 8 ++++++ .../component/display/mesh/MeshComponent.cpp | 13 +++++++++- .../component/display/mesh/MeshComponent.hpp | 12 +++++++-- .../display/mesh/PlaneMeshComponent.cpp | 25 +++++++++++++++++++ .../display/mesh/PlaneMeshComponent.hpp | 5 +++- .../display/mesh/QuadMeshComponent.cpp | 4 +-- src/dawn/event/Event.hpp | 7 ++++++ src/dawn/scene/Scene.cpp | 3 +++ src/dawn/scene/Scene.hpp | 1 + src/dawnrpg/component/RPGMap.cpp | 20 +++++++++++++++ src/dawnrpg/component/RPGMap.hpp | 18 +++++++++++++ 12 files changed, 115 insertions(+), 6 deletions(-) create mode 100644 src/dawnrpg/component/RPGMap.cpp create mode 100644 src/dawnrpg/component/RPGMap.hpp diff --git a/src/dawn/component/display/mesh/CubeMeshComponent.cpp b/src/dawn/component/display/mesh/CubeMeshComponent.cpp index 0b019a3f..30f6e2ae 100644 --- a/src/dawn/component/display/mesh/CubeMeshComponent.cpp +++ b/src/dawn/component/display/mesh/CubeMeshComponent.cpp @@ -25,4 +25,9 @@ void CubeMeshComponent::meshLoad(std::shared_ptr ctx) { } else { this->size = glm::vec3(1.0f, 1.0f, 1.0f); } +} + +void CubeMeshComponent::setSize(const glm::vec3 &size) { + this->size = size; + this->invalidate(); } \ No newline at end of file diff --git a/src/dawn/component/display/mesh/CubeMeshComponent.hpp b/src/dawn/component/display/mesh/CubeMeshComponent.hpp index f641ad52..4f8e23da 100644 --- a/src/dawn/component/display/mesh/CubeMeshComponent.hpp +++ b/src/dawn/component/display/mesh/CubeMeshComponent.hpp @@ -13,5 +13,13 @@ namespace Dawn { void meshLoad(std::shared_ptr ctx) override; void meshBuffer(std::shared_ptr mesh) override; + + public: + /** + * Sets the size of the cube. + * + * @param size Size of the cube. + */ + void setSize(const glm::vec3 &size); }; } \ No newline at end of file diff --git a/src/dawn/component/display/mesh/MeshComponent.cpp b/src/dawn/component/display/mesh/MeshComponent.cpp index fc2381f5..bd34c9fc 100644 --- a/src/dawn/component/display/mesh/MeshComponent.cpp +++ b/src/dawn/component/display/mesh/MeshComponent.cpp @@ -4,6 +4,7 @@ // https://opensource.org/licenses/MIT #include "MeshComponent.hpp" +#include "scene/Scene.hpp" #include "component/display/MeshRenderer.hpp" using namespace Dawn; @@ -11,11 +12,21 @@ using namespace Dawn; void MeshComponent::buffer() { if(!mesh) mesh = std::make_shared(); this->meshBuffer(mesh); + this->valid = true; + events.clear(); +} + +void MeshComponent::invalidate() { + if(valid) return; + valid = false; + events.push_back(getScene()->onNextFrame.listen([this]() { + this->buffer(); + valid = true; + })); } void MeshComponent::onInit() { this->buffer(); - auto renderer = getItem()->getComponent(); if(renderer) renderer->mesh = mesh; } diff --git a/src/dawn/component/display/mesh/MeshComponent.hpp b/src/dawn/component/display/mesh/MeshComponent.hpp index e34e5c78..b4642476 100644 --- a/src/dawn/component/display/mesh/MeshComponent.hpp +++ b/src/dawn/component/display/mesh/MeshComponent.hpp @@ -9,6 +9,14 @@ namespace Dawn { class MeshComponent : public SceneComponent { + private: + bool_t valid = true; + + /** + * Buffers the mesh. + */ + void buffer(); + protected: /** * Called when the mesh should be loaded. @@ -25,9 +33,9 @@ namespace Dawn { virtual void meshBuffer(std::shared_ptr mesh) = 0; /** - * Buffers the mesh. + * Invalidates the mesh. */ - void buffer(); + void invalidate(); public: std::shared_ptr mesh; diff --git a/src/dawn/component/display/mesh/PlaneMeshComponent.cpp b/src/dawn/component/display/mesh/PlaneMeshComponent.cpp index 81ecf801..abedeefc 100644 --- a/src/dawn/component/display/mesh/PlaneMeshComponent.cpp +++ b/src/dawn/component/display/mesh/PlaneMeshComponent.cpp @@ -65,4 +65,29 @@ void PlaneMeshComponent::meshBuffer(std::shared_ptr mesh) { columns, rows, uv ); +} + +void PlaneMeshComponent::setColumns(const int32_t columns) { + assertTrue(columns > 0, "Columns must be greater than 0."); + this->columns = columns; + this->invalidate(); +} + + +void PlaneMeshComponent::setRows(const int32_t rows) { + assertTrue(rows > 0, "Rows must be greater than 0."); + this->rows = rows; + this->invalidate(); +} + +void PlaneMeshComponent::setPlaneSize(const glm::vec2 &planeSize) { + assertTrue(planeSize.x > 0.0f, "Plane size x must be greater than 0."); + assertTrue(planeSize.y > 0.0f, "Plane size y must be greater than 0."); + this->planeSize = planeSize; + this->invalidate(); +} + +void PlaneMeshComponent::setUV(const glm::vec4 &uv) { + this->uv = uv; + this->invalidate(); } \ No newline at end of file diff --git a/src/dawn/component/display/mesh/PlaneMeshComponent.hpp b/src/dawn/component/display/mesh/PlaneMeshComponent.hpp index 32be5c17..8714b554 100644 --- a/src/dawn/component/display/mesh/PlaneMeshComponent.hpp +++ b/src/dawn/component/display/mesh/PlaneMeshComponent.hpp @@ -18,6 +18,9 @@ namespace Dawn { void meshBuffer(std::shared_ptr mesh) override; public: - + void setColumns(const int32_t columns); + void setRows(const int32_t rows); + void setPlaneSize(const glm::vec2 &planeSize); + void setUV(const glm::vec4 &uv); }; } \ No newline at end of file diff --git a/src/dawn/component/display/mesh/QuadMeshComponent.cpp b/src/dawn/component/display/mesh/QuadMeshComponent.cpp index 89724f1e..33f7528d 100644 --- a/src/dawn/component/display/mesh/QuadMeshComponent.cpp +++ b/src/dawn/component/display/mesh/QuadMeshComponent.cpp @@ -35,10 +35,10 @@ void QuadMeshComponent::meshLoad(std::shared_ptr ctx) { void QuadMeshComponent::setPositions(const glm::vec4 &positions) { this->positions = positions; - this->buffer(); + this->invalidate(); } void QuadMeshComponent::setUVs(const glm::vec4 &uvs) { this->uvs = uvs; - this->buffer(); + this->invalidate(); } \ No newline at end of file diff --git a/src/dawn/event/Event.hpp b/src/dawn/event/Event.hpp index 1cdeb4d9..807bf61f 100644 --- a/src/dawn/event/Event.hpp +++ b/src/dawn/event/Event.hpp @@ -32,6 +32,13 @@ namespace Dawn { } } + /** + * Clears all listeners from the event. + */ + void clear() { + listeners.clear(); + } + /** * Listens to the event. * @param listener The listener to add. diff --git a/src/dawn/scene/Scene.cpp b/src/dawn/scene/Scene.cpp index de693ed7..28e88eac 100644 --- a/src/dawn/scene/Scene.cpp +++ b/src/dawn/scene/Scene.cpp @@ -54,6 +54,9 @@ void Scene::update() { sceneItemsToRemove.clear(); // Tick scene items. + onNextFrame.emit(); + onNextFrame.clear(); + float_t delta = getGame()->timeManager.delta; if(paused) { onPausedUpdate.emit(delta); diff --git a/src/dawn/scene/Scene.hpp b/src/dawn/scene/Scene.hpp index 29cd7978..4653f47a 100644 --- a/src/dawn/scene/Scene.hpp +++ b/src/dawn/scene/Scene.hpp @@ -29,6 +29,7 @@ namespace Dawn { public: Event onUnpausedUpdate; Event onPausedUpdate; + Event<> onNextFrame; TimeoutEvent onTimeout; IntervalEvent onInterval; diff --git a/src/dawnrpg/component/RPGMap.cpp b/src/dawnrpg/component/RPGMap.cpp new file mode 100644 index 00000000..e3f45f98 --- /dev/null +++ b/src/dawnrpg/component/RPGMap.cpp @@ -0,0 +1,20 @@ +// Copyright (c) 2024 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#include "RPGMap.hpp" + +using namespace Dawn; + +void RPGMap::onInit() { + auto mesh = getItem()->getComponent(); + +} + +void RPGMap::onDispose() { +} + +void RPGMap::load(std::shared_ptr ctx) { + SceneComponent::load(ctx); +} \ No newline at end of file diff --git a/src/dawnrpg/component/RPGMap.hpp b/src/dawnrpg/component/RPGMap.hpp new file mode 100644 index 00000000..0f9e9ed7 --- /dev/null +++ b/src/dawnrpg/component/RPGMap.hpp @@ -0,0 +1,18 @@ +// Copyright (c) 2024 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#pragma once +#include "component/display/mesh/PlaneMeshComponent.hpp" + +namespace Dawn { + class RPGMap final : public SceneComponent { + private: + + public: + void onInit() override; + void onDispose() override; + void load(std::shared_ptr ctx) override; + }; +} \ No newline at end of file