windows api for the console

I am writing a code for a game using windows api for the console. the code i have so far moves a char left, right, up or down depending on which arrow was pressed. The problem is that it jumps 2 spaces for every key press instead of just one space. anyone have any idea how to make it jump one space only? this code i modified from a sample code in a tutorial i read.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <Windows.h>
#include <string>

HANDLE hScreen, hKeyboard;
void clearScreen( HANDLE );

int main(){
	hScreen = GetStdHandle( STD_OUTPUT_HANDLE );
	hKeyboard = GetStdHandle( STD_INPUT_HANDLE );

	const WORD width = 50, height = 40;			// width and height of screen
	const COORD winSize = { width, height };		

	CHAR_INFO bgBuff[ width * height ];

    SetConsoleScreenBufferSize( hScreen, winSize );

	MoveWindow( GetConsoleWindow(), 0,0, 430,550, TRUE );

	COORD homeCoords = { width/2, height/2};	// starting home coordinates
	DWORD dwWritten;

	DWORD numEvents = 0;		// How many events happened?
	BOOL appRunning = TRUE;		// Is the application running?
	
    DWORD numEventsRead = 0;	// How many events have we read from the console?

	while ( appRunning ){
		GetNumberOfConsoleInputEvents( hKeyboard, &numEvents );		// Get all input events that have happened
		if ( numEvents > 0 ){										// check if something happened
			INPUT_RECORD evBuff;									// make an input eventbuffer to hold the events happening
			ReadConsoleInput( hKeyboard, &evBuff, numEvents, &numEventsRead );	// Read events
				if ( evBuff.EventType == KEY_EVENT ){	// was it a keyboard event?
					switch ( evBuff.Event.KeyEvent.wVirtualKeyCode ){
						case VK_ESCAPE:
							appRunning = FALSE;		// set appRunning to false and exit loop
							break;
						case VK_LEFT:
							if ( homeCoords.X > 0 )
								homeCoords.X -= 1;
							break;
						case VK_RIGHT:
							if ( homeCoords.X < (width - 1) )
								homeCoords.X += 1;
							break;
						case VK_UP:
							if ( homeCoords.Y > 0 )
								homeCoords.Y -= 1;
							break;
						case VK_DOWN:
							if ( homeCoords.Y < (height - 1) )
								homeCoords.Y += 1;
							break;
						default:
							//CHAR charBuff = ;		// get char value
							FillConsoleOutputCharacter( hScreen, evBuff.Event.KeyEvent.wVirtualKeyCode, 1, homeCoords, &dwWritten );		// print char to screen
							break;
					}// END SWITCH
					clearScreen( hScreen );
					FillConsoleOutputCharacter( hScreen, 'A', 1, homeCoords, &dwWritten );		// print char to screen
					//Sleep(5);
				}// END IF
		}// END IF
	}// END WHILE LOOP
	
	//Sleep( 1000 );
	clearScreen( hScreen );
	return 0;
}
I haven't seen this style before but the issue seems to be:
COORD homeCoords = { width/2, height/2};

When you're at for example position 24, the real position is 48 (multiplied by two). Similarly, when you're at position 25 (one point over) the real position is 50.

So by moving over 1 "homecoord" value, youre moving over 2 real values
Thanks garanon, i solved the problem. but that was a good point you had. just in case anyone is interested, all i needed to do was surround the switch statement with the if statement bellow.
1
2
3
if ( evBuff.Event.KeyEvent.bKeyDown == TRUE ){
   //switch statement...
}
Topic archived. No new replies allowed.