testime.c File Reference
`#include <stdlib.h>`
`#include <stdio.h>`
`#include <string.h>`
`#include "SDL.h"`
`#include "SDL_test_common.h"`
Include dependency graph for testime.c:

Go to the source code of this file.

## Data Structures

struct | UnifontGlyph

## Macros

#define | DEFAULT_PTSIZE 30
#define | DEFAULT_FONT "unifont-9.0.02.hex"
#define | MAX_TEXT_LENGTH 256
#define | UNIFONT_MAX_CODEPOINT 0x1ffff
#define | UNIFONT_NUM_GLYPHS 0x20000
#define | UNIFONT_TEXTURE_WIDTH 512
#define | UNIFONT_GLYPHS_IN_ROW (UNIFONT_TEXTURE_WIDTH / 16)
#define | UNIFONT_GLYPHS_IN_TEXTURE (UNIFONT_GLYPHS_IN_ROW * UNIFONT_GLYPHS_IN_ROW)
#define | UNIFONT_NUM_TEXTURES ((UNIFONT_NUM_GLYPHS + UNIFONT_GLYPHS_IN_TEXTURE - 1) / UNIFONT_GLYPHS_IN_TEXTURE)
#define | UNIFONT_TEXTURE_SIZE (UNIFONT_TEXTURE_WIDTH * UNIFONT_TEXTURE_WIDTH * 4)
#define | UNIFONT_TEXTURE_PITCH (UNIFONT_TEXTURE_WIDTH * 4)
#define | UNIFONT_DRAW_SCALE 2

## Functions

static Uint8 | dehex (char c)
static Uint8 | dehex2 (char c1, char c2)
static Uint8 | validate_hex (const char *cp, size_t len, Uint32 *np)
static int | unifont_init (const char *fontname)
static void | unifont_make_rgba (Uint8 *src, Uint8 *dst, Uint8 width)
static int | unifont_load_texture (Uint32 textureID)
static Sint32 | unifont_draw_glyph (Uint32 codepoint, int rendererID, SDL_Rect *dstrect)
static void | unifont_cleanup ()
size_t | utf8_length (unsigned char c)
char * | utf8_next (char *p)
char * | utf8_advance (char *p, size_t distance)
Uint32 | utf8_decode (char *p, size_t len)
void | usage ()
void | InitInput ()
void | CleanupVideo ()
void | _Redraw (int rendererID)
void | Redraw ()
int | main (int argc, char *argv[])

## Variables

static SDLTest_CommonState * | state
static SDL_Rect | textRect
static SDL_Rect | markedRect
static SDL_Color | lineColor = {0,0,0,255}
static SDL_Color | backColor = {255,255,255,255}
static SDL_Color | textColor = {0,0,0,255}
static char | text [MAX_TEXT_LENGTH]
static char | markedText [SDL_TEXTEDITINGEVENT_TEXT_SIZE]
static int | cursor = 0
struct UnifontGlyph * | unifontGlyph
static SDL_Texture ** | unifontTexture
static Uint8 | unifontTextureLoaded [UNIFONT_NUM_TEXTURES] = {0}

## Macro Definition Documentation

## DEFAULT_FONT

#define DEFAULT_FONT "unifont-9.0.02.hex"

Definition at line 38 of file testime.c.

Referenced by main().

## DEFAULT_PTSIZE

#define DEFAULT_PTSIZE 30

Definition at line 27 of file testime.c.

Referenced by main().

## MAX_TEXT_LENGTH

#define MAX_TEXT_LENGTH 256

Definition at line 40 of file testime.c.

## UNIFONT_DRAW_SCALE

#define UNIFONT_DRAW_SCALE 2

Definition at line 61 of file testime.c.

Referenced by _Redraw().

## UNIFONT_GLYPHS_IN_ROW

#define UNIFONT_GLYPHS_IN_ROW (UNIFONT_TEXTURE_WIDTH / 16)

Definition at line 56 of file testime.c.

Referenced by unifont_draw_glyph(), and unifont_load_texture().

## UNIFONT_GLYPHS_IN_TEXTURE

#define UNIFONT_GLYPHS_IN_TEXTURE (UNIFONT_GLYPHS_IN_ROW * UNIFONT_GLYPHS_IN_ROW)

Definition at line 57 of file testime.c.

Referenced by unifont_draw_glyph(), and unifont_load_texture().

## UNIFONT_MAX_CODEPOINT

#define UNIFONT_MAX_CODEPOINT 0x1ffff

Definition at line 52 of file testime.c.

Referenced by unifont_draw_glyph(), and unifont_init().

## UNIFONT_NUM_GLYPHS

#define UNIFONT_NUM_GLYPHS 0x20000

Definition at line 53 of file testime.c.

Referenced by unifont_init().

## UNIFONT_NUM_TEXTURES

#define UNIFONT_NUM_TEXTURES ((UNIFONT_NUM_GLYPHS + UNIFONT_GLYPHS_IN_TEXTURE - 1) / UNIFONT_GLYPHS_IN_TEXTURE)

Definition at line 58 of file testime.c.

Referenced by unifont_cleanup(), unifont_draw_glyph(), unifont_init(), and unifont_load_texture().

## UNIFONT_TEXTURE_PITCH

#define UNIFONT_TEXTURE_PITCH (UNIFONT_TEXTURE_WIDTH * 4)

Definition at line 60 of file testime.c.

Referenced by unifont_load_texture(), and unifont_make_rgba().

## UNIFONT_TEXTURE_SIZE

#define UNIFONT_TEXTURE_SIZE (UNIFONT_TEXTURE_WIDTH * UNIFONT_TEXTURE_WIDTH * 4)

Definition at line 59 of file testime.c.

Referenced by unifont_load_texture().

## UNIFONT_TEXTURE_WIDTH

#define UNIFONT_TEXTURE_WIDTH 512

Definition at line 55 of file testime.c.

Referenced by unifont_load_texture().

## Function Documentation

## _Redraw()

void _Redraw | ( | int | rendererID | ) |

Definition at line 466 of file testime.c.

References SDL_Color::a, SDL_Color::b, cursor, SDL_Color::g, SDL_Rect::h, SDL_Surface::h, i, markedRect, markedText, NULL, SDL_Color::r, renderer, SDLTest_CommonState::renderers, SDL_CreateTextureFromSurface, SDL_DestroyTexture, SDL_FreeSurface, SDL_RenderCopy, SDL_RenderFillRect, SDL_SetRenderDrawColor, SDL_SetTextInputRect, SDL_StartTextInput, SDL_StopTextInput, SDL_strlen, text, textRect, unifont_draw_glyph(), UNIFONT_DRAW_SCALE, utf8_advance(), utf8_decode(), utf8_length(), SDL_Rect::w, SDL_Surface::w, SDL_Rect::x, and SDL_Rect::y.

Referenced by Redraw().

{
SDL_Renderer * renderer = state->renderers[rendererID];
SDL_Rect drawnTextRect, cursorRect, underlineRect;
drawnTextRect = textRect;
drawnTextRect.w = 0;
SDL_SetRenderDrawColor(renderer, backColor.r, backColor.g, backColor.b, backColor.a);
SDL_RenderFillRect(renderer,&textRect);
if (*text)
{
#ifdef HAVE_SDL_TTF
SDL_Surface *textSur = TTF_RenderUTF8_Blended(font, text, textColor);
SDL_Texture *texture;
/* Vertically center text */
drawnTextRect.y = textRect.y + (textRect.h - textSur->h) / 2;
drawnTextRect.w = textSur->w;
drawnTextRect.h = textSur->h;
texture = SDL_CreateTextureFromSurface(renderer,textSur);
SDL_FreeSurface(textSur);
SDL_RenderCopy(renderer,texture,NULL,&drawnTextRect);
SDL_DestroyTexture(texture);
#else
char *utext = text;
Uint32 codepoint;
size_t len;
SDL_Rect dstrect;
dstrect.x = textRect.x;
dstrect.y = textRect.y + (textRect.h - 16 * UNIFONT_DRAW_SCALE) / 2;
dstrect.w = 16 * UNIFONT_DRAW_SCALE;
dstrect.h = 16 * UNIFONT_DRAW_SCALE;
drawnTextRect.y = dstrect.y;
drawnTextRect.h = dstrect.h;
while ((codepoint = utf8_decode(utext, len = utf8_length(*utext))))
{
Sint32 advance = unifont_draw_glyph(codepoint, rendererID, &dstrect) * UNIFONT_DRAW_SCALE;
dstrect.x += advance;
drawnTextRect.w += advance;
utext += len;
}
#endif
}
markedRect.x = textRect.x + drawnTextRect.w;
markedRect.w = textRect.w - drawnTextRect.w;
if (markedRect.w < 0)
{
/* Stop text input because we cannot hold any more characters */
SDL_StopTextInput();
return;
}
else
{
SDL_StartTextInput();
}
cursorRect = drawnTextRect;
cursorRect.x += cursorRect.w;
cursorRect.w = 2;
cursorRect.h = drawnTextRect.h;
drawnTextRect.x += drawnTextRect.w;
drawnTextRect.w = 0;
SDL_SetRenderDrawColor(renderer, backColor.r, backColor.g, backColor.b, backColor.a);
SDL_RenderFillRect(renderer,&markedRect);
if (markedText[0])
{
#ifdef HAVE_SDL_TTF
SDL_Surface *textSur;
SDL_Texture *texture;
if (cursor)
{
char *p = utf8_advance(markedText, cursor);
char c = 0;
if (!p)
p = &markedText[SDL_strlen(markedText)];
c = *p;
*p = 0;
TTF_SizeUTF8(font, markedText, &drawnTextRect.w, NULL);
cursorRect.x += drawnTextRect.w;
*p = c;
}
textSur = TTF_RenderUTF8_Blended(font, markedText, textColor);
/* Vertically center text */
drawnTextRect.y = textRect.y + (textRect.h - textSur->h) / 2;
drawnTextRect.w = textSur->w;
drawnTextRect.h = textSur->h;
texture = SDL_CreateTextureFromSurface(renderer,textSur);
SDL_FreeSurface(textSur);
SDL_RenderCopy(renderer,texture,NULL,&drawnTextRect);
SDL_DestroyTexture(texture);
#else
int i = 0;
char *utext = markedText;
Uint32 codepoint;
size_t len;
SDL_Rect dstrect;
dstrect.x = drawnTextRect.x;
dstrect.y = textRect.y + (textRect.h - 16 * UNIFONT_DRAW_SCALE) / 2;
dstrect.w = 16 * UNIFONT_DRAW_SCALE;
dstrect.h = 16 * UNIFONT_DRAW_SCALE;
drawnTextRect.y = dstrect.y;
drawnTextRect.h = dstrect.h;
while ((codepoint = utf8_decode(utext, len = utf8_length(*utext))))
{
Sint32 advance = unifont_draw_glyph(codepoint, rendererID, &dstrect) * UNIFONT_DRAW_SCALE;
dstrect.x += advance;
drawnTextRect.w += advance;
if (i < cursor)
cursorRect.x += advance;
i++;
utext += len;
}
#endif
if (cursor > 0)
{
cursorRect.y = drawnTextRect.y;
cursorRect.h = drawnTextRect.h;
}
underlineRect = markedRect;
underlineRect.y = drawnTextRect.y + drawnTextRect.h - 2;
underlineRect.h = 2;
underlineRect.w = drawnTextRect.w;
SDL_SetRenderDrawColor(renderer, lineColor.r, lineColor.g, lineColor.b, lineColor.a);
SDL_RenderFillRect(renderer, &underlineRect);
}
SDL_SetRenderDrawColor(renderer, lineColor.r, lineColor.g, lineColor.b, lineColor.a);
SDL_RenderFillRect(renderer,&cursorRect);
SDL_SetTextInputRect(&markedRect);
}
backColor
static SDL_Color backColor
Definition: testime.c:45
SDL_Color::g
Uint8 g
Definition: SDL_pixels.h:298
SDL_Surface::h
int h
Definition: SDL_surface.h:73
UNIFONT_DRAW_SCALE
#define UNIFONT_DRAW_SCALE
Definition: testime.c:61
SDL_RenderFillRect
#define SDL_RenderFillRect
Definition: SDL_dynapi_overrides.h:341
p
GLfloat GLfloat p
Definition: SDL_opengl_glext.h:11090
SDL_Surface
A collection of pixels used in software blitting.
Definition: SDL_surface.h:69
SDL_Renderer
Definition: SDL_sysrender.h:76
SDL_Color::b
Uint8 b
Definition: SDL_pixels.h:299
Uint32
uint32_t Uint32
Definition: SDL_stdinc.h:181
SDL_Surface::w
int w
Definition: SDL_surface.h:73
len
GLenum GLsizei len
Definition: SDL_opengl_glext.h:2926
lineColor
static SDL_Color lineColor
Definition: testime.c:44
utf8_decode
Uint32 utf8_decode(char *p, size_t len)
Definition: testime.c:412
SDL_Texture
Definition: SDL_sysrender.h:49
cursor
static int cursor
Definition: testime.c:48
SDL_RenderCopy
#define SDL_RenderCopy
Definition: SDL_dynapi_overrides.h:343
utf8_advance
char * utf8_advance(char *p, size_t distance)
Definition: testime.c:402
texture
GLenum GLenum GLuint texture
Definition: SDL_opengl_glext.h:1178
SDL_StopTextInput
#define SDL_StopTextInput
Definition: SDL_dynapi_overrides.h:228
SDL_CreateTextureFromSurface
#define SDL_CreateTextureFromSurface
Definition: SDL_dynapi_overrides.h:307
SDL_Color::r
Uint8 r
Definition: SDL_pixels.h:297
SDL_FreeSurface
#define SDL_FreeSurface
Definition: SDL_dynapi_overrides.h:446
renderer
static SDL_Renderer * renderer
Definition: testaudiocapture.c:21
SDL_Color::a
Uint8 a
Definition: SDL_pixels.h:300
c
const GLubyte * c
Definition: SDL_opengl_glext.h:11093
state
static SDLTest_CommonState * state
Definition: testime.c:42
SDL_Rect::x
int x
Definition: SDL_rect.h:66
Sint32
int32_t Sint32
Definition: SDL_stdinc.h:175
SDL_Rect::w
int w
Definition: SDL_rect.h:67
SDL_StartTextInput
#define SDL_StartTextInput
Definition: SDL_dynapi_overrides.h:226
unifont_draw_glyph
static Sint32 unifont_draw_glyph(Uint32 codepoint, int rendererID, SDL_Rect *dstrect)
Definition: testime.c:320
SDLTest_CommonState::renderers
SDL_Renderer ** renderers
Definition: SDL_test_common.h:84
i
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
NULL
#define NULL
Definition: begin_code.h:164
text
static char text[MAX_TEXT_LENGTH]
Definition: testime.c:47
SDL_SetTextInputRect
#define SDL_SetTextInputRect
Definition: SDL_dynapi_overrides.h:229
markedText
static char markedText[SDL_TEXTEDITINGEVENT_TEXT_SIZE]
Definition: testime.c:47
SDL_DestroyTexture
#define SDL_DestroyTexture
Definition: SDL_dynapi_overrides.h:347
SDL_strlen
#define SDL_strlen
Definition: SDL_dynapi_overrides.h:393
SDL_Rect::h
int h
Definition: SDL_rect.h:67
textRect
static SDL_Rect textRect
Definition: testime.c:43
utf8_length
size_t utf8_length(unsigned char c)
Definition: testime.c:371
SDL_SetRenderDrawColor
#define SDL_SetRenderDrawColor
Definition: SDL_dynapi_overrides.h:330
SDL_Rect::y
int y
Definition: SDL_rect.h:66
markedRect
static SDL_Rect markedRect
Definition: testime.c:43
textColor
static SDL_Color textColor
Definition: testime.c:46
SDL_Rect
A rectangle, with the origin at the upper left.
Definition: SDL_rect.h:64

## CleanupVideo()

void CleanupVideo | ( |  | ) |

Definition at line 455 of file testime.c.

References SDL_StopTextInput, and unifont_cleanup().

Referenced by main().

{
SDL_StopTextInput();
#ifdef HAVE_SDL_TTF
TTF_CloseFont(font);
TTF_Quit();
#else
unifont_cleanup();
#endif
}
SDL_StopTextInput
#define SDL_StopTextInput
Definition: SDL_dynapi_overrides.h:228
unifont_cleanup
static void unifont_cleanup()
Definition: testime.c:345

## dehex()

static Uint8 dehex | ( | char | c | ) |

static

Definition at line 71 of file testime.c.

Referenced by dehex2(), and validate_hex().

{
if (c >= '0' && c <= '9')
return c - '0';
else if (c >= 'a' && c <= 'f')
return c - 'a' + 10;
else if (c >= 'A' && c <= 'F')
return c - 'A' + 10;
return 255;
}
c
const GLubyte * c
Definition: SDL_opengl_glext.h:11093

## dehex2()

static Uint8 dehex2 | ( | char | c1,
|  | char | c2
| ) |  |

static

Definition at line 82 of file testime.c.

References dehex().

Referenced by unifont_init().

{
return (dehex(c1) << 4) | dehex(c2);
}
dehex
static Uint8 dehex(char c)
Definition: testime.c:71

## InitInput()

void InitInput | ( |  | ) |

Definition at line 441 of file testime.c.

References DEFAULT_WINDOW_WIDTH, SDL_Rect::h, markedText, SDL_StartTextInput, text, textRect, SDL_Rect::w, SDL_Rect::x, and SDL_Rect::y.

Referenced by main().

{
/* Prepare a rect for text input */
textRect.x = textRect.y = 100;
textRect.w = DEFAULT_WINDOW_WIDTH - 2 * textRect.x;
textRect.h = 50;
text[0] = 0;
markedRect = textRect;
markedText[0] = 0;
SDL_StartTextInput();
}
SDL_Rect::x
int x
Definition: SDL_rect.h:66
SDL_Rect::w
int w
Definition: SDL_rect.h:67
SDL_StartTextInput
#define SDL_StartTextInput
Definition: SDL_dynapi_overrides.h:226
text
static char text[MAX_TEXT_LENGTH]
Definition: testime.c:47
markedText
static char markedText[SDL_TEXTEDITINGEVENT_TEXT_SIZE]
Definition: testime.c:47
SDL_Rect::h
int h
Definition: SDL_rect.h:67
textRect
static SDL_Rect textRect
Definition: testime.c:43
DEFAULT_WINDOW_WIDTH
#define DEFAULT_WINDOW_WIDTH
Definition: SDL_test_common.h:41
SDL_Rect::y
int y
Definition: SDL_rect.h:66
markedRect
static SDL_Rect markedRect
Definition: testime.c:43

## main()

int main | ( | int | argc,
|  | char * | argv[]
| ) |  |

Definition at line 632 of file testime.c.

References CleanupVideo(), cursor, DEFAULT_FONT, DEFAULT_PTSIZE, done, SDL_Event::edit, i, InitInput(), SDL_Event::key, SDL_KeyboardEvent::keysym, SDL_TextEditingEvent::length, markedText, SDLTest_CommonState::num_windows, Redraw(), renderer, SDLTest_CommonState::renderers, SDL_Keysym::scancode, SDL_BLENDMODE_NONE, SDL_GetKeyName, SDL_GetScancodeName, SDL_INIT_VIDEO, SDL_KEYDOWN, SDL_Log, SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, SDL_LogError, SDL_LogSetPriority, SDL_PollEvent, SDL_RenderClear, SDL_SetRenderDrawBlendMode, SDL_SetRenderDrawColor, SDL_strlcat, SDL_strlcpy, SDL_strlen, SDL_TEXTEDITING, SDL_TEXTEDITINGEVENT_TEXT_SIZE, SDL_TEXTINPUT, SDLK_BACKSPACE, SDLK_RETURN, SDLTest_CommonArg(), SDLTest_CommonCreateState(), SDLTest_CommonEvent(), SDLTest_CommonInit(), SDLTest_CommonQuit(), SDL_TextEditingEvent::start, SDL_Keysym::sym, text, SDL_TextEditingEvent::text, SDL_TextInputEvent::text, SDL_Event::text, SDL_Event::type, unifont_init(), usage(), and SDL_Rect::w.

{
int i, done;
SDL_Event event;
const char *fontname = DEFAULT_FONT;
/* Enable standard application logging */
SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
/* Initialize test framework */
state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
if (!state) {
return 1;
}
for (i = 1; i < argc;i++) {
SDLTest_CommonArg(state, i);
}
for (argc--, argv++; argc > 0; argc--, argv++)
{
if (strcmp(argv[0], "--help") == 0) {
usage();
return 0;
}
else if (strcmp(argv[0], "--font") == 0)
{
argc--;
argv++;
if (argc > 0)
fontname = argv[0];
else {
usage();
return 0;
}
}
}
if (!SDLTest_CommonInit(state)) {
return 2;
}
#ifdef HAVE_SDL_TTF
/* Initialize fonts */
TTF_Init();
font = TTF_OpenFont(fontname, DEFAULT_PTSIZE);
if (! font)
{
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to find font: %s\n", TTF_GetError());
return -1;
}
#else
if (unifont_init(fontname) < 0) {
return -1;
}
#endif
SDL_Log("Using font: %s\n", fontname);
InitInput();
/* Create the windows and initialize the renderers */
for (i = 0; i < state->num_windows; ++i) {
SDL_Renderer *renderer = state->renderers[i];
SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_NONE);
SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
SDL_RenderClear(renderer);
}
Redraw();
/* Main render loop */
done = 0;
while (!done) {
/* Check for events */
while (SDL_PollEvent(&event)) {
SDLTest_CommonEvent(state, &event, &done);
switch(event.type) {
case SDL_KEYDOWN: {
switch (event.key.keysym.sym)
{
case SDLK_RETURN:
text[0]=0x00;
Redraw();
break;
case SDLK_BACKSPACE:
/* Only delete text if not in editing mode. */
if (!markedText[0])
{
size_t textlen = SDL_strlen(text);
do {
if (textlen==0)
{
break;
}
if ((text[textlen-1] & 0x80) == 0x00)
{
/* One byte */
text[textlen-1]=0x00;
break;
}
if ((text[textlen-1] & 0xC0) == 0x80)
{
/* Byte from the multibyte sequence */
text[textlen-1]=0x00;
textlen--;
}
if ((text[textlen-1] & 0xC0) == 0xC0)
{
/* First byte of multibyte sequence */
text[textlen-1]=0x00;
break;
}
} while(1);
Redraw();
}
break;
}
if (done)
{
break;
}
SDL_Log("Keyboard: scancode 0x%08X = %s, keycode 0x%08X = %s\n",
event.key.keysym.scancode,
SDL_GetScancodeName(event.key.keysym.scancode),
event.key.keysym.sym, SDL_GetKeyName(event.key.keysym.sym));
break;
case SDL_TEXTINPUT:
if (event.text.text[0] == '\0' || event.text.text[0] == '\n' ||
markedRect.w < 0)
break;
SDL_Log("Keyboard: text input \"%s\"\n", event.text.text);
if (SDL_strlen(text) + SDL_strlen(event.text.text) < sizeof(text))
SDL_strlcat(text, event.text.text, sizeof(text));
SDL_Log("text inputed: %s\n", text);
/* After text inputed, we can clear up markedText because it */
/* is committed */
markedText[0] = 0;
Redraw();
break;
case SDL_TEXTEDITING:
SDL_Log("text editing \"%s\", selected range (%d, %d)\n",
event.edit.text, event.edit.start, event.edit.length);
SDL_strlcpy(markedText, event.edit.text, SDL_TEXTEDITINGEVENT_TEXT_SIZE);
cursor = event.edit.start;
Redraw();
break;
}
break;
}
}
}
CleanupVideo();
SDLTest_CommonQuit(state);
return 0;
}
SDL_strlcpy
#define SDL_strlcpy
Definition: SDL_dynapi_overrides.h:394
CleanupVideo
void CleanupVideo()
Definition: testime.c:455
SDL_TextEditingEvent::start
Sint32 start
Definition: SDL_events.h:212
SDL_PollEvent
#define SDL_PollEvent
Definition: SDL_dynapi_overrides.h:122
SDL_KEYDOWN
Definition: SDL_events.h:93
SDLK_BACKSPACE
Definition: SDL_keycode.h:56
SDL_strlcat
#define SDL_strlcat
Definition: SDL_dynapi_overrides.h:396
SDL_BLENDMODE_NONE
Definition: SDL_blendmode.h:42
SDLTest_CommonCreateState
SDLTest_CommonState * SDLTest_CommonCreateState(char **argv, Uint32 flags)
Parse command line parameters and create common state.
Definition: SDL_test_common.c:48
SDLTest_CommonArg
int SDLTest_CommonArg(SDLTest_CommonState *state, int index)
Process one common argument.
Definition: SDL_test_common.c:106
SDL_SetRenderDrawBlendMode
#define SDL_SetRenderDrawBlendMode
Definition: SDL_dynapi_overrides.h:332
SDL_Renderer
Definition: SDL_sysrender.h:76
SDL_Keysym::scancode
SDL_Scancode scancode
Definition: SDL_keyboard.h:49
SDLTest_CommonInit
SDL_bool SDLTest_CommonInit(SDLTest_CommonState *state)
Open test window.
Definition: SDL_test_common.c:726
SDL_TextEditingEvent::length
Sint32 length
Definition: SDL_events.h:213
SDLK_RETURN
Definition: SDL_keycode.h:54
cursor
static int cursor
Definition: testime.c:48
SDL_LogError
#define SDL_LogError
Definition: SDL_dynapi_overrides.h:36
SDLTest_CommonState::num_windows
int num_windows
Definition: SDL_test_common.h:77
SDL_Log
#define SDL_Log
Definition: SDL_dynapi_overrides.h:31
SDL_Event::text
SDL_TextInputEvent text
Definition: SDL_events.h:532
SDL_TextInputEvent::text
char text[SDL_TEXTINPUTEVENT_TEXT_SIZE]
Definition: SDL_events.h:226
SDLTest_CommonEvent
void SDLTest_CommonEvent(SDLTest_CommonState *state, SDL_Event *event, int *done)
Common event handler for test windows.
Definition: SDL_test_common.c:1425
InitInput
void InitInput()
Definition: testime.c:441
renderer
static SDL_Renderer * renderer
Definition: testaudiocapture.c:21
event
struct _cl_event * event
Definition: SDL_opengl_glext.h:2649
done
int done
Definition: checkkeys.c:28
SDL_TextEditingEvent::text
char text[SDL_TEXTEDITINGEVENT_TEXT_SIZE]
Definition: SDL_events.h:211
Redraw
void Redraw()
Definition: testime.c:615
SDL_LOG_PRIORITY_INFO
Definition: SDL_log.h:106
state
static SDLTest_CommonState * state
Definition: testime.c:42
SDL_KeyboardEvent::keysym
SDL_Keysym keysym
Definition: SDL_events.h:199
SDL_Rect::w
int w
Definition: SDL_rect.h:67
SDLTest_CommonState::renderers
SDL_Renderer ** renderers
Definition: SDL_test_common.h:84
unifont_init
static int unifont_init(const char *fontname)
Definition: testime.c:102
i
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
SDL_LogSetPriority
#define SDL_LogSetPriority
Definition: SDL_dynapi_overrides.h:236
SDL_Event::edit
SDL_TextEditingEvent edit
Definition: SDL_events.h:531
text
static char text[MAX_TEXT_LENGTH]
Definition: testime.c:47
SDL_RenderClear
#define SDL_RenderClear
Definition: SDL_dynapi_overrides.h:334
SDL_GetScancodeName
#define SDL_GetScancodeName
Definition: SDL_dynapi_overrides.h:222
SDL_TEXTINPUT
Definition: SDL_events.h:96
markedText
static char markedText[SDL_TEXTEDITINGEVENT_TEXT_SIZE]
Definition: testime.c:47
SDL_Event::key
SDL_KeyboardEvent key
Definition: SDL_events.h:530
SDL_strlen
#define SDL_strlen
Definition: SDL_dynapi_overrides.h:393
SDL_GetKeyName
#define SDL_GetKeyName
Definition: SDL_dynapi_overrides.h:224
SDL_Keysym::sym
SDL_Keycode sym
Definition: SDL_keyboard.h:50
DEFAULT_FONT
#define DEFAULT_FONT
Definition: testime.c:38
SDL_Event
General event structure.
Definition: SDL_events.h:525
SDL_SetRenderDrawColor
#define SDL_SetRenderDrawColor
Definition: SDL_dynapi_overrides.h:330
usage
void usage()
Definition: testime.c:436
DEFAULT_PTSIZE
#define DEFAULT_PTSIZE
Definition: testime.c:27
SDL_LOG_CATEGORY_APPLICATION
Definition: SDL_log.h:66
markedRect
static SDL_Rect markedRect
Definition: testime.c:43
SDLTest_CommonQuit
void SDLTest_CommonQuit(SDLTest_CommonState *state)
Close test window.
Definition: SDL_test_common.c:1751
SDL_TEXTEDITING
Definition: SDL_events.h:95
SDL_INIT_VIDEO
#define SDL_INIT_VIDEO
Definition: SDL.h:78
SDL_Event::type
Uint32 type
Definition: SDL_events.h:527
SDL_TEXTEDITINGEVENT_TEXT_SIZE
#define SDL_TEXTEDITINGEVENT_TEXT_SIZE
Definition: SDL_events.h:202

## Redraw()

void Redraw | ( |  | ) |

Definition at line 615 of file testime.c.

References _Redraw(), i, NULL, SDLTest_CommonState::num_windows, renderer, SDLTest_CommonState::renderers, SDL_RenderClear, SDL_RenderPresent, SDL_SetRenderDrawColor, and SDLTest_CommonState::windows.

Referenced by main().

{
int i;
for (i = 0; i < state->num_windows; ++i) {
SDL_Renderer *renderer = state->renderers[i];
if (state->windows[i] == NULL)
continue;
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
SDL_RenderClear(renderer);
/* Sending in the window id to let the font renderers know which one we're working with. */
_Redraw(i);
SDL_RenderPresent(renderer);
}
}
SDL_Renderer
Definition: SDL_sysrender.h:76
SDLTest_CommonState::windows
SDL_Window ** windows
Definition: SDL_test_common.h:78
SDLTest_CommonState::num_windows
int num_windows
Definition: SDL_test_common.h:77
renderer
static SDL_Renderer * renderer
Definition: testaudiocapture.c:21
_Redraw
void _Redraw(int rendererID)
Definition: testime.c:466
state
static SDLTest_CommonState * state
Definition: testime.c:42
SDLTest_CommonState::renderers
SDL_Renderer ** renderers
Definition: SDL_test_common.h:84
i
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
NULL
#define NULL
Definition: begin_code.h:164
SDL_RenderClear
#define SDL_RenderClear
Definition: SDL_dynapi_overrides.h:334
SDL_SetRenderDrawColor
#define SDL_SetRenderDrawColor
Definition: SDL_dynapi_overrides.h:330
SDL_RenderPresent
#define SDL_RenderPresent
Definition: SDL_dynapi_overrides.h:346

## unifont_cleanup()

static void unifont_cleanup | ( |  | ) |

static

Definition at line 345 of file testime.c.

References i, j, NULL, SDLTest_CommonState::num_windows, renderer, SDLTest_CommonState::renderers, SDL_DestroyTexture, SDL_free, UNIFONT_NUM_TEXTURES, unifontGlyph, unifontTextureLoaded, and SDLTest_CommonState::windows.

Referenced by CleanupVideo().

{
int i, j;
for (i = 0; i < state->num_windows; ++i)
{
SDL_Renderer *renderer = state->renderers[i];
if (state->windows[i] == NULL || renderer == NULL)
continue;
for (j = 0; j < UNIFONT_NUM_TEXTURES; j++)
{
SDL_Texture *tex = unifontTexture[UNIFONT_NUM_TEXTURES * i + j];
if (tex != NULL)
SDL_DestroyTexture(tex);
}
}
for (j = 0; j < UNIFONT_NUM_TEXTURES; j++)
unifontTextureLoaded[j] = 0;
SDL_free(unifontTexture);
SDL_free(unifontGlyph);
}
unifontTextureLoaded
static Uint8 unifontTextureLoaded[UNIFONT_NUM_TEXTURES]
Definition: testime.c:67
SDL_Renderer
Definition: SDL_sysrender.h:76
SDL_Texture
Definition: SDL_sysrender.h:49
SDLTest_CommonState::windows
SDL_Window ** windows
Definition: SDL_test_common.h:78
unifontTexture
static SDL_Texture ** unifontTexture
Definition: testime.c:66
SDLTest_CommonState::num_windows
int num_windows
Definition: SDL_test_common.h:77
renderer
static SDL_Renderer * renderer
Definition: testaudiocapture.c:21
SDL_free
#define SDL_free
Definition: SDL_dynapi_overrides.h:377
j
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 int in j)
Definition: SDL_x11sym.h:50
unifontGlyph
struct UnifontGlyph * unifontGlyph
state
static SDLTest_CommonState * state
Definition: testime.c:42
SDLTest_CommonState::renderers
SDL_Renderer ** renderers
Definition: SDL_test_common.h:84
i
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
NULL
#define NULL
Definition: begin_code.h:164
SDL_DestroyTexture
#define SDL_DestroyTexture
Definition: SDL_dynapi_overrides.h:347
UNIFONT_NUM_TEXTURES
#define UNIFONT_NUM_TEXTURES
Definition: testime.c:58

## unifont_draw_glyph()

static Sint32 unifont_draw_glyph | ( | Uint32 | codepoint,
|  | int | rendererID,
|  | SDL_Rect * | dstrect
| ) |  |

static

Definition at line 320 of file testime.c.

References SDL_Rect::h, NULL, SDLTest_CommonState::renderers, SDL_RenderCopy, UNIFONT_GLYPHS_IN_ROW, UNIFONT_GLYPHS_IN_TEXTURE, unifont_load_texture(), UNIFONT_MAX_CODEPOINT, UNIFONT_NUM_TEXTURES, unifontGlyph, unifontTextureLoaded, SDL_Rect::w, UnifontGlyph::width, SDL_Rect::x, and SDL_Rect::y.

Referenced by _Redraw().

{
SDL_Texture *texture;
const Uint32 textureID = codepoint / UNIFONT_GLYPHS_IN_TEXTURE;
SDL_Rect srcrect;
srcrect.w = srcrect.h = 16;
if (codepoint > UNIFONT_MAX_CODEPOINT) {
return 0;
}
if (!unifontTextureLoaded[textureID]) {
if (unifont_load_texture(textureID) < 0) {
return 0;
}
}
texture = unifontTexture[UNIFONT_NUM_TEXTURES * rendererID + textureID];
if (texture != NULL)
{
const Uint32 cInTex = codepoint % UNIFONT_GLYPHS_IN_TEXTURE;
srcrect.x = cInTex % UNIFONT_GLYPHS_IN_ROW * 16;
srcrect.y = cInTex / UNIFONT_GLYPHS_IN_ROW * 16;
SDL_RenderCopy(state->renderers[rendererID], texture, &srcrect, dstrect);
}
return unifontGlyph[codepoint].width;
}
UnifontGlyph::width
Uint8 width
Definition: testime.c:63
unifontTextureLoaded
static Uint8 unifontTextureLoaded[UNIFONT_NUM_TEXTURES]
Definition: testime.c:67
UNIFONT_GLYPHS_IN_ROW
#define UNIFONT_GLYPHS_IN_ROW
Definition: testime.c:56
Uint32
uint32_t Uint32
Definition: SDL_stdinc.h:181
UNIFONT_MAX_CODEPOINT
#define UNIFONT_MAX_CODEPOINT
Definition: testime.c:52
SDL_Texture
Definition: SDL_sysrender.h:49
unifontTexture
static SDL_Texture ** unifontTexture
Definition: testime.c:66
SDL_RenderCopy
#define SDL_RenderCopy
Definition: SDL_dynapi_overrides.h:343
UNIFONT_GLYPHS_IN_TEXTURE
#define UNIFONT_GLYPHS_IN_TEXTURE
Definition: testime.c:57
texture
GLenum GLenum GLuint texture
Definition: SDL_opengl_glext.h:1178
unifontGlyph
struct UnifontGlyph * unifontGlyph
state
static SDLTest_CommonState * state
Definition: testime.c:42
SDL_Rect::x
int x
Definition: SDL_rect.h:66
SDL_Rect::w
int w
Definition: SDL_rect.h:67
SDLTest_CommonState::renderers
SDL_Renderer ** renderers
Definition: SDL_test_common.h:84
NULL
#define NULL
Definition: begin_code.h:164
SDL_Rect::h
int h
Definition: SDL_rect.h:67
UNIFONT_NUM_TEXTURES
#define UNIFONT_NUM_TEXTURES
Definition: testime.c:58
SDL_Rect::y
int y
Definition: SDL_rect.h:66
unifont_load_texture
static int unifont_load_texture(Uint32 textureID)
Definition: testime.c:263
SDL_Rect
A rectangle, with the origin at the upper left.
Definition: SDL_rect.h:64

## unifont_init()

static int unifont_init | ( | const char * | fontname | ) |

static

Definition at line 102 of file testime.c.

References dehex2(), i, NULL, SDLTest_CommonState::num_windows, SDL_Log, SDL_LOG_CATEGORY_APPLICATION, SDL_LogError, SDL_LogWarn, SDL_malloc, SDL_memmove, SDL_memset, SDL_RWclose, SDL_RWFromFile, SDL_RWread, UNIFONT_MAX_CODEPOINT, UNIFONT_NUM_GLYPHS, UNIFONT_NUM_TEXTURES, unifontGlyph, validate_hex(), and UnifontGlyph::width.

Referenced by main().

{
Uint8 hexBuffer[65];
Uint32 numGlyphs = 0;
int lineNumber = 1;
size_t bytesRead;
SDL_RWops *hexFile;
const size_t unifontGlyphSize = UNIFONT_NUM_GLYPHS * sizeof(struct UnifontGlyph);
const size_t unifontTextureSize = UNIFONT_NUM_TEXTURES * state->num_windows * sizeof(void *);
/* Allocate memory for the glyph data so the file can be closed after initialization. */
unifontGlyph = (struct UnifontGlyph *)SDL_malloc(unifontGlyphSize);
if (unifontGlyph == NULL)
{
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "unifont: Failed to allocate %d KiB for glyph data.\n", (int)(unifontGlyphSize + 1023) / 1024);
return -1;
}
SDL_memset(unifontGlyph, 0, unifontGlyphSize);
/* Allocate memory for texture pointers for all renderers. */
unifontTexture = (SDL_Texture **)SDL_malloc(unifontTextureSize);
if (unifontTexture == NULL)
{
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "unifont: Failed to allocate %d KiB for texture pointer data.\n", (int)(unifontTextureSize + 1023) / 1024);
return -1;
}
SDL_memset(unifontTexture, 0, unifontTextureSize);
hexFile = SDL_RWFromFile(fontname, "rb");
if (hexFile == NULL)
{
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "unifont: Failed to open font file: %s\n", fontname);
return -1;
}
/* Read all the glyph data into memory to make it accessible later when textures are created. */
do {
int i, codepointHexSize;
size_t bytesOverread;
Uint8 glyphWidth;
Uint32 codepoint;
bytesRead = SDL_RWread(hexFile, hexBuffer, 1, 9);
if (numGlyphs > 0 && bytesRead == 0)
break; /* EOF */
if ((numGlyphs == 0 && bytesRead == 0) || (numGlyphs > 0 && bytesRead < 9))
{
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "unifont: Unexpected end of hex file.\n");
return -1;
}
/* Looking for the colon that separates the codepoint and glyph data at position 2, 4, 6 and 8. */
if (hexBuffer[2] == ':')
codepointHexSize = 2;
else if (hexBuffer[4] == ':')
codepointHexSize = 4;
else if (hexBuffer[6] == ':')
codepointHexSize = 6;
else if (hexBuffer[8] == ':')
codepointHexSize = 8;
else
{
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "unifont: Could not find codepoint and glyph data separator symbol in hex file on line %d.\n", lineNumber);
return -1;
}
if (!validate_hex((const char *)hexBuffer, codepointHexSize, &codepoint))
{
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "unifont: Malformed hexadecimal number in hex file on line %d.\n", lineNumber);
return -1;
}
if (codepoint > UNIFONT_MAX_CODEPOINT)
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, "unifont: Codepoint on line %d exceeded limit of 0x%x.\n", lineNumber, UNIFONT_MAX_CODEPOINT);
/* If there was glyph data read in the last file read, move it to the front of the buffer. */
bytesOverread = 8 - codepointHexSize;
if (codepointHexSize < 8)
SDL_memmove(hexBuffer, hexBuffer + codepointHexSize + 1, bytesOverread);
bytesRead = SDL_RWread(hexFile, hexBuffer + bytesOverread, 1, 33 - bytesOverread);
if (bytesRead < (33 - bytesOverread))
{
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "unifont: Unexpected end of hex file.\n");
return -1;
}
if (hexBuffer[32] == '\n')
glyphWidth = 8;
else
{
glyphWidth = 16;
bytesRead = SDL_RWread(hexFile, hexBuffer + 33, 1, 32);
if (bytesRead < 32)
{
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "unifont: Unexpected end of hex file.\n");
return -1;
}
}
if (!validate_hex((const char *)hexBuffer, glyphWidth * 4, NULL))
{
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "unifont: Malformed hexadecimal glyph data in hex file on line %d.\n", lineNumber);
return -1;
}
if (codepoint <= UNIFONT_MAX_CODEPOINT)
{
if (unifontGlyph[codepoint].width > 0)
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, "unifont: Ignoring duplicate codepoint 0x%08x in hex file on line %d.\n", codepoint, lineNumber);
else
{
unifontGlyph[codepoint].width = glyphWidth;
/* Pack the hex data into a more compact form. */
for (i = 0; i < glyphWidth * 2; i++)
unifontGlyph[codepoint].data[i] = dehex2(hexBuffer[i * 2], hexBuffer[i * 2 + 1]);
numGlyphs++;
}
}
lineNumber++;
} while (bytesRead > 0);
SDL_RWclose(hexFile);
SDL_Log("unifont: Loaded %u glyphs.\n", numGlyphs);
return 0;
}
UnifontGlyph::width
Uint8 width
Definition: testime.c:63
validate_hex
static Uint8 validate_hex(const char *cp, size_t len, Uint32 *np)
Definition: testime.c:87
UNIFONT_NUM_GLYPHS
#define UNIFONT_NUM_GLYPHS
Definition: testime.c:53
SDL_RWops
Definition: SDL_rwops.h:52
dehex2
static Uint8 dehex2(char c1, char c2)
Definition: testime.c:82
SDL_RWread
#define SDL_RWread(ctx, ptr, size, n)
Definition: SDL_rwops.h:187
data
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: SDL_opengl.h:1974
Uint32
uint32_t Uint32
Definition: SDL_stdinc.h:181
UNIFONT_MAX_CODEPOINT
#define UNIFONT_MAX_CODEPOINT
Definition: testime.c:52
width
GLint GLint GLsizei width
Definition: SDL_opengl.h:1572
SDL_Texture
Definition: SDL_sysrender.h:49
unifontTexture
static SDL_Texture ** unifontTexture
Definition: testime.c:66
SDL_LogError
#define SDL_LogError
Definition: SDL_dynapi_overrides.h:36
SDLTest_CommonState::num_windows
int num_windows
Definition: SDL_test_common.h:77
SDL_Log
#define SDL_Log
Definition: SDL_dynapi_overrides.h:31
SDL_RWFromFile
#define SDL_RWFromFile
Definition: SDL_dynapi_overrides.h:351
Uint8
uint8_t Uint8
Definition: SDL_stdinc.h:157
unifontGlyph
struct UnifontGlyph * unifontGlyph
SDL_memmove
#define SDL_memmove
Definition: SDL_dynapi_overrides.h:388
state
static SDLTest_CommonState * state
Definition: testime.c:42
i
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
NULL
#define NULL
Definition: begin_code.h:164
SDL_RWclose
#define SDL_RWclose(ctx)
Definition: SDL_rwops.h:189
UnifontGlyph
Definition: testime.c:62
numGlyphs
GLenum const void GLbitfield GLsizei numGlyphs
Definition: SDL_opengl_glext.h:9160
SDL_malloc
#define SDL_malloc
Definition: SDL_dynapi_overrides.h:374
SDL_LogWarn
#define SDL_LogWarn
Definition: SDL_dynapi_overrides.h:35
UNIFONT_NUM_TEXTURES
#define UNIFONT_NUM_TEXTURES
Definition: testime.c:58
SDL_LOG_CATEGORY_APPLICATION
Definition: SDL_log.h:66
SDL_memset
#define SDL_memset
Definition: SDL_dynapi_overrides.h:386

## unifont_load_texture()

static int unifont_load_texture | ( | Uint32 | textureID | ) |

static

Definition at line 263 of file testime.c.

References i, NULL, SDLTest_CommonState::num_windows, renderer, SDLTest_CommonState::renderers, SDL_BLENDMODE_BLEND, SDL_CreateTexture, SDL_free, SDL_Log, SDL_LOG_CATEGORY_APPLICATION, SDL_LogError, SDL_malloc, SDL_memset, SDL_PIXELFORMAT_ARGB8888, SDL_SetTextureBlendMode, SDL_TEXTUREACCESS_STATIC, SDL_UpdateTexture, UNIFONT_GLYPHS_IN_ROW, UNIFONT_GLYPHS_IN_TEXTURE, unifont_make_rgba(), UNIFONT_NUM_TEXTURES, UNIFONT_TEXTURE_PITCH, UNIFONT_TEXTURE_SIZE, UNIFONT_TEXTURE_WIDTH, unifontGlyph, unifontTextureLoaded, and SDLTest_CommonState::windows.

Referenced by unifont_draw_glyph().

{
int i;
Uint8 * textureRGBA;
if (textureID >= UNIFONT_NUM_TEXTURES)
{
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "unifont: Tried to load out of range texture %u.\n", textureID);
return -1;
}
textureRGBA = (Uint8 *)SDL_malloc(UNIFONT_TEXTURE_SIZE);
if (textureRGBA == NULL)
{
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "unifont: Failed to allocate %d MiB for a texture.\n", UNIFONT_TEXTURE_SIZE / 1024 / 1024);
return -1;
}
SDL_memset(textureRGBA, 0, UNIFONT_TEXTURE_SIZE);
/* Copy the glyphs into memory in RGBA format. */
for (i = 0; i < UNIFONT_GLYPHS_IN_TEXTURE; i++)
{
Uint32 codepoint = UNIFONT_GLYPHS_IN_TEXTURE * textureID + i;
if (unifontGlyph[codepoint].width > 0)
{
const Uint32 cInTex = codepoint % UNIFONT_GLYPHS_IN_TEXTURE;
const size_t offset = (cInTex / UNIFONT_GLYPHS_IN_ROW) * UNIFONT_TEXTURE_PITCH * 16 + (cInTex % UNIFONT_GLYPHS_IN_ROW) * 16 * 4;
unifont_make_rgba(unifontGlyph[codepoint].data, textureRGBA + offset, unifontGlyph[codepoint].width);
}
}
/* Create textures and upload the RGBA data from above. */
for (i = 0; i < state->num_windows; ++i)
{
SDL_Renderer *renderer = state->renderers[i];
SDL_Texture *tex = unifontTexture[UNIFONT_NUM_TEXTURES * i + textureID];
if (state->windows[i] == NULL || renderer == NULL || tex != NULL)
continue;
tex = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STATIC, UNIFONT_TEXTURE_WIDTH, UNIFONT_TEXTURE_WIDTH);
if (tex == NULL)
{
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "unifont: Failed to create texture %u for renderer %d.\n", textureID, i);
return -1;
}
unifontTexture[UNIFONT_NUM_TEXTURES * i + textureID] = tex;
SDL_SetTextureBlendMode(tex, SDL_BLENDMODE_BLEND);
if (SDL_UpdateTexture(tex, NULL, textureRGBA, UNIFONT_TEXTURE_PITCH) != 0)
{
SDL_Log("unifont error: Failed to update texture %u data for renderer %d.\n", textureID, i);
}
}
SDL_free(textureRGBA);
unifontTextureLoaded[textureID] = 1;
return 0;
}
SDL_CreateTexture
#define SDL_CreateTexture
Definition: SDL_dynapi_overrides.h:306
SDL_PIXELFORMAT_ARGB8888
Definition: SDL_pixels.h:248
unifontTextureLoaded
static Uint8 unifontTextureLoaded[UNIFONT_NUM_TEXTURES]
Definition: testime.c:67
UNIFONT_GLYPHS_IN_ROW
#define UNIFONT_GLYPHS_IN_ROW
Definition: testime.c:56
SDL_SetTextureBlendMode
#define SDL_SetTextureBlendMode
Definition: SDL_dynapi_overrides.h:313
SDL_Renderer
Definition: SDL_sysrender.h:76
unifont_make_rgba
static void unifont_make_rgba(Uint8 *src, Uint8 *dst, Uint8 width)
Definition: testime.c:227
data
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: SDL_opengl.h:1974
offset
GLintptr offset
Definition: SDL_opengl_glext.h:538
Uint32
uint32_t Uint32
Definition: SDL_stdinc.h:181
UNIFONT_TEXTURE_WIDTH
#define UNIFONT_TEXTURE_WIDTH
Definition: testime.c:55
SDL_UpdateTexture
#define SDL_UpdateTexture
Definition: SDL_dynapi_overrides.h:315
width
GLint GLint GLsizei width
Definition: SDL_opengl.h:1572
SDL_Texture
Definition: SDL_sysrender.h:49
SDLTest_CommonState::windows
SDL_Window ** windows
Definition: SDL_test_common.h:78
unifontTexture
static SDL_Texture ** unifontTexture
Definition: testime.c:66
SDL_LogError
#define SDL_LogError
Definition: SDL_dynapi_overrides.h:36
SDLTest_CommonState::num_windows
int num_windows
Definition: SDL_test_common.h:77
SDL_Log
#define SDL_Log
Definition: SDL_dynapi_overrides.h:31
UNIFONT_GLYPHS_IN_TEXTURE
#define UNIFONT_GLYPHS_IN_TEXTURE
Definition: testime.c:57
renderer
static SDL_Renderer * renderer
Definition: testaudiocapture.c:21
Uint8
uint8_t Uint8
Definition: SDL_stdinc.h:157
SDL_free
#define SDL_free
Definition: SDL_dynapi_overrides.h:377
SDL_TEXTUREACCESS_STATIC
Definition: SDL_render.h:93
unifontGlyph
struct UnifontGlyph * unifontGlyph
state
static SDLTest_CommonState * state
Definition: testime.c:42
SDLTest_CommonState::renderers
SDL_Renderer ** renderers
Definition: SDL_test_common.h:84
i
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
NULL
#define NULL
Definition: begin_code.h:164
UNIFONT_TEXTURE_SIZE
#define UNIFONT_TEXTURE_SIZE
Definition: testime.c:59
SDL_malloc
#define SDL_malloc
Definition: SDL_dynapi_overrides.h:374
UNIFONT_TEXTURE_PITCH
#define UNIFONT_TEXTURE_PITCH
Definition: testime.c:60
UNIFONT_NUM_TEXTURES
#define UNIFONT_NUM_TEXTURES
Definition: testime.c:58
SDL_LOG_CATEGORY_APPLICATION
Definition: SDL_log.h:66
SDL_memset
#define SDL_memset
Definition: SDL_dynapi_overrides.h:386
SDL_BLENDMODE_BLEND
Definition: SDL_blendmode.h:44

## unifont_make_rgba()

static void unifont_make_rgba | ( | Uint8 * | src,
|  | Uint8 * | dst,
|  | Uint8 | width
| ) |  |

static

Definition at line 227 of file testime.c.

References SDL_Color::a, SDL_Color::b, SDL_Color::g, i, j, SDL_Color::r, and UNIFONT_TEXTURE_PITCH.

Referenced by unifont_load_texture().

{
int i, j;
Uint8 *row = dst;
for (i = 0; i < width * 2; i++)
{
Uint8 data = src[i];
for (j = 0; j < 8; j++)
{
if (data & 0x80)
{
row[0] = textColor.r;
row[1] = textColor.g;
row[2] = textColor.b;
row[3] = textColor.a;
}
else
{
row[0] = 0;
row[1] = 0;
row[2] = 0;
row[3] = 0;
}
data <<= 1;
row += 4;
}
if (width == 8 || (width == 16 && i % 2 == 1))
{
dst += UNIFONT_TEXTURE_PITCH;
row = dst;
}
}
}
SDL_Color::g
Uint8 g
Definition: SDL_pixels.h:298
dst
GLenum GLenum dst
Definition: SDL_opengl_glext.h:1737
data
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: SDL_opengl.h:1974
SDL_Color::b
Uint8 b
Definition: SDL_pixels.h:299
src
GLenum src
Definition: SDL_opengl_glext.h:1737
width
GLint GLint GLsizei width
Definition: SDL_opengl.h:1572
SDL_Color::r
Uint8 r
Definition: SDL_pixels.h:297
SDL_Color::a
Uint8 a
Definition: SDL_pixels.h:300
Uint8
uint8_t Uint8
Definition: SDL_stdinc.h:157
j
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 int in j)
Definition: SDL_x11sym.h:50
i
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
UNIFONT_TEXTURE_PITCH
#define UNIFONT_TEXTURE_PITCH
Definition: testime.c:60
row
GLenum GLenum void * row
Definition: SDL_opengl_glext.h:3138
textColor
static SDL_Color textColor
Definition: testime.c:46

## usage()

void usage | ( |  | ) |

Definition at line 436 of file testime.c.

References SDL_Log.

Referenced by main().

{
SDL_Log("usage: testime [--font fontfile]\n");
}
SDL_Log
#define SDL_Log
Definition: SDL_dynapi_overrides.h:31

## utf8_advance()

char* utf8_advance | ( | char * | p,
|  | size_t | distance
| ) |  |

Definition at line 402 of file testime.c.

References i, and utf8_next().

Referenced by _Redraw().

{
size_t i = 0;
for (; i < distance && p; ++i)
{
p = utf8_next(p);
}
return p;
}
p
GLfloat GLfloat p
Definition: SDL_opengl_glext.h:11090
utf8_next
char * utf8_next(char *p)
Definition: testime.c:386
distance
GLsizei GLsizei GLfloat distance
Definition: SDL_opengl_glext.h:9200
i
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

## utf8_decode()

Uint32 utf8_decode | ( | char * | p,
|  | size_t | len
| ) |  |

Definition at line 412 of file testime.c.

References i.

Referenced by _Redraw().

{
Uint32 codepoint = 0;
size_t i = 0;
if (!len)
return 0;
for (; i < len; ++i)
{
if (i == 0)
codepoint = (0xff >> len) & *p;
else
{
codepoint <<= 6;
codepoint |= 0x3f & *p;
}
if (!*p)
return 0;
p++;
}
return codepoint;
}
p
GLfloat GLfloat p
Definition: SDL_opengl_glext.h:11090
Uint32
uint32_t Uint32
Definition: SDL_stdinc.h:181
len
GLenum GLsizei len
Definition: SDL_opengl_glext.h:2926
i
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

## utf8_length()

size_t utf8_length | ( | unsigned char | c | ) |

Definition at line 371 of file testime.c.

Referenced by _Redraw(), and utf8_next().

{
c = (unsigned char)(0xff & c);
if (c < 0x80)
return 1;
else if ((c >> 5) ==0x6)
return 2;
else if ((c >> 4) == 0xe)
return 3;
else if ((c >> 3) == 0x1e)
return 4;
else
return 0;
}
c
const GLubyte * c
Definition: SDL_opengl_glext.h:11093

## utf8_next()

char* utf8_next | ( | char * | p | ) |

Definition at line 386 of file testime.c.

References i, and utf8_length().

Referenced by utf8_advance().

{
size_t len = utf8_length(*p);
size_t i = 0;
if (!len)
return 0;
for (; i < len; ++i)
{
++p;
if (!*p)
return 0;
}
return p;
}
p
GLfloat GLfloat p
Definition: SDL_opengl_glext.h:11090
len
GLenum GLsizei len
Definition: SDL_opengl_glext.h:2926
i
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
utf8_length
size_t utf8_length(unsigned char c)
Definition: testime.c:371

## validate_hex()

static Uint8 validate_hex | ( | const char * | cp,
|  | size_t | len,
|  | Uint32 * | np
| ) |  |

static

Definition at line 87 of file testime.c.

References dehex(), and NULL.

Referenced by unifont_init().

{
Uint32 n = 0;
for (; len > 0; cp++, len--)
{
Uint8 c = dehex(*cp);
if (c == 255)
return 0;
n = (n << 4) | c;
}
if (np != NULL)
*np = n;
return 1;
}
dehex
static Uint8 dehex(char c)
Definition: testime.c:71
Uint32
uint32_t Uint32
Definition: SDL_stdinc.h:181
len
GLenum GLsizei len
Definition: SDL_opengl_glext.h:2926
Uint8
uint8_t Uint8
Definition: SDL_stdinc.h:157
c
const GLubyte * c
Definition: SDL_opengl_glext.h:11093
NULL
#define NULL
Definition: begin_code.h:164
n
GLdouble n
Definition: SDL_opengl_glext.h:1952
cp
static const double cp
Definition: e_pow.c:92

## Variable Documentation

## backColor

SDL_Color backColor = {255,255,255,255}

static

Definition at line 45 of file testime.c.

## cursor

int cursor = 0

static

Definition at line 48 of file testime.c.

Referenced by _Redraw(), and main().

## lineColor

SDL_Color lineColor = {0,0,0,255}

static

Definition at line 44 of file testime.c.

## markedRect

SDL_Rect markedRect

static

Definition at line 43 of file testime.c.

Referenced by _Redraw().

## markedText

char markedText[SDL_TEXTEDITINGEVENT_TEXT_SIZE]

static

Definition at line 47 of file testime.c.

Referenced by _Redraw(), InitInput(), and main().

## state

SDLTest_CommonState* state

static

Definition at line 42 of file testime.c.

## text

char text[MAX_TEXT_LENGTH]

static

Definition at line 47 of file testime.c.

Referenced by SDL_BApp::_HandleKey(), _Redraw(), clipboard_testClipboardTextFunctions(), clipboard_testSetClipboardText(), InitInput(), main(), SDL_GetClipboardText(), SDL_LogOutput(), SDL_memset4(), SDL_PrintFloat(), SDL_ScanFloat(), SDL_ScanLong(), SDL_ScanLongLong(), SDL_ScanUintPtrT(), SDL_ScanUnsignedLong(), SDL_ScanUnsignedLongLong(), SDL_sscanf(), SDL_vsnprintf(), SDL_vsscanf(), SDLTest_CommonEvent(), SDLTest_CommonInit(), SDLTest_PrintRenderer(), stdlib_getsetenv(), stdlib_snprintf(), and stdlib_strlcpy().

## textColor

SDL_Color textColor = {0,0,0,255}

static

Definition at line 46 of file testime.c.

## textRect

SDL_Rect textRect

static

Definition at line 43 of file testime.c.

Referenced by _Redraw(), and InitInput().

## unifontGlyph

struct UnifontGlyph * unifontGlyph

Referenced by unifont_cleanup(), unifont_draw_glyph(), unifont_init(), and unifont_load_texture().

## unifontTexture

SDL_Texture** unifontTexture

static

Definition at line 66 of file testime.c.

## unifontTextureLoaded

Uint8 unifontTextureLoaded[UNIFONT_NUM_TEXTURES] = {0}

static

Definition at line 67 of file testime.c.

Referenced by unifont_cleanup(), unifont_draw_glyph(), and unifont_load_texture().