help on SetEventReceiver

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
bappy
Posts: 63
Joined: Fri Dec 12, 2003 10:49 am
Location: france
Contact:

help on SetEventReceiver

Post by bappy »

hi all!
i need just a little help on the SetEventReceiver.
i ve got 2 class (Intro and Company)which are base on the IEventReceiver.

here is my main loop

main()
{
Intro.Open(); // create all gui and graphic stuff
Intro.Run(); // go in while(escape_key) loop
Intro.Close() // delete all stuff

Company.Open(); // create all gui and graphic stuff
Company.Run(); // go in while(escape_key) loop
Company.Close() // delete all stuff

}


and in Intro.Open()
{
Irrlicht.Device->setEventReceiver(this);
StaticText = guienv->addStaticText(L"",false,rect<int>(0,0,150,20));
// other init stuff
}

in Intro.Run()
{
// basic draw loop
while(!Exit)
{
if (Irrlicht.Device->isWindowActive())
{
float elapsedtime= GameTimer.ElapsedTime();
Irrlicht.Device->run();
Update(elapsedtime);
Irrlicht.Driver->beginScene(true, true,SColor(0,0,0,0));
guienv->drawAll();
Irrlicht.Driver->endScene();
StaticText->setText(text);
}
}

in Intro.Close()
{
Irrlicht.Device->setEventReceiver(NULL);
StaticText->Remove();
}

and in event:
OnEvent(SEvent event)
{
if(event.EventType == EET_KEY_INPUT_EVENT )
{
s32 id = event.KeyInput.Key;
switch(id)
{
case KEY_ESCAPE: LogFile.Write(LOGERROR,"INTRO ESCAPE KEY PRESS",true);
Exit=true;
break;
}
}
return false;
}

and i ve got the same code in company class (just graphic stuff change)

so what i want is when i am in the intro loop, if i press escape i close intro (so unallocate all graphic stuff) and create company event and go thrue the loop.
All is ok, all is working, allocation and unallocation is good but there is something wrong...
when i press the first time escape, it close the intro ,set eventreceiver to NULL so it s ok, and when i create the company class, in the event function, it goes directly in the escape sequence. the engine has save the key input KEY_ESCAPE.
So i would like to know if there is a function after having set the EventReceiver to NULL with Device->setEventReceiver(NULL) that can "clean" the inputreceiver or Maybe NiKo can you implement a function like this:
Device->unsetEventReceiver(); that doing things in the right way...
thx, i hope you have understand...
-----------------------------
Sans danger, pas de gloire...
http;//bappy.free.fr
-----------------------------
nebukadnezzar
Posts: 25
Joined: Tue Dec 02, 2003 7:45 pm
Location: Germany - Bornheim

Post by nebukadnezzar »

maybe you should not return false.
try return true; in onEvent().

quote from api-doc
virtual bool irr::IEventReceiver::OnEvent ( SEvent event ) [pure virtual]

called if an event happened. returns true if event was processed
so let the engine know that ypu have processed the event - otherwise the engine keeps this event until its processed.
schick
Posts: 230
Joined: Tue Oct 07, 2003 3:55 pm
Location: Germany
Contact:

State Machine

Post by schick »

I am new to c++ and program design but i would use something like a state machine. There is a class which handles the current state and is able to switch between all know states. There is one eventreceiver in the handle class that receives all the events coming from the irrlicht engine and calls the state onevent function which processes all the events for the state. Every state is supposed to have its own draw function.

There is a simply state machine avaible using irrlicht by keless.

http://irrlicht.sourceforge.net/phpBB2/ ... .php?t=924

I modified it myself and its working great.
keless
Posts: 805
Joined: Mon Dec 15, 2003 10:37 pm
Location: Los Angeles, California, USA

Post by keless »

thanks for the plug schick :wink:

yes, there is no reason to have to shut down your device, unless you are switching renderers or bit depths/etc. If you download my framework, the example code given will allow you to compile it right away and see exactly how to switch between different states (such as intro and company).

part of the design issues you will come up with are, what deserves its own state? and what is merely a sub-state? I suggest you draw out a state-diagram or a program flow-chart to plan out how your program should act before coding.
a screen cap is worth 0x100000 DWORDS
bappy
Posts: 63
Joined: Fri Dec 12, 2003 10:49 am
Location: france
Contact:

Post by bappy »

thats ok, i have found what was the problem, here is the solution:
if(event.EventType == EET_KEY_INPUT_EVENT && EVENT.KEYINPUT.PRESSEDDOWN==FALSE)

i have forget to test if the key escape was release...
thx
-----------------------------
Sans danger, pas de gloire...
http;//bappy.free.fr
-----------------------------
Post Reply