Refactoring of player management and integration with the main window rendering
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
#include <algorithm>
|
||||
#include <chrono>
|
||||
|
||||
using namespace std;
|
||||
using namespace quokka_gfx;
|
||||
@@ -24,16 +25,15 @@ WindowManager *Application::GetWindowManager() noexcept
|
||||
void Application::Run() const
|
||||
{
|
||||
// Main loop
|
||||
bool running = true;
|
||||
uint64_t lastTime = SDL_GetTicksNS();
|
||||
bool running = true;
|
||||
auto lastTime = std::chrono::nanoseconds(SDL_GetTicksNS());
|
||||
|
||||
while (running && m_windowManager.HasAnyWindow())
|
||||
{
|
||||
// Calculate delta time
|
||||
const uint64_t currentTime = SDL_GetTicksNS();
|
||||
float dt = static_cast<float>(currentTime - lastTime) / 1e9f; // Convert nanoseconds to seconds
|
||||
lastTime = currentTime;
|
||||
dt = std::max(dt, 0.00001f); // Avoid zero or negative delta time
|
||||
const auto currentTime = std::chrono::nanoseconds(SDL_GetTicksNS());
|
||||
const auto dt = currentTime - lastTime;
|
||||
lastTime = currentTime;
|
||||
|
||||
// Poll and process events
|
||||
const auto result = m_eventManager.ProcessEvents();
|
||||
|
||||
@@ -84,7 +84,7 @@ void Window::SetMinSize(const Size &size) const
|
||||
void Window::OnEvent(SDL_Event *event) {}
|
||||
//--------------------------------------------------------------
|
||||
/* Update the window state */
|
||||
void Window::Update(const float dt)
|
||||
void Window::Update(std::chrono::nanoseconds dt)
|
||||
{
|
||||
// Nothing to do
|
||||
}
|
||||
@@ -101,10 +101,6 @@ void Window::Render()
|
||||
if (!m_renderer)
|
||||
return;
|
||||
|
||||
// Set the drawing color to black and clear the rendering target
|
||||
(void)m_renderer->setDrawColor(Color::Black);
|
||||
(void)m_renderer->clear();
|
||||
|
||||
// Draw the window content
|
||||
Draw();
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
#include "utils/Coords.h"
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
#include <chrono>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
@@ -41,14 +42,14 @@ class Window
|
||||
void Show(bool show = true) const; // Show or hide the window
|
||||
void Hide() const; // Hide the window
|
||||
|
||||
void Close(); // Close the window
|
||||
void SetMinSize(const Size &size) const; // Set the minimum size of the window
|
||||
void Close(); // Close the window
|
||||
void SetMinSize(const Size &size) const; // Set the minimum size of the window
|
||||
|
||||
// Window lifecycle functions
|
||||
virtual void OnEvent(SDL_Event *event); // Handle event
|
||||
virtual void Update(float dt); // Update the window state
|
||||
virtual bool Draw(); // Draw the window content
|
||||
void Render(); // Render the window
|
||||
virtual void OnEvent(SDL_Event *event); // Handle event
|
||||
virtual void Update(std::chrono::nanoseconds dt); // Update the window state
|
||||
virtual bool Draw(); // Draw the window content
|
||||
void Render(); // Render the window
|
||||
|
||||
protected:
|
||||
Application &m_appOwner; // Reference to the owning Application instance
|
||||
|
||||
@@ -63,7 +63,7 @@ void WindowManager::OnEvent(SDL_Event *event)
|
||||
}
|
||||
//--------------------------------------------------------------
|
||||
/* Update all windows */
|
||||
void WindowManager::Update(const float dt) const
|
||||
void WindowManager::Update(std::chrono::nanoseconds dt) const
|
||||
{
|
||||
// Propagate the Update call to all registered windows
|
||||
for (const auto &window : m_windows)
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
#include "../Window/Window.h"
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
#include <chrono>
|
||||
#include <vector>
|
||||
|
||||
namespace quokka_gfx
|
||||
@@ -33,9 +34,9 @@ class WindowManager
|
||||
protected:
|
||||
WindowList m_windows; // Vector to store unique pointers to windows
|
||||
|
||||
void OnEvent(SDL_Event *event); // Handle event for all windows
|
||||
void Update(float dt) const; // Update all windows
|
||||
void Render() const; // Render all windows
|
||||
void OnEvent(SDL_Event *event); // Handle event for all windows
|
||||
void Update(std::chrono::nanoseconds dt) const; // Update all windows
|
||||
void Render() const; // Render all windows
|
||||
};
|
||||
//--------------------------------------------------------------
|
||||
} // namespace quokka_gfx
|
||||
|
||||
@@ -27,6 +27,11 @@ struct FPos
|
||||
{
|
||||
float x = 0.0f;
|
||||
float y = 0.0f;
|
||||
|
||||
[[nodiscard]] bool isInBox(const FPos &boxPos, const FSize &boxSize) const
|
||||
{
|
||||
return x >= boxPos.x && y >= boxPos.y && x < boxPos.x + boxSize.w && y < boxPos.y + boxSize.h;
|
||||
}
|
||||
};
|
||||
//--------------------------------------------------------------
|
||||
} // namespace quokka_gfx
|
||||
|
||||
Reference in New Issue
Block a user