is there a std::cin method similar to allegro's keypressed?.
for example, if I like to make simple loop a exit with key A:
// allegro's key while(!key[KEY_A]) { /* do something */ } // std::cin while(!cin.empty()) { /* do something */ }
Of course, there's not an 'empty()' method for cin.
Thanks in advance!.
You would need to use unicode or make your own functions to hold the unicode. But if you are just waiting for a keypress(); to close a program then I use something like this:
#include <iostream> int main(int argc, char *argv[]) { char keypress; std::cout << "Enter a character and hit enter" << endl; std::cin >> keypress; return(0); }
Another method I use is this:
#include <iostream> #include <stdlib.h> int main(int argc, char *argv[]) { system("PAUSE"); return(0); }
your code block the program, and I dont want this.
[EDIT:] ...if the exit loop key wasn't pressed the program must continue
use readkey then, dont think im getting what your asking either, elaborate?.
Edit
Just read cin into a value and if that value is "esc", break the loop.
meant, ie
if( keypressed() ) { readkey(); }
in example, I like to call every game loop a function.
I break the loop when the key A was pressed.
Allegro isnt initialized yet, I need to use cin.
I will try:
for(;;) { if(cin.rdbuf()->in_avail()) { char c; cin.get(c); if(c=='A') break; } function(); }
Just read cin into a value and if that value is "esc", break the loop.
Nope, this will block the program.
If you are using a if(){} to do it then you would need to do something like this:
if(key[KEY_ESC]) { // close program or function you are running }
But there are two other ways to say this
do{ // continue doing program }while(!key[KEY_ESC]); // close program once esc is pressed
Or another way of the above do{}while(); loop:
while(!key[KEY_ESC]) { // run program until esc is pressed } return (0);
He wants to avoid using allegro for some reason, cin isnt realy suited for it tho (IMO).
Edit
Disregard the first reply, misread a bit
Any particular reason you cant use allegro to check this input?
yes, I have a little program to configure some things, and I dont need allegro while it is running.
If I cant find a way to do something like keypressed() without allegro, Ill init() and exit() it, but of course, if theres a way I will not install it for a simple keypressed().
thanks anyway!.
The standard C and C++ libraries don't have functions that report if a key is pressed, they will only report input after return has been pressed. You'll have to resort to platform-specific functions to detect keypresses, or use a third-party library such as Allegro or curses.
Thanks!
You have pure C functions to modify the buffer stream size. You can resize them to a minimal capacity. That's the first thing you must do (at least if you don't want a "queue" of keys pressed). The next thing you must do is find a way to check if the buffer has a key pressed with a non-blocking method. How can you do that? I think you can create a thread that checks if cin is empty or not, and if not what is the character that contains it.
How can you do that? I think you can create a thread that checks if cin is empty or not, and if not what is the character that contains it.
I dont have any idea about how a thread can help me.
Can you explain a little more.
I it seems that nothing is stored in the cin.rdbuf() until you press enter.
Scanning the input buffer parallel to the game processing. The thread will wait a key pressed, but the game stills running.
Can you use assembly? I haven't started read about it but I think it will help you here. IIRC you simply have to call an interrupt which returns the keyboard state (or something similar).
Scanning the input buffer parallel to the game processing. The thread will wait a key pressed, but the game stills running.
Can you use assembly? I haven't started read about it but I think it will help you here. IIRC you simply have to call an interrupt which returns the keyboard state (or something similar).
very good ideas!.
anyway, they are platform dependent . But still good!
(Edit: Nevermind, someone already mentioned threads)
of course, this is a very good idea.
but c/c++ cant handle threads without a lib.
Um, isn't conio.h a windows-only header?
You have pure C functions to modify the buffer stream size.
I don't think so. Do you have a link?
The next thing you must do is find a way to check if the buffer has a key pressed with a non-blocking method. How can you do that? I think you can create a thread that checks if cin is empty or not, and if not what is the character that contains it.
You can't. Your program doesn't get to see the input until return is pressed. At least not on some systems.
Um, isn't conio.h a windows-only header?
DOS only, I think. There may be ports to Linux console.
Hmm... Im sure theres a way to do it. Two ideas:
Play around with the std::cin.peek() method, it might be able to get input without blocking.
If that doesnt work, there has to be a way to insert a new line into the stream using some obscure streaming class. If you can postfix a newline, then std::cin.getline() will return immediately. I'd look into this more, but its not my project. If i get bored enough Ill test my hypothesis.
ANSI/ISO C No
POSIX No
One question, conio.h is only for windows?
Perhaps kbkey() is one of those no-portable functions, that are posted to every system...
I'll try with peek() too.
Thank you very much, I'll try this and... I WILL BACK
conio.h is DOS only not windows. Windows isn't DOS, DOS isn't Windows.
then we have problems again!.
I havent tried yet peek() but i dont think i will work, since the rdbuf()->in_avail() returns 0 even if the user is writing.
Is unicode cross-platform or OS-specific?
Well, Bjarne use my method in his book:
if(cin.rdbuf()->in_avail()) { char c; cin.get(c); /* something */ } else { /* something else */ }
And this doesnt work.
Hmm... Im sure theres a way to do it
Sure there's a way to do it. Just not a standard cross-platform way using only standard library functions.
Is unicode cross-platform or OS-specific?
Unicode is standardized and as that, should be cross-platform. Never had any problems with unicode, in none of the OSes I use.
conio.h is a borland library. that is, it is not standard, So don't use it.
and about threads, in linux you can use the standard POSIX threads, and in windows too, but using a special wrapper: I don't remember its name, but you can search google .
You can use select. The man page of select has an example:
1 | #include <stdio.h> |
2 | #include <sys/time. |
3 | #include <sys/types.h> |
4 | #include <unistd.h> |
5 | |
6 | int |
7 | main(void) |
8 | { |
9 | fd_set rfds; |
10 | struct timeval tv; |
11 | int retval; |
12 | |
13 | /* Watch stdin (fd 0) to see when it has input. */ |
14 | FD_ZERO(&rfds); |
15 | FD_SET(0, &rfds); |
16 | /* Wait up to five seconds. */ |
17 | tv.tv_sec = 5; |
18 | tv.tv_usec = 0; |
19 | |
20 | retval = select(1, &rfds, NULL, NULL, &tv); |
21 | /* Don't rely on the value of tv now! */ |
22 | |
23 | if (retval) |
24 | printf("Data is available now.\n"); |
25 | /* FD_ISSET(0, &rfds) will be true. */ |
26 | else |
27 | printf("No data within five seconds.\n"); |
28 | |
29 | exit(0); |
30 | } |
There is no way to do this portably. The console defined by C++ only sends its output when the enter key is pressed, so there is no way to see characters before that happens. C/C++ streams are not interactive user interfaces!
You have to use Allegro, or conio, or curses, or other things to get it to work, but the solution either won't work on all of your platforms unless you get libraries like Allegro.
Remember these languages and similar ones were around starting from the 60s when dumb terminals were prevalent. You actually had to send codes back to the terminals for them to even send keypresses over the wire, otherwise they wouldn't send until enter was pressed. Libraries like ncurses expose these historial things out in the open in its interface.
Hmm... how about using ioctl? I remember I wrote a small port for Linux when programming with conio. I will see if I can find it.
Hmm... how about using ioctl?
Not portable. In the extreme.
But every platform should have a function similar to it at least. Hmm... oh, well. Nobody is perfect
Yeah, you can do it on any platform (nowadays anyway), but you'll have to use platform specific methods for all of them.
Well, actually, you could probably just use curses on all of them...
Hmm... Evert gave another solution. Download pdcurses from Sourceforge, and check how they implemented the readkey function (I think that was... been some time since I last used ncurses).
(Edited: Actually, it is getch. I love Firefox "man" shortcut ).
I implemented a cross-platform getch for the GNE console library. It's not hard -- we're just saying you can't do it with standard C/C++ code, you have to resort to more direct APIs that are meant to work with keyboards and screens rather than streams, which is what the C/C++ I/O model is based off of.
I dont understand...
pdcurses have a portable keypressed-like function?
EDIT:
[code gillius]
I implemented a cross-platform getch for the GNE console library. It's not hard -- we're just saying you can't do it with standard C/C++ code, you have to resort to more direct APIs that are meant to work with keyboards and screens rather than streams, which is what the C/C++ I/O model is based off of.
</code>
So I can make a portable function then?
pdcurses have a portable keypressed-like function?
Curses provides that functionality on all platforms it supports. Just as Allegro does.
It wraps the platform specific functions in its own API, which is platform independent.
So I can make a portable function then?
You can't write a function that works in both DOS, Windows and UNIX. You can write a function prototype and implement it differently for all platforms you want to support.
Since there's no way to do this in a portable way, why don't you just use allegro? You're going to use it anyway, so you could use it for this task also.
Yes, Im trying to write platform-indepedent code.
Since for example, allegro isnt ported to a gameboy advance.
Of course, the input/output are different for each machine.
Thats trying to be to much cross-plaftorm compatible IMHO.
There is quite a difference between what a computer can handle nowdays and a small "game console".
yes of course, but without consider the obvious differece in the io system of each machine. I think is possible to write easy games like pacman, etc...
Im trying my game in the console, but I have this BIG problem with cin ...
if you are using allegro in your game anyway, what good will your configuration program being able to run on the gameboy serve if your game doesn't anyway?
I don't understan what you mean.
you were talking about allegro not running on the gameboy. You use allegro (i assume) in your game anyway. Why do you want that part of the program portable even tough it is all allegro anyway?
to re-use the code. (not sure again if you use this expression...)