Chronos 0.0
A advanced 2D rendering and animation system
Loading...
Searching...
No Matches
objectManager.cpp
Go to the documentation of this file.
1/*
2Copyright (c) 2024 Rahul Satish Vadhyar
3
4Permission is hereby granted, free of charge, to any person obtaining a copy
5of this software and associated documentation files (the "Software"), to deal
6in the Software without restriction, including without limitation the rights
7to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8copies of the Software, and to permit persons to whom the Software is
9furnished to do so, subject to the following conditions:
10
11The above copyright notice and this permission notice shall be included in all
12copies or substantial portions of the Software.
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20SOFTWARE.
21*/
22
23#include "objectManager.hpp"
24#include "helper.hpp"
25#include "texture.hpp"
26#include "ColoredRectangle.hpp"
27#include "TexturedRectangle.hpp"
28#include "text.hpp"
30 Chronos::Engine::SwapChain* swapChain, VkCommandPool commandPool)
31{
32 this->device = device;
33 this->swapChain = swapChain;
34 this->commandPool = commandPool;
35
37
38 this->renderPass
39 = Chronos::Engine::createRenderPass(*this->device, *this->swapChain,
40 VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
41 VK_IMAGE_LAYOUT_PRESENT_SRC_KHR, true, true, false);
45 *device, *swapChain, renderPass, true);
46}
48{
49 vkDestroySampler(device->device, textureSampler, nullptr);
50 vkDestroyRenderPass(device->device, renderPass, nullptr);
51 for (auto framebuffer : framebuffers)
52 vkDestroyFramebuffer(device->device, framebuffer, nullptr);
53 for (auto& objectMap : objects) {
54 objectMap.second->destroy();
55 }
56}
58 uint32_t currentFrame, uint32_t imageIndex, float bgColor[3])
59{
60 VkViewport viewport {};
61 viewport.x = 0.0f;
62 viewport.y = 0.0f;
63 viewport.width = static_cast<float>(swapChain->swapChainExtent.width);
64 viewport.height = static_cast<float>(swapChain->swapChainExtent.height);
65 viewport.minDepth = 0.0f;
66 viewport.maxDepth = 1.0f;
67
68 VkRect2D scissor {};
69 scissor.offset = { 0, 0 };
70 scissor.extent = swapChain->swapChainExtent;
71
72 vkResetCommandBuffer(commandBuffers[currentFrame], 0);
73 VkCommandBufferBeginInfo beginInfo {};
74 beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
75 if (vkBeginCommandBuffer(commandBuffers[currentFrame], &beginInfo)
76 != VK_SUCCESS) {
77 throw std::runtime_error("failed to begin recording command buffer!");
78 }
79
80 VkRenderPassBeginInfo renderPassInfo {};
81 renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
82 renderPassInfo.renderPass = renderPass;
83 renderPassInfo.framebuffer = framebuffers[imageIndex];
84 renderPassInfo.renderArea.offset = { 0, 0 };
85 renderPassInfo.renderArea.extent = swapChain->swapChainExtent;
86 VkClearValue clearColor
87 = { { { bgColor[0], bgColor[1], bgColor[2], 1.0f } } }; // bg color
88 renderPassInfo.clearValueCount = 1;
89 renderPassInfo.pClearValues = &clearColor;
90
91 vkCmdBeginRenderPass(commandBuffers[currentFrame], &renderPassInfo,
92 VK_SUBPASS_CONTENTS_INLINE);
93
94 for (auto& object : this->objects) {
95 if (this->objectsToBeRemoved.count(object.first) > 0)
96 continue;
97 object.second->render(currentFrame, imageIndex, bgColor, viewport,
98 scissor, commandBuffers);
99 }
100
101 vkCmdEndRenderPass(commandBuffers[currentFrame]);
102
103 if (vkEndCommandBuffer(commandBuffers[currentFrame]) != VK_SUCCESS) {
104 throw std::runtime_error("failed to record command buffer!");
105 }
106}
108{
109 vkDestroyRenderPass(device->device, renderPass, nullptr);
110 this->renderPass
111 = Chronos::Engine::createRenderPass(*this->device, *this->swapChain,
112 VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
113 VK_IMAGE_LAYOUT_PRESENT_SRC_KHR, true, true, false);
114 recreate();
115 for (auto& objectMap : objects) {
116 objectMap.second->recreateGraphicsPipeline();
117 }
118}
120{
121 cleanup();
122 framebuffers = createFramebuffer(*device, *swapChain, renderPass, true);
123}
125{
126 for (auto framebuffer : framebuffers)
127 vkDestroyFramebuffer(device->device, framebuffer, nullptr);
128}
129
131{
132 int objectNo = nextFreeObjectNo;
133 nextFreeObjectNo++;
134 objects[objectNo] = object;
135 return objectNo;
136}
138{
139 for (bool& flag : objectsToBeRemoved[objectNo]) {
140 flag = true;
141 }
142}
143void Chronos::Engine::ObjectManager::update(uint32_t currentFrame)
144{
145 for (auto& objectMap : objectsToBeRemoved) {
146 objectMap.second[currentFrame] = false;
147 bool toBeRemoved = true;
148 for (bool flag : objectMap.second) {
149 if (flag) {
150 toBeRemoved = false;
151 break;
152 }
153 }
154 if (toBeRemoved) {
155 objects[objectMap.first]->destroy();
156 objectsToBeRemoved.erase(objectMap.first);
157 objects.erase(objectMap.first);
158 Object* object = objects[objectMap.first];
159 switch (object->objectType) {
161 delete (Chronos::Engine::ColoredRectangle*)object;
162 break;
165 break;
167 delete (Chronos::Engine::Text*)object;
168 break;
169 default:
170 throw std::runtime_error("Invalid object type");
171 }
172 }
173 }
174 for (auto& objectMap : objects) {
175 if (objectsToBeRemoved.count(objectMap.first) == 0) {
176 objectMap.second->update(currentFrame);
177 }
178 }
179}
Contains the class for creating a rectangle filled with texture.
Contains the class for creating a rectangle filled with texture.
This initializes, manages and destroys the logical and physical devices(GPU).
Definition device.hpp:47
void render(uint32_t currentFrame, uint32_t imageIndex, float bgColor[3])
Chronos::Engine::Device * device
std::vector< VkCommandBuffer > commandBuffers
void init(Chronos::Engine::Device *device, Chronos::Engine::SwapChain *swapChain, VkCommandPool commandPool)
std::vector< VkFramebuffer > framebuffers
Chronos::Engine::SwapChain * swapChain
void update(uint32_t currentFrame)
Abstract class for creating and managing generic graphical objects.
Definition object.hpp:66
virtual void destroy()=0
Destroys the object and releases associated resources.
Definition object.cpp:235
Chronos::Engine::ObjectType objectType
Definition object.hpp:137
Class for creating a text object for rendering text.
Definition text.hpp:86
Contains various common functions used by other classes in the Engine namespace.
void createTextureSampler(Chronos::Engine::Device device, VkSampler *textureSampler)
Creates a VkSampler
Definition texture.cpp:278
std::vector< VkFramebuffer > createFramebuffer(Chronos::Engine::Device device, Chronos::Engine::SwapChain swapChain, VkRenderPass renderPass, bool msaa)
Creates a set of framebuffers for use.
Definition helper.cpp:276
std::vector< VkCommandBuffer > createCommandBuffer(Chronos::Engine::Device device, Chronos::Engine::SwapChain swapChain, VkCommandPool commandPool)
Creates a set of command buffers for use.
Definition helper.cpp:308
VkRenderPass createRenderPass(Chronos::Engine::Device device, Chronos::Engine::SwapChain swapChain, VkImageLayout initalLayout, VkImageLayout finalLayout, VkImageLayout msaaFinalLayout, bool msaa, bool clearFramebuffer, bool dependency)
Creates a render pass.
Definition helper.cpp:175
Defines the ObjectManager class for managing objects in the Chronos::Engine namespace.
Contains the functions for image manipulation along with the Texture class.