Skip to content

Commit

Permalink
Allow to define stretch mode to title screen:
Browse files Browse the repository at this point in the history
```
SetTitleScreens(
  {Image = "ui/title.png", StretchMode = "none"}, -- Keep original image size
  {Image = "ui/title.png", StretchMode = "keep-ratio"}, -- resize to touch border, and keep original size ratio
  {Image = "ui/title.png", StretchMode = "stretch"} -- (default) resize to the full windows
)
```
  • Loading branch information
Jarod42 committed Jul 24, 2024
1 parent 517ce4c commit c62640f
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 6 deletions.
13 changes: 11 additions & 2 deletions src/include/title.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,15 @@ class TitleScreenLabel
int Flags = 0;
};

enum class EStretchMode
{
None,
KeepRatio,
Stretch
};

std::optional<EStretchMode> StretchModeFromString(std::string_view);

class TitleScreen
{
public:
Expand All @@ -64,11 +73,11 @@ class TitleScreen
void ShowTitleImage();

private:
void ShowLabels();
void ShowLabels() const;
public:
fs::path File;
std::string Music;
bool StretchImage = true;
EStretchMode StretchMode = EStretchMode::Stretch;
int Timeout = 0;
int Editor = 0;
std::vector<std::unique_ptr<TitleScreenLabel>> Labels;
Expand Down
36 changes: 32 additions & 4 deletions src/stratagus/title.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,37 @@ std::vector<std::unique_ptr<TitleScreen>> TitleScreens; /// Title screens to sho
static bool WaitNoEvent; /// Flag got an event
extern std::string CliMapName;

void TitleScreen::ShowLabels()
static void ApplyStretchMode(CGraphic &g, EStretchMode mode, int width, int height)
{
switch (mode) {
case EStretchMode::None: break;
case EStretchMode::KeepRatio:
{
if (g.Width * height < width * g.Height) {
g.Resize(g.Width * height / g.Height, height);
} else {
g.Resize(width, g.Height * width / g.Width);
}
break;
}
case EStretchMode::Stretch: g.Resize(width, height); break;
}
}

std::optional<EStretchMode> StretchModeFromString(std::string_view s)
{
if (s == "none") {
return EStretchMode::None;
} else if (s == "keep-ratio") {
return EStretchMode::KeepRatio;
} else if (s == "stretch") {
return EStretchMode::Stretch;
} else {
return std::nullopt;
}
}

void TitleScreen::ShowLabels() const
{
for (auto &titleScreenlabel : this->Labels) {
if (!titleScreenlabel->Font) {
Expand Down Expand Up @@ -85,9 +115,7 @@ void TitleScreen::ShowTitleImage()

auto g = CGraphic::New(this->File.string());
g->Load();
if (this->StretchImage) {
g->Resize(Video.Width, Video.Height);
}
ApplyStretchMode(*g, this->StretchMode, Video.Width, Video.Height);

int timeout = this->Timeout ? this->Timeout * CYCLES_PER_SECOND : -1;

Expand Down
7 changes: 7 additions & 0 deletions src/ui/script_ui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,13 @@ static int CclSetTitleScreens(lua_State *l)
const std::string_view value = LuaToString(l, -2);
if (value == "Image") {
TitleScreens[j]->File = LuaToString(l, -1);
} else if (value == "StretchMode") {
const std::string_view value = LuaToString(l, -1);
if (auto stretchMode = StretchModeFromString(value)) {
TitleScreens[j]->StretchMode = *stretchMode;
} else {
LuaError(l, "Unknown StretchMode %s", value.data());
}
} else if (value == "Music") {
TitleScreens[j]->Music = LuaToString(l, -1);
} else if (value == "Timeout") {
Expand Down

0 comments on commit c62640f

Please sign in to comment.