From 8366e08fbe4e60bd35ef999c24da319771a01c8d Mon Sep 17 00:00:00 2001 From: Misa Date: Fri, 3 Jul 2020 21:30:31 -0700 Subject: [PATCH] Remove usage of std::string from MenuOption Instead, the string in MenuOption is just a buffer of 161 chars, which is 40 chars (160 bytes if each were the largest possible UTF-8 character size) plus a null terminator. This is because the maximum length of a menu option that can be fit on the screen without going past is 40 chars. --- desktop_version/src/Game.cpp | 30 ++++++++++++------ desktop_version/src/Game.h | 8 +++-- desktop_version/src/Graphics.cpp | 54 +++++++++++++++++++------------- 3 files changed, 59 insertions(+), 33 deletions(-) diff --git a/desktop_version/src/Game.cpp b/desktop_version/src/Game.cpp index a5c4e369..34456662 100644 --- a/desktop_version/src/Game.cpp +++ b/desktop_version/src/Game.cpp @@ -6766,27 +6766,39 @@ void Game::createmenu( enum Menu::MenuName t, bool samemenu/*= false*/ ) break; } } - std::string text; + const char* prefix; if(tvar>=0) { - if(customlevelstats[tvar].score==0) + switch (customlevelstats[tvar].score) { - text = " " + ed.ListOfMetaData[i].title; + case 0: + { + static const char tmp[] = " "; + prefix = tmp; + break; } - else if(customlevelstats[tvar].score==1) + case 1: { - text = " * " + ed.ListOfMetaData[i].title; + static const char tmp[] = " * "; + prefix = tmp; + break; } - else if(customlevelstats[tvar].score==3) + case 3: { - text = "** " + ed.ListOfMetaData[i].title; + static const char tmp[] = "** "; + prefix = tmp; + break; + } } } else { - text = " " + ed.ListOfMetaData[i].title; + static const char tmp[] = " "; + prefix = tmp; } - for (size_t ii = 0; ii < text.length(); ii++) + char text[menutextbytes]; + SDL_snprintf(text, sizeof(text), "%s%s", prefix, ed.ListOfMetaData[i].title.c_str()); + for (size_t ii = 0; ii < SDL_arraysize(text); ii++) { text[ii] = SDL_tolower(text[ii]); } diff --git a/desktop_version/src/Game.h b/desktop_version/src/Game.h index 9d9243a4..97f36127 100644 --- a/desktop_version/src/Game.h +++ b/desktop_version/src/Game.h @@ -10,7 +10,8 @@ struct MenuOption { - std::string text; + char text[161]; // 40 chars (160 bytes) covers the entire screen, + 1 more for null terminator + // WARNING: should match Game::menutextbytes below bool active; }; @@ -225,12 +226,13 @@ public: int current_credits_list_index; int menuxoff, menuyoff; int menuspacing; + static const int menutextbytes = 161; // this is just sizeof(MenuOption::text), but doing that is non-standard std::vector menustack; - void inline option(std::string text, bool active = true) + void inline option(const char* text, bool active = true) { MenuOption menuoption; - menuoption.text = text; + SDL_strlcpy(menuoption.text, text, sizeof(menuoption.text)); menuoption.active = active; menuoptions.push_back(menuoption); } diff --git a/desktop_version/src/Graphics.cpp b/desktop_version/src/Graphics.cpp index e2281e2d..56641eac 100644 --- a/desktop_version/src/Graphics.cpp +++ b/desktop_version/src/Graphics.cpp @@ -1302,20 +1302,24 @@ void Graphics::drawmenu( int cr, int cg, int cb ) //Draw it highlighted if (game.menuoptions[i].active) { - std::string tempstring = game.menuoptions[i].text; - for (size_t ii = 0; ii < tempstring.length(); ii++) + 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]); } - tempstring = std::string("[ ") + tempstring + std::string(" ]"); - Print((i * game.menuspacing) - 16 +game.menuxoff, 140 + (i * 12) +game.menuyoff, tempstring, cr, cg, cb); + 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 { - std::string tempstring = game.menuoptions[i].text; - tempstring = "[ " + tempstring + " ]"; + 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, tempstring, 128, 128, 128); + Print((i * game.menuspacing) - 16 +game.menuxoff, 140 + (i * 12)+game.menuyoff, buffer, 128, 128, 128); } } else @@ -1344,39 +1348,47 @@ void Graphics::drawlevelmenu( int cr, int cg, int cb ) //Draw it highlighted if (game.menuoptions[i].active) { - std::string tempstring = game.menuoptions[i].text; - for (size_t ii = 0; ii < tempstring.length(); ii++) + 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]); } - tempstring = std::string("[ ") + tempstring + std::string(" ]"); - Print((i * game.menuspacing) - 16 +game.menuxoff, 140+8 + (i * 12) +game.menuyoff, tempstring, cr, cg, cb); + char buffer[Game::menutextbytes]; + SDL_snprintf(buffer, sizeof(buffer), "[ %s ]", tempstring); + Print((i * game.menuspacing) - 16 +game.menuxoff, 140+8 + (i * 12) +game.menuyoff, buffer, cr, cg, cb); } else { - std::string tempstring = game.menuoptions[i].text; - tempstring = "[ " + tempstring + " ]"; + 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, tempstring, 128, 128, 128); + 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) { - std::string tempstring = game.menuoptions[i].text; - for (size_t ii = 0; ii < tempstring.length(); ii++) + 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]); } - tempstring = std::string("[ ") + tempstring + std::string(" ]"); - Print((i * game.menuspacing) - 16 +game.menuxoff, 144 + (i * 12) +game.menuyoff, tempstring, cr, cg, cb); + 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 { - std::string tempstring = game.menuoptions[i].text; - tempstring = "[ " + tempstring + " ]"; + 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, tempstring, 128, 128, 128); + Print((i * game.menuspacing) - 16 +game.menuxoff, 144 + (i * 12)+game.menuyoff, buffer, 128, 128, 128); } } }