From 511de0c5c1f5e488948252a09aef9212c36c123d Mon Sep 17 00:00:00 2001 From: Misa Date: Tue, 14 Apr 2020 21:50:17 -0700 Subject: [PATCH] Refactor menu creation code Firstly, menu options are no longer ad-hoc objects, and are added by using Game::option() (this is the biggest change). This removes the vector Game::menuoptionsactive, and Game::menuoptions is now a vector of MenuOption instead of std::string. Secondly, the manual tracker variable of the amount of menu options, Game::nummenuoptions, has been removed, in favor of using vectors properly and using Game::menuoptions::size(). As a result, a lot of copy-pasted code has been removed from Game::createmenu(), mostly due to having to have different versions of menus depending on whether or not we have certain defines, or having an mmmmmm.vvv file inside the VVVVVV directory. In the old days, you couldn't just add or remove a menu option conveniently, you had to shuffle around the position of every other menu option too, which resulted in lots of copy-pasted code. But now this copy-pasted code has been de-duplicated, at least in Game::createmenu(). --- desktop_version/src/Game.cpp | 691 ++++++++----------------------- desktop_version/src/Game.h | 5 +- desktop_version/src/Graphics.cpp | 48 +-- desktop_version/src/Input.cpp | 89 +--- desktop_version/src/Render.cpp | 2 +- desktop_version/src/editor.cpp | 4 +- 6 files changed, 209 insertions(+), 630 deletions(-) diff --git a/desktop_version/src/Game.cpp b/desktop_version/src/Game.cpp index cce6d756..b3a57d35 100644 --- a/desktop_version/src/Game.cpp +++ b/desktop_version/src/Game.cpp @@ -219,12 +219,9 @@ void Game::init(void) quick_currentarea = "Error! Error!"; //Menu stuff initiliased here: - menuoptions.resize(25); - menuoptionsactive.resize(25); unlock.resize(25); unlocknotify.resize(25); - nummenuoptions = 0; currentmenuoption = 0; menuselection = "null"; currentmenuname = "null"; @@ -6557,93 +6554,47 @@ void Game::createmenu( std::string t ) menuyoff = 0; menucountdown = 0; menudest="null"; + menuoptions.clear(); if (t == "mainmenu") { -#if defined(MAKEANDPLAY) - menuoptions[0] = "player levels"; - menuoptionsactive[0] = true; - menuoptions[1] = "graphic options"; - menuoptionsactive[1] = true; - menuoptions[2] = "game options"; - menuoptionsactive[2] = true; - menuoptions[3] = "quit game"; - menuoptionsactive[3] = true; - nummenuoptions = 4; - menuxoff = -16; - menuyoff = -10; -#elif !defined(MAKEANDPLAY) - #if defined(NO_CUSTOM_LEVELS) - menuoptions[0] = "start game"; - menuoptionsactive[0] = true; - menuoptions[1] = "graphic options"; - menuoptionsactive[1] = true; - menuoptions[2] = "game options"; - menuoptionsactive[2] = true; - menuoptions[3] = "view credits"; - menuoptionsactive[3] = true; - menuoptions[4] = "quit game"; - menuoptionsactive[4] = true; - nummenuoptions = 5; - menuxoff = -16; - menuyoff = -10; - #else - menuoptions[0] = "start game"; - menuoptionsactive[0] = true; - menuoptions[1] = "player levels"; - menuoptionsactive[1] = true; - menuoptions[2] = "graphic options"; - menuoptionsactive[2] = true; - menuoptions[3] = "game options"; - menuoptionsactive[3] = true; - menuoptions[4] = "view credits"; - menuoptionsactive[4] = true; - menuoptions[5] = "quit game"; - menuoptionsactive[5] = true; - nummenuoptions = 6; - menuxoff = -16; - menuyoff = -10; - #endif +#if !defined(MAKEANDPLAY) + option("start game"); #endif +#if !defined(NO_CUSTOM_LEVELS) + option("player levels"); +#endif + option("graphic options"); + option("game options"); +#if !defined(MAKEANDPLAY) + option("view credits"); +#endif + option("quit game"); + menuxoff = -16; + menuyoff = -10; } #if !defined(NO_CUSTOM_LEVELS) else if (t == "playerworlds") { + option("play a level"); #if !defined(NO_EDITOR) - menuoptions[0] = "play a level"; - menuoptionsactive[0] = true; - menuoptions[1] = "level editor"; - menuoptionsactive[1] = true; - //menuoptions[2] = "open level folder"; - //menuoptionsactive[2] = true; - menuoptions[2] = "back to menu"; - menuoptionsactive[2] = true; - nummenuoptions = 3; - menuxoff = -30; - menuyoff = -40; - #else - menuoptions[0] = "play a level"; - menuoptionsactive[0] = true; - menuoptions[1] = "back to menu"; - menuoptionsactive[1] = true; - nummenuoptions = 2; - menuxoff = -30; - menuyoff = -40; + option("level editor"); #endif + //option("open level folder"); + option("back to menu"); + menuxoff = -30; + menuyoff = -40; } else if (t == "levellist") { if(ed.ListOfMetaData.size()==0) { - menuoptions[0] = "ok"; - menuoptionsactive[0] = true; - nummenuoptions = 1; + option("ok"); menuxoff = 0; menuyoff = -20; } else { - int tcount=0; for(int i=0; i<(int) ed.ListOfMetaData.size(); i++) // FIXME: int/size_t! -flibit { if(i>=levelpage*8 && i< (levelpage*8)+8) @@ -6658,97 +6609,74 @@ void Game::createmenu( std::string t ) j=numcustomlevelstats+1; } } + std::string text; if(tvar>=0) { if(customlevelscore[tvar]==0) { - menuoptions[tcount] = " " + ed.ListOfMetaData[i].title; + text = " " + ed.ListOfMetaData[i].title; } else if(customlevelscore[tvar]==1) { - menuoptions[tcount] = " * " + ed.ListOfMetaData[i].title; + text = " * " + ed.ListOfMetaData[i].title; } else if(customlevelscore[tvar]==3) { - menuoptions[tcount] = "** " + ed.ListOfMetaData[i].title; + text = "** " + ed.ListOfMetaData[i].title; } } else { - menuoptions[tcount] = " " + ed.ListOfMetaData[i].title; + text = " " + ed.ListOfMetaData[i].title; } - menuoptionsactive[tcount] = true; - std::transform(menuoptions[tcount].begin(), menuoptions[tcount].end(), menuoptions[tcount].begin(), ::tolower); - tcount++; + std::transform(text.begin(), text.end(), text.begin(), ::tolower); + option(text); } } if((size_t) ((levelpage*8)+8) menuoptions; - std::vector menuoptionsactive; - int nummenuoptions, currentmenuoption ; + std::vector menuoptions; + int currentmenuoption ; std::string menuselection, currentmenuname, previousmenuname; int current_credits_list_index; int menuxoff, menuyoff; diff --git a/desktop_version/src/Graphics.cpp b/desktop_version/src/Graphics.cpp index 01a4947d..cf0274a1 100644 --- a/desktop_version/src/Graphics.cpp +++ b/desktop_version/src/Graphics.cpp @@ -1087,21 +1087,21 @@ void Graphics::processfade() void Graphics::drawmenu( int cr, int cg, int cb, int division /*= 30*/ ) { - for (int i = 0; i < game.nummenuoptions; i++) + for (size_t i = 0; i < game.menuoptions.size(); i++) { - if (i == game.currentmenuoption) + if ((int) i == game.currentmenuoption) { //Draw it highlighted - if (game.menuoptionsactive[i]) + if (game.menuoptions[i].active) { - std::string tempstring = game.menuoptions[i]; + std::string tempstring = game.menuoptions[i].text; std::transform(tempstring.begin(), tempstring.end(),tempstring.begin(), ::toupper); tempstring = std::string("[ ") + tempstring + std::string(" ]"); Print(110 + (i * division) - 16 +game.menuxoff, 140 + (i * 12) +game.menuyoff, tempstring, cr, cg, cb); } else { - std::string tempstring = game.menuoptions[i]; + std::string tempstring = game.menuoptions[i].text; tempstring = "[ " + tempstring + " ]"; //Draw it in gray Print(110 + (i * division) - 16 +game.menuxoff, 140 + (i * 12)+game.menuyoff, tempstring, 128, 128, 128); @@ -1110,14 +1110,14 @@ void Graphics::drawmenu( int cr, int cg, int cb, int division /*= 30*/ ) else { //Draw it normally - if (game.menuoptionsactive[i]) + if (game.menuoptions[i].active) { - Print(110 + (i * division) +game.menuxoff, 140 + (i * 12)+game.menuyoff, game.menuoptions[i], cr, cg, cb); + Print(110 + (i * division) +game.menuxoff, 140 + (i * 12)+game.menuyoff, game.menuoptions[i].text, cr, cg, cb); } else { //Draw it in gray - Print(110 + (i * division) +game.menuxoff, 140 + (i * 12)+game.menuyoff, game.menuoptions[i], 128, 128, 128); + Print(110 + (i * division) +game.menuxoff, 140 + (i * 12)+game.menuyoff, game.menuoptions[i].text, 128, 128, 128); } } } @@ -1125,38 +1125,38 @@ void Graphics::drawmenu( int cr, int cg, int cb, int division /*= 30*/ ) void Graphics::drawlevelmenu( int cr, int cg, int cb, int division /*= 30*/ ) { - for (int i = 0; i < game.nummenuoptions; i++) + for (size_t i = 0; i < game.menuoptions.size(); i++) { - if (i == game.currentmenuoption) + if ((int) i == game.currentmenuoption) { - if(game.nummenuoptions-i<=2){ + if(game.menuoptions.size()-i<=2){ //Draw it highlighted - if (game.menuoptionsactive[i]) + if (game.menuoptions[i].active) { - std::string tempstring = game.menuoptions[i]; + std::string tempstring = game.menuoptions[i].text; std::transform(tempstring.begin(), tempstring.end(),tempstring.begin(), ::toupper); tempstring = std::string("[ ") + tempstring + std::string(" ]"); Print(110 + (i * division) - 16 +game.menuxoff, 140+8 + (i * 12) +game.menuyoff, tempstring, cr, cg, cb); } else { - std::string tempstring = game.menuoptions[i]; + std::string tempstring = game.menuoptions[i].text; tempstring = "[ " + tempstring + " ]"; //Draw it in gray Print(110 + (i * division) - 16 +game.menuxoff, 140+8 + (i * 12)+game.menuyoff, tempstring, 128, 128, 128); } }else{ //Draw it highlighted - if (game.menuoptionsactive[i]) + if (game.menuoptions[i].active) { - std::string tempstring = game.menuoptions[i]; + std::string tempstring = game.menuoptions[i].text; std::transform(tempstring.begin(), tempstring.end(),tempstring.begin(), ::toupper); tempstring = std::string("[ ") + tempstring + std::string(" ]"); Print(110 + (i * division) - 16 +game.menuxoff, 140 + (i * 12) +game.menuyoff, tempstring, cr, cg, cb); } else { - std::string tempstring = game.menuoptions[i]; + std::string tempstring = game.menuoptions[i].text; tempstring = "[ " + tempstring + " ]"; //Draw it in gray Print(110 + (i * division) - 16 +game.menuxoff, 140 + (i * 12)+game.menuyoff, tempstring, 128, 128, 128); @@ -1165,27 +1165,27 @@ void Graphics::drawlevelmenu( int cr, int cg, int cb, int division /*= 30*/ ) } else { - if(game.nummenuoptions-i<=2){ + if(game.menuoptions.size()-i<=2){ //Draw it normally - if (game.menuoptionsactive[i]) + if (game.menuoptions[i].active) { - Print(110 + (i * division) +game.menuxoff, 140+8 + (i * 12)+game.menuyoff, game.menuoptions[i], cr, cg, cb); + Print(110 + (i * division) +game.menuxoff, 140+8 + (i * 12)+game.menuyoff, game.menuoptions[i].text, cr, cg, cb); } else { //Draw it in gray - Print(110 + (i * division) +game.menuxoff, 140+8 + (i * 12)+game.menuyoff, game.menuoptions[i], 128, 128, 128); + Print(110 + (i * division) +game.menuxoff, 140+8 + (i * 12)+game.menuyoff, game.menuoptions[i].text, 128, 128, 128); } }else{ //Draw it normally - if (game.menuoptionsactive[i]) + if (game.menuoptions[i].active) { - Print(110 + (i * division) +game.menuxoff, 140 + (i * 12)+game.menuyoff, game.menuoptions[i], cr, cg, cb); + Print(110 + (i * division) +game.menuxoff, 140 + (i * 12)+game.menuyoff, game.menuoptions[i].text, cr, cg, cb); } else { //Draw it in gray - Print(110 + (i * division) +game.menuxoff, 140 + (i * 12)+game.menuyoff, game.menuoptions[i], 128, 128, 128); + Print(110 + (i * division) +game.menuxoff, 140 + (i * 12)+game.menuyoff, game.menuoptions[i].text, 128, 128, 128); } } } diff --git a/desktop_version/src/Input.cpp b/desktop_version/src/Input.cpp index d0eba6a3..f30d51a6 100644 --- a/desktop_version/src/Input.cpp +++ b/desktop_version/src/Input.cpp @@ -174,8 +174,8 @@ void titleinput() } } - if (game.currentmenuoption < 0) game.currentmenuoption = game.nummenuoptions-1; - if (game.currentmenuoption >= game.nummenuoptions ) game.currentmenuoption = 0; + if (game.currentmenuoption < 0) game.currentmenuoption = game.menuoptions.size()-1; + if (game.currentmenuoption >= (int) game.menuoptions.size() ) game.currentmenuoption = 0; if (game.press_action) { @@ -214,15 +214,6 @@ void titleinput() //Options music.playef(11); game.createmenu("options"); - - //Add extra menu for mmmmmm mod - if(music.mmmmmm){ - game.menuoptions[3] = "soundtrack"; - game.menuoptionsactive[3] = true; - game.menuoptions[4] = "return"; - game.menuoptionsactive[4] = true; - game.nummenuoptions = 5; - } map.nexttowercolour(); } else if (game.currentmenuoption == 3) @@ -264,15 +255,6 @@ void titleinput() //Options music.playef(11); game.createmenu("options"); - - //Add extra menu for mmmmmm mod - if(music.mmmmmm){ - game.menuoptions[4] = "soundtrack"; - game.menuoptionsactive[4] = true; - game.menuoptions[5] = "return"; - game.menuoptionsactive[5] = true; - game.nummenuoptions = 6; - } map.nexttowercolour(); } else if (game.currentmenuoption == 3) @@ -326,15 +308,6 @@ void titleinput() //Options music.playef(11); game.createmenu("options"); - - //Add extra menu for mmmmmm mod - if(music.mmmmmm){ - game.menuoptions[4] = "soundtrack"; - game.menuoptionsactive[4] = true; - game.menuoptions[5] = "return"; - game.menuoptionsactive[5] = true; - game.nummenuoptions = 6; - } map.nexttowercolour(); } else if (game.currentmenuoption == 4) @@ -357,12 +330,12 @@ void titleinput() #if !defined(NO_CUSTOM_LEVELS) else if(game.currentmenuname=="levellist") { - if(game.currentmenuoption==game.nummenuoptions-1){ + if(game.currentmenuoption==(int)game.menuoptions.size()-1){ //go back to menu music.playef(11); game.createmenu("mainmenu"); map.nexttowercolour(); - }else if(game.currentmenuoption==game.nummenuoptions-2){ + }else if(game.currentmenuoption==(int)game.menuoptions.size()-2){ //next page music.playef(11); if((size_t) ((game.levelpage*8)+8) >= ed.ListOfMetaData.size()){ @@ -371,7 +344,7 @@ void titleinput() game.levelpage++; } game.createmenu("levellist"); - game.currentmenuoption=game.nummenuoptions-2; + game.currentmenuoption=game.menuoptions.size()-2; map.nexttowercolour(); }else{ //Ok, launch the level! @@ -685,24 +658,6 @@ void titleinput() //back music.playef(11); game.createmenu("options"); - - //Add extra menu for mmmmmm mod - if(music.mmmmmm){ -#if defined(MAKEANDPLAY) - game.menuoptions[3] = "soundtrack"; - game.menuoptionsactive[3] = true; - game.menuoptions[4] = "return"; - game.menuoptionsactive[4] = true; - game.nummenuoptions = 5; -#elif !defined(MAKEANDPLAY) - game.menuoptions[4] = "soundtrack"; - game.menuoptionsactive[4] = true; - game.menuoptions[5] = "return"; - game.menuoptionsactive[5] = true; - game.nummenuoptions = 6; -#endif - } - map.nexttowercolour(); } else @@ -1269,23 +1224,6 @@ void titleinput() { music.playef(11); game.createmenu("options"); - - //Add extra menu for mmmmmm mod - if(music.mmmmmm){ -#if defined(MAKEANDPLAY) - game.menuoptions[3] = "soundtrack"; - game.menuoptionsactive[3] = true; - game.menuoptions[4] = "return"; - game.menuoptionsactive[4] = true; - game.nummenuoptions = 5; -#elif !defined(MAKEANDPLAY) - game.menuoptions[4] = "soundtrack"; - game.menuoptionsactive[4] = true; - game.menuoptions[5] = "return"; - game.menuoptionsactive[5] = true; - game.nummenuoptions = 6; -#endif - } } } else if (game.currentmenuname == "cleardatamenu") @@ -1295,23 +1233,6 @@ void titleinput() //back music.playef(11); game.createmenu("options"); - - //Add extra menu for mmmmmm mod - if(music.mmmmmm){ -#if defined(MAKEANDPLAY) - game.menuoptions[3] = "soundtrack"; - game.menuoptionsactive[3] = true; - game.menuoptions[4] = "return"; - game.menuoptionsactive[4] = true; - game.nummenuoptions = 5; -#elif !defined(MAKEANDPLAY) - game.menuoptions[4] = "soundtrack"; - game.menuoptionsactive[4] = true; - game.menuoptions[5] = "return"; - game.menuoptionsactive[5] = true; - game.nummenuoptions = 6; -#endif - } map.nexttowercolour(); } else diff --git a/desktop_version/src/Render.cpp b/desktop_version/src/Render.cpp index 83d2dc26..cff12384 100644 --- a/desktop_version/src/Render.cpp +++ b/desktop_version/src/Render.cpp @@ -83,7 +83,7 @@ void titlerender() int tmp=game.currentmenuoption+(game.levelpage*8); if(tmp>=0 && tmp < (int) ed.ListOfMetaData.size()){ // FIXME: size_t/int! -flibit //Don't show next page or return to menu options here! - if(game.nummenuoptions - game.currentmenuoption<=2){ + if(game.menuoptions.size() - game.currentmenuoption<=2){ }else{ graphics.bigprint( -1, 15, ed.ListOfMetaData[tmp].title, tr, tg, tb, true); diff --git a/desktop_version/src/editor.cpp b/desktop_version/src/editor.cpp index e64dc04e..b6f50490 100644 --- a/desktop_version/src/editor.cpp +++ b/desktop_version/src/editor.cpp @@ -3918,8 +3918,8 @@ void editorinput() } } - if (game.currentmenuoption < 0) game.currentmenuoption = game.nummenuoptions-1; - if (game.currentmenuoption >= game.nummenuoptions ) game.currentmenuoption = 0; + if (game.currentmenuoption < 0) game.currentmenuoption = game.menuoptions.size()-1; + if (game.currentmenuoption >= (int) game.menuoptions.size() ) game.currentmenuoption = 0; if (game.press_action) {