This markdown file serves as the documentation for SDL_fox.
Use Ctrl+F to search for whatever you are looking for.
Index:
- Initialization and Library state
FOX_Init(void)
FOX_Exit(void)
FOX_WasInit(void)
- Loading Fonts
FOX_OpenFontFc()
FOX_OpenFont()
Fox_CloseFont()
- Text Rendering
FOX_RenderChar()
FOX_RenderText()
FOX_RenderTextInside()
FOX_RenderAtlas()
- Font Metrics
FOX_QueryGlyphMetrics()
FOX_GetKerningOffset()
FOX_GetAdvance()
FOX_EnableKerning()
FOX_QueryFontMetrics()
enum FOX_LibraryState {
FOX_UNINITIALIZED,
FOX_INITIALIZED
};
SDL_fox library initialization state:
FOX_UNINITIALIZED
: SDL_fox is not initialized.FOX_INITIALIZED
: SDL_fox is initialized.
enum FOX_LibraryState FOX_Init(void);
Initializes the SDL_fox library. If the library has been initialized
prior, then nothing is done. Thus it is safe to call it multiple times
even if SDL_fox has already been initialized.
This function has to be called before using any function not listed
in the Initialization and Library state section.
enum FOX_LibraryState
indicating the SDL_fox library state.
void FOX_Exit(void);
De-initializes the SDL_fox library. If the library has not been
initialized prior, then nothing is done. Thus it is safe to call it
multiple times even if SDL_fox has not been initialized.
This function does not return anything - you can assume that it always
succeeds.
enum FOX_LibraryState FOX_WasInit(void);
Returns the SDL_fox library state.
typedef struct FOX_Font FOX_Font;
The FOX_Font
type represents the internal font representation. The
implementation itself is hidden and only accessible within the SDL_fox.c
source file.
FOX_Font* FOX_OpenFontFc(SDL_Renderer *renderer, const unsigned char *fontstr);
(This function is only available if SDL_fox has been built with
fontconfig support.)
Loads a font via a fontconfig pattern. The supplied renderer is used
to render the font into an internal texture and subsequently on each
draw call. A font is always tied to one renderer.
renderer
: SDL Rendererfontstr
: fontconfig font pattern
FOX_Font*
: Pointer to a font handleNULL
: on error
A fontconfig font pattern might look like one of the examples below.
- fontstr:
"DejaVu Sans :style=Bold :size=20"
- fontstr:
"Arial"
- fontstr:
"Times New Roman :size=10"
In actual code:
FOX_Font *font = FOX_OpenFontFc(renderer, "Arial :size=14");
FOX_Font* FOX_OpenFont(SDL_Renderer *renderer, const char *path, int size);
Loads a font explicitly via a path to its file and its point size. This
function is also called by FOX_OpenFont internally after parsing
the fontconfig pattern.
If you want bold, italic or strikethrough fonts you need to load a seperate font for each of the desired style. That is because a font like
"Arial" is not stored inside one file, but split over multiple files - one
for each style. Using fontconfig makes loading fonts easy by no longer
requiring the exact path for each style.
renderer
: SDL rendererpath
: file path to the font filesize
: font point size (Wikipedia)
//const char *bold = "/usr/share/fonts/truetype/dejavu/DejaVuSansMono-Bold.ttf";
const char *path = "/usr/share/fonts/truetype/dejavu/DejaVuSansMono.ttf";
FOX_Font *font = FOX_OpenFont(renderer, path, 14);
void FOX_CloseFont(FOX_Font *font);
"Closes" a font handle. This essentially frees all memory associated with the font object. It is good practice to close a font handle once it is no longer required (e.g. when exiting the program).
font
: Fox font handle; can beNULL
FOX_Font*
: Pointer to a font handleNULL
: on error
int FOX_RenderChar(FOX_Font *font, Uint32 ch, Uint32 previous_ch, const SDL_Point *position);
Prints a character at the given position. To modify its color use
SDL_SetRenderDrawColor(SDL_Renderer*, Uint8 r, Uint8 g, Uint8 b, Uint8 a);
font
: SDL_fox font handlech
: current character (to be printed)previous_ch
: previously printed character or 0 (only relevant for kerning)position
: The x and y coordinate to start printing from. The character is rendered down and to the right starting from position (just like when rendering a texture to the screen).
- The total x-axis advance in pixels for the given character combination.
SDL_SetRenderDrawColor(renderer, 255, 100, 100, 255);
SDL_Point position = {100, 100};
position.x += FOX_RenderChar(font, 'H', 0, &position);
SDL_SetRenderDrawColor(renderer, 100, 255, 100, 255);
FOX_RenderChar(font, 'i', 'H', &position);
SDL_RenderPresent(renderer);
void FOX_RenderText(FOX_Font *font, const Uint8 *text, const SDL_Point *position);
Prints a string of text at the given position. To modify its color use
SDL_SetRenderDrawColor(SDL_Renderer*, Uint8 r, Uint8 g, Uint8 b, Uint8 a);
font
: SDL_fox font handletext
: UTF-8 string of text.position
: The x and y coordinate to start printing from. The text is rendered down and to the right starting from position (just like when rendering a texture to the screen).
SDL_SetRenderDrawColor(renderer, 255, 100, 100, 255);
SDL_Point position = {50, 100};
FOX_RenderText(font, "Hello World!\nSDL2 is cool!", &position);
SDL_RenderPresent(renderer);
int FOX_RenderTextInside(FOX_Font *font, const Uint8 *text, const Uint8 **endptr, const SDL_Rect *rect, int n);
Prints a string of text inside of the given rect. If the text does not
fit inside all at once it is printed partially. This is conveyed by
the return value.
To modify its color use
SDL_SetRenderDrawColor(SDL_Renderer*, Uint8 r, Uint8 g, Uint8 b, Uint8 a);
font
: SDL_fox font handletext
: UTF-8 string of text.endptr
: Pointer that is set by SDL_fox to the end of the (partially) printed string and thus marking start of the next string to print.rect
: The x and y coordinate to start printing from along with a maximum width and height. The text is rendered down and to the right starting from rect.(x,y) and up to rect.(x,y) + rect.(w,h).n
: The number of characters to print. Useful for implementing some kind of oldschool rpg game text scrolling. An example is given below.
-1
: The suppliedrect
is not large enough or the font is too big.0
: Everything got printed. Nothing left to print.1
: There is more text to be printed. Theendptr
has been modified to indicate the beginning of the new string. It is up to you to settext
equal to*endptr
and start printing the next "page".2
: There is more text to be printed, but we have not reached the limits of the rect yet. Printing has been stopped because the maximum numbern
of characters has been reached. Increasen
to continue "scrolling". Swappingtext
to*endptr
at this point will resume attext[n]
, not necessarily at the next page.
The example is given in a seperate file: scrolling.c.
It demonstrates oldschool rpg game text scrolling inside a fixed size
rectangular area. The scrolling is achieved by incrementing argument n
every 100ms. Once we have reached the end of the current page, we flash
a red rectangle on screen. If we have reached the last page, we flash
a yellow rectangle and start over. Switching to the next page is
triggered by pressing any button.
void FOX_RenderAtlas(FOX_Font *font, SDL_Point *pos);
Primarily useful for troubleshooting and debugging purposes. Displays the whole font at once, meaning every single character contained within that font.
font
: SDL_fox font handlepos
: Position in pixels; Atlas is rendered towards the right and down
SDL_Point position = {10, 10};
FOX_RenderAtlas(font, &position);
SDL_RenderPresent(renderer);
typedef struct {
SDL_Rect rect;
SDL_Point bearing;
int advance;
} FOX_GlyphMetrics;
FOX_GlyphMetrics represent the dimensions of a character inside the font. Using FOX_QueryGlyphMetrics() one can get a handle to these dimensions. Based on and closely related to freetype FT_Glyph_Metrics.
SDL_Rect rect
: The position and dimensions of the character inside the internal font atlas. Only width (rect.w) and height (rect.h) should be relevant for external users.SDL_Point bearing
: Have a look at the freetype documentation on the topic.int advance
: The value by which the cursor on the x-axis gets incremented after drawing the character.
const FOX_GlyphMetrics* FOX_QueryGlyphMetrics(FOX_Font *font, Uint32 ch);
Returns a const handle to FOX_GlyphMetrics for the given character.
font
: SDL_fox font handlech
: character
const FOX_GlyphMetrics*
on successNULL
on error
int FOX_GetKerningOffset(FOX_Font *font, Uint32 ch, Uint32 previous_ch);
Calculates the kerning offset between two characters ch
and its
predecessor previous_ch
. Find out more about kerning by taking a
look at the Wikipedia article.
font
: SDL_fox font handlech
: current characterprevious_ch
: previous character or 0
- The x-axis kerning offset between
ch
andprevious_ch
. May be negative, positive or 0.
int FOX_GetAdvance(FOX_Font *font, Uint32 ch, Uint32 previous_ch);
Calculates the total x-axis advance value for a given character. This
includes the FOX_GlyphMetrics->advance
value and if a previous character
is supplied and kerning supported and enabled, also the kerning offset.
font
: SDL_fox font handlech
: Current characterprevious_ch
: Previously printed character or 0
- The x-axis advance value in pixels for supplied character
ch
void FOX_EnableKerning(FOX_Font *font, SDL_bool enable);
This function lets you enable kerning for a font.
Wikipedia article
Kerning is enabled by default, assuming the font supports it. If a font
does not support kerning then any call made to this function has no effect.
font
: SDL_fox font handleenable
: boolean;SDL_TRUE
to enable,SDL_FALSE
to disable
typedef struct {
int ptsize;
int height;
int max_width;
int max_height;
int max_advance;
} FOX_FontMetrics;
FOX_FontMetrics represent the dimensions of the font. Using FOX_QueryFontMetrics() one can get a handle to these dimensions.
int ptsize
: Font point sizeint height
: Font (max) heightint max_width
: Font max glyph widthint max_height
: Font max glyph heightint max_advance
: Font max glyph advance
const FOX_FontMetrics* FOX_QueryFontMetrics(FOX_Font *font);
Returns a const handle to FOX_FontMetrics.
font
: SDL_fox font handle
const FOX_FontMetrics*
on successNULL
on error