Sign in to follow this  

*Typing* Text In OpenGL

This topic is 4353 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

Yes, thumbs up. This should be an easy one. I'm writing something that requires a string of text to be shown on the screen real time as it's being typed--it's going to be a filename that'll be opened and the information used. I'm already really familiar with NeHe #13, but that's more for putting things up that are "hardwired". Does anybody have an example I could look at that does something like this? Thanks, Michael

Share this post


Link to post
Share on other sites
That involves actually reading in keystrokes, which is not within OpenGL's purview. Depending on how your game is developed, you might handle Win32 messages, use SDL to query the keyboard's state, etc.

Share this post


Link to post
Share on other sites
I didn't think it'd involve that--figured it would just be something like cin.get or whatever and it didn't work. Where can I find out more info about your method? Let me know if you need more details on what I'm trying to do.

Thanks,

Michael

Share this post


Link to post
Share on other sites
Oh, I do understand what you're trying to do. It's just that there are a number of ways you could be doing things.

Reading from standard input is generally a line-oriented process, though there are ways to make it character-oriented, and is not really suitable to games. Additionally, text input routines are generally blocking, which means you'd have to involve multithreading...

On DOS/Windows, there are the kbhit() and getch() functions which, while crude, could get you started. The documentation for "Character Mode Applications" in Windows can be found here. The console-reading functions will get you a bit more control. I don't know (and doubt) if they will perform correctly in absence of a console window.

Which leaves you intercepting Win32 messages like WM_KEYDOWN and WM_CHAR, keeping track by yourself of which keys are up and down. If your game is in a state where it is expecting text input, you would gather those keypresses (handling things like repetition delay), translate them into text (it's up to you to add code to handle things like 'backspace') and render as needed.

I'm sure other people can inform you better than I, but this should give you somewhere to start looking. See, this is why people use game-related libraries, where all this basic work already has been done for you. [smile] For those which are open-source, you may even learn how to do it by looking at the code (e.g. SDL).

Share this post


Link to post
Share on other sites
Ok, I think I have the idea of what you're saying. Here's what I will try:

In all the NeHe tutorials there's a bunch of stuff for handling windows and messages. One of them is a case statement which deals with wm_keydown. wParam is some integer that represents what key I just pressed.

So if I can get that integer and use it to map against an array of characters then it should result in giving me the character to use in a string which will turn into that filename.

Seems kind of roundabout but would this work do you think?

Share this post


Link to post
Share on other sites
This is exactly what I did.

Excuse the horrible coding:

//Keyboard Handling Function
void ScanKey()
{
for(short int loop=0x01; loop < 0xFE; loop++)
{
if(keys[loop])
{
if(!CL_System.input_state || CL_ERROR_LIGHT)
{
if(keys[VK_RETURN])
{
//Read User input and act based on input
ParseEntry();
}
//For all other characters
else if((!CL_System.char_flow) && ((loop >= 0x30 && loop <= 0x5A) || (loop >= 0x60 && loop <= 0x70 || loop == 0x20)))
{
//Convert loop contents to character then place in string buffer
unsigned int ascii_value=MapVirtualKeyEx(loop, 2, CL_System.keyboard);
char buffer[2];
sprintf(buffer, "%c", (char)ascii_value);
//Add the character to the user string
strcpy(CL_UserString.hold, CL_UserString.user); //Copy to temp location
delete [] CL_UserString.user; //Delete source character array
CL_UserString.user=new char [strlen(CL_UserString.hold)+2]; //Allocate new size "+1 entry" and plus 1 for null terminator
strcpy(CL_UserString.user, CL_UserString.hold); //Copy temp location's contents to source character array
delete [] CL_UserString.hold; //Delete temp character array
CL_UserString.hold=new char [strlen(CL_UserString.user)+2]; //Allocate new size for use next time
if(GetAsyncKeyState(VK_SHIFT)) //Determine if shift key is pressed
{
buffer[0]=toupper(buffer[0]);
}
else
{
buffer[0]=tolower(buffer[0]);
}
strcat(CL_UserString.user, buffer); //Attach new character to user string contents
CL_UserString.x_change=TRUE; //Toggle to invalidate string length
}
if((keys[VK_BACK] || keys[VK_DELETE]) && (strlen(CL_UserString.user) != 0))
{
//Delete the last character to the user string
strcpy(CL_UserString.hold, CL_UserString.user);
delete [] CL_UserString.user;
CL_UserString.user=new char [strlen(CL_UserString.hold)-1];
for(int loop=0; loop < strlen(CL_UserString.hold)-1; loop++)
{
CL_UserString.user[loop]=CL_UserString.hold[loop];
}
CL_UserString.user[strlen(CL_UserString.hold)-1] = 0; //Null terminator
strcpy(CL_UserString.hold, "");
CL_UserString.x_change=TRUE;
}
if(CL_System.x_cursor >= 7.0) //Check to see if cursor will surpass the gamut limit HORIZONTALLY
{
CL_System.char_flow=TRUE;
}
else
{
CL_System.char_flow=FALSE;
}
}
else if(CL_System.input_state == CL_BUSY)
{

}
else if(CL_System.input_state == CL_ERROR_RANCOR)
{

}
else if(CL_System.input_state == CL_ERROR_HUH)
{

}
keys[loop]=FALSE;
}
}
}



This was done about a year ago so I think whenever I left it, the shift was still not working.

Share this post


Link to post
Share on other sites

This topic is 4353 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.

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

Sign in to follow this