• Advertisement

Archived

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

going through an enumerated type

This topic is 5024 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
(int)menustate--
firstly decreases menustate and then casts, right?
(Menu)((int)menustate)--

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
I don''t see anything wrong with that code; it should be fine.

Share this post


Link to post
Share on other sites
NO!!!!!

If menustate is an enumerated type, then do not try to increment or decrement it by casting it to an int like these bozos are saying. If you want to increment or decrement or do any kind of arithmetic on it, then make menustate an int. enums are not designed for this purpose. Why go through the mess and hassle of turning an enum into an int? Just use an int.

SiCrane''s suggestion of overloading the ++ and -- operators has merit. However, if you want to do more than go to the next or previous states, it will be inadequate.

Share this post


Link to post
Share on other sites
yeah whats the deal with making enums an int thier unisgned ints by default anyhow. Tons of business applications use it in state management.

Share this post


Link to post
Share on other sites
quote:
Original post by SiCrane
I don''t see anything wrong with that code; it should be fine.


The operators return operand +/- 1, but don''t actually change the value of the operand itself. Also, you defined the prefix operators and he is using the postfix operators.

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

Menu & operator++(Menu & m) // ++m
{
switch (m)
{
case NewGame: m = Highscore;
case Highscore: m = Exit;
case Exit: m = NewGame;
}

return m;
};

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

Menu operator++(Menu & m, int) // m++
{
Menu const old_m = m;

switch (m)
{
case NewGame: m = Highscore;
case Highscore: m = Exit;
case Exit: m = NewGame;
}

return old_m;
};

Menu operator--(Menu & m, int) // m--
{
Menu const old_m = m;

switch (m)
{
case NewGame: m = Exit;
case Highscore: m = NewGame;
case Exit: m = Highscore;
}
return old_m;
};

Share this post


Link to post
Share on other sites

  • Advertisement