i am making a part of a game that requires a function to control the cursor, in theory what i wrote must work perfectly but i cant understand where is the problem.
because it doesn't change the values of CoordX and CoordY past 1 and always prints Error.
when i pass by value it wants me to declare 2 extra variables is there any way to do it without reference ? (not using GetAsynKey).
edit: i entered 2 test variables and now the position changes perfectly but there is still a problem it prints error as if the default in the switch case is triggered every time you press any button.
Ditch the variables CoordX and CoordY.
Use the Windows API to query the current cursor position.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
template <typename T>
T Clamp( T low, T value, T high )
{
return std::max( low, std::min( value, high ) );
}
void Move_Cursor_Relative( int offset_X, int offset_Y )
{
HANDLE hStdOut = GetStdHandle( STD_OUTPUT_HANDLE );
CONSOLE_SCREEN_BUFFER_INFO csbi;
if (GetConsoleScreenBufferInfo( hStdOut, &csbi ))
{
csbi.dwCursorPosition.X = Clamp <SHORT> ( 0, csbi.dwCursorPosition.X+offset_X, csbi.dwSize.X-1 );
csbi.dwCursorPosition.Y = Clamp <SHORT> ( 0, csbi.dwCursorPosition.Y+offset_Y, csbi.dwSize.Y-1 );
SetConsoleCursorPosition( hStdOut, csbi.dwCursorPosition );
}
}
(I typed this in off the top of my head. Hopefully I got it right.)
Now, you can move the cursor relative to its current position easily enough:
1 2 3 4
case 72: if (current_keys.Up_Arrow) Move_Cursor_Relative( 0, -Skipper ); break;
case 80: if (current_keys.Down_Arrow) Move_Cursor_Relative( 0, Skipper ); break;
case 75: if (current_keys.Left_Arrow) Move_Cursor_Relative( -Skipper, 0 ); break;
case 77: if (current_keys.Right_Arrow) Move_Cursor_Relative( Skipper, 0 ); break;
Once again you come and help me Duoas :)
sadly i didn't learn templates yet can you send me a good source to learn templates and what is "Clamp" and what does this do:
csbi.dwCursorPosition.X = Clamp <SHORT> ( 0, csbi.dwCursorPosition.X+offset_X, csbi.dwSize.X-1 );
csbi.dwCursorPosition.Y = Clamp <SHORT> ( 0, csbi.dwCursorPosition.Y+offset_Y, csbi.dwSize.Y-1 );
and an explanation on how to use your template,Move_Cursor_Relative and how do i access the switch case and where ? i didn't quite understand how your code works and hope its not too much to ask.
int Clamp( int minimum, int value, int maximum )
{
return std::max( minimum, std::min( value, maximum ) );
}
It returns value such that minimum ≤ value ≤ maximum.
The code I gave you does exactly what your code does:
- I removed the two variables previously indicated (you don't need them).
- I renamed "set" to the more descriptive "current_keys".
Everything else will require you to try it out and see it work.