Drawing Text in SDL
I'm pretty new and I know there must be an easier way to draw text with SDL. This is how I'm doing it.
g_graphics->drawText(FONT_TYPE, "Press 'i' for instructions.", FONT_SIZE,
340,345,255,255,255,0,0,0);
g_graphics is a class that deals with all the SDL graphics including drawing text. drawText is a function. The part that makes it complicated is the 340, 345, which are the x and y values for the screen. (The other numbers are the font color and background color.)
Is there an easier way than trying to put the x and y for each line you type? I mean is there an easier way to center the text, write paragraphs, etc.?
You mean something like this? (made by me)
It loads a 640x100 bitmap and displays it at the bottom of the screen after blitting text to it.
[Edited by - orcfan32 on May 15, 2006 5:28:51 PM]
int DisplayMsg(std::string Line1, std::string Line2, std::string Line3, std::string Line4){ SDL_Surface *FinalMessage = load_image("GFX\\mbox.bmp"); SDL_Event events; bool MSG = true; RenderTextToSurface(Line1, 5,5, FinalMessage); RenderTextToSurface(Line2, 5,25+3, FinalMessage); RenderTextToSurface(Line3, 5,45+6, FinalMessage); RenderTextToSurface(Line4, 5,65+9, FinalMessage); while (MSG) { SDL_PollEvent(&events); if (events.key.keysym.sym == SDLK_RETURN) // If enter: { MSG = false; } put_surface(0, 380, FinalMessage, screen); if(SDL_Flip(screen) == -1) { SDL_FreeSurface(FinalMessage); return 1; } } SDL_FreeSurface(FinalMessage); return 0; // Normal termination}
It loads a 640x100 bitmap and displays it at the bottom of the screen after blitting text to it.
[Edited by - orcfan32 on May 15, 2006 5:28:51 PM]
The library ought to have some sort of function that can determine the size of text. You can parse the text by lines, measure the width/height of each, and use that accordingly.
[Sorry, somewhat a generic answer]
[Sorry, somewhat a generic answer]
if your using SDL_ttf then TTF_FontLineSkip( font ) gives you the recomended line skip for a font, so you can just add it to the previouse lines Y value
Hi, if you want to center the text, write paragraphs, etc, then you will have a bit more work to do, I find it quite tedious. The point where I am is that a very large string is automatically multilined in a square, and that's enough for me (message boxes).
This worked well, I have a TextRenderer that I give a string and a rectangle, you could use SDL_Rect. Then let the text renderer figure out how to place the string into the rectangle. The benefit of working with rectangles is that it is easier to lay-out, and I usually think / code in terms of rectangles anyway so I have these ready to give to the renderer.
I also have the TextRenderer remember what font size and colors to use, as this won't be changing all over the place. This could be expanded if needed into setting some 'behaviors', so the renderer could have states (centered, left outlined, right outlined, bold, etc.)
Probably the most important thing was to keep breaking the steps involved up into small working bits, using multiple small helper functions. If you use SDL_ttf, besides the lineskip function there is also a function to check the length (in pixels iirc) of a to-be-rendered string without actually rendering it. There is also the issue of kerning, it will be easier to just use monospace fonts.
Here is an old thread about using SDL_ttf with opengl, koshmaar has a post there with his font manager including code and UML diagram. It may be useful to get some more ideas.
Good luck.
This worked well, I have a TextRenderer that I give a string and a rectangle, you could use SDL_Rect. Then let the text renderer figure out how to place the string into the rectangle. The benefit of working with rectangles is that it is easier to lay-out, and I usually think / code in terms of rectangles anyway so I have these ready to give to the renderer.
I also have the TextRenderer remember what font size and colors to use, as this won't be changing all over the place. This could be expanded if needed into setting some 'behaviors', so the renderer could have states (centered, left outlined, right outlined, bold, etc.)
Probably the most important thing was to keep breaking the steps involved up into small working bits, using multiple small helper functions. If you use SDL_ttf, besides the lineskip function there is also a function to check the length (in pixels iirc) of a to-be-rendered string without actually rendering it. There is also the issue of kerning, it will be easier to just use monospace fonts.
Here is an old thread about using SDL_ttf with opengl, koshmaar has a post there with his font manager including code and UML diagram. It may be useful to get some more ideas.
Good luck.
@deadimp Yeah I was looking for functions like that in SDL but I couldn't find anything like that in the book "Focus on SDL"
@ender_341 Thanks I need that. Does that deal with the font size as well or is that just the font type? For example what do I put in the parenthisis, Arial or 12?
@DeadXorAlive I'm using only SDL. I haven't started on OpenGL and I would prefer to wait til I turn out a few more polished 2D games with SDL before I go into OpenGL. I like your idea of the TextRenderer and putting it into a SDL_Rect.
I'm trying to work with the Menu and Instruction screen and I wanted to work on having the title centered and the instructions or menu items to be in a paragraph format with margins.
@ender_341 Thanks I need that. Does that deal with the font size as well or is that just the font type? For example what do I put in the parenthisis, Arial or 12?
@DeadXorAlive I'm using only SDL. I haven't started on OpenGL and I would prefer to wait til I turn out a few more polished 2D games with SDL before I go into OpenGL. I like your idea of the TextRenderer and putting it into a SDL_Rect.
I'm trying to work with the Menu and Instruction screen and I wanted to work on having the title centered and the instructions or menu items to be in a paragraph format with margins.
Quote:Original post by eektor
@deadimp Yeah I was looking for functions like that in SDL but I couldn't find anything like that in the book "Focus on SDL"
@ender_341 Thanks I need that. Does that deal with the font size as well or is that just the font type? For example what do I put in the parenthisis, Arial or 12?
@DeadXorAlive I'm using only SDL. I haven't started on OpenGL and I would prefer to wait til I turn out a few more polished 2D games with SDL before I go into OpenGL. I like your idea of the TextRenderer and putting it into a SDL_Rect.
I'm trying to work with the Menu and Instruction screen and I wanted to work on having the title centered and the instructions or menu items to be in a paragraph format with margins.
In the linky I gave koshmaar has a diagram of how he designed his font manager class, it might still be useful to get a bigger picture of what is involved but I wouldn't recommend doing it so detailed, in the first place at least.
These are the functions from SDL_ttf that may be of use to you:
TTF_SizeText
TTF_FontLineSkip
TTF_GlyphMetrics
And this is from freetype (SDL_ttf is based upon that lib), has some useful info on what glyph metrics is about:
Glyph metrics
From the same site this explains kerning:
Kerning
This is just so you can know some background on the terms involved, and why you may want to decide between monospace and proportional fonts.
When you load a font in SDL_ttf, you determine which family it is from (arial for example) and what the size is (12). This determines what is the size of the text rendered, and all functions work with a TTF_Font from then on.
(About margins: working with rects this is easy, just subtract the total margin of the width and add margin to the x coordinate. You could make a function that takes a SDL_rect and margin, and returns the proper SDL_rect).
If you want to put your text onto a target surface and centered, then you could use my RenderTextToSurface function and make the X the screen width / 2.
It makes a color for the text which is white, then makes a surface to hold the message. Then in declares the font by opening a ttf in the same directory and makes it's size to 24. Then it checks to make sure that it rendered the text to the surface. After that, it puts the surface onto the destination and frees the surface.
int RenderTextToSurface(std::string Text, int x, int y, SDL_Surface *Dest){ SDL_Color TXT_Color; TXT_Color.r = 0xFF; TXT_Color.g = 0xFF; TXT_Color.b = 0xFF; SDL_Surface *TTF_Message; TTF_Font *font = TTF_OpenFont("font.ttf", 24); if(!(TTF_Message = TTF_RenderText_Solid(font, Text.c_str(), TXT_Color))) { SDL_FreeSurface(TTF_Message); Log("Error in function 'RenderTextToSurface': TTF_Message could not be blitted: returned 1"); return 1; } put_surface(x, y, TTF_Message, Dest); SDL_FreeSurface(TTF_Message); return 0;}
It makes a color for the text which is white, then makes a surface to hold the message. Then in declares the font by opening a ttf in the same directory and makes it's size to 24. Then it checks to make sure that it rendered the text to the surface. After that, it puts the surface onto the destination and frees the surface.
Thanks again. The post by koshmaar is pretty detailed and over my head. I'm just trying to clean up my Pong game which is my first 2D game.
Thanks for the link to the SDL_ttf site, those functions will come in very handy.
I like what you did in your project orcfan32.
I'm thinking on redoing what I have. This is the function I have:
Now I'm thinking I should split the text rendering (I guess that's the correct term instead of drawing text) into different functions. I would want a text renderer which is similar to what I have. Then create a function called ParagraphText and then CenterText. These functions I'll put the text into a SDL_Rect and render the text into a paragraph or center.
Does this sound good? Thanks for all the help you given me so far.
Thanks for the link to the SDL_ttf site, those functions will come in very handy.
I like what you did in your project orcfan32.
I'm thinking on redoing what I have. This is the function I have:
void SDL_Graphics::drawText(const std::string font_type, const std::string text, int size, int x, int y, int fR, int fG, int fB, int bR, int bG, int bB){ TTF_Font* font = TTF_OpenFont(font_type.c_str(), size); SDL_Color foregroundColor = { fR, fG, fB }; SDL_Color backgroundColor = { bR, bG, bB }; SDL_Surface* textSurface = TTF_RenderText_Shaded(font, text.c_str(), foregroundColor, backgroundColor); SDL_Rect textLocation = { x, y, 0, 0 }; SDL_BlitSurface(textSurface, NULL, m_screen, &textLocation); SDL_FreeSurface(textSurface); TTF_CloseFont(font);}
Now I'm thinking I should split the text rendering (I guess that's the correct term instead of drawing text) into different functions. I would want a text renderer which is similar to what I have. Then create a function called ParagraphText and then CenterText. These functions I'll put the text into a SDL_Rect and render the text into a paragraph or center.
Does this sound good? Thanks for all the help you given me so far.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement