diff --git a/enginecustom/Systemclass.cpp b/enginecustom/Systemclass.cpp index fa2c014..3e72119 100644 --- a/enginecustom/Systemclass.cpp +++ b/enginecustom/Systemclass.cpp @@ -163,6 +163,7 @@ bool SystemClass::Frame() m_Application->SetSpeed(speed); m_imguiManager->WidgetButton(); m_imguiManager->WidgetFPS(); + m_imguiManager->WidgetAddObject(m_Application); ImGui::End(); diff --git a/enginecustom/applicationclass.cpp b/enginecustom/applicationclass.cpp index f4e9acc..c5ce75d 100644 --- a/enginecustom/applicationclass.cpp +++ b/enginecustom/applicationclass.cpp @@ -126,6 +126,14 @@ void ApplicationClass::Shutdown() m_Direct3D = 0; } + // Libérez la mémoire pour chaque cube + for (auto cube : m_cubes) + { + cube->Shutdown(); + delete cube; + } + m_cubes.clear(); + return; } @@ -170,34 +178,22 @@ bool ApplicationClass::Render(float rotation) m_Camera->GetViewMatrix(viewMatrix); m_Direct3D->GetProjectionMatrix(projectionMatrix); - // Render the first cube - scaleMatrix = XMMatrixScaling(0.5f, 0.5f, 0.5f); - rotateMatrix = XMMatrixRotationX(rotation); - translateMatrix = XMMatrixTranslation(-2.0f, 0.0f, 0.0f); - srMatrix = XMMatrixMultiply(scaleMatrix, rotateMatrix); - worldMatrix = XMMatrixMultiply(srMatrix, translateMatrix); - - m_Model->Render(m_Direct3D->GetDeviceContext()); - result = m_LightShader->Render(m_Direct3D->GetDeviceContext(), m_Model->GetIndexCount(), worldMatrix, viewMatrix, projectionMatrix, m_Model->GetTexture(), - m_Light->GetDirection(), m_Light->GetDiffuseColor()); - if (!result) + for (auto cube : m_cubes) { - return false; - } + cube->Render(m_Direct3D->GetDeviceContext()); - // Render the second cube - scaleMatrix = XMMatrixScaling(1.5f, 1.5f, 1.5f); - rotateMatrix = XMMatrixRotationY(rotation); - translateMatrix = XMMatrixTranslation(2.0f, 0.0f, 0.0f); - srMatrix = XMMatrixMultiply(scaleMatrix, rotateMatrix); - worldMatrix = XMMatrixMultiply(srMatrix, translateMatrix); + scaleMatrix = cube->GetScaleMatrix(); + rotateMatrix = XMMatrixRotationY(rotation); + translateMatrix = cube->GetTranslateMatrix(); + srMatrix = XMMatrixMultiply(scaleMatrix, rotateMatrix); + worldMatrix = XMMatrixMultiply(srMatrix, translateMatrix); - m_Model->Render(m_Direct3D->GetDeviceContext()); - result = m_LightShader->Render(m_Direct3D->GetDeviceContext(), m_Model->GetIndexCount(), worldMatrix, viewMatrix, projectionMatrix, m_Model->GetTexture(), - m_Light->GetDirection(), m_Light->GetDiffuseColor()); - if (!result) - { - return false; + result = m_LightShader->Render(m_Direct3D->GetDeviceContext(), cube->GetIndexCount(), worldMatrix, viewMatrix, projectionMatrix, cube->GetTexture(), + m_Light->GetDirection(), m_Light->GetDiffuseColor()); + if (!result) + { + return false; + } } // Present the rendered scene to the screen. @@ -220,3 +216,22 @@ int ApplicationClass::GetScreenHeight() const { return GetSystemMetrics(SM_CYSCREEN); } + +void ApplicationClass::AddCube() +{ + char modelFilename[128]; + char textureFilename[128]; + + // Set the file name of the model. + strcpy_s(modelFilename, "cube.txt"); + + // Set the name of the texture file that we will be loading. + strcpy_s(textureFilename, "stone01.tga"); + + Object* newCube = new Object(); + newCube->Initialize(m_Direct3D->GetDevice(), m_Direct3D->GetDeviceContext(), modelFilename, textureFilename); + + newCube->SetTranslateMatrix(XMMatrixTranslation(0.0f, 0.0f, 0.0f)); + + m_cubes.push_back(newCube); +} \ No newline at end of file diff --git a/enginecustom/applicationclass.h b/enginecustom/applicationclass.h index 84a476b..679784d 100644 --- a/enginecustom/applicationclass.h +++ b/enginecustom/applicationclass.h @@ -7,9 +7,10 @@ /////////////////////// #include "d3dclass.h" #include "cameraclass.h" -#include "modelclass.h" +#include "object.h" #include "lightshaderclass.h" #include "lightclass.h" +#include ///////////// // GLOBALS // @@ -41,6 +42,9 @@ public: float GetSpeed() const { return speed; }; void SetSpeed(float speed) { this->speed = speed; }; + void AddCube(); + int GetCubeCount() const { return m_cubes.size(); }; + private: bool Render(float); @@ -52,6 +56,7 @@ private: LightShaderClass* m_LightShader; LightClass* m_Light; float speed = 0.1f; + std::vector m_cubes; }; #endif diff --git a/enginecustom/enginecustom.vcxproj b/enginecustom/enginecustom.vcxproj index f80b1f4..eaa80c3 100644 --- a/enginecustom/enginecustom.vcxproj +++ b/enginecustom/enginecustom.vcxproj @@ -37,6 +37,7 @@ + @@ -58,6 +59,7 @@ + diff --git a/enginecustom/imgui.ini b/enginecustom/imgui.ini index e04071a..62501f1 100644 --- a/enginecustom/imgui.ini +++ b/enginecustom/imgui.ini @@ -3,6 +3,6 @@ Pos=60,60 Size=400,400 [Window][Khaotic Engine] -Pos=21,21 +Pos=522,44 Size=694,367 diff --git a/enginecustom/imguiManager.cpp b/enginecustom/imguiManager.cpp index 0ef131b..3142e48 100644 --- a/enginecustom/imguiManager.cpp +++ b/enginecustom/imguiManager.cpp @@ -1,4 +1,5 @@ #include "imguiManager.h" +#include "applicationclass.h" imguiManager::imguiManager() { @@ -62,7 +63,15 @@ void imguiManager::WidgetFPS() ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / io->Framerate, io->Framerate); } -void imguiManager::WidgetFullscreen(bool* fullscreen) +void imguiManager::WidgetAddObject(ApplicationClass* app) { - ImGui::Checkbox("Fullscreen", fullscreen); + if (ImGui::CollapsingHeader("Objects")) + { + if (ImGui::Button("Add Cube")) + { + app->AddCube(); + } + ImGui::SameLine(); + ImGui::Text("Number of cubes: %d", app->GetCubeCount()); + } } \ No newline at end of file diff --git a/enginecustom/imguiManager.h b/enginecustom/imguiManager.h index 84ed906..9cb406f 100644 --- a/enginecustom/imguiManager.h +++ b/enginecustom/imguiManager.h @@ -7,6 +7,8 @@ #include #include +class ApplicationClass; + class imguiManager { public: @@ -22,7 +24,7 @@ public: void WidgetSpeedSlider(float* speed); void WidgetButton(); void WidgetFPS(); - void WidgetFullscreen(bool* fullscreen); + void WidgetAddObject(ApplicationClass* app); private: ImGuiIO* io; diff --git a/enginecustom/object.cpp b/enginecustom/object.cpp new file mode 100644 index 0000000..6a2ce8d --- /dev/null +++ b/enginecustom/object.cpp @@ -0,0 +1,64 @@ +#include "object.h" + +Object::Object() : ModelClass() +{ + m_scaleMatrix = XMMatrixIdentity(); + m_rotateMatrix = XMMatrixIdentity(); + m_translateMatrix = XMMatrixIdentity(); + m_srMatrix = XMMatrixIdentity(); + m_worldMatrix = XMMatrixIdentity(); +} + +Object::~Object() +{ +} + +void Object::SetScaleMatrix(XMMATRIX scaleMatrix) +{ + m_scaleMatrix = scaleMatrix; +} + +void Object::SetRotateMatrix(XMMATRIX rotateMatrix) +{ + m_rotateMatrix = rotateMatrix; +} + +void Object::SetTranslateMatrix(XMMATRIX translateMatrix) +{ + m_translateMatrix = translateMatrix; +} + +void Object::SetSRMatrix(XMMATRIX srMatrix) +{ + m_srMatrix = srMatrix; +} + +void Object::SetWorldMatrix(XMMATRIX worldMatrix) +{ + m_worldMatrix = worldMatrix; +} + +XMMATRIX Object::GetScaleMatrix() +{ + return m_scaleMatrix; +} + +XMMATRIX Object::GetRotateMatrix() +{ + return m_rotateMatrix; +} + +XMMATRIX Object::GetTranslateMatrix() +{ + return m_translateMatrix; +} + +XMMATRIX Object::GetSRMatrix() +{ + return m_srMatrix; +} + +XMMATRIX Object::GetWorldMatrix() +{ + return m_worldMatrix; +} diff --git a/enginecustom/object.h b/enginecustom/object.h new file mode 100644 index 0000000..cd62595 --- /dev/null +++ b/enginecustom/object.h @@ -0,0 +1,28 @@ +#pragma once +#include "modelclass.h" + +class Object : public ModelClass +{ +public: + Object(); + ~Object(); + + void SetScaleMatrix(XMMATRIX scaleMatrix); + void SetRotateMatrix(XMMATRIX rotateMatrix); + void SetTranslateMatrix(XMMATRIX translateMatrix); + void SetSRMatrix(XMMATRIX srMatrix); + void SetWorldMatrix(XMMATRIX worldMatrix); + + XMMATRIX GetScaleMatrix(); + XMMATRIX GetRotateMatrix(); + XMMATRIX GetTranslateMatrix(); + XMMATRIX GetSRMatrix(); + XMMATRIX GetWorldMatrix(); + +private: + XMMATRIX m_scaleMatrix; + XMMATRIX m_rotateMatrix; + XMMATRIX m_translateMatrix; + XMMATRIX m_srMatrix; + XMMATRIX m_worldMatrix; +};