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.cppCode: 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;
}