1
0
Fork 0
mirror of https://github.com/TerryCavanagh/VVVVVV.git synced 2024-11-04 18:29:41 +01:00

Generalize game loop order and fix it to what it was in 2.2

Okay, so the reason why all render functions were moved to the end of
the frame in #220 is because it's simpler to call two fixed functions
and then a delta function instead of one fixed function, then a delta
function, and then another fixed function.

This is because fixed functions need special handling inside
deltaloop(), and you can't simply duplicate this handling after calling
a delta function. Oh, and to make matters worse, it's not always
fixed-delta-fixed, sometimes (like in MAPMODE and TELEPORTERMODE) it's
delta-fixed-fixed, so we'd need to handle that somehow too.

The solution here is to generalize the game loop and factor out each
function, instead of hardcoding it. Instead of having hardcoded
case-switches directly in the loop, I made a function that returns an
array of functions for a given gamestate, along with the number of
functions, then the game loop processes it accordingly. In fixedloop(),
it iterates over the array and executes each function until it reaches a
delta function, at which point it stops. And when it reaches the end of
the array, it goes back to the start of the array.

But anyway, if it gets to a delta function, it'll stop the loop and
finish fixedloop(). Then deltaloop() will call the delta function. And
then on the next frame, the function index will be incremented again, so
fixedloop() will call the fixed functions again.

Actually, the previous game loop was actually made up of one big loop,
with a gamestate function loop nested inside it, flanked with code that
ran at the start and end of the "big loop". This would be easy to handle
with one loop (just include the beginning and end functions with the
gamestate functions in the array), except that the gamestate functions
could suddenly be swapped out with unfocused functions (the ones that
run when you unfocus the window) at any time (well, on frame boundaries,
since key.isActive only got checked once, guarding the entire "inner
loop" - and I made sure that changing key.isActive wouldn't immediately
apply, just like the previous game loop order) - so I had to add yet
another layer of indirection, where the gamestate functions could
immediately be swapped out with the unfocused functions (while still
running the beginning and end code, because that was how the previous
loop order worked, after all).

This also fixes a regression that the game loop that #220 introduced
had, where if the fixed functions switched the gamestate, the game would
prematurely start rendering the gamestate function of the new gamestate
in the deltaframes, which was a source of some deltaframe glitches. But
fixing this is likely to just as well cause deltaframe glitches, so it'd
be better to fix this along with fixing the loop order, and only have
one round of QA to do in the end, instead of doing one round after each
change separately.

Fixes #464... but this isn't the end of the patchset. There are bugs
that need to be fixed, and kludges that need to be reverted.
This commit is contained in:
Misa 2020-11-06 16:08:22 -08:00 committed by Ethan Lee
parent 5e440ac48d
commit 1e9fb6aac0

View file

@ -73,8 +73,293 @@ static inline Uint32 get_framerate(const int slowdown)
return 34; return 34;
} }
enum FuncType
{
Func_null,
Func_fixed,
Func_delta
};
struct ImplFunc
{
enum FuncType type;
void (*func)(void);
};
static void runscript(void)
{
script.run();
}
static void gamemodefunc1(void)
{
// WARNING: If updating this code, don't forget to update Map.cpp mapclass::twoframedelayfix()
// Ugh, I hate this kludge variable but it's the only way to do it
if (script.dontrunnextframe)
{
script.dontrunnextframe = false;
}
else
{
runscript();
}
//Update old lerp positions of entities - has to be done BEFORE gameinput!
for (size_t i = 0; i < obj.entities.size(); i++)
{
obj.entities[i].lerpoldxp = obj.entities[i].xp;
obj.entities[i].lerpoldyp = obj.entities[i].yp;
}
}
static void teleportermodeinput(void)
{
if (game.useteleporter)
{
teleporterinput();
}
else
{
script.run();
gameinput();
}
}
/* Only gets used in EDITORMODE. I assume the compiler will optimize this away
* if this is a NO_CUSTOM_LEVELS or NO_EDITOR build
*/
static void flipmodeoff(void)
{
graphics.flipmode = false;
}
static void focused_begin(void);
static void focused_end(void);
static const inline struct ImplFunc* get_gamestate_funcs(
const int gamestate,
int* num_implfuncs
) {
switch (gamestate)
{
#define FUNC_LIST_BEGIN(GAMESTATE) \
case GAMESTATE: \
{ \
static const struct ImplFunc implfuncs[] = { \
{Func_fixed, focused_begin},
#define FUNC_LIST_END \
{Func_fixed, focused_end} \
}; \
*num_implfuncs = SDL_arraysize(implfuncs); \
return implfuncs; \
}
FUNC_LIST_BEGIN(GAMEMODE)
{Func_fixed, gamemodefunc1},
{Func_fixed, gameinput},
{Func_fixed, gamerenderfixed},
{Func_delta, gamerender},
{Func_fixed, gamelogic},
FUNC_LIST_END
FUNC_LIST_BEGIN(TITLEMODE)
{Func_fixed, titleinput},
{Func_fixed, titlerenderfixed},
{Func_delta, titlerender},
{Func_fixed, titlelogic},
FUNC_LIST_END
FUNC_LIST_BEGIN(MAPMODE)
{Func_fixed, maprenderfixed},
{Func_delta, maprender},
{Func_fixed, mapinput},
{Func_fixed, maplogic},
FUNC_LIST_END
FUNC_LIST_BEGIN(TELEPORTERMODE)
{Func_fixed, maprenderfixed},
{Func_delta, teleporterrender},
{Func_fixed, teleportermodeinput},
{Func_fixed, maplogic},
FUNC_LIST_END
FUNC_LIST_BEGIN(GAMECOMPLETE)
{Func_fixed, gamecompleterenderfixed},
{Func_delta, gamecompleterender},
{Func_fixed, gamecompleteinput},
{Func_fixed, gamecompletelogic},
FUNC_LIST_END
FUNC_LIST_BEGIN(GAMECOMPLETE2)
{Func_delta, gamecompleterender2},
{Func_fixed, gamecompleteinput2},
{Func_fixed, gamecompletelogic2},
FUNC_LIST_END
#if !defined(NO_CUSTOM_LEVELS) && !defined(NO_EDITOR)
FUNC_LIST_BEGIN(EDITORMODE)
{Func_fixed, flipmodeoff},
{Func_fixed, editorinput},
{Func_fixed, editorrenderfixed},
{Func_delta, editorrender},
{Func_fixed, editorlogic},
FUNC_LIST_END
#endif
FUNC_LIST_BEGIN(PRELOADER)
{Func_fixed, preloaderinput},
{Func_fixed, preloaderrenderfixed},
{Func_delta, preloaderrender},
FUNC_LIST_END
#undef FUNC_LIST_END
#undef FUNC_LIST_BEGIN
}
SDL_assert(0 && "Invalid gamestate!");
return NULL;
}
enum IndexCode
{
Index_none,
Index_end
};
static const struct ImplFunc* gamestate_funcs = NULL;
static int num_gamestate_funcs = 0;
static int gamestate_func_index = -1;
static enum IndexCode increment_gamestate_func_index(void)
{
gamestate_func_index++;
if (gamestate_func_index == num_gamestate_funcs)
{
/* Reached the end of current gamestate order.
* Re-fetch for new order if gamestate changed.
*/
gamestate_funcs = get_gamestate_funcs(
game.gamestate,
&num_gamestate_funcs
);
gamestate_func_index = 0;
return Index_end;
}
return Index_none;
}
static void unfocused_run(void);
static const struct ImplFunc unfocused_func_list[] = {
Func_fixed,
unfocused_run
};
static const struct ImplFunc* unfocused_funcs = unfocused_func_list;
static int num_unfocused_funcs = SDL_arraysize(unfocused_func_list);
static int unfocused_func_index = -1;
static enum IndexCode increment_unfocused_func_index(void)
{
unfocused_func_index++;
if (unfocused_func_index == num_unfocused_funcs)
{
unfocused_func_index = 0;
return Index_end;
}
return Index_none;
}
static const struct ImplFunc** active_funcs = NULL;
static int* num_active_funcs = NULL;
static int* active_func_index = NULL;
static enum IndexCode (*increment_func_index)(void) = NULL;
enum LoopCode
{
Loop_continue,
Loop_stop
};
static enum LoopCode loop_assign_active_funcs(void)
{
if (key.isActive)
{
active_funcs = &gamestate_funcs;
num_active_funcs = &num_gamestate_funcs;
active_func_index = &gamestate_func_index;
increment_func_index = &increment_gamestate_func_index;
}
else
{
active_funcs = &unfocused_funcs;
num_active_funcs = &num_unfocused_funcs;
active_func_index = &unfocused_func_index;
increment_func_index = &increment_unfocused_func_index;
}
return Loop_continue;
}
static enum LoopCode loop_run_active_funcs(void)
{
while ((*active_funcs)[*active_func_index].type != Func_delta)
{
const struct ImplFunc* implfunc = &(*active_funcs)[*active_func_index];
enum IndexCode index_code;
if (implfunc->type != Func_null && implfunc->func != NULL)
{
implfunc->func();
}
index_code = increment_func_index();
if (index_code == Index_end)
{
return Loop_continue;
}
}
return Loop_stop;
}
static enum LoopCode loop_begin(void);
static enum LoopCode loop_end(void);
static enum LoopCode (*const meta_funcs[])(void) = {
loop_begin,
loop_assign_active_funcs,
loop_run_active_funcs,
loop_end
};
static int meta_func_index = 0;
static void inline fixedloop(void)
{
while (true)
{
enum LoopCode loop_code = meta_funcs[meta_func_index]();
if (loop_code == Loop_stop)
{
break;
}
meta_func_index = (meta_func_index + 1) % SDL_arraysize(meta_funcs);
}
}
static void inline deltaloop(void); static void inline deltaloop(void);
static void inline fixedloop(void);
static void cleanup(void); static void cleanup(void);
@ -336,6 +621,9 @@ int main(int argc, char *argv[])
key.isActive = true; key.isActive = true;
game.gametimer = 0; game.gametimer = 0;
gamestate_funcs = get_gamestate_funcs(game.gamestate, &num_gamestate_funcs);
loop_assign_active_funcs();
while(!key.quitProgram) while(!key.quitProgram)
{ {
f_time = SDL_GetTicks(); f_time = SDL_GetTicks();
@ -402,6 +690,8 @@ static void inline deltaloop(void)
while (accumulator >= timesteplimit) while (accumulator >= timesteplimit)
{ {
increment_func_index();
accumulator = SDL_fmodf(accumulator, timesteplimit); accumulator = SDL_fmodf(accumulator, timesteplimit);
fixedloop(); fixedloop();
@ -409,54 +699,39 @@ static void inline deltaloop(void)
const float alpha = game.over30mode ? static_cast<float>(accumulator) / timesteplimit : 1.0f; const float alpha = game.over30mode ? static_cast<float>(accumulator) / timesteplimit : 1.0f;
graphics.alpha = alpha; graphics.alpha = alpha;
if (key.isActive) if (active_func_index == NULL
|| *active_func_index == -1
|| active_funcs == NULL)
{ {
switch (game.gamestate) /* Somehow the first deltatime has been too small and things haven't
{ * initialized. We'll just no-op for now.
case PRELOADER: */
preloaderrender();
break;
#if !defined(NO_CUSTOM_LEVELS) && !defined(NO_EDITOR)
case EDITORMODE:
graphics.flipmode = false;
editorrender();
break;
#endif
case TITLEMODE:
titlerender();
break;
case GAMEMODE:
gamerender();
break;
case MAPMODE:
maprender();
break;
case TELEPORTERMODE:
teleporterrender();
break;
case GAMECOMPLETE:
gamecompleterender();
break;
case GAMECOMPLETE2:
gamecompleterender2();
break;
case CLICKTOSTART:
help.updateglow();
break;
} }
else
{
const struct ImplFunc* implfunc = &(*active_funcs)[*active_func_index];
if (implfunc->type == Func_delta && implfunc->func != NULL)
{
implfunc->func();
gameScreen.FlipScreen(); gameScreen.FlipScreen();
} }
}
} }
static void inline fixedloop(void) static enum LoopCode loop_begin(void)
{ {
// Update network per frame. // Update network per frame.
NETWORK_update(); NETWORK_update();
key.Poll(); key.Poll();
if(!key.isActive) return Loop_continue;
{ }
static void unfocused_run(void)
{
Mix_Pause(-1); Mix_Pause(-1);
Mix_PauseMusic(); Mix_PauseMusic();
@ -472,116 +747,31 @@ static void inline fixedloop(void)
gameScreen.FlipScreen(); gameScreen.FlipScreen();
//We are minimised, so lets put a bit of a delay to save CPU //We are minimised, so lets put a bit of a delay to save CPU
SDL_Delay(100); SDL_Delay(100);
} }
else
{ static void focused_begin(void)
{
Mix_Resume(-1); Mix_Resume(-1);
Mix_ResumeMusic(); Mix_ResumeMusic();
game.gametimer++; game.gametimer++;
}
switch(game.gamestate) static void focused_end(void)
{ {
case PRELOADER:
preloaderinput();
preloaderrenderfixed();
break;
#if !defined(NO_CUSTOM_LEVELS) && !defined(NO_EDITOR)
case EDITORMODE:
//Input
editorinput();
////Logic
editorlogic();
editorrenderfixed();
break;
#endif
case TITLEMODE:
//Input
titleinput();
////Logic
titlelogic();
titlerenderfixed();
break;
case GAMEMODE:
// WARNING: If updating this code, don't forget to update Map.cpp mapclass::twoframedelayfix()
// Ugh, I hate this kludge variable but it's the only way to do it
if (script.dontrunnextframe)
{
script.dontrunnextframe = false;
}
else
{
script.run();
}
//Update old lerp positions of entities - has to be done BEFORE gameinput!
for (size_t i = 0; i < obj.entities.size(); i++)
{
obj.entities[i].lerpoldxp = obj.entities[i].xp;
obj.entities[i].lerpoldyp = obj.entities[i].yp;
}
gameinput();
gamerenderfixed();
gamelogic();
break;
case MAPMODE:
mapinput();
maplogic();
maprenderfixed();
break;
case TELEPORTERMODE:
if(game.useteleporter)
{
teleporterinput();
}
else
{
script.run();
gameinput();
}
maplogic();
teleporterrenderfixed();
break;
case GAMECOMPLETE:
//Input
gamecompleteinput();
//Logic
gamecompletelogic();
gamecompleterenderfixed();
break;
case GAMECOMPLETE2:
//Input
gamecompleteinput2();
//Logic
gamecompletelogic2();
break;
case CLICKTOSTART:
break;
default:
break;
}
}
//Screen effects timers //Screen effects timers
if (key.isActive && game.flashlight > 0) if (game.flashlight > 0)
{ {
game.flashlight--; game.flashlight--;
} }
if (key.isActive && game.screenshake > 0) if (game.screenshake > 0)
{ {
game.screenshake--; game.screenshake--;
graphics.updatescreenshake(); graphics.updatescreenshake();
} }
}
static enum LoopCode loop_end(void)
{
if (graphics.screenbuffer->badSignalEffect) if (graphics.screenbuffer->badSignalEffect)
{ {
UpdateFilter(); UpdateFilter();
@ -646,4 +836,6 @@ static void inline fixedloop(void)
music.processmusic(); music.processmusic();
graphics.processfade(); graphics.processfade();
game.gameclock(); game.gameclock();
return Loop_continue;
} }