keyboard class

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
Asimov
Posts: 246
Joined: Thu Dec 04, 2014 7:41 pm
Contact:

keyboard class

Post by Asimov »

Hi all,

I have just learnt how to get keys working, which is cool. I can now rotate a model using keys.
eg

Code: Select all

 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;
 
 
        if(receiver.IsKeyDown(irr::KEY_KEY_A))
            {
                rotation+=50 * frameDeltaTime;
            }
 
        if(receiver.IsKeyDown(irr::KEY_KEY_D))
            {
                rotation-=50 * frameDeltaTime;
            }
 
        node->setRotation(core::vector3df(0,rotation,0));
        driver->beginScene(true,true,video::SColor(100,100,100,255));
 
        smgr->drawAll();
        guienv->drawAll();
        driver->endScene();
    }
    device->drop();
    return 0;
 
}
 
But I like to streamline my code. Basically instead of

Code: Select all

    if(receiver.IsKeyDown(irr::KEY_KEY_A))
            {
                rotation+=50 * frameDeltaTime;
            }
 
        if(receiver.IsKeyDown(irr::KEY_KEY_D))
            {
                rotation-=50 * frameDeltaTime;
            }
I want to add a method to the keyboard listener class something like check keys
Something like MyEventReceiver::Checkkeys();

but I know I would have to put something between the brackets so that it knows which key I am pressing eg
MyEventReceiver::Checkkeys(current key being pressed);

Yes I know I can't put that. It will probably be some pointer of the keyboard I have to pass into my method.

I hope you can understand what I am going on about heh heh.

My method will probably be something like this

Code: Select all

 
Checkkeys(passed keyboard device or pointer)
{
if(receiver.IsKeyDown(irr::KEY_KEY_A))
            {
                rotation+=50 * frameDeltaTime;
            }
 
        if(receiver.IsKeyDown(irr::KEY_KEY_D))
            {
                rotation-=50 * frameDeltaTime;
            }
}
 
then in my game loop I would only have one line instead of having lots of key reference lines.
I will probably need to pass a pointer to the rotation variable also.

PS just tried this

Code: Select all

void Checkkeys(MyEventReceiver *receiver,const *frameDeltaTime,float *rotation)
    {
         if(receiver.IsKeyDown(irr::KEY_KEY_D))
            {
                rotation-=50 * frameDeltaTime;
            }
    }
Obviously didn't work LOL
PPS
So I have tried this

Code: Select all

 
void Checkkeys(MyEventReceiver *receiver,float *frameDeltaTime,float *rotation)
    {
         if(receiver->IsKeyDown(irr::KEY_KEY_D))
            {
               rotation-=50 * frameDeltaTime;
            }
    }
 
Right I got it to compile if I comment out the rotation line, and if I have it in I get an error
H:\Codeblock_Games\Irrlicht\MyTests\main.cpp|45|error: invalid operands of types 'int' and 'float*' to binary 'operator*'|

I don't understand the error as rotation is a float, but I think I am getting closer to working this out.
Asimov
Posts: 246
Joined: Thu Dec 04, 2014 7:41 pm
Contact:

Re: keyboard class

Post by Asimov »

Hi all,

If you have read this, thanks, but I have solved the problem. It took me a while because my C++ is rusty, but I remembered a few lessons about sending pointers.
I am actually glad I solved this on my own. Here is my solution

Code: Select all

void Checkkeys(MyEventReceiver *receiver,const f32 *frameDeltaTime,float *rotation)
    {
         if(receiver->IsKeyDown(irr::KEY_KEY_D))
            {
               *rotation-=50 * *frameDeltaTime;
            }
    }
And I call my keyboard method in the game loop like this

Code: Select all

 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;
 
receiver.Checkkeys(&receiver,&frameDeltaTime,&rotation);
 
        node->setRotation(core::vector3df(0,rotation,0));
        driver->beginScene(true,true,video::SColor(100,100,100,255));
 
        smgr->drawAll();
        guienv->drawAll();
        driver->endScene();
    }
Now I am going to move the MyEventReceiver into a separate header and cpp file.
I love it when I work out a problem on my own.

PS. It took me about 2 hours because I couldn't get it working at first, but now I have managed to move the MyEventReceiver into a separate file.
I would post it here, but I think it would be more useful in the snippets section. So here is the link
http://irrlicht.sourceforge.net/forum/v ... 06#p291006
thanhle
Posts: 325
Joined: Wed Jun 12, 2013 8:09 am

Re: keyboard class

Post by thanhle »

Maybe try not to use a float as a pointer in your function argument.
Asimov
Posts: 246
Joined: Thu Dec 04, 2014 7:41 pm
Contact:

Re: keyboard class

Post by Asimov »

Hi thanhle,

I have removed the pointers from frameDelaTime and receiver now. I have kept the pointer for rotation because this is just at test and not an actual game.
I know what kind of game I want to make and learning how to do the things to get that game done. When I get to do the actual game the rotation variable will be in the class also,
but maybe not this class heh heh.

Actually I have added my new code here http://irrlicht.sourceforge.net/forum/v ... 06#p291006
thanhle
Posts: 325
Joined: Wed Jun 12, 2013 8:09 am

Re: keyboard class

Post by thanhle »

Hi mate,
I'm still at the learning stage as well.
Although I have very little time to do that atm.

Goodluck with learning mate.
Regards

Thanh
Post Reply