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;
}