SFML community forums
Help => Graphics => Topic started by: David on May 22, 2011, 11:41:45 pm
-
How would you display a simple health bar in SFML?
-
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".
-
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?
-
For a Torus shape, you'll have to create your own Torus class, inheriting sf::Drawable.
-
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).
-
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.
Energy_Bar_Spr.SetScale(ship_energy / 100, 1);
-
I have used
HealthBar.SetSubRect(sf::IntRect(0,0,int(currentHP/maxHP*229),14));
229 being the width, and 14 is the height of your sprite.