Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: SDL TTF Issue

  1. #1
    Join Date
    Jul 2010
    Posts
    472

    SDL TTF Issue

    Well, over the past couple of hours or so, I've been banging my head against a particular problem.

    If I attempt to pass a string through TTF_RenderText_Solid, it won't work; so naturally, I ran with what it'll accept: const char *.

    There's a slight problem, though; the goal here is to make it possible to underline a single character throughout the text given. The goal is to create buttons that look like... well... buttons, naturally; with the ability for 'hotkeying'.

    So, I have questions when dealing with this little issue. One, is there a way to pick on a single character in a const char * variable? Or, is there a way, with SDL's TTF structure, to assign Underlining to only one character?

    Finally, -if- there's a way to underline only one character in the listing ... is there a way to change only the underline? That part isn't necessary by any means, but it would be fun to know, in case the topic comes up later.

    Thanks!
    "It's most impressive when it's least expected."

  2. #2
    Join Date
    Jun 2003
    Location
    Trier, Germany
    Posts
    1,350
    Quote Originally Posted by PDI247 View Post
    One, is there a way to pick on a single character in a const char * variable?
    what do you mean by 'pick'? you can acquire a pointer to it using your favorite string find functionality...

    Or, is there a way, with SDL's TTF structure, to assign Underlining to only one character?
    probably not. the way i remember it (i don't know the library very well, so this could be wrong) you just put in a text and get a surface in return. you can either stitch several surfaces together, so that underlined characters always have a surface of their own (the easy way). or you could try to find the position of a rendered character on the surface and draw the underline yourself (probably quite hard if you want a robust solution).

    alternatively, use a more powerful text renderer. libfreetype is an excellent text renderer that is very powerful and flexible. this one will require some digging into though.

  3. #3
    Join Date
    Jul 2010
    Posts
    472
    Well, the thing is... what I'm attempting to accomplish is, basically, pulling each and every character out of the const char * structure, and adding it into the rendered string one at a time. When it manages to get to the hotkey character, there's an if check that defines just that... where the character is then underlined before rendering.

    Unfortunately, const char * structures don't seem to have array functionality, and unfortunately... although I -have- appended the const char * value to a string (Thus, it's the only thing -in- that string), but alas... can't cast a string's character to be a const char *, and I've tried a few other ways to make the same structure occur. Can't seem to get it to work.

    Is there an 'obvious' way to do this that I'm just not thinking up at the moment?

    Edit:
    OR, is there perhaps a way to pull the rendered string out a character at a time, so I can just change the text format for a particular letter?

    ... And if not, is that library a multi-platform? o.o
    Last edited by PDI247; 12-10-2010 at 04:15 PM.
    "It's most impressive when it's least expected."

  4. #4
    Join Date
    Jun 2003
    Location
    Trier, Germany
    Posts
    1,350
    Quote Originally Posted by PDI247 View Post
    Well, the thing is... what I'm attempting to accomplish is, basically, pulling each and every character out of the const char * structure, and adding it into the rendered string one at a time. When it manages to get to the hotkey character, there's an if check that defines just that... where the character is then underlined before rendering.
    i'm not seeing where your problem lies here. i would just stuff the whole thing into a std::string, which gives you iterators as well as array-style access and on top of that some useful string algorithms. maybe you could give some code example of where exactly things go bad for you?

    is that library a multi-platform? o.o
    should run on any of the major platforms (personally used it in win32 and linux environments)

  5. #5
    Join Date
    Jul 2010
    Posts
    472
    Code:
            TTF_Font *tempFont = optionsFont;
    	TTF_SetFontStyle(tempFont, TTF_STYLE_UNDERLINE);
    	for (int i = 0; i < (int)tempString.length(); i++)
    		if (i != keyChar)
    			text.option = TTF_RenderText_Solid(optionsFont, (const char *)tempString[i]), defaultColor);
    		else
    			text.option = TTF_RenderText_Solid(tempFont, (const char *)tempString[i], defaultColor);
    	optionsText.push_back(text);
    This is the snippet that ends up going bad. The problem is, TTF_RenderText functionality -only- takes const char *. This compiles, but distributes a run-time error, naturally. On that same token, I can't just pass a char through.

    i != keyChar is designed that way because a number is passed in to define the exact key (So... how far into the string is the hotkey character?).
    "It's most impressive when it's least expected."

  6. #6
    Join Date
    Jul 2010
    Posts
    472
    Found a work-around, but it revolves around... not actually underlining it. But! If anyone happens to figure out/know how to underline a specific character, I'd be interested in knowing how.

    Thanks, Comic!
    "It's most impressive when it's least expected."

  7. #7
    Join Date
    Jun 2003
    Location
    Trier, Germany
    Posts
    1,350
    what type is tempString in your code?

    most string classes provide access to an underlying c-style string for just such cases (e.g. std::string.c_str()).

  8. #8
    Join Date
    Jul 2010
    Posts
    472
    It's a string, -but-, you can't do tempString.c_str()[i]; ... because it converts it to a const char *, which doesn't have that kind of functionality.
    "It's most impressive when it's least expected."

  9. #9
    Join Date
    Jun 2003
    Location
    Trier, Germany
    Posts
    1,350
    yes you can. you simply forgot that about the implicit dereferencing of [] (x[i] for built-in types is just short for *(x + i)).

    so either of those should be fine:
    &tempString[i]
    &tempString.c_str()[i]
    tempString.c_str() + i

    and many more. if you don't need the string specific functionality of std::string, a std::vector<char> will do the job just as fine.

  10. #10
    Join Date
    Jul 2010
    Posts
    472
    Huh... wasn't aware that was possible, actually...

    Thanks!
    "It's most impressive when it's least expected."

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •