creating HUD destroys performance

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
GundamO
Posts: 9
Joined: Thu Nov 11, 2010 11:34 am

creating HUD destroys performance

Post by GundamO »

okk, so the last thing i need to create for my game is my Heads Up Display to show health and collectables. at the moment i have created a class which will draw heart images depending on how much health you have, the only problem i have is that it drops my fps from about 50 - 80 down to 8 - 20. my question is, is there any way to add my HUD without the drop in fps?

Code: Select all

#include "HUD.h"
#include "Engine.h"


HUD::HUD(video::IVideoDriver *driver,gui::IGUIEnvironment *guienv,Character* turtle1)
{
	turtle=turtle1;

	float width= driver->getViewPort().getWidth();
	float height = driver->getViewPort().getHeight();
	heart = driver->getTexture("media/heart.png");
	heart2 = driver->getTexture("media/heart2.png");

	guienv->addImage(driver->getTexture("media/irrlichtLogo2.png"),position2d<int>(width - 130 ,height - 130));
	//guienv->addImage(driver->getTexture("media/hud.png"),position2d<int>(10,10)); 

	guienv->addImage(heart,position2d<int>(55,10));
	guienv->addImage(heart,position2d<int>(110,10));
	guienv->addImage(heart,position2d<int>(165,10));
	guienv->addImage(heart,position2d<int>(220,10));
	guienv->addImage(heart,position2d<int>(275,10));
}
void HUD::Update(float frameDeltaTime,video::IVideoDriver *driver,gui::IGUIEnvironment *guienv)
{
	if(turtle->Health <= 80)
		guienv->addImage(heart2,position2d<int>(55,10));
	if(turtle->Health <= 60)
		guienv->addImage(heart2,position2d<int>(110,10));
	if(turtle->Health <= 40)
		guienv->addImage(heart2,position2d<int>(165,10));
	if(turtle->Health <= 20)
		guienv->addImage(heart2,position2d<int>(220,10));
	if(turtle->Health <= 0)
		guienv->addImage(heart2,position2d<int>(275,10));
}

HUD::~HUD(void)
{
}
to call it

Code: Select all

HUD* hud = new HUD(driver,guienv,turtle);  
finally, within the device->run()

Code: Select all

float Lasthealth = -1.f;
			float health = turtle->Health;

			if(Lasthealth != health){
				hud->Update(frameDeltaTime,driver,guienv);
				Lasthealth = health;
			}
Thanks for any help
serengeor
Posts: 1712
Joined: Tue Jan 13, 2009 7:34 pm
Location: Lithuania

Post by serengeor »

Gui really drops fps if you add anything, but it shouldn't drop that much.
I think you are having problems because you are adding images to gui every time you health changes(and in constructor).

Instead you should hold pointers to your Images and make them visible/invisible.
Working on game: Marrbles (Currently stopped).
GundamO
Posts: 9
Joined: Thu Nov 11, 2010 11:34 am

Post by GundamO »

Thanks alot, it worked perfectly, now it only drops to about 40 - 60 fps :)

Code: Select all

#include "HUD.h"
#include "Engine.h"


HUD::HUD(video::IVideoDriver *driver,gui::IGUIEnvironment *guienv,Character* turtle1)
{
	turtle=turtle1;

	float width= driver->getViewPort().getWidth();
	float height = driver->getViewPort().getHeight();
	heart = driver->getTexture("media/heart.png");

	guienv->addImage(driver->getTexture("media/irrlichtLogo2.png"),position2d<int>(width - 130 ,height - 130));
	//guienv->addImage(driver->getTexture("media/hud.png"),position2d<int>(10,10)); 

	heart1 = guienv->addImage(heart,position2d<int>(55,10));
	heart2 = guienv->addImage(heart,position2d<int>(110,10));
	heart3 = guienv->addImage(heart,position2d<int>(165,10));
	heart4 = guienv->addImage(heart,position2d<int>(220,10));
	heart5 = guienv->addImage(heart,position2d<int>(275,10));
}
void HUD::Update(float frameDeltaTime,video::IVideoDriver *driver,gui::IGUIEnvironment *guienv)
{
	if(turtle->Health <= 80)
		heart5->setVisible(false);
	if(turtle->Health <= 60)
		heart4->setVisible(false);
	if(turtle->Health <= 40)
		heart3->setVisible(false);
	if(turtle->Health <= 20)
		heart2->setVisible(false);
	if(turtle->Health <= 0)
		heart1->setVisible(false);
}

HUD::~HUD(void)
{
}
[/quote]
Post Reply