|
key define KEY_MAX |
Martin Kalbfuß
Member #9,131
October 2007
|
What is it good for? Is it only used internally? http://remote-lisp.spdns.de -- my server side lisp interpreter |
Thomas Fjellstrom
Member #476
June 2000
|
It lets you know how big the key array is for one. -- |
Martin Kalbfuß
Member #9,131
October 2007
|
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 |
Erin Maus
Member #7,537
July 2006
|
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
|
If you only use 4 keys in your game it's poinless looping round 50 Neil. wii:0356-1384-6687-2022, kart:3308-4806-6002. XBOX:chucklepie |
Erin Maus
Member #7,537
July 2006
|
Neil Walker said: 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
|
Thanks to all. http://remote-lisp.spdns.de -- my server side lisp interpreter |
Neil Walker
Member #210
April 2000
|
yes, you keep a reference to the keys you redefine, you don't need to loop through them all. Neil. wii:0356-1384-6687-2022, kart:3308-4806-6002. XBOX:chucklepie |
Erin Maus
Member #7,537
July 2006
|
Not when you do it dynamically. [edit] And looping fifty times in a game won't kill performance. --- |
Neil Walker
Member #210
April 2000
|
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. wii:0356-1384-6687-2022, kart:3308-4806-6002. XBOX:chucklepie |
bamccaig
Member #7,536
July 2006
|
Aaron Bolyard said: 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: 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... */
-- 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 |
MiquelFire
Member #3,110
January 2003
|
During the reconfiguring step this could be used. --- |
Erin Maus
Member #7,537
July 2006
|
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
|
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.
|
|