mirror of
https://gitlab.com/drummyfish/anarch.git
synced 2024-11-28 03:42:15 -05:00
Improve main loop
This commit is contained in:
parent
aca237d825
commit
9805b34f23
29
game.h
29
game.h
@ -352,7 +352,6 @@ struct
|
|||||||
drawing. */
|
drawing. */
|
||||||
uint32_t frameTime; ///< Keeps a constant time (in ms) during a frame
|
uint32_t frameTime; ///< Keeps a constant time (in ms) during a frame
|
||||||
uint32_t frame;
|
uint32_t frame;
|
||||||
uint32_t lastFrameTimeMs;
|
|
||||||
uint8_t selectedMenuItem;
|
uint8_t selectedMenuItem;
|
||||||
uint8_t selectedLevel; ///< Level to play selected in the main menu.
|
uint8_t selectedLevel; ///< Level to play selected in the main menu.
|
||||||
uint8_t antiSpam; ///< Prevents log message spamming.
|
uint8_t antiSpam; ///< Prevents log message spamming.
|
||||||
@ -1320,6 +1319,8 @@ void SFG_initPlayer()
|
|||||||
{
|
{
|
||||||
RCL_initCamera(&SFG_player.camera);
|
RCL_initCamera(&SFG_player.camera);
|
||||||
|
|
||||||
|
SFG_game.frameTime = SFG_getTimeMs();
|
||||||
|
|
||||||
SFG_player.camera.resolution.x =
|
SFG_player.camera.resolution.x =
|
||||||
SFG_GAME_RESOLUTION_X / SFG_RAYCASTING_SUBSAMPLE;
|
SFG_GAME_RESOLUTION_X / SFG_RAYCASTING_SUBSAMPLE;
|
||||||
|
|
||||||
@ -1694,7 +1695,6 @@ void SFG_init()
|
|||||||
|
|
||||||
SFG_game.backgroundScroll = 0;
|
SFG_game.backgroundScroll = 0;
|
||||||
SFG_currentLevel.levelPointer = 0;
|
SFG_currentLevel.levelPointer = 0;
|
||||||
SFG_game.lastFrameTimeMs = SFG_getTimeMs();
|
|
||||||
SFG_game.selectedMenuItem = 0;
|
SFG_game.selectedMenuItem = 0;
|
||||||
SFG_game.selectedLevel = 0;
|
SFG_game.selectedLevel = 0;
|
||||||
SFG_game.settings = 0x03;
|
SFG_game.settings = 0x03;
|
||||||
@ -4771,32 +4771,29 @@ uint8_t SFG_mainLoopBody()
|
|||||||
Each game logic (physics) frame is performed with the SFG_MS_PER_FRAME
|
Each game logic (physics) frame is performed with the SFG_MS_PER_FRAME
|
||||||
delta time. */
|
delta time. */
|
||||||
uint32_t timeNow = SFG_getTimeMs();
|
uint32_t timeNow = SFG_getTimeMs();
|
||||||
uint32_t timeNextFrame = SFG_game.lastFrameTimeMs + SFG_MS_PER_FRAME;
|
int32_t timeSinceLastFrame = timeNow - SFG_game.frameTime;
|
||||||
|
|
||||||
SFG_game.frameTime = timeNow;
|
if (timeSinceLastFrame >= SFG_MS_PER_FRAME)
|
||||||
|
|
||||||
if (timeNow >= timeNextFrame)
|
|
||||||
{
|
{
|
||||||
uint32_t timeSinceLastFrame = timeNow - SFG_game.lastFrameTimeMs;
|
|
||||||
|
|
||||||
uint8_t steps = 0;
|
uint8_t steps = 0;
|
||||||
|
|
||||||
uint8_t previousWeapon = SFG_player.weapon;
|
|
||||||
|
|
||||||
// perform game logic (physics etc.), for each frame
|
|
||||||
while (timeSinceLastFrame >= SFG_MS_PER_FRAME)
|
while (timeSinceLastFrame >= SFG_MS_PER_FRAME)
|
||||||
{
|
{
|
||||||
|
uint8_t previousWeapon = SFG_player.weapon;
|
||||||
|
|
||||||
|
SFG_game.frameTime += SFG_MS_PER_FRAME;
|
||||||
|
|
||||||
SFG_gameStep();
|
SFG_gameStep();
|
||||||
|
|
||||||
|
if (SFG_player.weapon != previousWeapon)
|
||||||
|
SFG_processEvent(SFG_EVENT_PLAYER_CHANGES_WEAPON,SFG_player.weapon);
|
||||||
|
|
||||||
timeSinceLastFrame -= SFG_MS_PER_FRAME;
|
timeSinceLastFrame -= SFG_MS_PER_FRAME;
|
||||||
|
|
||||||
SFG_game.frame++;
|
SFG_game.frame++;
|
||||||
steps++;
|
steps++;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (SFG_player.weapon != previousWeapon)
|
|
||||||
SFG_processEvent(SFG_EVENT_PLAYER_CHANGES_WEAPON,SFG_player.weapon);
|
|
||||||
|
|
||||||
if ((steps > 1) && (SFG_game.antiSpam == 0))
|
if ((steps > 1) && (SFG_game.antiSpam == 0))
|
||||||
{
|
{
|
||||||
SFG_LOG("failed to reach target FPS! consider setting a lower value")
|
SFG_LOG("failed to reach target FPS! consider setting a lower value")
|
||||||
@ -4808,12 +4805,10 @@ uint8_t SFG_mainLoopBody()
|
|||||||
|
|
||||||
// render noly once
|
// render noly once
|
||||||
SFG_draw();
|
SFG_draw();
|
||||||
|
|
||||||
SFG_game.lastFrameTimeMs = timeNow;
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
SFG_sleepMs((timeNextFrame - timeNow) / 2); // wait, relieve CPU
|
SFG_sleepMs((SFG_game.frameTime + SFG_MS_PER_FRAME - timeNow) / 2); // wait, relieve CPU
|
||||||
}
|
}
|
||||||
|
|
||||||
return SFG_game.continues;
|
return SFG_game.continues;
|
||||||
|
Loading…
Reference in New Issue
Block a user