Skip to content

Instantly share code, notes, and snippets.

@untodesu
Created August 27, 2019 07:17
Show Gist options
  • Save untodesu/3635dbf280daf6c5136d8738aff0ac75 to your computer and use it in GitHub Desktop.
Save untodesu/3635dbf280daf6c5136d8738aff0ac75 to your computer and use it in GitHub Desktop.
Source SDK HL2 Beta sprite HUD implementation.
#include "cbase.h"
#include "hud_bitmapnumericdisplay.h"
#include "iclientmode.h"
#include <Color.h>
#include <KeyValues.h>
#include <vgui/ISurface.h>
#include <vgui/ISystem.h>
#include <vgui/IVGui.h>
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
using namespace vgui;
//-----------------------------------------------------------------------------
// Purpose: constructor
//-----------------------------------------------------------------------------
CHudBitmapNumericDisplay::CHudBitmapNumericDisplay(vgui::Panel *parent, const char *name, char *label) : vgui::Panel(parent, name)
{
vgui::Panel *pParent = g_pClientMode->GetViewport();
SetParent( pParent );
SetSize(256, 256);
m_iValue = 0;
m_iValue2 = 0;
m_bDisplayValue = true;
m_bDisplayValue2 = false;
m_sLabel = label;
memset(m_pNumbers, 0, 10 * sizeof(CHudTexture *));
}
//-----------------------------------------------------------------------------
// Purpose: data accessor
//-----------------------------------------------------------------------------
void CHudBitmapNumericDisplay::SetDisplayValue(int value)
{
m_iValue = value;
}
//-----------------------------------------------------------------------------
// Purpose: data accessor
//-----------------------------------------------------------------------------
void CHudBitmapNumericDisplay::SetDisplayValue2(int value)
{
m_iValue2 = value;
}
//-----------------------------------------------------------------------------
// Purpose: renders the vgui panel
//-----------------------------------------------------------------------------
void CHudBitmapNumericDisplay::Paint(void)
{
int alpha = m_flAlphaOverride / 255;
Color fgColor = GetFgColor();
fgColor[3] *= alpha;
SetFgColor( fgColor );
//Draw simple digits
if (m_bDisplayValue) {
// draw our numbers
PaintNumbers(digit_xpos, digit_ypos, m_iValue, GetFgColor());
// draw the overbright blur
for (float fl = m_flBlur; fl > 0.0f; fl -= 1.0f) {
if (fl >= 1.0f) {
PaintNumbers(digit_xpos, digit_ypos, m_iValue, GetFgColor());
}
else {
// draw a percentage of the last one
Color col = GetFgColor();
col[3] *= fl;
PaintNumbers(digit_xpos, digit_ypos, m_iValue, col);
}
}
}
//Total ammo
if(m_bDisplayValue2) {
PaintNumbersSmall(digit2_xpos, digit2_ypos, m_iValue2, GetFgColor());
}
}
//-----------------------------------------------------------------------------
// Purpose: data accessor
//-----------------------------------------------------------------------------
void CHudBitmapNumericDisplay::SetShouldDisplayValue(bool state)
{
m_bDisplayValue = state;
}
//-----------------------------------------------------------------------------
// Purpose: data accessor
//-----------------------------------------------------------------------------
void CHudBitmapNumericDisplay::SetShouldDisplayValue2(bool state)
{
m_bDisplayValue2 = state;
}
//-----------------------------------------------------------------------------
// Purpose: Draw background
//-----------------------------------------------------------------------------
void CHudBitmapNumericDisplay::PaintBackground( void )
{
SetBgColor(Color(0, 0, 0, 0));
BaseClass::PaintBackground();
}
//-----------------------------------------------------------------------------
// Purpose: Draw big number
//-----------------------------------------------------------------------------
void CHudBitmapNumericDisplay::PaintNumbers(int xpos, int ypos, int value, Color col, int numSigDigits)
{
//Load numbers
if(!m_pNumbers[0]) {
char a[16];
for(int i = 0; i < 10; i++) {
sprintf( a, "number_%d", i );
m_pNumbers[i] = gHUD.GetIcon(a);
}
if(!m_pNumbers[0]) {
return;
}
}
//Load numbers dulls
if(!m_pDull[0]) {
char a[16];
//Load big(normal)
for(int i = 0; i < 2; i++) {
sprintf(a, "number_d%d", i);
m_pDull[i] = gHUD.GetIcon(a);
}
if(!m_pDull[0]) {
return;
}
}
//Load label if needed
if(m_sLabel && !m_pLabel) {
m_pLabel = gHUD.GetIcon(m_sLabel);
if(!m_pLabel) {
return;
}
}
//Get some data
Color color = GetFgColor();
int width = m_pNumbers[0]->Width();
//Align
xpos -= width * numSigDigits;
//Draw label if needed
if(m_sLabel) {
m_pLabel->DrawSelf(xpos, ypos - (digit_height * 0.6), col);
}
//Draw digits
if(value > 0) {
int k;
//Draw 100s
if(value >= 100) {
k = value / 100;
m_pDull[0]->DrawSelf(xpos, ypos, col);
m_pNumbers[k]->DrawSelf(xpos, ypos, col);
xpos += width;
}
else {
m_pDull[0]->DrawSelf(xpos, ypos, col);
xpos += width;
}
//Draw 10s
if(value >= 10) {
k = (value % 100) / 10;
m_pDull[1]->DrawSelf(xpos, ypos, col);
m_pNumbers[k]->DrawSelf(xpos, ypos, col);
xpos += width;
}
else {
m_pDull[1]->DrawSelf(xpos, ypos, col);
xpos += width;
}
//Draw ones
k = value % 10;
m_pDull[0]->DrawSelf(xpos, ypos, col);
m_pNumbers[k]->DrawSelf(xpos, ypos, col);
xpos += width;
}
//Draw zero
else {
for(int i = 0; i < numSigDigits; i++) {
m_pDull[0]->DrawSelf(xpos, ypos, col);
if(i == (numSigDigits - 1) && value == 0) {
m_pNumbers[0]->DrawSelf(xpos, ypos, col);
}
xpos += width;
}
}
}
//-----------------------------------------------------------------------------
// Purpose: Draw small number
//-----------------------------------------------------------------------------
void CHudBitmapNumericDisplay::PaintNumbersSmall(int xpos, int ypos, int value, Color col, int numSigDigits)
{
//Load numbers
if(!m_pNumbers_s[0]) {
char a[16];
for(int i = 0; i < 10; i++) {
sprintf(a, "number_s%d", i);
m_pNumbers_s[i] = gHUD.GetIcon(a);
}
if(!m_pNumbers_s[0]) {
return;
}
}
//Load numbers dulls
if(!m_pDull_s[0]) {
char a[16];
//Load big(normal)
for(int i = 0; i < 2; i++) {
sprintf(a, "number_sd%d", i);
m_pDull_s[i] = gHUD.GetIcon(a);
}
if(!m_pDull_s[0]) {
return;
}
}
//Get some data
Color color = GetFgColor();
int width = m_pNumbers_s[0]->Width();
//int height = m_pNumbers_s[0]->Height();
//Align
xpos -= width * numSigDigits;
//Draw digits
if(value > 0) {
int k;
//Draw 100s
if(value >= 100) {
k = value / 100;
m_pDull_s[0]->DrawSelf(xpos, ypos, col);
m_pNumbers_s[k]->DrawSelf(xpos, ypos, col);
xpos += width;
}
else {
m_pDull_s[0]->DrawSelf(xpos, ypos, col);
xpos += width;
}
//Draw 10s
if(value >= 10) {
k = (value % 100) / 10;
m_pDull_s[1]->DrawSelf(xpos, ypos, col);
m_pNumbers_s[k]->DrawSelf(xpos, ypos, col);
xpos += width;
}
else {
m_pDull_s[1]->DrawSelf(xpos, ypos, col);
xpos += width;
}
//Draw ones
k = value % 10;
m_pDull_s[0]->DrawSelf(xpos, ypos, col);
m_pNumbers_s[k]->DrawSelf(xpos, ypos, col);
xpos += width;
}
//Draw zero
else {
for(int i = 0; i < numSigDigits; i++) {
m_pDull_s[0]->DrawSelf(xpos, ypos, col);
if(i == (numSigDigits - 1) && value == 0) {
m_pNumbers_s[0]->DrawSelf(xpos, ypos, col);
}
xpos += width;
}
}
}
void CHudBitmapNumericDisplay::Reset(void)
{
m_flBlur = 0.0f;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment