Pause a game

Hi Everyone,

I'm trying to write a function that pauses a game when 'p' is pressed, and then resumes when 'p' is pressed again. Here's what I wrote, but it works only for a long press of 'p' to start the pause (I guess it's because I can't capture the exact moment of the key press). Could you please tell me what adjustment to make so it would work right? Thanks.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

bool pause_state=false;

     if (kbhit()) {
       
     if (GetAsyncKeyState(0x50)==true && pause_call==false) {
        pause_call=true;        
        Sleep(700);
   
        while (!GetAsyncKeyState(0x50)) 
            Sleep(1);             
       
        pause_call=false; 
     
     } 
} 
You probably should be using an event system, not GetAsyncKeyState.

catch WM_CHAR messages instead.


In your message handler:
1
2
3
case WM_CHAR:
  if(wParam == 'p')
    paused = !paused;  // toggle paused state 


Then in your logic loop, simply do nothing / sleep if is paused is true.
Thanks, but I get a compilation error:

269 C:\Dev-Cpp\Projects\Pr\str.cpp case label `258' not within a switch statement
270 C:\Dev-Cpp\Projects\Pr\str.cpp `wParam' undeclared (first use this function)
(Each undeclared identifier is reported only once for each function it appears in.)
I can't find a clear example of how to use WM_CHAR syntax properly, help will be most appreciated...
er... that code goes inside your message handler. Inside your wndproc.

I assume you are doing this with WinAPI. If so you must have a wndproc for your window, right?

Or if this is a console program... see this: http://cplusplus.com/forum/articles/28558/
Topic archived. No new replies allowed.