diff --git a/src/Game.c b/src/Game.c index 662f7acea..9d6df282a 100644 --- a/src/Game.c +++ b/src/Game.c @@ -178,7 +178,7 @@ void Game_UpdateProjection(void) { void Game_Disconnect(const String* title, const String* reason) { Event_RaiseVoid(&NetEvents.Disconnected); Gui_FreeActive(); - Gui_SetActive(DisconnectScreen_MakeInstance(title, reason)); + DisconnectScreen_Show(title, reason); Game_Reset(); } diff --git a/src/Gui.c b/src/Gui.c index 9cd261722..4916b3d9b 100644 --- a/src/Gui.c +++ b/src/Gui.c @@ -129,8 +129,8 @@ static void Gui_Init(void) { Event_RegisterEntry(&TextureEvents.FileChanged, NULL, Gui_FileChanged); Gui_LoadOptions(); - Gui_Status = StatusScreen_MakeInstance(); - Gui_HUD = HUDScreen_MakeInstance(); + StatusScreen_Show(); + HUDScreen_Show(); Elem_Init(Gui_Status); Elem_Init(Gui_HUD); } diff --git a/src/Gui.h b/src/Gui.h index 2b5a1ec56..099e70921 100644 --- a/src/Gui.h +++ b/src/Gui.h @@ -109,6 +109,7 @@ enum GuiPriority { GUI_PRIORITY_LOADING = 17, }; +extern struct Screen* Gui_Status; extern struct Screen* Gui_HUD; extern struct Screen* Gui_Active; #define GUI_MAX_OVERLAYS 4 diff --git a/src/InputHandler.c b/src/InputHandler.c index ef97bffd3..614233dbd 100644 --- a/src/InputHandler.c +++ b/src/InputHandler.c @@ -208,7 +208,7 @@ static bool InputHandler_HandleCoreKey(Key key) { } } else if (key == KeyBinds[KEYBIND_INVENTORY] && active == Gui_HUD) { Gui_FreeActive(); - Gui_SetActive(InventoryScreen_MakeInstance()); + InventoryScreen_Show(); } else if (key == KEY_F5 && Game_ClassicMode) { int weather = Env.Weather == WEATHER_SUNNY ? WEATHER_RAINY : WEATHER_SUNNY; Env_SetWeather(weather); diff --git a/src/Screens.c b/src/Screens.c index d7bd7a550..fde297d6f 100644 --- a/src/Screens.c +++ b/src/Screens.c @@ -20,37 +20,6 @@ #include "Menus.h" #include "World.h" -struct InventoryScreen { - Screen_Layout - FontDesc font; - struct TableWidget table; - bool releasedInv, deferredSelect; -}; - -struct StatusScreen { - Screen_Layout - FontDesc font; - struct TextWidget line1, line2; - struct TextAtlas posAtlas; - double accumulator; - int frames, fps; - bool speed, halfSpeed, noclip, fly, canSpeed; - int lastFov; -}; - -struct LoadingScreen { - Screen_Layout - FontDesc font; - float progress; - - struct TextWidget title, message; - String titleStr, messageStr; - const char* lastState; - - char _titleBuffer[STRING_SIZE]; - char _messageBuffer[STRING_SIZE]; -}; - #define CHAT_MAX_STATUS Array_Elems(Chat_Status) #define CHAT_MAX_BOTTOMRIGHT Array_Elems(Chat_BottomRight) #define CHAT_MAX_CLIENTSTATUS Array_Elems(Chat_ClientStatus) @@ -80,19 +49,6 @@ struct HUDScreen { struct Texture chatTextures[TEXTGROUPWIDGET_MAX_LINES]; }; -struct DisconnectScreen { - Screen_Layout - TimeMS initTime; - bool canReconnect, lastActive; - int lastSecsLeft; - struct ButtonWidget reconnect; - - FontDesc titleFont, messageFont; - struct TextWidget title, message; - char _titleBuffer[STRING_SIZE]; - char _messageBuffer[STRING_SIZE]; - String titleStr, messageStr; -}; static bool Screen_Mouse(void* elem, int x, int y, MouseButton btn) { return false; } static bool Screen_MouseMove(void* elem, int x, int y) { return false; } @@ -101,7 +57,13 @@ static bool Screen_MouseMove(void* elem, int x, int y) { return false; } /*########################################################################################################################* *-----------------------------------------------------InventoryScreen-----------------------------------------------------* *#########################################################################################################################*/ -static struct InventoryScreen InventoryScreen_Instance; +static struct InventoryScreen { + Screen_Layout + FontDesc font; + struct TableWidget table; + bool releasedInv, deferredSelect; +} InventoryScreen_Instance; + static void InventoryScreen_OnBlockChanged(void* screen) { struct InventoryScreen* s = (struct InventoryScreen*)screen; TableWidget_OnInventoryChanged(&s->table); @@ -241,13 +203,13 @@ static struct ScreenVTABLE InventoryScreen_VTABLE = { InventoryScreen_MouseDown, InventoryScreen_MouseUp, InventoryScreen_MouseMove, InventoryScreen_MouseScroll, InventoryScreen_OnResize, InventoryScreen_ContextLost, InventoryScreen_ContextRecreated, }; -struct Screen* InventoryScreen_MakeInstance(void) { +void InventoryScreen_Show(void) { struct InventoryScreen* s = &InventoryScreen_Instance; s->handlesAllInput = true; s->closable = true; s->VTABLE = &InventoryScreen_VTABLE; - return (struct Screen*)s; + Gui_SetActive((struct Screen*)s); } struct Screen* InventoryScreen_UNSAFE_RawPointer = (struct Screen*)&InventoryScreen_Instance; @@ -255,7 +217,17 @@ struct Screen* InventoryScreen_UNSAFE_RawPointer = (struct Screen*)&InventoryScr /*########################################################################################################################* *-------------------------------------------------------StatusScreen------------------------------------------------------* *#########################################################################################################################*/ -static struct StatusScreen StatusScreen_Instance; +static struct StatusScreen { + Screen_Layout + FontDesc font; + struct TextWidget line1, line2; + struct TextAtlas posAtlas; + double accumulator; + int frames, fps; + bool speed, halfSpeed, noclip, fly, canSpeed; + int lastFov; +} StatusScreen_Instance; + static void StatusScreen_MakeText(struct StatusScreen* s, String* status) { int indices, ping; s->fps = (int)(s->frames / s->accumulator); @@ -441,19 +413,31 @@ static struct ScreenVTABLE StatusScreen_VTABLE = { Screen_Mouse, Screen_Mouse, Screen_MouseMove, StatusScreen_MouseScroll, StatusScreen_OnResize, StatusScreen_ContextLost, StatusScreen_ContextRecreated, }; -struct Screen* StatusScreen_MakeInstance(void) { +void StatusScreen_Show(void) { struct StatusScreen* s = &StatusScreen_Instance; s->handlesAllInput = false; - s->VTABLE = &StatusScreen_VTABLE; - return (struct Screen*)s; + s->VTABLE = &StatusScreen_VTABLE; + Gui_Status = (struct Screen*)s; } /*########################################################################################################################* *------------------------------------------------------LoadingScreen------------------------------------------------------* *#########################################################################################################################*/ -static struct LoadingScreen LoadingScreen_Instance; +static struct LoadingScreen { + Screen_Layout + FontDesc font; + float progress; + + struct TextWidget title, message; + String titleStr, messageStr; + const char* lastState; + + char _titleBuffer[STRING_SIZE]; + char _messageBuffer[STRING_SIZE]; +} LoadingScreen_Instance; + static void LoadingScreen_SetTitle(struct LoadingScreen* s) { Elem_TryFree(&s->title); TextWidget_Create(&s->title, &s->titleStr, &s->font); @@ -724,7 +708,7 @@ static void HUDScreen_UpdateAltTextY(struct HUDScreen* s) { } static String HUDScreen_GetChat(void* obj, int i) { - i += *((int*)obj); /* argument is offset into chat */ + i += HUDScreen_Instance.chatIndex; if (i >= 0 && i < Chat_Log.count) { return StringsBuffer_UNSAFE_Get(&Chat_Log, i); @@ -776,7 +760,6 @@ static void HUDScreen_ConstructWidgets(struct HUDScreen* s) { TextGroupWidget_Create(&s->chat, Gui_Chatlines, &s->chatFont, s->chatTextures, HUDScreen_GetChat); s->chat.underlineUrls = !Game_ClassicMode; - s->chat.getLineObj = &s->chatIndex; Widget_SetLocation(&s->chat, ANCHOR_MIN, ANCHOR_MAX, 10, yOffset); Elem_Init(&s->chat); @@ -1215,7 +1198,9 @@ static void HUDScreen_Init(void* screen) { Drawer2D_MakeFont(&s->playerFont, size, FONT_STYLE_NORMAL); s->wasShowingList = false; + HUDScreen_InitChatFonts(s); HotbarWidget_Create(&s->hotbar); + Screen_CommonInit(s); Event_RegisterChat(&ChatEvents.ChatReceived, s, HUDScreen_ChatReceived); Event_RegisterInt(&ChatEvents.ColCodeChanged, s, HUDScreen_ColCodeChanged); @@ -1274,14 +1259,14 @@ static struct ScreenVTABLE HUDScreen_VTABLE = { HUDScreen_MouseDown, Screen_Mouse, Screen_MouseMove, HUDScreen_MouseScroll, HUDScreen_OnResize, HUDScreen_ContextLost, HUDScreen_ContextRecreated, }; -struct Screen* HUDScreen_MakeInstance(void) { +void HUDScreen_Show(void) { struct HUDScreen* s = &HUDScreen_Instance; s->wasShowingList = false; s->inputOldHeight = -1; s->lastDownloadStatus = Int32_MinValue; s->VTABLE = &HUDScreen_VTABLE; - return (struct Screen*)s; + Gui_HUD = (struct Screen*)s; } void HUDScreen_OpenInput(const String* text) { @@ -1307,7 +1292,20 @@ struct Widget* HUDScreen_GetHotbar(void) { /*########################################################################################################################* *----------------------------------------------------DisconnectScreen-----------------------------------------------------* *#########################################################################################################################*/ -static struct DisconnectScreen DisconnectScreen_Instance; +static struct DisconnectScreen { + Screen_Layout + TimeMS initTime; + bool canReconnect, lastActive; + int lastSecsLeft; + struct ButtonWidget reconnect; + + FontDesc titleFont, messageFont; + struct TextWidget title, message; + char _titleBuffer[STRING_SIZE]; + char _messageBuffer[STRING_SIZE]; + String titleStr, messageStr; +} DisconnectScreen_Instance; + #define DISCONNECT_DELAY_MS 5000 static void DisconnectScreen_ReconnectMessage(struct DisconnectScreen* s, String* msg) { if (s->canReconnect) { @@ -1440,7 +1438,7 @@ static struct ScreenVTABLE DisconnectScreen_VTABLE = { DisconnectScreen_MouseDown, DisconnectScreen_MouseUp, DisconnectScreen_MouseMove, DisconnectScreen_MouseScroll, DisconnectScreen_OnResize, DisconnectScreen_ContextLost, DisconnectScreen_ContextRecreated }; -struct Screen* DisconnectScreen_MakeInstance(const String* title, const String* message) { +void DisconnectScreen_Show(const String* title, const String* message) { static const String kick = String_FromConst("Kicked "); static const String ban = String_FromConst("Banned "); String why; char whyBuffer[STRING_SIZE]; @@ -1459,6 +1457,6 @@ struct Screen* DisconnectScreen_MakeInstance(const String* title, const String* String_AppendColorless(&why, message); s->canReconnect = !(String_CaselessStarts(&why, &kick) || String_CaselessStarts(&why, &ban)); - s->VTABLE = &DisconnectScreen_VTABLE; - return (struct Screen*)s; + s->VTABLE = &DisconnectScreen_VTABLE; + Gui_SetActive((struct Screen*)s); } diff --git a/src/Screens.h b/src/Screens.h index b3333bf1d..511290df4 100644 --- a/src/Screens.h +++ b/src/Screens.h @@ -7,12 +7,12 @@ struct Screen; struct Widget; -struct Screen* InventoryScreen_MakeInstance(void); -struct Screen* StatusScreen_MakeInstance(void); +void InventoryScreen_Show(void); +void StatusScreen_Show(void); struct Screen* LoadingScreen_MakeInstance(const String* title, const String* message); struct Screen* GeneratingScreen_MakeInstance(void); -struct Screen* HUDScreen_MakeInstance(void); -struct Screen* DisconnectScreen_MakeInstance(const String* title, const String* message); +void HUDScreen_Show(void); +void DisconnectScreen_Show(const String* title, const String* message); /* Raw pointer to inventory screen. DO NOT USE THIS. Use InventoryScreen_MakeInstance() */ extern struct Screen* InventoryScreen_UNSAFE_RawPointer;