Problem with EventReceiver

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
Raptor
Posts: 4
Joined: Mon Jun 21, 2004 4:03 pm
Location: Germany

Problem with EventReceiver

Post by Raptor »

Hi all,

I have a problem with my event receiver. When im not add the Receiver to the Create Device function the error dont come. So i think its the Receiver function.

Here the code for the Receiver Function:

Code: Select all

class InputReceiver : public IEventReceiver
{
public:
	virtual bool OnEvent(SEvent event)
	{
		if(g_Game.GetGameState() == GS_MAINMENU && 
			event.EventType == irr::EET_KEY_INPUT_EVENT &&
		   !event.KeyInput.PressedDown)
		{
			switch(event.KeyInput.Key)
			{
			case KEY_KEY_W:
				{
					if(g_Game.m_MainMenu.m_ActiveBtn == SB_START)
					{
						g_Game.m_MainMenu.m_ActiveBtn = SB_END;
					}
					if(g_Game.m_MainMenu.m_ActiveBtn == SB_END)
					{
						g_Game.m_MainMenu.m_ActiveBtn = SB_START;
					}
				}
				return true;
			}
		}
		return false;
	}
};
I cant write the error down here cause i have to look in dissambly. So the error is in Irrlicht i think. If i start my application it starts normaly. Than i goes out the programm (i dont do anything). If i start the debugger there comes an Save error with the follow adresses :
0x0804b9b1 in Blobattack 4.exe: 0xC0000096: Privileged instruction.

thats the error. Please help me, we are programming a game with irrlicht and have to go on. But without the receiver i cant go on :( .
warui
Posts: 232
Joined: Wed Apr 14, 2004 12:06 pm
Location: Lodz, Poland
Contact:

Post by warui »

What you mean by "Than i goes out the programm (i dont do anything)." ? The program quits by itself or you quit it ? Maybe you try to read or write something that has not been constructed yet, like g_Game or m_MainMenu.
Tomasz Nowakowski
Openoko - www.openoko.pl
Raptor
Posts: 4
Joined: Mon Jun 21, 2004 4:03 pm
Location: Germany

Post by Raptor »

Sry for my english. I mean the programm quits itself. No here is the Code where i Init everything.

Game.cpp

Code: Select all

#include "Game.h"

IrrlichtDevice*  g_pDevice;
IVideoDriver*    g_pDriver;
ISceneManager*   g_pScene;
IGUIEnvironment* g_pGUI;
CGame*           g_Game;

class InputReceiver : public IEventReceiver
{
public:
	virtual bool OnEvent(SEvent event)
	{
		if(g_Game->GetGameState() == GS_MAINMENU && 
			event.EventType == EET_KEY_INPUT_EVENT &&
		   !event.KeyInput.PressedDown)
		{
			switch(event.KeyInput.Key)
			{
			case KEY_KEY_W:
				{
					if(g_Game->m_MainMenu->m_ActiveBtn == SB_START)
					{
						g_Game->m_MainMenu->m_ActiveBtn = SB_END;
					}
					if(g_Game->m_MainMenu->m_ActiveBtn == SB_END)
					{
						g_Game->m_MainMenu->m_ActiveBtn = SB_START;
					}
				}
				return true;
			}
		}
		return false;
	}
};

int CGame::Move()
{
	return 0;
}

int CGame::Render()
{
	if(g_pDevice->isWindowActive())
	{
		g_pDriver->beginScene(true, true, SColor(0, 255,255,255));
		m_MainMenu->Render();
		g_pDriver->endScene();
	}
	return 0;
}

void IrrlichtStartUp()
{
	InputReceiver Receiver;

	g_pDevice = createDevice(EDT_DIRECTX8, dimension2d<s32>(GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN)), 16, true, false, &Receiver);
	g_pDevice->setWindowCaption(L"Blobattack 4");

	g_pDriver = g_pDevice->getVideoDriver();
	g_pScene  = g_pDevice->getSceneManager();
	g_pGUI    = g_pDevice->getGUIEnvironment();
}

int main()
{
	g_Game = new CGame;
	g_Game->m_MainMenu = new CMainMenu;

	IrrlichtStartUp();

	while(g_pDevice->run())
	{
		g_Game->Render();
	}

	g_pDevice->drop();
	delete g_Game;
	delete g_Game->m_MainMenu;

	return 0;
}
Game.h

Code: Select all

#include <Irrlicht.h>
#include <Windows.h>
#include "MainMenu.h"

using namespace irr;
using namespace core;
using namespace scene;
using namespace io;
using namespace gui;
using namespace video;

extern IrrlichtDevice*  g_pDevice;
extern IVideoDriver*    g_pDriver;
extern ISceneManager*   g_pScene;
extern IGUIEnvironment* g_pGUI;

enum EGameState
{
	GS_NONE      = 0,
	GS_INTRO     = 1,
	GS_MAINMENU  = 2,
	GS_GAME      = 3
};

class CGame
{
private:
	EGameState  m_GameState;

public:
	int Render();
	int Move();

	EGameState GetGameState() { return m_GameState; }

	CMainMenu*   m_MainMenu;
};
MainMenu.h

Code: Select all

using namespace irr;
using namespace core;
using namespace scene;
using namespace io;
using namespace gui;
using namespace video;

enum SelectedButton
{
	SB_START,
	SB_END
};

typedef SelectedButton SButton;

class CMainMenu
{
private:
	ITexture*      m_StartTex;
	ITexture*      m_EndTex;
	ITexture*      m_StartSel;
	ITexture*      m_EndSel;

public:
	int Render();
	int Move();

	void Set();

	SButton m_ActiveBtn;
};
MainMenu.cpp

Code: Select all

#include "Game.h"

int CMainMenu::Render()
{
	m_StartTex = g_pDriver->getTexture("button.bmp");
	m_EndTex   = g_pDriver->getTexture("end.bmp");
	m_StartSel = g_pDriver->getTexture("StartSel.bmp");

	g_pDriver->draw2DImage(m_StartTex, position2d<s32>(200, 200), rect<s32>(0, 0, 152, 32), 0, SColor(255,255,255,255), 0);
	g_pDriver->draw2DImage(m_EndTex, position2d<s32>(200, 250), rect<s32>(0, 0, 152, 32), 0, SColor(255,255,255,255), 0);

	if(m_ActiveBtn == SB_START)
	{
		g_pDriver->draw2DImage(m_StartSel, position2d<s32>(200, 200), rect<s32>(0, 0, 152, 32), 0, SColor(255,255,255,255), 0);
	}
	if(m_ActiveBtn == SB_END)
	{
		g_pDriver->draw2DImage(m_StartSel, position2d<s32>(200, 250), rect<s32>(0, 0, 152, 32), 0, SColor(255,255,255,255), 0);
	}

	return 0;
}

int CMainMenu::Move()
{
	return 0;
}
warui
Posts: 232
Joined: Wed Apr 14, 2004 12:06 pm
Location: Lodz, Poland
Contact:

Post by warui »

Here's your problem:
You create InputReceiver as a local variable of IrrlichtStartUp() so it is deleted as soon as program leaves function body. That's why your program crashes, because it trys to sent information to an object that don't exists anymore.

Solutions:
*) make InputReceiver object global
*) create InputReciver inside main() function and pass pointer to it as an argument of IrrlichtStartUp()
*) create InputReceiver object using new, but you will have to store the pointer to it and delete it on exiting program.

One more thing:

Code: Select all

               if(g_Game->m_MainMenu->m_ActiveBtn == SB_START) 
               { 
                  g_Game->m_MainMenu->m_ActiveBtn = SB_END; 
               } 
               if(g_Game->m_MainMenu->m_ActiveBtn == SB_END) 
               { 
                  g_Game->m_MainMenu->m_ActiveBtn = SB_START; 
               } 
If m_ActiveBtn will be equal SB_START you will end with m_ActiveBtn still being SB_START. Because you change the value of m_ActiveBtn and then check it again. It should be:

Code: Select all

               if(g_Game->m_MainMenu->m_ActiveBtn == SB_START) 
               { 
                  g_Game->m_MainMenu->m_ActiveBtn = SB_END; 
               } 
               else if(g_Game->m_MainMenu->m_ActiveBtn == SB_END) 
               { 
                  g_Game->m_MainMenu->m_ActiveBtn = SB_START; 
               } 
or even

Code: Select all

if(g_Game->m_MainMenu->m_ActiveBtn == SB_START) 
    g_Game->m_MainMenu->m_ActiveBtn = SB_END; 
else
    g_Game->m_MainMenu->m_ActiveBtn = SB_START; 
if m_ActiveBtn can have only two states (if it has more use switch & case if possible).
Tomasz Nowakowski
Openoko - www.openoko.pl
Raptor
Posts: 4
Joined: Mon Jun 21, 2004 4:03 pm
Location: Germany

Post by Raptor »

Thanks :). Now it works. Yeah u are rigth, the gameprogramming starts today. It was just a test. Look at the MainMenu.cpp . There i Load the Texture every time. Now i can go on with programming and optimizing the functions :) .

P.S: Cool community here :)
Post Reply