Please help with keyboard input!

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
AusZero
Posts: 11
Joined: Sat Oct 06, 2007 11:16 pm

Please help with keyboard input!

Post by AusZero »

I really need help or a tutorial on how to get keyboard input besides the one that is on the tutorials page please help or just fix my code.

All I need is to be able to press the spacebar and it change gamemode to 1.

Code: Select all

#include "stdafx.h"

int gamemode = 0;

ISceneNode* menucubenode = 0;
IrrlichtDevice* device = 0;

class MyEventReceiver : public IEventReceiver
{
public:
	virtual bool OnEvent(SEvent event)
	{
		if (menucubenode != 0 && event.EventType == irr::EET_KEY_INPUT_EVENT && !event.KeyInput.PressedDown)
		{
			switch(event.KeyInput.Key)
			{
			case KEY_SPACE:
				{
					if (event.KeyInput.Key == KEY_KEY_W)
						menucubenode->drop();
				}
				return true;
			}
		}

		return false;
	}
};

#pragma comment(linker, "/SUBSYSTEM:windows /ENTRY:mainCRTStartup")

int main()
{
	MyEventReceiver receiver;

	IrrlichtDevice *device = createDevice(video::EDT_DIRECT3D8, dimension2d<s32>(640, 480), 32, false, false, false, &receiver);
	IVideoDriver* driver = device->getVideoDriver();
	ISceneManager* smgr = device->getSceneManager();
	ISoundEngine* sound = createIrrKlangDevice();

	device->setWindowCaption(L"Block Platformer");

	smgr->addLightSceneNode(0, vector3df(0,128,-128), SColor(255,255,255,255),100);

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

	IAnimatedMesh* menucubemesh = smgr->getMesh("./Data/menucube.3ds");
	IAnimatedMeshSceneNode* menucubenode = smgr->addAnimatedMeshSceneNode( menucubemesh );

	IAnimatedMesh* infomesh = smgr->getMesh("./Data/text.3ds");
	IAnimatedMeshSceneNode* infonode = smgr->addAnimatedMeshSceneNode( infomesh );

	if (menucubenode)
	{
		ISceneNodeAnimator* menuanim = smgr->createRotationAnimator(vector3df(0.1,0.1,0.1));
		menucubenode->addAnimator(menuanim);
		menuanim->drop();
	}

	if (infonode)
	{
	infonode->setMaterialFlag(EMF_LIGHTING, false);
	}

	if (gamemode == 0)
	{
		sound->play2D("./Data/title.mod");
		infonode->setPosition(vector3df(2.5,0,0));
	}

	while(device->run())
	{
		driver->beginScene(true, true, SColor(255,0,0,0));

		smgr->drawAll();

		driver->endScene();
	}
	device->drop();

	sound->drop();

	return 0;
}
Sorry for bad English and messy code and all, but I'm tired and I've been on this all night.

Just please anyone help.

Maybe I should just forget about this game and continue development on my operating system.
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

You are using two variables of the name menucubenode. The event receiver uses the global one (which is always 0), while the actual scene node is stored in the local variable of main().
AusZero
Posts: 11
Joined: Sat Oct 06, 2007 11:16 pm

Post by AusZero »

Still doesn't work.
I have tried to fix my code a little bit but it still doesn't work.

Code: Select all

#include "stdafx.h"

int gamemode = 0;

ISceneNode* eventnode = 0;
IrrlichtDevice* device = 0;

class MyEventReceiver : public IEventReceiver
{
public:
	virtual bool OnEvent(SEvent event)
	{
		if (eventnode != 0 && event.EventType == irr::EET_KEY_INPUT_EVENT && !event.KeyInput.PressedDown)
		{
			switch(event.KeyInput.Key)
			{
			case KEY_SPACE:
				{
					if (event.KeyInput.Key == KEY_SPACE)
						gamemode = 1;
				}
				return true;
			}
		}

		return false;
	}
};

#pragma comment(linker, "/SUBSYSTEM:windows /ENTRY:mainCRTStartup")

int main()
{
	MyEventReceiver receiver;

	IrrlichtDevice *device = createDevice(video::EDT_DIRECT3D8, dimension2d<s32>(640, 480), 32, false, false, false, &receiver);
	IVideoDriver* driver = device->getVideoDriver();
	ISceneManager* smgr = device->getSceneManager();
	ISoundEngine* sound = createIrrKlangDevice();

	device->setWindowCaption(L"Block Platformer");

	smgr->addLightSceneNode(0, vector3df(0,128,-128), SColor(255,255,255,255),100);

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

	IAnimatedMesh* menucubemesh = smgr->getMesh("./Data/menucube.3ds");
	IAnimatedMeshSceneNode* menucubenode = smgr->addAnimatedMeshSceneNode( menucubemesh );

	IAnimatedMesh* infomesh = smgr->getMesh("./Data/text.3ds");
	IAnimatedMeshSceneNode* infonode = smgr->addAnimatedMeshSceneNode( infomesh );

	if (menucubenode)
	{
		ISceneNodeAnimator* menuanim = smgr->createRotationAnimator(vector3df(0.1,0.1,0.1));
		menucubenode->addAnimator(menuanim);
		menuanim->drop();
	}

	if (infonode)
	{
	infonode->setMaterialFlag(EMF_LIGHTING, false);
	}

	if (gamemode == 0)
	{
		sound->play2D("./Data/title.mod");
		infonode->setPosition(vector3df(2.5,0,0));
	}

	while(device->run())
	{
		driver->beginScene(true, true, SColor(255,0,0,0));

		if (gamemode != 0)
		{
			menucubenode->drop();
			infonode->drop();
			sound->stopAllSounds();
		}

		smgr->drawAll();

		driver->endScene();
	}
	device->drop();

	sound->drop();

	return 0;
}
Please help.
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

Now the event receiver waits for eventnode becoming non-zero. But that will never happen.
Could you please take some more time and common sense to think twice about what you're really doing? If you cannot do it with just the code you should use a debugger and step through. This will show all values of your variables and you can learn how the program is really working.
AusZero
Posts: 11
Joined: Sat Oct 06, 2007 11:16 pm

Post by AusZero »

I finally got it to work! :D
Post Reply