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

@@ -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 */