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++)
{
if ((int) i == game.currentmenuoption)
MenuOption& opt = game.menuoptions[i];
int fr, fg, fb;
if (opt.active)
{
//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, 140 + (i * 12) +game.menuyoff, buffer, cr, cg, cb);
// Color it normally
fr = cr;
fg = cg;
fb = 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, 140 + (i * 12)+game.menuyoff, buffer, 128, 128, 128);
// Color it gray
fr = 128;
fg = 128;
fb = 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
{
//Draw it normally
if (game.menuoptions[i].active)
{
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);
}
}
// Get out of the way of the level descriptions
y += 4;
}
}
void Graphics::drawlevelmenu( int cr, int cg, int cb )
{
for (size_t i = 0; i < game.menuoptions.size(); i++)
{
char tempstring[Game::menutextbytes];
SDL_strlcpy(tempstring, opt.text, sizeof(tempstring));
char buffer[Game::menutextbytes];
if ((int) i == game.currentmenuoption)
{
if(game.menuoptions.size()-i<=3){
//Draw it highlighted
if (game.menuoptions[i].active)
if (opt.active)
{
char tempstring[Game::menutextbytes];
SDL_strlcpy(tempstring, game.menuoptions[i].text, sizeof(tempstring));
// Uppercase the text
// FIXME: This isn't UTF-8 aware!
for (size_t ii = 0; ii < SDL_arraysize(tempstring); ii++)
{
tempstring[ii] = SDL_toupper(tempstring[ii]);
}
char buffer[Game::menutextbytes];
}
// Add brackets
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
{
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, 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);
}
}
SDL_strlcpy(buffer, tempstring, sizeof(buffer));
}
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 drawmenu(int cr, int cg, int cb);
void drawlevelmenu(int cr, int cg, int cb);
void drawmenu(int cr, int cg, int cb, bool levelmenu = false);
void processfade();

View file

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