Add FPS measure

This commit is contained in:
Miloslav Ciz 2025-04-20 14:18:57 +02:00
parent 975bf16399
commit 06c1d1c42e
3 changed files with 62 additions and 1 deletions

58
game.h
View file

@ -176,6 +176,12 @@ uint8_t LCR_gameGetNextAudioSample(void);
#define LCR_LOADING_COMMAND ;
#endif
/*
To make the game log FPS define a macro named LCR_FPS_GET_MS to a command that
evaluates to the current number of milliseconds since some given time. It
will be used to measure frame duration and average values will be logged.
*/
//------------------------------------------------------------------------------
#define LCR_LOG0(s) ;
@ -357,6 +363,13 @@ struct
is to allow ghosts for even long replays. */
} ghost;
#endif
#ifdef LCR_FPS_GET_MS
uint16_t renderFrameMS;
uint16_t physicsFrameMS;
uint8_t renderFramesMeasured;
uint8_t physicsFramesMeasured;
#endif
} LCR_game;
uint8_t LCR_gameMusicOn(void)
@ -1039,6 +1052,11 @@ void LCR_gameInit(int argc, const char **argv)
LCR_game.cameraMode = LCR_CAMERA_MODE_DRIVE;
LCR_currentMap.blockCount = 0; // means no map loaded
#ifdef LCR_FPS_GET_MS
LCR_game.renderFrameMS = 0;
LCR_game.physicsFrameMS = 0;
#endif
LCR_LOG2("parsing arguments");
uint8_t quickLoad = 0;
@ -1550,6 +1568,10 @@ uint8_t LCR_gameStep(uint32_t time)
{
LCR_LOG2("game step (start)");
#ifdef LCR_FPS_GET_MS
uint32_t frameTime;
#endif
uint32_t sleep = 0;
int paused = LCR_game.state == LCR_GAME_STATE_MENU ||
LCR_game.state == LCR_GAME_STATE_RUN_STARTING;
@ -1593,8 +1615,17 @@ uint8_t LCR_gameStep(uint32_t time)
(LCR_game.keyStates[LCR_KEY_DOWN] ? LCR_RACING_INPUT_BACK : 0) |
(LCR_game.keyStates[LCR_KEY_LEFT] ? LCR_RACING_INPUT_LEFT : 0));
#ifdef LCR_FPS_GET_MS
frameTime = LCR_FPS_GET_MS;
#endif
uint32_t events = paused ? 0 : LCR_racingStep(input);
#ifdef LCR_FPS_GET_MS
LCR_game.physicsFrameMS += LCR_FPS_GET_MS - frameTime;
LCR_game.physicsFramesMeasured++;
#endif
if (events & LCR_RACING_EVENT_CP_TAKEN)
{
int carBlock[3];
@ -1680,6 +1711,10 @@ uint8_t LCR_gameStep(uint32_t time)
while (time >= LCR_game.nextRenderFrameTime)
LCR_game.nextRenderFrameTime += 1000 / LCR_SETTING_FPS;
#ifdef LCR_FPS_GET_MS
frameTime = LCR_FPS_GET_MS;
#endif
if ((LCR_game.state == LCR_GAME_STATE_MENU) ||
LCR_game.state == LCR_GAME_STATE_LOADING)
LCR_rendererDrawMenu(LCR_texts[LCR_TEXTS_TABS
@ -1696,6 +1731,11 @@ uint8_t LCR_gameStep(uint32_t time)
LCR_gameDrawPopupMessage();
LCR_game.popupCountdown--;
}
#ifdef LCR_FPS_GET_MS
LCR_game.renderFrameMS += LCR_FPS_GET_MS - frameTime;
LCR_game.renderFramesMeasured++;
#endif
}
else
{
@ -1722,6 +1762,24 @@ uint8_t LCR_gameStep(uint32_t time)
LCR_game.frame++;
LCR_LOG2("game step (end)");
#ifdef LCR_FPS_GET_MS
if (LCR_game.renderFramesMeasured >= 64)
{
LCR_LOG0("us/frame (render):");
LCR_LOG0_NUM((LCR_game.renderFrameMS * 1000) / 64);
LCR_game.renderFramesMeasured = 0;
LCR_game.renderFrameMS = 0;
}
if (LCR_game.physicsFramesMeasured >= 64)
{
LCR_LOG0("us/frame (physics):");
LCR_LOG0_NUM((LCR_game.physicsFrameMS * 1000) / 64);
LCR_game.physicsFramesMeasured = 0;
LCR_game.physicsFrameMS = 0;
}
#endif
return 1;
}