Problem with draw2DImage

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
Methuselah
Posts: 19
Joined: Fri Nov 24, 2006 9:33 am

Problem with draw2DImage

Post by Methuselah »

I was trying to create my own gui and decided to have all the elements in the gui drawn by the main gui object, so there is a simple function that calls draw2DImage with each element's ITexture*. The problem is that once that is in a separate function it no longer works. It works fine when the actual call to draw2DImage is in the main loop, but for some reason, when it is in a function it breaks down.

Any ideas?
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post by rogerborg »

Sorry, I'm not psychic. Do you want to post your code?
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
Methuselah
Posts: 19
Joined: Fri Nov 24, 2006 9:33 am

Post by Methuselah »

Prep

Code: Select all

GUI mygui; //the main gui object
	Button button(driver->getTexture("irrlichtlogo.jpg"),NULL,NULL,core::position2d<s32>(10,10)); //creates a button
	mygui.addButton(&button);//adds the button to the gui

Main Loop

Code: Select all

while (device->run() && driver)
	{
		u32 time = device->getTimer()->getTime();
		driver->beginScene(true,true,video::SColor(255,0,0,0));
		smng->drawAll();
		mygui.drawAll();
		//driver->draw2DImage(button.drawTexture, button.position);
                // ^ the commented out part works

		driver->endScene();
	}
Here's the GUI::drawAll()

Code: Select all

void GUI::drawAll()
{
	//buttons
	for (unsigned i=0;i<buttons.size();i++)
		driver->draw2DImage(buttons[i]->drawTexture, buttons[i]->position);
}
buttons is an std::vector<Button*>
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post by rogerborg »

"Button button" creates an object on the stack, which you're then passing and storing by reference. Unless the stack remains intact up to the point where you access mygui.buttons, you'll be accessing garbage.

I'd suggest:

Code: Select all

 Button * button = new Button(driver->getTexture("irrlichtlogo.jpg"),NULL,NULL,core::position2d<s32>(10,10)); //creates a button
mygui.addButton(button);//adds the button to the gui
Remembering that you'll need to delete all of the buttons in mygui when you're done with them.
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
Methuselah
Posts: 19
Joined: Fri Nov 24, 2006 9:33 am

Post by Methuselah »

Well, I tried that and it did not work. I also tried to have the Button objects created directly from the addButton function and then deleted by the destructor of the GUI object, but that did not work as well.
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post by rogerborg »

Then I suggest that you post your entire source, as I'm not going to start guessing what you mean by "did not work".
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
Methuselah
Posts: 19
Joined: Fri Nov 24, 2006 9:33 am

Post by Methuselah »

This is most of the source. I've haven't included the classes but they only have the definitions of the variables and function prototypes. The project is barely beginning and I wanted to start with a basic GUI of my own.
In terms of "not working" - I mean I get the "Do you want to send a report to microsoft" type of window. It is not a compile/linker error. When I try to debug the program, it crashes everything, including the Visual Studio and I have to end the devenv.exe process.
Post Reply