Fixed text alignment to be visible based rather than surface based
This commit is contained in:
@@ -77,42 +77,52 @@ TTF_Font* get_font(int size, const string& filename)
|
||||
}
|
||||
|
||||
SDL_Rect surface_align(const SDL_Surface* base, const SDL_Surface* applied,
|
||||
HorizontalAlign halign, VerticalAlign valign)
|
||||
HorizontalAlign halign, VerticalAlign valign, const SDL_Rect* hint_external)
|
||||
{
|
||||
SDL_Rect align = {.x = 0, .y = 0, .w = 0, .h = 0};
|
||||
SDL_Rect hint;
|
||||
|
||||
if ((nullptr == base) || (nullptr == applied))
|
||||
{
|
||||
return align;
|
||||
}
|
||||
|
||||
if (nullptr == hint_external)
|
||||
{
|
||||
hint = SDL_Rect{.x = 0, .y = 0, .w = applied->w, .h = applied->h};
|
||||
}
|
||||
else
|
||||
{
|
||||
hint = *hint_external;
|
||||
}
|
||||
|
||||
switch (halign)
|
||||
{
|
||||
case HALIGN_LEFT:
|
||||
align.x = 0;
|
||||
align.x = -hint.x;
|
||||
break;
|
||||
|
||||
case HALIGN_CENTER:
|
||||
align.x = base->w / 2 - applied->w / 2;
|
||||
align.x = base->w / 2 - hint.x - hint.w / 2;
|
||||
break;
|
||||
|
||||
case HALIGN_RIGHT:
|
||||
align.x = base->w - applied->w;
|
||||
align.x = base->w - (hint.x + hint.w);
|
||||
break;
|
||||
}
|
||||
|
||||
switch (valign)
|
||||
{
|
||||
case VALIGN_TOP:
|
||||
align.y = 0;
|
||||
align.y = -hint.y;
|
||||
break;
|
||||
|
||||
case VALIGN_CENTER:
|
||||
align.y = base->h / 2 - applied->h / 2;
|
||||
align.y = base->h / 2 - hint.y - hint.h / 2;
|
||||
break;
|
||||
|
||||
case VALIGN_BOTTOM:
|
||||
align.y = base->h - applied->h;
|
||||
align.y = base->h - (hint.y + hint.h);
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user