How to Move MyEventReceiver into a separate file

Post those lines of code you feel like sharing or find what you require for your project here; or simply use them as tutorials.
Post Reply
Asimov
Posts: 246
Joined: Thu Dec 04, 2014 7:41 pm
Contact:

How to Move MyEventReceiver into a separate file

Post by Asimov »

HI all,

It took me about two hours to get this working, with my rusty knowledge of C++.
Personally I like to work in an object orientated way if I can. The only thing I don't like doing is keeping all my classes in the same file as the main.cpp.
Basically all I like to keep in there is the game loop and calls to my other classes. I have failed so far on a few aspects of this, but I got the MyEventReceiver to work
in a separate file. Now I may have to add to this class later, as I am not sure if the original code does the mouse input or anything yet.

The other reason I keep things in separate files is that if I need to do a new project I can just copy over the class files to a new project.

The original class which I used I copied from one of the tutorials, so I am not saying this is my code, but the implementation into separate files was all my doing, and of course the one method is mine.

Bear in mind in the main.cpp I iniatialise this class with MyEventReceiver receiver; like normal.
I do have a float for rotation which I set up too eg float: rotation=0; but that is only applicable to my test project. I left the function in for people who don't know how to do this.
If anyone finds a problem with my code then let me know. I won't be offended.

I believe it would be helpful if you had stuff like this in the tutorials.
Next I want to do a separate class for node creation and mesh loading.

Ok first my header file
MyEventReceiver.h

Code: Select all

#pragma once
#ifndef MYEVENTRECEIVER_H
#define MYEVENTRECEIVER_H
 
#include <Irrlicht.h>
 
 
using namespace irr;
 
class MyEventReceiver : public IEventReceiver
{
    public:
        MyEventReceiver();
        virtual bool OnEvent(const SEvent& event);
        virtual bool IsKeyDown(EKEY_CODE keyCode) const;
// This is my method
        void Checkkeys(MyEventReceiver *receiver,const f32 *frameDeltaTime,float *rotation);
 
    private:
        bool KeyIsDown[KEY_KEY_CODES_COUNT];
};
 
#endif // MYEVENTRECEIVER_H
 
Now my cpp file
MyEventReceiver.cpp

Code: Select all

#include "MyEventReceiver.h"
 
MyEventReceiver::MyEventReceiver()
{
    for (u32 i=0; i<KEY_KEY_CODES_COUNT; ++i)
            KeyIsDown[i] = false;
}
 
 bool MyEventReceiver::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
    bool MyEventReceiver::IsKeyDown(EKEY_CODE keyCode) const
    {
        return KeyIsDown[keyCode];
    }
 // This is my method for rotating my game mesh
    void MyEventReceiver::Checkkeys(MyEventReceiver *receiver,const f32 *frameDeltaTime,float *rotation)
    {
         if(receiver->IsKeyDown(irr::KEY_KEY_D))
            {
               *rotation-=50 * *frameDeltaTime;
            }
            if(receiver->IsKeyDown(irr::KEY_KEY_A))
            {
                *rotation+=50 * *frameDeltaTime;
            }
    }
   
 
Last edited by Asimov on Mon Dec 15, 2014 7:06 pm, edited 1 time in total.
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: How to Move MyEventReceiver into a separate file

Post by CuteAlien »

I appreciate that you try to help the community. But I'd recommend to wait with posting code-snippets until you are a little bit more familiar with c++ yourself. This are c++ basics which have not to do too much with the engine. I'm sure it also can help someone. But there is another problem, being relatively new to c++ you do minor strange things even in short codes which are not exactly good examples. For example in the code above:
- You include <Irrlicht.h>, but it's irrlicht.h not Irrlicht.h - case matters depending on the filesystem you are on. Also it's generally better to use "" instead of <> for headers which are not in the standard library.
- You did put a using namespace in the header. That's valid c++ as in "it compiles". But it's something you should never do! Because now you have polluted the namespace of every files which includes that header. Which defeats the idea of using namespaces in the first place. So only do that in .cpp files and write out the complete namespaces in the header files (so it's irr::IEventReceiver and irr::SEvent in the header).
- Working with pointers for the float's in Checkkeys is very unusual for c++. frameDeltaTime could be passed directly - no need to use a pointer. rotation looks like it would make more sense as a member variable. Or if you really want to pass it around then a c++ programmer would generally use a reference for that case to show the intent that this is a return variable more clearly.
- You use the virtual keyword a little bit randomly. It's very likely you wouldn't use it for IsKeyDown - or you would use it for Checkkeys (less likely in this case - this kinda depends on wether your write a framework or an application class). It's not that virtual makes it wrong - it always works - but it's less effective which is something you likely wouldn't want in the case of IsKeyDown. Really a minor point.
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
Asimov
Posts: 246
Joined: Thu Dec 04, 2014 7:41 pm
Contact:

Re: How to Move MyEventReceiver into a separate file

Post by Asimov »

Hi CuteAlien,
I appreciate that you try to help the community. But I'd recommend to wait with posting code-snippets until you are a little bit more familiar with c++ yourself. This are c++ basics which have not to do too much with the engine. I'm sure it also can help someone. But there is another problem, being relatively new to c++ you do minor strange things even in short codes which are not exactly good examples. For example in the code above:
Sorry I was so excited about getting it to work on my own without any help, I thought it might help someone else.

Code: Select all

You include <Irrlicht.h>, but it's irrlicht.h not Irrlicht.h - case matters
Strangely enough I knew this one, but it is just a typo, and not intentional.
instead of <> for headers which are not in the standard library
Normally I do use "" for headers, but I was following the book "Irrlicht 1.7 Realtime 3D Engine for beginners Guide" on page 56 it says to use #include <irrlicht.h>, but obviously if it is wrong I will do it with "". As for namespaces I did use them a long time ago, but it has been 2 years since I have done any C++.
Working with pointers for the float's in Checkkeys is very unusual for c++. frameDeltaTime could be passed directly - no need to use a pointer. rotation looks like it would make more sense as a member variable. Or if you really want to pass it around then a c++ programmer would generally use a reference for that case to show the intent that this is a return variable more clearly.
Yes I agree I would have made rotation a member variable if this was anything more than a test, but seeing as I was testing the keys I thought this was ok for test purposes.
You were totally right about the frameDeltaTime. I have now managed to pass them in without using a pointer. I did try originally without a pointer, but I missed something out and tried the pointer method. I have also managed to pass in the receiver without a pointer too now.

Code: Select all

You use the virtual keyword a little bit randomly. It's very likely you wouldn't use it for IsKeyDown - or you would use it for Checkkeys (less likely in this case - this kinda depends on wether your write a framework or an application class). It's not that virtual makes it wrong - it always works - but it's less effective which is something you likely wouldn't want in the case of IsKeyDown. Really a minor point.
To be honest normally I would never ever use virtual bool, as I don't know what it does. This was from the original code from the tutorial. I mean to find out what the difference between a virtual bool and a bool later however. I know what a bool is, but not sure what a virtual bool is.

I have now updated my header, so I think it should be right now. I haven't changed the virtual bools because, because until I understand why they were in the original tutorial, I haven't removed them yet. I won't post any more code in this section if that is ok. I just thought I should try and put right my original post. Thank you for your comments, I have learnt a lot from them.

Code: Select all

 
#pragma once
#ifndef MYEVENTRECEIVER_H
#define MYEVENTRECEIVER_H
 
#include "irrlicht.h"
 
 
class MyEventReceiver : public irr::IEventReceiver
{
    public:
        MyEventReceiver();
        virtual bool OnEvent(const irr::SEvent& event);
        virtual bool IsKeyDown(irr::EKEY_CODE keyCode) const;
        void Checkkeys(MyEventReceiver receiver,const irr::f32 frameDeltaTime,float *rotation);
        
    private:
        bool KeyIsDown[irr::KEY_KEY_CODES_COUNT];
};
 
#endif // MYEVENTRECEIVER_H
 
and my new cpp

Code: Select all

#include "MyEventReceiver.h"
 
MyEventReceiver::MyEventReceiver()
{
    for (irr::u32 i=0; i<irr::KEY_KEY_CODES_COUNT; ++i)
            KeyIsDown[i] = false;
}
 
 bool MyEventReceiver::OnEvent(const irr::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
    bool MyEventReceiver::IsKeyDown(irr::EKEY_CODE keyCode) const
    {
        return KeyIsDown[keyCode];
    }
 
    void MyEventReceiver::Checkkeys(MyEventReceiver receiver,const irr::f32 frameDeltaTime,float *rotation)
    {
         if(receiver.IsKeyDown(irr::KEY_KEY_D))
            {
               *rotation-=50 * frameDeltaTime;
            }
            if(receiver.IsKeyDown(irr::KEY_KEY_A))
            {
                *rotation+=50 * frameDeltaTime;
            }
 
    }
 
 
 
juanjpro
Posts: 12
Joined: Mon Nov 24, 2014 12:54 pm

Re: How to Move MyEventReceiver into a separate file

Post by juanjpro »

Asimov wrote:I know what a bool is, but not sure what a virtual bool is.
"virtual" does not affect the type returned by the function, it's to allow derived classes to redefine inherited functions. http://www.cplusplus.com/doc/tutorial/p ... al_members
Asimov
Posts: 246
Joined: Thu Dec 04, 2014 7:41 pm
Contact:

Re: How to Move MyEventReceiver into a separate file

Post by Asimov »

Hi juanjpro,

Ahh thanks I understand now. It is due to inheritence again. So the new method is derived from a method in the master class IEventReceiver and changes the way it functions in some way.
I have tended to avoid inheritence because I have never found a good use for it, eh until now heh heh
kormoran
Posts: 47
Joined: Mon Dec 28, 2015 4:50 pm
Location: Tolentino

Re: How to Move MyEventReceiver into a separate file

Post by kormoran »

Thank you Asimov, just what I needed :o
Mel
Competition winner
Posts: 2292
Joined: Wed May 07, 2008 11:40 am
Location: Granada, Spain

Re: How to Move MyEventReceiver into a separate file

Post by Mel »

Perhaps you want to extent this event receiver to handle mouse (screen coordinates, button presses) and controller inputs :) And even do more stuff than just controlling the keypresses (key releases, control, shift and alt states... etc)
"There is nothing truly useless, it always serves as a bad example". Arthur A. Schmitt
Asimov
Posts: 246
Joined: Thu Dec 04, 2014 7:41 pm
Contact:

Re: How to Move MyEventReceiver into a separate file

Post by Asimov »

I did add some more stuff to it, but I have been too busy with 3d modelling on a team based project recently to do any programming myself. I do love programming, but I love 3D modellng more. I will get back to it some time.
Post Reply