Jump to content
  • Advertisement
Sign in to follow this  
  • entries
    122
  • comments
    121
  • views
    68929

Keyboard input

Sign in to follow this  
Twisol

348 views

With the help of some of the folks on IRC, I finally got some basic user input working. And it doesn't hog the CPU, either! I think the code speaks better than I can this time.


bool Game::Update(INPUT_RECORD* records, DWORD numEvents)
{
bool retval = false;
for (DWORD i = 0; i < numEvents; ++i)
{
if (records.EventType == KEY_EVENT)
{
if (records.Event.KeyEvent.bKeyDown)
{
switch (records.Event.KeyEvent.wVirtualKeyCode)
{
case VK_UP:
retval = true; y -= 1; break;
case VK_DOWN:
retval = true; y += 1; break;
case VK_LEFT:
retval = true; x -= 1; break;
case VK_RIGHT:
retval = true; x += 1; break;
}
}

keypress[records.Event.KeyEvent.wVirtualKeyCode] = (records.Event.KeyEvent.bKeyDown != 0);
}
}
return retval;
}

int Game::Run()
{
Draw();

INPUT_RECORD* records = new INPUT_RECORD[1];
DWORD numEvents = 1;
bool redraw = false;

while (ReadConsoleInput(hIn, records, numEvents, &numEvents))
{
redraw = Update(records, numEvents);

if (keypress[VK_ESCAPE])
break;

if (redraw)
{
Draw();
redraw = false;
}

GetNumberOfConsoleInputEvents(hIn, &numEvents);
if (numEvents)
{
delete[] records;
records = new INPUT_RECORD[numEvents];
}
}
delete[] records;

return 0;
}



It's very basic right now, but most of that is going to go into that Keyboard class I mentioned (currently not being used). While I was working this out, I was using a while (true) loop. That turned out to be a very bad idea, because when I opened up the Task Manager, Cripes was sucking up almost 20% of my CPU time. I figured this version out, luckily, which uses far less CPU time because ReadConsoleInput() blocks when there's no input in the buffer.

This code will definitely be changing, mostly as I move code into the Keyboard class and start utilizing it in Run(). For now I'm just glad I can kinda-sorta move the map around. :P

~Jonathan
Sign in to follow this  


2 Comments


Recommended Comments

Well, just changing it from while (true) to while (GetConsoleInpt(...)) fixed the CPU problem, so I'm just going by logic and evidence.

EDIT: Here's the code I was using before.

while (true)
{
if (GetAsyncKeyState(VK_ESCAPE))
break;

GetNumberOfConsoleInputEvents(hIn, &numEvents);
if (numEvents != 0)
{
records = new INPUT_RECORD[numEvents];
ReadConsoleInput(hIn, records, numEvents, &numEvents);
for (DWORD i = 0; i < numEvents; ++i)
{
if (records[i].EventType == KEY_EVENT)
{
redraw = true;
keypress[records[i].Event.KeyEvent.wVirtualKeyCode] = (records[i].Event.KeyEvent.bKeyDown != 0);
}
}
}

if (redraw)
{
Draw();
redraw = false;
}
}



Looking over it again, I see that the problem was that I was continuously looping when there were no events (if (numEvents != 0) { /**/ }). I still think while (GetConsoleInput(...)) is better, and it's a bit harder to make mistakes like this.

Share this comment


Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!