Skip to content

Commit

Permalink
Client: HUD: History: Remove hardcoded margins
Browse files Browse the repository at this point in the history
  • Loading branch information
tmp64 committed Nov 25, 2023
1 parent f758899 commit 44015b1
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 7 deletions.
2 changes: 2 additions & 0 deletions src/game/client/cl_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ inline struct cvar_s *CVAR_CREATE(const char *cv, const char *val, const int fla
#define XRES(x) (x * ((float)ScreenWidth / 640))
#define YRES(y) (y * ((float)ScreenHeight / 480))

#define SPR_RES_SCALED(x) (x * ((float)gHUD.m_iRes / HUD_FALLBACK_RES))

#define GetScreenInfo (*gEngfuncs.pfnGetScreenInfo)
#define ServerCmd (*gEngfuncs.pfnServerCmd)
#define EngineClientCmd (*gEngfuncs.pfnClientCmd)
Expand Down
20 changes: 20 additions & 0 deletions src/game/client/hud.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <cstdio>
#include <FileSystem.h>
#include <vgui/IScheme.h>
#include <vgui/ISurface.h>
#include <vgui_controls/AnimationController.h>
#include <vgui_controls/Controls.h>

Expand Down Expand Up @@ -434,6 +435,8 @@ void CHud::VidInit(void)
if (m_iRes == -1)
m_iRes = GetHudSize(m_scrinfo, GetMaxHudScale());

m_iTextSize = GetTextSize();

// Only load this once
if (!m_pSpriteList)
{
Expand Down Expand Up @@ -836,6 +839,23 @@ EHudScale CHud::DetectMaxHudScale()
return pMaxScaleInfo->nScale;
}

int CHud::GetTextSize()
{
vgui2::HScheme hScheme = g_pVGuiSchemeManager->GetDefaultScheme();
if (!hScheme)
return 0;

vgui2::IScheme *pScheme = g_pVGuiSchemeManager->GetIScheme(hScheme);
if (!pScheme)
return 0;

vgui2::HFont hFont = pScheme->GetFont("CreditsFont", true);
if (!hFont)
return 0;

return g_pVGuiSurface->GetFontTall(hFont);
}

CON_COMMAND(append, "Puts a command into the end of the command buffer")
{
if (gEngfuncs.Cmd_Argc() != 2)
Expand Down
4 changes: 4 additions & 0 deletions src/game/client/hud.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ class CHud
int m_Teamplay;
int m_iRes = -1;
int m_iFontHeight;
int m_iTextSize = 0;
int m_iWeaponBits;
int m_fPlayerDead;
int m_iIntermission;
Expand Down Expand Up @@ -303,6 +304,9 @@ class CHud
//! Detects the maximum supported HUD scale.
EHudScale DetectMaxHudScale();

//! @returns The font size used by engine text draw functions.
int GetTextSize();

template <typename T>
inline T *RegisterHudElem()
{
Expand Down
4 changes: 2 additions & 2 deletions src/game/client/hud/ammo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1149,7 +1149,7 @@ int CHudAmmo::DrawWList(float flTime)
else
{
// Pink rect
gEngfuncs.pfnFillRGBA(x, y, rcSprSelection.right - rcSprSelection.left, rcSprSelection.bottom - rcSprSelection.top, 64, 0, 64, a);
gEngfuncs.pfnFillRGBA(x, y, rcSprSelection.GetWidth(), rcSprSelection.GetHeight(), 64, 0, 64, a);
}

SPR_Set(hSprSelection, r, g, b);
Expand Down Expand Up @@ -1179,7 +1179,7 @@ int CHudAmmo::DrawWList(float flTime)
else
{
// Pink rect
gEngfuncs.pfnFillRGBA(x, y, rcSprSelection.right - rcSprSelection.left, rcSprSelection.bottom - rcSprSelection.top, 48, 0, 48, a);
gEngfuncs.pfnFillRGBA(x, y, rcSprSelection.GetWidth(), rcSprSelection.GetHeight(), 48, 0, 48, a);
}
}

Expand Down
16 changes: 11 additions & 5 deletions src/game/client/hud/ammohistory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ void HistoryResource::CheckClearHistory(void)
//
int HistoryResource::DrawAmmoHistory(float flTime)
{
//! Margin from the right screen border to sprite center
const int itemMarginRight = SPR_RES_SCALED(24);
const int weaponMarginRight = SPR_RES_SCALED(16);
const int itemWidth = SPR_RES_SCALED(16);

for (int i = 0; i < MAX_HISTORY; i++)
{
if (rgAmmoHistory[i].type)
Expand Down Expand Up @@ -134,15 +139,16 @@ int HistoryResource::DrawAmmoHistory(float flTime)

// Draw the pic
int ypos = ScreenHeight - (AMMO_PICKUP_PICK_HEIGHT + (AMMO_PICKUP_GAP * i));
int xpos = ScreenWidth - 24;
int xpos = ScreenWidth - itemMarginRight;
if (spr && *spr) // weapon isn't loaded yet so just don't draw the pic
{ // the dll has to make sure it has sent info the weapons you need
SPR_Set(*spr, r, g, b);
SPR_DrawAdditive(0, xpos, ypos, &rcPic);
SPR_DrawAdditive(0, xpos - rcPic.GetWidth() / 2, ypos, &rcPic);
}

// Draw the number
gHUD.DrawHudNumberString(xpos - 10, ypos, xpos - 100, rgAmmoHistory[i].iCount, r, g, b);
int yposText = ypos + rcPic.GetHeight() / 2 - gHUD.m_iTextSize / 2;
gHUD.DrawHudNumberString(xpos - itemWidth, yposText, xpos - 100, rgAmmoHistory[i].iCount, r, g, b);
}
else if (rgAmmoHistory[i].type == HISTSLOT_WEAP)
{
Expand All @@ -165,7 +171,7 @@ int HistoryResource::DrawAmmoHistory(float flTime)
ScaleColors(r, g, b, a);

int ypos = ScreenHeight - (AMMO_PICKUP_PICK_HEIGHT + (AMMO_PICKUP_GAP * i));
int xpos = ScreenWidth - (weap->rcInactive.right - weap->rcInactive.left);
int xpos = ScreenWidth - weap->rcInactive.GetWidth() - weaponMarginRight;
SPR_Set(weap->hInactive, r, g, b);
SPR_DrawAdditive(0, xpos, ypos, &weap->rcInactive);
}
Expand All @@ -186,7 +192,7 @@ int HistoryResource::DrawAmmoHistory(float flTime)
ScaleColors(r, g, b, a);

int ypos = ScreenHeight - (AMMO_PICKUP_PICK_HEIGHT + (AMMO_PICKUP_GAP * i));
int xpos = ScreenWidth - (rect.right - rect.left) - 10;
int xpos = ScreenWidth - rect.GetWidth() / 2 - itemMarginRight;

SPR_Set(gHUD.GetSprite(rgAmmoHistory[i].iId), r, g, b);
SPR_DrawAdditive(0, xpos, ypos, &rect);
Expand Down
3 changes: 3 additions & 0 deletions src/game/client/wrect.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
typedef struct rect_s
{
int left, right, top, bottom;

int GetWidth() const { return right - left; }
int GetHeight() const { return bottom - top; }
} wrect_t;

#endif

0 comments on commit 44015b1

Please sign in to comment.