clear() function problems

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
gamescoper
Posts: 7
Joined: Fri Feb 01, 2008 9:33 pm

clear() function problems

Post by gamescoper »

Firstly this is my first attempt at windows programming in c++ (my first attempt at c++ was for dos) anyway i was trying to make a menu and i thought that i should clear the gui objects before drawing in the next menu. Well after going through a few tutorials i got this far and the clear() function doesnt work. Heres the code i will be really grateful for any help i can get

Code: Select all

//includes
#include <irrlicht.h>
//namespaces
using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;
//not sure about this bit
#ifdef _IRR_WINDOWS_
#pragma comment(lib, "Irrlicht.lib")
#endif
//this is need for the gui
IrrlichtDevice *device = 0;
s32 cnt = 0;
IGUIListBox* listbox = 0;
//event reciever
class MyEventReceiver : public IEventReceiver
{
public:
	virtual bool OnEvent(const SEvent& event)
	{
		if (event.EventType == EET_GUI_EVENT)
		{
			s32 id = event.GUIEvent.Caller->getID();
			IGUIEnvironment* guienv = device->getGUIEnvironment();
			switch(event.GUIEvent.EventType)
{
    case EGET_BUTTON_CLICKED:

				if (id == 101)
				{
//problem here
					guienv->clear();
					guienv->addButton(rect<s32>(104, 64, 320, 96), 0, -1, L"Back");
                    guienv->addStaticText(L"Story mode in develpoment", rect<s32>(104, 32, 320, 52), false, false, 0, -1);
					return true;
				}
}
}
}
};
//main bit for drawing
int main()
{
    IrrlichtDevice *device =
		createDevice( video::EDT_SOFTWARE, dimension2d<s32>(640, 480), 32, true, false, false, 0);
    device->setWindowCaption(L"Window");
    IVideoDriver* driver = device->getVideoDriver();
	ISceneManager* smgr = device->getSceneManager();
	IGUIEnvironment* guienv = device->getGUIEnvironment();
	guienv->addButton(rect<s32>(88, 200, 304, 240), 0, 4, L"Exit");
	guienv->addButton(rect<s32>(88, 152, 304, 192), 0, 3, L"Options");
    guienv->addButton(rect<s32>(88, 104, 304, 144), 0, 2, L"Multiplayer");
    guienv->addButton(rect<s32>(88, 56, 304, 96), 0, 101, L"Story Mode");
    guienv->addStaticText(L"Game menu", rect<s32>(88, 16, 304, 36), false, false, 0, -1);

    while(device->run())
	{
		driver->beginScene(true, true, SColor(255,100,101,140));

		smgr->drawAll();
		guienv->drawAll();

		driver->endScene();
	}
}    
sorry about it not being neat and i never compile with the comments so dont worry if there in the way somehow. I have searched around a bit but i havent anything yet
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post by rogerborg »

No problem, I prefer full runnable source than a commented snippet, because the compiler and debugger are far better at analysing source than my feeble meatbrain.

Just a couple of common snafus.

You need to actually instantiate your event receiver and pass it to the device. Also, you're hiding your global device variable with a local one in your main(). So just change your device instantiation to this:

Code: Select all

    MyEventReceiver receiver;
    device = createDevice( video::EDT_SOFTWARE, dimension2d<s32>(640, 480), 32, true, false, false, &receiver);
Two other things. Your OnEvent() method should return false; at the end, and just before you exit your main() function, call device->drop() to clean up all the memory.

I highly recommend that you learn to use your debugger. Sticking a breakpoint in your OnEvent() method would have shown very quickly that it wasn't being called, and you can work backwards from there.
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
gamescoper
Posts: 7
Joined: Fri Feb 01, 2008 9:33 pm

Post by gamescoper »

thanks for the help and as a matter of fact this is THE best forum i have been to people here are so nice and comfortable to speak with unless they are shouting at you but thats impossible without VOIP
ibax
Posts: 193
Joined: Thu Jun 21, 2007 8:56 am
Location: hungary
Contact:

Post by ibax »

I have problems, with the clear() function.

when my project reaches the "env->clear();" part of my code, it crashes. I use this clear() function sometimes in my project, and is working, but now it crashes. when I delete it, the program works correctly (but the previously added GUI elements are on the screen, too.

I can't find the solution... once it is working, once not. why is it? if I use for the GUI elements the setVisible(false) method, it is working, but I have lot of GUI elements...

can somebody help me?
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

Possibly your env pointer is invalid, have you got two declarations of it and you're only assigning a value to one?
Image Image Image
ibax
Posts: 193
Joined: Thu Jun 21, 2007 8:56 am
Location: hungary
Contact:

Post by ibax »

hmm, it is interesting, what u said, but this function works, but if I remove the comments ( // ), the program crashes...

Code: Select all

void Admin::changeLogin(IGUIEnvironment* env, IVideoDriver* driver, IrrlichtDevice* dev)
{
// env->clear(); 

env->addStaticText(L"New login name",rect<s32>(50,140,250,162),0,0,0,-1,1);
		
env->addStaticText(L"New password",rect<s32>(50,221,250,243),0,0,0,-1,1);
env->addStaticText(L"New password once again",rect<s32>(50,302,250,324),0,0,0,-1,1);
}
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

Two things... It is very useful if you provide a simple testcase that compiles, runs and illustrates the problem. It is also nice if you create your own thread if your particular issue is different from the original [which it is].

Travis
Post Reply