Page 1 of 1

GUI crashes (and some other questions)

Posted: Tue Jul 26, 2005 6:50 am
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;
}

Posted: Tue Jul 26, 2005 11:36 am
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.

Posted: Tue Jul 26, 2005 11:44 am
by markzer
Thanks, I will try it


It works, but now it crash if I click Quit button

Posted: Wed Jul 27, 2005 7:29 pm
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

Posted: Fri Jul 29, 2005 6:21 am
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.

Posted: Fri Jul 29, 2005 6:38 am
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.

Posted: Fri Jul 29, 2005 3:05 pm
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