sdl_picofont
About
sdl_picofont is a very, very (very) simple and small font library for SDL. It doesn't feature any fancy stuff like UTF8 support (although that might change). In fact it only features one built in 8x8 1bit bitmap font. What it DOES support however is newlines and tabs, unlike some other, more extensive font libraries for SDL (yes SDL_ttf, I'm looking at you). sdl_picofont is written i C and is released under the GPLv2 license. Oh, and if you happen to use sdl_picofont in a project of some sort, please drop me a line about it.
Screenshots
Why?
There are a lot of text output libraries for SDL, a quick search on "font" under libraries on libsdl.org gives you like 10 hits. But most of them are a bit too complex for really small applications and most of them require you to have some external font files to load. This library is designed for you who just want som damned text output on the screen.
Authors
Fredrik Hultin
With additional patches from
Saul D "KrayZier" Beniquez
Requirements
sdl_picofont requires SDL to work.
Changelog
Version 1.2
- Added cmake-file to build as a library
- Added include guard for .h-file (which I apparently forgot, thanks Saul!)
- Renamed spf.h to SDL_picofont.h to match SDL-naming conventions
- Added a pkg-config file
- Added extern "C"-stuff in files, since it's now a "real library" (Saul again)
Version 1.1
- Fixed warning when compiling with g++
- Removed crap about buffer overflows in the docs
- Renamed the render functions (hope it didn't break anyone's code)
Version 1.0
Usage
Code says more than a thousand words, so take a look at the
example.
Functions
The sdl_picofont library only has two functions, FNT_Render and FNT_RenderMax. The functions work like this:
SDL_Surface* FNT_Render(const char* text, SDL_Color color);
SDL_Surface* FNT_RenderMax(const char* text, unsigned int len, SDL_Color color);
- text - A pointer to the text you want to display
- len - The number of characters in the text you want to display
- color - The color you want the output to be
FNT_Render (and FNT_RenderMax) creates a new 8 bit SDL-surface with the exact width and height of the output text. The surface has the flag SDL_SWSURFACE set. The text will have the color specified by "color" and have palette index 1. The background color will be color key transparent and have the palette index 0. The color key of the returned surface is the user specified text color but:
- color key red = (given red + 0x7f) % 0xff
- color key blue = (given blue + 0x7f) % 0xff
- color key green = (given green + 0x7f) % 0xff
Behaviour
FNT_RenderMax stops parsing the string when it hits a NULL termination or when "len" is reached, whichever comes first.
Giving FNT_RenderMax an unterminated string and a "len" value higher than the length of the string results in undefined behaviour. It will probably cause garbled output on the screen and a segmentation fault.
Giving FNT_Render an unterminated string results in undefined behaviour. It will probably cause garbeled output, segmentation faults and global warming.
Giving any of the render functions a 0 byte string will generate a 0x0 surface.
If FNT_Render or FNT_RenderMax returns a NULL pointer, then SDL_CreateRGBSurface failed and returned NULL for some reason. So probably something related to SDL rather than sdl_picofont.
Compiling and installing
sdl_picofont is so small that it feels unnecessary to compile it separately and install on the system. Just throw the two c-files and the h file into your current project.
Portability
sdl_picofont has only been tested under GNU/Linux and X11 but should work fine with other systems that SDL supports.
Downloads