From fd72b491418c08e8362673ab49bf6d143795f591 Mon Sep 17 00:00:00 2001 From: tmp64 Date: Sat, 25 Nov 2023 10:51:17 +0700 Subject: [PATCH] Client: HUD: Ammo: Fix custom sprites on scaled HUDs --- src/game/client/hud.h | 3 +++ src/game/client/hud/ammo.cpp | 25 ++++++++++++++++++------- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/src/game/client/hud.h b/src/game/client/hud.h index 36e180a4..feed442a 100644 --- a/src/game/client/hud.h +++ b/src/game/client/hud.h @@ -52,6 +52,9 @@ #define HUD_INTERMISSION 2 #define HUD_DRAW_ALWAYS 4 //!< Draw even if hud_draw = 0 +//! Fallback sprite resolution if the current one can't be found. +constexpr int HUD_FALLBACK_RES = 640; + namespace vgui2 { class IScheme; diff --git a/src/game/client/hud/ammo.cpp b/src/game/client/hud/ammo.cpp index ef0a7035..b8e1aeb9 100644 --- a/src/game/client/hud/ammo.cpp +++ b/src/game/client/hud/ammo.cpp @@ -78,12 +78,8 @@ int WeaponsResource::HasAmmo(WEAPON *p) void WeaponsResource::LoadWeaponSprites(WEAPON *pWeapon) { - int i, iRes; - - if (ScreenWidth < 640) - iRes = 320; - else - iRes = 640; + int i = 0; + int iRes = gHUD.m_iRes; char sz[256]; @@ -1259,12 +1255,27 @@ client_sprite_t *GetSpriteFromList(client_sprite_t *pList, const char *pszNameSt int len = strlen(pszNameStart); client_sprite_t *p = pList; + client_sprite_t *pFallback = nullptr; //!< Fallback sprite if can't find for current HUD scale while (iCount--) { + if (!strncmp(pszNameStart, p->szName, len)) + { + if (p->iRes == iRes) + { + // Found the requested sprite + return p; + } + else if (p->iRes == HUD_FALLBACK_RES) + { + // At least found a fallbcak + pFallback = p; + } + } if (p->iRes == iRes && !strncmp(pszNameStart, p->szName, len)) return p; p++; } - return NULL; + // Return the fallback. It may be null but that's fine. + return pFallback; }