I have a GUI problem

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
keless
Posts: 805
Joined: Mon Dec 15, 2003 10:37 pm
Location: Los Angeles, California, USA

I have a GUI problem

Post by keless »

Hey, I'm new to Irrlicht, but an experienced C++ game programmer.

Anyhow, I'm trying to use the GUI button elements. I have succesfully created and attached them to the GUI environment, and they display properly. (based on the 4.0 demo #5, User Interface)

There are two buttons ('quit' and 'new'), each with their own ID (specified by an enum, but with values of 100 and 101).

Here is the problem: when I handle an event on either of the buttons, their text gets messed up. If I press the New Game button, its text dissappears. If I press the Quit button, the New Game button's text changes to 'Iew Game' (I think its an 'I', might be a '1' or an 'l', or just a cut-off letter 'N')- no matter whether the text is currently there or not.

If, however, I comment out my GUI handling code, the buttons can be pressed (they show depress) and the text does not change.

Here is my code:

Code: Select all

    if (event.EventType == EET_GUI_EVENT) {
        s32 id = event.GUIEvent.Caller->getID();
        switch(event.GUIEvent.EventType) {
        case EGET_BUTTON_CLICKED:
                if(id == GUI_BTN_QUIT) { 
                  key = true; 
                  return true;          
                }
                
                if(id == GUI_BTN_NEW) {
                  key = false;
                  return true;
                }
                //return true; //if supported button id
                break;        
        }//end switch: type of GUI event
    }//end if: GUI event
EDIT: "key" is just some dummy value I put in there to see if its working.
niko
Site Admin
Posts: 1759
Joined: Fri Aug 22, 2003 4:44 am
Location: Vienna, Austria
Contact:

Post by niko »

Very strange, looks like something is overwriting data in the GUI elements. I Don't know what it could be, it works well for me. Is this the only change you did to the code?
keless
Posts: 805
Joined: Mon Dec 15, 2003 10:37 pm
Location: Los Angeles, California, USA

Post by keless »

Here is the rest of the relevant code.
Its being called from within gsr_Intro, from a pointer to a game_State. I intend to have each game state as a class which inherits from game_State. I doubt this is causing the problem, but I thought I should mention it:

Code: Select all

enum tGUIobjs {
    GUI_BTN_QUIT = 101, //quit application
    GUI_BTN_NEW,        //new game
    GUI_BTN_OPTS,       //go to options menu
    GUI_OBJS_NUM        // unused 
};

class game_State
{
    protected:
    IrrlichtDevice       *m_device; //irrlicht device pointer
    video::IVideoDriver  *m_driver; //irrlicht video driver pointer
    gui::IGUIEnvironment *m_env;    //irrlicht GUI environment ptr
	public:
        game_State(game_tetris* game);  //gets G_T's dv and dr and env
        virtual void Render() = 0;
        virtual bool onEvent(SEvent event) = 0;
    // .... stuff cut
};

class gsr_Intro : public game_State {
    bool key;
public:
	gsr_Intro(game_tetris *game);
	virtual void Render();
	virtual bool onEvent(SEvent event);
	virtual void Update();
};

//init code:
gsr_Intro :: gsr_Intro (game_tetris) : game_State(game_tetris) {
    int btn_x = 200 ;
    int btn_y = 200 ;
    #define BTN_ROW_SPACING  40
    #define BTN_W            90
    #define BTN_H            30
    key = false;
    m_env->addButton( core::rect<s32>(btn_x, btn_y, btn_x + BTN_W, btn_y + BTN_H), 0, GUI_BTN_QUIT, L"Quit");
    btn_y -= BTN_ROW_SPACING; // move up
    m_env->addButton(core::rect<s32>(btn_x, btn_y, btn_x + BTN_W, btn_y + BTN_H), 0, GUI_BTN_NEW, L"New Game");
}

//render code:
void gsr_Intro :: Render() {
    m_driver->beginScene(false, false, video::SColor(0,122,65,171));
    if(key) DrawRect(video::SColor(100,200,255,255), core::rect<s32>(50, 50, 100, 100));
    m_env->drawAll();
    m_driver->endScene();
}
Its important to note that the demo #5 _DOES_ work for me! So it must be something I'm doing wrong.. though I dont know what/how/why.

Also, the 'key' variable from above does NOT seem to be affected by button pressed..

EDIT: I just added a 3rd button 'options'. So now they are ordered from top to bottom by: "New Game","Options","Quit".

When I push "New Game", the "Options" button's text dissappears.
When I push "Quit", the "Options" button's text changes to: "lptions"
When I push 'Options', the button depresses and the text stays the same- meaning if its text was already messed up, or missing, it stays that way.

I havent yet added any event handling for the Options button- which is why its not doing anything. I wonder what freaky thing will happen when I do..

EDIT: I've added event handling for the Options button. Now its almost working! I dont know WHY, but its no longer messing up text!!

event handling--
NewGame should make the square dissapear (key=false)
Options should make the square appear (key=true)
Quit should quit the game (game->RequestDestroy() )

what actually happens--
NewGame does nothing (boo)
Options makes the square appear (key is set to true, yay!)
Quit properly exits the game (yay!)

the code:

Code: Select all

        case EGET_BUTTON_CLICKED:
                if(id == GUI_BTN_QUIT) { 
                  p_game->RequestDestroy();
                  return true;          
                }
                if(id == GUI_BTN_NEW) {
                  key = false;
                  return true;
                }
                if(id == GUI_BTN_OPTS) {
                  key = true;
                  return true;
                }
                //return true; //if supported button id
                break;        
        }//end switch: type of GUI event
keless
Posts: 805
Joined: Mon Dec 15, 2003 10:37 pm
Location: Los Angeles, California, USA

Post by keless »

I figured out my problem:
m_driver->beginScene(false, false, video::SColor(0,122,65,171));

Because I wasnt refreshing the screen, the buttons displayed unpredictably. Also, I only "thought" the key variable wasnt being set because the square wasnt going away-- but thats just becasue it wasnt getting cleared, not because the key var was set to true!

That'll teach me to use graphics to debug my program. (I'm using DevCPP instead of my usual VS.NET, so I'm not used to the debugger yet.)

Anyhow, thanks for your concern. Problem solved.

Note to self: ALWAYS REFRESH SCREEN WHEN USING GUI OBJECTS
Post Reply