Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
453 views
in Technique[技术] by (71.8m points)

c - 有没有办法在等待输入时保持程序运行?(Is there a way to keep the program running while waiting for input?)

We have an assignment to create a game in C. We can't use any external headers, and it should run on console.

(我们分配了一个用C创建游戏的任务。我们不能使用任何外部标头,并且应在控制台上运行。)

I thought it would be cool to have animations going on.

(我认为动画继续进行会很酷。)

We already have a walking character and everything (printing matrix, updating, clearing screen, reprinting...), but it would be nice to have things such as running water, for example.

(我们已经有了行走的角色,并且拥有一切(打印矩阵,更新,清除屏幕,重印...),但是拥有诸如自来水之类的东西会很好。)

To do that, I'd need a way to keep the program running while waiting for an input.

(为此,我需要一种在等待输入的同时保持程序运行的方法。)

As it currently is, the animations would stop as the user is prompted for an input (and he'll constantly be asked for directions), so it wouldn't work.

(按照目前的状态,动画将在提示用户输入内容时停止播放(并且会不断询问他的方向),因此它将无法正常工作。)

I'm wondering if there'd be a way to keep the animation running while the input prompt is still going, maybe having a timeout for the input function and re-prompting every frame until the user starts typing.

(我想知道是否有一种方法可以在输入提示仍在继续的同时保持动画运行,也许输入功能超时并重新提示每一帧,直到用户开始输入为止。)

We're familiar with C, but not with any of the obscure functions and headers that could be used to work around this problem.

(我们熟悉C,但不熟悉可用于解决此问题的任何晦涩的函数和标头。)

How can I keep the program running while waiting for input?

(等待输入时如何保持程序运行?)

  ask by Azeew translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use the select to monitor a file or a group of file descriptors until they have input available.

(您可以使用select监视一个文件或一组文件描述符,直到它们具有可用的输入。)

Depending on the structure of your program you could also use asynchronous input where a callback is called when I/O is signaled as ready for a given file descriptor.

(根据程序的结构,您还可以使用异步输入,其中当发信号通知I / O准备好给定文件描述符时,将调用回调。)

The rough snippet below shows the necessary methodology to allow for the callback action_handler to be called when input becomes available on the target file description, which emittes SIGIO.

(下面的粗略片段显示了必要的方法,该方法允许在目标文件描述中的输入变为可用时调用回调action_handler ,从而发出SIGIO。)

This allows for the input to be processed when it arrives, if it arrives.

(这允许输入到达时对其进行处理(如果到达)。)

Using a socket (lets assume UDP) you would have something similar to the following.

(使用套接字(假设使用UDP),您将具有类似于以下内容的内容。)

#include <fcntl.h>
#include <signal.h>
#include <sys/socket.h>
...
create and bind socket
...

/** Set sigaction to use sigaction callback and not handler */
act.sa_flags = SA_SIGINFO; // Enables sigaction instead of handler
act.sa_sigaction = action_handler; // Callback function

/** Set mask to ignore all signals except SIGIO during execution of handler */
sigfillset(&act.sa_mask); // Mask all
sigdelset(&act.sa_mask, SIGIO); // Clear SIGIO from mask

/** Set callback for SIGIO */
sigaction(SIGIO, &act, NULL)

/** Set socket io signals to async and make sure SIGIO is sent to current
* process when a TCP connection is made on the socket
* */
file_status = fcntl(socket_fd, F_GETFL); // Get current status
file_status |= O_ASYNC | O_NONBLOCK;
fcntl(socket_fd, F_SETFL, file_status); // Set modified status flags
fcntl(socket_fd, F_SETSIG, SIGIO); // Produce a SIGIO signal when i/o is possible 
fcntl(socket_fd, F_SETOWN, getpid()); // Make sure SIGIO signal is sent to current process

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...