Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » [A5] Simple Keypress?

This thread is locked; no one can reply to it. rss feed Print
[A5] Simple Keypress?
Neil Roy
Member #2,229
April 2002
avatar

Is there a simple way with Allegro 5 to just wait for a keypress to continue?

I have a title screen. In A4 I used to use keypress() to wait for the user to press a key to continue.

Please tell me I don't have to go through all the trouble to set up key events and such JUST to wait for a simple keypress to continue?

If Allegro doesn't have a simple method, what is a good way under C++? I am about ready to switch back to simple C programming.

I googled it and get all sorts of stuff about cin.ignore(), cin.clear() etc... etc... and none of them work. I can't see WHY Allegro 5 can't have some basic functions for simple things like waiting for any key to continue. <sigh>

---
“I love you too.” - last words of Wanda Roy

torhu
Member #2,727
September 2002
avatar

Completely not tested, but here you go.

void wait_for_keypress()
{
  ALLEGRO_EVENT_QUEUE *event_queue;
  ALLEGRO_EVENT event;
  
  al_install_keyboard();
  event_queue = al_create_event_queue();
  al_register_event_source(event_queue, al_get_keyboard_event_source());

  do
    al_wait_for_event(event_queue, &event);
  while (event.type != ALLEGRO_EVENT_KEY_DOWN);
  
  al_destroy_event_queue(event_queue);
}

Usually you would have the event loop already, because you need it for other stuff.

By the way, the C and C++ standard libraries don't have any GUI-related functions, just console.

Thomas Fjellstrom
Member #476
June 2000
avatar

Neil Roy said:

I googled it and get all sorts of stuff about cin.ignore(), cin.clear() etc... etc... and none of them work. I can't see WHY Allegro 5 can't have some basic functions for simple things like waiting for any key to continue. <sigh>

for any reasonable program using Allegro 5, it will surely have an event loop already set up. Then all you have to do is sit and wait for a key event.

You don't do things in Allegro 5 the way you did in Allegro 4. Trying to use the same "paradigms" will just cause you headaches.

It's a different api, and a different way of thinking.

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

Neil Roy
Member #2,229
April 2002
avatar

Yeah, it's difficult to re-organize my thinking. I'm used to C and Allegro 4, now trying to use C++ and Allegro 5. I had a hard time sleeping last night, just had programming on my brain. ;D, I realized what my mistake was, and that is I need to create an input class of some sort.

I had set up what was needed for getting input the normal Allegro 5 way, in a loop as discussed in other parts of the forum, but that's no good in a game that has separate functions.

After looking at the demos I noticed A5teroids has an input class, that helped me see how to do it properly I think, I haven't created it yet but I am enthusiastic again. ;) I am SO not used to programming in C++, I was ready to switch back to C but that would make no difference because as mentioned, I noticed that all the input examples online have to do with CONSOLE input, which I find strange in 2012.

One thing I am learning, all the tutorials online in the world can't replace just experience, trial and error. Most of the tutorials give bad examples and teach bad habits that don't work well in actual games.

"The easier your solution is, the harder it will be on you later on" seems to be pretty accurate. :)]

Edit 1: How would you handle keyboard input in C? In C++ you have classes. I assume there would be global variables for events? It would be handy to know as I am still not so certain I want to program in C++. :P

Edit 2: Okay, I got it working without classes for now, although I DO want to try my hand at creating an input class.

I initialize queue and timer etc.. in an initialize() function, timer and queue are global variables (I'm in trouble now)...

Oh, display is also a global, I originally done this so I can have a shutdown() function that can be called from anywhere in the program which destroys bitmaps and other such things. Any suggestions for a better way is welcome, but this works, my globals are all grouped together so I can easily track them.

void initialize()
{
   // other initialize stuff here

   timer = al_create_timer(1.0 / 60);
   queue = al_create_event_queue();
   al_register_event_source(queue, al_get_keyboard_event_source());
   al_register_event_source(queue, al_get_display_event_source(display));
   al_register_event_source(queue, al_get_timer_event_source(timer));
   al_start_timer(timer);
}

and then I have the wait_for_keypress() in it's own function...

void wait_for_keypress()
{
  do
    al_wait_for_event(queue, &event);
  while (event.type != ALLEGRO_EVENT_KEY_DOWN);
}

Now that I have this working, I think it should be easier to translate it into an input class if I so choose.

---
“I love you too.” - last words of Wanda Roy

Thomas Fjellstrom
Member #476
June 2000
avatar

In a C++ allegro 5 project, I'd probably setup a "world" controller class that takes input from allegro via a method, then either acts on that directly, or passes input to various entities in the world. Or both.

Either way, my game entities wouldn't normally take ALLEGRO_EVENT's rather they'd have callback methods for keyUP, keyDOWN or maybe a higher level engine specific event object.

Neil Roy said:

Now that I have this working, I think it should be easier to translate it into an input class if I so choose.

You could do that, but it'd completely block any animation you might want to do, and if the display context is "lost" you'd end up with a completely black screen (or filled with random contents). So that solution isn't entirely ideal. I might suggest setting a flag that your event loop checks for to "pause" the game (block events from being passed to the world entities, but still handle basic stuff like display flipping, and any other basic stuff), and wait for a keypress, or even a specific key. Depending.

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

Neil Roy
Member #2,229
April 2002
avatar

I have this in my main() currently. I still have a lot of work to do on this project and quit a bit will change. I just wanted a simple way to wait for a key press on an opening title screen that will only be seen once.

This works so far, I'm feeling less and less comfortable with C++, currently my project is being compiled as C99. But I may still switch back, old habits die hard. ;)

#SelectExpand
1while(!done) 2 { 3 do 4 { 5 al_wait_for_event(queue, &event); 6 7 if(event.type == ALLEGRO_EVENT_DISPLAY_CLOSE) done = true; 8 9 else if(event.type == ALLEGRO_EVENT_KEY_DOWN) 10 { 11 al_get_keyboard_state(&keys); 12 if(event.keyboard.keycode == ALLEGRO_KEY_ESCAPE) done = true; 13 // code removed for brevity 14 } 15 else if(event.type == ALLEGRO_EVENT_KEY_UP) 16 { 17 al_get_keyboard_state(&keys); 18 // code removed for brevity 19 } 20 21 else if(event.type == ALLEGRO_EVENT_TIMER) 22 { 23 // code removed for brevity 24 redraw = true; 25 } 26 } 27 while(!al_is_event_queue_empty(queue)); 28 29 if(redraw) 30 { 31 redraw = false; 32 33 al_clear_to_color(al_map_rgb(0,0,0)); 34 35 al_draw_text(font_Amaze, al_map_rgb(255,255,0), display_w/2, display_h/2-50, ALLEGRO_ALIGN_CENTRE, "Do stuff here."); 36 37 al_flip_display(); 38 } 39 }

In my old game (this is a new version, I am rewriting) I had code in that detected when the game lost focus (alt+tab) and paused the game. I'm not sure how that works now, if that is what you are talking about. I just started on this recently so it isn't too far along to change.

---
“I love you too.” - last words of Wanda Roy

Thomas Fjellstrom
Member #476
June 2000
avatar

You can detect when the display/window is tab'ed out of, or display is lost for any other reason.

But if you're looping in your wait_for_keypress function, you can't handle any of that. So I just suggested moving that wait logic into the event loop. when you're waiting for a keypress, just skip most of the logic.

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

Neil Roy
Member #2,229
April 2002
avatar

OH, you mean in my wait for key press, change it so that it can detect when the game loses focus? (also in the main game loop)

If it looses focus, skip any key processing until it regains it back.

Also, I used to have my while() loop set up like you originally suggested, then I changed it to add in that do while() loop, now I am wondering if I shouldn't go back to the way you suggested so that if the game loses focus I could more easily jump to the draw section briefly to update the screen?

So much to think about...

---
“I love you too.” - last words of Wanda Roy

Thomas Fjellstrom
Member #476
June 2000
avatar

Neil Roy said:

OH, you mean in my wait for key press, change it so that it can detect when the game loses focus? (also in the main game loop)

That, or make "wait_for_keypress" just set a flag, that the main game loop notices and takes care of.

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

Go to: