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? )
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!