How to get user writing input string in openGl?

Started by
15 comments, last by Mathimetric 10 years, 3 months ago

This example:

q676.png

Was made with SFML, but ANY graphics library can do the same thing.

I rendered the background (grass and water), then I rendered a stretched image over it, with transparency enabled:

39dz.png

Then I captured keyboard events to get keyboard input from the user and so the user can enter text into a std::string.

Then I used SFML to render some text from the std::string using a font, and drew that text over the panel.

This can be done with any decent graphics library, whether SDL, or SFML, or OpenGL, or whatever - the only difficulty is drawing the text, which OpenGL doesn't have native support for - you can use SFML or SDL to render text for you, and then upload it as a texture for OpenGL to use.

Multiple individual steps working together - how they will work together in your game will be different than how they work together in mine, so there's no code for you to copy and paste, you have to figure out how to write it yourself. Just break it into small steps like I just described, and accomplish the goals one by one.

Or, use a OpenGL-based GUI library.

Advertisement
Yeah, I did this before with openGl. I had to create a Sentence Class, A Word Class, and a Letter Class. I Create a sentence at a certain position on the screen, and add text through its constructor, then creates words out of the text by splitting the spaces. It then creates a letter object for each letter in each word, and it renders each individual letter.

View my game dev blog here!

@

Servant of the Lord

I have used sdl_ttf in opengl . but i have this error :

  1. error LNK1104: cannot open file 'SDL_ttf.lib'

i already have tried to remove the "SDL_ttf.lib" , but it didn't useful.
i have trid used : lazyfoo.net for setting up the opengl with sdl
pls tell me what must i do ? .

Are you on Windows or Linux or Mac OSX?

If on Windows, are you using Visual Studio or MinGW to compile your code?

quik simple example

inside ~ MainWindowProc ~

(I have used this to print 3D text from user keyboard input)

[attachment=19238:example.JPG]

 
//                                                                  //
    case WM_CHAR:
    switch(wParam)
    {
    case VK_BACK:
    if(print.BuffPos)
    print.text[--print.BuffPos] = 0;
    if(print.sBuffPos)
    print.gBuff[--print.sBuffPos] = 0;
    break;
    case VK_RETURN:
    print.text[++print.BuffPos] = NULL;
    print.gBuff[++print.sBuffPos] = NULL;
    print.Return0(print.text,"char", 256);
    print.BuffPos = 0;
    print.Return0(print.gBuff, "char", 256);
    print.sBuffPos = 0;
    ++print.rtrn;
    break;
 case VK_NEXT:
 //MoonT.translation[1] -= 2;
 break;
 case VK_PRIOR:
 //MoonT.translation[1] += 2;
 break;
 break;
 break;
 
 case VK_LEFT:
        break;
 
 case VK_RIGHT:
      break;
 case VK_DOWN:     
 break;
 
 case VK_UP:      
 break;
    default:
    print.text[print.BuffPos] = (TCHAR) wParam;
    print.gBuff[print.sBuffPos] = (TCHAR) wParam;
    ++print.BuffPos;    ++print.sBuffPos;
    break;
    }
    break;
//                                                                    //
////////////////////////////////////////////////////////////////////////
 
Notice when a key is entered (printable character)
the switch is defaulted to update a string buffer.
--when enter key is depressed the handle Ends buffer with a NULL terminate char
|- then updates the data "rtrn" member to allow the program to know what line or how many times
the return key has been pressed. - this info is passed to the graphics functions for render()
 
-when the backspace key is pressed the string buffer's current Position is  zero'ed , and decrimented (pos) to re-enter the key that needs to be edited.
 
 
*in this case the buffer is used simulair to an I/O stream buffer in witch the buffer can be used to pass data to parameter's givin to
a function or class member function; such as GetString(int Rtn_order, char * format, ...)
 
Original standard streaming I/O buffers uses access of the Keyboard buffer and passes its data to a TEMP file buffer
{
(I.E.)
 
extern  FILE        _RTLENTRY _EXPDATA _streams[];
extern  unsigned    _RTLENTRY _EXPDATA _nfile;
 
stdin   (&_streams[0])
stdout  (&_streams[1])
stderr  (&_streams[2])
}
and C string Buffer's using BIOS (basic input/output instruction Sets')

its math do it

@

Mathimetric

pls can you give me the file for this example .

@ Moudy I can try to find a source for this example (a HEllo World example)

but it will be un-related to the Hex editor -converter dipicted in the screen shot.

if thats ok? ; I will attach it to this messge when I have it together.

here is an alternate example of GetString using OpenGL and windows library

-So in function the *strng is updated (represinting a Line) via the stream buffer gBuff [ ]

-when the return key check (rtrn) is (>) greater then (Order) ; the function returns the string length of *strng -

and stops updating the *strng

the reason it looks this way is because it is meant to be inserted into a windows Loop or Application window Loop.

functions that are inside a window are a little different then the normal console apps' sometimes.

-class interface design-


class GLText
public:
    int BuffPos, sBuffPos,
    First, Count1, Count2;
    unsigned int base;
    unsigned int m_fontListBase;
    GLYPHMETRICSFLOAT gmf[256];   // holds orientation and placement
                                  // info for display lists

     GLText();
    ~GLText();

    bool Init(char *fontname, int fontsize, float depth);
    unsigned int CreateOutlineFont(char *fontname, int fontsize, float depth);
    void PositionFont(float xPos, float yPos, float zPos);
    void PrintFont(int order, const char *__format, ...);
    void CenterFont();
    void ReleaseFont(unsigned int base);
    int  ScanFont(int order, const char *__format, ...);
    void NewLine(float Mult = 1, float X = 0, float Y = 0, float Z = 0, char *Direction = "down");
    bool Shutdown();
    void Return0(void *var, char *type, int elms); //same as memset (...) for string
    int  GetString(int Order, char *strng);
    int rtrn;
    char *pBuff;
    char *sBuff;
    char *gBuff;
    char text[256];

int GLText::GetString(int Order, char *strng)
{
if(Order<=rtrn)
{
if (Order == rtrn )
{
 glPushAttrib(GL_LIST_BIT);
        glListBase(base);
        glCallLists(strlen(gBuff), GL_UNSIGNED_BYTE, gBuff);
 glPopAttrib();
        strng[BuffPos] = gBuff[BuffPos];
        strcpy(strng, gBuff);
return 0;
}
else
if(Order <= rtrn)
{
        glPushAttrib(GL_LIST_BIT);
        glListBase(base);
        glCallLists(strlen(strng), GL_UNSIGNED_BYTE, strng);
 glPopAttrib();
return strlen(strng);
}
return 0;
}
else
return strlen(strng);
}

I attached the class function def source code in src.cpp.txt

you will need to use your knowledge of C++ to piece them together for compilation/debugging/porting or use in another instance.

You can do 1 of 2 things with it

1. you could just include (src.cpp) it to the appended end of the class body Def?

2. precompile it and link it with the H and the Main.cpp with the OBJ file

(this also includes letting microsofts visual studio LInk the resources with (h/cpp))

Summary:

this class will allow you, if you are using openGL (v1.0 or greater) 3d perspectives or ortho (ithink), a pixel pallete window application (WIN32); To draw 3d text on to the screen. Givin also the interface' to use input/output UI via keyboard or simulair (drivin) hardware.

--another requirement is that the window(x/s) APP must have a procedure for handling the window and its messeges--

your skill level needs to be intermediate to advanced to understand how to compile or port this into your own apps.

creating a window App is not an easy task for the beginner. You need to understand your own compiler and interfaces,

and some compiler's dont always have the nessesary DLL's included in its default libraies; so sometimes you need to know how to

find them and import them.

its math do it

This topic is closed to new replies.

Advertisement