How to Move MyEventReceiver into a separate file
Posted: Mon Dec 15, 2014 12:16 am
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
Now my cpp file
MyEventReceiver.cpp
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
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;
}
}