From 6b5b9d485a2e0e68844285274e5b800f1186243f Mon Sep 17 00:00:00 2001 From: kallehed Date: Sat, 1 Oct 2022 10:25:45 +0200 Subject: [PATCH] Possibly fixed too high framerate issues --- sdl_test/Game.cpp | 10 ++++++---- sdl_test/Game.h | 2 +- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/sdl_test/Game.cpp b/sdl_test/Game.cpp index 9db3bc3..81e0394 100644 --- a/sdl_test/Game.cpp +++ b/sdl_test/Game.cpp @@ -108,8 +108,8 @@ Game::Game() SDL_WINDOW_SHOWN); SDL_SetWindowSize(_window, _WIDTH, _HEIGHT); - _renderer = SDL_CreateRenderer(_window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC); - //_renderer = SDL_CreateRenderer(_window, -1, SDL_RENDERER_ACCELERATED); + //_renderer = SDL_CreateRenderer(_window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC); + _renderer = SDL_CreateRenderer(_window, -1, SDL_RENDERER_ACCELERATED); SDL_SetRenderDrawBlendMode(_renderer, SDL_BLENDMODE_BLEND); IMG_Init(IMG_INIT_PNG | IMG_INIT_JPG); @@ -433,10 +433,12 @@ void Game::game_loop() //std::cout << std::endl << "frame time MS " << _dt << std::endl; if (_ticks % 60 == 0) { - if (_dt != 0.f) std::cout << "fps: " << (int)(1000 / (_dt)) << std::endl; + //if (_dt != 0.f) std::cout << "fps: " << (int)(1000 / (_dt)) << std::endl; } _dt = 1000.f * (SDL_GetPerformanceCounter() - start_time) / ((float)SDL_GetPerformanceFrequency()); - _dt = std::min(_MAX_DT, _dt); // no less than 50 fps simulated. + SDL_Delay(std::min((Uint32)30,std::max((Uint32)0,(Uint32)floor(16.666f - _dt)))); + //_dt = std::min(_MAX_DT, _dt); // no less than 50 fps simulated. + _dt = _MAX_DT; _dt *= _slow_motion_factor; _slow_motion_factor += 0.02f; _slow_motion_factor = std::min(1.f, _slow_motion_factor); diff --git a/sdl_test/Game.h b/sdl_test/Game.h index f5b3a9c..4ead4ae 100644 --- a/sdl_test/Game.h +++ b/sdl_test/Game.h @@ -49,7 +49,7 @@ class Game { std::array _press_e_w_and_h; // width and height of "press e" texture float _dt = 0; // time for previous frame to run in MS - const float _MAX_DT = 1000.f / 50.f; // 50 is the lowest framerate allowed. Also maximum _dt + const float _MAX_DT = 1000.f / 60.f; // 50 is the lowest framerate allowed. Also maximum _dt float _slow_motion_factor = 1.f;