1
0
Fork 0
mirror of https://github.com/TerryCavanagh/VVVVVV.git synced 2024-12-23 10:09:43 +01:00

De-duplicate Graphics::drawmenu() and Graphics::drawlevelmenu()

Graphics::drawmenu() no longer has copy-pasted code for each individual
case. Instead, the individual cases have their own adding on to common
code, which is far easier to maintain.

Also, the only difference Graphics::drawlevelmenu() does is in some
y-positioning stuff. There's no reason to make it a whole separate
function and duplicate everything AGAIN. So it's been consolidated into
Graphics::drawmenu() as well, and I've added a boolean to draw a menu
this way if it's the level menu.
This commit is contained in:
Misa 2020-07-04 14:53:05 -07:00 committed by Ethan Lee
parent fbfeeaccd1
commit fd0dafc16c
3 changed files with 59 additions and 126 deletions

View file

@ -1293,131 +1293,72 @@ void Graphics::processfade()
} }
} }
void Graphics::drawmenu( int cr, int cg, int cb ) void Graphics::drawmenu( int cr, int cg, int cb, bool levelmenu /*= false*/ )
{ {
for (size_t i = 0; i < game.menuoptions.size(); i++) for (size_t i = 0; i < game.menuoptions.size(); i++)
{ {
if ((int) i == game.currentmenuoption) MenuOption& opt = game.menuoptions[i];
int fr, fg, fb;
if (opt.active)
{ {
//Draw it highlighted // Color it normally
if (game.menuoptions[i].active) fr = cr;
{ fg = cg;
char tempstring[Game::menutextbytes]; fb = cb;
SDL_strlcpy(tempstring, game.menuoptions[i].text, sizeof(tempstring));
for (size_t ii = 0; ii < SDL_arraysize(tempstring); ii++)
{
tempstring[ii] = SDL_toupper(tempstring[ii]);
}
char buffer[Game::menutextbytes];
SDL_snprintf(buffer, sizeof(buffer), "[ %s ]", tempstring);
Print((i * game.menuspacing) - 16 +game.menuxoff, 140 + (i * 12) +game.menuyoff, buffer, cr, cg, cb);
} }
else else
{ {
char tempstring[Game::menutextbytes]; // Color it gray
SDL_strlcpy(tempstring, game.menuoptions[i].text, sizeof(tempstring)); fr = 128;
char buffer[Game::menutextbytes]; fg = 128;
SDL_snprintf(buffer, sizeof(buffer), "[ %s ]", tempstring); fb = 128;
//Draw it in gray
Print((i * game.menuspacing) - 16 +game.menuxoff, 140 + (i * 12)+game.menuyoff, buffer, 128, 128, 128);
} }
int x = i*game.menuspacing + game.menuxoff;
int y = 140 + i*12 + game.menuyoff;
if (levelmenu)
{
if (game.menuoptions.size() - i <= 3)
{
// We're on "next page", "previous page", or "return to menu". Draw them separated by a bit
y += 8;
} }
else else
{ {
//Draw it normally // Get out of the way of the level descriptions
if (game.menuoptions[i].active) y += 4;
{
Print((i * game.menuspacing) +game.menuxoff, 140 + (i * 12)+game.menuyoff, game.menuoptions[i].text, cr, cg, cb);
}
else
{
//Draw it in gray
Print((i * game.menuspacing) +game.menuxoff, 140 + (i * 12)+game.menuyoff, game.menuoptions[i].text, 128, 128, 128);
}
}
} }
} }
void Graphics::drawlevelmenu( int cr, int cg, int cb ) char tempstring[Game::menutextbytes];
{ SDL_strlcpy(tempstring, opt.text, sizeof(tempstring));
for (size_t i = 0; i < game.menuoptions.size(); i++)
{ char buffer[Game::menutextbytes];
if ((int) i == game.currentmenuoption) if ((int) i == game.currentmenuoption)
{ {
if(game.menuoptions.size()-i<=3){ if (opt.active)
//Draw it highlighted
if (game.menuoptions[i].active)
{ {
char tempstring[Game::menutextbytes]; // Uppercase the text
SDL_strlcpy(tempstring, game.menuoptions[i].text, sizeof(tempstring)); // FIXME: This isn't UTF-8 aware!
for (size_t ii = 0; ii < SDL_arraysize(tempstring); ii++) for (size_t ii = 0; ii < SDL_arraysize(tempstring); ii++)
{ {
tempstring[ii] = SDL_toupper(tempstring[ii]); tempstring[ii] = SDL_toupper(tempstring[ii]);
} }
char buffer[Game::menutextbytes]; }
// Add brackets
SDL_snprintf(buffer, sizeof(buffer), "[ %s ]", tempstring); SDL_snprintf(buffer, sizeof(buffer), "[ %s ]", tempstring);
Print((i * game.menuspacing) - 16 +game.menuxoff, 140+8 + (i * 12) +game.menuyoff, buffer, cr, cg, cb); // Account for brackets
x -= 16;
} }
else else
{ {
char tempstring[Game::menutextbytes]; SDL_strlcpy(buffer, tempstring, sizeof(buffer));
SDL_strlcpy(tempstring, game.menuoptions[i].text, sizeof(tempstring));
char buffer[Game::menutextbytes];
SDL_snprintf(buffer, sizeof(buffer), "[ %s ]", tempstring);
//Draw it in gray
Print((i * game.menuspacing) - 16 +game.menuxoff, 140+8 + (i * 12)+game.menuyoff, buffer, 128, 128, 128);
}
}else{
//Draw it highlighted
if (game.menuoptions[i].active)
{
char tempstring[Game::menutextbytes];
SDL_strlcpy(tempstring, game.menuoptions[i].text, sizeof(tempstring));
for (size_t ii = 0; ii < SDL_arraysize(tempstring); ii++)
{
tempstring[ii] = SDL_toupper(tempstring[ii]);
}
char buffer[Game::menutextbytes];
SDL_snprintf(buffer, sizeof(buffer), "[ %s ]", tempstring);
Print((i * game.menuspacing) - 16 +game.menuxoff, 144 + (i * 12) +game.menuyoff, buffer, cr, cg, cb);
}
else
{
char tempstring[Game::menutextbytes];
SDL_strlcpy(tempstring, game.menuoptions[i].text, sizeof(tempstring));
char buffer[Game::menutextbytes];
SDL_snprintf(buffer, sizeof(buffer), "[ %s ]", tempstring);
//Draw it in gray
Print((i * game.menuspacing) - 16 +game.menuxoff, 144 + (i * 12)+game.menuyoff, buffer, 128, 128, 128);
}
}
}
else
{
if(game.menuoptions.size()-i<=3){
//Draw it normally
if (game.menuoptions[i].active)
{
Print((i * game.menuspacing) +game.menuxoff, 140+8 + (i * 12)+game.menuyoff, game.menuoptions[i].text, cr, cg, cb);
}
else
{
//Draw it in gray
Print((i * game.menuspacing) +game.menuxoff, 140+8 + (i * 12)+game.menuyoff, game.menuoptions[i].text, 128, 128, 128);
}
}else{
//Draw it normally
if (game.menuoptions[i].active)
{
Print((i * game.menuspacing) +game.menuxoff, 144 + (i * 12)+game.menuyoff, game.menuoptions[i].text, cr, cg, cb);
}
else
{
//Draw it in gray
Print((i * game.menuspacing) +game.menuxoff, 144 + (i * 12)+game.menuyoff, game.menuoptions[i].text, 128, 128, 128);
}
}
} }
Print(x, y, buffer, fr, fg, fb);
} }
} }

View file

@ -46,8 +46,7 @@ public:
void drawcoloredtile(int x, int y, int t, int r, int g, int b); void drawcoloredtile(int x, int y, int t, int r, int g, int b);
void drawmenu(int cr, int cg, int cb); void drawmenu(int cr, int cg, int cb, bool levelmenu = false);
void drawlevelmenu(int cr, int cg, int cb);
void processfade(); void processfade();

View file

@ -1188,14 +1188,7 @@ void titlerender()
if(tg>255) tg=255; if(tg>255) tg=255;
if (tb < 0) tb = 0; if (tb < 0) tb = 0;
if(tb>255) tb=255; if(tb>255) tb=255;
if (game.currentmenuname == Menu::levellist) graphics.drawmenu(tr, tg, tb, game.currentmenuname == Menu::levellist);
{
graphics.drawlevelmenu(tr, tg, tb);
}
else
{
graphics.drawmenu(tr, tg, tb);
}
} }
graphics.drawfade(); graphics.drawfade();