C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 12-01-2009, 06:30 PM   #1
Registered User
 
Join Date: Nov 2009
Posts: 11
Using curses

Hey,
I've built a very simple and small 'Lunar Lander' game using a combination of timers and curses. I've come to a point thought where I'd like to improve it and I'm stumped at what to do about a particular issue.

From how I've organized me curses, I can only detect on key press at a time. My basic loop structure is this:

Code:
while(1)
{
	c = getch();
	if (c != ERR)
	{
		if (c == ' ')
		{
		........
		}
		else if (c == 't')
		{
		.....
		}
	}
}
What happens because of this is that actions will be interrupted because of other key presses. Can I get past this limitation while still using curses? If there's a better way without curses I'd also like to know.

Thanks.
TheDenominater is offline   Reply With Quote
Old 12-01-2009, 06:42 PM   #2
+++ OK NO CARRIER
 
quzah's Avatar
 
Join Date: Oct 2001
Posts: 11,317
Push them all onto a queue, stack, whatever, as you press them, and pull them off as you run through your game loop.

Quzah.
__________________
Hundreds of thousands of dipshits can't be wrong.


Are you up for the suck?
quzah is offline   Reply With Quote
Old 12-01-2009, 06:45 PM   #3
dat is, vast staat
 
MK27's Avatar
 
Join Date: Jul 2008
Location: SE Queens
Posts: 6,612
Make the condition:
Code:
while (ch=getch()) {
And then do EVERYTHING inside that loop. This guarantees ch can only have one value per iteration, so nothing has to be interrupted to check the keyboard.

You could use quzah's suggestion, but 1) I don't think it will be so simple unless you are threading this; 2) the loop should execute rapidly enough that this is not a real issue; 3) users hitting several key simultaneously need to learn to use the interface properly
__________________
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
GDB tutorial #1 -- gnu debugger tutorials -- GDB tutorial #2
cpwiki -- our wiki on sourceforge

Last edited by MK27; 12-01-2009 at 06:48 PM.
MK27 is offline   Reply With Quote
Old 12-01-2009, 06:59 PM   #4
Registered User
 
Join Date: Nov 2009
Posts: 11
Hey,
Thanks for the replies. I'm not quite sure either of those will work if I understand them correctly. I should explain myself more.

My code consists mainly of two part: that continuous while loop for input and a timer function that gets called every 0.05 seconds. The way it's set up now pressing a key will set a variable that affects the outcome of the timer function. If you release that key the variable will return to it's 'un-pressed' state.

The problem I am facing now is that I want simultaneous key presses but I don't have them. Say the user is holding down the space-bar, but wants to press an arrow key. Pressing the arrow key will stop the loop from setting the 'space-bar' variable, but that's not what should happen, the space-bar should not be interrupted and continue after the user releases the arrow key.

I have a feeling I might have to fundamentally change how it works so that I can do that.


P.S. A related problem I have is that when you press a key it sets the value, pauses for a second, then start setting the value continuously. The same thing that happens as when you're typing and you hold down a key. I also want to change that.
TheDenominater is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Curses + Mouse control guesst Linux Programming 0 05-26-2008 03:06 PM
Curses keyboard input without pause guesst Linux Programming 3 04-14-2008 10:22 AM
Just wrote my first Curses program guesst Linux Programming 14 04-02-2008 10:44 AM
output an array to curses, or to another window? trev456 C++ Programming 2 04-18-2007 10:08 AM
Fonts and colors in curses Unregistered C Programming 1 02-26-2002 06:22 AM


All times are GMT -6. The time now is 08:31 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22