key define KEY_MAX
Martin Kalbfuß

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

Thomas Fjellstrom

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

Martin Kalbfuß

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.

Erin Maus

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

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

Erin Maus

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ß

Thanks to all.

Neil Walker

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

Erin Maus

Not when you do it dynamically.

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

Neil Walker

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.

bamccaig

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

During the reconfiguring step this could be used.

Erin Maus

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

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.

Thread #599902. Printed from Allegro.cc