i wrote a small program to test the EventReceiver and Logger from Irrlicht, but it doesnt work, and i dont know why
the EventReceiver shall process all LogEvents and write them to a html logfile.
Therefore i once create the file (constructor) and then always reopen (append) the file, write to it and close everytime an event occurs.
is this right?
The Program stopps without reason after some written Events, no matter if mouse or key events.
I also dont know when i have to return false or true within the OnEvent Method.
further questions:
- is something wrong with my dbPrint functions?
- why is my info-text for keyEvents so messed up? (i chose UTF8 Fileencoding, so cout should work, but!) html needs to replace special chars, but cant for now
- why is the LogEvent.Text member just a char* pointer, when there is a wchar_t method ???? --- > logger->log(wchar_t* txt, ...)
- how can i convert a wchar_t (KeyEvent.Char) to print with LogEvent.Text (Type char*)
pls help, i got no idea.
thx in advance
here is the code.
Code: Select all
////!#############################################################################
////!
////! (c) 2011 by BenjaminHampe@gmx.de
////!
////!#############################################################################
//
#define _IRR_STATIC_LIB_
#include <irrlicht.h>
using namespace irr;
using namespace core;
using namespace gui;
using namespace video;
using namespace scene;
using namespace io;
#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <ctime>
#include <stdio.h>
#include <stdarg.h> // für dbPrint(const wchar_t* formatString, ...) mit variabler Parameterzahl [...]
#include <time.h>
using namespace std;
#include <GL/gl.h>
#include <GL/glu.h>
#ifndef _CRT_SECURE_NO_DEPRECATE
#define _CRT_SECURE_NO_DEPRECATE
#endif
#ifndef SAVE_DROP
#define SAVE_DROP(x) {if((x)){(x)->drop();(x)=0;}}
#endif
#ifndef SAVE_DELETE
#define SAVE_DELETE(x) {if((x)){delete (x); (x)=0;}}
#endif
// makro Any Code Name to printable String per concat preprocessor command '#'
// small but insanely important little helper
//
#define mStr( name ) # name
// hardcoded EKEY_CODE Values as Strings
const char* const keyNames[] =
{
mStr( KEY_LBUTTON ),
mStr( KEY_RBUTTON ),
mStr( KEY_CANCEL ),
mStr( KEY_MBUTTON ),
mStr( KEY_XBUTTON1 ),
mStr( KEY_XBUTTON2 ),
mStr( KEY_BACK ),
mStr( KEY_TAB ),
mStr( KEY_CLEAR ),
mStr( KEY_RETURN ),
mStr( KEY_SHIFT ),
mStr( KEY_CONTROL ),
mStr( KEY_MENU ),
mStr( KEY_PAUSE ),
mStr( KEY_CAPITAL ),
mStr( KEY_KANA ),
mStr( KEY_HANGUEL ),
mStr( KEY_HANGUL ),
mStr( KEY_JUNJA ),
mStr( KEY_FINAL ),
mStr( KEY_HANJA ),
mStr( KEY_KANJI ),
mStr( KEY_ESCAPE ),
mStr( KEY_CONVERT ),
mStr( KEY_NONCONVERT ),
mStr( KEY_ACCEPT ),
mStr( KEY_MODECHANGE ),
mStr( KEY_SPACE ),
mStr( KEY_PRIOR ),
mStr( KEY_NEXT ),
mStr( KEY_END ),
mStr( KEY_HOME ),
mStr( KEY_LEFT ),
mStr( KEY_UP ),
mStr( KEY_RIGHT ),
mStr( KEY_DOWN ),
mStr( KEY_SELECT ),
mStr( KEY_PRINT ),
mStr( KEY_EXECUT ),
mStr( KEY_SNAPSHOT ),
mStr( KEY_INSERT ),
mStr( KEY_DELETE ),
mStr( KEY_HELP ),
mStr( KEY_KEY_0 ),
mStr( KEY_KEY_1 ),
mStr( KEY_KEY_2 ),
mStr( KEY_KEY_3 ),
mStr( KEY_KEY_4 ),
mStr( KEY_KEY_5 ),
mStr( KEY_KEY_6 ),
mStr( KEY_KEY_7 ),
mStr( KEY_KEY_8 ),
mStr( KEY_KEY_9 ),
mStr( KEY_KEY_A ),
mStr( KEY_KEY_B ),
mStr( KEY_KEY_C ),
mStr( KEY_KEY_D ),
mStr( KEY_KEY_E ),
mStr( KEY_KEY_F ),
mStr( KEY_KEY_G ),
mStr( KEY_KEY_H ),
mStr( KEY_KEY_I ),
mStr( KEY_KEY_J ),
mStr( KEY_KEY_K ),
mStr( KEY_KEY_L ),
mStr( KEY_KEY_M ),
mStr( KEY_KEY_N ),
mStr( KEY_KEY_O ),
mStr( KEY_KEY_P ),
mStr( KEY_KEY_Q ),
mStr( KEY_KEY_R ),
mStr( KEY_KEY_S ),
mStr( KEY_KEY_T ),
mStr( KEY_KEY_U ),
mStr( KEY_KEY_V ),
mStr( KEY_KEY_W ),
mStr( KEY_KEY_X ),
mStr( KEY_KEY_Y ),
mStr( KEY_KEY_Z ),
mStr( KEY_LWIN ),
mStr( KEY_RWIN ),
mStr( KEY_APPS ),
mStr( KEY_SLEEP ),
mStr( KEY_NUMPAD0 ),
mStr( KEY_NUMPAD1 ),
mStr( KEY_NUMPAD2 ),
mStr( KEY_NUMPAD3 ),
mStr( KEY_NUMPAD4 ),
mStr( KEY_NUMPAD5 ),
mStr( KEY_NUMPAD6 ),
mStr( KEY_NUMPAD7 ),
mStr( KEY_NUMPAD8 ),
mStr( KEY_NUMPAD9 ),
mStr( KEY_MULTIPLY ),
mStr( KEY_ADD ),
mStr( KEY_SEPARATOR ),
mStr( KEY_SUBTRACT ),
mStr( KEY_DECIMAL ),
mStr( KEY_DIVIDE ),
mStr( KEY_F1 ),
mStr( KEY_F2 ),
mStr( KEY_F3 ),
mStr( KEY_F4 ),
mStr( KEY_F5 ),
mStr( KEY_F6 ),
mStr( KEY_F7 ),
mStr( KEY_F8 ),
mStr( KEY_F9 ),
mStr( KEY_F10 ),
mStr( KEY_F11 ),
mStr( KEY_F12 ),
mStr( KEY_F13 ),
mStr( KEY_F14 ),
mStr( KEY_F15 ),
mStr( KEY_F16 ),
mStr( KEY_F17 ),
mStr( KEY_F18 ),
mStr( KEY_F19 ),
mStr( KEY_F20 ),
mStr( KEY_F21 ),
mStr( KEY_F22 ),
mStr( KEY_F23 ),
mStr( KEY_F24 ),
mStr( KEY_NUMLOCK ),
mStr( KEY_SCROLL ),
mStr( KEY_LSHIFT ),
mStr( KEY_RSHIFT ),
mStr( KEY_LCONTROL ),
mStr( KEY_RCONTROL ),
mStr( KEY_LMENU ),
mStr( KEY_RMENU ),
mStr( KEY_PLUS ),
mStr( KEY_COMMA ),
mStr( KEY_MINUS ),
mStr( KEY_PERIOD ),
mStr( KEY_ATTN ),
mStr( KEY_CRSEL ),
mStr( KEY_EXSEL ),
mStr( KEY_EREOF ),
mStr( KEY_PLAY ),
mStr( KEY_ZOOM ),
mStr( KEY_PA1 ),
mStr( KEY_OEM_CLEAR ),
mStr( KEY_KEY_CODES_COUNT )
};
// OPERATOR OVERLOADS for IRRLICHT STRING TYPES
inline ostream& operator<<(ostream& out, const stringc& txt)
{
out<<txt.c_str();
return out;
}
inline ostream& operator<<(ostream& out, const stringw& txt)
{
out<<txt.c_str();
return out;
}
inline stringc dbPrint(const char* formatString, ...)
{
va_list p;
va_start(p,formatString);
char tmp[1024];
vsnprintf(&(tmp[0]), 1024, formatString, p);
va_end(p);
stringc s = tmp;
return s;
}
inline stringw dbPrint(const wchar_t* formatString, ...)
{
va_list p;
va_start(p,formatString);
wchar_t tmp[1024];
vsnwprintf(&(tmp[0]), 1024, formatString, p);
va_end(p);
stringw s = tmp;
return s;
}
SIrrlichtCreationParameters params;
IrrlichtDevice* device;
ILogger* logger;
IVideoDriver* driver;
IVideoModeList* display;
IGUIEnvironment* guienv;
ISceneManager* smgr;
IOSOperator* os;
IFileSystem* fs;
vector3di desktop;
vector3di screen;
bool hasOpenGL;
bool hasNonPowerOf2Textures;
bool hasNonQuadraticTextures;
dimension2du maxTextureSize;
///
/// class Stopuhr
///
class Uhr
{
public:
Uhr(){}
~Uhr(){}
static stringc getTime()
{
stringc s("");
time_t tmNow;
time(&tmNow);
tm* p = localtime(&tmNow);
if (p)
{
s+=(1900+p->tm_year); s+="."; if (p->tm_mon<9) s+="0";
s+=(1+p->tm_mon); s+="."; if (p->tm_mday<10) s+="0";
s+=p->tm_mday; s+=" "; if (p->tm_hour<10) s+="0";
s+=p->tm_hour; s+=":"; if (p->tm_min<10) s+="0";
s+=p->tm_min; s+=":"; if (p->tm_sec<10) s+="0";
s+=p->tm_sec; s+=" DST"; s+=(p->tm_isdst);
}
SAVE_DELETE(p);
return s;
}
};
class CMasterEventReceiver : public IEventReceiver
{
public:
path logFileName;
array<bool> keys;
/// Default constructor
CMasterEventReceiver(const path& logfile = "CMasterEventReceiver.html")
: logFileName(logfile), keys(KEY_KEY_CODES_COUNT)
{
// set all keys to 'not pressed'
for (u32 i=0; i<KEY_KEY_CODES_COUNT; i++)
keys[i] = false;
// open htmllogfile
ofstream ooo(logFileName.c_str(), ios::out);
if (ooo.is_open())
{
ooo<<"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" "
<<"\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n"
<<"<html xmlns=\"http://www.w3.org/1999/xhtml\">\n<head>\n"
<<"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\" />\n"
<<"<title>"<<logFileName<<"(c) 2008 - 2011 by BenjaminHampe@gmx.de</title>\n<style type=\"text/css\">\n"
<<"body {background-color:#000000; color:#FFFFFF; font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px; }\n"
<<"date {color:#FF8F40; font-weight:900; font-size:10px; margin-right:20px;}\n"
<<"warn {color:#FFFF00;}\n"
<<"info {color:#FFFFFF;}\n"
<<"error {color:#FF0000;}\n"
<<"none {color:#00FF00;}\n"
<<"</style>\n</head>\n<body>\n";
ooo.close();
}
}
/// Default destructor
~CMasterEventReceiver()
{
// close filestream
ofstream ooo(logFileName.c_str(), ios::app);
if (ooo.is_open())
{ ooo<<"</body>\n</html>\n";
ooo.close();
}
}
virtual bool OnEvent(const SEvent& event)
{
//handle Log Events
if (event.EventType == EET_LOG_TEXT_EVENT)
{
// log normal ascii string
stringc t = Uhr::getTime(); // pure datetime string
stringc c = t; c+=" "; c+=event.LogEvent.Text; // why is LogEvent.Text a char and not wchar_t ???
stringc h("<date>"); h+=t; h+="</date>"; // html datetime tag string
switch (event.LogEvent.Level)
{
// case ELL_NONE: h+="<none>"; h+=event.LogEvent.Text; h+="</none>"; break;
case ELL_INFORMATION: h+="<info>"; h+=event.LogEvent.Text; h+="</info>"; break;
case ELL_WARNING: h+="<warn>"; h+=event.LogEvent.Text; h+="</warn>"; break;
case ELL_ERROR: h+="<error>"; h+=event.LogEvent.Text; h+="</error>"; break;
default: break;
}
// write to file
ofstream o(logFileName.c_str(), ios::app);
if (o.is_open())
{ o<<h.c_str()<<"<br/>\n"; // new line
o.close();
}
return false; // is this right???
}
// handle Keyboard Events
if (event.EventType == EET_KEY_INPUT_EVENT)
{
keys[event.KeyInput.Key] = event.KeyInput.PressedDown;
if (logger)
logger->log(dbPrint(L"Taste %d gedrückt, %hs", event.KeyInput.Key,
keyNames[event.KeyInput.Key]).c_str(), ELL_INFORMATION);
if (keys[KEY_ESCAPE]) device->closeDevice(); // right??
return false; // is this right???
}
// handle Mouse Events
if (event.EventType == EET_MOUSE_INPUT_EVENT)
{
if (logger)
{
s32 x = event.MouseInput.X;
s32 y = event.MouseInput.Y;
f32 w = event.MouseInput.Wheel;
bool l = event.MouseInput.isLeftPressed();
bool m = event.MouseInput.isMiddlePressed();
bool r = event.MouseInput.isRightPressed();
logger->log(dbPrint("Mouse X=%d, Y=%d, Left=%d, Middle=%d, Right=%d, Wheel=%f", x,y,l,m,r,w).c_str(), ELL_INFORMATION);
}
return false;
}
return false;
}
};
// console window
int main(int argc, char** argv)
{
cout << dbPrint("IrrlichtTest by %s %d.%d.%d...", "BenjaminHampe@gmx.de", 2011, 8, 1) << endl;
device = createDevice(EDT_OPENGL, dimension2du(1024,768), 32, false, true, true);
if (!device)
{
cerr << dbPrint("Error creating OpenGL Device! Exit now...")<<endl;
exit(1);
}
device->setResizable(true);
logger = device->getLogger();
driver = device->getVideoDriver();
display = device->getVideoModeList();
smgr = device->getSceneManager();
guienv = device->getGUIEnvironment();
os = device->getOSOperator();
fs = device->getFileSystem();
CMasterEventReceiver receiver;
device->setEventReceiver(&receiver);
// device->run();
if (logger)
{ logger->log("Hello World", ELL_ERROR);
logger->log("Hello World", ELL_INFORMATION);
logger->log("Hello World", ELL_WARNING);
logger->log(L"Hello World", ELL_ERROR);
logger->log(L"Hello World", ELL_INFORMATION);
logger->log(L"Hello World", ELL_WARNING);
}
while (device->run())
{
// if (device->isWindowActive())
// {
driver->beginScene();
smgr->drawAll();
guienv->drawAll();
driver->endScene();
// }
// else
// {
// device->yield();
// }
}
device->closeDevice();
return 0;
}

- The glory days.