[Add] Implement delta time game loop.

This commit is contained in:
Ritchie Cunningham 2025-10-04 21:28:16 +01:00
parent d602a8f1d4
commit caa482a7a0
7 changed files with 30 additions and 17 deletions

View File

@ -94,11 +94,11 @@ void GameState::handle_event(SDL_Event* event, int screen_width, int screen_heig
}
}
void GameState::update(void) {
void GameState::update(float dt) {
switch(_current_screen) {
case Screen::MAIN_MENU: {
if(!_main_menu) break;
Screen next_screen = _main_menu->update();
Screen next_screen = _main_menu->update(dt);
if(next_screen != Screen::MAIN_MENU) {
const MenuButton* clicked_button = _main_menu->get_clicked_button();
if(clicked_button) {
@ -196,7 +196,7 @@ void GameState::update(void) {
* TODO: These fuck'in window dimensions just need to be global at this point!!
* Pass GameState by reference ?
*/
_desktop->update(_screen_width, _screen_height);
_desktop->update(dt, _screen_width, _screen_height);
}
break;
}

View File

@ -26,7 +26,7 @@ public:
void init(int screen_width, int screen_height);
void start_single_player_now(int screen_width, int screen_height);
void handle_event(SDL_Event* event, int screen_width, int screen_height);
void update(void);
void update(float dt);
void render(const RenderContext& context);
private:

View File

@ -4,6 +4,7 @@
#include <GL/glew.h>
#include <SDL3/SDL.h>
#include <SDL3/SDL_keyboard.h>
#include <SDL3/SDL_timer.h>
#include "gfx/shape_renderer.h"
#include "gfx/txt_renderer.h"
@ -94,6 +95,10 @@ int main(int argc, char** argv) {
Uint32 last_blink_time = 0;
bool show_cursor = true;
/* Timestep. */
Uint64 last_frame_time = SDL_GetPerformanceCounter();
float dt = 0.0f;
bool running = true;
while(running) {
/* Event handling. */
@ -103,7 +108,15 @@ int main(int argc, char** argv) {
game_state->handle_event(&event, SCREEN_WIDTH, SCREEN_HEIGHT);
}
game_state->update();
/* Calculate delta time. */
Uint64 current_frame_time = SDL_GetPerformanceCounter();
dt = (current_frame_time - last_frame_time) / (float)SDL_GetPerformanceFrequency();
last_frame_time = current_frame_time;
/* Clamp dt to avoid large jumps. */
if(dt > 0.1f) dt = 0.1f;
game_state->update(dt);
Uint32 current_time = SDL_GetTicks();
if(current_time - last_blink_time > 500) { /* Every 500ms. */

View File

@ -162,7 +162,7 @@ void Desktop::handle_event(SDL_Event* event, int screen_width, int screen_height
}
}
void Desktop::update(int screen_width, int screen_height) {
void Desktop::update(float dt, int screen_width, int screen_height) {
/* Remove closed windows. */
if(_focused_window) {
IWindowContent* content = _focused_window->get_content();
@ -189,7 +189,7 @@ void Desktop::update(int screen_width, int screen_height) {
window->update();
}
}
_update_wallpaper(screen_width, screen_height);
_update_wallpaper(dt, screen_width, screen_height);
_windows.erase(std::remove_if(_windows.begin(), _windows.end(),
[this](const std::unique_ptr<UIWindow>& w) {
if (w->should_close()) {
@ -237,9 +237,9 @@ void Desktop::_render_wallpaper(UIRenderer* ui_renderer) {
}
}
void Desktop::_update_wallpaper(int screen_width, int screen_height) {
void Desktop::_update_wallpaper(float dt, int screen_width, int screen_height) {
for(auto& line : _background_text) {
line.y_precise -= line.speed;
line.y_precise -= line.speed * dt * 100.0f;
line.render_y = static_cast<int>(std::round(line.y_precise));
if(line.render_y < 0) {
line.y_precise = screen_height;

View File

@ -27,7 +27,7 @@ public:
void add_window(std::unique_ptr<UIWindow> window);
void handle_event(SDL_Event* event, int screen_width, int screen_height);
void update(int screen_width, int screen_height);
void update(float dt, int screen_width, int screen_height);
void render(const RenderContext& context);
UIWindow* get_focused_window(void);
@ -35,7 +35,7 @@ public:
private:
void _set_focused_window(UIWindow* window);
void _render_wallpaper(UIRenderer* ui_renderer);
void _update_wallpaper(int screen_width, int screen_height);
void _update_wallpaper(float dt, int screen_width, int screen_height);
std::vector<std::unique_ptr<UIWindow>> _windows;
std::unique_ptr<Taskbar> _taskbar;

View File

@ -89,8 +89,8 @@ void MainMenu::handle_event(SDL_Event* event) {
}
}
Screen MainMenu::update(void) {
_update_background();
Screen MainMenu::update(float dt) {
_update_background(dt);
return _next_screen;
}
@ -127,9 +127,9 @@ void MainMenu::render(UIRenderer* ui_renderer) {
ui_renderer->flush_text();
}
void MainMenu::_update_background(void) {
void MainMenu::_update_background(float dt) {
for(auto& line : _background_text) {
line.y -= line.speed;
line.y -= line.speed * dt * 100.0f;
if(line.y < 0) {
line.y = _screen_height;
line.x = (float)(rand() % _screen_width);

View File

@ -23,13 +23,13 @@ public:
~MainMenu(void);
void handle_event(SDL_Event* event);
Screen update(void);
Screen update(float dt);
void render(UIRenderer* ui_renderer);
const MenuButton* get_clicked_button(void) const { return _clicked_button; }
private:
void _update_background(void);
void _update_background(float dt);
void _render_background(UIRenderer* ui_renderer);
/* For animated background. */