Page 1 of 2

(C++) HealthBarSceneNode ( health bar above player)

Posted: Fri Jun 30, 2006 12:00 am
by drac_gd
/*******************************************************************
* Copyright (C) 2006 Brett R. Jones issued to public domain
*********************************************************************/
#ifndef VX_HEALTH_SCENE_NODE_H
#define VX_HEALTH_SCENE_NODE_H

#include "ISceneNode.h"
#include "ISceneCollisionManager.h"

namespace irr
{
namespace scene
{

class VxHealthSceneNode : public ISceneNode
{
public:

//! constructor
VxHealthSceneNode( ISceneNode* parent,
ISceneManager* mgr,
s32 id,
scene::ISceneCollisionManager* coll,
s32 width = 100,
s32 height = 10,
const core::vector3df& position = core::vector3df(0,0,0),
video::SColor BarColor=video::SColor(150,0,200,0),
video::SColor BkgColor=video::SColor(150,200,0,0),
video::SColor BoarderColor=video::SColor(200,220,220,220) );

//! destructor
virtual ~VxHealthSceneNode();

virtual void OnPreRender();

//! renders the node.
virtual void render();

//! returns the axis aligned bounding box of this node
virtual const core::aabbox3d<f32>& getBoundingBox() const;

//! returns amount of materials used by this scene node.
virtual s32 getMaterialCount();

//! sets progress percentage ( or health )
virtual void setProgress( s32 s32Percent );

//! sets the color of the progress bar
virtual void setProgressColor(video::SColor color);

//! sets the color of the progress bar background
virtual void setBackgroundColor(video::SColor color);

//! sets the color of the progress bar border
virtual void setBorderColor(video::SColor color);


private:

core::aabbox3d< f32 > Box;
video::SColor BarColor;
video::SColor BorderColor;
video::SColor BkgColor;
scene::ISceneCollisionManager* Coll;
core::dimension2d<s32> m_gDim; //dimension width, height
s32 m_bBorder;
s32 MaxNum;
s32 isVisible;
s32 m_s32Percent;
};

} // end namespace scene
} // end namespace irr

#endif // VX_HEALTH_SCENE_NODE_H

/*******************************************************************
* Copyright (C) 2006 Brett R. Jones issued to public domain
*********************************************************************/
#include "irrlicht.h"
#include "VxHealthSceneNode.h"
#include "ISceneManager.h"
#include "IVideoDriver.h"


namespace irr
{
namespace scene
{

//! constructor
VxHealthSceneNode::VxHealthSceneNode( ISceneNode* parent,
ISceneManager* mgr,
s32 id,
scene::ISceneCollisionManager* coll,
s32 width,
s32 height,
const core::vector3df& position,
video::SColor BarColorIn,
video::SColor BkgColorIn,
video::SColor BorderColorIn )
: ISceneNode(parent, mgr, id, position),
Coll(coll),
BarColor(BarColorIn),
BkgColor(BkgColorIn),
BorderColor(BorderColorIn),
m_gDim( width, height ),
m_bBorder(1),
isVisible(1),
m_s32Percent(100)
{
#ifdef _DEBUG
setDebugName("VxHealthSceneNode");
#endif

setAutomaticCulling(false);
//////////////////////////////////////////////////////////
//Set Bounding box
f32 halfWidth = (f32)m_gDim.Width/2;
f32 halfHeight = (f32)m_gDim.Height/2;
Box.MinEdge.set(-halfWidth,-halfHeight,-1.0);
Box.MaxEdge.set(halfWidth,halfHeight,1.0);
}

//! destructor
VxHealthSceneNode::~VxHealthSceneNode()
{
}

void VxHealthSceneNode::OnPreRender()
{
if (IsVisible)
SceneManager->registerNodeForRendering(this, ESNRP_SHADOW);

ISceneNode::OnPreRender();
}

//! renders the node.
void VxHealthSceneNode::render()
{
if( !Coll )
return;
if(!isVisible)
{
return;
}

/////////////////////////////////////////////////////////////////////
video::IVideoDriver* driver = SceneManager->getVideoDriver();
ICameraSceneNode* camera = SceneManager->getActiveCamera();

if (!camera || !driver)
return;

if (DebugDataVisible)
{
driver->setTransform(video::ETS_WORLD, AbsoluteTransformation);
video::SMaterial m;
m.Lighting = false;
driver->setMaterial(m);
driver->draw3DBox(Box, video::SColor(0,208,195,152));
}
//////////////////////////////////////////////////////////////////////

core::position2d<s32> pos = Coll->getScreenCoordinatesFrom3DPosition(getAbsolutePosition(),
SceneManager->getActiveCamera());

// calculate health bar rectangle
f32 halfWidth = (f32)m_gDim.Width/2;
core::rect<s32> AbsoluteRect( pos, m_gDim );
// center the bar
AbsoluteRect.UpperLeftCorner.X -= halfWidth;
AbsoluteRect.LowerRightCorner.X -= halfWidth;

core::rect<s32> BarRect = AbsoluteRect;

// draw boarder if needed
if( m_bBorder )
{
driver->draw2DRectangle( BorderColor, AbsoluteRect, &AbsoluteRect);
// shrink by one for bar
BarRect.UpperLeftCorner.X += 1;
BarRect.UpperLeftCorner.Y += 1;
BarRect.LowerRightCorner.X -= 1;
BarRect.LowerRightCorner.Y -= 1;
}
// calculate progress bar
MaxNum = (BarRect.LowerRightCorner.X - BarRect.UpperLeftCorner.X) - 1;
s32 PercentNum = (s32)((m_s32Percent * MaxNum) / 100);

// draw progress part
core::rect<s32> LoadRect = BarRect;
LoadRect.LowerRightCorner.X = BarRect.UpperLeftCorner.X + PercentNum;
driver->draw2DRectangle( BarColor, LoadRect, &LoadRect );

// draw empty part
LoadRect.UpperLeftCorner.X = BarRect.UpperLeftCorner.X + PercentNum;
LoadRect.LowerRightCorner.X = BarRect.LowerRightCorner.X;
driver->draw2DRectangle( BkgColor, LoadRect, &LoadRect );
}


//! returns the axis aligned bounding box of this node
const core::aabbox3d<f32>& VxHealthSceneNode::getBoundingBox() const
{
return Box;
}

//! returns amount of materials used by this scene node.
s32 VxHealthSceneNode::getMaterialCount()
{
return 0;
}

//! sets the progress percentage ( or health )
void VxHealthSceneNode::setProgress( s32 s32Percent )
{
m_s32Percent = s32Percent;
if( m_s32Percent < 0 )
{
m_s32Percent = 0;
}
if( m_s32Percent > 100 )
{
m_s32Percent = 100;
}

}

//! sets the color of the progress
void VxHealthSceneNode::setProgressColor(video::SColor color)
{
BarColor = color;
}

//! sets the color of the progress bar background
void VxHealthSceneNode::setBackgroundColor(video::SColor color)
{
BkgColor = color;
}

//! sets the color of the progress bar border
void VxHealthSceneNode::setBorderColor(video::SColor color)
{
BorderColor = color;
}


} // end namespace scene
} // end namespace irr

//------------------------------------------------------------------------
/// use somthing like this
pgMgr->m_pgAlphaPlayer->m_pgHealthBar = new scene::VxHealthSceneNode(
pgMgr->m_pgAlphaPlayer->m_pgNode, // parent node
pgMgr->m_pgSceneMgr, // scene manager
-1, // id
pgMgr->m_pgSceneMgr->getSceneCollisionManager(), // collision manager
50, // width
6, // height
core::vector3df(0,40,0), // position
video::SColor(150,0,200,0), // bar color
video::SColor(150,220,0,0), // background color
video::SColor(255,255,255,255) ); // boarder color
//set percent of health
pgMgr->m_pgAlphaPlayer->m_pgHealthBar->setProgress( 60 );

Posted: Fri Jun 30, 2006 12:30 am
by jam

Code: Select all

#include "appall.h"
Is this a required header file? If so where do we obtain it?

Otherwise a neat code snippet 8)

Posted: Fri Jun 30, 2006 2:17 am
by drac_gd
no.. replace #define "appall.h" and #define "stdafx.h" with #define "irrlicht.h"

Posted: Fri Jun 30, 2006 12:42 pm
by stodge
Use the source (code?) tag to retain formatting.

Posted: Sun Jul 30, 2006 3:44 am
by monkeycracks
I get an warning compiling with Dev-Cpp

irr::s32 to irr::f32
twice...

AbsoluteRect.UpperLeftCorner.X -= halfWidth;
AbsoluteRect.LowerRightCorner.X -= halfWidth;

its having problems there.

any suggestions?

Posted: Fri Aug 04, 2006 7:23 pm
by Mikenoworth

Code: Select all

AbsoluteRect.UpperLeftCorner.X -= (s32) halfWidth; 
AbsoluteRect.LowerRightCorner.X -= (s32) halfWidth; 

Posted: Fri Aug 11, 2006 6:24 pm
by monkeycracks
Yet another problem for me,

Code: Select all

	     pgMgr->m_pgAlphaPlayer->m_pgHealthBar = new scene::VxHealthSceneNode( 
         pgMgr->m_pgAlphaPlayer->m_pgNode, // parent node 
         pgMgr->m_pgSceneMgr, // scene manager 
         -1, // id 
         pgMgr->m_pgSceneMgr->getSceneCollisionManager(), // collision manager 
         50, // width 
         6, // height 
         core::vector3df(0,40,0), // position 
         video::SColor(150,0,200,0), // bar color 
         video::SColor(150,220,0,0), // background color 
         video::SColor(255,255,255,255) ); // boarder color 
         //set percent of health 
         pgMgr->m_pgAlphaPlayer->m_pgHealthBar->setProgress( 60 );
pgMgr undefined and I've got no clue where to find it.

Posted: Fri Aug 11, 2006 7:34 pm
by hybrid
Do you know what alphaplayer and sceneMgr are? Where could those be located? Maybe in a game. so we probably have a game manager here.
Try to understand the code instead of just copying. You should also avoid to copy the code fragment somewhere into our app as the necessary objects have to exist.
Oh, and remove your crosspost before someone posts and you cannot remove it anymore.

Posted: Fri Aug 11, 2006 7:41 pm
by monkeycracks
hybrid wrote:Do you know what alphaplayer and sceneMgr are? Where could those be located? Maybe in a game. so we probably have a game manager here.
Try to understand the code instead of just copying. You should also avoid to copy the code fragment somewhere into our app as the necessary objects have to exist.
Oh, and remove your crosspost before someone posts and you cannot remove it anymore.
Post removed ;)
I tried looking over it to understand what pgMgr could be but I'm not grasping anything, could be the tiredness but if you could give me some more help that'd be great.

Posted: Fri Aug 11, 2006 7:59 pm
by hybrid
It is completely useless for you unless you also put all your meshes and the scene manager into one class. Just remove it and apply the code fragment directly on instantiated meshes.

Posted: Mon Aug 14, 2006 2:48 pm
by Mancuso Raffaele
i have a prob with this code. if the bar is set to 100%, there is a small red rectangle, the bar is not all green (I use default colours)

Posted: Fri Aug 25, 2006 1:06 am
by zenaku
Copyright (C) 2006 Brett R. Jones issued to public domain
I'm not a lawyer but I know you cannot release something into the public domain and retain copyright. They are contradictory terms. For something to be in the public domain means it has no copyrights. Do you mean it was copyright 2006, now it's public domain? It's hard to tell.

Posted: Fri Aug 25, 2006 7:52 am
by Mikenoworth
That's why alot of projects use gnu licenses, public domain can seperate the author from his works, while the license implements a type of legal glue that makes sure you are always credited for your original works.

edit: Sorry for continuing an off-topic reply. :oops:

Posted: Fri Aug 25, 2006 12:41 pm
by drac_gd
Your right.. basically the copywrite should be removed because public domain means you can do whatever you want with it. I am no lawyer I just wanted to make sure that everone knows it is public domain. having the copywrite notice on the code does however document when it was issued in case someone tries to do a patent later then the code can be shown as prior works and the patent is invalid. I issue to public domain because I am not interested in recognition.. just helping to advance the project and if I did come up with something good then there would be no question about whether it could be modified and added to Irrlicht.

Posted: Fri Aug 25, 2006 2:25 pm
by hybrid
Release it under zlib license. Then you hold the copyright, but everyone can do whatever he wants.