Skip to content

Commit b255c81

Browse files
committed
add an Application class
1 parent 13ee6f9 commit b255c81

File tree

6 files changed

+106
-65
lines changed

6 files changed

+106
-65
lines changed

CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@ add_executable(Reactor src/core/main.cpp
5353
src/core/Camera.cpp
5454
src/core/Camera.hpp
5555
src/core/OrbitController.cpp
56-
src/core/OrbitController.hpp)
56+
src/core/OrbitController.hpp
57+
src/core/Application.cpp
58+
src/core/Application.hpp)
5759

5860
target_link_libraries(Reactor PRIVATE Vulkan::Vulkan Vulkan::Headers glfw fmt::fmt spdlog::spdlog GPUOpen::VulkanMemoryAllocator glm::glm)
5961
target_link_libraries(Reactor PRIVATE imgui::imgui)

src/core/Application.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include "Application.hpp"
2+
3+
namespace reactor {
4+
5+
Application::Application() {
6+
initialize();
7+
}
8+
9+
Application::~Application() = default;
10+
11+
void Application::initialize() {
12+
m_eventManager = std::make_unique<EventManager>();
13+
14+
m_window = std::make_unique<Window>(1280, 720, "Reactor", *m_eventManager);
15+
m_camera = std::make_unique<Camera>();
16+
m_orbitController = std::make_unique<OrbitController>(*m_camera);
17+
18+
// subscribe controller to input events.
19+
m_eventManager->subscribe(EventType::MouseMoved, m_orbitController.get());
20+
m_eventManager->subscribe(EventType::MouseButtonPressed, m_orbitController.get());
21+
m_eventManager->subscribe(EventType::MouseButtonReleased, m_orbitController.get());
22+
23+
RendererConfig config{
24+
.windowWidth = 1280,
25+
.windowHeight = 720,
26+
.windowTitle = "Reactor",
27+
.vertShaderPath = "../resources/shaders/triangle.vert.spv",
28+
.fragShaderPath = "../resources/shaders/triangle.frag.spv",
29+
.compositeVertShaderPath = "../resources/shaders/composite.vert.spv",
30+
.compositeFragShaderPath = "../resources/shaders/composite.frag.spv"
31+
};
32+
33+
m_renderer = std::make_unique<VulkanRenderer>(config, *m_window, *m_camera);
34+
}
35+
36+
void Application::run() {
37+
while (!m_window->shouldClose()) {
38+
m_window->pollEvents();
39+
m_renderer->drawFrame();
40+
}
41+
}
42+
43+
44+
} // reactor

src/core/Application.hpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#pragma once
2+
3+
#include "Camera.hpp"
4+
#include "EventManager.hpp"
5+
#include "OrbitController.hpp"
6+
#include "Window.hpp"
7+
#include "../vulkan/VulkanRenderer.hpp"
8+
#include <memory>
9+
10+
namespace reactor {
11+
12+
class Application {
13+
public:
14+
Application();
15+
~Application();
16+
17+
void run();
18+
19+
private:
20+
void initialize();
21+
22+
std::unique_ptr<EventManager> m_eventManager;
23+
std::unique_ptr<Window> m_window;
24+
std::unique_ptr<Camera> m_camera;
25+
std::unique_ptr<OrbitController> m_orbitController;
26+
std::unique_ptr<VulkanRenderer> m_renderer;
27+
28+
};
29+
30+
} // namespace reactor
31+

src/core/main.cpp

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,16 @@
11

22
#include "../vulkan/VulkanContext.hpp"
33
#include "../vulkan/VulkanRenderer.hpp"
4+
#include "Application.hpp"
45
#include "Window.hpp"
56

67
int main() {
78

8-
reactor::RendererConfig config;
9-
config.windowWidth = 1280;
10-
config.windowHeight = 720;
11-
config.windowTitle = "Reactor";
12-
config.vertShaderPath = "../resources/shaders/triangle.vert.spv";
13-
config.fragShaderPath = "../resources/shaders/triangle.frag.spv";
14-
config.compositeVertShaderPath = "../resources/shaders/composite.vert.spv";
15-
config.compositeFragShaderPath = "../resources/shaders/composite.frag.spv";
16-
17-
reactor::VulkanRenderer renderer(config);
18-
renderer.run();
19-
return 0;
9+
try {
10+
reactor::Application app;
11+
app.run();
12+
} catch (const std::exception& e) {
13+
return EXIT_FAILURE;
14+
}
15+
return EXIT_SUCCESS;
2016
}

src/vulkan/VulkanRenderer.cpp

Lines changed: 14 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66

77
#include <glm/glm.hpp>
88
#include <glm/gtc/matrix_transform.hpp>
9-
#include <utility>
109

1110
namespace reactor {
12-
VulkanRenderer::VulkanRenderer(RendererConfig config) : m_config(std::move(config)) {
11+
VulkanRenderer::VulkanRenderer(const RendererConfig& config, Window& window, Camera& camera)
12+
: m_config(config), m_window(window), m_camera(camera) {
1313
createCoreVulkanObjects();
1414
createSwapchainAndFrameManager();
1515

@@ -25,29 +25,17 @@ VulkanRenderer::VulkanRenderer(RendererConfig config) : m_config(std::move(confi
2525
createSampler();
2626
createDescriptorSets();
2727

28-
m_camera = std::make_unique<Camera>();
29-
m_orbitController = std::make_unique<OrbitController>(*m_camera);
30-
m_eventManager->subscribe(EventType::MouseMoved, m_orbitController.get());
31-
m_eventManager->subscribe(EventType::MouseButtonPressed, m_orbitController.get());
32-
m_eventManager->subscribe(EventType::MouseButtonReleased, m_orbitController.get());
3328
}
3429

3530
void VulkanRenderer::createCoreVulkanObjects() {
36-
m_eventManager = std::make_unique<EventManager>();
37-
m_window =
38-
std::make_unique<Window>(
39-
m_config.windowWidth,
40-
m_config.windowHeight,
41-
m_config.windowTitle,
42-
*m_eventManager);
43-
m_context = std::make_unique<VulkanContext>(m_window->getNativeWindow());
31+
m_context = std::make_unique<VulkanContext>(m_window.getNativeWindow());
4432
m_allocator = std::make_unique<Allocator>(m_context->physicalDevice(), m_context->device(),
4533
m_context->instance());
4634
}
4735

4836
void VulkanRenderer::createSwapchainAndFrameManager() {
4937
m_swapchain = std::make_unique<Swapchain>(m_context->device(), m_context->physicalDevice(),
50-
m_context->surface(), *m_window);
38+
m_context->surface(), m_window);
5139

5240
uint32_t swapchainImageCount = m_swapchain->getImageViews().size();
5341
m_frameManager = std::make_unique<FrameManager>(m_context->device(), *m_allocator, 0, 2,
@@ -89,36 +77,30 @@ void VulkanRenderer::createPipelineAndDescriptors() {
8977
}
9078

9179
void VulkanRenderer::handleSwapchainResizing() {
92-
if (m_window->wasResized()) {
93-
vk::Extent2D size = m_window->getFramebufferSize();
80+
if (m_window.wasResized()) {
81+
vk::Extent2D size = m_window.getFramebufferSize();
9482
while (size.width == 0 || size.height == 0) {
9583
Window::waitEvents();
96-
size = m_window->getFramebufferSize();
84+
size = m_window.getFramebufferSize();
9785
}
9886
m_context->device().waitIdle();
9987
m_swapchain->recreate();
100-
m_window->resetResizedFlag();
88+
m_window.resetResizedFlag();
10189
}
10290
}
10391

104-
void VulkanRenderer::setupUI() { m_imgui = std::make_unique<Imgui>(*m_context, *m_window); }
92+
void VulkanRenderer::setupUI() { m_imgui = std::make_unique<Imgui>(*m_context, m_window); }
10593

10694
VulkanRenderer::~VulkanRenderer() {
95+
96+
m_context->device().waitIdle();
97+
10798
for (auto i = 0; i < m_frameManager->getFramesInFlightCount(); ++i) {
10899
m_context->device().destroyImageView(m_msaaColorViews[i]);
109100
m_context->device().destroyImageView(m_resolveViews[i]);
110101
}
111102
}
112103

113-
void VulkanRenderer::run() {
114-
while (!m_window->shouldClose()) {
115-
Window::pollEvents();
116-
drawFrame();
117-
}
118-
119-
m_context->device().waitIdle();
120-
}
121-
122104
void VulkanRenderer::beginCommandBuffer(vk::CommandBuffer cmd) {
123105
cmd.begin({vk::CommandBufferUsageFlagBits::eOneTimeSubmit});
124106
}
@@ -221,8 +203,8 @@ void VulkanRenderer::drawFrame() {
221203
utils::setupViewportAndScissor(cmd, extent);
222204

223205
SceneUBO ubo{};
224-
ubo.view = m_camera->getView();
225-
ubo.projection = m_camera->getProjection();
206+
ubo.view = m_camera.getView();
207+
ubo.projection = m_camera.getProjection();
226208

227209
m_uniformManager->update<SceneUBO>(frameIdx, ubo);
228210

src/vulkan/VulkanRenderer.hpp

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
1-
//
2-
// Created by Robert F. Dickerson on 6/9/25.
3-
//
1+
#pragma once
42

5-
#ifndef VULKANRENDERER_HPP
6-
#define VULKANRENDERER_HPP
73
#include <memory>
84

95
#include "../core/Camera.hpp"
10-
#include "../core/EventManager.hpp"
11-
#include "../core/OrbitController.hpp"
126
#include "../core/Window.hpp"
137
#include "../imgui/Imgui.hpp"
148
#include "Allocator.hpp"
@@ -34,22 +28,19 @@ namespace reactor {
3428
std::string compositeFragShaderPath;
3529
};
3630

37-
38-
3931
class VulkanRenderer {
4032
public:
41-
explicit VulkanRenderer(RendererConfig config);
33+
VulkanRenderer(const RendererConfig& config, Window& window, Camera& camera);
4234
~VulkanRenderer();
4335

44-
void run();
45-
4636
void drawFrame();
4737

4838
private:
49-
RendererConfig m_config{};
50-
std::unique_ptr<EventManager> m_eventManager;
39+
const RendererConfig& m_config;
40+
Window& m_window;
41+
Camera& m_camera;
42+
5143
std::unique_ptr<VulkanContext> m_context;
52-
std::unique_ptr<Window> m_window;
5344
std::unique_ptr<Swapchain> m_swapchain;
5445
std::unique_ptr<Allocator> m_allocator;
5546
std::unique_ptr<FrameManager> m_frameManager;
@@ -59,9 +50,6 @@ class VulkanRenderer {
5950
std::unique_ptr<DescriptorSet> m_compositeDescriptorSet;
6051
std::unique_ptr<Sampler> m_sampler;
6152
std::unique_ptr<UniformManager> m_uniformManager;
62-
std::unique_ptr<Camera> m_camera;
63-
std::unique_ptr<OrbitController> m_orbitController;
64-
6553
std::unique_ptr<Imgui> m_imgui;
6654

6755
ImageStateTracker m_imageStateTracker;
@@ -85,7 +73,6 @@ class VulkanRenderer {
8573
void handleSwapchainResizing();
8674
void beginCommandBuffer(vk::CommandBuffer cmd);
8775
void beginDynamicRendering(vk::CommandBuffer cmd, vk::ImageView imageView, vk::Extent2D extent, bool clear);
88-
void updateUniformBuffer(Buffer* uniformBuffer);
8976
void bindDescriptorSets(vk::CommandBuffer cmd);
9077
void drawGeometry(vk::CommandBuffer cmd);
9178
void renderUI(vk::CommandBuffer cmd) const;
@@ -98,4 +85,3 @@ class VulkanRenderer {
9885

9986
}
10087

101-
#endif //VULKANRENDERER_HPP

0 commit comments

Comments
 (0)