Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Cant control key rate

This thread is locked; no one can reply to it. rss feed Print
Cant control key rate
cpayette
Member #7,348
June 2006

I have a problem. I'm using P to pause my game but the user has to have lightning skills not to have the keyboard read multiple presses. Basically, its hard to pause the game because the input is so fast.

I tried set_keyboard_rate(0,0); and it still refreshes just as fast.

1void Playing_Input()
2{
3 
4 // R - Reset the ball. This is DEBUG
5 if(key[KEY_R] && !Paused)
6 {
7 BallX = 300;
8 BallY = 400;
9 BallSpeedX = BallSpeedY = 1.5;
10 MoveLeft = false;
11 MoveDown = true;
12 }
13 if(key[KEY_P])
14 {
15 if(!Paused)
16 {
17 PausedMouseX = mouse_x;
18 PausedMouseY = mouse_y;
19 }
20 if(Paused)
21 {
22 position_mouse(PausedMouseX, PausedMouseY);
23 }
24 
25 Paused = !Paused; //herin lies the problem, happens too fast
26 }
27 
28 // Clear the key buffer.
29 clear_keybuf();
30 
31}

Ive tried creating a static variable to snapshot the current time, then compare the snapshotted time to the newest current time, but it doesn't seem to work as my timer counts to 240 and resets. Am I using the wrong functions here to get input? Honestly, if allegro had some way to just check for Key UP, it'd be easy. Every other library I've worked with has had keyup and keydown checks, and I've used keyup when I don't want the action repeating.

Edit: I guess I should show this too:

1// The main loop for the playing state. Run until the user hits ESC.
2 while(!key[KEY_ESC])
3 {
4 // Run the "logic" portion of the playing state while our main
5 // timer is 1. At the end it will be reset to 0. Allegro will
6 // manage the timer incrementing.
7 while(gMainTimer > 0)
8 {
9 // Get input, check for collisions, and update each object.
10 Playing_Input();
11 if(!Paused)
12 {
13 Playing_Collision();
14 Playing_Update();
15 }
16 gMainTimer--;
17 }
18 
19 // Render everything.
20 Playing_Render();
21 }

gnolam
Member #2,030
March 2002
avatar

Quote:

I tried set_keyboard_rate(0,0); and it still refreshes just as fast.

That only affects buffered input (i.e. keypresses read with readkey()/ureadkey()).

Anyway:

void do_input_stuff(void)
{
 static int repeat_counter = 0;

 if ((key[KEY_FOO] && (repeat_counter == 0)) {
    do_stuff();
    repeat_counter = 10;
   }

 if (repeat_counter > 10)
    repeat_counter--;
}

Or see this thread.

[EDIT]Fixed the code.

--
Move to the Democratic People's Republic of Vivendi Universal (formerly known as Sweden) - officially democracy- and privacy-free since 2008-06-18!

cpayette
Member #7,348
June 2006

Awesome that helped a lot. I came close to that but couldn't quite get it. FYI, in case this post helps anyone else, make sure you do this:

if(repeat_counter > 0)
repeat_counter--;

Thanks again!

gnolam
Member #2,030
March 2002
avatar

Ah, yes. The dangers of typing example code directly into the code box. ;)

--
Move to the Democratic People's Republic of Vivendi Universal (formerly known as Sweden) - officially democracy- and privacy-free since 2008-06-18!

Go to: