From 5c78093da4ae4045b25467a1ad6c7000595696b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20M=C3=BCller?= Date: Sat, 6 May 2023 14:48:01 +0200 Subject: [PATCH] Fade out broadcast in last second, use text container Fade the broadcast alpha to zero in the last second that the broadcast is shown. Use text container to support the fade out and to make broadcast rendering more efficient. --- src/game/client/components/broadcast.cpp | 26 ++++++++++++++++++++++-- src/game/client/components/broadcast.h | 5 +++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/game/client/components/broadcast.cpp b/src/game/client/components/broadcast.cpp index 0869ee3d7..61e2f6851 100644 --- a/src/game/client/components/broadcast.cpp +++ b/src/game/client/components/broadcast.cpp @@ -16,6 +16,12 @@ void CBroadcast::OnReset() { m_BroadcastTick = 0; + TextRender()->DeleteTextContainer(m_TextContainerIndex); +} + +void CBroadcast::OnWindowResize() +{ + TextRender()->DeleteTextContainer(m_TextContainerIndex); } void CBroadcast::OnRender() @@ -30,17 +36,32 @@ void CBroadcast::RenderServerBroadcast() { if(m_pClient->m_Scoreboard.Active() || m_pClient->m_Motd.IsActive() || !g_Config.m_ClShowBroadcasts) return; + const float SecondsRemaining = (m_BroadcastTick - Client()->GameTick(g_Config.m_ClDummy)) / (float)Client()->GameTickSpeed(); + if(SecondsRemaining <= 0.0f) + { + TextRender()->DeleteTextContainer(m_TextContainerIndex); + return; + } const float Height = 300.0f; const float Width = Height * Graphics()->ScreenAspect(); Graphics()->MapScreen(0.0f, 0.0f, Width, Height); - if(Client()->GameTick(g_Config.m_ClDummy) < m_BroadcastTick) + if(!m_TextContainerIndex.Valid()) { CTextCursor Cursor; TextRender()->SetCursor(&Cursor, m_BroadcastRenderOffset, 40.0f, 12.0f, TEXTFLAG_RENDER | TEXTFLAG_STOP_AT_END); Cursor.m_LineWidth = Width - m_BroadcastRenderOffset; - TextRender()->TextEx(&Cursor, m_aBroadcastText, -1); + TextRender()->CreateTextContainer(m_TextContainerIndex, &Cursor, m_aBroadcastText); + } + if(m_TextContainerIndex.Valid()) + { + const float Alpha = SecondsRemaining >= 1.0f ? 1.0f : SecondsRemaining; + ColorRGBA TextColor = TextRender()->DefaultTextColor(); + TextColor.a *= Alpha; + ColorRGBA OutlineColor = TextRender()->DefaultTextOutlineColor(); + OutlineColor.a *= Alpha; + TextRender()->RenderTextContainer(m_TextContainerIndex, TextColor, OutlineColor); } } @@ -61,6 +82,7 @@ void CBroadcast::OnBroadcastMessage(const CNetMsg_Sv_Broadcast *pMsg) str_copy(m_aBroadcastText, pMsg->m_pMessage); m_BroadcastRenderOffset = Width / 2.0f - TextRender()->TextWidth(12.0f, m_aBroadcastText, -1, Width, TEXTFLAG_STOP_AT_END) / 2.0f; m_BroadcastTick = Client()->GameTick(g_Config.m_ClDummy) + Client()->GameTickSpeed() * 10; + TextRender()->DeleteTextContainer(m_TextContainerIndex); if(g_Config.m_ClPrintBroadcasts) { diff --git a/src/game/client/components/broadcast.h b/src/game/client/components/broadcast.h index 371c72d6f..056adce91 100644 --- a/src/game/client/components/broadcast.h +++ b/src/game/client/components/broadcast.h @@ -2,6 +2,9 @@ /* If you are missing that file, acquire a complete release at teeworlds.com. */ #ifndef GAME_CLIENT_COMPONENTS_BROADCAST_H #define GAME_CLIENT_COMPONENTS_BROADCAST_H + +#include + #include class CBroadcast : public CComponent @@ -10,6 +13,7 @@ class CBroadcast : public CComponent char m_aBroadcastText[1024]; int m_BroadcastTick; float m_BroadcastRenderOffset; + STextContainerIndex m_TextContainerIndex; void RenderServerBroadcast(); void OnBroadcastMessage(const CNetMsg_Sv_Broadcast *pMsg); @@ -17,6 +21,7 @@ class CBroadcast : public CComponent public: virtual int Sizeof() const override { return sizeof(*this); } virtual void OnReset() override; + virtual void OnWindowResize() override; virtual void OnRender() override; virtual void OnMessage(int MsgType, void *pRawMsg) override; };