Skip to content

Commit 26b49ab

Browse files
authored
Merge pull request #23 from kimkulling/bugfix/move_fonts_to_context_issue-22
Move the font instances
2 parents acd302a + 3c4c813 commit 26b49ab

3 files changed

Lines changed: 17 additions & 17 deletions

File tree

src/backends/sdl2_renderer.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,18 @@ namespace tinyui {
3333
namespace {
3434

3535
void loadFont(Context &ctx) {
36-
ctx.mSDLContext.mDefaultFont = new Font;
37-
ctx.mSDLContext.mDefaultFont->mFont = new FontImpl;
38-
ctx.mSDLContext.mDefaultFont->mSize = ctx.mStyle.mFont.mSize;
39-
ctx.mSDLContext.mDefaultFont->mFont->mFontImpl = TTF_OpenFont(ctx.mStyle.mFont.mName, ctx.mStyle.mFont.mSize);
36+
ctx.mDefaultFont = new Font;
37+
ctx.mDefaultFont->mFont = new FontImpl;
38+
ctx.mDefaultFont->mSize = ctx.mStyle.mFont.mSize;
39+
ctx.mDefaultFont->mFont->mFontImpl = TTF_OpenFont(ctx.mStyle.mFont.mName, ctx.mStyle.mFont.mSize);
4040
}
4141

4242
Font *loadDefaultFont(Context &ctx) {
4343
Font *font = nullptr;
44-
if (ctx.mSDLContext.mDefaultFont == nullptr) {
44+
if (ctx.mDefaultFont == nullptr) {
4545
if (ctx.mStyle.mFont.mName != nullptr) {
4646
loadFont(ctx);
47-
font = ctx.mSDLContext.mDefaultFont;
47+
font = ctx.mDefaultFont;
4848
}
4949
}
5050
return font;
@@ -174,9 +174,9 @@ ret_code Renderer::releaseRenderer(Context &ctx) {
174174
return ErrorCode;
175175
}
176176

177-
if (ctx.mSDLContext.mDefaultFont != nullptr) {
178-
delete ctx.mSDLContext.mDefaultFont;
179-
ctx.mSDLContext.mDefaultFont = nullptr;
177+
if (ctx.mDefaultFont != nullptr) {
178+
delete ctx.mDefaultFont;
179+
ctx.mDefaultFont = nullptr;
180180
}
181181

182182
IMG_Quit();
@@ -192,15 +192,15 @@ ret_code Renderer::drawText(Context &ctx, const char *string, Font *font, const
192192
return InvalidHandle;
193193
}
194194

195-
if (ctx.mSDLContext.mDefaultFont == nullptr) {
195+
if (ctx.mDefaultFont == nullptr) {
196196
if (ctx.mStyle.mFont.mName != nullptr) {
197197
loadFont(ctx);
198198
if (font == nullptr) {
199-
font = ctx.mSDLContext.mDefaultFont;
199+
font = ctx.mDefaultFont;
200200
}
201201
}
202202
}
203-
font = ctx.mSDLContext.mDefaultFont;
203+
font = ctx.mDefaultFont;
204204

205205
if (font == nullptr) {
206206
return InvalidHandle;

src/tinyui.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -385,8 +385,6 @@ struct SDLContext {
385385
SDL_Window *mWindow{nullptr}; ///< The window.
386386
SDL_Surface *mSurface{ nullptr }; ///< The surface.
387387
SDL_Renderer *mRenderer{ nullptr }; ///< The renderer.
388-
Font *mDefaultFont{ nullptr }; ///< The default font.
389-
Font *mSelectedFont{ nullptr }; ///< The selected font.
390388
bool mOwner{ false }; ///< The owner state.
391389
};
392390

@@ -403,6 +401,8 @@ struct Context {
403401
Style mStyle{}; ///< The active style.
404402
Widget *mRoot{nullptr}; ///< The root widget.
405403
Widget *mFocus{nullptr}; ///< The widget which is in focus.
404+
Font *mDefaultFont{ nullptr }; ///< The default font.
405+
Font *mSelectedFont{ nullptr }; ///< The selected font.
406406
Widget *mCurrentParent{ nullptr }; ///< The current parent widget for new widgets.
407407
tui_log_func mLogger{}; ///< The logger function.
408408
EventDispatchMap mEventDispatchMap; ///< The event dispatch map.

src/widgets.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,7 @@ static void render(Context &ctx, Widget *currentWidget) {
502502
}
503503
if (!currentWidget->mText.empty()) {
504504
Color4 fg = ctx.mStyle.mTextColor, bg = ctx.mStyle.mBg;
505-
Renderer::drawText(ctx, currentWidget->mText.c_str(), ctx.mSDLContext.mDefaultFont,
505+
Renderer::drawText(ctx, currentWidget->mText.c_str(), ctx.mDefaultFont,
506506
currentWidget->mRect, fg, bg, currentWidget->mAlignment);
507507
}
508508
}
@@ -513,7 +513,7 @@ static void render(Context &ctx, Widget *currentWidget) {
513513
Renderer::drawRect(ctx, r.top.x, r.top.y, r.width, r.height, false, ctx.mStyle.mFg);
514514
if (!currentWidget->mText.empty()) {
515515
Color4 fg = ctx.mStyle.mTextColor, bg = ctx.mStyle.mBg;
516-
Renderer::drawText(ctx, currentWidget->mText.c_str(), ctx.mSDLContext.mDefaultFont,
516+
Renderer::drawText(ctx, currentWidget->mText.c_str(), ctx.mDefaultFont,
517517
currentWidget->mRect, fg, bg, currentWidget->mAlignment);
518518
}
519519
}
@@ -523,7 +523,7 @@ static void render(Context &ctx, Widget *currentWidget) {
523523
{
524524
if (!currentWidget->mText.empty()) {
525525
Color4 fg = ctx.mStyle.mTextColor, bg = ctx.mStyle.mBg;
526-
Renderer::drawText(ctx, currentWidget->mText.c_str(), ctx.mSDLContext.mDefaultFont,
526+
Renderer::drawText(ctx, currentWidget->mText.c_str(), ctx.mDefaultFont,
527527
currentWidget->mRect, fg, bg, currentWidget->mAlignment);
528528
}
529529
}

0 commit comments

Comments
 (0)