I’m building a video game engine using C++ as the coding language and Vulkan for…
- User
I’m building a video game engine using C++ as the coding language and Vulkan for graphics. I am trying to set up a generic renderer using Vulkan that is flexible and will render objects based on a vector that is supplied to it. The renderer will also handle the creation of the window using GLFW and use GLM for all relevant math calls. I am using the ASSIMP library to load 3d models and animations.
Here is a portion of the code:
Engine.h:
#pragma once
#include "Window.h" #include "Renderer.h" #include "Scene.h"
class Engine { public: Engine(); ~Engine();
codevoid Run(); void Shutdown();private:
codevoid Initialize(); void MainLoop(); void Update(float deltaTime); void Render(); Window window; Renderer renderer; Scene scene;};
Engine.cpp:
#include "Engine.h" #include "Terrain.h" #include
Engine::Engine() { Initialize(); }
Engine::~Engine() { Shutdown(); }
void Engine::Run() { MainLoop(); }
void Engine::Initialize() { // Initialize window, renderer, and scene window.Initialize(); renderer.Initialize(window.GetWindow()); scene.Initialize();
codeVkDescriptorSetLayout descriptorSetLayout = renderer.CreateDescriptorSetLayout(); VkDescriptorPool descriptorPool = renderer.CreateDescriptorPool(1); // Assuming only one terrain object Terrain terrain(0,10,1,renderer.GetDevice(), renderer.GetPhysicalDevice(), renderer.GetCommandPool(), renderer.GetGraphicsQueue()); terrain.GenerateTerrain(descriptorSetLayout, descriptorPool); scene.AddGameObject(terrain.GetTerrainObject()); float deltaTime = window.GetDeltaTime();}
void Engine::MainLoop() { while (!window.ShouldClose()) { window.PollEvents();
codefloat deltaTime = window.GetDeltaTime(); Update(deltaTime); Render(); }}
void Engine::Update(float deltaTime) { scene.Update(deltaTime); }
void Engine::Render() { renderer.BeginFrame(); scene.Render(renderer); renderer.EndFrame(); }
- Assistant
It seems that most of the memory management is handled correctly in the provided code. However, there are no comments or documentation for the included libraries and some of the Vulkan helper functions. I will point out a potential memory leak with the descriptor set and pool creation, and recommend some changes to clean up.
Potential memory leak:
In the
Initialize()function of yourEngineclass, you create a descriptor set layout and a descriptor pool. However, in theShutdown()function, you don't destroy them. To fix the memory leaks and ensure proper cleanup, you should store the generated VkDescriptorSetLayout and VkDescriptorPool as class members in the Engine class and destroy them in the function.