IProgressBar: A progress-bar gui element

Post those lines of code you feel like sharing or find what you require for your project here; or simply use them as tutorials.
Zurzaza
Posts: 153
Joined: Fri Mar 26, 2010 9:41 pm
Location: Filo (FE), Italy
Contact:

IProgressBar: A progress-bar gui element

Post by Zurzaza »

Ok, i noticed that I need a progress bar into my project, so I realized this little class that do this work!

CProgressBar.h

Code: Select all

#ifndef IPROGRESSBAR_H_
#define IPROGRESSBAR_H_
#include <irrlicht.h>

using namespace irr;
using namespace core;
using namespace scene;
using namespace gui;
class IProgressBar : public IGUIElement
{
public:
	
	IProgressBar(IGUIEnvironment * guienv,const core::rect<s32>& rectangle,s32 id=-1,IGUIElement * parent=0);
	

	/*Set percentage in positive percentual (0~100). Please note that a call to this function with others values, will set the progress bar to 0.*/
	void setProgress(irr::u32 progress);

	/*Set bar Colors*/
	void setColors(irr::video::SColor progress= irr::video::SColor(255,255,255,255),irr::video::SColor filling= irr::video::SColor(255,0,0,0));

	/*Allow you to add a "border" into your bar. You MUST specify the size (of course in pixel) of the border. You can also pass a color parameter (Black by default)*/
	void addBorder(irr::s32 size,irr::video::SColor color = irr::video::SColor(255,0,0,0));

	
	virtual void draw();
private:
	
	IGUIEnvironment * gui; //GUI ENV. pointer
	irr::s32 total; //Dimension (X) of the bar, to calculate relative percentage.
	rect<s32> bar; //Dimension of the bar
	rect<s32> position; //Bar
	rect<s32> border; //Border 
	rect<s32> tofill; //Percentage indicator
	rect<s32> empty; //"Empty" indicator

	irr::video::SColor fillcolor;
	irr::video::SColor emptycolor;
	irr::video::SColor bordercolor;
	irr::video::IVideoDriver * vdriver;
	
};
#endif
CProgressBar.cpp

Code: Select all

#include "CProgressBar.h"

IProgressBar::IProgressBar(IGUIEnvironment * guienv,const core::rect<s32>& rectangle,s32 id,IGUIElement * parent) : IGUIElement(EGUIET_ELEMENT,guienv,parent,id,rectangle)
{
	total = rectangle.LowerRightCorner.X - rectangle.UpperLeftCorner.X;
	gui = guienv;
	bar = rectangle;

	if(parent == 0)
		guienv->getRootGUIElement()->addChild(this); //Ensure that draw method is called
	vdriver = this->gui->getVideoDriver();
	fillcolor.set(255,255,255,255);
	emptycolor.set(255,0,0,0);
	border = bar;
	this->setProgress(0);
}
void IProgressBar::setColors(irr::video::SColor progress,irr::video::SColor filling)
{
	fillcolor = progress;
	emptycolor = filling;
}
void IProgressBar::addBorder(irr::s32 size,irr::video::SColor color)
{
	bordercolor = color;
	border = bar;
	border.UpperLeftCorner.X -= size;
	border.UpperLeftCorner.Y -= size;
	border.LowerRightCorner.X += size;
    border.LowerRightCorner.Y += size; 
}
void IProgressBar::setProgress(irr::u32 progress)
{
	if(progress > 100)
		progress = 0;
	
	u32 xpercentage;
	xpercentage = (progress * total)/100; //Reducing to the bar size
	tofill.UpperLeftCorner.set(bar.UpperLeftCorner.X,bar.UpperLeftCorner.Y);
	tofill.LowerRightCorner.set(bar.UpperLeftCorner.X+xpercentage,bar.LowerRightCorner.Y);
	empty.UpperLeftCorner.set(tofill.LowerRightCorner.X,tofill.UpperLeftCorner.Y);
	empty.LowerRightCorner.set(bar.LowerRightCorner.X,bar.LowerRightCorner.Y);
}
void IProgressBar::draw()
{
	if(this->IsVisible == false)
		return;

	vdriver->draw2DRectangle(bordercolor,border);
	vdriver->draw2DRectangle(fillcolor,tofill);
	vdriver->draw2DRectangle(emptycolor,empty);
}
It's very simple to use...here is it a simple code:

Code: Select all

	IProgressBar * pb = new IProgressBar(guienv,rect<s32>(100,500,600,600));
	
	pb->setProgress(90);
	pb->addBorder(1);
	pb->drop();
Hope this can help...for any problems or bugs, i'm here to listen you.
Last edited by Zurzaza on Fri Jul 30, 2010 11:15 am, edited 3 times in total.
Brainsaw
Posts: 1241
Joined: Wed Jan 07, 2004 12:57 pm
Location: Bavaria

Post by Brainsaw »

That's nice. I think I'll include it in my next project.
Image
Dustbin::Games on the web: https://www.dustbin-online.de/
Zurzaza
Posts: 153
Joined: Fri Mar 26, 2010 9:41 pm
Location: Filo (FE), Italy
Contact:

Post by Zurzaza »

Thank you ;)
Here is a screenshot from a scene in irrlicht with this bar:

Image
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post by Acki »

yeah, a progressbar was one of my very first extensions, too !!! 8)
a nice addition would be to implement the possibility for text, like I did !!! ;)
Image
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
Zurzaza
Posts: 153
Joined: Fri Mar 26, 2010 9:41 pm
Location: Filo (FE), Italy
Contact:

Post by Zurzaza »

Acki wrote:yeah, a progressbar was one of my very first extensions, too !!! 8)
oh i didn't know that you made a extension like this :P
trollger
Posts: 84
Joined: Tue Jun 01, 2010 2:17 am
Location: At my computer

Post by trollger »

just to let you know,I'm using code::blocks
when I use it I get the error:

C:\Documents and Settings\Brian\Desktop\josh\program_files\Code_Blocks\Projects\Irr\main.cpp||In function 'int main()':|
C:\Documents and Settings\Brian\Desktop\josh\program_files\Code_Blocks\Projects\Irr\main.cpp|77|error: no matching function for call to 'IProgressBar::IProgressBar(irr::gui::IGUIEnvironment*&, irr::core::rect<int>)'|
C:\Documents and Settings\Brian\Desktop\josh\program_files\Code_Blocks\Projects\Irr\Classes.h|42|note: candidates are: IProgressBar::IProgressBar(irr::gui::IGUIEnvironment*, irr::core::rect<int>&, irr::s32, irr::gui::IGUIElement*)|
C:\Documents and Settings\Brian\Desktop\josh\program_files\Code_Blocks\Projects\Irr\Classes.h|39|note: IProgressBar::IProgressBar(const IProgressBar&)|
C:\Documents and Settings\Brian\Desktop\josh\program_files\Code_Blocks\Projects\Irr\main.cpp|150|warning: unused variable 'TR'|
C:\Documents and Settings\Brian\Desktop\josh\program_files\Code_Blocks\Projects\Irr\main.cpp|98|warning: unused variable 'SkyBox'|
||=== Build finished: 1 errors, 2 warnings ===|
Zurzaza
Posts: 153
Joined: Fri Mar 26, 2010 9:41 pm
Location: Filo (FE), Italy
Contact:

Post by Zurzaza »

How did you allocate the IProgressBar object?
trollger
Posts: 84
Joined: Tue Jun 01, 2010 2:17 am
Location: At my computer

Post by trollger »

I just used

Code: Select all

   IProgressBar * pb = new IProgressBar(guienv,rect<s32>(100,500,600,600));
   
   pb->setProgress(90);
   pb->addBorder(1);
   pb->drop(); 
and it gave me that error.
Zurzaza
Posts: 153
Joined: Fri Mar 26, 2010 9:41 pm
Location: Filo (FE), Italy
Contact:

Post by Zurzaza »

it seems that code blocks doesn't recognize the default parameters, you can try to add the default parameters (defined in the prototype):

Code: Select all

   IProgressBar * pb = new IProgressBar(guienv,rect<s32>(100,500,600,600),-1,0); 
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post by Acki »

Zurzaza wrote:it seems that code blocks doesn't recognize the default parameters
of course it does !!! ;)

the error is with the definition of the rect !!!
either do it like this:

Code: Select all

  rect<s32> rrr(100,500,600,600);
  IProgressBar * pb = new IProgressBar(guienv, rrr);
or change the constructor to (see different rect<s32> definition):

Code: Select all

IProgressBar(IGUIEnvironment* guienv,core::rect<s32> rectangle,s32 id=-1,IGUIElement * parent=0);
Last edited by Acki on Thu Jul 29, 2010 11:11 pm, edited 2 times in total.
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
Zurzaza
Posts: 153
Joined: Fri Mar 26, 2010 9:41 pm
Location: Filo (FE), Italy
Contact:

Post by Zurzaza »

Acki wrote:
Zurzaza wrote:it seems that code blocks doesn't recognize the default parameters
of course it does !!! ;)

the error is with the definition of the rect !!!
oh...i think that he forgot to declare the namespace :P
so the solution is to use irr::core::rect<s32>..I thought that there was a problem with the prototype :P I'm getting old...


edit: I see your reply...but i run my example correctly, so I think he forgot to use the namespace ;)
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post by Acki »

Zurzaza wrote:I see your reply...but i run my example correctly, so I think he forgot to use the namespace ;)
no, I used all namespaces for testing and had the same problem... ;)
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
Zurzaza
Posts: 153
Joined: Fri Mar 26, 2010 9:41 pm
Location: Filo (FE), Italy
Contact:

Post by Zurzaza »

Acki wrote:
Zurzaza wrote:I see your reply...but i run my example correctly, so I think he forgot to use the namespace ;)
no, I used all namespaces for testing and had the same problem... ;)
ok so it seems that only visual studio accepts this kind of constructor. Thank for your help (changed the constructor)

@trollger: you only need to update both files
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post by Acki »

Zurzaza wrote:ok so it seems that only visual studio accepts this kind of constructor.
also not correct, C::B accepts it, otherwise the first solution I posted wouldn't work either... ;)

maybe there is a compiler flag you can set so it converts the vars automatically, and maybe its set as default for VC but not for C::B ???
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

Actually, your first version was nearer to the correct (IMHO!) solution. Just use a

Code: Select all

const rect<s32>&
instead of the used

Code: Select all

rect<s32>&
(mind the 'const'). Your version now creates a copy, which works with temporaries as arguments, but requires additional cycles on each call. With your original version using a reference, the temporary cannot be used as an argument. But using the const reference avoids the additional memory and parameter passing overhead, and can work with much more argument types than both other versions.
Post Reply