GUI crashes (and some other questions)

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
markzer
Posts: 3
Joined: Tue Jul 26, 2005 6:42 am
Location: Estonia

GUI crashes (and some other questions)

Post by markzer »

I made a little GUI, but it crash every time combile and run it. And I don't know what is wrong.

Code is here:

Code: Select all

#include <irrlicht.h>
#include <iostream>

using namespace irr;

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

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

IrrlichtDevice* device = 0;

class MyEventReceiver : public IEventReceiver
{
public:
       virtual bool OnEvent(SEvent event)
       {
               if (event.EventType == EET_GUI_EVENT)
               {
                  s32 id = event.GUIEvent.Caller->getID();
                  IGUIEnvironment* env = device->getGUIEnvironment();
                  
                  switch(event.GUIEvent.EventType)
                  {
                  case EGET_BUTTON_CLICKED:
                       
                       if (id == 105)
                       {
                              device->closeDevice();
                              return true;
                       }
                       
                       break;
                  }
               }
               
               return false;
       }
};

int main()
{
    E_DRIVER_TYPE driverType = EDT_OPENGL;
    
    IrrlichtDevice* device = createDevice(driverType, dimension2d<s32>(1024, 768), 32, true);
    
    if (device == 0)
       return 1;
    
    MyEventReceiver receiver;
    device->setEventReceiver(&receiver);
    
    IVideoDriver* driver = device->getVideoDriver();
    ISceneManager* smgr = device->getSceneManager();
    IGUIEnvironment* env = device->getGUIEnvironment();
    
    env->addButton(rect<s32>(50,200,200,240), 0, 101, L"Uus mang"); //New game
    env->addButton(rect<s32>(50,250,200,290), 0, 102, L"Lae mang"); //Load game
    env->addButton(rect<s32>(50,300,200,340), 0, 103, L"Seaded"); //Options
    env->addButton(rect<s32>(50,350,200,390), 0, 105, L"Valja"); //Quit
    
    IGUIImage* img = env->addImage(driver->getTexture("/dev-cpp/mang/irrlichtlogoalpha.tga"), position2d<int>(10,700));
    
    IGUISkin* skin = env->getSkin();
    IGUIFont* font = env->getFont("/dev-cpp/mang/font/fontcourier.bmp");
    if (font)
       skin->setFont(font);
       
    while(device->run() && driver)
    if (device->isWindowActive())
    {
       driver->beginScene(true, true, SColor(0,200,200,200));
       
       env->drawAll();
       
       driver->endScene();
    }
    
    device->drop();
    
    return 0;
}
Last edited by markzer on Fri Jul 29, 2005 6:23 am, edited 1 time in total.
DexteR
Posts: 15
Joined: Mon Jul 18, 2005 1:52 am
Location: @ Kernel32.DLL

Post by DexteR »

Try to delete this "IGUIEnvironment* env = device->getGUIEnvironment(); "
under your "MyEventReciever class" it was already define under your main, I
think this makes an error.
AnimaKomix: A Half-3D Packer & Animator
http://www.freewebs.com/project_z/
markzer
Posts: 3
Joined: Tue Jul 26, 2005 6:42 am
Location: Estonia

Post by markzer »

Thanks, I will try it


It works, but now it crash if I click Quit button
evo
Posts: 96
Joined: Mon Jun 27, 2005 6:46 pm
Location: The Netherlands

Post by evo »

As DexteR said:

Code: Select all

int main() 
{ 
    E_DRIVER_TYPE driverType = EDT_OPENGL; 
    
    IrrlichtDevice* device = createDevice(driverType, dimension2d<s32>(1024, 768), 32, true); 
should be

Code: Select all

int main() 
{ 
    E_DRIVER_TYPE driverType = EDT_OPENGL; 
    
    device = createDevice(driverType, dimension2d<s32>(1024, 768), 32, true);
otherwise you have a local and a global instance of the IrrlichtDevice.

Interestingly enough the program crashes as soon as the mouse hovers over a button.

good luck
markzer
Posts: 3
Joined: Tue Jul 26, 2005 6:42 am
Location: Estonia

Post by markzer »

Now it works fine, thanks.

And now I have new questions.

Code: Select all

...
case EGET_BUTTON_CLICKED:
                       
if (id == 101)
{

       ?????; //here should be something

       return true;
}
                       
break; 
...
I want to run a code in other source file (game.cpp) and I don't know how.
evo
Posts: 96
Joined: Mon Jun 27, 2005 6:46 pm
Location: The Netherlands

Post by evo »

Hi,

Your question to me seems more about C++ than about Irrlicht. It may be usefull to look at projects other people made how you can set things up.

Personally I would start with building and loading the first level of your game or project. Dealing with things lika animation, movement, sound, etcetera. Only after this works start thinking about menu's.
A look at the tool list on th FAQ forum might be usefull too.
Midnight
Posts: 1772
Joined: Fri Jul 02, 2004 2:37 pm
Location: Wonderland

Post by Midnight »

markzer wrote:Now it works fine, thanks.

And now I have new questions.

Code: Select all

...
case EGET_BUTTON_CLICKED:
                       
if (id == 101)
{

       ?????; //here should be something

       return true;
}
                       
break; 
...
I want to run a code in other source file (game.cpp) and I don't know how.
I would say take a look at the Guice source code but it's not available at the moment.

Here is an example of a void function for using bits of code in different places, files, functions of your code.

Code: Select all

header file  ".h"
------------------------
#include <irrlicht.h

int a;
int b;   

void myfunction();        

//it could even have a varible to pass or several like this

void myfunction(int x, int y);






main.cpp         .cpp stands for C++   FYI   it is where the code goes.
----------------------------
int main() //main loop the beginning of the application
{
IGUIEnviroment* env = blahblah  
//The enviroment is just like the 
//function or the integers it has been
//prototyped and you must declare it 
//somewhere in order to use it.
//Same goes for the scene manager 
//which is 3d where as enviroment is just for the GUI.

 void myfunction()
 { 
//this is the beginning of the myfunction "scope" 
//it's where the function itself is written as a "void" function meaning 
//it's like a blank check at this point unfinished but valid.
    
  if(this ==  that)
  {
  then do this;
  } 

 }


myfunction();  
// this is where the function is actually used 
//it could be done anywhere many times over


}
hope this helps
Post Reply