• Advertisement
Sign in to follow this  
  • entries
    122
  • comments
    121
  • views
    68556

O_O

Sign in to follow this  

123 views

I've, like, neglected this thing. Not to mention GDNet+ expired on me. I've moved on from my Arena Battle project.. in retrospect, it was getting kind of convoluted. I'm now working on something totally new for me... Project 1 in the C++ Workshop!

No, seriously, it's new! You have to color the output, so I Googled around and found this. Encapsulating it in a class (cConsole), and setting a few functions for easy manipulation, I've created an easy interface with the console screen. I can color output with output(), and check specific keypresses with getKey().

Oh, getKey().. I just love it. I'll code-tag it here, and explain it, although all you C++ gurus out there should be able to understand it just fine. I just like explaining. :D


char cConsole::getKey()
{
while (true)
{
GetNumberOfConsoleInputEvents(rHnd, &numEvents);
if (numEvents == 0) continue;

INPUT_RECORD *eventBuffer = new INPUT_RECORD[numEvents];
ReadConsoleInput(rHnd, eventBuffer, numEvents, &numEventsRead);

int i = 0;
for (; i < numEventsRead;)
{
if (eventBuffer.EventType == KEY_EVENT)
{
int j = 0;
while (j <= keys.length())
{
if (eventBuffer.Event.KeyEvent.wVirtualKeyCode == keys[j])
{
delete eventBuffer;
break;
}
j++;
}
if (j == keys.length() + 1)
{
i++;
continue;
}
else return keys[j];
}
else
{
i++;
continue;
}
}
}
}






To start with, I have a public string variable in cConsole (don't yell!) that the user can set to, say, "123". This function cycles through any events that occur until a key event occurs. Then it cycles through the 'keys' string I mentioned, checking the key event against each character in 'keys', until it finds a match or cycles the whole string. If the iteration variable 'j' is 1 more than the string length, then it didn't find a match, and it continues. Otherwise, it returns the char that was matched, so the calling function can deal with it.

I love it. :D
Sign in to follow this  


2 Comments


Recommended Comments

Very nicely done! Nice clean code, simple to use, and very helpful.

Btw, I'm stealing it when I do my text-based RPG!

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