Enumify all fade modes

This removes the magic numbers previously used for controlling the fade
mode, which are really not readable at all unless you already know what
they mean.

0: FADE_NONE
1: FADE_FULLY_BLACK
2: FADE_START_FADEOUT
3: FADE_FADING_OUT
4: FADE_START_FADEIN
5: FADE_FADING_IN

There is also the macro FADEMODE_IS_FADING, which indicates when the
intention is to only check if the game is fading right now, which wasn't
clearly conveyed previously.

I also took the opportunity to clean up the style of any lines I
touched. This included rewriting if-else chains into case-switches,
turning one-liner if-then statements into proper blocks, fixing up
comments, and even commenting the `fademode == FADE_NONE` on the tower
spike checks (which, it was previously undocumented why that check was
there, but I think I know why it's there).

As for type safety, we already get some by transforming the variable
types into the enum. Assignment is prohibited without a cast. But,
apparently, comparison is perfectly legal and won't even give so much as
a warning. To work around this and make absolutely sure I made all
existing comparisons now use the enum, I temporarily changed it to be an
`enum class`, which is a C++11 feature that makes it so all comparisons
are illegal. Unfortunately, it scopes them in a namespace with the same
name as a class, so I had to temporarily define macros to make sure my
existing code worked. I also had to temporarily up the standard in
CMakeLists.txt to get it to compile. But after all that was done, I
found the rest of the places where a comparison to an integer was used,
and fixed them.
This commit is contained in:
Misa 2022-04-25 00:57:47 -07:00
parent af1cebf7a1
commit 98cb415675
9 changed files with 159 additions and 112 deletions

View File

@ -1718,7 +1718,7 @@ void editorlogic(void)
ed.notedelay--;
}
if (graphics.fademode == 1)
if (graphics.fademode == FADE_FULLY_BLACK)
{
//Return to game
graphics.titlebg.colstate = 10;
@ -1896,7 +1896,7 @@ static void editormenuactionpress(void)
//Quit without saving
music.playef(11);
music.fadeout();
graphics.fademode = 2;
graphics.fademode = FADE_START_FADEOUT;
break;
case 2:
//Go back to editor
@ -1914,7 +1914,7 @@ static void editormenuactionpress(void)
void editorinput(void)
{
extern editorclass ed;
if (graphics.fademode == 3 /* fading out */)
if (graphics.fademode == FADE_FADING_OUT)
{
return;
}
@ -2346,7 +2346,7 @@ void editorinput(void)
if (ed.saveandquit)
{
graphics.fademode = 2; // quit editor
graphics.fademode = FADE_START_FADEOUT; /* quit editor */
}
break;
}

View File

@ -1399,7 +1399,10 @@ void Game::updatestate(void)
case 80:
//Used to return to menu from the game
if(graphics.fademode == 1) state++;
if (graphics.fademode == FADE_FULLY_BLACK)
{
state++;
}
break;
case 81:
quittomenu();
@ -1454,13 +1457,16 @@ void Game::updatestate(void)
savestatsandsettings();
graphics.fademode = 2;
graphics.fademode = FADE_START_FADEOUT;
music.fadeout();
state++;
break;
case 83:
frames--;
if(graphics.fademode == 1) state++;
if (graphics.fademode == FADE_FULLY_BLACK)
{
state++;
}
break;
case 84:
quittomenu();
@ -1537,7 +1543,10 @@ void Game::updatestate(void)
case 96:
//Used to return to gravitron to game
if(graphics.fademode == 1) state++;
if (graphics.fademode == FADE_FULLY_BLACK)
{
state++;
}
break;
case 97:
returntolab();
@ -1924,7 +1933,7 @@ void Game::updatestate(void)
{
if(map.custommodeforreal)
{
graphics.fademode = 2;
graphics.fademode = FADE_START_FADEOUT;
state=1014;
}
#ifndef NO_EDITOR
@ -1946,7 +1955,10 @@ void Game::updatestate(void)
#endif
case 1014:
frames--;
if(graphics.fademode == 1) state++;
if (graphics.fademode == FADE_FULLY_BLACK)
{
state++;
}
break;
case 1015:
#if !defined(NO_CUSTOM_LEVELS)
@ -2372,12 +2384,12 @@ void Game::updatestate(void)
}
break;
case 3055:
graphics.fademode = 2;
graphics.fademode = FADE_START_FADEOUT;
state++;
statedelay = 10;
break;
case 3056:
if(graphics.fademode==1)
if (graphics.fademode == FADE_FULLY_BLACK)
{
startscript = true;
if (crewrescued() == 6)
@ -2443,11 +2455,14 @@ void Game::updatestate(void)
case 3070:
graphics.fademode = 2;
graphics.fademode = FADE_START_FADEOUT;
state++;
break;
case 3071:
if (graphics.fademode == 1) state++;
if (graphics.fademode == FADE_FULLY_BLACK)
{
state++;
}
break;
case 3072:
//Ok, we need to adjust some flags based on who've we've rescued. Some of there conversation options
@ -2522,20 +2537,23 @@ void Game::updatestate(void)
//returning from an intermission, very like 3070
if (inintermission)
{
graphics.fademode = 2;
graphics.fademode = FADE_START_FADEOUT;
companion = 0;
state=3100;
}
else
{
unlocknum(7);
graphics.fademode = 2;
graphics.fademode = FADE_START_FADEOUT;
companion = 0;
state++;
}
break;
case 3081:
if (graphics.fademode == 1) state++;
if (graphics.fademode == FADE_FULLY_BLACK)
{
state++;
}
break;
case 3082:
map.finalmode = false;
@ -2552,21 +2570,24 @@ void Game::updatestate(void)
companion = 0;
supercrewmate = false;
state++;
graphics.fademode = 2;
graphics.fademode = FADE_START_FADEOUT;
music.fadeout();
state=3100;
}
else
{
unlocknum(6);
graphics.fademode = 2;
graphics.fademode = FADE_START_FADEOUT;
companion = 0;
supercrewmate = false;
state++;
}
break;
case 3086:
if (graphics.fademode == 1) state++;
if (graphics.fademode == FADE_FULLY_BLACK)
{
state++;
}
break;
case 3087:
map.finalmode = false;
@ -2576,7 +2597,10 @@ void Game::updatestate(void)
break;
case 3100:
if(graphics.fademode == 1) state++;
if (graphics.fademode == FADE_FULLY_BLACK)
{
state++;
}
break;
case 3101:
quittomenu();
@ -2787,18 +2811,18 @@ void Game::updatestate(void)
break;
}
case 3516:
graphics.fademode = 2;
graphics.fademode = FADE_START_FADEOUT;
state++;
break;
case 3517:
if (graphics.fademode == 1)
if (graphics.fademode == FADE_FULLY_BLACK)
{
state++;
statedelay = 30;
}
break;
case 3518:
graphics.fademode = 4;
graphics.fademode = FADE_START_FADEIN;
state = 0;
statedelay = 30;
@ -2819,11 +2843,14 @@ void Game::updatestate(void)
hascontrol = false;
crewstats[0] = true;
graphics.fademode = 2;
graphics.fademode = FADE_START_FADEOUT;
state++;
break;
case 3521:
if(graphics.fademode == 1) state++;
if (graphics.fademode == FADE_FULLY_BLACK)
{
state++;
}
break;
case 3522:
copyndmresults();
@ -6699,7 +6726,7 @@ bool Game::save_exists(void)
void Game::quittomenu(void)
{
gamestate = TITLEMODE;
graphics.fademode = 4;
graphics.fademode = FADE_START_FADEIN;
FILESYSTEM_unmountAssets();
cliplaytest = false;
graphics.titlebg.tdrawback = true;
@ -6751,7 +6778,7 @@ void Game::quittomenu(void)
void Game::returntolab(void)
{
gamestate = GAMEMODE;
graphics.fademode = 4;
graphics.fademode = FADE_START_FADEIN;
map.gotoroom(119, 107);
int player = obj.getplayer();
if (INBOUNDS_VEC(player, obj.entities))
@ -6791,7 +6818,7 @@ void Game::returntoeditor(void)
completestop = false;
state = 0;
graphics.showcutscenebars = false;
graphics.fademode = 0;
graphics.fademode = FADE_NONE;
ed.keydelay = 6;
ed.settingskey = true;

View File

@ -97,8 +97,8 @@ void Graphics::init(void)
SDL_memset(fadebars, 0, sizeof(fadebars));
setfade(0);
fademode = 0;
ingame_fademode = 0;
fademode = FADE_NONE;
ingame_fademode = FADE_NONE;
// initialize everything else to zero
backBuffer = NULL;
@ -1451,68 +1451,67 @@ void Graphics::createtextboxflipme(
void Graphics::drawfade(void)
{
int usethisamount = lerp(oldfadeamount, fadeamount);
if ((fademode == 1)||(fademode == 4))
switch (fademode)
{
case FADE_FULLY_BLACK:
case FADE_START_FADEIN:
ClearSurface(backBuffer);
}
else if(fademode==3)
{
break;
case FADE_FADING_OUT:
for (size_t i = 0; i < SDL_arraysize(fadebars); i++)
{
FillRect(backBuffer, fadebars[i], i * 16, usethisamount, 16, 0x000000 );
}
}
else if(fademode==5 )
{
break;
case FADE_FADING_IN:
for (size_t i = 0; i < SDL_arraysize(fadebars); i++)
{
FillRect(backBuffer, fadebars[i]-usethisamount, i * 16, 500, 16, 0x000000 );
}
break;
case FADE_NONE:
case FADE_START_FADEOUT:
break;
}
}
void Graphics::processfade(void)
{
oldfadeamount = fadeamount;
if (fademode > 1)
switch (fademode)
{
if (fademode == 2)
case FADE_START_FADEOUT:
for (size_t i = 0; i < SDL_arraysize(fadebars); i++)
{
//prepare fade out
for (size_t i = 0; i < SDL_arraysize(fadebars); i++)
{
fadebars[i] = -int(fRandom() * 12) * 8;
}
setfade(0);
fademode = 3;
fadebars[i] = -int(fRandom() * 12) * 8;
}
else if (fademode == 3)
setfade(0);
fademode = FADE_FADING_OUT;
break;
case FADE_FADING_OUT:
fadeamount += 24;
if (fadeamount > 416)
{
fadeamount += 24;
if (fadeamount > 416)
{
fademode = 1; //faded
}
fademode = FADE_FULLY_BLACK;
}
else if (fademode == 4)
break;
case FADE_START_FADEIN:
for (size_t i = 0; i < SDL_arraysize(fadebars); i++)
{
//prepare fade in
for (size_t i = 0; i < SDL_arraysize(fadebars); i++)
{
fadebars[i] = 320 + int(fRandom() * 12) * 8;
}
setfade(416);
fademode = 5;
fadebars[i] = 320 + int(fRandom() * 12) * 8;
}
else if (fademode == 5)
setfade(416);
fademode = FADE_FADING_IN;
break;
case FADE_FADING_IN:
fadeamount -= 24;
if (fadeamount <= 0)
{
fadeamount -= 24;
if (fadeamount <= 0)
{
fademode = 0; //normal
}
fademode = FADE_NONE;
}
case FADE_NONE:
case FADE_FULLY_BLACK:
break;
}
}

View File

@ -11,6 +11,18 @@
#include "Textbox.h"
#include "TowerBG.h"
enum FadeBars
{
FADE_NONE,
FADE_FULLY_BLACK,
FADE_START_FADEOUT,
FADE_FADING_OUT,
FADE_START_FADEIN,
FADE_FADING_IN
};
#define FADEMODE_IS_FADING(mode) ((mode) != FADE_NONE && (mode) != FADE_FULLY_BLACK)
class Graphics
{
public:
@ -289,11 +301,11 @@ public:
int crewframe;
int crewframedelay;
int fademode;
enum FadeBars fademode;
int fadeamount;
int oldfadeamount;
int fadebars[15];
int ingame_fademode;
enum FadeBars ingame_fademode;
bool trinketcolset;
int trinketr, trinketg, trinketb;

View File

@ -284,7 +284,7 @@ static int gotomode = 0;
static void startmode(const int mode)
{
gotomode = mode;
graphics.fademode = 2; /* fading out */
graphics.fademode = FADE_START_FADEOUT;
fadetomode = true;
fadetomodedelay = 19;
}
@ -1880,7 +1880,7 @@ void titleinput(void)
if (!game.press_action && !game.press_left && !game.press_right && !key.isDown(27) && !key.isDown(game.controllerButton_esc)) game.jumpheld = false;
if (!game.press_map) game.mapheld = false;
if (!game.jumpheld && graphics.fademode==0)
if (!game.jumpheld && graphics.fademode == FADE_NONE)
{
if (game.press_action || game.press_left || game.press_right || game.press_map || key.isDown(27) || key.isDown(game.controllerButton_esc))
{
@ -2074,7 +2074,7 @@ void gameinput(void)
game.interactheld = false;
}
if (game.intimetrial && graphics.fademode == 1 && game.quickrestartkludge)
if (game.intimetrial && graphics.fademode == FADE_FULLY_BLACK && game.quickrestartkludge)
{
//restart the time trial
game.quickrestartkludge = false;
@ -2367,10 +2367,10 @@ void gameinput(void)
game.gamesavefailed = false;
game.menupage = 20; // The Map Page
}
else if (game.intimetrial && graphics.fademode == 0)
else if (game.intimetrial && graphics.fademode == FADE_NONE)
{
//Quick restart of time trial
graphics.fademode = 2;
graphics.fademode = FADE_START_FADEOUT;
game.completestop = true;
music.fadeout();
game.quickrestartkludge = true;
@ -2431,7 +2431,7 @@ void mapinput(void)
game.press_map = false;
game.press_interact = false;
if (version2_2 && graphics.fademode == 1 && graphics.menuoffset == 0)
if (version2_2 && graphics.fademode == FADE_FULLY_BLACK && graphics.menuoffset == 0)
{
// Deliberate re-addition of the glitchy gamestate-based fadeout!
@ -2496,7 +2496,7 @@ void mapinput(void)
if(graphics.menuoffset==0
&& ((!version2_2 && !game.fadetomenu && game.fadetomenudelay <= 0 && !game.fadetolab && game.fadetolabdelay <= 0)
|| graphics.fademode == 0))
|| graphics.fademode == FADE_NONE))
{
if (key.isDown(KEYBOARD_LEFT) || key.isDown(KEYBOARD_UP) || key.isDown(KEYBOARD_a) || key.isDown(KEYBOARD_w)|| key.controllerWantsLeft(true))
{
@ -2672,7 +2672,7 @@ static void mapmenuactionpress(const bool version2_2)
//Kill contents of offset render buffer, since we do that for some reason.
//This fixes an apparent frame flicker.
ClearSurface(graphics.tempBuffer);
graphics.fademode = 2;
graphics.fademode = FADE_START_FADEOUT;
music.fadeout();
map.nexttowercolour();
if (!version2_2)
@ -2691,7 +2691,7 @@ static void mapmenuactionpress(const bool version2_2)
case 21:
//quit to menu
game.swnmode = false;
graphics.fademode = 2;
graphics.fademode = FADE_START_FADEOUT;
music.fadeout();
if (!version2_2)
{
@ -2712,7 +2712,7 @@ static void mapmenuactionpress(const bool version2_2)
graphics.flipmode = false;
game.ingame_titlemode = true;
graphics.ingame_fademode = graphics.fademode;
graphics.fademode = 0;
graphics.fademode = FADE_NONE;
// Set this before we create the menu
game.kludge_ingametemp = game.currentmenuname;
@ -2892,9 +2892,9 @@ void gamecompleteinput(void)
game.creditposition -= 6;
if (game.creditposition <= -Credits::creditmaxposition)
{
if(graphics.fademode==0)
if (graphics.fademode == FADE_NONE)
{
graphics.fademode = 2;
graphics.fademode = FADE_START_FADEOUT;
}
game.creditposition = -Credits::creditmaxposition;
}
@ -2911,9 +2911,9 @@ void gamecompleteinput(void)
if(game.press_map)
{
//Return to game
if(graphics.fademode==0)
if(graphics.fademode == FADE_NONE)
{
graphics.fademode = 2;
graphics.fademode = FADE_START_FADEOUT;
}
}
}
@ -2936,9 +2936,9 @@ void gamecompleteinput2(void)
game.oldcreditposx++;
if (game.creditposy >= 30)
{
if(graphics.fademode==0)
if(graphics.fademode == FADE_NONE)
{
graphics.fademode = 2;
graphics.fademode = FADE_START_FADEOUT;
music.fadeout();
}
}
@ -2951,9 +2951,9 @@ void gamecompleteinput2(void)
if(game.press_map)
{
//Return to game
if(graphics.fademode==0)
if(graphics.fademode == FADE_NONE)
{
graphics.fademode = 2;
graphics.fademode = FADE_START_FADEOUT;
music.fadeout();
}
}

View File

@ -68,7 +68,7 @@ void gamecompletelogic(void)
graphics.titlebg.bscroll = +1;
}
if (graphics.fademode == 1)
if (graphics.fademode == FADE_FULLY_BLACK)
{
//Fix some graphical things
graphics.showcutscenebars = false;
@ -77,7 +77,7 @@ void gamecompletelogic(void)
graphics.titlebg.bypos = 0;
//Return to game
game.gamestate = GAMECOMPLETE2;
graphics.fademode = 4;
graphics.fademode = FADE_START_FADEIN;
}
}
@ -100,7 +100,7 @@ void gamecompletelogic2(void)
}
}
if (graphics.fademode == 1)
if (graphics.fademode == FADE_FULLY_BLACK)
{
//Fix some graphical things
graphics.showcutscenebars = false;
@ -457,8 +457,11 @@ void gamelogic(void)
game.deathseq = 1;
game.gethardestroom();
//start depressing sequence here...
if (game.gameoverdelay <= -10 && graphics.fademode==0) graphics.fademode = 2;
if (graphics.fademode == 1)
if (game.gameoverdelay <= -10 && graphics.fademode == FADE_NONE)
{
graphics.fademode = FADE_START_FADEOUT;
}
if (graphics.fademode == FADE_FULLY_BLACK)
{
game.copyndmresults();
script.resetgametomenu();
@ -878,7 +881,10 @@ void gamelogic(void)
{
//special for tower: is the player touching any spike blocks?
int player = obj.getplayer();
if(INBOUNDS_VEC(player, obj.entities) && obj.checktowerspikes(player) && graphics.fademode==0)
if (INBOUNDS_VEC(player, obj.entities)
&& obj.checktowerspikes(player)
/* not really needed, but is slight improvement when exiting to menu near spikes */
&& graphics.fademode == FADE_NONE)
{
game.deathseq = 30;
}

View File

@ -1716,7 +1716,11 @@ void gamerender(void)
}
}
if (graphics.fademode==0 && !game.intimetrial && !game.isingamecompletescreen() && (!game.swnmode || game.swngame != 1) && game.showingametimer)
if (graphics.fademode == FADE_NONE
&& !game.intimetrial
&& !game.isingamecompletescreen()
&& (!game.swnmode || game.swngame != 1)
&& game.showingametimer)
{
char buffer[SCREEN_WIDTH_CHARS + 1];
graphics.bprint(6, 6, "TIME:", 255,255,255);
@ -1911,7 +1915,7 @@ void gamerender(void)
}
}
if (game.intimetrial && graphics.fademode==0)
if (game.intimetrial && graphics.fademode == FADE_NONE)
{
//Draw countdown!
if (game.timetrialcountdown > 0)
@ -2703,8 +2707,7 @@ void maprender(void)
// being jankily brought down in glitchrunner mode when exiting to the title
// Otherwise, there's no reason to obscure the menu
if (GlitchrunnerMode_less_than_or_equal(Glitchrunner2_2)
|| graphics.fademode == 3
|| graphics.fademode == 5
|| FADEMODE_IS_FADING(graphics.fademode)
|| game.fadetomenu
|| game.fadetolab)
{

View File

@ -1298,19 +1298,19 @@ void scriptclass::run(void)
else if (words[0] == "befadein")
{
graphics.setfade(0);
graphics.fademode= 0;
graphics.fademode = FADE_NONE;
}
else if (words[0] == "fadein")
{
graphics.fademode = 4;
graphics.fademode = FADE_START_FADEIN;
}
else if (words[0] == "fadeout")
{
graphics.fademode = 2;
graphics.fademode = FADE_START_FADEOUT;
}
else if (words[0] == "untilfade")
{
if (graphics.fademode>1)
if (FADEMODE_IS_FADING(graphics.fademode))
{
scriptdelay = 1;
position--;
@ -1383,7 +1383,7 @@ void scriptclass::run(void)
#endif
{
game.gamestate = GAMECOMPLETE;
graphics.fademode = 4;
graphics.fademode = FADE_START_FADEIN;
game.creditposition = 0;
}
}
@ -2374,7 +2374,7 @@ static void gotoerrorloadinglevel(void)
{
game.createmenu(Menu::errorloadinglevel);
map.nexttowercolour();
graphics.fademode = 4; /* start fade in */
graphics.fademode = FADE_START_FADEIN; /* start fade in */
music.currentsong = -1; /* otherwise music.play won't work */
music.play(6); /* title screen music */
}
@ -2424,7 +2424,7 @@ void scriptclass::startgamemode( int t )
map.resetplayer();
map.gotoroom(game.saverx, game.savery);
map.initmapdata();
graphics.fademode = 4;
graphics.fademode = FADE_START_FADEIN;
break;
case 2: //Load Quicksave
game.gamestate = GAMEMODE;
@ -2460,7 +2460,7 @@ void scriptclass::startgamemode( int t )
map.cameramode = 0;
map.colsuperstate = 0;
}
graphics.fademode = 4;
graphics.fademode = FADE_START_FADEIN;
break;
case 3:
case 4:
@ -2522,7 +2522,7 @@ void scriptclass::startgamemode( int t )
map.resetplayer();
map.gotoroom(game.saverx, game.savery);
map.initmapdata();
graphics.fademode = 4;
graphics.fademode = FADE_START_FADEIN;
break;
case 9:
game.gamestate = GAMEMODE;
@ -2597,7 +2597,7 @@ void scriptclass::startgamemode( int t )
map.gotoroom(game.saverx, game.savery);
map.initmapdata();
music.play(11);
graphics.fademode = 4;
graphics.fademode = FADE_START_FADEIN;
break;
case 12:
game.gamestate = GAMEMODE;
@ -2857,7 +2857,7 @@ void scriptclass::startgamemode( int t )
map.resetplayer();
map.gotoroom(game.saverx, game.savery);
map.initmapdata();
graphics.fademode = 4;
graphics.fademode = FADE_START_FADEIN;
break;
case 21: //play custom level (in editor)
game.gamestate = GAMEMODE;
@ -2934,7 +2934,7 @@ void scriptclass::startgamemode( int t )
}else{
music.currentsong=-1;
}
graphics.fademode = 4;
graphics.fademode = FADE_START_FADEIN;
break;
}
case 23: //Continue in custom level
@ -2972,7 +2972,7 @@ void scriptclass::startgamemode( int t )
map.gotoroom(game.saverx, game.savery);
map.initmapdata();
cl.generatecustomminimap();
graphics.fademode = 4;
graphics.fademode = FADE_START_FADEIN;
break;
}
#endif /* NO_CUSTOM_LEVELS */

View File

@ -656,7 +656,7 @@ int main(int argc, char *argv[])
script.startgamemode(22);
}
graphics.fademode = 0;
graphics.fademode = FADE_NONE;
}
#endif