From f9935d6b772edc894e3d0f22a5eec3dd80fa5a02 Mon Sep 17 00:00:00 2001 From: JackCarterSmith Date: Fri, 7 Feb 2025 00:33:23 +0100 Subject: [PATCH] Objects constructor infinite allocation fix --- Engine/Graphics/3DRenderer.cpp | 6 ++-- Engine/Graphics/Camera.cpp | 2 +- Engine/Graphics/UI.cpp | 12 +++---- Engine/World/DbgCube.cpp | 62 ++++++++++++++++++---------------- Engine/World/Tank.cpp | 5 ++- Engine/World/WorldObject.hpp | 1 + 6 files changed, 47 insertions(+), 41 deletions(-) diff --git a/Engine/Graphics/3DRenderer.cpp b/Engine/Graphics/3DRenderer.cpp index d0cd1cd..90fd526 100644 --- a/Engine/Graphics/3DRenderer.cpp +++ b/Engine/Graphics/3DRenderer.cpp @@ -128,11 +128,11 @@ void Graphic3DRenderer::Draw(sf::RenderTexture& context) { void Graphic3DRenderer::UpdateInternalTestObjects() { static float thetaAngle = 0.31f; - thetaAngle = thetaAngle >= 6.283185f ? -6.283185f : thetaAngle + 0.004f; + thetaAngle = thetaAngle >= M3D_2PI ? -M3D_2PI : thetaAngle + 0.004f; static float thetaAngle2 = 2.12f; - thetaAngle2 = thetaAngle2 >= 6.283185f ? -6.283185f : thetaAngle2 + 0.005f; + thetaAngle2 = thetaAngle2 >= M3D_2PI ? -M3D_2PI : thetaAngle2 + 0.005f; static float thetaAngle3 = -4.78f; - thetaAngle3 = thetaAngle3 >= 6.283185f ? -6.283185f : thetaAngle3 + 0.008f; + thetaAngle3 = thetaAngle3 >= M3D_2PI ? -M3D_2PI : thetaAngle3 + 0.008f; mWorldObjsList[0]->SetRotation(thetaAngle, 0.f, thetaAngle * 0.5f); mWorldObjsList[1]->SetRotation(thetaAngle2, 0.f, thetaAngle2 * 0.5f); mWorldObjsList[2]->SetRotation(thetaAngle3, 0.f, thetaAngle3 * 0.5f); diff --git a/Engine/Graphics/Camera.cpp b/Engine/Graphics/Camera.cpp index ab99709..6955127 100644 --- a/Engine/Graphics/Camera.cpp +++ b/Engine/Graphics/Camera.cpp @@ -2,7 +2,7 @@ Camera::Camera() { - SetFrustrum(90.f, (1280.f/1024.f), 1.0f, 1000.f); + SetFrustrum(90.f, (1280.f/1024.f), 1.0f, 100.f); } void Camera::SetFrustrum(float fov, float r, float zn, float zf) { diff --git a/Engine/Graphics/UI.cpp b/Engine/Graphics/UI.cpp index fd86dd8..da90897 100644 --- a/Engine/Graphics/UI.cpp +++ b/Engine/Graphics/UI.cpp @@ -56,23 +56,23 @@ void CockpitUI::Update() { } void CockpitUI::Draw(std::shared_ptr& context) { - sf::BlendMode sBM = sf::BlendNone; + sf::BlendMode sBM = sf::BlendAlpha; sf::RenderStates sRS(sBM); // Clear the UI screen mUIRender.clear(sf::Color::Transparent); - // Draw the static board - sf::Sprite staticBoardSprite(mStaticCockpitTexture); - mUIRender.draw(staticBoardSprite, sRS); - // Draw the radar display sf::CircleShape radar(95, 8); radar.setRotation(22.5f); radar.setPosition(sf::Vector2f(838,378)); - radar.setFillColor(sf::Color::Yellow); + radar.setFillColor(SF_COLOR_4CHEX(0x666666FF)); mUIRender.draw(radar, sRS); + // Draw the static board + sf::Sprite staticBoardSprite(mStaticCockpitTexture); + mUIRender.draw(staticBoardSprite, sRS); + // Do the final texture render mUIRender.display(); diff --git a/Engine/World/DbgCube.cpp b/Engine/World/DbgCube.cpp index bbd471d..b2c7484 100644 --- a/Engine/World/DbgCube.cpp +++ b/Engine/World/DbgCube.cpp @@ -2,35 +2,37 @@ ObjectDbgCube::ObjectDbgCube() { - try { - mMesh.vertices.resize(8); - } catch (const std::length_error& ex) { - throw ex; + if (!isMeshLoaded) { + try { + mMesh.vertices.resize(8); + } catch (const std::length_error& ex) { + throw ex; + } + + mMesh.vertices[0] = { M3D_F3(0.5f, 0.5f, 0.5f), sf::Color::White}; + mMesh.vertices[1] = { M3D_F3(0.5f, -0.5f, 0.5f), sf::Color::Green}; + mMesh.vertices[2] = { M3D_F3(-0.5f, 0.5f, 0.5f), sf::Color::Yellow}; + mMesh.vertices[3] = { M3D_F3(-0.5f, -0.5f, 0.5f), sf::Color::Cyan}; + mMesh.vertices[4] = { M3D_F3(0.5f, 0.5f, -0.5f), sf::Color::Blue}; + mMesh.vertices[5] = { M3D_F3(0.5f, -0.5f, -0.5f), sf::Color::Red}; + mMesh.vertices[6] = { M3D_F3(-0.5f, 0.5f, -0.5f), sf::Color::Magenta}; + mMesh.vertices[7] = { M3D_F3(-0.5f, -0.5f, -0.5f), sf::Color::Black}; + + MeshPart basePart; + MHELPER_INDICES_TRI_ADD(basePart.indices, 4, 2, 0); + MHELPER_INDICES_TRI_ADD(basePart.indices, 2, 7, 3); + MHELPER_INDICES_TRI_ADD(basePart.indices, 6, 5, 7); + MHELPER_INDICES_TRI_ADD(basePart.indices, 1, 7, 5); + MHELPER_INDICES_TRI_ADD(basePart.indices, 0, 3, 1); + MHELPER_INDICES_TRI_ADD(basePart.indices, 4, 1, 5); + MHELPER_INDICES_TRI_ADD(basePart.indices, 4, 6, 2); + MHELPER_INDICES_TRI_ADD(basePart.indices, 2, 6, 7); + MHELPER_INDICES_TRI_ADD(basePart.indices, 6, 4, 5); + MHELPER_INDICES_TRI_ADD(basePart.indices, 1, 3, 7); + MHELPER_INDICES_TRI_ADD(basePart.indices, 0, 2, 3); + MHELPER_INDICES_TRI_ADD(basePart.indices, 4, 0, 1); + mMesh.parts.push_back(basePart); + + UpdateAABBFromMesh(); } - - mMesh.vertices[0] = { M3D_F3(0.5f, 0.5f, 0.5f), sf::Color::White}; - mMesh.vertices[1] = { M3D_F3(0.5f, -0.5f, 0.5f), sf::Color::Green}; - mMesh.vertices[2] = { M3D_F3(-0.5f, 0.5f, 0.5f), sf::Color::Yellow}; - mMesh.vertices[3] = { M3D_F3(-0.5f, -0.5f, 0.5f), sf::Color::Cyan}; - mMesh.vertices[4] = { M3D_F3(0.5f, 0.5f, -0.5f), sf::Color::Blue}; - mMesh.vertices[5] = { M3D_F3(0.5f, -0.5f, -0.5f), sf::Color::Red}; - mMesh.vertices[6] = { M3D_F3(-0.5f, 0.5f, -0.5f), sf::Color::Magenta}; - mMesh.vertices[7] = { M3D_F3(-0.5f, -0.5f, -0.5f), sf::Color::Black}; - - MeshPart basePart; - MHELPER_INDICES_TRI_ADD(basePart.indices, 4, 2, 0); - MHELPER_INDICES_TRI_ADD(basePart.indices, 2, 7, 3); - MHELPER_INDICES_TRI_ADD(basePart.indices, 6, 5, 7); - MHELPER_INDICES_TRI_ADD(basePart.indices, 1, 7, 5); - MHELPER_INDICES_TRI_ADD(basePart.indices, 0, 3, 1); - MHELPER_INDICES_TRI_ADD(basePart.indices, 4, 1, 5); - MHELPER_INDICES_TRI_ADD(basePart.indices, 4, 6, 2); - MHELPER_INDICES_TRI_ADD(basePart.indices, 2, 6, 7); - MHELPER_INDICES_TRI_ADD(basePart.indices, 6, 4, 5); - MHELPER_INDICES_TRI_ADD(basePart.indices, 1, 3, 7); - MHELPER_INDICES_TRI_ADD(basePart.indices, 0, 2, 3); - MHELPER_INDICES_TRI_ADD(basePart.indices, 4, 0, 1); - mMesh.parts.push_back(basePart); - - UpdateAABBFromMesh(); } \ No newline at end of file diff --git a/Engine/World/Tank.cpp b/Engine/World/Tank.cpp index 4b16c0b..7a01427 100644 --- a/Engine/World/Tank.cpp +++ b/Engine/World/Tank.cpp @@ -2,5 +2,8 @@ Tank::Tank() { - LoadMeshFromObjFile("tank_sample.obj"); + if (!isMeshLoaded) { + LoadMeshFromObjFile("tank_sample.obj"); + isMeshLoaded = true; + } } \ No newline at end of file diff --git a/Engine/World/WorldObject.hpp b/Engine/World/WorldObject.hpp index 154ea08..1e6b803 100644 --- a/Engine/World/WorldObject.hpp +++ b/Engine/World/WorldObject.hpp @@ -57,6 +57,7 @@ public: protected: inline static Mesh mMesh; + inline static bool isMeshLoaded = false; };