From 18ead3861d0e0fb73463c46c193d398c3f7b2af9 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Fri, 10 Jun 2022 23:10:07 +1000 Subject: [PATCH] Chat_LogTimes doesn't need to be a dynamic array, only needs to be a fixed size array of around GUI_MAX_CHATLINES in length --- src/Chat.c | 23 +++-------------------- src/Chat.h | 26 +++++++++++++++----------- src/Constants.h | 2 ++ src/Gui.c | 2 +- src/Screens.c | 6 +++--- src/Widgets.c | 8 ++++---- src/Widgets.h | 3 +-- 7 files changed, 29 insertions(+), 41 deletions(-) diff --git a/src/Chat.c b/src/Chat.c index 00c532669..3270a6774 100644 --- a/src/Chat.c +++ b/src/Chat.c @@ -45,27 +45,10 @@ cc_bool Chat_Logging; /*########################################################################################################################* *-------------------------------------------------------Chat logging------------------------------------------------------* *#########################################################################################################################*/ -#define CHAT_LOGTIMES_DEF_ELEMS 256 -static double defaultLogTimes[CHAT_LOGTIMES_DEF_ELEMS]; -static int logTimesCapacity = CHAT_LOGTIMES_DEF_ELEMS, logTimesCount; -double* Chat_LogTime = defaultLogTimes; - -static void AppendChatLogTime(void) { - double now = Game.Time; - - if (logTimesCount == logTimesCapacity) { - Utils_Resize((void**)&Chat_LogTime, &logTimesCapacity, - sizeof(double), CHAT_LOGTIMES_DEF_ELEMS, 512); - } - Chat_LogTime[logTimesCount++] = now; -} +double Chat_RecentLogTimes[CHATLOG_TIME_MASK + 1]; static void ClearChatLogs(void) { - if (Chat_LogTime != defaultLogTimes) Mem_Free(Chat_LogTime); - Chat_LogTime = defaultLogTimes; - logTimesCount = 0; - logTimesCapacity = CHAT_LOGTIMES_DEF_ELEMS; - + Mem_Set(Chat_RecentLogTimes, 0, sizeof(Chat_RecentLogTimes)); StringsBuffer_Clear(&Chat_Log); } @@ -236,8 +219,8 @@ void Chat_AddOf(const cc_string* text, int msgType) { Chat_AddRaw("&cChat log cleared as it hit 8.3 million character limit"); } + Chat_GetLogTime(Chat_Log.count) = Game.Time; StringsBuffer_Add(&Chat_Log, text); - AppendChatLogTime(); AppendChatLog(text); } else if (msgType >= MSG_TYPE_STATUS_1 && msgType <= MSG_TYPE_STATUS_3) { /* Status[0] is for texture pack downloading message */ diff --git a/src/Chat.h b/src/Chat.h index 1a74692e8..e336ebecc 100644 --- a/src/Chat.h +++ b/src/Chat.h @@ -25,22 +25,26 @@ enum MsgType { MSG_TYPE_EXTRASTATUS_2 = 361 }; -extern cc_string Chat_Status[5], Chat_BottomRight[3], Chat_ClientStatus[2]; -extern cc_string Chat_Announcement, Chat_BigAnnouncement, Chat_SmallAnnouncement; -/* All chat messages received. */ -extern struct StringsBuffer Chat_Log; -/* Time each chat message was received at. */ -extern double* Chat_LogTime; -/* All chat entered by the user. */ -extern struct StringsBuffer Chat_InputLog; -/* Whether chat messages are logged to disc. */ -extern cc_bool Chat_Logging; +/* NOTE: this must be: (next power of next larger than GUI_MAX_CHATLINES) - 1 */ +#define CHATLOG_TIME_MASK 31 +/* Time most recent chat message were received at */ +extern double Chat_RecentLogTimes[CHATLOG_TIME_MASK + 1]; +#define Chat_GetLogTime(i) Chat_RecentLogTimes[(i) & CHATLOG_TIME_MASK] -/* Times at which last announcement messages were received. */ +/* Times at which last announcement messages were received */ extern double Chat_AnnouncementReceived; extern double Chat_BigAnnouncementReceived; extern double Chat_SmallAnnouncementReceived; +extern cc_string Chat_Status[5], Chat_BottomRight[3], Chat_ClientStatus[2]; +extern cc_string Chat_Announcement, Chat_BigAnnouncement, Chat_SmallAnnouncement; +/* All chat messages received */ +extern struct StringsBuffer Chat_Log; +/* All chat entered by the user */ +extern struct StringsBuffer Chat_InputLog; +/* Whether chat messages are logged to disc */ +extern cc_bool Chat_Logging; + /* This command is only available in singleplayer */ #define COMMAND_FLAG_SINGLEPLAYER_ONLY 0x01 /* args is passed as a single string instead of being split by spaces */ diff --git a/src/Constants.h b/src/Constants.h index a162230d0..d3092d0a6 100644 --- a/src/Constants.h +++ b/src/Constants.h @@ -48,6 +48,8 @@ #define GAME_DEF_TICKS (1.0 / 20) #define GAME_NET_TICKS (1.0 / 60) +#define GUI_MAX_CHATLINES 30 + enum FACE_CONSTS { FACE_XMIN = 0, /* Face X = 0 */ FACE_XMAX = 1, /* Face X = 1 */ diff --git a/src/Gui.c b/src/Gui.c index 13c4fdbfb..1d788c2d3 100644 --- a/src/Gui.c +++ b/src/Gui.c @@ -90,7 +90,7 @@ void Gui_ShowDefault(void) { static void LoadOptions(void) { Gui.DefaultLines = Game_ClassicMode ? 10 : 12; - Gui.Chatlines = Options_GetInt(OPT_CHATLINES, 0, 30, Gui.DefaultLines); + Gui.Chatlines = Options_GetInt(OPT_CHATLINES, 0, GUI_MAX_CHATLINES, Gui.DefaultLines); Gui.ClickableChat = !Game_ClassicMode && Options_GetBool(OPT_CLICKABLE_CHAT, !Input_TouchMode); Gui.TabAutocomplete = !Game_ClassicMode && Options_GetBool(OPT_TAB_AUTOCOMPLETE, true); diff --git a/src/Screens.c b/src/Screens.c index caf67f371..1a6ee3812 100644 --- a/src/Screens.c +++ b/src/Screens.c @@ -766,7 +766,7 @@ static struct ChatScreen { struct Texture statusTextures[CHAT_MAX_STATUS]; struct Texture bottomRightTextures[CHAT_MAX_BOTTOMRIGHT]; struct Texture clientStatusTextures[CHAT_MAX_CLIENTSTATUS]; - struct Texture chatTextures[TEXTGROUPWIDGET_MAX_LINES]; + struct Texture chatTextures[GUI_MAX_CHATLINES]; } ChatScreen_Instance; #define CH_EXTENT 16 @@ -1012,7 +1012,7 @@ static void ChatScreen_DrawChat(struct ChatScreen* s, double delta) { if (!tex.ID) continue; if (logIdx < 0 || logIdx >= Chat_Log.count) continue; - if (Chat_LogTime[logIdx] + 10 >= now) Texture_Render(&tex); + if (Chat_GetLogTime(logIdx) + 10 >= now) Texture_Render(&tex); } } @@ -1245,7 +1245,7 @@ static int ChatScreen_PointerDown(void* screen, int id, int x, int y) { i = TextGroupWidget_GetSelected(&s->chat, &text, x, y); if (!Utils_IsUrlPrefix(&text)) return false; - if (Chat_LogTime[s->chatIndex + i] + 10 < Game.Time) return false; + if (Chat_GetLogTime(s->chatIndex + i) + 10 < Game.Time) return false; UrlWarningOverlay_Show(&text); return TOUCH_TYPE_GUI; } diff --git a/src/Widgets.c b/src/Widgets.c index 9b6f12447..aa3157f59 100644 --- a/src/Widgets.c +++ b/src/Widgets.c @@ -2060,8 +2060,8 @@ static void TextGroupWidget_Output(struct Portion bit, int lineBeg, int lineEnd, static int TextGroupWidget_Reduce(struct TextGroupWidget* w, char* chars, int target, struct Portion* portions) { struct Portion* start = portions; - int begs[TEXTGROUPWIDGET_MAX_LINES]; - int ends[TEXTGROUPWIDGET_MAX_LINES]; + int begs[GUI_MAX_CHATLINES]; + int ends[GUI_MAX_CHATLINES]; struct Portion bit; cc_string line; int nextStart, i, total = 0, end; @@ -2112,7 +2112,7 @@ static void TextGroupWidget_FormatUrl(cc_string* text, const cc_string* url) { } static cc_bool TextGroupWidget_GetUrl(struct TextGroupWidget* w, cc_string* text, int index, int mouseX) { - char chars[TEXTGROUPWIDGET_MAX_LINES * TEXTGROUPWIDGET_LEN]; + char chars[GUI_MAX_CHATLINES * TEXTGROUPWIDGET_LEN]; struct Portion portions[2 * (TEXTGROUPWIDGET_LEN / TEXTGROUPWIDGET_HTTP_LEN)]; struct Portion bit; struct DrawTextArgs args = { 0 }; @@ -2176,7 +2176,7 @@ static cc_bool TextGroupWidget_MightHaveUrls(struct TextGroupWidget* w) { } static void TextGroupWidget_DrawAdvanced(struct TextGroupWidget* w, struct Texture* tex, struct DrawTextArgs* args, int index, const cc_string* text) { - char chars[TEXTGROUPWIDGET_MAX_LINES * TEXTGROUPWIDGET_LEN]; + char chars[GUI_MAX_CHATLINES * TEXTGROUPWIDGET_LEN]; struct Portion portions[2 * (TEXTGROUPWIDGET_LEN / TEXTGROUPWIDGET_HTTP_LEN)]; struct Portion bit; int width, height; diff --git a/src/Widgets.h b/src/Widgets.h index 5987af05a..bebb801fd 100644 --- a/src/Widgets.h +++ b/src/Widgets.h @@ -229,7 +229,6 @@ CC_NOINLINE void ChatInputWidget_SetFont(struct ChatInputWidget* w, struct FontD /* Retrieves the text for the i'th line in the group */ typedef cc_string (*TextGroupWidget_Get)(int i); -#define TEXTGROUPWIDGET_MAX_LINES 30 #define TEXTGROUPWIDGET_LEN (STRING_SIZE + (STRING_SIZE / 2)) /* A group of text labels. */ @@ -238,7 +237,7 @@ struct TextGroupWidget { int lines, defaultHeight; struct FontDesc* font; /* Whether a line has zero height when that line has no text in it. */ - cc_bool collapsible[TEXTGROUPWIDGET_MAX_LINES]; + cc_bool collapsible[GUI_MAX_CHATLINES]; cc_bool underlineUrls; struct Texture* textures; TextGroupWidget_Get GetLine;