SFML community forums

Help => General => Topic started by: Roose Bolton of the Dreadfort on June 01, 2012, 06:36:22 am

Title: Health Bars..
Post by: Roose Bolton of the Dreadfort on June 01, 2012, 06:36:22 am
Hello SFML'ers!

I just wrote a solution for health bars in my game but not sure if its the best way/correct way to do it?

if it is, then maybe it could be useful for some people!

HealthBar.H

Code: [Select]
#pragma once

#include <SFML/Graphics.hpp>

class HealthBar
{
public:
HealthBar(void);
~HealthBar(void);

void Draw(sf::Vector2f iPosition, float currentHealth, float maxHealth);

sf::Sprite mCurrentHealth;
sf::Sprite mHealthBackground;
};

HealthBar.cpp

Code: [Select]
#include "HealthBar.h"
#include "GraphicsManager.h"

HealthBar::HealthBar(void)
{
//load from Graphics
this->mCurrentHealth.setTexture( *Graphics->GetTextureList()[2] );
this->mHealthBackground.setTexture( *Graphics->GetTextureList()[3] );

//Set initial sizes
this->mHealthBackground.setTextureRect( sf::Rect<int>(0, 0, 50, 5) );
this->mCurrentHealth.setTextureRect( sf::Rect<int>(0, 0, 50, 5) );
}


HealthBar::~HealthBar(void)
{
//TODO
}

void HealthBar::Draw(sf::Vector2f iPosition, float currentHealth, float maxHealth)
{
this->mCurrentHealth.setTextureRect( sf::Rect<int>(0, 0, currentHealth / 2, 5));
this->mHealthBackground.setTextureRect( sf::Rect<int>(0, 0, 50, 5) );

this->mCurrentHealth.setPosition(iPosition);
this->mHealthBackground.setPosition(iPosition);

//Draw background first!
Graphics->GetWindow().draw(mHealthBackground);
Graphics->GetWindow().draw(mCurrentHealth);
}

then in my GameEntities I instantiate a healthbar for them each.

each time the GameEntities draw function is called I then also call its healthbar to draw

Code: [Select]
bar.Draw(this->GetPosition() + sf::Vector2f(-22, -24), health, 100);

resulting in a nice health bar :D
Title: Re: Health Bars..
Post by: mateandmetal on June 01, 2012, 07:29:20 am
You are not using the float maxHealth in your Draw method?

sf::Rect<int> can be replaced with sf::IntRect
Title: Re: Health Bars..
Post by: Roose Bolton of the Dreadfort on June 01, 2012, 07:32:21 am
Not at the moment no :/ I will in the future  :P

is this the correct way 'as such' to do it? I get fair frame losses though when applying these healthbars to all my entities (50)
Title: Re: Health Bars..
Post by: mateandmetal on June 01, 2012, 07:46:12 am
Do you really need textures for your health bars?
I´m just drawing a simple sf::RectangleShape

If the entity has "low" health, I draw a red bar
cuad.setFillColor (sf::Color::Red);
 

else, I draw a green bar
cuad.setFillColor (sf::Color::Green);
 
Title: Re: Health Bars..
Post by: Roose Bolton of the Dreadfort on June 01, 2012, 08:02:13 am
I appear to get more FPS using textures :/
Title: Re: Health Bars..
Post by: thePyro_13 on June 01, 2012, 08:06:21 am
You could try batching the health bars, draw them all at the same time so that opengl can reuse the stored texture.

Switching back to the health bar texture between every entity draw could be slowing it down a bit?