Inputting text in OpenGL environment

This is a discussion on Inputting text in OpenGL environment within the Game Programming forums, part of the General Programming Boards category; How do I input text to a string in an OpenGL program? I know I need to catch the key ...

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    162

    Question Inputting text in OpenGL environment

    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!

  2. #2
    Satan valar_king's Avatar
    Join Date
    Aug 2001
    Posts
    288
    one way, not the best way, is if it's for windows, include <windows.h> and then have a popup textbox get the input.
    -Save the whales. Collect the whole set.

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    162
    Nah, that wouldn't look very... nice, but thanks for the suggestion.

  4. #4
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,267
    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?
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  5. #5
    Registered User
    Join Date
    Nov 2001
    Posts
    162
    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!

  6. #6
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,267
    ::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!

    Code:
    // 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!
    Last edited by no-one; 05-01-2002 at 11:51 PM.
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  7. #7
    Registered User
    Join Date
    Nov 2001
    Posts
    162
    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!

  8. #8
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,267
    >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
    Code:
    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++;
            }
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Positioning of Text under Graphical Environment
    By devarishi in forum C Programming
    Replies: 2
    Last Post: 03-17-2009, 02:15 PM
  2. OpenGL - text output
    By gavra in forum C Programming
    Replies: 3
    Last Post: 08-06-2008, 01:22 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Replies: 1
    Last Post: 07-13-2002, 05:45 PM
  5. OpenGL and text
    By Malek in forum Windows Programming
    Replies: 3
    Last Post: 09-06-2001, 08:01 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21