diff --git a/client/src/game_state.cpp b/client/src/game_state.cpp index d54a2a2..9c0ade4 100644 --- a/client/src/game_state.cpp +++ b/client/src/game_state.cpp @@ -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; } diff --git a/client/src/game_state.h b/client/src/game_state.h index 7b032dd..ed4664a 100644 --- a/client/src/game_state.h +++ b/client/src/game_state.h @@ -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: diff --git a/client/src/main.cpp b/client/src/main.cpp index a289d5a..44efaa7 100644 --- a/client/src/main.cpp +++ b/client/src/main.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #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. */ diff --git a/client/src/ui/desktop.cpp b/client/src/ui/desktop.cpp index 22a526f..8fc7f51 100644 --- a/client/src/ui/desktop.cpp +++ b/client/src/ui/desktop.cpp @@ -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& 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(std::round(line.y_precise)); if(line.render_y < 0) { line.y_precise = screen_height; diff --git a/client/src/ui/desktop.h b/client/src/ui/desktop.h index e9e484f..c67f38e 100644 --- a/client/src/ui/desktop.h +++ b/client/src/ui/desktop.h @@ -27,7 +27,7 @@ public: void add_window(std::unique_ptr 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> _windows; std::unique_ptr _taskbar; diff --git a/client/src/ui/main_menu.cpp b/client/src/ui/main_menu.cpp index 5cc8809..3035be0 100644 --- a/client/src/ui/main_menu.cpp +++ b/client/src/ui/main_menu.cpp @@ -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); diff --git a/client/src/ui/main_menu.h b/client/src/ui/main_menu.h index 9226934..e694376 100644 --- a/client/src/ui/main_menu.h +++ b/client/src/ui/main_menu.h @@ -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. */