Advertisement

Infinite Loop input thingy

Started by May 21, 2001 06:06 PM
6 comments, last by Mister Pie 23 years, 8 months ago
Hi all! Sorry for the vague topic on this post but I wasn''t sure how to label it (I''m a beginner in game programming). Anyways, I have one quick question that I''m sure many people here already know the answer to. For my computer science class, we get to do a big project for a final and many people (including me) have chosen to do games. However, there is just one hurdle I can''t get over, which is the handling of input. I know that generally (in a simplistic sense) games run over an infinite loop where over each loop, key presses are either detected (and the game reacts accordingly) or they aren''t (where the game just updates the image on screen or whatever without doing anything). However, the problem is that in my class, we have stuck to the use of "cin" or text-file input in our assignments throughout the year. Now, it seems to me that using "cin" to get input from the user would not be a good idea because "cin" stops the entire program and WAITs there idly until something is entered WITH a carriage return. So, my question is, exactly HOW do I write a function that detects input, but doesn''t say... grind the program to a halt (AKA our good friend "cin" from iostream). Thank you advance for your help.... -Mister Pie
It really depends on which platform you are developing on. Is it a windows application or a console based one etc...
You might want to look into allegro. Its quite simple to use and is sufficient for basic games.

Snale
+--My humble and superior homepage
Advertisement
If you are writing a windows application, you could try short GetAsyncKeyState(int vKey). This function returns true when the key referred to by vKey is down, and false otherwise. The argument vKey refers to a bunch of declares such as VK_SPACE, VK_0, VK_A, etc. Only thing is, you have to #include in order to use the function and have access to the requisite declares.
-david
At this moment, I think I want to take things one step at a time so I would really like to know how to implement this in a console before I learn how to implement it under a windows program. I think the underlying problem I have is just detecting the input, which still eludes me.
If your using C/C++ try kbhit(), out of , here''s an example:

#include
#include
int main()
{
char c;
for (int i = 0; i < 20000; i++)
{ if (!kbhit())
printf("%d\n", i);
else
{ printf( "Key struck was ''%c''\n", getch() );
getch();
}
}
return 0;
}
I seem to have lost a few words in my last post. kbhit() is in conio.h.

and the includes in the sample are conio.h and stdio.h
Advertisement
Thank you, that was exactly what I was looking for. One quick question: what is the variable character 'c' for? It doesn't seem to be used in the sample program.

Also, how exactly do you detect what has been pressed? getchar()? Is there any other way or is that basically it?

Edited by - Mister Pie on May 22, 2001 2:06:35 AM
Actually, I think the variable char c was included by rote, ie it wasn''t necessarily needed. There''s an even more compact way to write the example, which does use c, so I''m assuming Russ started out writing that but switched midway.

Using only getch(), do:

char c = NULL;
if( c = getch()) //only do this part if keyboard hit
{
// respond to input
switch(c)
{
case ''A'': // to respond to ''A'' key on keyboard
// ...
break;
.
.
.
default: break;
}
}
// continue with game loop...


---
Those who can do nothing criticize; those who can, critique.

This topic is closed to new replies.

Advertisement