SDL  2.0
SDL_test_font.h File Reference
#include "begin_code.h"
#include "close_code.h"
+ Include dependency graph for SDL_test_font.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define FONT_CHARACTER_SIZE   8

Functions

int SDLTest_DrawCharacter (SDL_Renderer *renderer, int x, int y, char c)
 Draw a string in the currently set font.
int SDLTest_DrawString (SDL_Renderer *renderer, int x, int y, const char *s)
 Draw a string in the currently set font.
void SDLTest_CleanupTextDrawing (void)
 Cleanup textures used by font drawing functions.

Detailed Description

Include file for SDL test framework.

This code is a part of the SDL2_test library, not the main SDL library.

Definition in file SDL_test_font.h.

Macro Definition Documentation

#define FONT_CHARACTER_SIZE   8

Definition at line 41 of file SDL_test_font.h.

Referenced by SDLTest_DrawCharacter(), and SDLTest_DrawString().

Function Documentation

void SDLTest_CleanupTextDrawing ( void  )

Cleanup textures used by font drawing functions.

Definition at line 3239 of file SDL_test_font.c.

References i, NULL, SDL_arraysize, and SDL_DestroyTexture.

int SDLTest_DrawCharacter ( SDL_Renderer renderer,
int  x,
int  y,
char  c 
)

Draw a string in the currently set font.

Parameters
rendererThe renderer to draw on.
xThe X coordinate of the upper left corner of the character.
yThe Y coordinate of the upper left corner of the character.
cThe character to draw.
Returns
Returns 0 on success, -1 on failure.

Definition at line 3117 of file SDL_test_font.c.

References FONT_CHARACTER_SIZE, SDL_Rect::h, NULL, SDL_Surface::pitch, SDL_Surface::pixels, SDL_CreateRGBSurface, SDL_CreateTextureFromSurface, SDL_FreeSurface, SDL_GetRenderDrawColor, SDL_RenderCopy, SDL_SetTextureAlphaMod, SDL_SetTextureColorMod, SDL_SWSURFACE, SDLTest_FontData, SDL_Rect::w, SDL_Rect::x, and SDL_Rect::y.

Referenced by SDLTest_DrawString().

{
const Uint32 charWidth = FONT_CHARACTER_SIZE;
const Uint32 charHeight = FONT_CHARACTER_SIZE;
const Uint32 charSize = FONT_CHARACTER_SIZE;
SDL_Rect srect;
SDL_Rect drect;
int result;
Uint32 ix, iy;
const unsigned char *charpos;
Uint8 *curpos;
Uint8 patt, mask;
Uint8 *linepos;
Uint32 pitch;
SDL_Surface *character;
Uint32 ci;
Uint8 r, g, b, a;
/*
* Setup source rectangle
*/
srect.x = 0;
srect.y = 0;
srect.w = charWidth;
srect.h = charHeight;
/*
* Setup destination rectangle
*/
drect.x = x;
drect.y = y;
drect.w = charWidth;
drect.h = charHeight;
/* Character index in cache */
ci = (unsigned char)c;
/*
* Create new charWidth x charHeight bitmap surface if not already present.
*/
/*
* Redraw character into surface
*/
charWidth, charHeight, 32,
0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF);
if (character == NULL) {
return (-1);
}
charpos = SDLTest_FontData + ci * charSize;
linepos = (Uint8 *)character->pixels;
pitch = character->pitch;
/*
* Drawing loop
*/
patt = 0;
for (iy = 0; iy < charWidth; iy++) {
mask = 0x00;
curpos = linepos;
for (ix = 0; ix < charWidth; ix++) {
if (!(mask >>= 1)) {
patt = *charpos++;
mask = 0x80;
}
if (patt & mask) {
*(Uint32 *)curpos = 0xffffffff;
} else {
*(Uint32 *)curpos = 0;
}
curpos += 4;
}
linepos += pitch;
}
/* Convert temp surface into texture */
SDL_FreeSurface(character);
/*
* Check pointer
*/
return (-1);
}
}
/*
* Set color
*/
result = 0;
result |= SDL_GetRenderDrawColor(renderer, &r, &g, &b, &a);
/*
* Draw texture onto destination
*/
result |= SDL_RenderCopy(renderer, SDLTest_CharTextureCache[ci], &srect, &drect);
return (result);
}
int SDLTest_DrawString ( SDL_Renderer renderer,
int  x,
int  y,
const char *  s 
)

Draw a string in the currently set font.

Parameters
rendererThe renderer to draw on.
xThe X coordinate of the upper left corner of the string.
yThe Y coordinate of the upper left corner of the string.
sThe string to draw.
Returns
Returns 0 on success, -1 on failure.

Definition at line 3222 of file SDL_test_font.c.

References FONT_CHARACTER_SIZE, and SDLTest_DrawCharacter().

Referenced by main().

{
const Uint32 charWidth = FONT_CHARACTER_SIZE;
int result = 0;
int curx = x;
int cury = y;
const char *curchar = s;
while (*curchar && !result) {
result |= SDLTest_DrawCharacter(renderer, curx, cury, *curchar);
curx += charWidth;
curchar++;
}
return (result);
}