Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Health Bars..  (Read 5521 times)

0 Members and 1 Guest are viewing this topic.

Roose Bolton of the Dreadfort

  • Full Member
  • ***
  • Posts: 113
  • Full-time Procrastinator, Part-time programmer.
    • View Profile
    • Personal Portfolio/Website/Blog
Health Bars..
« 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
« Last Edit: June 01, 2012, 07:31:38 am by 1337matty »
Trying so very hard to finish at-least one project.

Watch out for the RAII police, they will get you.

www.bantersaurus-games.com

mateandmetal

  • Full Member
  • ***
  • Posts: 171
  • The bird is the word
    • View Profile
    • my blog
Re: Health Bars..
« Reply #1 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
« Last Edit: June 01, 2012, 07:33:12 am by mateandmetal »
- Mate (beverage) addict
- Heavy metal addict _lml
- SFML 2 addict
- My first (and free) game: BichingISH!

Roose Bolton of the Dreadfort

  • Full Member
  • ***
  • Posts: 113
  • Full-time Procrastinator, Part-time programmer.
    • View Profile
    • Personal Portfolio/Website/Blog
Re: Health Bars..
« Reply #2 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)
Trying so very hard to finish at-least one project.

Watch out for the RAII police, they will get you.

www.bantersaurus-games.com

mateandmetal

  • Full Member
  • ***
  • Posts: 171
  • The bird is the word
    • View Profile
    • my blog
Re: Health Bars..
« Reply #3 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);
 
- Mate (beverage) addict
- Heavy metal addict _lml
- SFML 2 addict
- My first (and free) game: BichingISH!

Roose Bolton of the Dreadfort

  • Full Member
  • ***
  • Posts: 113
  • Full-time Procrastinator, Part-time programmer.
    • View Profile
    • Personal Portfolio/Website/Blog
Re: Health Bars..
« Reply #4 on: June 01, 2012, 08:02:13 am »
I appear to get more FPS using textures :/
Trying so very hard to finish at-least one project.

Watch out for the RAII police, they will get you.

www.bantersaurus-games.com

thePyro_13

  • Full Member
  • ***
  • Posts: 156
    • View Profile
Re: Health Bars..
« Reply #5 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?

 

anything