Order Freeing Memory

This commit is contained in:
fred 2025-01-19 16:03:36 +01:00
parent 3887733e67
commit 2e223e4ae1
6 changed files with 39 additions and 31 deletions

View File

@ -27,7 +27,6 @@
*.lib *.lib
# Executables # Executables
*.exe
*.out *.out
*.app *.app
@ -50,12 +49,14 @@
mono_crash.* mono_crash.*
# Build results # Build results
x64/
x86/
[Dd]ebug/ [Dd]ebug/
[Dd]ebugPublic/ [Dd]ebugPublic/
[Rr]elease/ [Rr]elease/
[Rr]eleases/ [Rr]eleases/
x64/
x86/
[Ww][Ii][Nn]32/ [Ww][Ii][Nn]32/
[Aa][Rr][Mm]/ [Aa][Rr][Mm]/
[Aa][Rr][Mm]64/ [Aa][Rr][Mm]64/

View File

@ -15,9 +15,10 @@ private:
GLFWwindow* _window; GLFWwindow* _window;
InputSystem* _inputSystem; InputSystem* _inputSystem;
glm::mat4 _view;
glm::mat4 _projection; glm::mat4 _projection;
float _distance;
glm::quat _orientation; glm::quat _orientation;
glm::vec2 _dragStart; glm::vec2 _dragStart;
@ -26,12 +27,14 @@ private:
float _scrollPositionPrevious; float _scrollPositionPrevious;
float _scrollPositionDelta; float _scrollPositionDelta;
glm::mat4 _initialCameraView;
static inline float cameraDistance = 8.15f; static inline float cameraDistance = 8.15f;
static inline Camera* _instance; static inline Camera* _instance;
public: public:
const glm::mat4& View() const { return _view * glm::mat4_cast(_orientation); } const glm::mat4& View() const { return LocalToWorld(); }
const glm::mat4& Projection() const { return _projection; } const glm::mat4& Projection() const { return _projection; }
public: public:
@ -40,9 +43,14 @@ public:
_window = window; _window = window;
_inputSystem = inputSystem; _inputSystem = inputSystem;
_distance = 0.0f;
_wasMouseClicked = false; _wasMouseClicked = false;
_view = glm::lookAt(glm::vec3(0.0f, 0.0f, cameraDistance), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f)); _initialCameraView = glm::lookAt(glm::vec3(0.0f, 0.0f, cameraDistance), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f));
SetTransform(_initialCameraView);
_projection = glm::mat4(1.0f); _projection = glm::mat4(1.0f);
} }
@ -91,9 +99,12 @@ public:
_orientation += 0.5f * (float)deltaTime * velocityQuaternion * _orientation; _orientation += 0.5f * (float)deltaTime * velocityQuaternion * _orientation;
_orientation = glm::normalize(_orientation); _orientation = glm::normalize(_orientation);
_view = glm::translate(_view, glm::vec3(0.0f, 0.0f, _scrollPositionDelta)); _distance += _scrollPositionDelta;
_scrollPositionDelta = 0.0f; _scrollPositionDelta = 0.0f;
SetTransform(_initialCameraView * glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 0.0f, _distance)) * glm::mat4_cast(_orientation));
// since callbacks need to be static use a singleton // since callbacks need to be static use a singleton
glfwSetScrollCallback(_window, Camera::scrollCallbackGlobal); glfwSetScrollCallback(_window, Camera::scrollCallbackGlobal);
} }

View File

@ -42,13 +42,7 @@ Cube::Cube() : Entity(nullptr)
} }
Cube::~Cube() { Cube::~Cube() {
for(int x=0;x<3;x++) {
for(int y=0;y<3;y++) {
for(int z=0;z<3;z++) {
delete _children[x][y][z];
}
}
}
} }
void Cube::_FindAxisChildren(const glm::ivec3& axis, int index, std::vector<glm::ivec3>& result) const { void Cube::_FindAxisChildren(const glm::ivec3& axis, int index, std::vector<glm::ivec3>& result) const {

View File

@ -23,6 +23,9 @@ Entity::Entity(Entity* parent) : _localToWorld(1.0f), _worldToLocal(1.0f)
} }
Entity::~Entity() { Entity::~Entity() {
if (_parent == nullptr)
return;
_parent->RemoveChild(this); _parent->RemoveChild(this);
} }

View File

@ -22,6 +22,7 @@ public:
Entity(Entity* parent); Entity(Entity* parent);
virtual ~Entity(); virtual ~Entity();
const std::unordered_set<Entity*>& Children() { return _children; }
void AddChild(Entity* entity); void AddChild(Entity* entity);
void RemoveChild(Entity* entity); void RemoveChild(Entity* entity);

View File

@ -1,7 +1,8 @@
#include "SceneInterface.h"
#include "ShaderUtil.h" #include "ShaderUtil.h"
#include "SceneInterface.h"
#include "PartitionedCube.h" #include "PartitionedCube.h"
#include "Plane.h" #include "Plane.h"
@ -9,21 +10,16 @@
#include <glm/glm.hpp> #include <glm/glm.hpp>
#include <glm/ext.hpp> #include <glm/ext.hpp>
#include <glm/gtx/string_cast.hpp> #include <glm/gtx/string_cast.hpp>
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#include <iostream> #include <iostream>
SceneInterface::SceneInterface() SceneInterface::SceneInterface() {
{
} }
SceneInterface::~SceneInterface() SceneInterface::~SceneInterface() {
{ std::reverse(_entities.begin(), _entities.end());
for(Entity* entity : _entities) { for(Entity* entity : _entities) {
delete entity; delete entity;
} }
@ -31,8 +27,7 @@ SceneInterface::~SceneInterface()
delete currentAction; delete currentAction;
} }
void SceneInterface::Initialize(GLFWwindow* window) void SceneInterface::Initialize(GLFWwindow* window) {
{
_inputSystem.SetWindow(window); _inputSystem.SetWindow(window);
_inputSystem.ObserveKey(GLFW_KEY_SPACE); _inputSystem.ObserveKey(GLFW_KEY_SPACE);
@ -140,8 +135,7 @@ void SceneInterface::EnqueueAction(Action* action) {
_actions.push(action); _actions.push(action);
} }
void SceneInterface::Update(float deltaTime) void SceneInterface::Update(float deltaTime) {
{
_inputSystem.Update(); _inputSystem.Update();
for(Entity* entity : _entities) { for(Entity* entity : _entities) {
@ -296,6 +290,8 @@ void SceneInterface::OnDragStart() {
continue; continue;
} }
_spinDelta = 0.0f;
_spinIndex = -1;
_plane = plane; _plane = plane;
_mousePositionPlane = intersectionPlane; _mousePositionPlane = intersectionPlane;
@ -361,6 +357,9 @@ void SceneInterface::OnDragStop() {
// Undo any temporarilies // Undo any temporarilies
_cube->UndoTransformTemp(); _cube->UndoTransformTemp();
if (_spinDelta < 0.001f)
return;
// Transform Instantly to current angle // Transform Instantly to current angle
_cube->Transform(_spinAxis, _spinIndex, glm::rotate(glm::mat4(1.0f), _spinDelta, glm::vec3(_spinAxis))); _cube->Transform(_spinAxis, _spinIndex, glm::rotate(glm::mat4(1.0f), _spinDelta, glm::vec3(_spinAxis)));
@ -406,7 +405,6 @@ void SceneInterface::Render(float aspectRatio) {
// std::cout << "model : " << glm::to_string(_cube.Transform()) << std::endl; // std::cout << "model : " << glm::to_string(_cube.Transform()) << std::endl;
} }
void SceneInterface::ClearResources() void SceneInterface::ClearResources() {
{
} }