LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-25-2014, 12:54 PM   #1
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Rep: Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694
C loop .. continuation?


I have this code:

Code:
#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.
 
Old 11-25-2014, 01:01 PM   #2
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 4,140

Rep: Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263
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.

http://nikhilm.github.io/uvbook/basics.html
 
1 members found this post helpful.
Old 11-25-2014, 01:15 PM   #3
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Original Poster
Rep: Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694
That is what I am looking for. Thank you.

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.
 
Old 11-25-2014, 01:24 PM   #4
genss
Member
 
Registered: Nov 2013
Posts: 741

Rep: Reputation: Disabled
something like

Code:
int flags = fcntl(0, F_GETFL);
fcntl(0, F_SETFL, flags | O_NONBLOCK);

Last edited by genss; 11-25-2014 at 01:35 PM.
 
Old 11-25-2014, 01:28 PM   #5
psionl0
Member
 
Registered: Jan 2011
Distribution: slackware_64 14.1
Posts: 722
Blog Entries: 2

Rep: Reputation: 124Reputation: 124
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()).
 
Old 11-25-2014, 01:42 PM   #6
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Original Poster
Rep: Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694
Quote:
Originally Posted by psionl0 View Post
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?
 
Old 11-25-2014, 02:08 PM   #7
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,864
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
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
 
1 members found this post helpful.
Old 11-25-2014, 02:15 PM   #8
genss
Member
 
Registered: Nov 2013
Posts: 741

Rep: Reputation: Disabled
Quote:
Originally Posted by szboardstretcher View Post
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?
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)
 
1 members found this post helpful.
Old 11-25-2014, 02:17 PM   #9
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Original Poster
Rep: Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694
So far it seems that libuv is the accepted way of doing this - as explained above.

Also, ncurses allows easy non-blocking input control.

Last edited by szboardstretcher; 11-26-2014 at 01:06 PM.
 
Old 11-25-2014, 02:35 PM   #10
genss
Member
 
Registered: Nov 2013
Posts: 741

Rep: Reputation: Disabled
and i just hacked something :/

Code:
#include <stdio.h>
#include <fcntl.h>

#define FALSE 0
#define  TRUE 1

int main(int argc, char *argv[]){
    int quit = FALSE;    
    int i = 0;
    char c;
    int err;
	
    fcntl(0, F_SETFL, fcntl(0, F_GETFL) | O_NONBLOCK);
	
    while (quit == FALSE) {
        ++i;
        err = read(0, &c, 1);
	if( err > 0 ) {
            printf("counter   : %d \n", i);
            printf("this char : %c \n", c);
	}
    }
    
    return 0;
}
maybe termios would be better

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

Last edited by genss; 11-25-2014 at 02:42 PM.
 
1 members found this post helpful.
Old 11-25-2014, 07:59 PM   #11
psionl0
Member
 
Registered: Jan 2011
Distribution: slackware_64 14.1
Posts: 722
Blog Entries: 2

Rep: Reputation: 124Reputation: 124
Quote:
Originally Posted by szboardstretcher View Post
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>).

Another alternative is suggested here: http://www.flipcode.com/archives/_kbhit_for_Linux.shtml
 
Old 11-26-2014, 07:46 AM   #12
mina86
Member
 
Registered: Aug 2008
Distribution: Debian
Posts: 517

Rep: Reputation: 229Reputation: 229Reputation: 229
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.

Last edited by mina86; 11-26-2014 at 10:30 AM.
 
Old 11-26-2014, 08:09 AM   #13
genss
Member
 
Registered: Nov 2013
Posts: 741

Rep: Reputation: Disabled
Quote:
Originally Posted by mina86 View Post
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)

http://pubs.opengroup.org/onlinepubs...d/termios.html
and man termios explain it,
the code linked to is too complicated for a newbie

Last edited by genss; 11-26-2014 at 08:11 AM.
 
Old 11-26-2014, 09:15 AM   #14
psionl0
Member
 
Registered: Jan 2011
Distribution: slackware_64 14.1
Posts: 722
Blog Entries: 2

Rep: Reputation: 124Reputation: 124
Quote:
Originally Posted by mina86 View Post
3. Yes, what psionl0 wrote is correct if you're running under DOS or Windows.
I haven't tested it yet but my linux system has caca_conio installed so my advice should also be applicable for linux.
 
Old 11-26-2014, 10:55 AM   #15
mina86
Member
 
Registered: Aug 2008
Distribution: Debian
Posts: 517

Rep: Reputation: 229Reputation: 229Reputation: 229
Quote:
Originally Posted by genss View Post
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 View Post
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.
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
html: line continuation for strings stateless Programming 5 02-18-2014 04:41 AM
how to find continuation of line with PERL menylea Linux - Newbie 2 03-14-2012 01:51 AM
continuation cleopard Programming 1 04-10-2008 12:16 PM
wget continuation problem ksd Linux - Software 2 10-20-2003 10:51 AM
p4pe sound problem continuation shadeXIII Linux - Hardware 1 06-06-2003 10:20 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:36 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration