Jump to content
  • Advertisement
Sign in to follow this  
arbuckle911

in-game command line

This topic is 4503 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

what's the best way to take keyboard input and append it to a string for a command line in the 3d window? It needs to take into account all special characters and caps. I'd just rather not have to code all that in myself...any body already to know how to do this?

Share this post


Link to post
Share on other sites
Advertisement
There are several issues involved here, and only a couple of them (text rendering, and perhaps input via GLUT) are specifically OpenGL-related. Can you be more specific about what you need help with?

In general, an in-game command line can be about as simple or as complex as you want it to be. If you're starting from scratch, I'd go for simple to start with.

The first step will be setting up a system to render text to the screen. I think GLU or GLUT offers some support for this via fonts and display lists, or you can just use an alpha-blended texture with the proper texture coordinates.

As for input, that depends, but I recommend SDL.

Finally, you'll want to maintain a string for the command line, and whenever a key event is received, append it to the string. When enter or return is received, you flush the command line, which involves clearing the string and doing any parsing or processing that you want to do. And, of course, every frame you render the contents of the string to the screen using whatever text display system you have available.

Share this post


Link to post
Share on other sites
Quote:
Original post by jyk
And, of course, every frame you render the contents of the string to the screen using whatever text display system you have available.


Actually, most text-rendering systems are fairly slow. You may want to just render the text to a texture when it changes, and render the texture to the screen every frame. Well, at least SDL_ttf is slow with blended text.

And, I don't know of any easy way to get key strokes into characters other than pure ifs/switches.

Share this post


Link to post
Share on other sites
Quote:
Original post by Ezbez
Quote:
Original post by jyk
And, of course, every frame you render the contents of the string to the screen using whatever text display system you have available.


Actually, most text-rendering systems are fairly slow. You may want to just render the text to a texture when it changes, and render the texture to the screen every frame. Well, at least SDL_ttf is slow with blended text.
Well, I assumed OpenGL since this was the forum he posted to, in which case performance will not be a problem and rendering the text from scratch each frame should work just fine.
Quote:
And, I don't know of any easy way to get key strokes into characters other than pure ifs/switches.
Again I recommend SDL for this, as it's portable and provides a layer of abstraction between you and the details of managing keyboard input. Using SDL at least, the correct characters for each key are included in the event data structure associated with each keypress.

Share this post


Link to post
Share on other sites
I assume getting the right character for each key stroke isn't the problem, but how to concat this one to a string.
Well then youll have to add each single char from input to a string checking whether it's a char or something else (probably backspace would be needed to delete the last char).

The more difficult thing is using special characters... Do you really need that stuff? Well, the only way to do that is to check whether ALT OR SHIFT are hold down during the input and then add a different character to you string...

Share this post


Link to post
Share on other sites
I'm not sure how to render text to the screen but this is how I'd approach it.

Have a bool called isConsole somewhere that represents if console typing is on.

This code assumes you have an array keys that registers if a key is down or up.

Then have a section that runs if isConsole is true:

for (int i = (int)'A'; i <= (int)'Z'; i += 1) {
if (keys) {
// somehow append the char string or something
}
}
// do this for other characters and what not

When you press enter it locks all the characters before it so when you click backspace it only erases the string back to the last locked char.

Share this post


Link to post
Share on other sites
I'll be more specific.
I have already done the text rendering. My only question was how to get a character out of keyboard input rather than just the key codes. Buecause there are modifyers like Shift and alt etc. I was wondering if there was a function I could use like char getKeyboardInput(); Rather than just checking for individual keys then writting everything to interpret them into an actual character to append.

Share this post


Link to post
Share on other sites
Quote:
Original post by arbuckle911
I'll be more specific.
I have already done the text rendering. My only question was how to get a character out of keyboard input rather than just the key codes. Buecause there are modifyers like Shift and alt etc. I was wondering if there was a function I could use like char getKeyboardInput(); Rather than just checking for individual keys then writting everything to interpret them into an actual character to append.


You will need to handle the WM_CHAR message. The character that is part of that message is the exact character you will be using. Everything is already taken care of for you when using that message. Just appened it as is to your buffer, handle spaces/backspaces, and you should be set.

/shameless plug for my Win32 input library.
Look at this example and this how to

Share this post


Link to post
Share on other sites
Quote:
Original post by arbuckle911
I'll be more specific.
I have already done the text rendering. My only question was how to get a character out of keyboard input rather than just the key codes. Buecause there are modifyers like Shift and alt etc. I was wondering if there was a function I could use like char getKeyboardInput(); Rather than just checking for individual keys then writting everything to interpret them into an actual character to append.


This is a snippet of code from my in-engine console.


// convert to character
// From MSDN:
// 2: vkey is a virtual-key code and is translated into an unshifted character value in the
// low-order word of the return value. Dead keys (diacritics) are indicated by setting the
// top bit of the return value. If there is no translation, the function returns 0.
char c = MapVirtualKey( vkey, 2 );

// if we have a printable character
if( isprint(c) ){
// GetAsyncKetState returns a short. If the most significant bit is set, the key is down,
// and if the least significant bit is set, the key was pressed after the previous call
// to GetAsyncKeyState.
// If the shift key is down right now
short short_temp = GetAsyncKeyState(VK_LSHIFT);
if( short_temp & 0x8000 ){
unsigned char kstate[256];
unsigned short tempc[3];
int temp;

GetKeyboardState(kstate); // get the keyboard state
temp = ToAscii( vkey, 0, kstate, tempc, 0 ); // get the ascii character/s

// no characters were copied to 'tempc' the virtual key code has no translation
if( temp == 0 )
return false;

// else copy to to input line
for(int i=0 ; i < temp; ++i)
addUserInput( (char) tempc );

// added input everything fine
return true;
}// if GetAsyncKeyState
else{ // no shift, just lower case
if( isalpha(c) ){
addUserInput(c+32); // add character to input line
return true;
}
else{
addUserInput(c); // add character to input line
return true;
}
}

return false;
}// if isprint(c)



Where 'vkey' is the virtual key code of the button that was pressed.
'addUserInput' simply adds the character to a string object that is a member variable of the class.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!