Skip to content

Commit

Permalink
Changes to fonts automatically update text objects using them.
Browse files Browse the repository at this point in the history
Also added the ability to update text object properties (engine, font, text, etc.) dynamically at runtime.
  • Loading branch information
slouken committed Oct 2, 2024
1 parent eed3670 commit 6b5e9a2
Show file tree
Hide file tree
Showing 8 changed files with 932 additions and 231 deletions.
151 changes: 124 additions & 27 deletions examples/showfont.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ typedef struct {
SDL_FRect messageRect;
TextEngine textEngine;
TTF_Text *text;
SDL_FRect textRect;
} Scene;

static void draw_scene(Scene *scene)
Expand All @@ -70,29 +71,25 @@ static void draw_scene(Scene *scene)
SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF);
SDL_RenderClear(renderer);

/* Flush the renderer so we can draw to the window surface in TextEngineSurface mode */
SDL_FlushRenderer(renderer);

if (scene->text) {
int i;
int w, h;

SDL_GetRenderOutputSize(renderer, &w, &h);

for (i = 0; i < 100; ++i) {
int x = SDL_rand(w) - scene->text->w / 2;
int y = SDL_rand(h) - scene->text->h / 2;

switch (scene->textEngine) {
case TextEngineSurface:
TTF_DrawSurfaceText(scene->text, x, y, scene->window_surface);
break;
case TextEngineRenderer:
TTF_DrawRendererText(scene->text, (float)x, (float)y);
break;
default:
break;
}
float x = scene->textRect.x + 4.0f;
float y = scene->textRect.y + 4.0f;

/* Clear the text rect to light gray */
SDL_SetRenderDrawColor(renderer, 0xCC, 0xCC, 0xCC, 0xFF);
SDL_RenderFillRect(renderer, &scene->textRect);

switch (scene->textEngine) {
case TextEngineSurface:
/* Flush the renderer so we can draw directly to the window surface */
SDL_FlushRenderer(renderer);
TTF_DrawSurfaceText(scene->text, (int)x, (int)y, scene->window_surface);
break;
case TextEngineRenderer:
TTF_DrawRendererText(scene->text, x, y);
break;
default:
break;
}
}

Expand Down Expand Up @@ -394,11 +391,12 @@ int main(int argc, char *argv[])
break;
}
if (engine) {
if (wrap) {
scene.text = TTF_CreateText_Wrapped(engine, font, message, 0, 0);
} else {
scene.text = TTF_CreateText(engine, font, message, 0);
}
scene.textRect.x = 8.0f;
scene.textRect.y = scene.captionRect.y + scene.captionRect.h + 4.0f;
scene.textRect.w = WIDTH / 2 - scene.textRect.x * 2;
scene.textRect.h = scene.messageRect.y - scene.textRect.y - 16.0f;

scene.text = TTF_CreateText_Wrapped(engine, font, message, 0, (int)scene.textRect.w);
if (scene.text) {
scene.text->color.r = forecol->r / 255.0f;
scene.text->color.g = forecol->g / 255.0f;
Expand All @@ -420,6 +418,105 @@ int main(int argc, char *argv[])
break;

case SDL_EVENT_KEY_DOWN:
switch (event.key.key) {
case SDLK_B:
/* Toggle bold style */
{
int style = TTF_GetFontStyle(font);
if (style & TTF_STYLE_BOLD) {
style &= ~TTF_STYLE_BOLD;
} else {
style |= TTF_STYLE_BOLD;
}
TTF_SetFontStyle(font, style);
}
break;
case SDLK_C:
/* Copy to clipboard */
if (event.key.mod & SDL_KMOD_CTRL) {
if (scene.text) {
SDL_SetClipboardText(scene.text->text);
}
}
break;
case SDLK_I:
/* Toggle italic style */
{
int style = TTF_GetFontStyle(font);
if (style & TTF_STYLE_ITALIC) {
style &= ~TTF_STYLE_ITALIC;
} else {
style |= TTF_STYLE_ITALIC;
}
TTF_SetFontStyle(font, style);
}
break;
case SDLK_O:
/* Toggle font outline */
{
int outline = TTF_GetFontOutline(font);
if (outline) {
outline = 0;
} else {
outline = 1;
}
TTF_SetFontOutline(font, outline);
}
break;
case SDLK_S:
/* Toggle strike-through style */
{
int style = TTF_GetFontStyle(font);
if (style & TTF_STYLE_STRIKETHROUGH) {
style &= ~TTF_STYLE_STRIKETHROUGH;
} else {
style |= TTF_STYLE_STRIKETHROUGH;
}
TTF_SetFontStyle(font, style);
}
break;
case SDLK_U:
/* Toggle underline style */
{
int style = TTF_GetFontStyle(font);
if (style & TTF_STYLE_UNDERLINE) {
style &= ~TTF_STYLE_UNDERLINE;
} else {
style |= TTF_STYLE_UNDERLINE;
}
TTF_SetFontStyle(font, style);
}
break;
case SDLK_V:
/* Paste from clipboard */
if (event.key.mod & SDL_KMOD_CTRL) {
if (scene.text) {
TTF_SetTextString(scene.text, SDL_GetClipboardText(), 0);
}
}
break;
case SDLK_UP:
/* Increase font size */
{
float ptsize = TTF_GetFontSize(font);
TTF_SetFontSize(font, ptsize + 2.0f);
}
break;
case SDLK_DOWN:
/* Decrease font size */
{
float ptsize = TTF_GetFontSize(font);
TTF_SetFontSize(font, ptsize - 2.0f);
}
break;
case SDLK_ESCAPE:
done = 1;
break;
default:
break;
}
break;

case SDL_EVENT_QUIT:
done = 1;
break;
Expand Down
4 changes: 1 addition & 3 deletions include/SDL3_ttf/SDL_textengine.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,9 @@ struct TTF_TextEngine
* All fields of `text` except `internal->engine_text` will already be filled out.
*
* \param userdata the userdata pointer in this interface.
* \param font the font being used.
* \param font_generation the unique ID of the font generation being used. This changes whenever the font changes size or style and needs new glyphs, and is unique across all fonts.
* \param text the text object being created.
*/
bool (SDLCALL *CreateText)(void *userdata, TTF_Font *font, Uint32 font_generation, TTF_Text *text);
bool (SDLCALL *CreateText)(void *userdata, TTF_Text *text);

/**
* Destroy a text representation.
Expand Down
Loading

0 comments on commit 6b5e9a2

Please sign in to comment.