may be a C++ question

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
campino
Posts: 24
Joined: Wed Sep 21, 2005 12:57 pm
Location: Germany

may be a C++ question

Post by campino »

hi,
I code a EventReceiver like in Tutorial 05.UserInterface. I compiled it without errors and execute it (using Dev C++). First it works but than I get this Windows Error Message (An error has occured. Please send a bug report to Microsoft and so on, I think you know it).
By using the Debugger I get a Segmentation Fault. The Debugger didn't show a position but if I set the comments before switch and after device->closeDevice() the error didn't occurs (but it didn't work, of course). Where is my mistake?

Code: Select all

bool DriverTypeReceiver::OnEvent(SEvent event){
	if (event.EventType == EET_GUI_EVENT)
	{
			s32 id = event.GUIEvent.Caller->getID();

			if(event.GUIEvent.EventType==gui::EGET_BUTTON_CLICKED){
                   s32 s=listbox->getSelected();
                   /*switch(s){ //<-error without coment
                             case 0:
                                  driverType=video::EDT_DIRECTX9;
                                  break;
                             case 1:
                                  driverType=video::EDT_DIRECTX8;
                                  break;
                             case 2:
                                  driverType=video::EDT_OPENGL;
                                  break;
                   }
                   device->closeDevice();*/ //<-error without coment
                   return true;                                           
            }
   }
		return false;
};
Thanks for helping me

PS: I'm sorry for my english, it's only my second language, my first one is german.
evo
Posts: 96
Joined: Mon Jun 27, 2005 6:46 pm
Location: The Netherlands

Post by evo »

It is propably not wise to use 'device->closeDevice();' in an EventReceiver class.
In the tutorial 'device->drop();' is used. But only when stopping the application. What is your aim ? Changing the drivertype 'on the fly' ?
Midnight
Posts: 1772
Joined: Fri Jul 02, 2004 2:37 pm
Location: Wonderland

Post by Midnight »

The next version of Guice will show an example of how to drop the device set a new one and reload the application much like you see in most commercial games.

I can't really comment on how "proper" my method is but it doesn't seem to cause any problems that I can see so far.

Sorry I didn't include the code here but it's just a bit complex and I'm busy trying to get it released.

Another week or 2 at the most... I know I know I keep saying that but I mean it this time I have set it as a personal goal two weeks or less starting today.
campino
Posts: 24
Joined: Wed Sep 21, 2005 12:57 pm
Location: Germany

Post by campino »

evo wrote:It is propably not wise to use 'device->closeDevice();' in an EventReceiver class.
In the tutorial 'device->drop();' is used. But only when stopping the application. What is your aim ? Changing the drivertype 'on the fly' ?
In the example definitly device->closeDevice() is used. (main.cpp line 90).

I didn't want exactly to change the driver type on the fly.

I create an device with the Software Renderer to create a gui. The user can choose the driver he want to use with the gui. My scene is painted with a new device and the new driver after that (To replace the console-using driver choose from the tutorials). So I didn't choose the driver type on the fly but create a new device with another driver.
Youth-Irrlicht-Coder

There are 10 sorts of people, those who understand binaries and those who don't.
evo
Posts: 96
Joined: Mon Jun 27, 2005 6:46 pm
Location: The Netherlands

Post by evo »

hmm. Yes you are right :oops:

Thinking this time. Try to find the exact place where the program crashes. Put something like 'printf("Reached this point.\n");' everywhere you think it may crash and check the command window on execution.

I guess (from the small section of code you showed) the crash happens elsewere. Maybe you are using objects that are not initialized any more.

PS. Your english is far better than my german :wink:
campino
Posts: 24
Joined: Wed Sep 21, 2005 12:57 pm
Location: Germany

Post by campino »

okay then, I add a few printf's and match the Error to the line with

Code: Select all

s32=listbox->getSelected();
in it. I think listbox isn't initialized anymore, but I can't find out why. I post my whole Code for this featzre, maybe you find out more. so, here it is:

Code: Select all

//DriverTypeReceiver.h
#include <irrlicht.h>

using namespace irr;

#pragma comment(lib, "Irrlicht.lib")

class DriverTypeReceiver : public IEventReceiver
{
private:
        IrrlichtDevice* device;
        gui::IGUIListBox* listbox;
        gui::IGUIEnvironment* env;
public:
       void start();
       video::E_DRIVER_TYPE driverType;
       virtual bool OnEvent(SEvent event);
};

//DriverTypeReceiver.cpp
#include <iostream>
#include "DriverTypeReceiver.h"

using namespace irr;

#pragma comment(lib, "Irrlicht.lib")

void DriverTypeReceiver::start(){
                     device=createDevice(video::EDT_SOFTWARE, core::dimension2d<s32>(640, 480), 16, false);
                     
                     env=device->getGUIEnvironment();
                     video::IVideoDriver* driver=device->getVideoDriver();
                     
                     DriverTypeReceiver* receiver=new DriverTypeReceiver();
                     device->setEventReceiver(receiver);
                     
                     env->addStaticText(L"Treibertype waehlen: ", core::rect<s32>(10,10,250,20), true);
                     listbox = env->addListBox(core::rect<s32>(10, 40, 250, 180));

                     listbox->addItem(L"Direct3D 9.0");
                     listbox->addItem(L"Direct3D 8.1");
                     listbox->addItem(L"OpenGL 1.2");
                     listbox->addItem(L"Direct3D 9.0");
                     
                     env->addButton(core::rect<s32>(10, 230, 250, 250),0, 101, L"Okay");
                     
                     while(device->run()){
                         driver->beginScene(true, true, video::SColor(255,100,101,140));
                         
                         env->drawAll();
                         
                         driver->endScene();
                     }
                     
                     device->drop();	              
}

bool DriverTypeReceiver::OnEvent(SEvent event){
	if (event.EventType == EET_GUI_EVENT)
	{
			s32 id = event.GUIEvent.Caller->getID();

			if(event.GUIEvent.EventType==gui::EGET_BUTTON_CLICKED){
                   printf("In event"); //is printed
                   s32 s=listbox->getSelected();
                   printf("before switch"); //isn't printed
                   switch(s){
                             case 0:
                                  driverType=video::EDT_DIRECTX9;
                                  break;
                             case 1:
                                  driverType=video::EDT_DIRECTX8;
                                  break;
                             case 2:
                                  driverType=video::EDT_OPENGL;
                                  break;
                   }
                   printf("before drop();"); //isn't printed
                   device->drop();
                   return true;                                           
            }
   }
		return false;
};

Again, thanks for putting me up ;))
and thanks for helping me
evo wrote: PS. Your english is far better than my german :wink:
My english teacher said something else ;))

EDIT:
I try to add a listbox->getSelected(); to the example (replacing a addItem) and it didn't work also. Maybe it is a bug in Irrlicht? (I use v0.12)
Youth-Irrlicht-Coder

There are 10 sorts of people, those who understand binaries and those who don't.
evo
Posts: 96
Joined: Mon Jun 27, 2005 6:46 pm
Location: The Netherlands

Post by evo »

Well, the crash is definitely caused by the fact that within your 'OnEvent' function the handle to 'listbox' is lost. You can at least catch the runtime error by doing:

Code: Select all

if (listbox)
{
     irr::s32 s = listbox->getSelected(); 
     switch(s){ 
                  case 0: 
     ............
     }
}
I can't figure out why the handle to listbox is lost though. Sorry.

You may look in the (meshviewer) tutorial for the correct use of the createDevice function. You can add 3 more parameters. The last one being a pointer to your eventreceiver class. In your code that would be 'itself'. This is likely also the cause of the problem.

I hope this helps a bit.
campino
Posts: 24
Joined: Wed Sep 21, 2005 12:57 pm
Location: Germany

Post by campino »

ups... :oops:

Of course device->setEventReceiver(this);. Think I have been sleeping by coding this poop. Thanks
Youth-Irrlicht-Coder

There are 10 sorts of people, those who understand binaries and those who don't.
Post Reply