Sign in to follow this  

TTF_RenderText_Shaded() help please

Recommended Posts

Hello I'm trying to create a simple function where all i have to do is say
draw_text(font, "string", x_pos, y_pos, width, height);

I want to get it at the point where I don't have to create an image every time I want text on the screen, and I load all the fonts that will be in the game once, so I can easily change the font, string, x_pos, y_pos, width and height of whatever text I want on the screen at any give time. so I have been in the process of trying to put a global function in my game for this to work; however, I have gotten lost with my own work, or really don't know where to go from here. The only source you'll need is below I believe unless I can't do this at all. Here it is.
SDL_Surface draw_text(TT_F *font, char *text, char *message, SDL_Surface *message, SDL_Color bgcolor, SDL_Color foreground, int x, int y, int w, int h)
	SDL_Surface *message;

	//Hold the offsets
	SDL_Rect offset;

	//Get the offsets
	offset.x = x;
	offset.y = y;
	offset.w = w;
	offset.h = h;

	message = TTF_RenderText_Solid(font, "some text", fg, bg);
        SDL_Rect textlocation = {x, y, w, h};

        return message;


I know this isn't right, but I'm sure from the code, and the above message you know what I'm trying to accomplish. If you can help me with this at all I much appreciate it. Thanks in advance

Share this post

Link to post
Share on other sites
OK, yet again I solved my own problem through a little more research, and ingenuity's. I saw some tutorial off of a FAQ Library forum here that took me to a "Aaron's Tutorials" page that had TTF listed as a tutorial, and within that tutorial he/she had created a function that did just what I wanted; however, it only used aerial.ttf as it's font, and I wanted the programmer to choose whatever font they had in their font library, so I changed it just a bit, and for all of those who are programming in SDL, or using SDL_ttf and you want to make random text at some point here is Aaron's code moded just a little to accomadate those needs of drawing text anywhere on the screen, with any font without having to load fonts out the *whistle whistle*

//draw text on the screen
void drawText(string filename, SDL_Surface* screen, char* string, int size, int x, int y, int fR, int fG, int fB, int bR, int bG, int bB)
TTF_Font* font = TTF_OpenFont(filename.c_str(), size);

SDL_Color foregroundColor = { fR, fG, fB };
SDL_Color backgroundColor = { bR, bG, bB };

SDL_Surface* textSurface = TTF_RenderText_Shaded(font, string,
foregroundColor, backgroundColor);

SDL_Rect textLocation = { x, y, 0, 0 };

SDL_BlitSurface(textSurface, NULL, screen, &textLocation);



Share this post

Link to post
Share on other sites
A few tips:
- You are getting a string by copy. It means you are copying the entire filename parameter into a local variable. Since you never modify that, you could simply take a const string& parameter.
- You are mixing std::string and char* (which, btw, should be const char*). Keep to the utility class goodness, the std::string.

Anyways, that method is not efficient. You are loading the font everytime you are rendering the text. If you are looking for something to help you debug by printing things on the window, that's more than fine, but don't expect nice performance coming out of that code, if you use it for anything else.

This snippet below should give you an idea of how you could make things better by thinking about that function as an atom inside your program -- it should simply draw the text and nothing else.

// Just some lame color class with an ugly conversion method
struct Color
Uint8 r,g,b;

Color(Uint8 rr, Uint8 gg, Uint8 bb)
: r(rr), g(gg), b(bb) { }

SDL_Color convert() const
SDL_Color c = { r,g,b };

return c;

// Shouldn't load things in here -- this function draws text only
void draw_text(SDL_Surface *img, TTF_Font *font, const std::string &text, const Color &fg, const Color &bg, int x, int y)
SDL_Surface *rendered = TTF_RenderText_Shaded(font,text.c_str(),fg.convert(),bg.convert());

SDL_Rect rect = { x,y,0,0 };



// Using it somewhere...
void im_using_that_function_here(SDL_Surface *img)
TTF_Font *font15 = TTF_OpenFont("font.ttf",15);

// Write "draw this" at the surface img using font15 with
// foreground color 255,120,33 and background color 12,40,107
// at position 120,160
draw_text(img,font15,"draw this",Color(255,120,33),Color(12,40,107),120,160);


Share this post

Link to post
Share on other sites
Thanks for the code. Now that I see it I did over complicate things a bit, but I did figure out a way to do it, but it isn't the most efficient way. I'll use your code, and modify it accordingly.

Thanks so much! :)

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this