Jump to content
  • Advertisement
Sign in to follow this  
beloxx89

OpenGL Opengl fonts 2d

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

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!