Set fps limit for game.

This commit is contained in:
Евгений 2022-05-22 19:41:31 +04:00
parent 4921424c7d
commit f0f3d40095
4 changed files with 54 additions and 7 deletions

18
src/FpsLimiter.cc Normal file
View File

@ -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));
}

18
src/FpsLimiter.h Normal file
View File

@ -0,0 +1,18 @@
#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

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.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) {

View File

@ -2,6 +2,7 @@
#define MAIN_H
#include "art.h"
#include "FpsLimiter.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 */