# SDL_TTF text entry?

This topic is 2878 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm in kind of a goofy scenario right now. Two things must be done before I can get back into working on this (Ultima-style) RPG I'm planning, and one of them is driving me loopy:
How might I go about allowing the player to enter text and /blit the text entered/ like a normal SDL_TTF surface? Must I really work this entire process out from scratch?

##### Share on other sites
How is the word displayed right now? Is there a loop thats running constantly updating the screen and drawing the currecnt contents of user_input, or is it waiting for a keypress and then updating the screen? What type is user_input?

##### Share on other sites
It isn't the display of the input which gives me the trouble, I really should have clarified this. It is more about how the input is received. I've been using something along these lines:
while(SDL_WaitEvent(&event)!=0){ switch(event.type){ case SDL_KEYDOWN: if(event.key.keysym.sym == SDLK_a){ //does stuff }
...I'm pretty sure I'll have to write a single function which can take input from every necessary key, unless there's a better way (I figured SDL might have a function which sorta simulates the input capability of an unix terminal, but I'm starting to guess that was wrong). As for displaying, I've not been doing it very intelligently. There /is/ a way to use or define an entire char array at once, right? If so, I should try that instead. I'm sure there is, but I can't remember... x_x;

##### Share on other sites
The SDLK_ codes correspond to the ASCII codes. Check SDL/SDL_keysym.h in your include directory against ASCII Codes. You can just do
char letter=event.key.keysym.sym;

to get the letter as a char

##### Share on other sites

There /is/ a way to use or define an entire char array at once, right?

Im not sure what you mean. What are you trying to do exactly?

##### Share on other sites
Well, I was trying (unsuccessfully, obviously) to fill a generic input char array which could be used as various things like for character-naming or things said to an NPC. Basically it would have gone like this, if possible (doubt it is, now):

char input_thing[8]; //gets input //assigns input to first available array 'slot' //repeats until user hits 'enter' //copies input_thing into character's name or uses it to get a response from an NPC

Basically, I was being stupid and making something more complicated than it needed to be for the sake of over-sorting, I think. It shouldn't be a problem after the information you've given me, though! Thanks!

##### Share on other sites
From the SDL documentation:

And at last, never ever ever write your own custom ASCII conversion table. If you do that, you'll get angry non US users that will pester you to fix the keyboard handling for them, one for each keyboard layout existing in the world.
[/quote]

SDL provides for unicode translation on the keysym structure. This is the preferred way to build text input in SDL games.

##### Share on other sites
Alrighty, then, I see that SDL's innate input was more flexible than I originally thought! I'll take some time to play about with it and see if I can get something working.

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 15
• 21
• 23
• 11
• 25