ddnet/src/game/client/components/broadcast.cpp

100 lines
3.1 KiB
C++
Raw Normal View History

2010-11-20 10:37:14 +00:00
/* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */
/* If you are missing that file, acquire a complete release at teeworlds.com. */
2010-05-29 07:25:38 +00:00
#include <engine/graphics.h>
#include <engine/shared/config.h>
2010-05-29 07:25:38 +00:00
#include <engine/textrender.h>
2022-05-29 16:33:38 +00:00
#include <game/generated/protocol.h>
2010-05-29 07:25:38 +00:00
#include <game/client/gameclient.h>
#include <game/client/components/motd.h>
#include <game/client/components/scoreboard.h>
2010-05-29 07:25:38 +00:00
#include "broadcast.h"
2010-05-29 07:25:38 +00:00
void CBroadcast::OnReset()
{
m_BroadcastTick = 0;
TextRender()->DeleteTextContainer(m_TextContainerIndex);
}
void CBroadcast::OnWindowResize()
{
TextRender()->DeleteTextContainer(m_TextContainerIndex);
}
2010-05-29 07:25:38 +00:00
void CBroadcast::OnRender()
{
if(Client()->State() != IClient::STATE_ONLINE && Client()->State() != IClient::STATE_DEMOPLAYBACK)
return;
RenderServerBroadcast();
}
void CBroadcast::RenderServerBroadcast()
{
2021-07-12 09:43:56 +00:00
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(!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()->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);
}
}
2010-05-29 07:25:38 +00:00
void CBroadcast::OnMessage(int MsgType, void *pRawMsg)
{
2010-05-29 07:25:38 +00:00
if(MsgType == NETMSGTYPE_SV_BROADCAST)
{
OnBroadcastMessage((CNetMsg_Sv_Broadcast *)pRawMsg);
}
}
void CBroadcast::OnBroadcastMessage(const CNetMsg_Sv_Broadcast *pMsg)
{
const float Height = 300.0f;
const float Width = Height * Graphics()->ScreenAspect();
Graphics()->MapScreen(0.0f, 0.0f, Width, Height);
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)
{
const char *pText = m_aBroadcastText;
char aLine[sizeof(m_aBroadcastText)];
while((pText = str_next_token(pText, "\n", aLine, sizeof(aLine))))
2014-09-23 17:06:25 +00:00
{
if(aLine[0] != '\0')
2014-09-23 17:06:25 +00:00
{
m_pClient->Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "broadcast", aLine, color_cast<ColorRGBA>(ColorHSLA(g_Config.m_ClMessageHighlightColor)));
2014-09-23 17:06:25 +00:00
}
}
}
}