|
|
| 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
|
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
|
Chris Cameron said: 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:
|
|
LennyLen
Member #5,313
December 2004
|
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
|
You do want the array if you want to change the mapping at runtime though. -- |
|
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
|
Chris Cameron said: As Tom said, I want to be able to change it at runtime. That contradicts what you said earlier. 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
|
Use the scancode value.
|
|
Thomas Fjellstrom
Member #476
June 2000
|
Chris Cameron said: 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
Member #5,313
December 2004
|
Thomas Fjellstrom said: 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
|
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
Member #5,313
December 2004
|
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
|
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. 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. -- acc.js | al4anim - Allegro 4 Animation library | Allegro 5 VS/NuGet Guide | Allegro.cc Mockup | Allegro.cc <code> Tag | Allegro 4 Timer Example (w/ Semaphores) | Allegro 5 "Winpkg" (MSVC readme) | Bambot | Blog | C++ STL Container Flowchart | Castopulence Software | Check Return Values | Derail? | Is This A Discussion? Flow Chart | Filesystem Hierarchy Standard | Clean Code Talks - Global State and Singletons | How To Use Header Files | GNU/Linux (Debian, Fedora, Gentoo) | rot (rot13, rot47, rotN) | Streaming |
|
Tobias Dammers
Member #2,604
August 2002
|
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: 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). --- |
|
|