Inputting text in OpenGL environment [Archive] - C Board

PDA

View Full Version : Inputting text in OpenGL environment


Crossbow
04-30-2002, 08:11 PM
How do I input text to a string in an OpenGL program? I know I need to catch the key presses and I do that, but how do I input that to a string. If I tell the user to type a his/her name in, then I make the program loop through a function that repeats until the user presses the enter key. Then it stores this text to a string. I hope you understand what I am talking about. Thank you!

valar_king
04-30-2002, 08:13 PM
one way, not the best way, is if it's for windows, include <windows.h> and then have a popup textbox get the input.

Crossbow
04-30-2002, 09:02 PM
Nah, that wouldn't look very... nice, but thanks for the suggestion.

no-one
04-30-2002, 10:55 PM
ok lemmie get this straight~ you want to input text from the user and then display it. yes?

first do you know how to display text in OpenGL?

Crossbow
05-01-2002, 08:16 PM
Yes, I know how to display text. And yes that is what I want to do. If you could give me some idea of how to do this, I would greatly appreciate it!

no-one
05-01-2002, 11:46 PM
::edit:: this is a bit overcomplicated in my opinion! say the word if you want a simple/better explaination, tomorrow!

i'll give you the concept, and if i can get time i'll try and write a working code example

there are a few qustions!
followed by what the ~pseudo example will use

1. how many characters per line? 80
2. how many line to display at once? 4
3. how many refernce lines of text to store? 100
4. what key starts text input? Return/enter key
5. how to store them? a inked list will be the easiest method!

ok heres some ~psuedo code! keep in mind there are possibly better more efficient ways, this is just the basic idea!

this is a simple example that ignores system keys(capslock,shift,ect)
i'll use windows messages as the example since its easiest to use and easily converted! but it has one down fall it uses Virtual keycodes so its kind of a pain to get punctuations and certain other keys to work properly!



// use const or define whichever you prefer
#define MAX_VIEWABLE_LINES 4
#define MAX_LINE_LENGTH 80
#define MAX_STORED_LINES 100

typedef struct
{
char* text;
next;
prev;
}Linked List Node;

ect. ect... end up with a linked list;

int Current_Line_Length = 0
int Current_Line_Count = -1;
bool newline = false; // new line signal
bool typing = false; // flag if user is currently typing or not

linked list input_text_head; // used as head stores no text;
linked list* currentlinkpos = input_text->next; // current link

init_func()
{
currentlinkpos = new node[1]; // allocate storage
}

display func()
{
// draw a simple cursor if we're typing
if(typing)
{
get the current text pos! // depends on what kind of text how to do this
DrawText(current x, current y,"_");
}
// draw all the lines of text
for(int i = 0, linked list c = input_text->nexy; i < MAX_VIEWABLE_LINES;i++, c = c->next)
{
DrawText(x,y,c->text);
}
}

//in wndproc function

case WM_KEYDOWN:

// check if over the max lines count
if(Current_Line_Count >= MAX_STORED_LINES)
{
// remove the oldest link and make room for a new one
linked list t;
t = input_text->next->next;
delete input_text->next;
input_text->next = t;
Current_Line_Count--;
}
if(typing ) // i were typing
{
// if were on a new line or have reached max line length(newline)
// if so alocate a new link
if(newline || Current_Line_Length >= MAX_LINE_LENGTH-1)
{
newline = false;
Current_Line_Count++;
currentlinkpos->text = new unsigned char[MAX_LINE_LENGTH];
currentlinkpos->next = new node[1];
// nul the line to prevent printing errors
memset(currentlinkpos->text ,0,MAX_LINE_LENGTH);
Current_Line_Length = 0;
currentlinkpos = currentlinkpos->next; // updat current link pos
}
// check for a graphical character or space and add it to the string
if(isgraph(wParam) || wParam == VK_SPACE)
{
currentlinkpos->text[Current_Line_Length ] = wParam;
Current_Line_Length++;
}
}

// check for return to start of a or end of a line of text!
if(wParam == VK_RETURN)
{
// if they're not typeing then it has to be a new line of text!
if(!typing)
newline = true;
typing = !typing;
}
if(typing)
disable other input response
return 0;


btw: if this is cryptic or you cant understand parts of it, or you just want a plain text explaination!!! LET ME KNOW!!!!! because i don't know what you know and if there are things i misexplained or your not ready for or you need a different explaination, there are other different methods!

by the way im sorry for any errors! its 1 am so im not really sure of much, i just hope this can help!

Crossbow
05-02-2002, 07:15 PM
This really helps me alot! I understand it enough, I think it is simple enough, because I am planning to make it into a console, and I will need multiple lines and stuff. One thing though: will this account for backspaces, or will I have to add some code to it? Also, do you know what the ASCII number for that block you see in the quake consoles, indicating where your typing cursor is? Thanks a lot!

no-one
05-02-2002, 09:31 PM
>will this account for backspaces, or will I have to add some code to it?

doah i knew i forgot something!!!

try this

in WndProc


if(typing ) // i were typing
{
// if were on a new line or have reached max line length(newline)
// if so alocate a new link
if(newline || Current_Line_Length >= MAX_LINE_LENGTH-1)
{
newline = false;
Current_Line_Count++;
currentlinkpos->text = new unsigned char[MAX_LINE_LENGTH];
currentlinkpos->next = new node[1];
// nul the line to prevent printing errors
memset(currentlinkpos->text ,0,MAX_LINE_LENGTH);
Current_Line_Length = 0;
currentlinkpos = currentlinkpos->next; // updat current link pos
}

// added BackSpace Code
// if backspace
if(wParam == VK_BACK) // possible VK_BACK_SPACE??
{
currentlinkpos->text[Current_Line_Length ] = 0;
Current_Line_Length--;
return 0;
}
// end added BackSpace Code

// check for a graphical character or space and add it to the string
if(isgraph(wParam) || wParam == VK_SPACE)
{
currentlinkpos->text[Current_Line_Length ] = wParam;
Current_Line_Length++;
}