Skip to content

Commit

Permalink
Possibly fixed too high framerate issues
Browse files Browse the repository at this point in the history
  • Loading branch information
kallehed committed Oct 1, 2022
1 parent c33f751 commit 6b5b9d4
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
10 changes: 6 additions & 4 deletions sdl_test/Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion sdl_test/Game.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class Game {
std::array<int, 2> _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;

Expand Down

0 comments on commit 6b5b9d4

Please sign in to comment.