Reputation: 139
I'm making Tetris game in C. This game have to work on Linux. I need to get current cursor position to return them. I don't want to use curses and ncurses.
point GetCurrentCursorPos(void)
{
point curPoint;
CONSOLE_SCREEN_BUFFER_INFO curInfo;
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &curInfo);
curPoint.x=curInfo.dwCursorPosition.X;
curPoint.y=curInfo.dwCursorPosition.Y;
return curPoint;
}
Here's the code I want to change.
and point struct looks like this.
typedef struct __point
{
int x;
int y;
} point;
I really appreciate your help!
Upvotes: 1
Views: 2741
Reputation:
If you really don't want to use (n)curses, you're stuck interfacing with the terminal directly. You can use the VT100 DSR sequence to request a cursor position report -- write the characters
"\e[6n"
to the terminal, and it will reply with a sequence similar to:
"\e[12;34R"
indicating that the cursor is at row 12, column 34 (for example). The sequence is sent inline with user input, so you may need to take special precautions to avoid consuming user input while trying to get the cursor location…
Needless to say, this is a clumsy interface to work with, and you are really better off avoiding needing it in the first place. Keep track of the cursor's location in code and you won't need to ask the terminal where it is.
Upvotes: 1