![]() |
|
problem with static struct |
Murasame
Member #15,209
July 2013
|
Okay, this is in correlation to a problem I had earlier. I am attempting to create a version of geyKeyboardState that runs on events, so instead of updating it constantly, it only updates when a new event occurs. for those of you who don't understand why I do it this way, you have to call al_get_keyboard_state(foo) every time the keyboard state needs to update. it does not do so automatically, and any reallocation is not done until you tell it to. and since I use events for everything else, why not use it for this too? My Current solution uses a struct: 1//KeyHeld header file
2#ifndef KEYHELD_H
3#define KEYHELD_H
4#include "allegro5/allegro.h"
5struct keyHeld
6{
7 static bool isKeyHeld [ALLEGRO_KEY_MAX];
8 static void refocus(ALLEGRO_KEYBOARD_STATE *);//should be called whenever the game regains focus
9 static void keyDown(int);
10 static void keyUp(int);
11};
12#endif
and 1#include "keyHeld.h"
2
3void keyHeld::refocus(ALLEGRO_KEYBOARD_STATE * keys)
4{
5 for (unsigned int x = 0; x<ALLEGRO_KEY_MAX; x++)
6 {
7 if (al_key_down(keys,x))
8 {
9 isKeyHeld[x] = true;
10 }
11 else
12 {
13 isKeyHeld[x] = false;
14 }
15 }
16}
17void keyHeld::keyDown(int keycode)
18{
19 isKeyHeld[keycode] = true;
20}
21void keyHeld::keyUp(int keycode)
22{
23 isKeyHeld[keycode] = false;
24}
and is called in main like so: 1#include "keyHeld.h"
2//one of many includes. not all are listed here to save your eyeballs
3int main(int argc, char **argv)
4{
5ALLEGRO_EVENT_QUEUE *event_queue = NULL;
6 event_queue = al_create_event_queue();
7 if(!event_queue) {
8 fprintf(stderr, "failed to create event_queue!\n");
9 al_destroy_display(display);
10 al_destroy_timer(timer);
11 return -1;
12 }
13//skips some data here too
14 al_register_event_source(event_queue, al_get_display_event_source(display));//registers display events
15
16 al_register_event_source(event_queue, al_get_timer_event_source(timer));//registers timer events
17
18 al_register_event_source(event_queue, al_get_keyboard_event_source());//registers events for keypress
19
20 ALLEGRO_EVENT ev;//for event listener
21//... skips a lot of code here that does not affect anything dealing with this.
22 else if (ev.type == ALLEGRO_EVENT_KEY_DOWN) //if a key is pressed
23 {
24 cout << "registers a keypress";
25 keyHeld.keyDown(ev.keyboard.keycode);
26 }
my problem is that I get compile errors wherever I put the word keyHeld in main. the error is always expected a ";" before ".". I had a buddy look at it, and he didn't know what the problem was either. perhaps we are both a bit sleep deprived, and overlooked something stupid. regardless, any help would be appreciated |
l j
Member #10,584
January 2009
![]() |
keyHeld::keyDown(ev.keyboard.keycode);?
|
Murasame
Member #15,209
July 2013
|
I now feel stupid. I guess fatigue really can screw you up. thanks for the really simple fix. I cannot believe we missed that |
|