hi,
does anyone knows if is there a way to make getchar non blocking?
I'm using curses and I'm on linux platform. With a thread version of the same program I have solved my problems with getch and with the timeout function but now the program doesn't work properly and I must use
Using timeout(1) makes non blocking only the c=getch part of this code but not the getchar one.Code:c=getchar(); if (c==0) c=getch();
I have tried to solve this problem using this function
and this codeCode:int kbhit(void) { struct timeval tv; fd_set read_fd; tv.tv_sec=0; tv.tv_usec=0; FD_ZERO(&read_fd); FD_SET(0,&read_fd); if(select(1, &read_fd, NULL, NULL, &tv) == -1) return 0; if(FD_ISSET(0,&read_fd)) return 1; return 0; }
but the program doesn't work properly...Code:if (kbhit()) c = getchar();
Does anyone knows a solution?
I'm 5 days in a row searching of solve this big problem unhappy
I don't understand what exactly you're trying to do, can you be more specific?
I've no experience with curses or linux programming in general, so I'll probably be unable to help you.
thx for the interest.
I'm making a videogame like spaceinvaders and I want get input with getchar in way the getchar function doesn't block the program if no input by the user is pressed.. Curses provides two functions (getch and timeout) but they don't work in this case and I must use getchar..
I hope I made myself clear
I think you need multithreading. And do you really have to use getchar()?
getchar() expects you to press enter, before it "takes" the input - getch() doesn't, it "takes" exactly when you press it.
In multithreading you're running two (or more) threads, with different tasks. In your project, a thread could have the task to get user input - without disturbing other threads (of course a bit, but the user will not see or feel it).
I know enough of linux programming to know that you can use the library pthread, for multithreading. You can create a thread with pthread_create() and stop it using pthread_join(). The prototype for one of the arguments passed to pthread_create(), a function always have to be like this;
In the thread function, you can do something like this, to get the userinput, while in the game.Code:#include <pthread.h> // Just to show, what to include, if using the functions. // ... void *thread_function_name(void *argument_name);
It's pretty primitive, but I hope you get the idea. If using getchar() the user have to press enter, as said before, before the input will get "accepted".Code:char c; while(1) // Forever { sleep(??); // Sleep for a little to prevent full CPU usage if(c = getch()) printf("You typed, while in game: %c!\n", c); // What did the user type? }
I can't make any examples about it all, because I'm not experienced with Linux, but I hope it was enough to get you started.
Last edited by v0id; 04-19-2007 at 11:20 AM.
hi, unfortunately I can't use threads because my prof forbid it. I must use only multiprocessing and System V objects like shared memory and semaphores.
I need to use getchar because getch isn't working properly with the use of System V objects.. It is very very strange
thanks for the advices anyway![]()
I'm afraid I can't help you then, sorry.
Good luck! :-)
(I'll be glad to hear about your solution, when the time comes)
don't you worry, thanks anyway.
Anyone that can help me?
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks