From 3c4c813bfabf4906c3fca969744bd9c884c0e62d Mon Sep 17 00:00:00 2001 From: Kim Kulling Date: Tue, 17 Mar 2026 14:03:59 +0100 Subject: [PATCH] Move the font instances --- src/backends/sdl2_renderer.cpp | 24 ++++++++++++------------ src/tinyui.h | 4 ++-- src/widgets.cpp | 6 +++--- 3 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/backends/sdl2_renderer.cpp b/src/backends/sdl2_renderer.cpp index 49fc4cb..272a46b 100644 --- a/src/backends/sdl2_renderer.cpp +++ b/src/backends/sdl2_renderer.cpp @@ -33,18 +33,18 @@ namespace tinyui { namespace { void loadFont(Context &ctx) { - ctx.mSDLContext.mDefaultFont = new Font; - ctx.mSDLContext.mDefaultFont->mFont = new FontImpl; - ctx.mSDLContext.mDefaultFont->mSize = ctx.mStyle.mFont.mSize; - ctx.mSDLContext.mDefaultFont->mFont->mFontImpl = TTF_OpenFont(ctx.mStyle.mFont.mName, ctx.mStyle.mFont.mSize); + ctx.mDefaultFont = new Font; + ctx.mDefaultFont->mFont = new FontImpl; + ctx.mDefaultFont->mSize = ctx.mStyle.mFont.mSize; + ctx.mDefaultFont->mFont->mFontImpl = TTF_OpenFont(ctx.mStyle.mFont.mName, ctx.mStyle.mFont.mSize); } Font *loadDefaultFont(Context &ctx) { Font *font = nullptr; - if (ctx.mSDLContext.mDefaultFont == nullptr) { + if (ctx.mDefaultFont == nullptr) { if (ctx.mStyle.mFont.mName != nullptr) { loadFont(ctx); - font = ctx.mSDLContext.mDefaultFont; + font = ctx.mDefaultFont; } } return font; @@ -174,9 +174,9 @@ ret_code Renderer::releaseRenderer(Context &ctx) { return ErrorCode; } - if (ctx.mSDLContext.mDefaultFont != nullptr) { - delete ctx.mSDLContext.mDefaultFont; - ctx.mSDLContext.mDefaultFont = nullptr; + if (ctx.mDefaultFont != nullptr) { + delete ctx.mDefaultFont; + ctx.mDefaultFont = nullptr; } IMG_Quit(); @@ -192,15 +192,15 @@ ret_code Renderer::drawText(Context &ctx, const char *string, Font *font, const return InvalidHandle; } - if (ctx.mSDLContext.mDefaultFont == nullptr) { + if (ctx.mDefaultFont == nullptr) { if (ctx.mStyle.mFont.mName != nullptr) { loadFont(ctx); if (font == nullptr) { - font = ctx.mSDLContext.mDefaultFont; + font = ctx.mDefaultFont; } } } - font = ctx.mSDLContext.mDefaultFont; + font = ctx.mDefaultFont; if (font == nullptr) { return InvalidHandle; diff --git a/src/tinyui.h b/src/tinyui.h index 4edd5ea..004a7cd 100644 --- a/src/tinyui.h +++ b/src/tinyui.h @@ -385,8 +385,6 @@ struct SDLContext { SDL_Window *mWindow{nullptr}; ///< The window. SDL_Surface *mSurface{ nullptr }; ///< The surface. SDL_Renderer *mRenderer{ nullptr }; ///< The renderer. - Font *mDefaultFont{ nullptr }; ///< The default font. - Font *mSelectedFont{ nullptr }; ///< The selected font. bool mOwner{ false }; ///< The owner state. }; @@ -403,6 +401,8 @@ struct Context { Style mStyle{}; ///< The active style. Widget *mRoot{nullptr}; ///< The root widget. Widget *mFocus{nullptr}; ///< The widget which is in focus. + Font *mDefaultFont{ nullptr }; ///< The default font. + Font *mSelectedFont{ nullptr }; ///< The selected font. Widget *mCurrentParent{ nullptr }; ///< The current parent widget for new widgets. tui_log_func mLogger{}; ///< The logger function. EventDispatchMap mEventDispatchMap; ///< The event dispatch map. diff --git a/src/widgets.cpp b/src/widgets.cpp index 802cabb..acac289 100644 --- a/src/widgets.cpp +++ b/src/widgets.cpp @@ -502,7 +502,7 @@ static void render(Context &ctx, Widget *currentWidget) { } if (!currentWidget->mText.empty()) { Color4 fg = ctx.mStyle.mTextColor, bg = ctx.mStyle.mBg; - Renderer::drawText(ctx, currentWidget->mText.c_str(), ctx.mSDLContext.mDefaultFont, + Renderer::drawText(ctx, currentWidget->mText.c_str(), ctx.mDefaultFont, currentWidget->mRect, fg, bg, currentWidget->mAlignment); } } @@ -513,7 +513,7 @@ static void render(Context &ctx, Widget *currentWidget) { Renderer::drawRect(ctx, r.top.x, r.top.y, r.width, r.height, false, ctx.mStyle.mFg); if (!currentWidget->mText.empty()) { Color4 fg = ctx.mStyle.mTextColor, bg = ctx.mStyle.mBg; - Renderer::drawText(ctx, currentWidget->mText.c_str(), ctx.mSDLContext.mDefaultFont, + Renderer::drawText(ctx, currentWidget->mText.c_str(), ctx.mDefaultFont, currentWidget->mRect, fg, bg, currentWidget->mAlignment); } } @@ -523,7 +523,7 @@ static void render(Context &ctx, Widget *currentWidget) { { if (!currentWidget->mText.empty()) { Color4 fg = ctx.mStyle.mTextColor, bg = ctx.mStyle.mBg; - Renderer::drawText(ctx, currentWidget->mText.c_str(), ctx.mSDLContext.mDefaultFont, + Renderer::drawText(ctx, currentWidget->mText.c_str(), ctx.mDefaultFont, currentWidget->mRect, fg, bg, currentWidget->mAlignment); } }