Added Deconstructor for Mesh

This commit is contained in:
OusmBlueNinja 2024-12-31 16:35:06 -06:00
parent b6aa69210f
commit dbd23a30fc
5 changed files with 106 additions and 63 deletions

View File

@ -1,6 +1,6 @@
[Window][DockSpace] [Window][DockSpace]
Pos=0,0 Pos=0,0
Size=1280,720 Size=1920,1177
Collapsed=0 Collapsed=0
[Window][Debug##Default] [Window][Debug##Default]
@ -68,26 +68,26 @@ Size=1920,1177
Collapsed=0 Collapsed=0
[Window][Inspector##InspectorWindow] [Window][Inspector##InspectorWindow]
Pos=896,27 Pos=1536,27
Size=376,685 Size=376,1142
Collapsed=0 Collapsed=0
DockId=0x00000016,0 DockId=0x00000016,0
[Window][Editor##EditorWindow] [Window][Editor##EditorWindow]
Pos=332,27 Pos=332,27
Size=562,319 Size=1202,776
Collapsed=0 Collapsed=0
DockId=0x00000017,0 DockId=0x00000017,0
[Window][Performance##performance] [Window][Performance##performance]
Pos=8,468 Pos=8,761
Size=322,244 Size=322,408
Collapsed=0 Collapsed=0
DockId=0x0000001C,0 DockId=0x0000001C,0
[Window][Logger##logger] [Window][Logger##logger]
Pos=332,348 Pos=332,805
Size=562,364 Size=1202,364
Collapsed=0 Collapsed=0
DockId=0x00000019,0 DockId=0x00000019,0
@ -105,7 +105,7 @@ DockId=0x0000000F,0
[Window][Scene Window##SceneWindow] [Window][Scene Window##SceneWindow]
Pos=8,27 Pos=8,27
Size=322,439 Size=322,732
Collapsed=0 Collapsed=0
DockId=0x0000001B,0 DockId=0x0000001B,0
@ -134,7 +134,7 @@ Column 2 Weight=0.6474
Column 3 Weight=1.0088 Column 3 Weight=1.0088
[Docking][Data] [Docking][Data]
DockSpace ID=0x14621557 Window=0x3DA2F1DE Pos=42,84 Size=1264,685 Split=X Selected=0xF7365A5A DockSpace ID=0x14621557 Window=0x3DA2F1DE Pos=8,50 Size=1904,1142 Split=X Selected=0xF7365A5A
DockNode ID=0x00000013 Parent=0x14621557 SizeRef=322,1142 Split=Y Selected=0x818D04BB DockNode ID=0x00000013 Parent=0x14621557 SizeRef=322,1142 Split=Y Selected=0x818D04BB
DockNode ID=0x0000001B Parent=0x00000013 SizeRef=264,456 HiddenTabBar=1 Selected=0x1D5D92B6 DockNode ID=0x0000001B Parent=0x00000013 SizeRef=264,456 HiddenTabBar=1 Selected=0x1D5D92B6
DockNode ID=0x0000001C Parent=0x00000013 SizeRef=264,254 HiddenTabBar=1 Selected=0x818D04BB DockNode ID=0x0000001C Parent=0x00000013 SizeRef=264,254 HiddenTabBar=1 Selected=0x818D04BB

View File

@ -16,6 +16,47 @@ MeshComponent::MeshComponent()
{ {
} }
MeshComponent::~MeshComponent()
{
for (auto &submesh : submeshes)
{
// Delete OpenGL buffers associated with the submesh
if (submesh.vbo != 0)
{
glDeleteBuffers(1, &submesh.vbo);
submesh.vbo = 0;
}
if (submesh.ebo != 0)
{
glDeleteBuffers(1, &submesh.ebo);
submesh.ebo = 0;
}
if (submesh.vao != 0)
{
glDeleteVertexArrays(1, &submesh.vao);
submesh.vao = 0;
}
// Clear textures associated with the submesh
for (const auto &texture : submesh.textures)
{
if (texture.id != 0)
{
glDeleteTextures(1, &texture.id);
}
}
// Clear submesh data
submesh.textures.clear();
submesh.vertices.clear();
submesh.indices.clear();
}
// Clear the submesh container
submeshes.clear();
}
const std::string &MeshComponent::GetName() const const std::string &MeshComponent::GetName() const
{ {
return name; return name;
@ -26,6 +67,7 @@ const std::string &MeshComponent::GetStaticName()
return name; return name;
} }
void MeshComponent::Update(float deltaTime) void MeshComponent::Update(float deltaTime)
{ {
(void)deltaTime; (void)deltaTime;

View File

@ -23,6 +23,8 @@ public:
MeshComponent(); MeshComponent();
~MeshComponent();
virtual const std::string& GetName() const override; virtual const std::string& GetName() const override;
static const std::string& GetStaticName(); static const std::string& GetStaticName();

View File

@ -194,23 +194,23 @@ public:
std::string key = generateKey(type, path); std::string key = generateKey(type, path);
// 2) Check if its already loaded // 2) Check if its already loaded
auto it = m_AssetMap.find(key); // auto it = m_AssetMap.find(key);
if (it != m_AssetMap.end()) // if (it != m_AssetMap.end())
{ //{
// Debug: Log the variant type // // Debug: Log the variant type
if (std::holds_alternative<std::shared_ptr<T>>(it->second)) // if (std::holds_alternative<std::shared_ptr<T>>(it->second))
{ // {
#ifdef DEBUG // #ifdef DEBUG
DebugAssetMap(); // DebugAssetMap();
#endif // #endif
std::cout << "[AssetManager] Retrieved asset from cache: " << key << std::endl; // std::cout << "[AssetManager] Retrieved asset from cache: " << key << std::endl;
return std::get<std::shared_ptr<T>>(it->second); // return std::get<std::shared_ptr<T>>(it->second);
} // }
else // else
{ // {
throw std::runtime_error("Asset type mismatch for key: " + key); // throw std::runtime_error("Asset type mismatch for key: " + key);
} // }
} //}
// 3) Not loaded yet // 3) Not loaded yet
AssetVariant assetData = loadAssetFromDisk(type, path); AssetVariant assetData = loadAssetFromDisk(type, path);

View File

@ -27,11 +27,8 @@ extern std::vector<std::shared_ptr<GameObject>> g_GameObjects;
// Extern reference to our global (or extern) asset manager // Extern reference to our global (or extern) asset manager
extern AssetManager g_AssetManager; extern AssetManager g_AssetManager;
extern std::shared_ptr<CameraComponent> g_RuntimeCameraObject; extern std::shared_ptr<CameraComponent> g_RuntimeCameraObject;
extern int g_GPU_Triangles_drawn_to_screen; extern int g_GPU_Triangles_drawn_to_screen;
// Example cube data (position + UVs) // Example cube data (position + UVs)
@ -185,16 +182,12 @@ static unsigned int g_CubeIndices[] =
// Bottom // Bottom
20, 21, 22, 22, 23, 20}; 20, 21, 22, 22, 23, 20};
bool PlayPauseButton(const char *label, bool *isPlaying, ImVec2 Size)
bool PlayPauseButton(const char* label, bool* isPlaying)
{ {
// Define button size // Define button size
ImVec2 buttonSize = ImVec2(50, 50); // Adjust size as needed
// Begin the button // Begin the button
if (ImGui::Button(label, buttonSize)) if (ImGui::Button(label, Size))
{ {
// Toggle the state // Toggle the state
*isPlaying = !(*isPlaying); *isPlaying = !(*isPlaying);
@ -208,7 +201,7 @@ bool PlayPauseButton(const char* label, bool* isPlaying)
} }
// Get the current window's draw list // Get the current window's draw list
ImDrawList* draw_list = ImGui::GetWindowDrawList(); ImDrawList *draw_list = ImGui::GetWindowDrawList();
// Get the position of the button // Get the position of the button
ImVec2 button_pos = ImGui::GetItemRectMin(); ImVec2 button_pos = ImGui::GetItemRectMin();
@ -216,7 +209,7 @@ bool PlayPauseButton(const char* label, bool* isPlaying)
ImVec2 center = ImVec2(button_pos.x + button_size.x * 0.5f, button_pos.y + button_size.y * 0.5f); ImVec2 center = ImVec2(button_pos.x + button_size.x * 0.5f, button_pos.y + button_size.y * 0.5f);
// Define icon size // Define icon size
float icon_size = 20.0f; float icon_size = 0.4f * Size.x;
float half_icon_size = icon_size / 2.0f; float half_icon_size = icon_size / 2.0f;
// Define colors // Define colors
@ -226,7 +219,7 @@ bool PlayPauseButton(const char* label, bool* isPlaying)
{ {
// Draw Pause Icon (two vertical bars) // Draw Pause Icon (two vertical bars)
float bar_width = 4.0f; float bar_width = 4.0f;
float spacing = 6.0f; float spacing = 0.1f * Size.x;
// Left bar // Left bar
ImVec2 left_bar_p1 = ImVec2(center.x - spacing - bar_width, center.y - half_icon_size); ImVec2 left_bar_p1 = ImVec2(center.x - spacing - bar_width, center.y - half_icon_size);
@ -252,37 +245,28 @@ bool PlayPauseButton(const char* label, bool* isPlaying)
void RenderWindow::Show(bool *GameRunning) void RenderWindow::Show(bool *GameRunning)
{ {
ImGui::Begin("Editor##EditorWindow"); ImGui::Begin("Editor##EditorWindow");
ImVec2 size = ImGui::GetContentRegionAvail();
int w = static_cast<int>(size.x);
int h = static_cast<int>(size.y);
if (!m_Initialized) if (!m_Initialized)
{ {
InitGLResources(); InitGLResources();
m_Initialized = true; m_Initialized = true;
} }
// Center the button ImVec2 size = ImGui::GetContentRegionAvail();
ImGui::SetCursorPosX((ImGui::GetWindowWidth() - 60) * 0.5f); int w = static_cast<int>(size.x);
int h = static_cast<int>(size.y);
// Render the Play/Pause button
// Render the Play/Pause button
PlayPauseButton("##PlayPauseButton", GameRunning);
// If there's space, render to the FBO, then show it as an ImGui image // If there's space, render to the FBO, then show it as an ImGui image
if (w > 0 && h > 0) if (w > 0 && h > 0)
{ {
if (w != m_LastWidth || h != m_LastHeight) if (w != m_LastWidth || h != m_LastHeight)
{ {
m_FBO.Create(w, h); m_FBO.Create(w, h);
m_LastWidth = w; m_LastWidth = w;
m_LastHeight = h; m_LastHeight = h;
@ -290,18 +274,39 @@ void RenderWindow::Show(bool *GameRunning)
RenderSceneToFBO(GameRunning); RenderSceneToFBO(GameRunning);
// Render the image first
ImGui::Image(m_FBO.GetTextureID(), size, ImVec2(0, 0), ImVec2(1, 1)); ImGui::Image(m_FBO.GetTextureID(), size, ImVec2(0, 0), ImVec2(1, 1));
// Calculate button position to place it slightly right and down from the top-left of the image
ImVec2 imagePos = ImGui::GetItemRectMin();
// Add an offset to position the button
ImVec2 buttonOffset(10.0f, 10.0f); // Adjust these values as needed for the desired offset
ImVec2 buttonPos = ImVec2(imagePos.x + buttonOffset.x, imagePos.y + buttonOffset.y);
// Set cursor position for the button
ImGui::SetCursorScreenPos(buttonPos);
// Dynamically calculate button size based on window size
float buttonWidth = size.x * 0.03f; // 5% of the window width
ImVec2 buttonSize = ImVec2(buttonWidth, buttonWidth);
// Render the Play/Pause button with the calculated size
PlayPauseButton("##PlayPauseButton", GameRunning, buttonSize);
} }
else else
{ {
ImGui::Text("No space to render."); ImGui::Text("No space to render.");
} }
ImGui::End(); ImGui::End();
} }
void RenderWindow::InitGLResources() void RenderWindow::InitGLResources()
{ {
// ---------------------------------------------------- // ----------------------------------------------------
@ -365,7 +370,6 @@ void RenderWindow::InitGLResources()
// ---------------------------------------------------- // ----------------------------------------------------
} }
void CheckOpenGLError(const std::string &location) void CheckOpenGLError(const std::string &location)
{ {
GLenum err; GLenum err;
@ -381,11 +385,6 @@ void CheckOpenGLError(const std::string &location)
} }
} }
#include <glm/gtc/type_ptr.hpp> // For glm::value_ptr #include <glm/gtc/type_ptr.hpp> // For glm::value_ptr
#include <algorithm> // Ensure <algorithm> is included #include <algorithm> // Ensure <algorithm> is included
@ -504,7 +503,7 @@ void RenderWindow::RenderSceneToFBO(bool *GameRunning)
} }
// Assign default texture to unused texture slots to prevent shader errors // Assign default texture to unused texture slots to prevent shader errors
for(int i = textureUnit; i < MAX_DIFFUSE; ++i) for (int i = textureUnit; i < MAX_DIFFUSE; ++i)
{ {
std::string uniformName = "uTextures.texture_diffuse[" + std::to_string(i) + "]"; std::string uniformName = "uTextures.texture_diffuse[" + std::to_string(i) + "]";
m_ShaderPtr->SetInt(uniformName, 0); // Assign texture unit 0 (ensure texture 0 is a valid default) m_ShaderPtr->SetInt(uniformName, 0); // Assign texture unit 0 (ensure texture 0 is a valid default)