From 4f6835c485f3e07b832a9eab9769153e0b81e644 Mon Sep 17 00:00:00 2001 From: Misa Date: Wed, 15 Apr 2020 17:59:03 -0700 Subject: [PATCH] De-duplicate copy-pasted input/render code in menus This removes duplicate code that came about as a result of various possible permutations of menu options, depending on being M&P, having no custom level support, having no editor support, and having MMMMMM. The menus with such permutations are the following: - main menu - "start game" is gone in MAKEANDPLAY - "player levels" is gone in NO_CUSTOM_LEVELS - "view credits" is gone in MAKEANDPLAY - "game options" - "unlock play data" is gone in MAKEANDPLAY - "soundtrack" is gone if you don't have an mmmmmm.vvv file - "player levels" - "level editor" is gone in NO_EDITOR I achieve this de-duplication by clever use of calculating offsets, which I feel is the best way to de-duplicate the code with the least amount of work, if a little brittle. The other options are to (1) put function pointers on each MenuOption object, which is pretty verbose and would inflate Game::createmenu() by a lot, (2) switch all game.currentmenuoption checks to instead check for the text of the currently-selected menu option, which is very error-prone because if you make a typo it won't be caught at compile-time, (3) add a unique ID to each MenuOption object that represents a text but will error at compile-time if you make a typo, however this just duplicates all the menu option text, which is more code than was duplicated previously. So I just went with this one. --- desktop_version/src/Input.cpp | 265 +++++++++------------------------ desktop_version/src/Render.cpp | 45 ++---- 2 files changed, 85 insertions(+), 225 deletions(-) diff --git a/desktop_version/src/Input.cpp b/desktop_version/src/Input.cpp index 47fab70d..a57f23b0 100644 --- a/desktop_version/src/Input.cpp +++ b/desktop_version/src/Input.cpp @@ -114,39 +114,21 @@ void menuactionpress() { if (game.currentmenuname == "mainmenu") { - #if defined(MAKEANDPLAY) - if (game.currentmenuoption == 0) - { - //Bring you to the normal playmenu - music.playef(11); - game.createmenu("playerworlds"); - map.nexttowercolour(); - } - else if (game.currentmenuoption == 1) - { - //Options - music.playef(11); - game.createmenu("graphicoptions"); - map.nexttowercolour(); - } - else if (game.currentmenuoption == 2) - { - //Options - music.playef(11); - game.createmenu("options"); - map.nexttowercolour(); - } - else if (game.currentmenuoption == 3) - { - //bye! - music.playef(2); - game.mainmenu = 100; - graphics.fademode = 2; - } - } -#elif !defined(MAKEANDPLAY) - #if defined(NO_CUSTOM_LEVELS) +#define MPOFFSET -1 +#else +#define MPOFFSET 0 +#endif + +#if defined(NO_CUSTOM_LEVELS) +#define NOCUSTOMSOFFSET -1 +#else +#define NOCUSTOMSOFFSET 0 +#endif + +#define OFFSET (MPOFFSET+NOCUSTOMSOFFSET) + +#if !defined(MAKEANDPLAY) if (game.currentmenuoption == 0) { //Play @@ -164,90 +146,55 @@ void menuactionpress() map.nexttowercolour(); } } - else if (game.currentmenuoption == 1) - { - //Options - music.playef(11); - game.createmenu("graphicoptions"); - map.nexttowercolour(); - } - else if (game.currentmenuoption == 2) - { - //Options - music.playef(11); - game.createmenu("options"); - map.nexttowercolour(); - } - else if (game.currentmenuoption == 3) - { - //Credits - music.playef(11); - game.createmenu("credits"); - map.nexttowercolour(); - } - else if (game.currentmenuoption == 4) - { - //bye! - music.playef(2); - game.mainmenu = 100; - graphics.fademode = 2; - } - #else - if (game.currentmenuoption == 0) - { - //Play - if (game.telesummary == "" && game.quicksummary == "") - { - //No saves exist, just start a new game - game.mainmenu = 0; - graphics.fademode = 2; - } - else - { - //Bring you to the normal playmenu - music.playef(11); - game.createmenu("play"); - map.nexttowercolour(); - } - } - else if (game.currentmenuoption == 1) + else +#endif +#if !defined(NO_CUSTOM_LEVELS) + if (game.currentmenuoption == OFFSET+1) { //Bring you to the normal playmenu music.playef(11); game.createmenu("playerworlds"); map.nexttowercolour(); } - else if (game.currentmenuoption == 2) + else +#endif + if (game.currentmenuoption == OFFSET+2) { //Options music.playef(11); game.createmenu("graphicoptions"); map.nexttowercolour(); } - else if (game.currentmenuoption == 3) + else if (game.currentmenuoption == OFFSET+3) { //Options music.playef(11); game.createmenu("options"); map.nexttowercolour(); } - else if (game.currentmenuoption == 4) +#if !defined(MAKEANDPLAY) + else if (game.currentmenuoption == OFFSET+4) { //Credits music.playef(11); game.createmenu("credits"); map.nexttowercolour(); } - else if (game.currentmenuoption == 5) +#else + #undef MPOFFSET + #define MPOFFSET -2 +#endif + else if (game.currentmenuoption == OFFSET+5) { //bye! music.playef(2); game.mainmenu = 100; graphics.fademode = 2; } - #endif +#undef OFFSET +#undef NOCUSTOMSOFFSET +#undef MPOFFSET } -#endif #if !defined(NO_CUSTOM_LEVELS) else if(game.currentmenuname=="levellist") { @@ -305,7 +252,11 @@ void menuactionpress() #if !defined(NO_CUSTOM_LEVELS) else if(game.currentmenuname=="playerworlds") { - #if !defined(NO_EDITOR) + #if defined(NO_EDITOR) + #define OFFSET -1 + #else + #define OFFSET 0 + #endif if(game.currentmenuoption==0){ music.playef(11); @@ -314,13 +265,17 @@ void menuactionpress() game.loadcustomlevelstats(); //Should only load a file if it's needed game.createmenu("levellist"); map.nexttowercolour(); - }else if(game.currentmenuoption==1){ + } + #if !defined(NO_EDITOR) + if(game.currentmenuoption==1){ //LEVEL EDITOR HOOK music.playef(11); game.mainmenu = 20; graphics.fademode = 2; ed.filename=""; - }/*else if(game.currentmenuoption==2){ + } + #endif + /*else if(game.currentmenuoption==offset+2){ music.playef(11); //"OPENFOLDERHOOK" //When the player selects the "open level folder" menu option, @@ -330,27 +285,13 @@ void menuactionpress() // - Open the levels folder for whatever operating system we're on SDL_assert(0 && "Remove open level dir"); - }*/else if(game.currentmenuoption==2){ + }*/else if(game.currentmenuoption==OFFSET+2){ //back music.playef(11); game.createmenu("mainmenu"); map.nexttowercolour(); } - #else - if(game.currentmenuoption==0){ - music.playef(11); - game.levelpage=0; - ed.getDirectoryData(); - game.loadcustomlevelstats(); //Should only load a file if it's needed - game.createmenu("levellist"); - map.nexttowercolour(); - }else if(game.currentmenuoption==1){ - //back - music.playef(11); - game.createmenu("mainmenu"); - map.nexttowercolour(); - } - #endif +#undef OFFSET } #endif else if(game.currentmenuname=="errornostart"){ @@ -589,8 +530,12 @@ void menuactionpress() } else if (game.currentmenuname == "options") { - #if defined(MAKEANDPLAY) +#define OFFSET -1 +#else +#define OFFSET 0 +#endif + if (game.currentmenuoption == 0) { //accessibility options @@ -598,61 +543,7 @@ void menuactionpress() game.createmenu("accessibility"); map.nexttowercolour(); } - - else if (game.currentmenuoption == 1) - { - //clear data menu - music.playef(11); - game.createmenu("controller"); - map.nexttowercolour(); - } - else if (game.currentmenuoption == 2) - { - //clear data menu - music.playef(11); - game.createmenu("cleardatamenu"); - map.nexttowercolour(); - } - - if(music.mmmmmm){ - if (game.currentmenuoption == 3) - { - //**** TOGGLE MMMMMM - if(game.usingmmmmmm > 0){ - game.usingmmmmmm=0; - }else{ - game.usingmmmmmm=1; - } - music.usingmmmmmm = !music.usingmmmmmm; - music.playef(11); - music.play(6); - game.savestats(); - } - if (game.currentmenuoption == 4) - { - //back - music.playef(11); - game.createmenu("mainmenu"); - map.nexttowercolour(); - } - }else{ - if (game.currentmenuoption == 3) - { - //back - music.playef(11); - game.createmenu("mainmenu"); - map.nexttowercolour(); - } - } - -#elif !defined(MAKEANDPLAY) - if (game.currentmenuoption == 0) - { - //accessibility options - music.playef(11); - game.createmenu("accessibility"); - map.nexttowercolour(); - } +#if !defined(MAKEANDPLAY) else if (game.currentmenuoption == 1) { //unlock play options @@ -660,14 +551,15 @@ void menuactionpress() game.createmenu("unlockmenu"); map.nexttowercolour(); } - else if (game.currentmenuoption == 2) +#endif + else if (game.currentmenuoption == OFFSET+2) { //clear data menu music.playef(11); game.createmenu("controller"); map.nexttowercolour(); } - else if (game.currentmenuoption == 3) + else if (game.currentmenuoption == OFFSET+3) { //clear data menu music.playef(11); @@ -675,37 +567,28 @@ void menuactionpress() map.nexttowercolour(); } - if(music.mmmmmm){ - if (game.currentmenuoption == 4) - { - //**** TOGGLE MMMMMM - if(game.usingmmmmmm > 0){ - game.usingmmmmmm=0; - }else{ - game.usingmmmmmm=1; - } - music.usingmmmmmm = !music.usingmmmmmm; - music.playef(11); - music.play(6); - game.savestats(); - } - if (game.currentmenuoption == 5) - { - //back - music.playef(11); - game.createmenu("mainmenu"); - map.nexttowercolour(); - } - }else{ - if (game.currentmenuoption == 4) - { - //back - music.playef(11); - game.createmenu("mainmenu"); - map.nexttowercolour(); + int mmmmmm_offset = music.mmmmmm ? 0 : -1; + if (game.currentmenuoption == OFFSET+4+mmmmmm_offset) + { + //**** TOGGLE MMMMMM + if(game.usingmmmmmm > 0){ + game.usingmmmmmm=0; + }else{ + game.usingmmmmmm=1; } + music.usingmmmmmm = !music.usingmmmmmm; + music.playef(11); + music.play(6); + game.savestats(); } -#endif + else if (game.currentmenuoption == OFFSET+5+mmmmmm_offset) + { + //back + music.playef(11); + game.createmenu("mainmenu"); + map.nexttowercolour(); + } +#undef OFFSET } else if (game.currentmenuname == "unlockmenutrials") { diff --git a/desktop_version/src/Render.cpp b/desktop_version/src/Render.cpp index a7cbe6d0..6fb936f8 100644 --- a/desktop_version/src/Render.cpp +++ b/desktop_version/src/Render.cpp @@ -66,61 +66,38 @@ void menurender() } else if (game.currentmenuname == "options") { - #if defined(MAKEANDPLAY) +#define OFFSET -1 +#else +#define OFFSET 0 +#endif + if (game.currentmenuoption == 0) { graphics.bigprint( -1, 30, "Accessibility", tr, tg, tb, true); graphics.Print( -1, 65, "Disable screen effects, enable", tr, tg, tb, true); graphics.Print( -1, 75, "slowdown modes or invincibility", tr, tg, tb, true); } - else if (game.currentmenuoption == 1) - { - graphics.bigprint( -1, 30, "Game Pad Options", tr, tg, tb, true); - graphics.Print( -1, 65, "Rebind your controller's buttons", tr, tg, tb, true); - graphics.Print( -1, 75, "and adjust sensitivity", tr, tg, tb, true); - } - else if (game.currentmenuoption == 2) - { - graphics.bigprint( -1, 30, "Clear Data", tr, tg, tb, true); - graphics.Print( -1, 65, "Delete your save data", tr, tg, tb, true); - graphics.Print( -1, 75, "and unlocked play modes", tr, tg, tb, true); - }else if (game.currentmenuoption == 3){ - if(music.mmmmmm){ - graphics.bigprint( -1, 30, "Soundtrack", tr, tg, tb, true); - graphics.Print( -1, 65, "Toggle between MMMMMM and PPPPPP", tr, tg, tb, true); - if(music.usingmmmmmm){ - graphics.Print( -1, 85, "Current soundtrack: MMMMMM", tr, tg, tb, true); - }else{ - graphics.Print( -1, 85, "Current soundtrack: PPPPPP", tr, tg, tb, true); - } - } - } -#elif !defined(MAKEANDPLAY) - if (game.currentmenuoption == 0) - { - graphics.bigprint( -1, 30, "Accessibility", tr, tg, tb, true); - graphics.Print( -1, 65, "Disable screen effects, enable", tr, tg, tb, true); - graphics.Print( -1, 75, "slowdown modes or invincibility", tr, tg, tb, true); - } +#if !defined(MAKEANDPLAY) else if (game.currentmenuoption == 1) { graphics.bigprint( -1, 30, "Unlock Play Modes", tr, tg, tb, true); graphics.Print( -1, 65, "Unlock parts of the game normally", tr, tg, tb, true); graphics.Print( -1, 75, "unlocked as you progress", tr, tg, tb, true); } - else if (game.currentmenuoption == 2) +#endif + else if (game.currentmenuoption == OFFSET+2) { graphics.bigprint( -1, 30, "Game Pad Options", tr, tg, tb, true); graphics.Print( -1, 65, "Rebind your controller's buttons", tr, tg, tb, true); graphics.Print( -1, 75, "and adjust sensitivity", tr, tg, tb, true); } - else if (game.currentmenuoption == 3) + else if (game.currentmenuoption == OFFSET+3) { graphics.bigprint( -1, 30, "Clear Data", tr, tg, tb, true); graphics.Print( -1, 65, "Delete your save data", tr, tg, tb, true); graphics.Print( -1, 75, "and unlocked play modes", tr, tg, tb, true); - }else if (game.currentmenuoption == 4) + }else if (game.currentmenuoption == OFFSET+4) { if(music.mmmmmm){ graphics.bigprint( -1, 30, "Soundtrack", tr, tg, tb, true); @@ -132,7 +109,7 @@ void menurender() } } } -#endif +#undef OFFSET } else if (game.currentmenuname == "graphicoptions") {