Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Pause buttons vs. movement buttons

This thread is locked; no one can reply to it. rss feed Print
Pause buttons vs. movement buttons
Raf Vermeulen
Member #7,166
April 2006

Does anybody here know if there're commands or ways integrated in Allegro that allows different kinds of buttons?

What I mean is if there's some way to have two kinds of buttons: 1) Buttons that keep repeating the commands whenever you press them, and 2) Buttons that perform the commands, then waits a bit before it can perform it again.

Why I ask this, is because I'm trying to implement a pause button, but when I press it, the game quickly pauses and unpauses itself, just to pause and unpause again, even though I press the button for a fraction of a second. That's obviously not the way it should be, even though it's the way movement keys should work (which they do)

Matthew Leverton
Supreme Loser
January 1999
avatar

In short, no*. This is just a simple logical thing that you should be able to figure out intuitively as a programmer.

if (key[KEY_ESC]) // user pressed it
{
   while (key[KEY_ESC]); // user is still pressing it

   // user let go

   game_paused = 1 - game_paused; // pause or unpause
}

This is a rather lame method (because it locks up the game while the key is being held down), but it works. If you think about it a bit harder, you can come up with something maybe a bit better.


* You can use keypressed() and readkey(), but they don't play nice with key[].

Kris Asick
Member #1,424
July 2001

First off, there are issues with using the actual pause key. You may want to skip on using that specific key and see if that solves your problems.

If not, the way I do a pause routine is to first, check for the pause key in my game loop. When the keypress is detected, I show the pause screen then create an empty while loop that isn't exited until the key[] variable for the pause key reports FALSE. Then, I simply test for when the pause key is pushed again, have another while loop for the same purpose, then finally return control to the program.

Like this:

#define PAUSE_KEY  KEY_P

void PauseRoutine (void)
{
  DisplayPauseWindow();

  while (key[PAUSE_KEY]) { }

  do {
    GameSync(); // Also updates keyboard buffer.
  } while (!key[PAUSE_KEY]);

  while (key[PAUSE_KEY]) { }      
}

--- Kris Asick (Gemini)
--- http://www.pixelships.com

--- Kris Asick (Gemini)
--- http://www.pixelships.com

Albin Engström
Member #8,110
December 2006
avatar

This is something you have to do yourself, i don't know how much experience you have with programming and allegro but there's no such thing to play with (that i know of), you'll have to make it yourself, and that wont be hard :).

loop
{
if(key[KEY_M]&&slowermamacanpress==true)
{dothestuff();}

if(key[KEY_N]&&mama==true)
{dothatthingstuff();}

if(key[KEY_B]&&thiskeyhasbeenpressed==false)
{dotheotherstuff();thiskeyhasbeenpressed=true;}

if(!key[KEY_B])
{thiskeyhasbeenpressed=false;}

if(slowermama>=10)
{slowermama=0;slowermamacanpress=true;}

onetimemama=true;
}

millisecondtimer
{
mama=true;
slowermama++;
}

that was fast (maybe too fast) correct my faults but don't think i made them thinking it would be that way... :)

[EDIT]
2 answers before me? and i thougth i was first...

Raf Vermeulen
Member #7,166
April 2006

Thanks. I know it's up to me then, and wouldn't be improper conduct to go further along the way I've been going. Just added something to my code, and it seems to work quite well now.

1void special_keys() {
2 if (key[KEY_SPACE]) {
3 if (pause == false) pause = true;
4 else pause = false;
5 clear_keybuf();
6 rest(200);
7 }
8}
9 
10 
11void main_game() {
12 int count=counter;
13 for(;;) {
14 while ( count == counter ) rest(20);
15 count=counter;
16 if(key[KEY_ESC] || exit_application) break;
17 clear_keybuf();
18 special_keys();
19 if (pause == false) {
20 movement();
21 add_monster();
22 movement_monsters();
23 Tile::increment_delay();
24 }
25 draw();
26 }
27}

This works quite well:D

Albin Engström
Member #8,110
December 2006
avatar

^^' ... gratz...

Onewing
Member #6,152
August 2005
avatar

Once again, I'm going to put in my two cents about what I do, I guess food for thought.

I have a global game structure/class (doesn't really matter) which I use to store things about the game. One of the variables is as follows:

GAME_STATE state;

Where GAME_STATE is an enum with values like {NO_STATE, MAIN_MENU, PLAY_GAME, PAUSE, ...}. Now the game loop contains two function calls, one for updating the game and one for drawing the game. The update function figures out what the current state is and proceeds to all the actions of that state.

Hence, the PAUSE state doesn't update any of the other objects in the game and is simply waiting for whatever the unpause key is to be pressed. Of course, this still leaves you with the problem of going into that state and leaving it quickly if the pause/unpause key is the same (which it most likely is). From here, you can use methods above or create a variable to track how much time is passed (that is, if they hold the pause/unpause key, it will still move between the states, but not quickly and at a more controlled rate).

I'm probably just going overkill here, but this state method works really well when you start having several different parts to a game. My current game has over 15 different game states. :)

------------
Solo-Games.org | My Tech Blog: The Digital Helm

Raf Vermeulen
Member #7,166
April 2006

Would that system allow a button to turn music on/off, too?

Onewing
Member #6,152
August 2005
avatar

I don't think MUSIC_ON and MUSIC_OFF would be a state of the game. What I do, is create an audio class that manages my sfx and music. I store volumes there, because I've never needed a music on/off option, but that wouldn't be difficult (since it would just be bool).

------------
Solo-Games.org | My Tech Blog: The Digital Helm

Archon
Member #4,195
January 2004
avatar

Quote:

game_paused = 1 - game_paused; // pause or unpause

Isn't this better:

bool game_paused = false;
//later...
game_paused = !game_paused;

?

Matthew Leverton
Supreme Loser
January 1999
avatar

If by "better" you mean doesn't work in C, then yeah. But regardless of that, trying to optimize how fast you toggle the game_pause state is like giving the most fat kid in the room the candy bar.

Archon
Member #4,195
January 2004
avatar

Quote:

If by "better" you mean doesn't work in C, then yeah.

Doesn't it work in C++?

Quote:

But regardless of that, trying to optimize how fast you toggle the game_pause state is like giving the most fat kid in the room the candy bar.

I don't understand that analogy.

Matthew Leverton
Supreme Loser
January 1999
avatar

Of course it works in C++, but the original poster never said he was using C++. Obviously he is by his later posts.

The best method in this case is the one most understandable to you. The computer isn't going to care what you use. In C++, I would use a boolean for this because that's what they are there for, but I wouldn't lose any sleep if someone didn't.

Archon
Member #4,195
January 2004
avatar

Quote:

I would use a boolean for this because that's what they are there for, but I wouldn't lose any sleep if someone didn't.

I bet you would! I've lost sleep due to my indecision of whether to use a linked list or a vector :-[

Go to: