Movement tutorial on OSX, keys don't respond

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
phlogios
Posts: 13
Joined: Fri Aug 03, 2007 9:23 pm
Location: Lund, Sweden

Movement tutorial on OSX, keys don't respond

Post by phlogios »

Just got IrrLicht working the other day, thanks to a fellow poster on this forum.
However, I'm having problems following the tutorials. I'm on Movement, and I can't seem to get the input to work.

My code has a few changes from the original, thought I'd make it simpler. I don't know, maybe you guys can help me spot the problem.
Yes, I tried moving the object independently from keyboard input, and it worked. I also tried returning 0 when a key was pressed. Didn't work.
Code:

Code: Select all

#include "main.h"
#include <Irrlicht/Irrlicht.h>
#include <OpenGL/OpenGL.h>

using namespace irr;
using namespace core;
using namespace video;
using namespace io;
using namespace gui;

class MyEventReceiver : public IEventReceiver
	{
	public:
        // This is the one method that we have to implement
        virtual bool OnEvent(const SEvent& event)
        {
			// Remember whether each key is down or up
			if (event.EventType == irr::EET_KEY_INPUT_EVENT)
				KeyIsDown[event.KeyInput.Key] = event.KeyInput.PressedDown;
			
			return false;
        }
		
        // This is used to check whether a key is being held down
        virtual bool IsKeyDown(EKEY_CODE keyCode) const
        {
			return KeyIsDown[keyCode];
        }
        
        MyEventReceiver()
        {
			for (u32 i=0; i<KEY_KEY_CODES_COUNT; ++i)
				KeyIsDown[i] = false;
        }
		
	private:
        // We use this array to store the current state of each key
        bool KeyIsDown[KEY_KEY_CODES_COUNT];
	};

int main(){
	MyEventReceiver receiver;
	IrrlichtDevice *device =
	#ifdef _IRR_OSX_PLATFORM_
	createDevice( video::EDT_OPENGL, dimension2d<s32>(640, 480), 16, false, false, false, 0);
	#else
	createDevice( video::EDT_SOFTWARE, dimension2d<s32>(640, 480), 16, false, false, false, 0);
	#endif
	if (!device) return 1;
	#ifdef _IRR_OSX_PLATFORM_
	device->getFileSystem ()->changeWorkingDirectoryTo ("Rat.app");
	device->getFileSystem ()->changeWorkingDirectoryTo ("Contents");
	device->getFileSystem ()->changeWorkingDirectoryTo ("Resources");
	#endif 
	device->setWindowCaption(L"Hello World!");
	IVideoDriver* driver = device->getVideoDriver();
	scene::ISceneManager* smgr = device->getSceneManager();
	IGUIEnvironment* guienv = device->getGUIEnvironment();
	guienv->addStaticText(L"Hello World! This is the Irrlicht Software renderer!",
						  rect<s32>(10,10,260,22), true);

	scene::ISceneNode* node = smgr->addSphereSceneNode();
	if (node)
	{
		node->setPosition(core::vector3df(0,0,30));
		node->setMaterialTexture(0, driver->getTexture("sydney.bmp"));
		node->setMaterialFlag(video::EMF_LIGHTING, false);
	}
	

	smgr->addCameraSceneNode(0, vector3df(0,30,-40), vector3df(0,5,0));

	//device->getCursorControl()->setVisible(false);

	
	u32 then = device->getTimer()->getTime();
	const f32 MOVEMENT_SPEED = 5.f;
	
	int lastFPS = -1;
	while(device->run())
	{
		// Work out a frame delta time.
		const u32 now = device->getTimer()->getTime();
		const f32 frameDeltaTime = (f32)(now - then) / 1000.f; // Time in seconds
		then = now;
		
		core::vector3df nodePosition = node->getPosition();
		if(receiver.IsKeyDown(irr::KEY_KEY_W))
			nodePosition.Y += MOVEMENT_SPEED * frameDeltaTime;
		else if(receiver.IsKeyDown(irr::KEY_KEY_S))
			nodePosition.Y -= MOVEMENT_SPEED * frameDeltaTime;
		
		if(receiver.IsKeyDown(irr::KEY_KEY_A))
			nodePosition.X -= MOVEMENT_SPEED * frameDeltaTime;
		else if(receiver.IsKeyDown(irr::KEY_KEY_D))
			nodePosition.X += MOVEMENT_SPEED * frameDeltaTime;
		
		node->setPosition(nodePosition);
		
		if (device->isWindowActive())
		{

						
			driver->beginScene(true, true, video::SColor(255,200,200,200));
			smgr->drawAll();
			driver->endScene();
			
			int fps = driver->getFPS();
			
			if (lastFPS != fps)
			{
				core::stringw str = L"[";
				str += driver->getName();
				str += "] FPS:";
				str += fps;
				
				device->setWindowCaption(str.c_str());
				lastFPS = fps;
			}
		}
		else
			device->yield();
	}
	device->drop();	
	return 0;
}
Thanks in advance.
hAlp!?
DavidJE13
Posts: 165
Joined: Tue Jan 09, 2007 7:17 pm

Post by DavidJE13 »

you need to add:

device->setEventReceiver( receiver );

somewhere in your setup code, or your event receiver won't be receiving any events!
phlogios
Posts: 13
Joined: Fri Aug 03, 2007 9:23 pm
Location: Lund, Sweden

Post by phlogios »

device->setEventReceiver( &receiver );

This command made it work, thanks!
hAlp!?
hdalin
Posts: 3
Joined: Tue Sep 01, 2009 6:57 pm

Post by hdalin »

as far as i know you can also set the event receiver in the begining:

Code: Select all

IrrlichtDevice *device = createDevice(video::EDT_OPENGL,
        dimension2d<s32>(640,480), 16, false, false, false, &receiver);
DavidJE13
Posts: 165
Joined: Tue Jan 09, 2007 7:17 pm

Post by DavidJE13 »

phlogios wrote:device->setEventReceiver( &receiver );

This command made it work, thanks!
heh, oops. Always forgetting those little bits :?

and hdalin's right. I usually use the SIrrlichtCreationParameters object with createDeviceEx which makes it easier to see what's going on in my oppinion (as well as giving you more control over other things)

glad to help :)
Post Reply