What's wrong with my code?

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
Pythy Python
Posts: 30
Joined: Sun Mar 29, 2015 1:31 am

What's wrong with my code?

Post by Pythy Python »

I copied exact same code in tutorial 4: movement but it's not working. (If I press w s a or d, the sphere doesn't move...)


The only difference is that I didn't put the ninja model in the scene.

Code: Select all

 
#include "include/irrlicht.h"
 
using namespace irr;
 
 
class EventReceiver: public IEventReceiver{
    public:
        virtual bool OnEvent(const SEvent &event){
            if(event.EventType == irr::EET_KEY_INPUT_EVENT){
                keyIsDown[event.KeyInput.Key] == event.KeyInput.PressedDown;
                
                return false;
            }
        }
        
        virtual bool IsKeyDown(EKEY_CODE keyCode) const{
            return keyIsDown[keyCode];
        }
        
        EventReceiver(){
            for(u32 i = 0; i < KEY_KEY_CODES_COUNT; ++i){
                keyIsDown[i] = false;
            }
        }
        
        private:
            bool keyIsDown[KEY_KEY_CODES_COUNT];
};
 
 
int main(){
    EventReceiver receiver;
 
    IrrlichtDevice *device = createDevice(video::EDT_OPENGL, core::dimension2d<u32>(800, 600), 16, false, false, false, &receiver);
    
    if(!device){
        return -1;
    }
    
    video::IVideoDriver *driver = device->getVideoDriver();
    scene::ISceneManager *smgr = device->getSceneManager();
    
    scene::ISceneNode *node = smgr->addSphereSceneNode();
    
    if(node){
        node->setPosition(core::vector3df(0, 0, 30));
        node->setMaterialTexture(0,driver->getTexture("wall.bmp"));
        node->setMaterialFlag(video::EMF_LIGHTING, false);
    }
    
    scene::ISceneNode *cube = smgr->addCubeSceneNode();
    
    if(cube){
        cube->setMaterialTexture(0, driver->getTexture("t351sml.jpg"));
        cube->setMaterialFlag(video::EMF_LIGHTING, false);
        scene::ISceneNodeAnimator *anim = smgr->createFlyCircleAnimator(core::vector3df(0,0,30), 20.0f);
        
        if (anim)
        {
            cube->addAnimator(anim);
            anim->drop();
        }
    }
 
    smgr->addCameraSceneNodeFPS();
    device->getCursorControl()->setVisible(false);
    
    int lastFPS = -1;
    
    u32 then = device->getTimer()->getTime();
    const f32 MOVEMENT_SPEED = 5.f;
    
    while(device->run()){
        const u32 now = device->getTimer()->getTime();
        const f32 frameDeltaTime = (f32)(now - then) / 1000.f;
        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;
        }
        else 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);
        
        driver->beginScene(true, true, video::SColor(255, 113, 113, 113));
        smgr->drawAll();
        driver->endScene();
        
        int fps = driver->getFPS();
        
        
        if(lastFPS != fps){
            core::stringw tmp(L"Movement [");
            tmp += driver->getName();
            tmp += L"] fps: ";
            tmp += fps;
            
            device->setWindowCaption(tmp.c_str());
            lastFPS = fps;
        }
        
    }
    
    device->drop();
 
    
    return 0;
}
 

Why is this not working?
mongoose7
Posts: 1227
Joined: Wed Apr 06, 2011 12:13 pm

Re: What's wrong with my code?

Post by mongoose7 »

This statement does nothing:

keyIsDown[event.KeyInput.Key] == event.KeyInput.PressedDown;
Pythy Python
Posts: 30
Joined: Sun Mar 29, 2015 1:31 am

Re: What's wrong with my code?

Post by Pythy Python »

Thanks it worked. But my FPS camera doesn't work either...funny thing is that if I remove

Code: Select all

&receiver
at IrrlichtDevice, it works.

Code: Select all

 
IrrlichtDevice *device = createDevice(video::EDT_OPENGL, core::dimension2d<u32>(800, 600), 16, false, false, false /*&receiver*/);
 
Do you know how to fix this? thanks.
mongoose7
Posts: 1227
Joined: Wed Apr 06, 2011 12:13 pm

Re: What's wrong with my code?

Post by mongoose7 »

CuteAlien knows. I think you have to return false from your event receiver. I notice you do not have a return when the 'if' is not true. I guess you ignore compiler warnings? (I think the compiler would have warned you about the other problem too, but I guess you know better?)
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: What's wrong with my code?

Post by CuteAlien »

Hehe, I think you found it already mongoose :-)
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Post Reply