how to use sprite for button?

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
comicsteam
Posts: 51
Joined: Thu Sep 11, 2008 2:24 pm
Location: Hong Kong
Contact:

how to use sprite for button?

Post by comicsteam »

i want to use button->setSprite() to make my button use different texture image on different state.
however, i don't know how to use the sprite bank
my program just crashed after i clicked the button.
here's my code:

Code: Select all

	gui::IGUIButton* button= guienv->addButton(core::rect<s32>(core::position2di(500,400), core::dimension2di(64,64)), 0, -1, L"Send", 0);
	if(button){
		//----make a sprite bank
		gui::IGUISpriteBank* sb = guienv->addEmptySpriteBank("my button");
		video::ITexture* texture_down = driver->getTexture("button_down.png");
		video::ITexture* texture_over = driver->getTexture("button_over.png");
		video::ITexture* texture_up = driver->getTexture("button_up.png");
		sb->addTexture(texture_down);
		//sb->setTexture(0, texture_down);
		sb->addTexture(texture_over);
		//sb->setTexture(1, texture_over);
		sb->addTexture(texture_up);
		//sb->setTexture(2, texture_up);

		//----apply to my button
		button->setSpriteBank(sb);
		button->setSprite(gui::EGBS_BUTTON_DOWN, 0, video::SColor(255, 255, 255, 255), false);
	}
does anyone know what the problem is?
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

what does your debugger say when it crashes? :p

you could check out my IGUIFileSelector which I put in the Code Snippets thread a while ago, that uses a sprite bank, i do remember they were a bit tricky to handle...
Image Image Image
comicsteam
Posts: 51
Joined: Thu Sep 11, 2008 2:24 pm
Location: Hong Kong
Contact:

Post by comicsteam »

JP wrote:what does your debugger say when it crashes? :p

you could check out my IGUIFileSelector which I put in the Code Snippets thread a while ago, that uses a sprite bank, i do remember they were a bit tricky to handle...
it said "Unhandled exception at 0x1001d0c2 in sc_viewer.exe: 0xC0000005: Access violation reading location 0x00000008."

i have read your post, but still couldnt find any solution :(
i thought sprite bank is just an array of texture....
am i misunderstand about what sprite bank truly means??
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

If you really debug your app you'll aslo see the stack trace and the debugger will point to the problematic code section. That's the really important thing.
asparagusx
Posts: 81
Joined: Thu Oct 16, 2008 6:50 am

Post by asparagusx »

Are you sure all the textures exist and that the getTexture function is not returning 0 (NULL)?
comicsteam
Posts: 51
Joined: Thu Sep 11, 2008 2:24 pm
Location: Hong Kong
Contact:

Post by comicsteam »

hybrid wrote:If you really debug your app you'll aslo see the stack trace and the debugger will point to the problematic code section. That's the really important thing.
it always pointed to "env->drawAll();"
asparagusx wrote:Are you sure all the textures exist and that the getTexture function is not returning 0 (NULL)?
i m sure that there's no texture missing
i use sb->getTextureCount() to check it
and sb->getTexture(0) can return the texture I have just added

it seems that it cannot find any sprite in the sprite bank
do I need to add some sprites into the sprite bank before calling setSprite()?
how can i do this?
comicsteam
Posts: 51
Joined: Thu Sep 11, 2008 2:24 pm
Location: Hong Kong
Contact:

Post by comicsteam »

problem solved.....
i have added a new empty sprite bank, but it don't have any sprite in it
now i added a sprite (with a single frame) and it works

Code: Select all

//add an empty sprite bank
gui::IGUISpriteBank* sb = guienv->addEmptySpriteBank("my button");
video::ITexture* texture_up = driver->getTexture("button_up.png");
video::ITexture* texture_down = driver->getTexture("button_down.png");
video::ITexture* texture_over = driver->getTexture("button_over.png");

//add textures into sprite bank
sb->addTexture(texture_up); //0
sb->addTexture(texture_down); //1
sb->addTexture(texture_over); //2

//add sizes
sb->getPositions().push_back(core::rect<s32>(core::position2di(0,0), texture_up->getOriginalSize()));
sb->getPositions().push_back(core::rect<s32>(core::position2di(0,0), texture_down->getOriginalSize()));
sb->getPositions().push_back(core::rect<s32>(core::position2di(0,0), texture_over->getOriginalSize()));

//add sprites
gui::SGUISprite sprite;
gui::SGUISpriteFrame sframe;
sprite.Frames.push_back(sframe);
sprite.Frames[0].rectNumber = 0;
sprite.Frames[0].textureNumber = 0;
sb->getSprites().push_back(sprite);
sprite.Frames[0].rectNumber = 1;
sprite.Frames[0].textureNumber = 1;
sb->getSprites().push_back(sprite);
sprite.Frames[0].rectNumber = 2;
sprite.Frames[0].textureNumber = 2;
sb->getSprites().push_back(sprite);

//use the sprite bank
button->setSpriteBank(sb);
button->setSprite(gui::EGBS_BUTTON_UP, 0, video::SColor(255, 255, 255, 255), false);
button->setSprite(gui::EGBS_BUTTON_DOWN, 1, video::SColor(255, 255, 255, 255), false);
button->setSprite(gui::EGBS_BUTTON_MOUSE_OVER, 2, video::SColor(255, 255, 255, 255), false);
button->setSprite(gui::EGBS_BUTTON_MOUSE_OFF, 0, video::SColor(255, 255, 255, 255), false);
however, another problem comes.........
both EGBS_BUTTON_MOUSE_OVER and EGBS_BUTTON_MOUSE_OFF are not usable
the button changes when it's clicked, but it doesn't change when the cursor moves over it
Nox
Posts: 304
Joined: Wed Jan 14, 2009 6:23 pm

Post by Nox »

The problem is, that some parts of irrlicht are incomplete. But a working mouseover effect would be cool.
Nox
Posts: 304
Joined: Wed Jan 14, 2009 6:23 pm

Post by Nox »

I created a patch for the GUIButton. You can apply it easly to a workingcopy of irrlicht. Link: https://sourceforge.net/tracker/index.p ... tid=540678

May be it will be added soon.
Post Reply