KEY_ datatype
Chris Cameron

Hi

Basically I don't understand enumerations at all, and I want to make an array of key press enums.

Firstly, how would I declare this array?

Secondly, any recommended reading for enumerations for newbies?

Thanks

Desmond Taylor

I havent got to learning those yet but I was given this link and it looks strait forward. I just dont need them atm to learn it.

http://enel.ucalgary.ca/People/Norman/enel315_winter1997/enum_types/

LennyLen

Basically I don't understand enumerations at all, and I want to make an array of key press enums.

Can you describe what you want them for? Making a set of enumeration instead of just using Allegro's predefined KEY_ macros my not be necessary.

Chris Cameron

I want to add a layer between the keyboard presses and the key[KEY_BLAH] stuff so that I can very quickly change controls in 1 header rather than mess around with various classes.

This is what I'm attempting:

#SelectExpand
1int IN_UP = 0; 2int IN_DOWN = 1; 3int IN_LEFT = 2; 4int IN_RIGHT = 3; 5 6int in_keys[total_keys] = {KEY_W, KEY_S KEY_A, KEY_D}; 7 8bool inKey(int key){ 9 if(key[in_keys[key]]) return true; 10 return false; 11}

LennyLen

Well in that case, you don't need an array:

typedef enum { IN_UP = KEY_W, IN_DOWN = KEY_S, IN_LEFT = KEY_D, IN_RIGHT = KEY_D } KeyDefs;

bool inKey(KeyDefs keyPressed) {
  if(key[keyPressed]) return true;
  return false;
}

Thomas Fjellstrom

You do want the array if you want to change the mapping at runtime though.

Chris Cameron

As Tom said, I want to be able to change it at runtime.

LennyLen

As Tom said, I want to be able to change it at runtime.

That contradicts what you said earlier. :P

Doing it at runtime, I wouldn't use an enumeration at all. Just read in the value the user wants to use for each, and store that in the applicable array offset.

Chris Cameron

That's what I'm trying to do. The only reason I mentioned enums at all is because I looked at keyboard.h and found the KEY_ stuff stored as enums.

My problem is that

int inkeys[x] = {KEY_W, KEY_A,. . .};

Is not correct as the keys are not integers. How should I declare this array?

LennyLen

Use the scancode value.

Thomas Fjellstrom

Is not correct as the keys are not integers. How should I declare this array?

They are integers though. It'll be fine. Just use an int array.

LennyLen

They are integers though. It'll be fine. Just use an int array.

I thought C++ didn't allow you to use them interchangeably, or is it just that you're not allowed to use ints where it's expecting an enum?

Thomas Fjellstrom

I can't ever remember the exact rules. Most of the time I never actually use the enum type itself, and just use ints where I'm going to use the enum values.

LennyLen

I looked it up[1]. You can use the enum value in place of an int, but not the other way around. So in this case, an int array with enum values will be fine.

bamccaig
LennyLen said:

I wouldn't use an enumeration at all. Just read in the value the user wants to use for each, and store that in the applicable array offset.

The array offset is what you would enumerate. :P

#SelectExpand
1enum GAME_KEYS 2{ 3 KEY_FORWARD, 4 KEY_BACKWARD, 5 KEY_LEFT, 6 KEY_RIGHT, 7 KEY_JUMP, 8 KEY_FIRE 9}; 10 11int gamekeys[6] = 12{ 13 KEY_W, 14 KEY_S, 15 KEY_A, 16 KEY_D, 17 KEY_SPACE, 18 KEY_ENTER 19};

(Note: the above is for demonstration purposes only. I would discourage a global.)

And for the record, an int can be cast to an enum type. Then again, you might be just as well declaring the game keys as separate ints since there doesn't seem to be any advantage to the array... At least none that springs to mind right now. I would certainly store them in a struct or class though.

Tobias Dammers
LennyLen said:

I thought C++ didn't allow you to use them interchangeably, or is it just that you're not allowed to use ints where it's expecting an enum?

C++ distinguishes 3 kinds of casts:
- widening casts, where a value is cast to a more general type (e.g. a char to an int, or an object to one of its superclasses, like std::cin to a std::istream). If the new type can represent every value that the old type can represent, then it's a widening cast. Widening casts may be performed implicitly.
- narrowing casts, the opposite of a widening cast. Because there is a possible loss of precision, or an incorrect pointer may be generated, C++ mandates that narrowing casts be explicit.
- invalid casts, where you try to cast to a completely incompatible type

Casting from an enum to int is widening, and thus allowed to be done implicitly. Casting from int to enum is narrowing, and must be done explicitly (keeping in mind that the cast may fail).

Thread #604238. Printed from Allegro.cc