• Advertisement

Archived

This topic is now archived and is closed to further replies.

going through an enumerated type

This topic is 5120 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hi, I''m trying to create a menu for my game and I thought the best way to do this was by using an enumerated type : menu {NewGame, HighScore, Exit} ; menu menustate ; when my menu is on the position of New game it makes the text "jump" a little bit, now what I''m trying to do is when I press up/down the next text "jumps" instead of "New Game" but how can I step through my states? I can''t do : If (up is pressed) menustate -- if (down is pressed) menustate++ How do you guys do it?

Share this post


Link to post
Share on other sites
Advertisement

enum { NewGame, HighScore, Exit, NumStates };

if( uppressed )
menustate = (menustate + 1) % NumStates;

if( downpressed )
menustate = (menustate - 1) % NumStates


And make sure menustate is an unsigned int, or when you go below zero, it will become -1 and could cause wacky results. So now NumStates would be 3, so if you had menustate = 10, it would actually be the HighScore state because 10 % 3 is 1 which is the HighScore enum

No more need to worry about going out of bounds.[/source]


| TripleBuffer Software |
| Plug-in Manager :: System Information Class :: C++ Debug Kit :: DirectX Tutorials :: Awesome Books |


[edited by - ifoobar on April 14, 2004 10:35:27 AM]

Share this post


Link to post
Share on other sites
quote:
Original post by Da cobra
hi,

I'm trying to create a menu for my game and I thought the best way to do this was by using an enumerated type :

menu {NewGame, HighScore, Exit} ;

menu menustate ;



Well, it's pretty simple. You just need to cast it to int:


(int)menustate--
(int)menustate++


Enumerators are just integers after all.
Just make sure you do some bounds-checking, maybe using the modulus (%) operator, like IFooBar suggested.

Hope this helps.

[edited by - mandrav on April 14, 2004 2:08:50 PM]

Share this post


Link to post
Share on other sites
nope still some errors, this is what I have now :


enum Menu {NewGame=0, Highscore=1, Exit=2} ;
Menu g_MenuState ;

if (KeyDown(KeyState, DIK_UP))
{
(int)g_MenuState++ ;
if ((int)g_MenuState < NewGame)
(int)g_MenuState = Exit ;
}

if (KeyDown(KeyState, DIK_UP))
{
(int)g_MenuState-- ;
if ((int)g_MenuState > Exit)
(int)g_MenuState = NewGame ;
}


and I get the following error :

error C2676: binary '++' : 'enum Menu' does not define this operator or a conversion to a type acceptabl

on the line : (int)g_MenuState++ ;

any suggestions?

[edited by - Da Cobra on April 15, 2004 12:35:06 PM]

noone that can help me?

[edited by - da cobra on April 16, 2004 5:55:42 AM]

edit: fixed your source box

[edited by - SiCrane on April 16, 2004 6:02:43 AM]

Share this post


Link to post
Share on other sites
I personally prefer to overload operator++() and operator--() for enum types that get iterated over. ex:

enum Menu {NewGame=0, Highscore=1, Exit=2} ;

Menu operator++(Menu m) {
switch (m) {
case NewGame: return Highscore;
case Highscore: return Exit;
case Exit: return NewGame;
}
};

Menu operator--(Menu m) {
switch (m) {
case NewGame: return Exit;
case Highscore: return NewGame;
case Exit: return Highscore;
}
};

Share this post


Link to post
Share on other sites
quote:
error C2676: binary '++' : 'enum Menu' does not define this operator or a conversion to a type acceptabl


You can do what SiCrane suggested or you could try and break it down to something like:

instead of (int)menustate++, try

int i = (int)menustate; // the (int) cast might not be needed, depends on your compiler
++i;
menustate = (Menu)i; // same remark here about the cast


But since the code starts to grow, SiCrane's suggestion is better because it does not allow out-of-bounds errors.

quote:


if ((int)g_MenuState < NewGame)
(int)g_MenuState = Exit ;



No need for casts here. You comparing/assigning vars/consts of the same data type...


[edited by - mandrav on April 16, 2004 7:14:48 AM]

Share this post


Link to post
Share on other sites
quote:
Original post by SiCrane
I personally prefer to overload operator++() and operator--() for enum types that get iterated over. ex:

enum Menu {NewGame=0, Highscore=1, Exit=2} ;

Menu operator++(Menu m) {
switch (m) {
case NewGame: return Highscore;
case Highscore: return Exit;
case Exit: return NewGame;
}
};

Menu operator--(Menu m) {
switch (m) {
case NewGame: return Exit;
case Highscore: return NewGame;
case Exit: return Highscore;
}
};



Can I use your code like this then?

if (KeyDown(g_keystate, DIK_UP))
{
// Menu item up
g_Menu-- ;
}

if (KeyDown(g_keystate, DIK_DOWN))
{
// Menu item down
g_Menu++ ;
}


because when I press up/down with this code my application just exits

Share this post


Link to post
Share on other sites

  • Advertisement