My IEventReceiver is just not working...

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
WLGfx
Posts: 9
Joined: Fri Nov 23, 2012 12:56 am

My IEventReceiver is just not working...

Post by WLGfx »

I'm currently on Linux and was using 1.7.3 (which didn't work either) and now on 1.8 and using Code::Blocks. I've even just copied one of the examples and threw in all my source files into the project so that it uses all the correct settings. The projects settings do pick up the local include and lib dirs and has been compiled to both static and shared library.

I've had the examples old main.cpp side by side with my event receiver and my other code and come up with every way possible to implement the receiver. I'm pulling my hair out because I'm on the fifth week, lots of code written in other files and no way to access mouse and keyboard input.

And I will feel even more ashamed if it is something so daft... Here's the main portions of the code...:

Please, can someone point out my mistake?

Code: Select all

#ifndef MY_IRR_H
#define MY_IRR_H
 
 
#include <irrlicht.h>
#include "stdio.h"
#include "stdlib.h"
 
#define DEG2RAD(a) (a*3.14159265f/180.0f)
 
using namespace irr;
 
// ----------------------------------------------------------------------------
 
class IRRevent : public IEventReceiver
{
public:
    bool keys[KEY_KEY_CODES_COUNT];
 
    virtual bool OnEvent(const SEvent& event)
    {
        // Keys???
        if (event.EventType == EET_KEY_INPUT_EVENT)
        {
            keys[event.KeyInput.Key] = event.KeyInput.PressedDown;
            return false;
        }
        else if (event.EventType == irr::EET_MOUSE_INPUT_EVENT)
        {
            switch ((int)event.MouseInput.Event)
            {
            case irr::EMIE_MOUSE_MOVED          :
                mousex = event.MouseInput.X;
                mousey = event.MouseInput.Y;
                break;
            case irr::EMIE_MOUSE_WHEEL          :
                mousez = event.MouseInput.Wheel;
                break;
            case irr::EMIE_LMOUSE_LEFT_UP       :
                mouseclick |= 0x01;
                break;
            case irr::EMIE_LMOUSE_PRESSED_DOWN  :
                mouseclick &= 0x06;
                break;
            case irr::EMIE_RMOUSE_LEFT_UP       :
                mouseclick |= 0x02;
                break;
            case irr::EMIE_RMOUSE_PRESSED_DOWN  :
                mouseclick &= 0x05;
                break;
            case irr::EMIE_MMOUSE_LEFT_UP       :
                mouseclick |= 0x04;
                break;
            case irr::EMIE_MMOUSE_PRESSED_DOWN  :
                mouseclick &= 0x03;
                break;
            }
            return false;
        }
        return false;
    }
 
 
    IRRevent()
    {
        for (u32 i=0; i<irr::KEY_KEY_CODES_COUNT; i++)
            keys[i]=false;
    }
 
 
    int     mousex, mousey, mousez, mouseclick;
    int     lastmousex, lastmousey;
};
 
// ----------------------------------------------------------------------------
 
#endif // MY_IRR_H
 

Code: Select all

#include "../include/defs.h"
 
#include "../include/GAME_CLASS.h"
#include "../include/player_class.h"
 
void test1();
 
// implemented if I decide to link to another language
 
#ifdef COMPILE_MAIN
 
int main( int argc, char *argv[] )
{
    printf("Calling function - test1\n");
    test1();
 
    return 0;
}
 
#endif
 
void test1()
{
    wchar_t buf[32];    // temporary character buffer
 
    printf("Initializing IrrLicht\n");
 
    gc.irr_init( 800, 600 );
 
    printf("Getting the current TIMER value\n");
 
    u32 time = gc.device->getTimer()->getTime();
 
    while ( gc.device->getTimer()->getTime() < ( time + 4000 ) )
    {
        gc.device->getTimer()->tick();
        gc.driver->beginScene(true, true, 0x002040 );
 
        printf("About to print HELLO WORLD!\n");
 
        gc.font->draw( L"HELLO WORLD", core::rect<s32>(0,0,100,10),0xffffffff );
 
        printf("Displaying MOUSE COORDS\n");
 
        swprintf( buf,16,L"%d,%d", gc.events->mousex, gc.events->mousey );
        gc.font->draw( buf, core::rect<s32>(0,10,100,20),0xffffffff );
 
        for (int key=0; key<16; key++)
        {
            for (int pos=0; pos<16; pos++)
            {
                buf[pos] = gc.events->keys[key*16+pos] + '0';
            }
            buf[16] = 0;
            gc.font->draw( buf, core::rect<s32>(0,key*10+30,0,0),0xffffffff );
        }
 
        printf("Displaying screen FPS");
 
        swprintf( buf, 16, L"Free-Born FPS-%d", gc.driver->getFPS() );
        gc.font->draw( buf, core::rect<s32>(0,200,0,0), 0xffffffff );
 
        gc.smgr->drawAll();
        gc.gui->drawAll();
        gc.driver->endScene();
        gc.device->yield();
    }
 
    gc.device->drop();
}
 

Code: Select all

#ifndef GAME_CLASS_H
#define GAME_CLASS_H
 
#include "../include/player_class.h"
#include <mysql/mysql.h>
#include "irrlicht.h"
#include "../include/my_irr.h"
 
using namespace irr;
 
class GAME_CLASS
{
public:
    GAME_CLASS();
    virtual ~GAME_CLASS();
 
    MYSQL*  get_mysql()
    {
        return mysql;
    }
    MYSQL*  sql_init()
    {
        return mysql_init( mysql );
    }
    void    sql_close()
    {
        mysql_close( mysql );
    }
 
    /*
     * MYSQL details for reading the main game database
     */
 
private:
 
    MYSQL   *mysql;
 
    /*
     * Irrlicht stuff for accessing the 3D/2D, etc engine
     */
 
public:
 
    irr::IrrlichtDevice         *device;
    irr::video::IVideoDriver    *driver;
    irr::scene::ISceneManager   *smgr;
    irr::gui::IGUIEnvironment   *gui;
    irr::gui::IGUIFont          *font;
    IRRevent                    *events;
 
    void irr_init(int w,int h);
};
 
//extern IRRevent   irrevent;
 
extern GAME_CLASS gc;
 
#endif // GAME_CLASS_H
 

Code: Select all

#include "../include/GAME_CLASS.h"
 
/*
 * GLOBALs for the entire game
 */
 
GAME_CLASS  gc;
 
/*
 * De/Constructor for the main Game class
 */
 
GAME_CLASS::GAME_CLASS()
{
    mysql = 0;
    events = 0;
}
 
 
GAME_CLASS::~GAME_CLASS()
{
    if (events) delete events;
}
 
 
/*
 * Irrlicht stuff for accessing the 3D/2D, etc engine
 */
 
void GAME_CLASS::irr_init(int w,int h)
{
    events  = new IRRevent;
    device  = irr::createDevice(video::EDT_OPENGL,
              core::dimension2du(w,h),32,false, false, false, events);
    driver  = device->getVideoDriver();
    smgr    = device->getSceneManager();
    gui     = device->getGUIEnvironment();
    font    = gui->getBuiltInFont();
}
 
Thankyou for your patience...
zerochen
Posts: 273
Joined: Wed Jan 07, 2009 1:17 am
Location: Germany

Re: My IEventReceiver is just not working...

Post by zerochen »

hi,

you forgot to call device->run() somethere in your code

eg here:

Code: Select all

while (device->run() && gc.device->getTimer()->getTime() < ( time + 4000 ) )
CuteAlien
Admin
Posts: 9974
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: My IEventReceiver is just not working...

Post by CuteAlien »

Also your code doesn't show if you even set the eventreceiver somewhere.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
WLGfx
Posts: 9
Joined: Fri Nov 23, 2012 12:56 am

Re: My IEventReceiver is just not working...

Post by WLGfx »

gc.irr_init(800,600) initialises the event receiver in another class...

But after adding that silly gc.device->run() it all kicked in perfect! I assumed device->run() just returned a flag saying everything was working okay... I'll be checking the source. :-(

I knew I'd feel daft because of something silly... Thankyou...
REDDemon
Developer
Posts: 1044
Joined: Tue Aug 31, 2010 8:06 pm
Location: Genova (Italy)

Re: My IEventReceiver is just not working...

Post by REDDemon »

That's because all events need to be processed before rendering, and the nicest way to do that is in run(). What if gui is updated in the middle of rendering?
Junior Irrlicht Developer.
Real value in social networks is not about "increasing" number of followers, but about getting in touch with Amazing people.
- by Me
Post Reply