Skip to content

Commit ccb5c15

Browse files
committed
addressing more comments.
1 parent 0b0dddb commit ccb5c15

File tree

11 files changed

+257
-178
lines changed

11 files changed

+257
-178
lines changed

attachments/simple_engine/audio_system.cpp

Lines changed: 203 additions & 113 deletions
Large diffs are not rendered by default.

attachments/simple_engine/audio_system.h

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,11 @@ class AudioSystem {
148148
*/
149149
AudioSystem() = default;
150150

151+
/**
152+
* @brief Flush audio output: clears pending processing and device buffers so playback restarts cleanly.
153+
*/
154+
void FlushOutput();
155+
151156
/**
152157
* @brief Destructor for proper cleanup.
153158
*/
@@ -165,7 +170,7 @@ class AudioSystem {
165170
* @brief Update the audio system.
166171
* @param deltaTime The time elapsed since the last update.
167172
*/
168-
void Update(float deltaTime);
173+
void Update(std::chrono::milliseconds deltaTime);
169174

170175
/**
171176
* @brief Load an audio file.
@@ -318,8 +323,9 @@ class AudioSystem {
318323
std::vector<float> inputBuffer;
319324
std::vector<float> outputBuffer;
320325
float sourcePosition[3];
321-
uint32_t sampleCount;
322-
uint32_t actualSamplesProcessed;
326+
uint32_t sampleCount; // total frames in input/output (may include history)
327+
uint32_t actualSamplesProcessed; // frames to write this tick (new part)
328+
uint32_t trimFront; // frames to skip from output front (history length)
323329
AudioOutputDevice* outputDevice;
324330
float masterVolume;
325331
};
@@ -392,5 +398,5 @@ class AudioSystem {
392398
* @param actualSamplesProcessed The number of samples actually processed.
393399
* @return True if the task was submitted successfully, false otherwise.
394400
*/
395-
bool submitAudioTask(const float* inputBuffer, uint32_t sampleCount, const float* sourcePosition, uint32_t actualSamplesProcessed);
401+
bool submitAudioTask(const float* inputBuffer, uint32_t sampleCount, const float* sourcePosition, uint32_t actualSamplesProcessed, uint32_t trimFront);
396402
};

attachments/simple_engine/component.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#include "component.h"
2-
#include "entity.h"
32

43
// Most of the Component class implementation is in the header file
54
// This file is mainly for any methods that need to access the Entity class

attachments/simple_engine/component.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#pragma once
22

3-
#include <memory>
3+
#include <chrono>
44
#include <string>
55

66
// Forward declaration
@@ -44,7 +44,7 @@ class Component {
4444
* Called every frame.
4545
* @param deltaTime The time elapsed since the last frame.
4646
*/
47-
virtual void Update(float deltaTime) {}
47+
virtual void Update(std::chrono::milliseconds deltaTime) {}
4848

4949
/**
5050
* @brief Render the component.

attachments/simple_engine/engine.cpp

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include <chrono>
66
#include <algorithm>
7+
#include <ranges>
78
#include <stdexcept>
89
#include <iostream>
910
#include <random>
@@ -122,11 +123,11 @@ void Engine::Run() {
122123
}
123124

124125
// Calculate delta time
125-
deltaTime = CalculateDeltaTime();
126+
deltaTimeMs = CalculateDeltaTimeMs();
126127

127128
// Update frame counter and FPS
128129
frameCount++;
129-
fpsUpdateTimer += deltaTime;
130+
fpsUpdateTimer += deltaTimeMs.count() * 0.001f;
130131

131132
// Update window title with FPS and frame time every second
132133
if (fpsUpdateTimer >= 1.0f) {
@@ -147,7 +148,7 @@ void Engine::Run() {
147148
}
148149

149150
// Update
150-
Update(deltaTime);
151+
Update(deltaTimeMs);
151152

152153
// Render
153154
Render();
@@ -369,7 +370,7 @@ void Engine::handleKeyInput(uint32_t key, bool pressed) {
369370
}
370371
}
371372

372-
void Engine::Update(float deltaTime) {
373+
void Engine::Update(TimeDelta deltaTime) {
373374
// Debug: Verify Update method is being called
374375
static int updateCallCount = 0;
375376
updateCallCount++;
@@ -415,27 +416,27 @@ void Engine::Render() {
415416
renderer->Render(entities, activeCamera, imguiSystem.get());
416417
}
417418

418-
float Engine::CalculateDeltaTime() {
419+
std::chrono::milliseconds Engine::CalculateDeltaTimeMs() {
419420
// Get current time using a steady clock to avoid system time jumps
420421
uint64_t currentTime = static_cast<uint64_t>(
421422
std::chrono::duration_cast<std::chrono::milliseconds>(
422423
std::chrono::steady_clock::now().time_since_epoch()
423424
).count()
424425
);
425426

426-
// Initialize lastFrameTime on first call
427-
if (lastFrameTime == 0) {
428-
lastFrameTime = currentTime;
429-
return 0.016f; // ~16ms as a sane initial guess
427+
// Initialize lastFrameTimeMs on first call
428+
if (lastFrameTimeMs == 0) {
429+
lastFrameTimeMs = currentTime;
430+
return std::chrono::milliseconds(16); // ~16ms as a sane initial guess
430431
}
431432

432433
// Calculate delta time in milliseconds
433-
uint64_t delta = currentTime - lastFrameTime;
434+
uint64_t delta = currentTime - lastFrameTimeMs;
434435

435436
// Update last frame time
436-
lastFrameTime = currentTime;
437+
lastFrameTimeMs = currentTime;
437438

438-
return static_cast<float>(delta) / 1000.0f;
439+
return std::chrono::milliseconds(static_cast<long long>(delta));
439440
}
440441

441442
void Engine::HandleResize(int width, int height) const {
@@ -458,7 +459,7 @@ void Engine::HandleResize(int width, int height) const {
458459
}
459460
}
460461

461-
void Engine::UpdateCameraControls(float deltaTime) const {
462+
void Engine::UpdateCameraControls(TimeDelta deltaTime) const {
462463
if (!activeCamera) return;
463464

464465
// Get a camera transform component
@@ -492,7 +493,7 @@ void Engine::UpdateCameraControls(float deltaTime) const {
492493

493494
// Manual camera controls (only when tracking is disabled)
494495
// Calculate movement speed
495-
float velocity = cameraControl.cameraSpeed * deltaTime;
496+
float velocity = cameraControl.cameraSpeed * deltaTime.count() * .001f;
496497

497498
// Calculate camera direction vectors based on yaw and pitch
498499
glm::vec3 front;
@@ -859,10 +860,10 @@ void Engine::RunAndroid() {
859860
// We just need to update and render when the platform is ready
860861

861862
// Calculate delta time
862-
deltaTime = CalculateDeltaTime();
863+
deltaTimeMs = CalculateDeltaTimeMs();
863864

864865
// Update
865-
Update(deltaTime);
866+
Update(deltaTimeMs);
866867

867868
// Render
868869
Render();

attachments/simple_engine/engine.h

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <vector>
55
#include <memory>
66
#include <unordered_map>
7+
#include <chrono>
78

89
#include "platform.h"
910
#include "renderer.h"
@@ -23,6 +24,7 @@
2324
*/
2425
class Engine {
2526
public:
27+
using TimeDelta = std::chrono::milliseconds;
2628
/**
2729
* @brief Default constructor.
2830
*/
@@ -197,8 +199,9 @@ class Engine {
197199
bool running = false;
198200

199201
// Delta time calculation
200-
float deltaTime = 0.0f;
201-
uint64_t lastFrameTime = 0;
202+
// deltaTimeMs: time since last frame in milliseconds (for clarity)
203+
std::chrono::milliseconds deltaTimeMs{0};
204+
uint64_t lastFrameTimeMs = 0;
202205

203206
// Frame counter and FPS calculation
204207
uint64_t frameCount = 0;
@@ -269,18 +272,19 @@ class Engine {
269272
* @brief Update the engine state.
270273
* @param deltaTime The time elapsed since the last update.
271274
*/
272-
void Update(float deltaTime);
275+
// Accepts a time delta in milliseconds for clarity
276+
void Update(TimeDelta deltaTime);
273277

274278
/**
275279
* @brief Render the scene.
276280
*/
277281
void Render();
278282

279283
/**
280-
* @brief Calculate the delta time between frames.
281-
* @return The delta time in seconds.
284+
* @brief Calculate the time delta between frames.
285+
* @return The delta time in milliseconds (steady_clock based).
282286
*/
283-
float CalculateDeltaTime();
287+
std::chrono::milliseconds CalculateDeltaTimeMs();
284288

285289
/**
286290
* @brief Handle window resize events.
@@ -293,7 +297,7 @@ class Engine {
293297
* @brief Update camera controls based on input state.
294298
* @param deltaTime The time elapsed since the last update.
295299
*/
296-
void UpdateCameraControls(float deltaTime) const;
300+
void UpdateCameraControls(TimeDelta deltaTime) const;
297301

298302
/**
299303
* @brief Generate random PBR material properties for the ball.

attachments/simple_engine/entity.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ void Entity::Initialize() {
99
}
1010
}
1111

12-
void Entity::Update(float deltaTime) {
12+
void Entity::Update(std::chrono::milliseconds deltaTime) {
1313
if (!active) return;
1414

1515
for (auto& component : components) {

attachments/simple_engine/entity.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <memory>
55
#include <string>
66
#include <algorithm>
7+
#include <chrono>
78
#include <typeindex>
89
#include <unordered_map>
910
#include <type_traits>
@@ -62,7 +63,7 @@ class Entity {
6263
* @brief Update all components of the entity.
6364
* @param deltaTime The time elapsed since the last frame.
6465
*/
65-
void Update(float deltaTime);
66+
void Update(std::chrono::milliseconds deltaTime);
6667

6768
/**
6869
* @brief Render all components of the entity.

attachments/simple_engine/imgui_system.cpp

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,7 @@ void ImGuiSystem::NewFrame() {
281281
if (ImGui::Button("Play")) {
282282
if (currentSource) {
283283
currentSource->Play();
284+
if (audioSystem) { audioSystem->FlushOutput(); }
284285
if (useDebugPing) {
285286
std::cout << "Started playing debug ping (800Hz sine wave) with HRTF processing" << std::endl;
286287
} else {
@@ -311,34 +312,10 @@ void ImGuiSystem::NewFrame() {
311312
ImGui::Text("Use directional buttons to move the audio source in 3D space");
312313
ImGui::Text("You should hear the audio move around you!");
313314

314-
// HRTF Processing Mode Selection
315+
// HRTF Processing Mode: GPU only (checkbox removed)
315316
ImGui::Separator();
316317
ImGui::Text("HRTF Processing Mode:");
317-
318-
static bool cpuOnlyMode = false;
319-
static bool initialized = false;
320-
321-
// Initialize checkbox state to match audio system state
322-
if (!initialized && audioSystem) {
323-
cpuOnlyMode = audioSystem->IsHRTFCPUOnly();
324-
initialized = true;
325-
}
326-
327-
if (ImGui::Checkbox("Force CPU-only HRTF processing", &cpuOnlyMode)) {
328-
if (audioSystem) {
329-
audioSystem->SetHRTFCPUOnly(cpuOnlyMode);
330-
std::cout << "HRTF processing mode changed to: " << (cpuOnlyMode ? "CPU-only" : "Vulkan shader (when available)") << std::endl;
331-
}
332-
}
333-
334-
// Display current processing mode
335-
if (audioSystem) {
336-
if (audioSystem->IsHRTFCPUOnly()) {
337-
ImGui::Text("Current Mode: CPU-only processing");
338-
} else {
339-
ImGui::Text("Current Mode: Vulkan shader processing (when available)");
340-
}
341-
}
318+
ImGui::Text("Current Mode: Vulkan shader processing (GPU)");
342319
} else {
343320
ImGui::Text("HRTF Processing: DISABLED");
344321
}

attachments/simple_engine/physics_system.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ bool PhysicsSystem::Initialize() {
201201
return true;
202202
}
203203

204-
void PhysicsSystem::Update(float deltaTime) {
204+
void PhysicsSystem::Update(std::chrono::milliseconds deltaTime) {
205205
// GPU-ONLY physics - NO CPU fallback available
206206

207207
// Check if GPU physics is properly initialized and available
@@ -977,7 +977,7 @@ void PhysicsSystem::CleanupVulkanResources() {
977977
vulkanResources.physicsBufferMemory = nullptr;
978978
}
979979

980-
void PhysicsSystem::UpdateGPUPhysicsData(float deltaTime) const {
980+
void PhysicsSystem::UpdateGPUPhysicsData(std::chrono::milliseconds deltaTime) const {
981981
if (!renderer) {
982982
return;
983983
}
@@ -1082,7 +1082,7 @@ void PhysicsSystem::UpdateGPUPhysicsData(float deltaTime) const {
10821082

10831083
// Update params buffer
10841084
PhysicsParams params{};
1085-
params.deltaTime = deltaTime; // Use actual deltaTime instead of fixed timestep
1085+
params.deltaTime = deltaTime.count() * 0.001f; // Use actual deltaTime instead of fixed timestep
10861086
params.numBodies = static_cast<uint32_t>(rigidBodies.size());
10871087
params.maxCollisions = maxGPUCollisions;
10881088
params.padding = 0.0f; // Initialize padding to zero for proper std140 alignment
@@ -1192,7 +1192,7 @@ void PhysicsSystem::ReadbackGPUPhysicsData() const {
11921192
}
11931193
}
11941194

1195-
void PhysicsSystem::SimulatePhysicsOnGPU(const float deltaTime) const {
1195+
void PhysicsSystem::SimulatePhysicsOnGPU(const std::chrono::milliseconds deltaTime) const {
11961196
if (!renderer) {
11971197
fprintf(stderr, "SimulatePhysicsOnGPU: No renderer available");
11981198
return;

0 commit comments

Comments
 (0)