Jump to content

View more

Image of the Day

Inventory ! Va falloir trouver une autre couleur pour le cadre D: #AzTroScreenshot #screenshotsaturday https://t.co/PvxhGL7cOH
IOTD | Top Screenshots

The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

Sign up now

Useful SDL_Library for download- Text Formating and rendering

4: Adsense

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 guadrians   Members   


Posted 31 August 2012 - 11:47 AM

Hello! This is my first post here, hi all!
I was in need of a basic text formater for my project. So I searched the internet. I stumbled upon this post:
Sadly, there was no solution for a pure SDL text formating. The solution offered involved the use of OpenGL:

So I made my own library for text formatting.

What it does? It takes as an argument, among other things, a SDL_rect and renders text in that rect.
The text is left aligned. It's quite a raw thing but it works ( it works only with 32bit surfaces). Also, acts as a basic text formatter, removing all spaces if they are at the begining of a line and all but one space if it is between words.
Here's the declaration:

bool TR_render_text(char * font,char * file,SDL_Color font_color,int font_size, SDL_Rect * rect,SDL_Surface * screen, SDL_Surface * destination)

font-font to be loaded. it uses SDL_ttf library
file-text file to be rendered
Color-color of the text
font_size-desired font size
rect-rendering box
screen-this is your main screen SDL_surface(the one you obtained with SDL_SetVideoMode
destination-sdl_surface where you want your text rendered(you can also put screen here). This surface should be create with SDL_CreateRGBSurface like so:
destination=SDL_CreateRGBSurface(SDL_HWSURFACE, screen->w,screen->h,32,screen->format->Rmask,screen->format->Gmask, screen->format->Bmask, screen->format->Amask);

Here's a screenshot of the TR_render_text in action.

Known bugs(so far):
-if the width of the rect is smaller than the actual size of the largest word(rendered word that is) program crashes
-not a bug per se but I noticed that SDL_ttf does not render well some characters, namely an apostroph with a ASCII code 0x92

Also to use this library you will have to install both SDL and SDL_ttf libraries.

If you don't know how to install a library check out Lazy foo tutorial here:

Let me know how it works for you!

Attached Files

Edited by guadrians, 31 August 2012 - 11:51 AM.

#2 guadrians   Members   


Posted 31 August 2012 - 11:52 AM

I can't seem to load a screenshot so here I try again.
SNAG 0001

#3 fastcall22   Moderators   

Posted 31 August 2012 - 03:39 PM

Congratulations on distributing your library!

Unfortunately, it shouldn't be used because it has some glaring issues:
1. The font parameter is a char*. Does this mean the font is loaded (and hopefully unloaded!) after every call? A game cannot afford load and unload fonts every frame. What if I wanted to render the different text with the same font? Is the font loaded twice? (And do you free the surfaces allocated by SDL_TTF after rendering?)
2. The function expects a file. Does this mean that the text file is loaded after every call? What if I just have a string I want to render? Would I need to write it to a file and then pass that filename to the function?
3. There are two SDL_Surface* parameters to render to. Which one do I use? Why should the function care whether or not it is drawing to the screen or the surface I created for it?

Possible solutions:
1. Change the font parameter from a string to a TTF font. In order to have minimal impact on performance, all resource loading should be handled separately of rendering and by a more appropriate manager -- let the user manage the resources.
2. Change the filename parameter to a plain string. Let the user worry about loading the text from disk. By doing so, you stop thrasing the harddrive by constantly writing/loading text to and fro.
3. Remove the screen parameter, just write to the destination parameter. Is there really any difference between writing to the screen and a surface created by a user?
zlib: eJzVVLsSAiEQ6/1qCwoK i7PxA/2S2zMOZljYB1TO ZG7OhUtiduH9egZQCJH9 KcJyo4Wq9t0/RXkKmjx+ cgU4FIMWHhKCU+o/Nx2R LEPgQWLtnfcErbiEl0u4 0UrMghhZewgYcptoEF42 YMj+Z1kg+bVvqxhyo17h nUf+h4b2W4bR4XO01TJ7 qFNzA7jjbxyL71Avh6Tv odnFk4hnxxAf4w6496Kd OgH7/RxC

#4 guadrians   Members   


Posted 31 August 2012 - 05:23 PM

It's a quick and dirty library which gets the job done for my project and I thought of sharing it...True, it could use with some improvements, but I ain't going to spend any more time on it now.Maybe in future. I focused on my specific needs instead of a more larger view so that's way the issues you highlighted above are present.
1.This is correct, a font is better than char*. Maybe I'll change it.
2.This issue has two faces really. I mean, yeah, to render a string could be useful. Probably will change that too.
3.I need the screen surface to pass parameters to SDL_CreateRGBSurface function. And was useful for debugging.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.