Page 1 of 1

Input is getting on my nerves.

Posted: Fri Feb 10, 2006 8:12 am
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

Posted: Fri Feb 10, 2006 9:28 am
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.

Posted: Fri Feb 10, 2006 9:47 am
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"