SFML community forums

Help => Graphics => Topic started by: David on May 22, 2011, 11:41:45 pm

Title: Health bar?
Post by: David on May 22, 2011, 11:41:45 pm
How would you display a simple health bar in SFML?
Title: Health bar?
Post by: Groogy on May 22, 2011, 11:56:48 pm
With a Sprite or Shape?

SFML is not a game engine so there is no direct magically working way to do it. You'll have to implement it yourself.

Anyway I would guess you either use a Sprite or Shape. You could use the SubRect on a Sprite to say "I only want to show this part of the image".
Title: Mmk
Post by: David on May 23, 2011, 01:00:39 am
I know that SFML is not a game engine (I came from actionscript)

But is there a way to do the same with a torus/doughnut-shaped health bar?
Title: Health bar?
Post by: Spidyy on May 23, 2011, 01:09:44 am
For a Torus shape, you'll have to create your own Torus class, inheriting sf::Drawable.
Title: Health bar?
Post by: Nexus on May 23, 2011, 01:52:03 am
Or you could build it from concentric circles, if the inner circle needn't be transparent.

My library also contains a class for concave shapes, maybe it can help you (especially the Pie() function, which creates a sector of a circle).
Title: Health bar?
Post by: Jove on May 23, 2011, 09:23:00 am
My health bar uses a sprite and image showing what the full health will be.

I use SetScale to shrink the sprite on the X plane from 1 (full size) down to 0. Using a variable that ranges from 0 to 100 as health remaining, it's easy to convert that into SetScale.

Code: [Select]
Energy_Bar_Spr.SetScale(ship_energy / 100, 1);
Title: Health bar?
Post by: AdventWolf on May 25, 2011, 08:17:58 pm
I have used
Code: [Select]

HealthBar.SetSubRect(sf::IntRect(0,0,int(currentHP/maxHP*229),14));


229 being the width, and 14 is the height of your sprite.