From d1f455ba277df793e2f7c6276cbee0d897b796ce Mon Sep 17 00:00:00 2001 From: Michael Smith Date: Sun, 8 Sep 2024 14:01:13 +0100 Subject: Improve the HUD API slightly - Add text size measurement (for centring etc.). - Add a wchar_t equivalent since ushort would be wrong on Linux. Avoid actually using wchar.h because it's a big bloaty header, particularly on Windows. --- gamedata/vguimatsurface.txt | 3 +++ src/hud.c | 12 +++++++++--- src/hud.h | 17 ++++++++++++++--- 3 files changed, 26 insertions(+), 6 deletions(-) diff --git a/gamedata/vguimatsurface.txt b/gamedata/vguimatsurface.txt index a9dc3f2..e35c125 100644 --- a/gamedata/vguimatsurface.txt +++ b/gamedata/vguimatsurface.txt @@ -45,5 +45,8 @@ vtidx_GetFontTall vtidx_GetCharacterWidth OrangeBoxbased 71 L4D 71 +vtidx_GetTextSize + OrangeBoxbased 72 + L4D 72 # vi: sw=4 ts=4 noet tw=80 cc=80 diff --git a/src/hud.c b/src/hud.c index b1cbb56..2334bb8 100644 --- a/src/hud.c +++ b/src/hud.c @@ -81,10 +81,12 @@ DECL_VFUNC_DYN(void, DrawPolyLine, int *, int *, int) DECL_VFUNC_DYN(void, DrawSetTextFont, struct handlewrap) DECL_VFUNC_DYN(void, DrawSetTextColor, struct rgba) DECL_VFUNC_DYN(void, DrawSetTextPos, int, int) -DECL_VFUNC_DYN(void, DrawPrintText, ushort *, int, int) +DECL_VFUNC_DYN(void, DrawPrintText, hud_wchar *, int, int) DECL_VFUNC_DYN(void, GetScreenSize, int *, int *) DECL_VFUNC_DYN(int, GetFontTall, struct handlewrap) DECL_VFUNC_DYN(int, GetCharacterWidth, struct handlewrap, int) +DECL_VFUNC_DYN(int, GetTextSize, struct handlewrap, const hud_wchar *, + int *, int *) // vgui::Panel DECL_VFUNC_DYN(void, SetPaintEnabled, bool) @@ -122,7 +124,7 @@ void hud_drawpolyline(int *x, int *y, int npoints, struct rgba colour) { DrawPolyLine(matsurf, x, y, npoints); } -void hud_drawtext(ulong font, int x, int y, struct rgba colour, ushort *str, +void hud_drawtext(ulong font, int x, int y, struct rgba colour, hud_wchar *str, int len) { DrawSetTextFont(matsurf, (struct handlewrap){font}); DrawSetTextPos(matsurf, x, y); @@ -138,10 +140,14 @@ int hud_fontheight(ulong font) { return GetFontTall(matsurf, (struct handlewrap){font}); } -int hud_charwidth(ulong font, int ch) { +int hud_charwidth(ulong font, hud_wchar ch) { return GetCharacterWidth(matsurf, (struct handlewrap){font}, ch); } +void hud_textsize(ulong font, const ushort *s, int *width, int *height) { + GetTextSize(matsurf, (struct handlewrap){font}, s, width, height); +} + static bool find_toolspanel(void *enginevgui) { const uchar *insns = (const uchar *)VFUNC(enginevgui, GetPanel); for (const uchar *p = insns; p - insns < 16;) { diff --git a/src/hud.h b/src/hud.h index fed152f..e3aea1d 100644 --- a/src/hud.h +++ b/src/hud.h @@ -1,5 +1,6 @@ /* * Copyright © 2022 Matthew Wozniak + * Copyright © 2024 Michael Smith * * Permission to use, copy, modify, and/or distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -21,6 +22,13 @@ #include "engineapi.h" #include "intdefs.h" +// ugh! +#ifdef _WIN32 +typedef ushort hud_wchar; +#else +typedef int hud_wchar; +#endif + /* * Emitted when the game HUD is being drawn. Allows features to draw their own * additional overlays atop the game's standard HUD. @@ -57,16 +65,19 @@ void hud_drawline(int x0, int y0, int x1, int y1, struct rgba colour); void hud_drawpolyline(int *xs, int *ys, int npoints, struct rgba colour); /* Draws text using a given font handle. */ -void hud_drawtext(ulong font, int x, int y, struct rgba colour, ushort *str, +void hud_drawtext(ulong font, int x, int y, struct rgba colour, hud_wchar *str, int len); -/* Returns the width and height of the game window in pixels. */ +/* Gets the width and height of the game window in pixels. */ void hud_screensize(int *width, int *height); /* Returns the height of a font, in pixels. */ int hud_fontheight(ulong font); /* Returns the width of a font character, in pixels. */ -int hud_charwidth(ulong font, int ch); +int hud_charwidth(ulong font, hud_wchar ch); + +/* Gets the width and height of string s, in pixels, using the given font. */ +void hud_textsize(ulong font, const ushort *s, int *width, int *height); #endif -- cgit v1.2.3