GUI Problem
-
- Posts: 8
- Joined: Sat Aug 26, 2006 6:05 pm
- Location: Germany
- Contact:
GUI Problem
Hi everybody
I´m just playing around with the GUI-System of Irrlicht 1.1. Here is what I have
--------------------------
#include <iostream>
using namespace std;
#include <irrlicht.h>
using namespace irr;
IrrlichtDevice *device = 0;
s32 cnt = 0;
class MyEventReceiver : public IEventReceiver
{
public:
virtual bool OnEvent(SEvent event)
{
if (event.EventType == EET_GUI_EVENT)
{
s32 id = event.GUIEvent.Caller->getID();
gui::IGUIEnvironment* env = device->getGUIEnvironment();
switch(event.GUIEvent.EventType)
{
case gui::EGET_BUTTON_CLICKED:
if (id == 101)
{
env->addFileOpenDialog(L"Please choose a file.");
return true;
}
break;
}
}
return false;
}
};
int main()
{
IrrlichtDevice *device = createDevice(video::EDT_NULL,
core::dimension2d<s32>(800,600),
false,false,false,0);
video::IVideoDriver* driver = device->getVideoDriver();
scene::ISceneManager* smgr = device->getSceneManager();
gui::IGUIEnvironment* env = device->getGUIEnvironment();
// HardwareMouse is enabled
device->getCursorControl() -> setVisible(true);
MyEventReceiver receiver;
device->setEventReceiver(&receiver);
device->setWindowCaption(L"Naphilia Test Area");
env->addButton(core::rect<s32>(20,210,100,240), 0, 101, L"Open File");
while (device->run())
{
driver->beginScene(true, true, video::SColor(0,0,0,0));
smgr->drawAll();
env->drawAll();
driver->endScene();
}
device->drop();
return 0;
}
---------------------
It Compiles with gcc (Codeblocks) and everything is fine except the thing, that, when i bring the MouseCursor over the Button, Windows shows up an error and closes the app.
Could need some help here.
Thanks in advance
I´m just playing around with the GUI-System of Irrlicht 1.1. Here is what I have
--------------------------
#include <iostream>
using namespace std;
#include <irrlicht.h>
using namespace irr;
IrrlichtDevice *device = 0;
s32 cnt = 0;
class MyEventReceiver : public IEventReceiver
{
public:
virtual bool OnEvent(SEvent event)
{
if (event.EventType == EET_GUI_EVENT)
{
s32 id = event.GUIEvent.Caller->getID();
gui::IGUIEnvironment* env = device->getGUIEnvironment();
switch(event.GUIEvent.EventType)
{
case gui::EGET_BUTTON_CLICKED:
if (id == 101)
{
env->addFileOpenDialog(L"Please choose a file.");
return true;
}
break;
}
}
return false;
}
};
int main()
{
IrrlichtDevice *device = createDevice(video::EDT_NULL,
core::dimension2d<s32>(800,600),
false,false,false,0);
video::IVideoDriver* driver = device->getVideoDriver();
scene::ISceneManager* smgr = device->getSceneManager();
gui::IGUIEnvironment* env = device->getGUIEnvironment();
// HardwareMouse is enabled
device->getCursorControl() -> setVisible(true);
MyEventReceiver receiver;
device->setEventReceiver(&receiver);
device->setWindowCaption(L"Naphilia Test Area");
env->addButton(core::rect<s32>(20,210,100,240), 0, 101, L"Open File");
while (device->run())
{
driver->beginScene(true, true, video::SColor(0,0,0,0));
smgr->drawAll();
env->drawAll();
driver->endScene();
}
device->drop();
return 0;
}
---------------------
It Compiles with gcc (Codeblocks) and everything is fine except the thing, that, when i bring the MouseCursor over the Button, Windows shows up an error and closes the app.
Could need some help here.
Thanks in advance
-
- Posts: 8
- Joined: Sat Aug 26, 2006 6:05 pm
- Location: Germany
- Contact:
You don't verify any pointer you get. createDevice() might return NULL (although this is not the case here), and event.GUIEvent.Caller might be NULL too (thus calling getID() on it will crash).silentpolygon wrote:Thanks
But this is the first time I use the debugger. This is what it says:
Program received signal (SIGSEGV)
Segmentation fault
Previous frame inner to this frame (corrupt stack?)
error
So what could it be?
Compilation is not a proof that your program will run correctly - that's why you have to use a debugger when something goes wrong. But the first thing to do is to write code that is robust - ie code that has no apparent reason to crash - and your's is not.
Murphy's law about this: if a pointer can't be NULL, be sure that at some point ot will be NULL
Regards,
-- Emmanuel D.
-
- Posts: 8
- Joined: Sat Aug 26, 2006 6:05 pm
- Location: Germany
- Contact:
Thank you, EmmanuelDEmmanuelD wrote:You don't verify any pointer you get. createDevice() might return NULL (although this is not the case here), and event.GUIEvent.Caller might be NULL too (thus calling getID() on it will crash).silentpolygon wrote:Thanks
But this is the first time I use the debugger. This is what it says:
Program received signal (SIGSEGV)
Segmentation fault
Previous frame inner to this frame (corrupt stack?)
error
So what could it be?
Compilation is not a proof that your program will run correctly - that's why you have to use a debugger when something goes wrong. But the first thing to do is to write code that is robust - ie code that has no apparent reason to crash - and your's is not.
Murphy's law about this: if a pointer can't be NULL, be sure that at some point ot will be NULL
Regards,
OK, so The BAckTrace function of Code::Blocks seems to be not very usefull to me. HOW do I verify a pointer??
The World is OpenSource if you have an IrrLicht that leads you...
-
- Admin
- Posts: 14143
- Joined: Wed Apr 19, 2006 9:20 pm
- Location: Oldenburg(Oldb), Germany
- Contact:
Code: Select all
if (pointer==0) doSomethingOnError();
else doSomethingWithGoodPointer();
-
- Posts: 8
- Joined: Sat Aug 26, 2006 6:05 pm
- Location: Germany
- Contact:
I think I should remind you that you are speaking with a n00b. The Pointer should be IEventReceiver?hybrid wrote:Code: Select all
if (pointer==0) doSomethingOnError(); else doSomethingWithGoodPointer();
Do I have to put this code into the main()?
The World is OpenSource if you have an IrrLicht that leads you...
The problem is that you have two device variables. One at global scope, and one inside main. When you call createDevice() you assign to the one in main, but the global one is still left pointing to garbage. You then use the garbage device pointer to call getGUIEnvironment(), which crashes.
Code: Select all
/*IrrlichtDevice **/device = createDevice(video::EDT_NULL,
Good catch - I didn't even see this. Obviously, this is the root of all evil in the OP's program.vitek wrote:The problem is that you have two device variables. One at global scope, and one inside main. When you call createDevice() you assign to the one in main, but the global one is still left pointing to garbage. You then use the garbage device pointer to call getGUIEnvironment(), which crashes.
Code: Select all
/*IrrlichtDevice **/device = createDevice(video::EDT_NULL,
-- Emmanuel D.