You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
102 lines
3.0 KiB
102 lines
3.0 KiB
|
7 years ago
|
#include "DiabloUI/text_draw.h"
|
||
|
|
|
||
|
7 years ago
|
#include "DiabloUI/art_draw.h"
|
||
|
5 years ago
|
#include "DiabloUI/diabloui.h"
|
||
|
7 years ago
|
#include "DiabloUI/fonts.h"
|
||
|
|
#include "DiabloUI/text.h"
|
||
|
7 years ago
|
#include "DiabloUI/ttf_render_wrapped.h"
|
||
|
5 years ago
|
#include "DiabloUI/ui_item.h"
|
||
|
|
#include "utils/display.h"
|
||
|
7 years ago
|
|
||
|
5 years ago
|
namespace devilution {
|
||
|
7 years ago
|
|
||
|
7 years ago
|
extern SDL_Surface *pal_surface;
|
||
|
|
|
||
|
7 years ago
|
namespace {
|
||
|
|
|
||
|
7 years ago
|
TextAlignment XAlignmentFromFlags(int flags)
|
||
|
|
{
|
||
|
|
if (flags & UIS_CENTER)
|
||
|
6 years ago
|
return TextAlignment_CENTER;
|
||
|
7 years ago
|
if (flags & UIS_RIGHT)
|
||
|
6 years ago
|
return TextAlignment_END;
|
||
|
|
return TextAlignment_BEGIN;
|
||
|
7 years ago
|
}
|
||
|
|
|
||
|
7 years ago
|
int AlignXOffset(int flags, const SDL_Rect &dest, int w)
|
||
|
7 years ago
|
{
|
||
|
7 years ago
|
if (flags & UIS_CENTER)
|
||
|
|
return (dest.w - w) / 2;
|
||
|
|
if (flags & UIS_RIGHT)
|
||
|
|
return dest.w - w;
|
||
|
|
return 0;
|
||
|
7 years ago
|
}
|
||
|
|
|
||
|
|
} // namespace
|
||
|
|
|
||
|
6 years ago
|
void DrawTTF(const char *text, const SDL_Rect &rectIn, int flags,
|
||
|
7 years ago
|
const SDL_Color &text_color, const SDL_Color &shadow_color,
|
||
|
|
TtfSurfaceCache **render_cache)
|
||
|
|
{
|
||
|
6 years ago
|
SDL_Rect rect(rectIn);
|
||
|
6 years ago
|
if (font == NULL || text == NULL || *text == '\0')
|
||
|
7 years ago
|
return;
|
||
|
6 years ago
|
if (*render_cache == NULL) {
|
||
|
6 years ago
|
const auto x_align = XAlignmentFromFlags(flags);
|
||
|
5 years ago
|
*render_cache = new TtfSurfaceCache {
|
||
|
|
/*.text=*/ScaleSurfaceToOutput(SDLSurfaceUniquePtr { RenderUTF8_Solid_Wrapped(font, text, text_color, rect.w, x_align) }),
|
||
|
|
/*.shadow=*/ScaleSurfaceToOutput(SDLSurfaceUniquePtr { RenderUTF8_Solid_Wrapped(font, text, shadow_color, rect.w, x_align) }),
|
||
|
|
};
|
||
|
7 years ago
|
}
|
||
|
5 years ago
|
SDL_Surface *text_surface = (*render_cache)->text.get();
|
||
|
|
SDL_Surface *shadow_surface = (*render_cache)->shadow.get();
|
||
|
6 years ago
|
if (text_surface == NULL)
|
||
|
7 years ago
|
return;
|
||
|
7 years ago
|
|
||
|
|
SDL_Rect dest_rect = rect;
|
||
|
6 years ago
|
ScaleOutputRect(&dest_rect);
|
||
|
|
dest_rect.x += AlignXOffset(flags, dest_rect, text_surface->w);
|
||
|
|
dest_rect.y += (flags & UIS_VCENTER) ? (dest_rect.h - text_surface->h) / 2 : 0;
|
||
|
7 years ago
|
|
||
|
|
SDL_Rect shadow_rect = dest_rect;
|
||
|
7 years ago
|
++shadow_rect.x;
|
||
|
|
++shadow_rect.y;
|
||
|
5 years ago
|
if (SDL_BlitSurface(shadow_surface, NULL, DiabloUiSurface(), &shadow_rect) < 0)
|
||
|
6 years ago
|
ErrSdl();
|
||
|
5 years ago
|
if (SDL_BlitSurface(text_surface, NULL, DiabloUiSurface(), &dest_rect) < 0)
|
||
|
6 years ago
|
ErrSdl();
|
||
|
7 years ago
|
}
|
||
|
|
|
||
|
|
void DrawArtStr(const char *text, const SDL_Rect &rect, int flags, bool drawTextCursor)
|
||
|
7 years ago
|
{
|
||
|
|
_artFontTables size = AFT_SMALL;
|
||
|
|
_artFontColors color = flags & UIS_GOLD ? AFC_GOLD : AFC_SILVER;
|
||
|
|
|
||
|
|
if (flags & UIS_MED)
|
||
|
|
size = AFT_MED;
|
||
|
|
else if (flags & UIS_BIG)
|
||
|
|
size = AFT_BIG;
|
||
|
|
else if (flags & UIS_HUGE)
|
||
|
|
size = AFT_HUGE;
|
||
|
|
|
||
|
7 years ago
|
const int x = rect.x + AlignXOffset(flags, rect, GetArtStrWidth(text, size));
|
||
|
|
const int y = rect.y + ((flags & UIS_VCENTER) ? (rect.h - ArtFonts[size][color].h()) / 2 : 0);
|
||
|
7 years ago
|
|
||
|
7 years ago
|
int sx = x, sy = y;
|
||
|
7 years ago
|
for (size_t i = 0, n = strlen(text); i < n; i++) {
|
||
|
|
if (text[i] == '\n') {
|
||
|
|
sx = x;
|
||
|
|
sy += ArtFonts[size][color].h();
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
BYTE w = FontTables[size][*(BYTE *)&text[i] + 2] ? FontTables[size][*(BYTE *)&text[i] + 2] : FontTables[size][0];
|
||
|
|
DrawArt(sx, sy, &ArtFonts[size][color], *(BYTE *)&text[i], w);
|
||
|
|
sx += w;
|
||
|
|
}
|
||
|
|
if (drawTextCursor && GetAnimationFrame(2, 500)) {
|
||
|
|
DrawArt(sx, sy, &ArtFonts[size][color], '|');
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
5 years ago
|
} // namespace devilution
|