Solved Make my Healthbar Better

Hexui Undetected CSGO Cheats Sinkicheat PUBG Cheat

steb

Jr.Coder
Full Member
Nobleman
Aug 16, 2016
63
383
1
Just a simple esp healthbar :p

Sorry for the messy coding, someone help me make this better (by making my code small) lool.



C++:
void CTools::Healthbar(LPDIRECT3DDEVICE9 pDevice, float x, float y, float Health)
{
	if (Health>0)
	{
		DrawRect(pDevice, x - 1, y - 1, 102, 7, D3DCOLOR_ARGB(255, 20, 20, 20));

		if (Health < 101 && Health > 1)
			DrawRect(pDevice, x, y, 10, 5, D3DCOLOR_ARGB(255, 255, 0, 0));

		if (Health < 101 && Health > 10)
			DrawRect(pDevice, x + 10, y, 10, 5, D3DCOLOR_ARGB(255, 255, 0, 0));

		if (Health < 101 && Health > 20)
			DrawRect(pDevice, x + 20, y, 10, 5, D3DCOLOR_ARGB(255, 255, 125, 0));

		if (Health < 101 && Health > 30)
			DrawRect(pDevice, x + 30, y, 10, 5, D3DCOLOR_ARGB(255, 255, 125, 0));

		if (Health < 101 && Health > 40)
			DrawRect(pDevice, x + 40, y, 10, 5, D3DCOLOR_ARGB(255, 255, 255, 0));

		if (Health < 101 && Health > 50)
			DrawRect(pDevice, x + 50, y, 10, 5, D3DCOLOR_ARGB(255, 255, 255, 0));

		if (Health < 101 && Health > 60)
			DrawRect(pDevice, x + 60, y, 10, 5, D3DCOLOR_ARGB(255, 0, 255, 0));

		if (Health < 101 && Health > 70)
			DrawRect(pDevice, x + 70, y, 10, 5, D3DCOLOR_ARGB(255, 0, 255, 0));

		if (Health < 101 && Health > 80)
			DrawRect(pDevice, x + 80, y, 10, 5, D3DCOLOR_ARGB(255, 0, 0, 255));

		if (Health < 101 && Health > 90)
			DrawRect(pDevice, x + 90, y, 10, 5, D3DCOLOR_ARGB(255, 0, 0, 255));
		

                //this is just for the vertical lines inside lol
		DrawRect(pDevice, x + 5, y, 1, 5, D3DCOLOR_ARGB(255, 81, 75, 61));
		DrawRect(pDevice, x + 10, y, 1, 5, D3DCOLOR_ARGB(255, 81, 75, 61));
		DrawRect(pDevice, x + 15, y, 1, 5, D3DCOLOR_ARGB(255, 81, 75, 61));
		DrawRect(pDevice, x + 20, y, 1, 5, D3DCOLOR_ARGB(255, 81, 75, 61));
		DrawRect(pDevice, x + 25, y, 1, 5, D3DCOLOR_ARGB(255, 81, 75, 61));
		DrawRect(pDevice, x + 30, y, 1, 5, D3DCOLOR_ARGB(255, 81, 75, 61));
		DrawRect(pDevice, x + 35, y, 1, 5, D3DCOLOR_ARGB(255, 81, 75, 61));
		DrawRect(pDevice, x + 40, y, 1, 5, D3DCOLOR_ARGB(255, 81, 75, 61));
		DrawRect(pDevice, x + 45, y, 1, 5, D3DCOLOR_ARGB(255, 81, 75, 61));
		DrawRect(pDevice, x + 50, y, 1, 5, D3DCOLOR_ARGB(255, 81, 75, 61));
		DrawRect(pDevice, x + 55, y, 1, 5, D3DCOLOR_ARGB(255, 81, 75, 61));
		DrawRect(pDevice, x + 60, y, 1, 5, D3DCOLOR_ARGB(255, 81, 75, 61));
		DrawRect(pDevice, x + 65, y, 1, 5, D3DCOLOR_ARGB(255, 81, 75, 61));
		DrawRect(pDevice, x + 70, y, 1, 5, D3DCOLOR_ARGB(255, 81, 75, 61));
		DrawRect(pDevice, x + 75, y, 1, 5, D3DCOLOR_ARGB(255, 81, 75, 61));
		DrawRect(pDevice, x + 80, y, 1, 5, D3DCOLOR_ARGB(255, 81, 75, 61));
		DrawRect(pDevice, x + 85, y, 1, 5, D3DCOLOR_ARGB(255, 81, 75, 61));
		DrawRect(pDevice, x + 90, y, 1, 5, D3DCOLOR_ARGB(255, 81, 75, 61));
		DrawRect(pDevice, x + 95, y, 1, 5, D3DCOLOR_ARGB(255, 81, 75, 61));
	}
}
 
Last edited:

Broihon

edgy 12 y/o
Escobar Tier VIP
Fleep Tier Donator
Dec 22, 2013
1,745
40,528
316
This is what I came up with:
C++:
void Healthbar(IDirect3DDevice9 * pDevice, float x, float y, float Health, float Height, float MaxWidth)
{
	if (Health <= 0.0f || Health > 100.0f)
		return;

	float Ratio = Health / 100.0f;
	UINT ColourHi = 0xFF000000 | ((DWORD)(0xFF * Ratio) << 8) | ((0xFF - (DWORD)(0xFF * Ratio)) << 16);
	UINT ColourLo = 0xFFFF0000;
	
	CUSTOM_D3DVERTEX V[16] =
	{
		{ x,					y + Height, 0.0f, 1.0f, ColourLo },
		{ x,					y,			0.0f, 1.0f, ColourLo },
		{ x + MaxWidth * Ratio,	y + Height, 0.0f, 1.0f, ColourHi },
		{ x + MaxWidth * Ratio,	y,			0.0f, 1.0f, ColourHi }
	};
	pDevice->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, V, sizeof(CUSTOM_D3DVERTEX));

	UINT LineCount = (UINT)ceil(Ratio * 5) + 1;
	float w = MaxWidth / 5.0f;

	V[0] = { x,						y,	0.0f, 1.0f, BLACK };
	V[1] = { x + MaxWidth * Ratio,	y,	0.0f, 1.0f, BLACK };
	
	V[2] = { x,						y + Height, 0.0f, 1.0f, BLACK };
	V[3] = { x + MaxWidth * Ratio,	y + Height, 0.0f, 1.0f, BLACK };

	for (UINT i = 0; i != LineCount - 1; ++i)
	{
		V[2 * i + 4] = { x + w * i, y,			0.0f, 1.0f, BLACK };
		V[2 * i + 5] = { x + w * i, y + Height, 0.0f, 1.0f, BLACK };
	}

	float Offset = 0.0f;
	if ((LineCount - 1) * w > MaxWidth * Ratio)
	{
		Offset = (LineCount - 1) * w - MaxWidth * Ratio;
	}

	V[(LineCount - 1) * 2 + 4] = { x + w * (LineCount - 1) - Offset, y,				0.0f, 1.0f, BLACK };
	V[(LineCount - 1) * 2 + 5] = { x + w * (LineCount - 1) - Offset, y + Height,	0.0f, 1.0f, BLACK };

	pDevice->DrawPrimitiveUP(D3DPT_LINELIST, LineCount + 3, V, sizeof(CUSTOM_D3DVERTEX));
}
Still not super efficient but it does a decent job I guess.

A version of your healthbar style (as predicted - 2 draw calls):

C++:
void Healthbar(IDirect3DDevice9 * pDevice, float x, float y, float Health, float Height, float MaxWidth)
{
	if (Health <= 0.0f || Health > 100.0f)
		return;
	
	if (Height < 3.0f)
		Height = 3.0f;

	if (MaxWidth < 41.0f)
		MaxWidth = 41.0f;

	DWORD Col[] = { RED, ORANGE, YELLOW, LIME, BLUE };

	CUSTOM_D3DVERTEX V[46];
	float w_unit	= MaxWidth / 5.0f;
	float w_unit_s	= MaxWidth / 20.0f;

	UINT ColCount = (UINT)ceil(Health / 20.0f);
	UINT UnitCount = (UINT)ceil(Health / 5.0f);
	for (UINT i = 0; i != ColCount - 1; ++i)
	{
		V[6 * i + 0] = { x + w_unit * i,		y,			0.0f, 1.0f, Col[i] };
		V[6 * i + 1] = { x + w_unit * (i + 1),	y,			0.0f, 1.0f, Col[i] };
		V[6 * i + 2] = { x + w_unit * i,		y + Height, 0.0f, 1.0f, Col[i] };

		V[6 * i + 3] = { x + w_unit * i,		y + Height, 0.0f, 1.0f, Col[i] };
		V[6 * i + 4] = { x + w_unit * (i + 1),	y,			0.0f, 1.0f, Col[i] };
		V[6 * i + 5] = { x + w_unit * (i + 1),	y + Height, 0.0f, 1.0f, Col[i] };
	}
	
	float Offset = (ColCount * 4 - UnitCount) * w_unit_s;

	V[(ColCount - 1) * 6 + 0] = { x + w_unit * (ColCount - 1),		y,			0.0f, 1.0f, Col[ColCount - 1] };
	V[(ColCount - 1) * 6 + 1] = { x + w_unit * ColCount - Offset,	y,			0.0f, 1.0f, Col[ColCount - 1] };
	V[(ColCount - 1) * 6 + 2] = { x + w_unit * (ColCount - 1),		y + Height, 0.0f, 1.0f, Col[ColCount - 1] };

	V[(ColCount - 1) * 6 + 3] = { x + w_unit * (ColCount - 1),		y + Height, 0.0f, 1.0f, Col[ColCount - 1] };
	V[(ColCount - 1) * 6 + 4] = { x + w_unit * ColCount - Offset,	y,			0.0f, 1.0f, Col[ColCount - 1] };
	V[(ColCount - 1) * 6 + 5] = { x + w_unit * ColCount - Offset,	y + Height, 0.0f, 1.0f, Col[ColCount - 1] };

	pDevice->DrawPrimitiveUP(D3DPT_TRIANGLELIST, ColCount * 2, V, sizeof(CUSTOM_D3DVERTEX));

	V[0] = { x,									y,			0.0f, 1.0f, BLACK };
	V[1] = { x + w_unit * ColCount - Offset,	y,			0.0f, 1.0f, BLACK };
	V[2] = { x,									y + Height - 1, 0.0f, 1.0f, BLACK };
	V[3] = { x + w_unit * ColCount - Offset,	y + Height - 1, 0.0f, 1.0f, BLACK };

	UINT LinCount = UnitCount + 3;
	for (UINT i = 2; i != LinCount - 1; ++i)
	{
		V[i * 2 + 0] = { x + w_unit_s * (i - 2), y,				0.0f, 1.0f, BLACK };
		V[i * 2 + 1] = { x + w_unit_s * (i - 2), y + Height,	0.0f, 1.0f, BLACK };
	}

	V[2 * (LinCount - 1) + 0] = { x + w_unit * ColCount - Offset, y,			0.0f, 1.0f, BLACK };
	V[2 * (LinCount - 1) + 1] = { x + w_unit * ColCount - Offset, y + Height,	0.0f, 1.0f, BLACK };

	pDevice->DrawPrimitiveUP(D3DPT_LINELIST, LinCount, V, sizeof(CUSTOM_D3DVERTEX));
}
 
Last edited:

Oneshot

Meme Tier VIP
Apr 4, 2015
232
190
13
4loops:trollface:


C++:
    if (Health>0)
	 {
        
         DrawRect(pDevice, x - 1, y - 1, 102, 7, D3DCOLOR_ARGB(255, 20, 20, 20));

    	if (Health < 101 && Health > 1)
         {
                DrawRect(pDevice, x, y, 10, 5, D3DCOLOR_ARGB(255, 255, 0, 0));
         }
			
        for(size_t i = 0; i < 9; i++) // l33t Codenz right here!!
         {
              if (Health < 101 && Health > (i * 10))
	           {
                    DrawRect(pDevice, x + (i * 10), y, 10, 5, D3DCOLOR_ARGB(255, 255, 0, 0));
               }
         }
         for(size_t i = 0; i < 19; i++) // and here!! 
         {
             DrawRect(pDevice, x + (i * 5), y, 1, 5, D3DCOLOR_ARGB(255, 81, 75, 61));
         }

     }
 
Last edited:

Broihon

edgy 12 y/o
Escobar Tier VIP
Fleep Tier Donator
Dec 22, 2013
1,745
40,528
316
Well, those are still 27 draw calls...
 

Lovelace

Jr.Coder
Full Member
Nobleman
Oct 23, 2015
83
938
5
omg this is so much cancer. Broihon is right, use vertex rendering. This is Fleep's fault for that c&p DrawRect function. Now everyone's doing it. :FeelsBadMan:
 

steb

Jr.Coder
Full Member
Nobleman
Aug 16, 2016
63
383
1
This is what I came up with:
C++:
void Healthbar(IDirect3DDevice9 * pDevice, float x, float y, float Health, float Height, float MaxWidth)
{
	if (Health <= 0.0f || Health > 100.0f)
		return;

	float Ratio = Health / 100.0f;
	UINT ColourHi = 0xFF000000 | ((DWORD)(0xFF * Ratio) << 8) | ((0xFF - (DWORD)(0xFF * Ratio)) << 16);
	UINT ColourLo = 0xFFFF0000;
	
	CUSTOM_D3DVERTEX V[16] =
	{
		{ x,					y + Height, 0.0f, 1.0f, ColourLo },
		{ x,					y,			0.0f, 1.0f, ColourLo },
		{ x + MaxWidth * Ratio,	y + Height, 0.0f, 1.0f, ColourHi },
		{ x + MaxWidth * Ratio,	y,			0.0f, 1.0f, ColourHi }
	};
	pDevice->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, V, sizeof(CUSTOM_D3DVERTEX));

	UINT LineCount = (UINT)ceil(Ratio * 5) + 1;
	float w = MaxWidth / 5.0f;

	V[0] = { x,						y,	0.0f, 1.0f, BLACK };
	V[1] = { x + MaxWidth * Ratio,	y,	0.0f, 1.0f, BLACK };
	
	V[2] = { x,						y + Height, 0.0f, 1.0f, BLACK };
	V[3] = { x + MaxWidth * Ratio,	y + Height, 0.0f, 1.0f, BLACK };

	for (UINT i = 0; i != LineCount - 1; ++i)
	{
		V[2 * i + 4] = { x + w * i, y,			0.0f, 1.0f, BLACK };
		V[2 * i + 5] = { x + w * i, y + Height, 0.0f, 1.0f, BLACK };
	}

	float Offset = 0.0f;
	if ((LineCount - 1) * w > MaxWidth * Ratio)
	{
		Offset = (LineCount - 1) * w - MaxWidth * Ratio;
	}

	V[(LineCount - 1) * 2 + 4] = { x + w * (LineCount - 1) - Offset, y,				0.0f, 1.0f, BLACK };
	V[(LineCount - 1) * 2 + 5] = { x + w * (LineCount - 1) - Offset, y + Height,	0.0f, 1.0f, BLACK };

	pDevice->DrawPrimitiveUP(D3DPT_LINELIST, LineCount + 3, V, sizeof(CUSTOM_D3DVERTEX));
}
Still not super efficient but it does a decent job I guess.

A version of your healthbar style (as predicted - 2 draw calls):

C++:
void Healthbar(IDirect3DDevice9 * pDevice, float x, float y, float Health, float Height, float MaxWidth)
{
	if (Health <= 0.0f || Health > 100.0f)
		return;
	
	if (Height < 3.0f)
		Height = 3.0f;

	if (MaxWidth < 41.0f)
		MaxWidth = 41.0f;

	DWORD Col[] = { RED, ORANGE, YELLOW, LIME, BLUE };

	CUSTOM_D3DVERTEX V[46];
	float w_unit	= MaxWidth / 5.0f;
	float w_unit_s	= MaxWidth / 20.0f;

	UINT ColCount = (UINT)ceil(Health / 20.0f);
	UINT UnitCount = (UINT)ceil(Health / 5.0f);
	for (UINT i = 0; i != ColCount - 1; ++i)
	{
		V[6 * i + 0] = { x + w_unit * i,		y,			0.0f, 1.0f, Col[i] };
		V[6 * i + 1] = { x + w_unit * (i + 1),	y,			0.0f, 1.0f, Col[i] };
		V[6 * i + 2] = { x + w_unit * i,		y + Height, 0.0f, 1.0f, Col[i] };

		V[6 * i + 3] = { x + w_unit * i,		y + Height, 0.0f, 1.0f, Col[i] };
		V[6 * i + 4] = { x + w_unit * (i + 1),	y,			0.0f, 1.0f, Col[i] };
		V[6 * i + 5] = { x + w_unit * (i + 1),	y + Height, 0.0f, 1.0f, Col[i] };
	}
	
	float Offset = (ColCount * 4 - UnitCount) * w_unit_s;

	V[(ColCount - 1) * 6 + 0] = { x + w_unit * (ColCount - 1),		y,			0.0f, 1.0f, Col[ColCount - 1] };
	V[(ColCount - 1) * 6 + 1] = { x + w_unit * ColCount - Offset,	y,			0.0f, 1.0f, Col[ColCount - 1] };
	V[(ColCount - 1) * 6 + 2] = { x + w_unit * (ColCount - 1),		y + Height, 0.0f, 1.0f, Col[ColCount - 1] };

	V[(ColCount - 1) * 6 + 3] = { x + w_unit * (ColCount - 1),		y + Height, 0.0f, 1.0f, Col[ColCount - 1] };
	V[(ColCount - 1) * 6 + 4] = { x + w_unit * ColCount - Offset,	y,			0.0f, 1.0f, Col[ColCount - 1] };
	V[(ColCount - 1) * 6 + 5] = { x + w_unit * ColCount - Offset,	y + Height, 0.0f, 1.0f, Col[ColCount - 1] };

	pDevice->DrawPrimitiveUP(D3DPT_TRIANGLELIST, ColCount * 2, V, sizeof(CUSTOM_D3DVERTEX));

	V[0] = { x,									y,			0.0f, 1.0f, BLACK };
	V[1] = { x + w_unit * ColCount - Offset,	y,			0.0f, 1.0f, BLACK };
	V[2] = { x,									y + Height - 1, 0.0f, 1.0f, BLACK };
	V[3] = { x + w_unit * ColCount - Offset,	y + Height - 1, 0.0f, 1.0f, BLACK };

	UINT LinCount = UnitCount + 3;
	for (UINT i = 2; i != LinCount - 1; ++i)
	{
		V[i * 2 + 0] = { x + w_unit_s * (i - 2), y,				0.0f, 1.0f, BLACK };
		V[i * 2 + 1] = { x + w_unit_s * (i - 2), y + Height,	0.0f, 1.0f, BLACK };
	}

	V[2 * (LinCount - 1) + 0] = { x + w_unit * ColCount - Offset, y,			0.0f, 1.0f, BLACK };
	V[2 * (LinCount - 1) + 1] = { x + w_unit * ColCount - Offset, y + Height,	0.0f, 1.0f, BLACK };

	pDevice->DrawPrimitiveUP(D3DPT_LINELIST, LinCount, V, sizeof(CUSTOM_D3DVERTEX));
}
damnn thats sexy
 

Broihon

edgy 12 y/o
Escobar Tier VIP
Fleep Tier Donator
Dec 22, 2013
1,745
40,528
316
Although this is MUCH better, you technically did the opposite of what the op asked for. He asked us to make it smaller. :kappa:
Actually that's not the case. I counted lines and including comments and empty lines his code is 59 lines just like my version
 
Attention! Before you post:

Read the How to Ask Questions Guide
99% of questions are answered in the Beginner's Guide, do it before asking a question.

No Hack Requests. Post in the correct section.  Search the forum first. Read the rules.

How to make a good post:

  • Fill out the form correctly
  • Tell us the game name & coding language
  • Post everything we need to know to help you
  • Ask specific questions, be descriptive
  • Post errors, line numbers & screenshots
  • Post code snippets using code tags
  • If it's a large project, zip it up and attach it

If you do not comply, your post may be deleted.  We want to help, please make a good post and we will do our best to help you.

Similar threads

Community Mods