Skip to content

Commit

Permalink
Merge pull request #699 from Wargus/KeepRatio
Browse files Browse the repository at this point in the history
Implement keep ratio for `CGraphic` (exposed to tolua++)
  • Loading branch information
Jarod42 authored Jul 27, 2024
2 parents c790a75 + 15ab650 commit 7be1f5f
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 9 deletions.
3 changes: 3 additions & 0 deletions src/include/video.h
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ class CGraphic : public gcn::SDLImage
void Load(bool grayscale = false);
void Flip();
void Resize(int w, int h);
void ResizeKeepRatio(int w, int h);
void SetOriginalSize();
void AppendFrames(const sequence_of_images &frames);
bool TransparentPixel(int x, int y);
Expand Down Expand Up @@ -198,6 +199,8 @@ class CGraphic : public gcn::SDLImage
int Width = 0; /// Width of a frame
int Height = 0; /// Height of a frame
int NumFrames = 1; /// Number of frames
int OriginWidth = 0; /// Origin graphic width
int OriginHeight = 0; /// Origin graphic height
bool Resized = false; /// Image has been resized

friend class CFont;
Expand Down
10 changes: 1 addition & 9 deletions src/stratagus/title.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,7 @@ static void ApplyStretchMode(CGraphic &g, EStretchMode mode, int width, int heig
{
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::KeepRatio: g.ResizeKeepRatio(width, height); break;
case EStretchMode::Stretch: g.Resize(width, height); break;
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/tolua/video.pkg
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ void ToggleFullScreen(void);
$using CGraphicPtr = std::shared_ptr<CGraphic>;
$void Load(CGraphicPtr* self, bool grayscale) { (*self)->Load(grayscale); }
$void Resize(CGraphicPtr* self, int w, int h) { (*self)->Resize(w, h); }
$void ResizeKeepRatio(CGraphicPtr* self, int w, int h) { (*self)->ResizeKeepRatio(w, h); }
$void SetPaletteColor(CGraphicPtr* self, int idx, int r, int g, int b) { (*self)->SetPaletteColor(idx, r, g, b); }
$void OverlayGraphic(CGraphicPtr* self, CGraphicPtr g, bool mask) { (*self)->OverlayGraphic(g.get(), mask); }

Expand All @@ -35,6 +36,7 @@ struct CGraphicPtr // std::shared_ptr<CGraphic>
{
tolua_outside void Load(bool grayscale = false);
tolua_outside void Resize(int w, int h);
tolua_outside void ResizeKeepRatio(int w, int h);
tolua_outside void SetPaletteColor(int idx, int r, int g, int b);
tolua_outside void OverlayGraphic(CGraphicPtr g, bool mask = false);
};
Expand Down
12 changes: 12 additions & 0 deletions src/video/graphic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,9 @@ void CGraphic::Load(bool grayscale)
ExitFatal(-1);
}

OriginWidth = mSurface->w;
OriginHeight = mSurface->h;

if (mSurface->format->BytesPerPixel == 1) {
VideoPaletteListAdd(mSurface);
}
Expand Down Expand Up @@ -902,6 +905,15 @@ void CGraphic::Resize(int w, int h)
GenFramesMap();
}

void CGraphic::ResizeKeepRatio(int width, int height)
{
if (this->OriginWidth * height < width * this->OriginHeight) {
this->Resize(this->OriginWidth * height / this->OriginHeight, height);
} else {
this->Resize(width, this->OriginHeight * width / this->OriginWidth);
}
}

/**
** Sets the original size for a graphic
**
Expand Down

0 comments on commit 7be1f5f

Please sign in to comment.