|
Home > Archive > Unix Programming > April 2006 > _pause() and _continue()
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
_pause() and _continue()
|
|
| DaVinci 2006-04-27, 7:55 am |
| I am writing a ncurses game,ping-pong,
void start()
{
//I had done something
}
void _pause()
//when the user press 'p',the game will pause
{
//??how to implement?
}
void _continue()
//when user press 'c',the game will continue to run
{
//??how to implement?
}
void myexit()
{
//I had done something
}
I had tried to write a sig_handle() function,but it looks not very
good.
any suggestion is appreciate.
Thanks very much.
| |
| Diomidis Spinellis 2006-04-27, 7:55 am |
| DaVinci wrote:
> I am writing a ncurses game,ping-pong,
> void _pause()
> //when the user press 'p',the game will pause
> {
> //??how to implement?
> }
Simply enter a state where you read and discard characters, until you
get an EOF or a 'p'.
| |
| Nils O. Selåsdal 2006-04-27, 7:55 am |
| DaVinci wrote:
> I am writing a ncurses game,ping-pong,
> void start()
> {
> //I had done something
> }
>
> void _pause()
> //when the user press 'p',the game will pause
> {
> //??how to implement?
> }
> void _continue()
> //when user press 'c',the game will continue to run
> {
> //??how to implement?
> }
>
> void myexit()
> {
> //I had done something
> }
>
>
> I had tried to write a sig_handle() function,but it looks not very
> good.
use e.g. getchar to read input. If the character is a 'p', you call your
pause function.
Also DO NOT prefix your function names with _ , those are reserved for
the implementation.
| |
| DaVinci 2006-04-27, 7:55 am |
|
Nils O. Sel=E5sdal wrote:
> DaVinci wrote:
> use e.g. getchar to read input. If the character is a 'p', you call your
> pause function.
thanks.
but the most important thing is that how to
wake up the process when I enter character 'c'.
> Also DO NOT prefix your function names with _ , those are reserved for
> the implementation.
| |
| DaVinci 2006-04-27, 7:55 am |
|
Diomidis Spinellis wrote:
> DaVinci wrote:
>
> Simply enter a state where you read and discard characters, until you
> get an EOF or a 'p'.
#include <signal.h>
#include <unistd.h>
#include <iostream>
void sig_cont(int signo)
{
std::cout<<"sig_cont ()"<<std::endl;
return ;
}
int main()
{
signal(SIGCONT,sig_cont);
char ch;
while(1)
{
signal(SIGCONT,sig_cont);
ch = getchar();
if(ch == 'p')
{
pause();
}
else if(ch == 'c')
{
kill(getpid(),SIGCONT);
std::cout<<"send signal SIGCONT"<<std::endl;
}
else
{
std::cout<<"hello world"<<std::endl;
}
}
}
when I programme had paused ,how can I wake up it? how can I make it
continue to run?
| |
| Diomidis Spinellis 2006-04-27, 7:55 am |
| DaVinci wrote:
> Diomidis Spinellis wrote:
>
> #include <signal.h>
> #include <unistd.h>
> #include <iostream>
> void sig_cont(int signo)
> {
> std::cout<<"sig_cont ()"<<std::endl;
> return ;
> }
>
> int main()
> {
> signal(SIGCONT,sig_cont);
> char ch;
> while(1)
> {
> signal(SIGCONT,sig_cont);
> ch = getchar();
> if(ch == 'p')
> {
> pause();
> }
> else if(ch == 'c')
> {
> kill(getpid(),SIGCONT);
> std::cout<<"send signal SIGCONT"<<std::endl;
> }
> else
> {
> std::cout<<"hello world"<<std::endl;
> }
> }
>
> }
> when I programme had paused ,how can I wake up it? how can I make it
> continue to run?
>
Don't pause the program with SIGTSTP, because then only another external
process can send you a SIGCONT.
--
Diomidis Spinellis
Code Quality: The Open Source Perspective (Addison-Wesley 2006)
http://www.spinellis.gr/codequality
| |
| Nils O. Selåsdal 2006-04-27, 7:55 am |
| DaVinci wrote:
> Nils O. Selåsdal wrote:
> thanks.
> but the most important thing is that how to
> wake up the process when I enter character 'c'.
You'd just pause the game - e.g. wait for user to hit 'c'
Perhaps something as simple as,
void pause_game(void)
{
int c;
while((c = getchar()) != EOF && c != 'c')
;
}
|
|
|
|
|