Opengl fonts 2d

Started by
2 comments, last by Servant of the Lord 15 years, 11 months ago
Hello, Im trying to include text in my 2d opengl game. Im using SDL for input so I was thinking, I could listen for all keys like that switch(event.type){ case SDL_KEYDOWN: switch(event.key.keysym.sym){ case SDLK_LEFT: ... case SDLK_RIGHT: ... case SDLK_UP: ... case SDLK_DOWN: ... case SDLK_A: ... case SDLK_B: ... case SDLK_C: ... case SDLK_D: ... and then draw each character from an opengl texture. Is that efficent or is there some other way? Thanks
Advertisement
What do you mean by text in you game? Do you want it for a HUD? A drop down console? GUI? A bit to vague.
Is your problem with reading in bits? If so, there's the "sym" member variable of SDL_keysym. You can use it like so:

//"event" is a keyboard event
Uint16 character = event.key.keysym.unicode;

It has the character associated with the key. If there's shift or caps on, it's properly capitalized. For things that aren't character keys (eg: F12, return), it will give you a non-existant character (usually drawn as a square or blank space).

Edit: I should point you over to the SDL reference which makes note of a couple things, such as you must call SDL_EnableUNICODE before this will work.
I think he means adding some kind of text-input box, or perhaps a chatting system, to his game.

@beloxx89: Your way would work, but a potentially* better way would be to make use of the 'unicode' field of the keysym.

*It'd involve less copy+pasted code of all 26 cases for each letter, but might be take a bit of trouble to get it working.

It'd look more like this:
std::string myString;switch(event.type){   case SDL_KEYDOWN:{       switch(event.key.keysym.sym){           case SDL_BACKSPACE:               myString.pop_back();           break;           default:{               if(event.key.keysym.unicode != 0){ //It's a charactor for us to read.                   if ((event.key.keysym.unicode & 0xFF80) == 0) { //Taken from the keysym page, should ensure it's a ASCII charactor.                       myString.append((keysym.unicode & 0x7F));                   }               }           } break;       }    }}

You'd have to mess around with it a bit to get it working properly. (Hmm, I just googled "event.key.keysym.unicode" to find you a article or tutorial to help, and apparently Lazyfoo has one)

This topic is closed to new replies.

Advertisement