From 38df36c3fd509e9eeba00a5bc91d046514cac111 Mon Sep 17 00:00:00 2001 From: Folkert van Verseveld Date: Fri, 10 May 2024 22:08:18 +0200 Subject: [PATCH] refactor some ui routines: add strnum, str_scream --- game/src/ui.cpp | 15 +++++++++++++++ game/src/ui.hpp | 3 +++ game/src/ui/game.cpp | 40 ++++++---------------------------------- 3 files changed, 24 insertions(+), 34 deletions(-) diff --git a/game/src/ui.cpp b/game/src/ui.cpp index 41e9e57..0d618ea 100644 --- a/game/src/ui.cpp +++ b/game/src/ui.cpp @@ -648,6 +648,21 @@ void UICache::str2(const ImVec2 &pos, const char *text, bool invert) { bkg->AddText(pos, fg, text); } +void UICache::strnum(const ImVec2 &pos, int v) { + char buf[16]; + snprintf(buf, sizeof buf, "%d", v); + str2(pos, buf); +} + +void UICache::str_scream(const char *txt) { + ImGuiIO &io = ImGui::GetIO(); + FontGuard fg(e->fnt.fnt_copper2); + + ImVec2 sz(ImGui::CalcTextSize(txt)); + + bkg->AddText(ImVec2((io.DisplaySize.x - sz.x) / 2, (io.DisplaySize.y - sz.y) / 2), IM_COL32_WHITE, txt); +} + void UICache::game_mouse_process() { ZoneScoped; diff --git a/game/src/ui.hpp b/game/src/ui.hpp index d09bc2d..73b3916 100644 --- a/game/src/ui.hpp +++ b/game/src/ui.hpp @@ -249,6 +249,9 @@ class UICache final { void show_hud_selection(float left, float top, float menubar_h); void str2(const ImVec2 &pos, const char *text, bool invert=false); + void strnum(const ImVec2 &pos, int v); + + void str_scream(const char *txt); const gfx::ImageRef &imgtile(uint8_t v); diff --git a/game/src/ui/game.cpp b/game/src/ui/game.cpp index 2263f0b..c5e7204 100644 --- a/game/src/ui/game.cpp +++ b/game/src/ui/game.cpp @@ -302,28 +302,12 @@ void UICache::show_multiplayer_game() { gmb_top.y = vp->WorkPos.y; gmb_top.h = menubar_h; - char buf[16]; - snprintf(buf, sizeof buf, "%d", p.res.wood); - buf[(sizeof buf) - 1] = '\0'; - float y = vp->WorkPos.y + 2 * scale; - str2(ImVec2(menubar_left + 32 * scale, y), buf); - - snprintf(buf, sizeof buf, "%d", p.res.food); - buf[(sizeof buf) - 1] = '\0'; - - str2(ImVec2(menubar_left + 99 * scale, y), buf); - - snprintf(buf, sizeof buf, "%d", p.res.gold); - buf[(sizeof buf) - 1] = '\0'; - - str2(ImVec2(menubar_left + 166 * scale, y), buf); - - snprintf(buf, sizeof buf, "%d", p.res.stone); - buf[(sizeof buf) - 1] = '\0'; - - str2(ImVec2(menubar_left + 234 * scale, y), buf); + strnum(ImVec2(menubar_left + 32 * scale, y), p.res.wood); + strnum(ImVec2(menubar_left + 99 * scale, y), p.res.food); + strnum(ImVec2(menubar_left + 166 * scale, y), p.res.gold); + strnum(ImVec2(menubar_left + 234 * scale, y), p.res.stone); ImVec2 sz(ImGui::CalcTextSize(age.c_str())); @@ -418,21 +402,9 @@ void UICache::show_multiplayer_game() { e->show_multiplayer_diplomacy(); if (e->cv.gameover) { - FontGuard fg(e->fnt.fnt_copper2); - - const char *txt = e->cv.victory ? "Victory" : "Game Over"; - - ImVec2 sz(ImGui::CalcTextSize(txt)); - - lst->AddText(ImVec2((io.DisplaySize.x - sz.x) / 2, (io.DisplaySize.y - sz.y) / 2), IM_COL32_WHITE, txt); + str_scream(e->cv.victory ? "Victory" : "Game Over"); } else if (!e->client->g.running) { - FontGuard fg(e->fnt.fnt_copper2); - - const char *txt = "Game Paused"; - - ImVec2 sz(ImGui::CalcTextSize(txt)); - - lst->AddText(ImVec2((io.DisplaySize.x - sz.x) / 2, (io.DisplaySize.y - sz.y) / 2), IM_COL32_WHITE, txt); + str_scream("Game Paused"); } }