-
Notifications
You must be signed in to change notification settings - Fork 56
Simple game engine #119
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Simple game engine #119
Changes from 1 commit
12f357f
7e419d5
4644950
2edbd75
b476bd1
f1d10cf
2a350b2
2d46280
3b3dc05
6cf03fc
50d78a7
9b2c781
ddd1177
5ec2035
31ded49
659ecb1
bd98b2d
8b83ead
9b2343a
7734f21
b8a9b39
e773353
6256f77
e6b8583
e3f6fc6
4a3b450
0833166
9513597
86c396a
a62e4a9
14fc6b1
b998a13
8be59f4
be45542
8bdf3d1
e80fd23
20a9ade
d834c39
ea39e65
858b792
815aa79
bdc1845
04c8b38
cdb01db
16019ca
4b8198d
14dcb35
dd9713a
27715c0
067ea74
a5edf28
adf461e
e57bc3d
e1d7059
1f17074
acfbd6e
5e38540
a58b884
4b4a243
6ec7321
8ecae4e
345b137
11e9a65
5e6209c
25e66d5
5849fa5
f1df3d2
7eaa47e
92c7f7d
625eb71
60ee95d
23952bc
65e5db3
e564b29
7b3e016
7bf9dab
47a6baa
44e7f38
dd51a38
f713e9c
9705b60
921eed4
2a22079
41e336e
b78f8b2
5c7a132
560b9f3
6db6b9d
02baf4d
0b0dddb
ccb5c15
aa321c0
5c4ec27
b4d6226
a8d11c6
b167c70
276d1cb
6e8adb4
e1ac508
bbbae41
03d05cf
0ef0316
277a817
ce7c00d
6378bef
0e9721e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,6 +11,7 @@ | |
| #include <queue> | ||
| #include <vulkan/vulkan_raii.hpp> | ||
| #include <vulkan/vk_platform.h> | ||
| #include <stdexcept> | ||
|
|
||
| /** | ||
| * @brief Class representing an audio source. | ||
|
|
@@ -148,6 +149,13 @@ class AudioSystem { | |
| */ | ||
| AudioSystem() = default; | ||
|
|
||
| // Constructor-based initialization to replace separate Initialize() calls | ||
| AudioSystem(Engine* engine, Renderer* renderer) { | ||
| if (!Initialize(engine, renderer)) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When you call |
||
| throw std::runtime_error("AudioSystem: initialization failed"); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @brief Flush audio output: clears pending processing and device buffers so playback restarts cleanly. | ||
| */ | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,11 +13,7 @@ | |
| // @see en/Building_a_Simple_Engine/Engine_Architecture/02_architectural_patterns.adoc | ||
|
|
||
| Engine::Engine() | ||
| : resourceManager(std::make_unique<ResourceManager>()), | ||
| modelLoader(std::make_unique<ModelLoader>()), | ||
| audioSystem(std::make_unique<AudioSystem>()), | ||
| physicsSystem(std::make_unique<PhysicsSystem>()), | ||
| imguiSystem(std::make_unique<ImGuiSystem>()) { | ||
| : resourceManager(std::make_unique<ResourceManager>()) { | ||
| } | ||
|
|
||
| Engine::~Engine() { | ||
|
|
@@ -64,37 +60,25 @@ bool Engine::Initialize(const std::string& appName, int width, int height, bool | |
| return false; | ||
| } | ||
|
|
||
| // Initialize model loader | ||
| if (!modelLoader->Initialize(renderer.get())) { | ||
| return false; | ||
| } | ||
|
|
||
| // Connect model loader to renderer for light extraction | ||
| renderer->SetModelLoader(modelLoader.get()); | ||
|
|
||
| // Initialize audio system | ||
| if (!audioSystem->Initialize(this, renderer.get())) { | ||
| return false; | ||
| } | ||
|
|
||
| // Initialize a physics system | ||
| physicsSystem->SetRenderer(renderer.get()); | ||
| try { | ||
| // Model loader via constructor; also wire into renderer | ||
| modelLoader = std::make_unique<ModelLoader>(renderer.get()); | ||
| renderer->SetModelLoader(modelLoader.get()); | ||
|
|
||
| // Enable GPU acceleration for physics calculations to drastically speed up computations | ||
| physicsSystem->SetGPUAccelerationEnabled(true); | ||
| // Audio system via constructor | ||
| audioSystem = std::make_unique<AudioSystem>(this, renderer.get()); | ||
|
|
||
| if (!physicsSystem->Initialize()) { | ||
| return false; | ||
| } | ||
| // Physics system via constructor (GPU enabled) | ||
| physicsSystem = std::make_unique<PhysicsSystem>(renderer.get(), true); | ||
|
|
||
| // Initialize ImGui system | ||
| if (!imguiSystem->Initialize(renderer.get(), width, height)) { | ||
| // ImGui via constructor, then connect audio system | ||
| imguiSystem = std::make_unique<ImGuiSystem>(renderer.get(), width, height); | ||
| imguiSystem->SetAudioSystem(audioSystem.get()); | ||
| } catch (const std::exception& e) { | ||
| std::cerr << "Subsystem initialization failed: " << e.what() << std::endl; | ||
| return false; | ||
| } | ||
|
|
||
| // Connect ImGui system to an audio system for UI controls | ||
| imguiSystem->SetAudioSystem(audioSystem.get()); | ||
|
|
||
| // Generate ball material properties once at load time | ||
| GenerateBallMaterial(); | ||
|
|
||
|
|
@@ -132,9 +116,15 @@ void Engine::Run() { | |
| // Update window title with FPS and frame time every second | ||
| if (fpsUpdateTimer >= 1.0f) { | ||
| uint64_t framesSinceLastUpdate = frameCount - lastFPSUpdateFrame; | ||
| currentFPS = framesSinceLastUpdate / fpsUpdateTimer; | ||
| // Average frame time in milliseconds over the last interval | ||
| double avgMs = (fpsUpdateTimer / static_cast<double>(framesSinceLastUpdate)) * 1000.0; | ||
| double avgMs = 0.0; | ||
| if (framesSinceLastUpdate > 0 && fpsUpdateTimer > 0.0f) { | ||
| currentFPS = static_cast<float>(static_cast<double>(framesSinceLastUpdate) / static_cast<double>(fpsUpdateTimer)); | ||
| avgMs = (fpsUpdateTimer / static_cast<double>(framesSinceLastUpdate)) * 1000.0; | ||
| } else { | ||
| // Avoid divide-by-zero; keep previous FPS and estimate avgMs from last delta | ||
| currentFPS = std::max(currentFPS, 1.0f); | ||
| avgMs = static_cast<double>(deltaTimeMs.count()); | ||
| } | ||
|
|
||
| // Update window title with frame count, FPS, and frame time | ||
| std::string title = "Simple Engine - Frame: " + std::to_string(frameCount) + | ||
|
|
@@ -402,15 +392,19 @@ void Engine::Update(TimeDelta deltaTime) { | |
| UpdateCameraControls(deltaTime); | ||
| } | ||
|
|
||
| // Update all entities | ||
| // Update all entities (guard against null unique_ptrs) | ||
| for (auto& entity : entities) { | ||
| if (entity->IsActive()) { | ||
| entity->Update(deltaTime); | ||
| } | ||
| if (!entity) { continue; } | ||
| if (!entity->IsActive()) { continue; } | ||
| entity->Update(deltaTime); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe a bit easier to digest: |
||
| } | ||
| } | ||
|
|
||
| void Engine::Render() { | ||
| // Ensure renderer is ready | ||
| if (!renderer || !renderer->IsInitialized()) { | ||
| return; | ||
| } | ||
|
|
||
| // Check if we have an active camera | ||
| if (!activeCamera) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -218,7 +218,7 @@ void ImGuiSystem::NewFrame() { | |
| } | ||
|
|
||
| // Exposure slider | ||
| static float exposure = 3.0f; // Higher default for emissive lighting | ||
| static float exposure = 1.2f; // Default tuned to avoid washout | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe add some |
||
| if (ImGui::SliderFloat("Exposure", &exposure, 0.1f, 10.0f, "%.2f")) { | ||
| // Update exposure in renderer | ||
| if (renderer) { | ||
|
|
@@ -228,7 +228,7 @@ void ImGuiSystem::NewFrame() { | |
| } | ||
| ImGui::SameLine(); | ||
| if (ImGui::Button("Reset##Exposure")) { | ||
| exposure = 3.0f; // Reset to higher value for emissive lighting | ||
| exposure = 1.2f; // Reset to a sane default to avoid washout | ||
| if (renderer) { | ||
| renderer->SetExposure(exposure); | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,7 @@ | |
| #include <memory> | ||
| #include <vulkan/vulkan_raii.hpp> | ||
| #include <vulkan/vk_platform.h> | ||
| #include <stdexcept> | ||
|
|
||
| // Forward declarations | ||
| class Renderer; | ||
|
|
@@ -25,6 +26,13 @@ class ImGuiSystem { | |
| */ | ||
| ImGuiSystem(); | ||
|
|
||
| // Constructor-based initialization to replace separate Initialize() calls | ||
| ImGuiSystem(Renderer* renderer, uint32_t width, uint32_t height) { | ||
| if (!Initialize(renderer, width, height)) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| throw std::runtime_error("ImGuiSystem: initialization failed"); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @brief Destructor for proper cleanup. | ||
| */ | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is this used for?