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. More...
 
int SDLTest_DrawString (SDL_Renderer *renderer, int x, int y, const char *s)
 Draw a string in the currently set font. More...
 
void SDLTest_CleanupTextDrawing (void)
 Cleanup textures used by font drawing functions. More...
 

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

◆ FONT_CHARACTER_SIZE

#define FONT_CHARACTER_SIZE   8

Definition at line 41 of file SDL_test_font.h.

Referenced by SDLTest_DrawCharacter(), and SDLTest_DrawString().

Function Documentation

◆ SDLTest_CleanupTextDrawing()

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.

3240 {
3241  unsigned int i;
3242  for (i = 0; i < SDL_arraysize(SDLTest_CharTextureCache); ++i) {
3243  if (SDLTest_CharTextureCache[i]) {
3246  }
3247  }
3248 }
static SDL_Texture * SDLTest_CharTextureCache[256]
Global cache for 8x8 pixel font textures created at runtime.
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
Definition: SDL_x11sym.h:50
#define NULL
Definition: begin_code.h:164
#define SDL_DestroyTexture
#define SDL_arraysize(array)
Definition: SDL_stdinc.h:93

◆ SDLTest_DrawCharacter()

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().

3118 {
3119  const Uint32 charWidth = FONT_CHARACTER_SIZE;
3120  const Uint32 charHeight = FONT_CHARACTER_SIZE;
3121  const Uint32 charSize = FONT_CHARACTER_SIZE;
3122  SDL_Rect srect;
3123  SDL_Rect drect;
3124  int result;
3125  Uint32 ix, iy;
3126  const unsigned char *charpos;
3127  Uint8 *curpos;
3128  Uint8 patt, mask;
3129  Uint8 *linepos;
3130  Uint32 pitch;
3131  SDL_Surface *character;
3132  Uint32 ci;
3133  Uint8 r, g, b, a;
3134 
3135  /*
3136  * Setup source rectangle
3137  */
3138  srect.x = 0;
3139  srect.y = 0;
3140  srect.w = charWidth;
3141  srect.h = charHeight;
3142 
3143  /*
3144  * Setup destination rectangle
3145  */
3146  drect.x = x;
3147  drect.y = y;
3148  drect.w = charWidth;
3149  drect.h = charHeight;
3150 
3151  /* Character index in cache */
3152  ci = (unsigned char)c;
3153 
3154  /*
3155  * Create new charWidth x charHeight bitmap surface if not already present.
3156  */
3157  if (SDLTest_CharTextureCache[ci] == NULL) {
3158  /*
3159  * Redraw character into surface
3160  */
3161  character = SDL_CreateRGBSurface(SDL_SWSURFACE,
3162  charWidth, charHeight, 32,
3163  0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF);
3164  if (character == NULL) {
3165  return (-1);
3166  }
3167 
3168  charpos = SDLTest_FontData + ci * charSize;
3169  linepos = (Uint8 *)character->pixels;
3170  pitch = character->pitch;
3171 
3172  /*
3173  * Drawing loop
3174  */
3175  patt = 0;
3176  for (iy = 0; iy < charWidth; iy++) {
3177  mask = 0x00;
3178  curpos = linepos;
3179  for (ix = 0; ix < charWidth; ix++) {
3180  if (!(mask >>= 1)) {
3181  patt = *charpos++;
3182  mask = 0x80;
3183  }
3184  if (patt & mask) {
3185  *(Uint32 *)curpos = 0xffffffff;
3186  } else {
3187  *(Uint32 *)curpos = 0;
3188  }
3189  curpos += 4;
3190  }
3191  linepos += pitch;
3192  }
3193 
3194  /* Convert temp surface into texture */
3195  SDLTest_CharTextureCache[ci] = SDL_CreateTextureFromSurface(renderer, character);
3196  SDL_FreeSurface(character);
3197 
3198  /*
3199  * Check pointer
3200  */
3201  if (SDLTest_CharTextureCache[ci] == NULL) {
3202  return (-1);
3203  }
3204  }
3205 
3206  /*
3207  * Set color
3208  */
3209  result = 0;
3210  result |= SDL_GetRenderDrawColor(renderer, &r, &g, &b, &a);
3211  result |= SDL_SetTextureColorMod(SDLTest_CharTextureCache[ci], r, g, b);
3213 
3214  /*
3215  * Draw texture onto destination
3216  */
3217  result |= SDL_RenderCopy(renderer, SDLTest_CharTextureCache[ci], &srect, &drect);
3218 
3219  return (result);
3220 }
GLdouble GLdouble GLdouble r
Definition: SDL_opengl.h:2079
static unsigned char SDLTest_FontData[SDL_TESTFONTDATAMAX]
Definition: SDL_test_font.c:33
GLuint64EXT * result
GLint GLint GLint GLint GLint x
Definition: SDL_opengl.h:1574
#define SDL_SWSURFACE
Definition: SDL_surface.h:52
A collection of pixels used in software blitting.
Definition: SDL_surface.h:69
uint32_t Uint32
Definition: SDL_stdinc.h:181
#define SDL_RenderCopy
#define SDL_CreateTextureFromSurface
void * pixels
Definition: SDL_surface.h:75
#define SDL_FreeSurface
uint8_t Uint8
Definition: SDL_stdinc.h:157
#define FONT_CHARACTER_SIZE
Definition: SDL_test_font.h:41
static SDL_Texture * SDLTest_CharTextureCache[256]
Global cache for 8x8 pixel font textures created at runtime.
#define SDL_GetRenderDrawColor
GLenum GLint GLuint mask
const GLubyte * c
GLint GLint GLint GLint GLint GLint y
Definition: SDL_opengl.h:1574
#define SDL_SetTextureColorMod
int x
Definition: SDL_rect.h:66
int w
Definition: SDL_rect.h:67
#define NULL
Definition: begin_code.h:164
#define SDL_CreateRGBSurface
int h
Definition: SDL_rect.h:67
GLboolean GLboolean GLboolean GLboolean a
GLboolean GLboolean g
GLboolean GLboolean GLboolean b
int y
Definition: SDL_rect.h:66
A rectangle, with the origin at the upper left.
Definition: SDL_rect.h:64
#define SDL_SetTextureAlphaMod

◆ SDLTest_DrawString()

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().

3223 {
3224  const Uint32 charWidth = FONT_CHARACTER_SIZE;
3225  int result = 0;
3226  int curx = x;
3227  int cury = y;
3228  const char *curchar = s;
3229 
3230  while (*curchar && !result) {
3231  result |= SDLTest_DrawCharacter(renderer, curx, cury, *curchar);
3232  curx += charWidth;
3233  curchar++;
3234  }
3235 
3236  return (result);
3237 }
GLuint64EXT * result
GLdouble s
Definition: SDL_opengl.h:2063
GLint GLint GLint GLint GLint x
Definition: SDL_opengl.h:1574
uint32_t Uint32
Definition: SDL_stdinc.h:181
#define FONT_CHARACTER_SIZE
Definition: SDL_test_font.h:41
GLint GLint GLint GLint GLint GLint y
Definition: SDL_opengl.h:1574
int SDLTest_DrawCharacter(SDL_Renderer *renderer, int x, int y, char c)
Draw a string in the currently set font.