From 264b6474be987e9171b951628e7273499ecd79c8 Mon Sep 17 00:00:00 2001 From: Dav999-v Date: Sat, 4 Mar 2023 23:29:12 +0100 Subject: [PATCH] Change font::print_wrap text argument from std::string to const char* We no longer need to pass a std::string object to the print and len functions - in fact, we often only have a C string that we want to print or get the visual width of (that C string most often comes from loc::gettext), and it's a bit wasteful to wrap it in a new std::string object on every print/len call. This does mean adding a few more .c_str()s, but there's not many places where a std::string is being passed to these functions, and we already use .c_str() sometimes. -> Commit 1/3: font::print_wrap Commit 2/3: font::print Commit 3/3: font::len --- desktop_version/src/Editor.cpp | 6 +++--- desktop_version/src/Font.cpp | 7 +++---- desktop_version/src/Font.h | 2 +- desktop_version/src/Render.cpp | 2 +- 4 files changed, 8 insertions(+), 9 deletions(-) diff --git a/desktop_version/src/Editor.cpp b/desktop_version/src/Editor.cpp index a3b83722..94b1c969 100644 --- a/desktop_version/src/Editor.cpp +++ b/desktop_version/src/Editor.cpp @@ -1181,7 +1181,7 @@ void editorrender(void) graphics.fill_rect(0,238-textheight,320,240, graphics.getRGB(32,32,32)); graphics.fill_rect(0,239-textheight,320,240, graphics.getRGB(0,0,0)); - font::print_wrap(0, 4, 240-textheight, message, 255,255,255, 8, 312); + font::print_wrap(0, 4, 240-textheight, message.c_str(), 255,255,255, 8, 312); } else if(ed.scripteditmod) { @@ -1283,7 +1283,7 @@ void editorrender(void) graphics.fill_rect(0, 238-textheight, 320, 240, graphics.getRGB(32, 32, 32)); graphics.fill_rect(0, 239-textheight, 320, 240, graphics.getRGB(0, 0, 0)); - font::print_wrap(0, 4, 240-textheight, wrapped, 255, 255, 255, 8, 312); + font::print_wrap(0, 4, 240-textheight, wrapped.c_str(), 255, 255, 255, 8, 312); std::string input = key.keybuffer; if (ed.entframe < 2) { @@ -1640,7 +1640,7 @@ void editorrender(void) float alpha = graphics.lerp(ed.oldnotedelay, ed.notedelay); graphics.fill_rect(0, banner_y, 320, 10+textheight, graphics.getRGB(92,92,92)); graphics.fill_rect(0, banner_y+1, 320, 8+textheight, graphics.getRGB(0,0,0)); - font::print_wrap(PR_CEN, -1,banner_y+5, wrapped, 196-((45.0f-alpha)*4), 196-((45.0f-alpha)*4), 196-((45.0f-alpha)*4)); + font::print_wrap(PR_CEN, -1,banner_y+5, wrapped.c_str(), 196-((45.0f-alpha)*4), 196-((45.0f-alpha)*4), 196-((45.0f-alpha)*4)); } graphics.drawfade(); diff --git a/desktop_version/src/Font.cpp b/desktop_version/src/Font.cpp index 6859943a..49c9df52 100644 --- a/desktop_version/src/Font.cpp +++ b/desktop_version/src/Font.cpp @@ -1261,7 +1261,7 @@ int print_wrap( uint32_t flags, const int x, int y, - const std::string& text, + const char* text, const uint8_t r, const uint8_t g, const uint8_t b, @@ -1292,7 +1292,6 @@ int print_wrap( flags &= ~PR_BOR; } - const char* str = text.c_str(); // This could fit 64 non-BMP characters onscreen, should be plenty char buffer[256]; size_t start = 0; @@ -1301,7 +1300,7 @@ int print_wrap( { // Correct for the height of the resulting print. size_t len = 0; - while (next_wrap(pf.font_sel, &start, &len, &str[start], maxwidth)) + while (next_wrap(pf.font_sel, &start, &len, &text[start], maxwidth)) { y += linespacing; } @@ -1309,7 +1308,7 @@ int print_wrap( start = 0; } - while (next_wrap_buf(pf.font_sel, buffer, sizeof(buffer), &start, str, maxwidth)) + while (next_wrap_buf(pf.font_sel, buffer, sizeof(buffer), &start, text, maxwidth)) { print(flags, x, y, buffer, r, g, b); diff --git a/desktop_version/src/Font.h b/desktop_version/src/Font.h index 5949b900..b768796e 100644 --- a/desktop_version/src/Font.h +++ b/desktop_version/src/Font.h @@ -96,7 +96,7 @@ int print_wrap( uint32_t flags, int x, int y, - const std::string& text, + const char* text, uint8_t r, uint8_t g, uint8_t b, int linespacing = -1, int maxwidth = -1 diff --git a/desktop_version/src/Render.cpp b/desktop_version/src/Render.cpp index 5e293357..2a8f920c 100644 --- a/desktop_version/src/Render.cpp +++ b/desktop_version/src/Render.cpp @@ -614,7 +614,7 @@ static void menurender(void) } else if ((unsigned)game.currentmenuoption < loc::languagelist.size()) { - font::print_wrap(PR_CEN, -1, 8, loc::languagelist[game.currentmenuoption].credit, tr/2, tg/2, tb/2); + font::print_wrap(PR_CEN, -1, 8, loc::languagelist[game.currentmenuoption].credit.c_str(), tr/2, tg/2, tb/2); font::print(PR_CEN, -1, 230, loc::languagelist[game.currentmenuoption].action_hint, tr/2, tg/2, tb/2); } break;