EventReceiver question

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
VeneX
Posts: 228
Joined: Sun Nov 30, 2003 3:32 pm
Location: The Netherlands
Contact:

EventReceiver question

Post by VeneX »

my eventreceiver:

Code: Select all

IAnimatedMeshSceneNode* node = 0;
IrrlichtDevice* device=0;

#pragma comment(lib, "Irrlicht.lib")

int main()
{
class MyEventReceiver : public IEventReceiver 
	{ 
	public: 

		virtual bool OnEvent(SEvent event) 
		{ 
		static bool model_anim = false;
		if (event.EventType == EET_KEY_INPUT_EVENT && !event.KeyInput.PressedDown) 
		{ 
            switch(event.KeyInput.Key) 
			{ 
				case KEY_KEY_W:
					{
						node->setMD2Animation(EMAT_RUN); 
					}
				case KEY_ESCAPE:
					{
						device->closeDevice();
					}
				default:
					{
						node->setMD2Animation(EMAT_STAND);
					}
			}
		 }
		 	
			if(event.EventType == EET_MOUSE_INPUT_EVENT && event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN )
			{
				node->setMD2Animation(EMAT_SALUTE);
			}
			return false;
		}
		 
	};

MyEventReceiver receiver;

	IrrlichtDevice *device = createDevice(EDT_DIRECTX8, dimension2d<s32>(1024, 768), 32, true, false, &receiver);
The node:

Code: Select all

SMaterial material;
	material.Texture1 = driver->getTexture("faerie2.bmp");
	material.Lighting = true;

	IAnimatedMesh* mesh = smgr->getMesh("faerie.md2");
	node = smgr->addAnimatedMeshSceneNode(mesh);

	if (node)
	{
		node->setPosition(vector3df(0,100,-90));
		node->setMD2Animation(EMAT_RUN);
		node->getMaterial(0) = material;
	}
If I use the W key the program exits hisself. What is wrong?
Thanx in advance.
Visit my website @ www.venex.be
Plethora project will be added to the site
AMD AthlonXP 2600+, 512MB DDR, Radeon M10 (mobile 9600) PRO 64MB, WinXP
level1

Post by level1 »

Ah, very easy, and just as easy to overlook:
you need to insert "break;" statements in the switch block

Code: Select all

switch(gugu)
{
case BLA:
  // dubidu
  break;
case BLU:
  // yeah baby yeah
  break;
default:
  // well...
}
VeneX
Posts: 228
Joined: Sun Nov 30, 2003 3:32 pm
Location: The Netherlands
Contact:

Post by VeneX »

:oops: I feel stupid!

Thanks for your help dude.


What's wrong with this code:

Code: Select all

class MyEventReceiver : public IEventReceiver 
	{ 
	public: 

		virtual bool OnEvent(SEvent event) 
		{ 
		static bool model_anim = false;
		if (event.EventType == EET_KEY_INPUT_EVENT && !event.KeyInput.PressedDown) 
		{ 
            switch(event.KeyInput.Key) 
			{ 
				case KEY_KEY_W:
					{
						event.KeyInput.Key=KEY_UP;
						node->setMD2Animation(EMAT_RUN); 
						break;
					}
				case KEY_KEY_A:
					{
						event.KeyInput.Key=KEY_LEFT;
						break;
					}
Visit my website @ www.venex.be
Plethora project will be added to the site
AMD AthlonXP 2600+, 512MB DDR, Radeon M10 (mobile 9600) PRO 64MB, WinXP
keless
Posts: 805
Joined: Mon Dec 15, 2003 10:37 pm
Location: Los Angeles, California, USA

Post by keless »

try putting the 'break;' outside of the code block:

Code: Select all

case KEY_KEY_W: 
               { 
                  event.KeyInput.Key=KEY_UP; 
                  node->setMD2Animation(EMAT_RUN); 
                   
               } 
break;
a screen cap is worth 0x100000 DWORDS
Guest
Posts: 35
Joined: Mon Feb 02, 2004 7:17 pm
Location: Russia, Saint-Petersburg

Post by Guest »

no. it useless cause 'break' breaks switch :)

1. I would recommend you ALWAYS use brackets when you are not sure about operators precedence. I have 7 years experience in C/C++ but I'm not sure which one is the first - 'comparison' or 'logical and'. Of course, you can use help but this makes your code difficult to read...

So I think you should set

Code: Select all

if ((event.EventType == EET_KEY_INPUT_EVENT) && !event.KeyInput.PressedDown)
After all I cannot see anything bad in this code. So nothing wrong with it.
Just don't forget that OnEvent function should return 0(false) if it doesn't process event, and nonzero (true) value if it does
there is another guest...
keless
Posts: 805
Joined: Mon Dec 15, 2003 10:37 pm
Location: Los Angeles, California, USA

Post by keless »

comparisson is done first, so he doesnt need parens here
a screen cap is worth 0x100000 DWORDS
Guest

Post by Guest »

You don't need the brackets in the case statements


class MyEventReceiver : public IEventReceiver
{
public:

virtual bool OnEvent(SEvent event) {
static bool model_anim = false;
if (event.EventType == EET_KEY_INPUT_EVENT && ! event.KeyInput.PressedDown) {
switch(event.KeyInput.Key) {
case KEY_KEY_W:
event.KeyInput.Key=KEY_UP;
node->setMD2Animation(EMAT_RUN);
break;
case KEY_KEY_A:
event.KeyInput.Key=KEY_LEFT;
break;
Guest
Posts: 35
Joined: Mon Feb 02, 2004 7:17 pm
Location: Russia, Saint-Petersburg

Post by Guest »

2keless: I have MSDN - thank you. But if you have ever tested your code for meeteing any code style then you should know that almost all of them give exactly this situation as an example how you don't have to write your code ;)

2Guest: almost the same ;) it's always better to set brackets than do not set (exception is only for one string expressions). BTW, if you declare variable in one case in switch without brackets - you'll get error (I think you know this) ;)

2VeneX: Finally, can you tell me what's wrong with your code? ;) I mean what goes with your program?

WBR,
Mike
there is another guest...
VeneX
Posts: 228
Joined: Sun Nov 30, 2003 3:32 pm
Location: The Netherlands
Contact:

Post by VeneX »

Sorry I forgot to say what didn't work. The code:

Code: Select all

event.KeyInput.Key=KEY_UP;
doesn't work....

I have a demo on my site now but the only pc he is working on is mine :?
I am trying to find the bug, expect some code...
Visit my website @ www.venex.be
Plethora project will be added to the site
AMD AthlonXP 2600+, 512MB DDR, Radeon M10 (mobile 9600) PRO 64MB, WinXP
Post Reply