Pausing code...?

Hi there,

I’ve been trying to create some code to pause a program (game in this case).
I did a SDL_GetKeyEvent to see if ‘p’ was pressed. then i went into a loop,
and inside that loop i checked if ‘p’ was pressed again. However, this does
not work… Could someone give me a little example of how to do this?

Thnx in advance,
Remenic._____________________________________________________
Richard ‘Remenic’ Stellingwerff
@Richard_Stellingwerf
http://remenic.2y.net

Add a small SDL_Delay to your loop to allow the event thread time to do its
thang.On Wed, Nov 29, 2000 at 11:38:02PM +0000, Remenic wrote:

Hi there,

I’ve been trying to create some code to pause a program (game in this case).
I did a SDL_GetKeyEvent to see if ‘p’ was pressed. then i went into a loop,
and inside that loop i checked if ‘p’ was pressed again. However, this does
not work… Could someone give me a little example of how to do this?

Thnx in advance,
Remenic.


Richard ‘Remenic’ Stellingwerff
remenic at remenic.2y.net
http://remenic.2y.net


Martin

Bother said the Moderator, &$&^%NO CARRIER

Add a small SDL_Delay to your loop to allow the event thread time to do its
thang.

Also add SDL_PumpEvents() so that the internal event state is updated:

while ( SDL_GetKeyState(NULL)[SDLK_p] == SDL_PRESSED ) {
	SDL_Delay(100);
	SDL_PumpEvents();
}

See ya!
-Sam Lantinga, Lead Programmer, Loki Entertainment Software

Hi there,

I’ve been trying to create some code to pause a program (game in this case).
I did a SDL_GetKeyEvent to see if ‘p’ was pressed. then i went into a loop,
and inside that loop i checked if ‘p’ was pressed again. However, this does
not work… Could someone give me a little example of how to do this?

A snippet from my game Circus Linux!'s “pausescreen()” function
(found in “circuslinux.c”, along with everything ELSE (ugh!)):

int pausescreen(void)
{
SDL_Event event;
SDLKey key;
int done, quit;

/* Stop music: */

#ifndef NOSOUND
if (use_sound)
{
Mix_PauseMusic();
}
#endif

/* Display “PAUSED” Message: */

… snipped for brevity …

/* Wait for keypress: */

done = 0;
quit = 0;

do
{
while (SDL_PollEvent(&event))
{
if (event.type == SDL_QUIT)
{
/* Quit event! */

          quit = 2;
        }
      else if (event.type == SDL_KEYDOWN)
        {
          /* A keypress! */

          key = event.key.keysym.sym;

          if (key == SDLK_SPACE || key == SDLK_TAB ||
              key == SDLK_p)
            {
              /* SPACE, TAB or P: Pause! */
              
              done = 1;
            }
          else if (key == SDLK_ESCAPE)
            {
              /* ESCAPE: Quit! */
              
              quit = 1;
            }
        }
    }
}

while (quit == 0 && done == 0);

/* Erase message: */
… snipped for brevity …

/* Unpause music: */

#ifndef NOSOUND
if (use_sound)
{
Mix_ResumeMusic();
}
#endif

return(quit);
}

If this function returns a “2”, then the main loop (which called
this function) will know to end, and quit the program completely.
(An SDL_QUIT event occured in the function… due to the user clicking
the windows “close” button, for example)

If it returns a “1”, then the main loop will end and the program will
return to the title screen. (The user press ESCAPE, which, during the
game, aborts the game… And why shouldn’t it do the same during pause? :slight_smile: )

Oh CRAP… looking at this, I really should be using
SDL_WaitEvent(), NOT SDL_PollEvent()… Crap crap crap…

This function’s loop will eat CPU massively. Crap!

-bill!

Oh CRAP… looking at this, I really should be using
SDL_WaitEvent(), NOT SDL_PollEvent()… Crap crap crap…

Does it redraw if the window gets dirty?

m.On Wed, Nov 29, 2000 at 04:56:38PM -0800, William Kendrick wrote:


Programmer “Ha ha.” “Ha ha.” "What are you laughing at?"
Loki Software "Just the horror of being alive."
http://lokigames.com/~briareos/ - Tony Millionaire

Oh CRAP… looking at this, I really should be using
SDL_WaitEvent(), NOT SDL_PollEvent()… Crap crap crap…

Does it redraw if the window gets dirty?

Yes, that’s handled internally.

See ya,
-Sam Lantinga, Lead Programmer, Loki Entertainment Software> On Wed, Nov 29, 2000 at 04:56:38PM -0800, William Kendrick wrote:

Oh CRAP… looking at this, I really should be using
SDL_WaitEvent(), NOT SDL_PollEvent()… Crap crap crap…

Does it redraw if the window gets dirty?

Hrm. Some things do. Seems to be getting done by SDL itself, not
my code. :wink:

-bill!> On Wed, Nov 29, 2000 at 04:56:38PM -0800, William Kendrick wrote:

Cool, this worked!!!

Thanks!

  • RemenicOn Thursday 30 November 2000 00:18, you wrote:

Also add SDL_PumpEvents() so that the internal event state is updated:

while ( SDL_GetKeyState(NULL)[SDLK_p] == SDL_PRESSED ) {
SDL_Delay(100);
SDL_PumpEvents();
}


Richard ‘Remenic’ Stellingwerff
@Richard_Stellingwerf
http://remenic.2y.net

Great! :slight_smile:

See ya,
-Sam Lantinga, Lead Programmer, Loki Entertainment Software> On Thursday 30 November 2000 00:18, you wrote:

Also add SDL_PumpEvents() so that the internal event state is updated:

while ( SDL_GetKeyState(NULL)[SDLK_p] == SDL_PRESSED ) {
  SDL_Delay(100);
  SDL_PumpEvents();
}

Cool, this worked!!!