ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
#include <stdio.h>
#define FALSE 0
#define TRUE 1
int main(int argc, char *argv[]){
int quit = FALSE;
int i = 0;
int c;
while (quit == FALSE) {
++i;
printf("counter : %d \n", i);
printf("last char : %c \n", c);
c = getchar();
}
return 0;
}
Which runs and sits there waiting for input. Everytime a character is entered and 'enter' is pressed, the counter increases. However - it waits for this input.
How do you go about writing a 'game loop' in C, where the loop continues to run, the counter increases, while simultaneously looking for an input?
Last edited by szboardstretcher; 11-25-2014 at 12:56 PM.
The best way is to write your program as event-driven. When an event such as keyboard input occurs, then the handler for that event runs. The code may be multi-threaded to do this but doesn't have to be. Here's a sample library that should work for C code.
One of the primary problems I am running into with learning programming is my lack of vocabulary. If I knew the lingo, I could easily do a search for this kind of thing.
You have written an endless loop. You will never break out of the loop unless quit is set to TRUE (presumably depending on what is returned by getchar()).
You have written an endless loop. You will never break out of the loop unless quit is set to TRUE (presumably depending on what is returned by getchar()).
Yes, thank you. Do you have any reply to the actual question?
Code:
int flags = fcntl(0, F_GETFL);
fcntl(0, F_SETFL, flags | O_NONBLOCK);
Thank you. Any explanation as to how this answers my question?
Quess your question is: why do you have to press Enter to see anything. It's because of line-buffering. termios is your friend, example program: shkeys.c
int flags = fcntl(0, F_GETFL);
fcntl(0, F_SETFL, flags | O_NONBLOCK);
Thank you. Any explanation as to how this answers my question?
non-blocking io mode meaning read() without anything written to it will return -EAGAIN
iirc
file descriptor 0 is stdin
bdw, normally you would write games using SDL or GLFW
they bout have cross platform input support
GLFW even has 2 types of input functions, when checked (if pressed/released/etc) and a callback function (similar)
note that this most probably won't work on windows
edit:
note that UNICODE can be multibyte and that if the loop is slow there can be multiple chars buffered
so a bigger buffer and handle if bytes read ("err" in this case) is same as asked for
Yes, thank you. Do you have any reply to the actual question?
From the way your OP was worded, I didn't realize that you wanted a function that would not wait until a key was actually pressed before returning.
One way to achieve that would be with the kbhit() function but that is in conio which linux doesn't have. If your system has caca_conio libraries then caca_conio_kbhit() would be an equivalent. (#include <caca_conio.h>).
There were several answers here and the topic is marked as solved, but I don't know if proper solution has been communicated, so let me make it clear:
1. No, you don't want to set O_NONBLOCK.
2. Yes, what NevemTeve wrote is correct, you should listen to him.
3. Yes, what psionl0 wrote is correct, you should listen to him.
Hope this helps to clarify things and clear out confusion.
There were several answers here and the topic is marked as solved, but I don't know if proper solution has been communicated, so let me make it clear:
1. No, you don't want to set O_NONBLOCK.
2. Yes, what NevemTeve wrote is correct, you should listen to him.
3. Yes, what psionl0 wrote is correct if you're running under DOS or Windows.
Hope this helps to clarify things and clear out confusion.
i did say termios would probably be better (dk the use case)
i did say termios would probably be better (dk the use case)
I just wanted to make sure that it's communicated clearly. File descriptor being non-blocking has nothing to do with terminal input buffering, and pointing people to O_NONBLOCK when what they need is termios is unhelpful and creates even more confusion about this already confusing subject.
Quote:
Originally Posted by psionl0
I haven't tested it yet but my linux system has caca_conio installed so my advice should also be applicable for linux.
I've updated my post.
Last edited by mina86; 11-26-2014 at 10:59 AM.
Reason: Made the post little less passive-aggressive.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.