Skip to content

Commit

Permalink
Merge pull request #690 from Wargus/printfFix
Browse files Browse the repository at this point in the history
Printf fix
  • Loading branch information
Jarod42 authored Jun 2, 2024
2 parents 3941a35 + a41a45e commit f97f671
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/missile/missile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ void MissileType::LoadMissileSprite()
}

// Correct the number of frames in graphic
DebugPrint("%s - %d>=%d\n", this->G->File.c_str(), this->G->NumFrames, this->SpriteFrames);
DebugPrint("%s - %d>=%d\n", this->G->File.string().c_str(), this->G->NumFrames, this->SpriteFrames);
Assert(this->G->NumFrames >= this->SpriteFrames);
this->G->NumFrames = this->SpriteFrames;
// FIXME: Don't use NumFrames as number of frames.
Expand Down
34 changes: 23 additions & 11 deletions src/ui/contenttype.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ void CContentTypeText::Draw(const CUnit &unit, CFont *defaultfont) const /* over
std::string text = EvalString(*this->Text);
std::string::size_type pos;
if ((pos = text.find("~|")) != std::string::npos) {
x += (label.Draw(x - font.getWidth(text.substr(0, pos)), y, text) - font.getWidth(text.substr(0, pos)));
const int offsetx = font.Width(std::string_view(text).substr(0, pos));
x += label.Draw(x - offsetx, y, text) - offsetx;
} else if (this->Centered) {
x += (label.DrawCentered(x, y, text) * 2);
} else {
Expand Down Expand Up @@ -105,16 +106,26 @@ void CContentTypeText::Draw(const CUnit &unit, CFont *defaultfont) const /* over

namespace
{
auto tr(const char *s)
std::string tr(const char *s)
{
return _(s);
return Translate(s);
}

auto tr(int n)
{
return n;
}

auto printfArg(const std::string& s)
{
return s.c_str();
}

auto printfArg(int n)
{
return n;
}

} // namespace

/**
Expand All @@ -137,14 +148,13 @@ void CContentTypeFormattedText::Draw(const CUnit &unit, CFont *defaultfont) cons

Assert((unsigned int) this->Index < UnitTypeVar.GetNumberVariable());
const auto usi1 = GetComponent(unit, this->Index, this->Component, 0);
std::visit(
[&](auto v) { snprintf(buf, sizeof(buf), this->Format.c_str(), tr(v)); },
usi1);
std::visit([&](auto v) { snprintf(buf, sizeof(buf), this->Format.c_str(), printfArg(tr(v))); },
usi1);

char *pos;
if ((pos = strstr(buf, "~|")) != nullptr) {
std::string buf2(buf);
label.Draw(this->Pos.x - font.getWidth(buf2.substr(0, pos - buf)), this->Pos.y, buf);
const int offsetx = font.Width(std::string_view(buf, pos - buf));
label.Draw(this->Pos.x - offsetx, this->Pos.y, buf);
} else if (this->Centered) {
label.DrawCentered(this->Pos.x, this->Pos.y, buf);
} else {
Expand Down Expand Up @@ -173,13 +183,15 @@ void CContentTypeFormattedText2::Draw(const CUnit &unit, CFont *defaultfont) con
const auto usi1 = GetComponent(unit, this->Index1, this->Component1, 0);
const auto usi2 = GetComponent(unit, this->Index2, this->Component2, 0);
std::visit(
[&](auto v1, auto v2) { snprintf(buf, sizeof(buf), this->Format.c_str(), tr(v1), tr(v2)); },
[&](auto v1, auto v2) {
snprintf(buf, sizeof(buf), this->Format.c_str(), printfArg(tr(v1)), printfArg(tr(v2)));
},
usi1,
usi2);
char *pos;
if ((pos = strstr(buf, "~|")) != nullptr) {
std::string buf2(buf);
label.Draw(this->Pos.x - font.getWidth(buf2.substr(0, pos - buf)), this->Pos.y, buf);
const int offsetx = font.Width(std::string_view(buf, pos - buf));
label.Draw(this->Pos.x - offsetx, this->Pos.y, buf);
} else if (this->Centered) {
label.DrawCentered(this->Pos.x, this->Pos.y, buf);
} else {
Expand Down

0 comments on commit f97f671

Please sign in to comment.