health bars?

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
Guest

health bars?

Post by Guest »

how would you make a health bar with this?
keless
Posts: 805
Joined: Mon Dec 15, 2003 10:37 pm
Location: Los Angeles, California, USA

Post by keless »

using draw2DImage() I'd draw a rectangular image of varying width
a screen cap is worth 0x100000 DWORDS
rt
Posts: 150
Joined: Sun Nov 30, 2003 6:54 am
Location: canada
Contact:

Re: health bars?

Post by rt »

Anonymous wrote:how would you make a health bar with this?
how about draw2DRectangle()

Code: Select all

driver->draw2DRectangle(irr::video::SColor(255,0,255,0), irr::core::rect<irr::s32>(0,0,256,64));
madinitaly
Posts: 92
Joined: Sat Nov 29, 2003 8:30 pm
Contact:

Post by madinitaly »

How about a fixed image for the frame and a rectangle of varying width for the actual bar? :wink:
DarkWhoppy
Posts: 386
Joined: Thu Sep 25, 2003 12:43 pm
Contact:

Post by DarkWhoppy »

I use a JPEG image and draw it as an ITexture, by putting a function in the main game loop to update it from left/right depending on how fast the ship is going in Galactic Racer. :D
Programmer of the Irrlicht Scene Editor:
Homepage - Forum Thread
Version 2 on its way!!
Gold_Fusion
Posts: 26
Joined: Tue Jan 20, 2004 9:25 pm
Location: England
Contact:

Post by Gold_Fusion »

How would you update the size of the health bar? Please forgive my newbishness. Here's the code I'm using in my drawing loop.

Code: Select all

driver->draw2DRectangle(SColor(255,255,0,0), rect<s32>(10,10,health+10,20));
Health is a int that increases by 5 when you press 1 and decreases by 5 when you press 2. It didn't seem to update in the game, though. Here's my event receiver code.

Code: Select all

class MyEventReceiver : public IEventReceiver
{
public:
	virtual bool OnEvent(SEvent event)
	{
		if(node != 0 && event.EventType == EET_KEY_INPUT_EVENT &&
		   !event.KeyInput.PressedDown)
		{
			switch(event.KeyInput.Key)
			{
			case KEY_KEY_1:
				{
					health -= 5;
				}
			case KEY_KEY_2:
				{
					health += 5;
				}
			}
		}
		if(camera)
		{
			return camera->OnEvent(event);
		}

		return false;
	}
};
Is this okay?
Thanks.

On an unrelated note, you can climb walls with the FPS camera if you just look up and walk into them.
rt
Posts: 150
Joined: Sun Nov 30, 2003 6:54 am
Location: canada
Contact:

Post by rt »

the code looks ok, it should work. post all of your code, must be a problem with scope or something..
Robomaniac
Posts: 602
Joined: Sat Aug 23, 2003 2:03 am
Location: Pottstown, PA
Contact:

Post by Robomaniac »

The wall climbing was fixed in v. 0.4.2
Gold_Fusion
Posts: 26
Joined: Tue Jan 20, 2004 9:25 pm
Location: England
Contact:

Post by Gold_Fusion »

The Code wrote:// Include the Irrlicht Engine header files.
#include <irrlicht.h>

// Use the Irrlicht Engine namespace and sub-namespaces.
using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;

// Link to the Irrlich Engine library.
#pragma comment(lib, "Irrlicht.lib")

// Create pointers and a variable.
ICameraSceneNode* camera = 0;
ISceneNode* node = 0;
IrrlichtDevice* device = 0;
int health = 100;

class MyEventReceiver : public IEventReceiver
{
public:
virtual bool OnEvent(SEvent event)
{
if(node != 0 && event.EventType == EET_KEY_INPUT_EVENT &&
!event.KeyInput.PressedDown)
{
switch(event.KeyInput.Key)
{
case KEY_KEY_1:
{
health -= 5;
}
case KEY_KEY_2:
{
health += 5;
}
}
}
if(camera)
{
return camera->OnEvent(event);
}

return false;
}
};

int main()
{
// Create a MyEventReceiver.
MyEventReceiver receiver;

// Create the Irrlicht Device.
device = createDevice(DT_DIRECTX8, dimension2d<s32>(640,480),
16, false, false, &receiver);
// Create pointers to IVideoDriver, ISceneManager and IGUIEnvironment.
IVideoDriver* driver = device->getVideoDriver();
ISceneManager* smgr = device->getSceneManager();
IGUIEnvironment* env = device->getGUIEnvironment();
// Create the sky box.
ISceneNode* skyboxNode = smgr->addSkyBoxSceneNode(
driver->getTexture("../irrlicht-0.4/media/irrlicht2_up.bmp"),
driver->getTexture("../irrlicht-0.4/media/irrlicht2_dn.bmp"),
driver->getTexture("../irrlicht-0.4/media/irrlicht2_lf.bmp"),
driver->getTexture("../irrlicht-0.4/media/irrlicht2_rt.bmp"),
driver->getTexture("../irrlicht-0.4/media/irrlicht2_ft.bmp"),
driver->getTexture("../irrlicht-0.4/media/irrlicht2_bk.bmp"));
// Load a Quake 3 map.
device->getFileSystem()->addZipFileArchive("../irrlicht-0.4/media/map-20kdm2.pk3");
// Display the Quake 3 map.
IAnimatedMesh* q3levelmesh = smgr->getMesh("20kdm2.bsp");
ISceneNode* q3node = 0;
if(q3levelmesh)
{
q3node = smgr->addOctTreeSceneNode(q3levelmesh->getMesh(0));
}
ITriangleSelector* selector = 0;
if(q3node)
{
// Move the level because it was not modelled around the origin.
q3node->setPosition(vector3df(-1300,-144,-1249));
selector = smgr->createOctTreeTriangleSelector(q3levelmesh->getMesh(0), q3node, 128);
q3node->setTriangleSelector(selector);
selector->drop();
}
// Create a camera.
camera = smgr->addCameraSceneNodeFPS(0,100.0f,300.0f);
camera->setPosition(vector3df(0,100,0));
// Create a CollisionResponseAnimator.
ISceneNodeAnimator* anim = smgr->createCollisionResponseAnimator(selector, camera,
vector3df(30,50,30), vector3df(0,-100,0), 100.0f, vector3df(0,50,0));
camera->addAnimator(anim);
anim->drop();
// Hide the mouse cursor.
device->getCursorControl()->setVisible(false);
// Create a billboard.
IBillboardSceneNode * bill = smgr->addBillboardSceneNode();
bill->setMaterialType(EMT_TRANSPARENT_ADD_COLOR);
bill->setMaterialTexture(0, driver->getTexture("../irrlicht-0.4/media/particle.bmp"));
bill->setMaterialFlag(EMF_LIGHTING, false);
bill->setSize(dimension2d<f32>(20.0f,20.0f));
// Create three animated faeries.
SMaterial material;
material.Texture1 = driver->getTexture("../irrlicht-0.4/media/faerie2.bmp");
material.Lighting = true;
IAnimatedMeshSceneNode* node = 0;
IAnimatedMesh* faerie = smgr->getMesh("../irrlicht-0.4/media/faerie.md2");
if(faerie)
{
node = smgr->addAnimatedMeshSceneNode(faerie);
node->setPosition(vector3df(-70,0,-90));
node->setMD2Animation(EMAT_RUN);
node->getMaterial(0) = material;

node = smgr->addAnimatedMeshSceneNode(faerie);
node->setPosition(vector3df(-70,0,-30));
node->setMD2Animation(EMAT_SALUTE);
node->getMaterial(0) = material;

node = smgr->addAnimatedMeshSceneNode(faerie);
node->setPosition(vector3df(-70,0,-60));
node->setMD2Animation(EMAT_JUMP);
node->getMaterial(0) = material;
}
material.Texture1 = 0;
material.Lighting = false;
// Create a light.
smgr->addLightSceneNode(0, vector3df(-60,100,400), SColorf(1.0f,1.0f,1.0f,1.0f), 600.0f);
// Create two pointers to store the current and last selected scene node.
ISceneNode* selectedSceneNode = 0;
ISceneNode* lastSelectedSceneNode = 0;
// Create a variable to store the frames per second.
int lastFPS = -1;
// Continually draw the scene, the FPS, and the number of primitives,
// until the program is shut down by the user.
while(device->run())
{
driver->beginScene(true, true, 0); // Clear the screen.
smgr->drawAll(); // Draw the contents of the Scene Manager.
// Draw health bar.
driver->draw2DRectangle(SColor(255,255,0,0), rect<s32>(10,10,health+10,20));
// Trace a ray between the camera and that which it is looking at and if it collides
// with a triangle, draw it and set the billboard position to the intersection point.
line3d<f32> line;
line.start = camera->getPosition();
line.end = line.start + (camera->getTarget() - line.start).normalize() * 1000.0f;
vector3df intersection;
triangle3df tri;
if(smgr->getSceneCollisionManager()->getCollisionPoint(line,selector,intersection,tri))
{
bill->setPosition(intersection);
driver->setTransform(TS_WORLD, matrix4());
driver->setMaterial(material);
driver->draw3DTriangle(tri, SColor(0,255,0,0));
}
// If we look at a scene node's bounding box, highlight it,
// if it isn't a billboard or the Quake 3 map.
selectedSceneNode = smgr->getSceneCollisionManager()->
getSceneNodeFromCameraBB(camera);
if(lastSelectedSceneNode)
{
lastSelectedSceneNode->setMaterialFlag(EMF_LIGHTING, true);
}
if(selectedSceneNode == q3node || selectedSceneNode == bill)
{
selectedSceneNode = 0;
}
if(selectedSceneNode)
{
selectedSceneNode->setMaterialFlag(EMF_LIGHTING, false);
}
lastSelectedSceneNode = selectedSceneNode;
driver->endScene(); // Finish drawing the scene.

int fps = driver->getFPS();
if(lastFPS != fps)
{
wchar_t tmp[1024];
swprintf(tmp, 1024,
L"Team4D - (fps:%d) Triangles:%d",
fps, driver->getPrimitiveCountDrawn());

device->setWindowCaption(tmp);
lastFPS = fps;
}
}
// Delete the Irrlicht Device.
device->drop();
return 0;
}
It's really just the collision tutorial code with a few modifications.
rt
Posts: 150
Joined: Sun Nov 30, 2003 6:54 am
Location: canada
Contact:

Post by rt »

two problems. the first problem is scope. in your OnEvent() function you have an if statement with "node != 0" which refers to the global "ISceneNode* node = 0;" variable. you also define a "IAnimatedMeshSceneNode* node = 0;" in the main() function which is NOT the same variable.

remove the line "IAnimatedMeshSceneNode* node = 0;" and it should work

the second problem is that your switch statement is missing a "break;" after each case. replace "health -= 5;" with "health -= 5;break;"
Gold_Fusion
Posts: 26
Joined: Tue Jan 20, 2004 9:25 pm
Location: England
Contact:

Post by Gold_Fusion »

I fixed the problems, but it still doesn't work... I got an error when I removed the line 'IAnimatedMeshSceneNode* node = 0', so I instead put 'IAnimatedMeshSceneNode* node2 = 0' and changed the 'node's below to 'node2's. It now runs, but the health bar still doesn't work.
Post Reply