Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » KEY_ datatype

This thread is locked; no one can reply to it. rss feed Print
KEY_ datatype
Chris Cameron
Member #11,078
June 2009

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
Member #11,943
May 2010
avatar

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
Member #5,313
December 2004
avatar

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
Member #11,078
June 2009

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
Member #5,313
December 2004
avatar

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
Member #476
June 2000
avatar

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

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Chris Cameron
Member #11,078
June 2009

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

LennyLen
Member #5,313
December 2004
avatar

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
Member #11,078
June 2009

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
Member #5,313
December 2004
avatar

Use the scancode value.

Thomas Fjellstrom
Member #476
June 2000
avatar

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.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

LennyLen
Member #5,313
December 2004
avatar

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
Member #476
June 2000
avatar

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.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

LennyLen
Member #5,313
December 2004
avatar

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
Member #7,536
July 2006
avatar

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
Member #2,604
August 2002
avatar

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).

---
Me make music: Triofobie
---
"We need Tobias and his awesome trombone, too." - Johan Halmén

Go to: