SDL_test_font.c File Reference
`#include "SDL_config.h"`
`#include "SDL_test.h"`
Include dependency graph for SDL_test_font.c:

Go to the source code of this file.

## Macros

#define | SDL_TESTFONTDATAMAX (8*256)

## Functions

int | SDLTest_DrawCharacter (SDL_Renderer *renderer, int x, int y, char c)
| Draw a string in the currently set font. More...
int | SDLTest_DrawString (SDL_Renderer *renderer, int x, int y, const char *s)
| Draw a string in the currently set font. More...

## Variables

static unsigned char | SDLTest_FontData [SDL_TESTFONTDATAMAX]
static SDL_Texture * | SDLTest_CharTextureCache [256]
| Global cache for 8x8 pixel font textures created at runtime. More...

## Macro Definition Documentation

#define SDL_TESTFONTDATAMAX (8*256)

Definition at line 31 of file SDL_test_font.c.

## Function Documentation

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

Draw a string in the currently set font.

Parameters

  renderer | The renderer to draw on.
  x | The X coordinate of the upper left corner of the character.
  y | The Y coordinate of the upper left corner of the character.
  c | The 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.
*/
if (SDLTest_CharTextureCache[ci] == NULL) {
/*
* Redraw character into surface
*/
character = SDL_CreateRGBSurface(SDL_SWSURFACE,
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 */
SDLTest_CharTextureCache[ci] = SDL_CreateTextureFromSurface(renderer, character);
SDL_FreeSurface(character);
/*
* Check pointer
*/
if (SDLTest_CharTextureCache[ci] == NULL) {
return (-1);
}
}
/*
* Set color
*/
result = 0;
result |= SDL_GetRenderDrawColor(renderer, &r, &g, &b, &a);
result |= SDL_SetTextureColorMod(SDLTest_CharTextureCache[ci], r, g, b);
result |= SDL_SetTextureAlphaMod(SDLTest_CharTextureCache[ci], a);
/*
* Draw texture onto destination
*/
result |= SDL_RenderCopy(renderer, SDLTest_CharTextureCache[ci], &srect, &drect);
return (result);
}
SDLTest_FontData
static unsigned char SDLTest_FontData[SDL_TESTFONTDATAMAX]
Definition: SDL_test_font.c:33
r
GLdouble GLdouble GLdouble r
Definition: SDL_opengl.h:2072
result
GLuint64EXT * result
Definition: SDL_opengl_glext.h:9432
Uint32
uint32_t Uint32
An unsigned 32-bit integer type.
Definition: SDL_stdinc.h:155
SDL_SWSURFACE
#define SDL_SWSURFACE
Definition: SDL_surface.h:52
x
GLint GLint GLint GLint GLint x
Definition: SDL_opengl.h:1567
SDL_Surface
A collection of pixels used in software blitting.
Definition: SDL_surface.h:69
g
GLboolean GLboolean g
Definition: SDL_opengl_glext.h:1109
SDL_RenderCopy
#define SDL_RenderCopy
Definition: SDL_dynapi_overrides.h:349
SDL_CreateTextureFromSurface
#define SDL_CreateTextureFromSurface
Definition: SDL_dynapi_overrides.h:313
y
GLint GLint GLint GLint GLint GLint y
Definition: SDL_opengl.h:1567
SDL_Surface::pixels
void * pixels
Definition: SDL_surface.h:75
SDL_FreeSurface
#define SDL_FreeSurface
Definition: SDL_dynapi_overrides.h:450
Uint8
uint8_t Uint8
An unsigned 8-bit integer type.
Definition: SDL_stdinc.h:139
FONT_CHARACTER_SIZE
#define FONT_CHARACTER_SIZE
Definition: SDL_test_font.h:41
SDLTest_CharTextureCache
static SDL_Texture * SDLTest_CharTextureCache[256]
Global cache for 8x8 pixel font textures created at runtime.
Definition: SDL_test_font.c:3115
SDL_GetRenderDrawColor
#define SDL_GetRenderDrawColor
Definition: SDL_dynapi_overrides.h:337
c
const GLubyte * c
Definition: SDL_opengl_glext.h:11093
SDL_SetTextureColorMod
#define SDL_SetTextureColorMod
Definition: SDL_dynapi_overrides.h:315
SDL_Rect::x
int x
Definition: SDL_rect.h:66
SDL_Rect::w
int w
Definition: SDL_rect.h:67
mask
GLenum GLint GLuint mask
Definition: SDL_opengl_glext.h:657
NULL
#define NULL
Definition: begin_code.h:143
SDL_CreateRGBSurface
#define SDL_CreateRGBSurface
Definition: SDL_dynapi_overrides.h:448
SDL_Rect::h
int h
Definition: SDL_rect.h:67
SDL_Surface::pitch
int pitch
Definition: SDL_surface.h:74
a
GLboolean GLboolean GLboolean GLboolean a
Definition: SDL_opengl_glext.h:1109
b
GLboolean GLboolean GLboolean b
Definition: SDL_opengl_glext.h:1109
SDL_Rect::y
int y
Definition: SDL_rect.h:66
SDL_Rect
A rectangle, with the origin at the upper left.
Definition: SDL_rect.h:64
SDL_SetTextureAlphaMod
#define SDL_SetTextureAlphaMod
Definition: SDL_dynapi_overrides.h:317

int SDLTest_DrawString | ( | SDL_Renderer * | renderer,
|  | int | x,
|  | int | y,
|  | const char * | s
| ) |  |

Draw a string in the currently set font.

Parameters

  renderer | The renderer to draw on.
  x | The X coordinate of the upper left corner of the string.
  y | The Y coordinate of the upper left corner of the string.
  s | The 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().

{
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);
}
result
GLuint64EXT * result
Definition: SDL_opengl_glext.h:9432
s
GLdouble s
Definition: SDL_opengl.h:2056
Uint32
uint32_t Uint32
An unsigned 32-bit integer type.
Definition: SDL_stdinc.h:155
x
GLint GLint GLint GLint GLint x
Definition: SDL_opengl.h:1567
y
GLint GLint GLint GLint GLint GLint y
Definition: SDL_opengl.h:1567
FONT_CHARACTER_SIZE
#define FONT_CHARACTER_SIZE
Definition: SDL_test_font.h:41
SDLTest_DrawCharacter
int SDLTest_DrawCharacter(SDL_Renderer *renderer, int x, int y, char c)
Draw a string in the currently set font.
Definition: SDL_test_font.c:3117

## Variable Documentation

SDL_Texture* SDLTest_CharTextureCache[256]

static

Global cache for 8x8 pixel font textures created at runtime.

Definition at line 3115 of file SDL_test_font.c.

unsigned char SDLTest_FontData[SDL_TESTFONTDATAMAX]

static

Definition at line 33 of file SDL_test_font.c.

Referenced by SDLTest_DrawCharacter().