Skip to content

Commit

Permalink
refactor some ui routines: add strnum, str_scream
Browse files Browse the repository at this point in the history
  • Loading branch information
FolkertVanVerseveld committed May 10, 2024
1 parent 3f8a9c8 commit 38df36c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 34 deletions.
15 changes: 15 additions & 0 deletions game/src/ui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
3 changes: 3 additions & 0 deletions game/src/ui.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
40 changes: 6 additions & 34 deletions game/src/ui/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()));

Expand Down Expand Up @@ -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");
}
}

Expand Down

0 comments on commit 38df36c

Please sign in to comment.