From f1b2de34d4e130b71bde738997c1497763f00f8a Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Tue, 18 Jan 2022 11:42:04 +1100 Subject: [PATCH] Launcher: Add dedicated DrawBackground method to Screens for drawing background launcher of screen/menu This means that e.g. time isn't wasted with drawing default background, then completely drawing over the top of it with checkresourcescreen's custom background --- src/LScreens.c | 51 +++++++++++++++++++++++++++++++++----------------- src/LScreens.h | 1 + src/Launcher.c | 38 ++++++------------------------------- src/Launcher.h | 2 ++ 4 files changed, 43 insertions(+), 49 deletions(-) diff --git a/src/LScreens.c b/src/LScreens.c index b3238fc66..9e78b8eff 100644 --- a/src/LScreens.c +++ b/src/LScreens.c @@ -16,6 +16,7 @@ #include "Input.h" #include "Options.h" #include "Utils.h" +#include "LBackend.h" /*########################################################################################################################* *---------------------------------------------------------Screen base-----------------------------------------------------* @@ -170,6 +171,31 @@ static void LScreen_MouseMove(struct LScreen* s, int idx) { } static void LScreen_MouseWheel(struct LScreen* s, float delta) { } +static void LScreen_DrawBackground(struct LScreen* s) { + cc_string title_fore, title_back; + struct DrawTextArgs args; + int x; + + if (!s->title_fore) { + Launcher_ResetArea(0, 0, WindowInfo.Width, WindowInfo.Height); + return; + } + title_fore = String_FromReadonly(s->title_fore); + title_back = String_FromReadonly(s->title_back); + + LBackend_ResetPixels(); + DrawTextArgs_Make(&args, &title_fore, &Launcher_LogoFont, false); + x = WindowInfo.Width / 2 - Drawer2D_TextWidth(&args) / 2; + + args.text = title_back; + Drawer2D_DrawText(&Launcher_Framebuffer, &args, + x + Display_ScaleX(4), Display_ScaleY(4)); + + args.text = title_fore; + Drawer2D_DrawText(&Launcher_Framebuffer, &args, + x, 0); +} + CC_NOINLINE static void LScreen_Reset(struct LScreen* s) { int i; @@ -184,9 +210,10 @@ CC_NOINLINE static void LScreen_Reset(struct LScreen* s) { s->MouseUp = LScreen_MouseUp; s->MouseMove = LScreen_MouseMove; s->MouseWheel = LScreen_MouseWheel; - s->HoverWidget = LScreen_HoverWidget; - s->UnhoverWidget = LScreen_UnhoverWidget; - s->TextChanged = LScreen_TextChanged; + s->HoverWidget = LScreen_HoverWidget; + s->UnhoverWidget = LScreen_UnhoverWidget; + s->TextChanged = LScreen_TextChanged; + s->DrawBackground = LScreen_DrawBackground; /* reset all widgets mouse state */ for (i = 0; i < s->numWidgets; i++) { @@ -1067,7 +1094,7 @@ CC_NOINLINE static void CheckResourcesScreen_ResetArea(int x, int y, int width, Launcher_MarkDirty(x, y, width, height); } -CC_NOINLINE static void CheckResourcesScreen_DrawBase(void) { +CC_NOINLINE static void CheckResourcesScreen_DrawBackground(struct LScreen* s) { int x, y, width, height; Drawer2D_Clear(&Launcher_Framebuffer, BitmapCol_Make(12, 12, 12, 255), 0, 0, WindowInfo.Width, WindowInfo.Height); @@ -1079,19 +1106,14 @@ CC_NOINLINE static void CheckResourcesScreen_DrawBase(void) { CheckResourcesScreen_ResetArea(x, y, width, height); } -static void CheckResourcesScreen_Draw(struct LScreen* s) { - CheckResourcesScreen_DrawBase(); - LScreen_Draw(s); -} - void CheckResourcesScreen_SetActive(void) { struct CheckResourcesScreen* s = &CheckResourcesScreen_Instance; LScreen_Reset((struct LScreen*)s); s->Init = CheckResourcesScreen_Init; s->Show = CheckResourcesScreen_Show; - s->Draw = CheckResourcesScreen_Draw; s->Layout = CheckResourcesScreen_Layout; - s->onEnterWidget = (struct LWidget*)&s->btnYes; + s->DrawBackground = CheckResourcesScreen_DrawBackground; + s->onEnterWidget = (struct LWidget*)&s->btnYes; Launcher_SetScreen((struct LScreen*)s); } @@ -1127,11 +1149,6 @@ static void FetchResourcesScreen_Layout(struct LScreen* s_) { LWidget_SetLocation(&s->sdrProgress, ANCHOR_CENTRE, ANCHOR_CENTRE, 0, 15); } -static void FetchResourcesScreen_Draw(struct LScreen* s) { - CheckResourcesScreen_DrawBase(); - LScreen_Draw(s); -} - static void FetchResourcesScreen_SetStatus(const cc_string* str) { struct LLabel* w = &FetchResourcesScreen_Instance.lblStatus; CheckResourcesScreen_ResetArea(w->last.X, w->last.Y, @@ -1197,9 +1214,9 @@ void FetchResourcesScreen_SetActive(void) { LScreen_Reset((struct LScreen*)s); s->Init = FetchResourcesScreen_Init; s->Show = FetchResourcesScreen_Show; - s->Draw = FetchResourcesScreen_Draw; s->Tick = FetchResourcesScreen_Tick; s->Layout = FetchResourcesScreen_Layout; + s->DrawBackground = CheckResourcesScreen_DrawBackground; Launcher_SetScreen((struct LScreen*)s); } diff --git a/src/LScreens.h b/src/LScreens.h index 1b1a33b81..913f86f59 100644 --- a/src/LScreens.h +++ b/src/LScreens.h @@ -17,6 +17,7 @@ typedef void(*LWidget_Func)(struct LScreen* s, struct LWidget* w); LScreen_Func Layout; /* Positions the widgets on the screen. */ \ LScreen_Func Draw; /* Draws all widgets and any other features such as lines/rectangles. */ \ LScreen_Func Tick; /* Repeatedly called multiple times every second. */ \ + void (*DrawBackground)(struct LScreen* s); \ void (*KeyDown)(struct LScreen* s, int key, cc_bool wasDown); \ void (*KeyPress)(struct LScreen* s, char c); \ void (*MouseDown)(struct LScreen* s, int idx); \ diff --git a/src/Launcher.c b/src/Launcher.c index 66dcaff3e..445612ded 100644 --- a/src/Launcher.c +++ b/src/Launcher.c @@ -27,11 +27,9 @@ static Rect2D dirty_rect; static struct LScreen* activeScreen; struct Bitmap Launcher_Framebuffer; -struct FontDesc Launcher_TitleFont, Launcher_TextFont, Launcher_HintFont; - +struct FontDesc Launcher_TitleFont, Launcher_TextFont; +struct FontDesc Launcher_HintFont, Launcher_LogoFont; static cc_bool pendingRedraw; -static struct FontDesc logoFont; -static int titleX, titleY; cc_bool Launcher_ShouldExit, Launcher_ShouldUpdate; static char hashBuffer[STRING_SIZE], userBuffer[STRING_SIZE]; @@ -255,7 +253,6 @@ static void Launcher_Init(void) { Drawer2D_MakeFont(&Launcher_TitleFont, 16, FONT_FLAGS_BOLD); Drawer2D_MakeFont(&Launcher_TextFont, 14, FONT_FLAGS_NONE); Drawer2D_MakeFont(&Launcher_HintFont, 12, FONT_FLAGS_NONE); - titleX = Display_ScaleX(4); titleY = Display_ScaleY(4); Drawer2D.Colors['g'] = BitmapCol_Make(125, 125, 125, 255); Utils_EnsureDirectory("texpacks"); @@ -265,10 +262,10 @@ static void Launcher_Init(void) { static void Launcher_Free(void) { Event_UnregisterAll(); Flags_Free(); - Font_Free(&logoFont); Font_Free(&Launcher_TitleFont); Font_Free(&Launcher_TextFont); Font_Free(&Launcher_HintFont); + Font_Free(&Launcher_LogoFont); hasBitmappedFont = false; CloseActiveScreen(); @@ -487,9 +484,9 @@ void Launcher_TryLoadTexturePack(void) { } void Launcher_UpdateLogoFont(void) { - Font_Free(&logoFont); + Font_Free(&Launcher_LogoFont); Drawer2D.BitmappedText = (useBitmappedFont || Launcher_Theme.ClassicBackground) && hasBitmappedFont; - Drawer2D_MakeFont(&logoFont, 32, FONT_FLAGS_NONE); + Drawer2D_MakeFont(&Launcher_LogoFont, 32, FONT_FLAGS_NONE); Drawer2D.BitmappedText = false; } @@ -498,31 +495,8 @@ void Launcher_ResetArea(int x, int y, int width, int height) { Launcher_MarkDirty(x, y, width, height); } -void Launcher_ResetPixels(void) { - cc_string title_fore, title_back; - struct DrawTextArgs args; - int x; - - if (activeScreen && !activeScreen->title_fore) { - Launcher_ResetArea(0, 0, WindowInfo.Width, WindowInfo.Height); - return; - } - title_fore = String_FromReadonly(activeScreen->title_fore); - title_back = String_FromReadonly(activeScreen->title_back); - - LBackend_ResetPixels(); - DrawTextArgs_Make(&args, &title_fore, &logoFont, false); - x = WindowInfo.Width / 2 - Drawer2D_TextWidth(&args) / 2; - - args.text = title_back; - Drawer2D_DrawText(&Launcher_Framebuffer, &args, x + titleX, titleY); - args.text = title_fore; - Drawer2D_DrawText(&Launcher_Framebuffer, &args, x, 0); - Launcher_MarkAllDirty(); -} - void Launcher_Redraw(void) { - Launcher_ResetPixels(); + activeScreen->DrawBackground(activeScreen); activeScreen->Draw(activeScreen); Launcher_MarkAllDirty(); } diff --git a/src/Launcher.h b/src/Launcher.h index c4d2fe907..ea0f1453e 100644 --- a/src/Launcher.h +++ b/src/Launcher.h @@ -13,6 +13,8 @@ extern struct Bitmap Launcher_Framebuffer; extern struct FontDesc Launcher_TitleFont, Launcher_TextFont; /* Default font for input widget hints. */ extern struct FontDesc Launcher_HintFont; +/* Default font for screen/menu titles. */ +extern struct FontDesc Launcher_LogoFont; /* Whether at the next tick, the launcher window should proceed to stop displaying frames and subsequently exit. */ extern cc_bool Launcher_ShouldExit;