Skip to content

Commit 6b5b9d4

Browse files
committed
Possibly fixed too high framerate issues
1 parent c33f751 commit 6b5b9d4

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

sdl_test/Game.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,8 @@ Game::Game()
108108
SDL_WINDOW_SHOWN);
109109
SDL_SetWindowSize(_window, _WIDTH, _HEIGHT);
110110

111-
_renderer = SDL_CreateRenderer(_window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
112-
//_renderer = SDL_CreateRenderer(_window, -1, SDL_RENDERER_ACCELERATED);
111+
//_renderer = SDL_CreateRenderer(_window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
112+
_renderer = SDL_CreateRenderer(_window, -1, SDL_RENDERER_ACCELERATED);
113113
SDL_SetRenderDrawBlendMode(_renderer, SDL_BLENDMODE_BLEND);
114114

115115
IMG_Init(IMG_INIT_PNG | IMG_INIT_JPG);
@@ -433,10 +433,12 @@ void Game::game_loop()
433433

434434
//std::cout << std::endl << "frame time MS " << _dt << std::endl;
435435
if (_ticks % 60 == 0) {
436-
if (_dt != 0.f) std::cout << "fps: " << (int)(1000 / (_dt)) << std::endl;
436+
//if (_dt != 0.f) std::cout << "fps: " << (int)(1000 / (_dt)) << std::endl;
437437
}
438438
_dt = 1000.f * (SDL_GetPerformanceCounter() - start_time) / ((float)SDL_GetPerformanceFrequency());
439-
_dt = std::min(_MAX_DT, _dt); // no less than 50 fps simulated.
439+
SDL_Delay(std::min((Uint32)30,std::max((Uint32)0,(Uint32)floor(16.666f - _dt))));
440+
//_dt = std::min(_MAX_DT, _dt); // no less than 50 fps simulated.
441+
_dt = _MAX_DT;
440442
_dt *= _slow_motion_factor;
441443
_slow_motion_factor += 0.02f;
442444
_slow_motion_factor = std::min(1.f, _slow_motion_factor);

sdl_test/Game.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class Game {
4949
std::array<int, 2> _press_e_w_and_h; // width and height of "press e" texture
5050

5151
float _dt = 0; // time for previous frame to run in MS
52-
const float _MAX_DT = 1000.f / 50.f; // 50 is the lowest framerate allowed. Also maximum _dt
52+
const float _MAX_DT = 1000.f / 60.f; // 50 is the lowest framerate allowed. Also maximum _dt
5353

5454
float _slow_motion_factor = 1.f;
5555

0 commit comments

Comments
 (0)