Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » key define KEY_MAX

Credits go to Aaron Bolyard, Neil Walker, Thomas Fjellstrom, and Vanneto for helping out!
This thread is locked; no one can reply to it. rss feed Print
key define KEY_MAX
Martin Kalbfuß
Member #9,131
October 2007
avatar

What is it good for? Is it only used internally?

http://remote-lisp.spdns.de -- my server side lisp interpreter
http://www.nongnu.org/gm2/ -- Modula-2 alias Pascal++

Thomas Fjellstrom
Member #476
June 2000
avatar

It lets you know how big the key array is for one.

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

Martin Kalbfuß
Member #9,131
October 2007
avatar

Could you give me an example where this is usefull in a game? I write a wrapper and I don't know if I need to add it.

http://remote-lisp.spdns.de -- my server side lisp interpreter
http://www.nongnu.org/gm2/ -- Modula-2 alias Pascal++

Aaron Bolyard
Member #7,537
July 2006
avatar

It's good, because, for example, say you want to keep an array of keys that were just pressed, or just released, since the last update. You could do something like this:

char oldKeys[KEY_MAX];
char curKeys[KEY_MAX];

/* Later... */
for (int i = 0; i < KEY_MAX; i++)
{
  oldKeys[i] = curKeys[i];
  curKeys[i] = key[i];
}

Then you could do something like:

if (oldKeys[KEY_ENTER] && !curKeys[KEY_ENTER])
  /* Just released... */

Vanneto
Member #8,643
May 2007

In capitalist America bank robs you.

Neil Walker
Member #210
April 2000
avatar

If you only use 4 keys in your game it's poinless looping round 50

Neil.
MAME Cabinet Blog / AXL LIBRARY (a games framework) / AXL Documentation and Tutorial

wii:0356-1384-6687-2022, kart:3308-4806-6002. XBOX:chucklepie

Aaron Bolyard
Member #7,537
July 2006
avatar

If you only use 4 keys in your game it's poinless looping round 50

All my games have customizable input (that used Allegro 4), so I would need to keep track of all of the keys... That's why I needed to loop.

It's just an example, though. Some people may need all keys checked, some may not. KEY_MAX would still be "required" for a faithful Allegro port.

Martin Kalbfuß
Member #9,131
October 2007
avatar

Thanks to all.

http://remote-lisp.spdns.de -- my server side lisp interpreter
http://www.nongnu.org/gm2/ -- Modula-2 alias Pascal++

Neil Walker
Member #210
April 2000
avatar

yes, you keep a reference to the keys you redefine, you don't need to loop through them all.

Neil.
MAME Cabinet Blog / AXL LIBRARY (a games framework) / AXL Documentation and Tutorial

wii:0356-1384-6687-2022, kart:3308-4806-6002. XBOX:chucklepie

Aaron Bolyard
Member #7,537
July 2006
avatar

Not when you do it dynamically.

[edit] And looping fifty times in a game won't kill performance.

Neil Walker
Member #210
April 2000
avatar

Course you do, you'll always know which keys you've defined. Create an array holding all the keys (left, right, up, down, fire, etc) and references to the key and states, whether it's an array of structs, multi-dimensioned array or multiple arrays, that's just implementation specific.

Neil.
MAME Cabinet Blog / AXL LIBRARY (a games framework) / AXL Documentation and Tutorial

wii:0356-1384-6687-2022, kart:3308-4806-6002. XBOX:chucklepie

bamccaig
Member #7,536
July 2006
avatar

All my games have customizable input (that used Allegro 4), so I would need to keep track of all of the keys...

Technically you'd just need to keep track of the keys that were configured. ;)

Primitive [untested] example:

#SelectExpand
1enum { 2 GAME_KEY_LEFT = 0, 3 GAME_KEY_RIGHT = 1, 4 GAME_KEY_UP = 2, 5 GAME_KEY_DOWN = 3, 6 GAME_KEY_FIRE = 4, 7 GAME_KEY_DEFEND = 5, 8 GAME_KEY_USE = 6, 9 GAME_KEY_MENU = 7, 10 GAME_KEY_EXIT = 8, 11 12 GAME_KEY_MAX = 9 13}; 14 15/* 16 * Ctrl+Alt+Shift will complicate things, obviously, but a struct instead of an 17 * int could handle that. ;) 18 */ 19int game_key[GAME_KEY_MAX]; 20 21// Set by user configuration or whatever. 22game_key[GAME_KEY_LEFT] = KEY_A; 23game_key[GAME_KEY_RIGHT] = KEY_D; 24game_key[GAME_KEY_UP] = KEY_W; 25game_key[GAME_KEY_DOWN] = KEY_S; 26game_key[GAME_KEY_FIRE] = KEY_SPACE; 27game_key[GAME_KEY_DEFEND] = KEY_E; 28game_key[GAME_KEY_USE] = KEY_F; 29game_key[GAME_KEY_MENU] = KEY_ENTER; 30game_key[GAME_KEY_EXIT] = KEY_ESC; 31 32. 33. 34. 35 36char old_game_keys[GAME_KEY_MAX]; 37char cur_game_keys[GAME_KEY_MAX]; 38 39. 40. 41. 42 43int i; 44 45for(i=0; i<GAME_KEY_MAX; i++) 46{ 47 old_game_keys[i] = cur_game_keys[i]; 48 cur_game_keys[i] = key[game_key[i]]; 49} 50 51. 52. 53. 54 55if(old_game_keys[GAME_KEY_FIRE] && !cur_game_keys[GAME_KEY_FIRE]) 56 /* Just released... */

MiquelFire
Member #3,110
January 2003
avatar

During the reconfiguring step this could be used.

---
Febreze (and other air fresheners actually) is just below perfumes/colognes, and that's just below dead skunks in terms of smells that offend my nose.
MiquelFire.red | +Me
Windows 8 is a toned, stylish, polished professional athlete. But it’s wearing clown makeup, and that creates a serious image problem. ~PCWorld Article

Aaron Bolyard
Member #7,537
July 2006
avatar

I know you can do all of that. It's just I write my games in Lua generally, so rarely do I care about the C/C++ side. It's easier just to update the keys in a loop and use the array in Lua than it is to do most of the (boring) stuff in C/C++.

[edit] Plus what Miquel said. To find which key was pressed, I need to scan through the array and find the most recent "just-pressed" key. Otherwise, I'd have to do "iskeypressed()" and "readkey()", but those don't work nice in my game (the way I designed it to be Lua-centric, rather than C/C++-centric).

[edit again] I would also like to add that it takes more processing time drawing a 32 x 32 sprite than it does looping over a few keys in an array... Especially once you start mixing in blending modes (this is regarding Allegro 4, where this applies).

OnlineCop
Member #7,919
October 2006
avatar

I would second bamccaig's post, and not just because it's what I've been using myself. Instead of your program worrying about which KEYS were pressed, it worried about what ACTION is being requested.

You may have, at most, 12 different "actions" that the player can perform. Things like UP/DOWN/LEFT/RIGHT, JUMP, FIRE, CONFIRM, CANCEL, etc. Like was mentioned before, there's no real reason to loop through all 50 (or so) possible keys when you only ever use 12 (or less).

In addition, it gives the added benefit of allowing you to map two actions to the same key, if you want. Your FIRE action button can be the same as your CONFIRM action button, CANCEL and RUN can be the same, etc.

I made a VR game!

Go to: