What I want to do is to use enum to specify different draw modes easily. So far this is what I've got:
class Grid {
enum drawMode { GRID, EROSION, RIVERS, HUMIDITY, ATMOSPHERE }
drawMode activeDraw;
void draw() {
switch(activeDraw) {
case GRID:
drawGrid();
break;
case EROSION:
drawErosion();
break;
// etc..
}
void keyPressed(int key) {
switch(key) {
case ' ':
// Cycle activeDraw to next drawMode
}
}
So if user hits spacebar the activeDraw will change to next value from enum. So if the current activeDraw is GRID after hitting space it will change to EROSION and if activeDraw is ATMOSPHERE it will change to GRID.
Is there a simple solution to this?
Thanks.
You can change default values of enum elements during declaration (if necessary).
Enum constants are implicitly static and final and you can not change their value once created. Enum in Java provides type-safety and can be used inside switch statements like int variables.
Nothing in the C Standard prevent incrementing variables of enum types. Of course the value of bla is just incremented by 1 , it may correspond to another enumeration value or not, depending on the actual values of the enumeration values in the enum type.
Incrementing an enumeration requires a cast to convert the integer result of addition back to the enumeration type, as in: d = day(d + 1);
As noted by Maroš Beťko, to add 1 to a variable, you have to cast the value to int
and back:
activeDraw = static_cast<drawMode>(static_cast<int>(activeDraw) + 1);
If the enum is defined without the C++11 enum class
syntax (like in the question's text), the casting to int
is not necessary:
activeDraw = static_cast<drawMode>(activeDraw + 1);
To make it cycle back to zero, use integer arithmetic, modulo operator:
activeDraw = static_cast<drawMode>((activeDraw + 1) % (ATMOSPHERE + 1));
To eliminate one ugly +1
, add another element to the enum:
enum drawMode { ..., ATMOSPHERE, NUM_DRAW_MODES };
...
activeDraw = static_cast<drawMode>((activeDraw + 1) % NUM_DRAW_MODES);
You can also stuff this code into a operator++
if you use it very often:
drawMode operator++(drawMode& mode)
{
mode = static_cast<drawMode>((mode + 1) % NUM_DRAW_MODES);
return mode;
}
drawMode operator++(drawMode& mode, int) // postfix operator
{
drawMode result = mode;
++mode;
return result;
}
Overloading operators for enum
s is rarely used, and some people consider it overkill (bad), but it will make your code shorter (and arguably cleaner).
Since your enumerates don't have a forced value, you could "increase" them, and perform a modulo on the last item + 1 to reset to the first one when needed:
activeDraw = drawMode((activeDraw+1) % (ATMOSPHERE+1));
BTW: also works in C language with a slight modification:
activeDraw = (activeDraw+1) % (ATMOSPHERE+1);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With