A little event receiver help.

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
Stu L Tissimus
Posts: 48
Joined: Thu Jul 15, 2004 8:45 pm
Location: Manhattan
Contact:

A little event receiver help.

Post by Stu L Tissimus »

How could I tell my event receiver to do something when I press, say, "W"? I currently have my event receiver using keycodes, but can I tell it to do something on "W" instead of the keycode 87?

Relevent code:

Code: Select all

   virtual bool OnEvent(SEvent event)
   {
    switch ( event.EventType ) {
        
        
       case EET_KEY_INPUT_EVENT: {
             m_key_buffer[event.KeyInput.Key] = event.KeyInput.PressedDown;
             
             switch (event.KeyInput.Key) {
                 case 87: cout << "whoamg its W" << endl; break;
                 case 65: cout << "y helo thar A" << endl; break;
                 case 83: cout << "O_O S" << endl; break;
                 case 68: cout << "I'm D. D for YOU!" << endl; break;
             }
                 
             return true; }
Laptop: P-M 1.6GHz|1024MB PC2800|Mob.Radeon9700Pro|60GB

Desktop: A-XP 2400+|512MB PC3200|Radeon 9700 Pro|180GB
digfarenough
Posts: 39
Joined: Sat Oct 30, 2004 4:35 pm
Location: Boston, MA
Contact:

Post by digfarenough »

can't you just use case 'W'? I often get C/C++ and MATLAB mixed up, but I think that works in C...
Spintz
Posts: 1688
Joined: Thu Nov 04, 2004 3:25 pm

Post by Spintz »

Look in Keycodes.h

You would use KEY_KEY_W for the W key. They are all listed in there.
Stu L Tissimus
Posts: 48
Joined: Thu Jul 15, 2004 8:45 pm
Location: Manhattan
Contact:

Post by Stu L Tissimus »

Ah, thank you!
Laptop: P-M 1.6GHz|1024MB PC2800|Mob.Radeon9700Pro|60GB

Desktop: A-XP 2400+|512MB PC3200|Radeon 9700 Pro|180GB
Stu L Tissimus
Posts: 48
Joined: Thu Jul 15, 2004 8:45 pm
Location: Manhattan
Contact:

Post by Stu L Tissimus »

Now, question for you. I'm trying to make my program read something from an XML file, and then use the variable it gets from that in the event receiver. I want to keep the XML reader in a seperate file from my main.cpp and my event receiver header. Buit I can't exactly figure out how I could do this. >_>

It would be like this:
-XMLReader.h contains a void that opens up and parses my SoldierKeys.xml file.
-MyEventReceiver.h will #include XMLReader.h and use a variable retrieved from the XML as a key.
-Main.cpp would #include MyEventReceiver, bladeebladeebla.

I'm not sure how to do this, however, because my XML reader opens files with an IrrlichtDevice. Here, let me post the stuff...

XMLReader.h
[code#ifndef __XMLKEYREADER_H__
#define __XMLKEYREADER_H__
#include "Irrlicht.h"
using namespace irr;
using namespace std;

class XMLKeyReader {

private:
core::stringw Up1; core::stringw Up2;
core::stringw Left1; core::stringw Left2;
core::stringw Right1; core::stringw Right2;
core::stringw Down1; core::stringw Down2;


public:
void ReadSoldierKeys {
io::IXMLReader* xml = device->getFileSystem()->createXMLReader("SoldierKeys.xml");

core::stringw messageText;
core::stringw caption;

while(xml && xml->read())
{
switch(xml->getNodeType())
{
case io::EXN_TEXT:
// in this xml file, the only text which occurs is the messageText
messageText = xml->getNodeData();
break;
case io::EXN_ELEMENT:
{
if (core::stringw("MovementButtons") == xml->getNodeName())
Up1 = xml->getAttributeValue(L"Up1");
else
if (core::stringw("messageText") == xml->getNodeName())
caption = xml->getAttributeValue(L"caption");
}
break;
}
}

if (xml)
xml->drop();
}

#endif[/code]

MyEventReceiver.h

Code: Select all

#ifndef __MYEVENTRECEIVER_H__
#define __MYEVENTRECEIVER_H__
#include "Irrlicht.h"
#include <iostream>

using namespace std;
using namespace irr;

const int MOUSE_BUTTON_CODE_COUNT = 3; 

class MyEventReceiver : public IEventReceiver {
private:
   core::vector2di m_mouseLocation;
   bool            m_mouseMoved;
   f32             m_mouseWheelValue;
   bool            m_key_buffer[KEY_KEY_CODES_COUNT];
   bool            m_key_buffer_toggle[KEY_KEY_CODES_COUNT];
   bool            m_mouse_buffer[MOUSE_BUTTON_CODE_COUNT];
   bool            m_mouse_buffer_toggle[MOUSE_BUTTON_CODE_COUNT]; 
    


public:
   
   virtual bool OnEvent(SEvent event)
   {
    switch ( event.EventType ) {
        
        
       case EET_KEY_INPUT_EVENT: {
             m_key_buffer[event.KeyInput.Key] = event.KeyInput.PressedDown;
             
             switch (event.KeyInput.Key) {
                 case Up1: cout << "whoamg its W, lol" << endl; break;
                 case 65: cout << "y helo thar A" << endl; break;
                 case 83: cout << "O_O S" << endl; break;
                 case 68: cout << "I'm D. D for YOU!" << endl; break;
             }
                 
             return true; }
             
       case EET_MOUSE_INPUT_EVENT:{
             if ( event.MouseInput.Event == EMIE_MOUSE_WHEEL )
                m_mouseWheelValue = event.MouseInput.Wheel;
    
             if ( event.MouseInput.Event == EMIE_MOUSE_MOVED ) {
                m_mouseLocation.X = event.MouseInput.X;
                m_mouseLocation.Y = event.MouseInput.Y;
                m_mouseMoved = true; }
    
             if ( event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN )
                m_mouse_buffer[EMIE_LMOUSE_PRESSED_DOWN] = true;
             else if ( event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP )
                m_mouse_buffer[EMIE_LMOUSE_PRESSED_DOWN] = false;
    
             if ( event.MouseInput.Event == EMIE_RMOUSE_PRESSED_DOWN  )
                m_mouse_buffer[EMIE_RMOUSE_PRESSED_DOWN ] = true;
             else if ( event.MouseInput.Event == EMIE_RMOUSE_LEFT_UP )
                m_mouse_buffer[EMIE_RMOUSE_PRESSED_DOWN] = false;
    
             if ( event.MouseInput.Event == EMIE_MMOUSE_PRESSED_DOWN  )
                m_mouse_buffer[EMIE_MMOUSE_PRESSED_DOWN ] = true;
             else if ( event.MouseInput.Event == EMIE_MMOUSE_LEFT_UP )
                m_mouse_buffer[EMIE_MMOUSE_PRESSED_DOWN] = false;
    
             return true;
          }
       } 

      return false;
   }
};


#endif // __MYEVENTRECEIVER_H__
main.cpp

Code: Select all

#include <iostream>
#include <fstream>
#include <vector>

#include "MyEventReceiver.h"

#include "Irrlicht.h"
#pragma comment(lib, "Irrlicht.lib")

using namespace irr;

int main() {
    MyEventReceiver receiver; 
    
    IrrlichtDevice *device = createDevice(
        video::EDT_OPENGL, core::dimension2d<s32>(640, 480), 16, false, false, false, &receiver);
    
    device->getSceneManager();

    video::IVideoDriver* driver = device->getVideoDriver();
	scene::ISceneManager* smgr = device->getSceneManager();
	
	device->getFileSystem()->addZipFileArchive("map-20kdm2.pk3");
	
	scene::IAnimatedMesh* mesh = smgr->getMesh("20kdm2.bsp");
	scene::IAnimatedMeshSceneNode* node;
	
	scene::IAnimatedMesh* mesh2 = smgr->getMesh("Soldier9.3ds");
	scene::IAnimatedMeshSceneNode* node2;
	
	scene::ISceneNode* camera = smgr->addCameraSceneNodeFPS();
	scene::ISceneNode* camera2 = smgr->addCameraSceneNodeFPS();
		
	int x = 1;
	if (mesh)
	    if (x == 1) {
	    node = smgr->addAnimatedMeshSceneNode(mesh, camera, -1, core::vector3df(10, 0, 0));
	    node2 = smgr->addAnimatedMeshSceneNode(mesh2, camera, -1, core::vector3df(500, 0, 0));}
	    else {
	    node = smgr->addAnimatedMeshSceneNode(mesh, camera, -1, core::vector3df(-500, 0, 0));
	    node2 = smgr->addAnimatedMeshSceneNode(mesh2, camera, -1, core::vector3df(500, 0, 0)); }
		device->getCursorControl()->setVisible(false);
	int lastFPS = -1;
	
	
	
	
	
    io::IXMLReader* xml = device->getFileSystem()->createXMLReader("SoldierKeys.xml");
    
    core::stringw messageText;
    core::stringw caption;
    core::stringw Up1;
    
    while(xml && xml->read())
    {
            switch(xml->getNodeType())
            {
            case io::EXN_TEXT:
                    // in this xml file, the only text which occurs is the messageText
                    messageText = xml->getNodeData();
                    break;
            case io::EXN_ELEMENT:
                    if (core::stringw("MovementButtons") == xml->getNodeName())
                            Up1 = xml->getAttributeValue(L"Up1");
                    else
                    if (core::stringw("messageText") == xml->getNodeName())
                            caption = xml->getAttributeValue(L"caption");
                    break;
            }
    }
    
    if (xml)
            xml->drop(); // don't forget to delete the xml reader 
	
	

	
	

	while(device->run())
	{
        driver->beginScene ( true, true, video::SColor ( 0, 0, 0, 0 ) );
        smgr->drawAll ( );
        driver->endScene ( ); 
		int fps = driver->getFPS();
		if (lastFPS != fps)
		{
			core::stringw str = L"Irrlicht Engine - Vertex and pixel shader example [";
			str += driver->getName();
			str += "] FPS:";
			str += fps;

			device->setWindowCaption(str.c_str());
			lastFPS = fps;
		}
		
		
	}
	device->drop();
	
	return 0;
}
Laptop: P-M 1.6GHz|1024MB PC2800|Mob.Radeon9700Pro|60GB

Desktop: A-XP 2400+|512MB PC3200|Radeon 9700 Pro|180GB
digfarenough
Posts: 39
Joined: Sat Oct 30, 2004 4:35 pm
Location: Boston, MA
Contact:

Post by digfarenough »

so I'm gonna trust you actually have a good reason in mind to use an eventreceiver to parse xml data (why not just write a for loop or something to parse it?)

anyhow, you might want to use the extern keyword on the variables you want to share.. in main.cpp you can create an instance of your xmlreader normally, then in the event receiver file put something like "extern XMLKeyReader xmlkr" so when it's compiled, references to the variable in that file will know to use the variable you declared in the first file (it's declared externally, as it were)
Last edited by digfarenough on Wed Jan 05, 2005 3:50 pm, edited 1 time in total.
Spintz
Posts: 1688
Joined: Thu Nov 04, 2004 3:25 pm

Post by Spintz »

You'd want to create a new class, that has things common between Main, EventReceiver, XMLReader. ( Such as maintaining pointers to your Irrlicht device, sccene manager, etc. )
Stu L Tissimus
Posts: 48
Joined: Thu Jul 15, 2004 8:45 pm
Location: Manhattan
Contact:

Post by Stu L Tissimus »

Hmmm... Good idea. I for one have a lot of troulbe with pointers and things..... Mind giving me an example of a class that includes a device pointer that I can then build off of?

Thanks a lot, Stu.
Laptop: P-M 1.6GHz|1024MB PC2800|Mob.Radeon9700Pro|60GB

Desktop: A-XP 2400+|512MB PC3200|Radeon 9700 Pro|180GB
Spintz
Posts: 1688
Joined: Thu Nov 04, 2004 3:25 pm

Post by Spintz »

CGameCore.h

Code: Select all

#ifndef __CGAMECORE_H__
#define __CGAMECORE_H__

class CEventReceiver;

namespace gamecore
{
class CGameCore
{
public:
	~CGameCore ( );

	static CGameCore* GetInstance ( );
	static void DropInstance ( );

	bool InitDevice ( );

	IrrlichtDevice*					GetDevice ( ) { return m_device; }
	video::IVideoDriver*			GetVideoDriver ( ) { return m_videoDriver; }
	scene::ISceneManager*			GetSceneManager ( ) { return m_smgr; }
	gui::IGUIEnvironment*			GetGUIEnvironment ( ) { return m_guiEnvironment; }
	CEventReceiver*					GetReceiver ( ) { return m_receiver; }

	int								GetTime ( ) { return m_timer->getTime ( ); }
	core::rect<s32>					GetViewPort ( ) { return m_videoDriver->getViewPort ( ); }
	int								GetViewPortWidth ( ) { return m_videoDriver->getViewPort ( ).getWidth ( ); }
	int								GetViewPortHeight ( ) { return m_videoDriver->getViewPort ( ).getHeight ( ); }
	
	void SetDeviceType ( video::E_DRIVER_TYPE deviceType ) { m_deviceType = deviceType; }
	void SetWindowSize ( core::dimension2d<s32> windowSize ) { m_windowSize = windowSize; }
	void SetBits ( u32 bits ) { m_bits = bits; }
	void SetFullscreen ( bool fullscreen ) { m_fullscreen = fullscreen; }
	void SetStencilBuffer ( bool stencilBuffer ) { m_stencilBuffer = stencilBuffer; }
	void SetVSync ( bool vsync ) { m_vsync = vsync; }

private:
	CGameCore ( );

	static CGameCore* m_instance;

	IrrlichtDevice*			m_device;
	scene::ISceneManager*	m_smgr;
	video::IVideoDriver*	m_videoDriver;
	gui::IGUIEnvironment*	m_guiEnvironment;
	ITimer*					m_timer;
	CEventReceiver*			m_receiver;

	video::E_DRIVER_TYPE	m_deviceType;
	core::dimension2d<s32>	m_windowSize;
	u32						m_bits;
	bool					m_fullscreen;
	bool					m_stencilBuffer;
	bool					m_vsync;
};
} // namespace gamecore
#endif // _CGAMECORE_H_
CGameCore.cpp

Code: Select all

#include "CGameCore.h"
#include "CEventReceiver.h"

using namespace gamecore;

CGameCore* CGameCore::m_instance = NULL;

CGameCore* CGameCore::GetInstance ( )
{
	if ( !m_instance )
		m_instance = new CGameCore ( );

	return m_instance;
}

void CGameCore::DropInstance ( )
{
	if ( m_instance )
		delete m_instance;
}

CGameCore::CGameCore ( )
{
	m_device = NULL;
	m_smgr = NULL;
	m_videoDriver = NULL;
	m_guiEnvironment = NULL;
	m_timer = NULL;
	m_receiver = NULL;

	m_deviceType = video::EDT_DIRECTX9;
	m_windowSize = core::dimension2d<s32> ( 1024, 768 );
	m_bits = 32;
	m_fullscreen = false;
	m_stencilBuffer = false;
	m_vsync = false;
	m_receiver = NULL;
}

CGameCore::~CGameCore ( )
{
	if ( m_receiver )
		delete m_receiver;
}

bool CGameCore::InitDevice ( )
{
	m_receiver = new CEventReceiver ( );

	m_device = createDevice ( m_deviceType, m_windowSize, m_bits, m_fullscreen, m_stencilBuffer, m_vsync, m_receiver );

	if ( m_device )
	{
		m_smgr = m_device->getSceneManager ( );
		m_videoDriver = m_device->getVideoDriver ( );
		m_guiEnvironment = m_device->getGUIEnvironment ( );
		m_timer = m_device->getTimer ( );

		// Set Window Caption
		core::stringw str = L"Irrlicht Engine - Test Application [";
		str += m_device->getVideoDriver ( )->getName ( );
		str += "]";
		m_device->setWindowCaption ( str.c_str ( ) );

		// Setup skin for GUI
		gui::IGUISkin* skin = GetGUIEnvironment ( )->getSkin ( );
		skin->setFont ( GetArialFont ( ) );
		skin->setColor ( gui::EGDC_3D_DARK_SHADOW, video::SColor ( 128, 0, 0, 0 ) );
		skin->setColor ( gui::EGDC_3D_SHADOW, video::SColor ( 128, 0, 0, 0 ) );
		skin->setColor ( gui::EGDC_3D_FACE, video::SColor ( 128, 0, 0, 0 ) );
		skin->setColor ( gui::EGDC_3D_HIGH_LIGHT, video::SColor ( 128, 0, 0, 0 ) );
		skin->setColor ( gui::EGDC_3D_LIGHT, video::SColor ( 128, 0, 0, 0 ) );
		skin->setColor ( gui::EGDC_ACTIVE_CAPTION, video::SColor ( 255, 255, 255, 255 ) );
		skin->setColor ( gui::EGDC_ACTIVE_BORDER, video::SColor ( 128, 0, 0, 0 ) );
		skin->setColor ( gui::EGDC_BUTTON_TEXT, video::SColor ( 255, 255, 255, 255 ) );	

		return true;
	}

	return false;
}
Note: I just copied and pasted some code from my own CGameCore class. I took out a lot of stuff, so I don't have to include my whole project, or explain a lot of extra stuff, so I don't know if this here compiles, but it should give you a good base to start from. Hope it helps! :)
Stu L Tissimus
Posts: 48
Joined: Thu Jul 15, 2004 8:45 pm
Location: Manhattan
Contact:

Post by Stu L Tissimus »

Yeah, it's a good start. But with me being the noob that I am, I need a little help >_<

Code: Select all

#ifndef __GAMECORE_H__
#define __GAMECORE_H__

#include "MyEventReceiver.h"
#include "Irrlicht.h"
#include <iostream>
using namespace std;
using namespace irr;

class GameCore {
    private:
        core::vector3d<int> Location;
        core::vector3d<int> Rotation;
    public:
        IrrlichtDevice *device;
        GameCore(){
                GameCore->device = NULL; }
        MyEventReceiver receiver;

        
        void InitDevice() {
                GameCore->device = createDevice(
                video::EDT_OPENGL, core::dimension2d<s32>
                (640, 480), 16, false, false, false, &receiver);
        }    
        
};

#endif
This isn't as much Irrlicht as it is C++, but could somebody help me? I know the problem lies within me using ->.
Laptop: P-M 1.6GHz|1024MB PC2800|Mob.Radeon9700Pro|60GB

Desktop: A-XP 2400+|512MB PC3200|Radeon 9700 Pro|180GB
digfarenough
Posts: 39
Joined: Sat Oct 30, 2004 4:35 pm
Location: Boston, MA
Contact:

Post by digfarenough »

there's no need to say GameCore->device inside the definition of GameCore, just use device

-> is the operator that simultaneously deferences the pointer and accesses a member function or variable, so elsewhere in your code if you have GameCore gc; you'd use gc.device to access the device or if you had a GameCore* gc you'd use gc->device to access it

but none of that matters here because within the definition of the class, it's implied that the variable is a member of it
Spintz
Posts: 1688
Joined: Thu Nov 04, 2004 3:25 pm

Post by Spintz »

I'd also recommend separating your class into .cpp and .h files. Functions which are very fast and small you could still implement right in the header file, however, larger, slower functions, you'd want in the .cpp file. Functions implemented in the .h will automatically be treated as inline functions, and that can result is some huge code, once you're project gets larger.
Post Reply