From f0f3d40095210418553ecc2eddbcdba6a545331a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=95=D0=B2=D0=B3=D0=B5=D0=BD=D0=B8=D0=B9?= Date: Sun, 22 May 2022 19:41:31 +0400 Subject: [PATCH] Set fps limit for game. --- src/FpsLimiter.cc | 18 ++++++++++++++++++ src/FpsLimiter.h | 18 ++++++++++++++++++ src/main.cc | 20 +++++++++++++++----- src/main.h | 5 +++-- 4 files changed, 54 insertions(+), 7 deletions(-) create mode 100644 src/FpsLimiter.cc create mode 100644 src/FpsLimiter.h diff --git a/src/FpsLimiter.cc b/src/FpsLimiter.cc new file mode 100644 index 0000000..156746e --- /dev/null +++ b/src/FpsLimiter.cc @@ -0,0 +1,18 @@ +#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)); +} diff --git a/src/FpsLimiter.h b/src/FpsLimiter.h new file mode 100644 index 0000000..b1a6b9b --- /dev/null +++ b/src/FpsLimiter.h @@ -0,0 +1,18 @@ +#ifndef Fallout2_RE_FpsLimiter_h_ +#define Fallout2_RE_FpsLimiter_h_ + +#include + +class FpsLimiter +{ +public: + FpsLimiter(size_t fps = 60); + void Begin(); + void End(); + +private: + size_t _Fps; + size_t _Ticks; +}; + +#endif diff --git a/src/main.cc b/src/main.cc index 2dcea8a..fbecc42 100644 --- a/src/main.cc +++ b/src/main.cc @@ -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.Begin(); + 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.End(); } 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.Begin(); + 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.End(); } if (oldCursorIsHidden) { diff --git a/src/main.h b/src/main.h index c878d3c..50665d3 100644 --- a/src/main.h +++ b/src/main.h @@ -2,6 +2,7 @@ #define MAIN_H #include "art.h" +#include "FpsLimiter.h" #include @@ -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 */