A problem with controls

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
Pörzi
Posts: 9
Joined: Wed Aug 08, 2007 8:16 am

A problem with controls

Post by Pörzi »

Why doesn't this work?

Code: Select all

int a = 0;

SEvent event;

while (device->run())
{	
		if (event.EventType == EET_KEY_INPUT_EVENT && !event.KeyInput.PressedDown)
		{
			if (event.KeyInput.Key == KEY_KEY_W) a += 10;
		}
The compiler doesn't give any error but when I press the w key nothing happens. (the a variable changes an angle...)
Perceval
Posts: 158
Joined: Tue May 30, 2006 2:42 pm

Post by Perceval »

Take a look at this tutorial http://irrlicht.sourceforge.net/tut004.html, which explains how event receiver work :)
Pörzi
Posts: 9
Joined: Wed Aug 08, 2007 8:16 am

Post by Pörzi »

Perceval wrote:Take a look at this tutorial http://irrlicht.sourceforge.net/tut004.html, which explains how event receiver work :)
The code is actually based on this tutorial...

I studied the tutorial again but the controls still don't work with an AnimatedMeshSceneNode. I now tried to make the controls like they are made in the tutorial. They work just fine with a SphereSceneNode but not with an AnimatedMeshSceneNode.

Thanks anyway!
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

The code is actually based on this tutorial...
Sorry, no it is not. Either that or you have a very liiberal definition of 'based on'.

The tutorial uses an event receiver to handle events, buy you're trying to handle them in main. If you want to handle events using Irrlicht, you need an event receiver.

Travis
Pörzi
Posts: 9
Joined: Wed Aug 08, 2007 8:16 am

Post by Pörzi »

vitek wrote:
The code is actually based on this tutorial...
Sorry, no it is not. Either that or you have a very liiberal definition of 'based on'.

The tutorial uses an event receiver to handle events, buy you're trying to handle them in main. If you want to handle events using Irrlicht, you need an event receiver.

Travis
I meant that I studied how to program the controls from that tutorial... my mistake.

Anyway I now copy pasted parts of the tutorial and the controls work with a SphereSceneNode but not with an AnimatedMeshSceneNode.

Thanks in advance! And sorry for my English.
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post by Acki »

Show us your event receiver and main loop, please... ;)
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
Pörzi
Posts: 9
Joined: Wed Aug 08, 2007 8:16 am

Post by Pörzi »

Acki wrote:Show us your event receiver and main loop, please... ;)
Here is the whole code. The comments were in Finnish so I removed most of them.

Code: Select all

#include<irrlicht.h>

using namespace irr;

using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;

#ifdef _IRR_WINDOWS_
#pragma comment(lib, "Irrlicht.lib")
#endif

scene::ISceneNode* node = 0;
IrrlichtDevice* device = 0;

class MyEventReceiver : public IEventReceiver
{
public:
	virtual bool OnEvent(SEvent event)
	{
		if (node != 0 && event.EventType == EET_KEY_INPUT_EVENT&&
			!event.KeyInput.PressedDown)
		{
			switch(event.KeyInput.Key)
			{
			case KEY_KEY_W:
			case KEY_KEY_S:
				{
					vector3df v = node->getPosition();
					v.Y += event.KeyInput.Key == KEY_KEY_W ? 2.0f : -2.0f;
					node->setPosition(v);
				}
				return true;
			}
		}

		return false;
	}
};

int main()
{
MyEventReceiver receiver;

IrrlichtDevice *device = createDevice(EDT_OPENGL, dimension2d<s32>(640,480), 16, false, false, false, &receiver);

IVideoDriver *driver = device->getVideoDriver();
ISceneManager *smgr = device->getSceneManager();
IGUIEnvironment *guienv = device->getGUIEnvironment();

//######## If I move the "//" from the next line to the line after that the controls will work just fine. ########
//node = smgr->addSphereSceneNode();
IAnimatedMesh *mesh = smgr->getMesh("../media/model.b3d");

IAnimatedMeshSceneNode *node = smgr->addAnimatedMeshSceneNode(mesh);

node->setAnimationSpeed(2000);

node->setMaterialFlag(EMF_LIGHTING, false);
node->setMaterialTexture(0, driver->getTexture("../media/model.png"))

smgr->addCameraSceneNode(0, vector3df(20,20,-20), vector3df(0,0,0));

while (device->run())
{	
	driver->beginScene(true, true, SColor(0,0,0, 0));
	smgr->drawAll();
	driver->endScene();

	stringw fps = L"FPS ";
	fps += driver->getFPS();
	device->setWindowCaption(fps.c_str());
}

device->drop();
return 0;
}
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post by Acki »

Ahhh, you have "node" declared 2 times, 1 global and 1 local !!!
change one of them to another name or remove the global one !!! ;)

EDIT: sorry, in your case remove the local declaration and change the global one to IAnimatedMeshSceneNode *node = 0;... :lol:
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
Pörzi
Posts: 9
Joined: Wed Aug 08, 2007 8:16 am

Post by Pörzi »

Acki wrote:Ahhh, you have "node" declared 2 times, 1 global and 1 local !!!
change one of them to another name or remove the global one !!! ;)

EDIT: sorry, in your case remove the local declaration and change the global one to IAnimatedMeshSceneNode *node = 0;... :lol:
How can I have been this stupid? :lol:

Thanks!
Post Reply