Add fps limit (#10)

This commit is contained in:
JordanCpp 2022-05-23 12:13:28 +04:00 committed by GitHub
parent 0c61f74ba9
commit 1f8a62f2b6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 59 additions and 7 deletions

View File

@ -228,6 +228,11 @@ add_executable(fallout2-ce WIN32
"src/xfile.h"
)
target_sources(fallout2-ce PUBLIC
"src/fps_limiter.cc"
"src/fps_limiter.h"
)
target_compile_definitions(fallout2-ce PUBLIC
_CRT_SECURE_NO_WARNINGS
_CRT_NONSTDC_NO_WARNINGS

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

@ -130,6 +130,8 @@ int falloutMain(int argc, char** argv)
gameMoviePlay(MOVIE_INTRO, 0);
gameMoviePlay(MOVIE_CREDITS, 0);
FpsLimiter fpsLimiter;
if (mainMenuWindowInit() == 0) {
bool done = false;
while (!done) {
@ -138,7 +140,7 @@ int falloutMain(int argc, char** argv)
mainMenuWindowUnhide(1);
mouseShowCursor();
int mainMenuRc = mainMenuWindowHandleEvents();
int mainMenuRc = mainMenuWindowHandleEvents(fpsLimiter);
mouseHideCursor();
switch (mainMenuRc) {
@ -154,7 +156,7 @@ int falloutMain(int argc, char** argv)
gameMoviePlay(MOVIE_ELDER, GAME_MOVIE_STOP_MUSIC);
randomSeedPrerandom(-1);
_main_load_new(_mainMap);
mainLoop();
mainLoop(fpsLimiter);
paletteFadeTo(gPaletteWhite);
objectHide(gDude, NULL);
_map_exit();
@ -189,7 +191,7 @@ int falloutMain(int argc, char** argv)
} else if (loadGameRc != 0) {
windowDestroy(win);
win = -1;
mainLoop();
mainLoop(fpsLimiter);
}
paletteFadeTo(gPaletteWhite);
if (win != -1) {
@ -298,7 +300,7 @@ int _main_load_new(char* mapFileName)
}
// 0x480E48
void mainLoop()
void mainLoop(FpsLimiter& fpsLimiter)
{
bool cursorWasHidden = cursorIsHidden();
if (cursorWasHidden) {
@ -310,6 +312,8 @@ void mainLoop()
scriptsEnable();
while (_game_user_wants_to_quit == 0) {
fpsLimiter.mark();
int keyCode = _get_input();
gameHandleKey(keyCode, false);
@ -326,6 +330,8 @@ void mainLoop()
_main_show_death_scene = 1;
_game_user_wants_to_quit = 2;
}
fpsLimiter.throttle();
}
scriptsDisable();
@ -759,7 +765,7 @@ int _main_menu_is_enabled()
}
// 0x481AEC
int mainMenuWindowHandleEvents()
int mainMenuWindowHandleEvents(FpsLimiter& fpsLimiter)
{
_in_main_menu = true;
@ -772,6 +778,8 @@ int mainMenuWindowHandleEvents()
int rc = -1;
while (rc == -1) {
fpsLimiter.mark();
int keyCode = _get_input();
for (int buttonIndex = 0; buttonIndex < MAIN_MENU_BUTTON_COUNT; buttonIndex++) {
@ -818,6 +826,8 @@ int mainMenuWindowHandleEvents()
rc = MAIN_MENU_TIMEOUT;
}
}
fpsLimiter.throttle();
}
if (oldCursorIsHidden) {

View File

@ -2,6 +2,7 @@
#define MAIN_H
#include "art.h"
#include "fps_limiter.h"
#include <stdbool.h>
@ -55,7 +56,7 @@ extern CacheEntry* gMainMenuBackgroundFrmHandle;
int falloutMain(int argc, char** argv);
bool falloutInit(int argc, char** argv);
int _main_load_new(char* fname);
void mainLoop();
void mainLoop(FpsLimiter& fpsLimiter);
void _main_selfrun_exit();
void showDeath();
void _main_death_voiceover_callback();
@ -66,6 +67,6 @@ void mainMenuWindowFree();
void mainMenuWindowHide(bool animate);
void mainMenuWindowUnhide(bool animate);
int _main_menu_is_enabled();
int mainMenuWindowHandleEvents();
int mainMenuWindowHandleEvents(FpsLimiter& fpsLimiter);
#endif /* MAIN_H */