I'm making a program where the user can press arrow keys to move around and press other keys to print characters on the screen.
I'll use _getch() to take input,_putch(int) to output and gotoxy(int,int)(which I copied from some site and which uses Win32 API's SetConsoleCursorPosition(HANDLE,COORD)) to move around.
I want to restrict the user's movement to areas where he has not typed any characters. So, I need a function which tells me what is the character written at the current cursor position or any specified cursor position.
I want to restrict the user's movement to areas where he has not typed any characters
¿So can I fill the screen?
You could define zones were you allow writing (and you overwrite if you need to). Or you could maintain a mapping of the screen.
I just need a function which tells me what is the character written at the current cursor position or any specified cursor position. If such a function exists(preferably in a visual C++ header file or Win32 API) please tell me its name, otherwise tell me that such a function doesn't exist! I can make a mapping of my own, but it would be a waste of time if the above function exists. A function would make things very simple as:
1. It is obvious the computer is also storing such a map (called console screen buffer) and I don't want to waste memory by making another map of my own. Such maps can be large, eg 120 x 1500 characters.
2. It would be easier to have a function to tell me what character is written at the current position than storing a character whenever a key is pressed and retrieving a character when an arrow key is pressed. If I use cout to output a string on the occurrence of an event, I would have to insert each character on my map 'character by character'.
3. If I had to map every character, I would have to store them in a 2D array or something. Then I would have to get the console screen size every time the program starts. Also, the console screen size can be changed during run-time. I don't know how to detect such a change. Even if I did, this would force me to make a new map and copy all characters from the old map.
I believe there is such a function (for Windows only). I've seen some posts in the Windows Programming forum about it so look there. It's not popular of course since most people wouldn't use the console this way when there are graphics libraries that are far more powerful and much easier to use.
EDIT: Ups, My class is in the Lounge section...
Here is the link, it can do what you want it to, you just have to learn it... http://cplusplus.com/forum/lounge/65208/
short CursorPosition_x, CursorPosition_Y;
console->GetPosition( CursorPosition_x, CursorPosition_Y ); // Get Cursor Position and store in CursorPosition_x and CursorPosition_y
console->GetPositionChar( x, y );
But if you don't want to use that loop, you can use it like this:
1 2 3 4 5 6
static Console * console = new Console("",0);
// Do whatever you want to do...
// And when you need to get the character...
short cp_x, cp_y; //cp_x and cp_y instead of CursorPositon_x .......
console->GetPosition( cp_x, cp_y ); // Get Cursor Position and store it
console->GetPositionChar( cp_x, cp_y ); // Get Character at chosen position