Input is getting on my nerves.

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
Fhqwhgads
Posts: 42
Joined: Tue Jun 21, 2005 1:43 am

Input is getting on my nerves.

Post by Fhqwhgads »

Ok, I have GameCore class which handels EVERYTHING. I have three states in my game

1) Main Menu
2) Game
3) Intermission

On both Main Menu and Intermission I need it to check if RETURN is pressed, and if so, run a function in class GameCore. Now in EventRecieve.h(my code) I can't include GameCore.h because it would then in turn re-include EventReciever.h and loop.How can I get it so if a key is pressed, run a function in GameClass.h?

Code: Select all

#include "headers.h"
#include "EventReceiver.h"

class GameCore
{
public:
	GameCore();
	~GameCore();

	void gameLoop();
	void createLevel();
private:
	IrrlichtDevice* irrDevice;
	IVideoDriver* irrDriver;
	ITexture* sprites;
	ITexture* map;
	IGUIFont* font;	
	ITimer* timer;
	MyEventReceiver* events;

	int level;
	int gameMode;
};	

Code: Select all

#include "headers.h"

class MyEventReceiver : public IEventReceiver
{
public:

	
	virtual bool onEvent(SEvent event)
	{
		if (event.EventType == EET_KEY_INPUT_EVENT)
		{
			if(event.KeyInput.Key == KEY_RETURN){

			}
			
		}

	}
};

Code: Select all

#ifndef Irr
#define Irr
#include <irrlicht.h>
#include <iostream>
#include <vector>


using std::vector;
using namespace std;
using namespace irr;
using namespace io;
using namespace core;
using namespace video;
using namespace gui;

#pragma comment(lib, "Irrlicht.lib")
#ifdef WIN32
#pragma comment(linker, "/subsystem:windows /entry:main")
#endif
#endif
Warchief
Posts: 204
Joined: Tue Nov 22, 2005 10:58 am

Post by Warchief »

Common solution:

Code: Select all

class MyEventReceiver : public IEventReceiver
{
private:
    bool events[NUM_EVENTS]; // Stores true if event has been fired

public:
   virtual bool onEvent(SEvent event)
   {
      if (event.EventType == EET_KEY_INPUT_EVENT)
      {
         if(event.KeyInput.Key == KEY_RETURN){
            if(!events[RET_PRESS]) // Lock (sync for threads)
            events[RET_PRESS] = true; // Fire
         }
      }
   } 
}

class GameCore
{
public: 
 run() {
    checkForEvents(); // Every frame
 }

 checkForEvents()
 {
    if myEventRecevier->Activated(RET_PRESS) {
        func(); // Call to your function
        myEventRecevier->Invalidate(RET_PRESS);
    }
 }
}

*RET_PRESS and NUM_EVENTS have to be declared.
Ced666
Posts: 86
Joined: Fri Jan 13, 2006 10:29 am
Location: Belgium

Post by Ced666 »

On both Main Menu and Intermission I need it to check if RETURN is pressed, and if so, run a function in class GameCore. Now in EventRecieve.h(my code) I can't include GameCore.h because it would then in turn re-include EventReciever.h and loop.How can I get it so if a key is pressed, run a function in GameClass.h?
Don't know if I really understood your problem, but the solution is to use forward declaration. In your GameCore.h file instead of including "EventReceive.h", just use a forward declaration of MyEventReceiver and include the file in your cpp file instead:

Code: Select all

#include "headers.h" 

class MyEventReceiver;

class GameCore 
{ 
public: 
   GameCore(); 
   ~GameCore(); 

   void gameLoop(); 
   void createLevel(); 
private: 
   IrrlichtDevice* irrDevice; 
   IVideoDriver* irrDriver; 
   ITexture* sprites; 
   ITexture* map; 
   IGUIFont* font;    
   ITimer* timer; 
   MyEventReceiver* events; 

   int level; 
   int gameMode; 
};    
And in your cpp file, include "EventReceiver.h"
Post Reply