Refactor to match style

This commit is contained in:
Alexander Batalov 2022-05-23 10:26:59 +03:00
parent edaae98a38
commit 2f3cc2d820
7 changed files with 46 additions and 42 deletions

View File

@ -226,7 +226,11 @@ add_executable(fallout2-ce WIN32
"src/world_map.h"
"src/xfile.cc"
"src/xfile.h"
"src/FpsLimiter.cc"
)
target_sources(fallout2-ce PUBLIC
"src/fps_limiter.cc"
"src/fps_limiter.h"
)
target_compile_definitions(fallout2-ce PUBLIC

View File

@ -1,18 +0,0 @@
#include "FpsLimiter.h"
FpsLimiter::FpsLimiter(size_t fps):
_Fps(fps),
_Ticks(0)
{
}
void FpsLimiter::Begin()
{
_Ticks = SDL_GetTicks();
}
void FpsLimiter::End()
{
if (1000 / _Fps > SDL_GetTicks() - _Ticks)
SDL_Delay(1000 / _Fps - (SDL_GetTicks() - _Ticks));
}

View File

@ -1,18 +0,0 @@
#ifndef Fallout2_RE_FpsLimiter_h_
#define Fallout2_RE_FpsLimiter_h_
#include <SDL.h>
class FpsLimiter
{
public:
FpsLimiter(size_t fps = 60);
void Begin();
void End();
private:
size_t _Fps;
size_t _Ticks;
};
#endif

21
src/fps_limiter.cc Normal file
View File

@ -0,0 +1,21 @@
#include "fps_limiter.h"
#include <SDL.h>
FpsLimiter::FpsLimiter(size_t fps)
: _fps(fps)
, _ticks(0)
{
}
void FpsLimiter::mark()
{
_ticks = SDL_GetTicks();
}
void FpsLimiter::throttle() const
{
if (1000 / _fps > SDL_GetTicks() - _ticks) {
SDL_Delay(1000 / _fps - (SDL_GetTicks() - _ticks));
}
}

15
src/fps_limiter.h Normal file
View File

@ -0,0 +1,15 @@
#ifndef FPS_LIMITER_H
#define FPS_LIMITER_H
class FpsLimiter {
public:
FpsLimiter(size_t fps = 60);
void mark();
void throttle() const;
private:
const size_t _fps;
size_t _ticks;
};
#endif /* FPS_LIMITER_H */

View File

@ -312,7 +312,7 @@ void mainLoop(FpsLimiter& fpsLimiter)
scriptsEnable();
while (_game_user_wants_to_quit == 0) {
fpsLimiter.Begin();
fpsLimiter.mark();
int keyCode = _get_input();
gameHandleKey(keyCode, false);
@ -331,7 +331,7 @@ void mainLoop(FpsLimiter& fpsLimiter)
_game_user_wants_to_quit = 2;
}
fpsLimiter.End();
fpsLimiter.throttle();
}
scriptsDisable();
@ -778,7 +778,7 @@ int mainMenuWindowHandleEvents(FpsLimiter& fpsLimiter)
int rc = -1;
while (rc == -1) {
fpsLimiter.Begin();
fpsLimiter.mark();
int keyCode = _get_input();
@ -827,7 +827,7 @@ int mainMenuWindowHandleEvents(FpsLimiter& fpsLimiter)
}
}
fpsLimiter.End();
fpsLimiter.throttle();
}
if (oldCursorIsHidden) {

View File

@ -2,7 +2,7 @@
#define MAIN_H
#include "art.h"
#include "FpsLimiter.h"
#include "fps_limiter.h"
#include <stdbool.h>