SDL 2.0
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...
 
void SDLTest_CleanupTextDrawing (void)
 Cleanup textures used by font drawing functions. More...
 

Variables

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

Macro Definition Documentation

◆ SDL_TESTFONTDATAMAX

#define SDL_TESTFONTDATAMAX   (8*256)

Definition at line 31 of file SDL_test_font.c.

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.

3240{
3241 unsigned int i;
3242 for (i = 0; i < SDL_arraysize(SDLTest_CharTextureCache); ++i) {
3246 }
3247 }
3248}
#define SDL_DestroyTexture
#define SDL_arraysize(array)
Definition: SDL_stdinc.h:115
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:167

References i, NULL, SDL_arraysize, SDL_DestroyTexture, and SDLTest_CharTextureCache.

◆ 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.

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 */
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 */
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;
3213
3214 /*
3215 * Draw texture onto destination
3216 */
3217 result |= SDL_RenderCopy(renderer, SDLTest_CharTextureCache[ci], &srect, &drect);
3218
3219 return (result);
3220}
#define SDL_SetTextureAlphaMod
#define SDL_CreateTextureFromSurface
#define SDL_GetRenderDrawColor
#define SDL_RenderCopy
#define SDL_CreateRGBSurface
#define SDL_FreeSurface
#define SDL_SetTextureColorMod
GLint GLint GLint GLint GLint GLint y
Definition: SDL_opengl.h:1574
GLdouble GLdouble GLdouble r
Definition: SDL_opengl.h:2079
GLint GLint GLint GLint GLint x
Definition: SDL_opengl.h:1574
GLboolean GLboolean GLboolean b
GLuint64EXT * result
GLboolean GLboolean GLboolean GLboolean a
const GLubyte * c
GLboolean GLboolean g
GLenum GLint GLuint mask
uint32_t Uint32
Definition: SDL_stdinc.h:203
uint8_t Uint8
Definition: SDL_stdinc.h:179
#define SDL_SWSURFACE
Definition: SDL_surface.h:52
static unsigned char SDLTest_FontData[SDL_TESTFONTDATAMAX]
Definition: SDL_test_font.c:33
#define FONT_CHARACTER_SIZE
Definition: SDL_test_font.h:41
A rectangle, with the origin at the upper left (integer).
Definition: SDL_rect.h:78
int h
Definition: SDL_rect.h:80
int w
Definition: SDL_rect.h:80
int y
Definition: SDL_rect.h:79
int x
Definition: SDL_rect.h:79
A collection of pixels used in software blitting.
Definition: SDL_surface.h:71
void * pixels
Definition: SDL_surface.h:76
static SDL_Renderer * renderer

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

Referenced by SDLTest_DrawString().

◆ 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.

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}
GLdouble s
Definition: SDL_opengl.h:2063
int SDLTest_DrawCharacter(SDL_Renderer *renderer, int x, int y, char c)
Draw a string in the currently set font.

References FONT_CHARACTER_SIZE, renderer, and SDLTest_DrawCharacter().

Referenced by main().

Variable Documentation

◆ SDLTest_CharTextureCache

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.

Referenced by SDLTest_CleanupTextDrawing(), and SDLTest_DrawCharacter().

◆ SDLTest_FontData

unsigned char SDLTest_FontData[SDL_TESTFONTDATAMAX]
static

Definition at line 33 of file SDL_test_font.c.

Referenced by SDLTest_DrawCharacter().