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. */
|
2011-08-31 11:56:04 +00:00
|
|
|
|
|
|
|
#include <base/tl/string.h>
|
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
#include <engine/shared/config.h>
|
2008-08-27 15:48:50 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
#include <base/math.h>
|
|
|
|
#include <game/collision.h>
|
|
|
|
#include <game/client/gameclient.h>
|
|
|
|
#include <game/client/component.h>
|
2008-08-27 15:48:50 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
#include "camera.h"
|
|
|
|
#include "controls.h"
|
2008-08-27 15:48:50 +00:00
|
|
|
|
2011-04-09 06:41:31 +00:00
|
|
|
#include <engine/serverbrowser.h>
|
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
CCamera::CCamera()
|
2008-08-27 15:48:50 +00:00
|
|
|
{
|
2011-03-12 17:07:57 +00:00
|
|
|
m_CamType = CAMTYPE_UNDEFINED;
|
2015-01-10 00:10:51 +00:00
|
|
|
m_ZoomSet = false;
|
2019-07-08 21:08:42 +00:00
|
|
|
m_Zoom = 1.0f;
|
2020-06-30 14:25:54 +00:00
|
|
|
m_StartZoom = m_Zoom;
|
|
|
|
m_TargetZoom = m_Zoom;
|
|
|
|
m_ZoomAnimStartTick = 0;
|
|
|
|
m_ZoomAnimEndTick = 0;
|
2008-08-27 15:48:50 +00:00
|
|
|
}
|
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
void CCamera::OnRender()
|
2008-08-27 15:48:50 +00:00
|
|
|
{
|
2020-06-30 14:25:54 +00:00
|
|
|
if(Client()->GameTick(g_Config.m_ClDummy) < m_ZoomAnimEndTick && m_ZoomAnimStartTick < m_ZoomAnimEndTick)
|
|
|
|
{
|
|
|
|
int SmoothTick;
|
|
|
|
Client()->GetSmoothTick(&SmoothTick, NULL, 0);
|
|
|
|
m_Zoom = mix(m_StartZoom, m_TargetZoom, (SmoothTick - m_ZoomAnimStartTick) / (m_ZoomAnimEndTick - m_ZoomAnimStartTick));
|
|
|
|
if(m_TargetZoom < m_StartZoom)
|
|
|
|
m_Zoom = clamp(m_Zoom, m_TargetZoom, m_StartZoom);
|
|
|
|
else
|
|
|
|
m_Zoom = clamp(m_Zoom, m_StartZoom, m_TargetZoom);
|
|
|
|
}
|
|
|
|
|
2019-06-03 19:52:14 +00:00
|
|
|
if(!(m_pClient->m_Snap.m_SpecInfo.m_Active || GameClient()->m_GameInfo.m_AllowZoom || Client()->State() == IClient::STATE_DEMOPLAYBACK))
|
2015-01-03 02:13:21 +00:00
|
|
|
{
|
2019-06-15 10:23:50 +00:00
|
|
|
m_ZoomSet = false;
|
2019-07-08 21:08:42 +00:00
|
|
|
m_Zoom = 1.0f;
|
2020-06-30 14:25:54 +00:00
|
|
|
m_StartZoom = m_Zoom;
|
|
|
|
m_TargetZoom = m_Zoom;
|
|
|
|
m_ZoomAnimEndTick = 0;
|
2015-01-03 02:13:21 +00:00
|
|
|
}
|
2015-01-10 00:10:51 +00:00
|
|
|
else if(!m_ZoomSet && g_Config.m_ClDefaultZoom != 10)
|
2015-01-03 02:13:21 +00:00
|
|
|
{
|
2015-01-10 00:10:51 +00:00
|
|
|
m_ZoomSet = true;
|
2015-01-03 02:13:21 +00:00
|
|
|
OnReset();
|
|
|
|
}
|
2010-08-25 14:15:59 +00:00
|
|
|
|
2011-04-13 18:37:12 +00:00
|
|
|
// update camera center
|
2011-03-12 17:07:57 +00:00
|
|
|
if(m_pClient->m_Snap.m_SpecInfo.m_Active && !m_pClient->m_Snap.m_SpecInfo.m_UsePosition)
|
2010-09-19 14:00:46 +00:00
|
|
|
{
|
2011-03-12 17:07:57 +00:00
|
|
|
if(m_CamType != CAMTYPE_SPEC)
|
2010-09-19 14:12:18 +00:00
|
|
|
{
|
2014-05-23 17:28:26 +00:00
|
|
|
m_LastPos[g_Config.m_ClDummy] = m_pClient->m_pControls->m_MousePos[g_Config.m_ClDummy];
|
2014-05-10 12:31:00 +00:00
|
|
|
m_pClient->m_pControls->m_MousePos[g_Config.m_ClDummy] = m_PrevCenter;
|
2010-09-19 14:12:18 +00:00
|
|
|
m_pClient->m_pControls->ClampMousePos();
|
2011-03-12 17:07:57 +00:00
|
|
|
m_CamType = CAMTYPE_SPEC;
|
2010-09-19 14:12:18 +00:00
|
|
|
}
|
2014-05-10 12:31:00 +00:00
|
|
|
m_Center = m_pClient->m_pControls->m_MousePos[g_Config.m_ClDummy];
|
2010-09-19 14:00:46 +00:00
|
|
|
}
|
2008-08-27 15:48:50 +00:00
|
|
|
else
|
|
|
|
{
|
2011-03-12 17:07:57 +00:00
|
|
|
if(m_CamType != CAMTYPE_PLAYER)
|
2010-09-19 14:00:46 +00:00
|
|
|
{
|
2019-07-16 10:56:43 +00:00
|
|
|
if((m_LastPos[g_Config.m_ClDummy].x < g_Config.m_ClMouseMinDistance) || (m_LastPos[g_Config.m_ClDummy].x < g_Config.m_ClDyncamMinDistance))
|
|
|
|
m_pClient->m_pControls->m_MousePos[g_Config.m_ClDummy].x = m_LastPos[g_Config.m_ClDummy].x + g_Config.m_ClMouseMinDistance + g_Config.m_ClDyncamMinDistance;
|
2019-04-13 12:33:25 +00:00
|
|
|
else
|
|
|
|
m_pClient->m_pControls->m_MousePos[g_Config.m_ClDummy] = m_LastPos[g_Config.m_ClDummy];
|
2010-09-19 14:00:46 +00:00
|
|
|
m_pClient->m_pControls->ClampMousePos();
|
2011-03-12 17:07:57 +00:00
|
|
|
m_CamType = CAMTYPE_PLAYER;
|
2010-09-19 14:00:46 +00:00
|
|
|
}
|
2011-03-10 16:57:15 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
vec2 CameraOffset(0, 0);
|
2010-09-27 19:41:41 +00:00
|
|
|
|
2014-05-10 12:31:00 +00:00
|
|
|
float l = length(m_pClient->m_pControls->m_MousePos[g_Config.m_ClDummy]);
|
2008-08-27 15:48:50 +00:00
|
|
|
if(l > 0.0001f) // make sure that this isn't 0
|
2011-03-10 16:57:15 +00:00
|
|
|
{
|
2015-08-27 18:26:05 +00:00
|
|
|
float DeadZone = g_Config.m_ClDyncam ? g_Config.m_ClDyncamDeadzone : g_Config.m_ClMouseDeadzone;
|
|
|
|
float FollowFactor = (g_Config.m_ClDyncam ? g_Config.m_ClDyncamFollowFactor : g_Config.m_ClMouseFollowfactor) / 100.0f;
|
2019-04-26 19:36:49 +00:00
|
|
|
float OffsetAmount = maximum(l-DeadZone, 0.0f) * FollowFactor;
|
2011-03-10 16:57:15 +00:00
|
|
|
|
2014-05-10 12:31:00 +00:00
|
|
|
CameraOffset = normalize(m_pClient->m_pControls->m_MousePos[g_Config.m_ClDummy])*OffsetAmount;
|
2011-03-10 16:57:15 +00:00
|
|
|
}
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2011-03-12 17:07:57 +00:00
|
|
|
if(m_pClient->m_Snap.m_SpecInfo.m_Active)
|
|
|
|
m_Center = m_pClient->m_Snap.m_SpecInfo.m_Position + CameraOffset;
|
2011-03-10 16:57:15 +00:00
|
|
|
else
|
|
|
|
m_Center = m_pClient->m_LocalCharacterPos + CameraOffset;
|
2008-08-27 15:48:50 +00:00
|
|
|
}
|
2011-03-12 17:07:57 +00:00
|
|
|
|
|
|
|
m_PrevCenter = m_Center;
|
2008-08-27 15:48:50 +00:00
|
|
|
}
|
2010-10-17 08:43:27 +00:00
|
|
|
|
|
|
|
void CCamera::OnConsoleInit()
|
|
|
|
{
|
2011-08-13 00:11:06 +00:00
|
|
|
Console()->Register("zoom+", "", CFGFLAG_CLIENT, ConZoomPlus, this, "Zoom increase");
|
|
|
|
Console()->Register("zoom-", "", CFGFLAG_CLIENT, ConZoomMinus, this, "Zoom decrease");
|
|
|
|
Console()->Register("zoom", "", CFGFLAG_CLIENT, ConZoomReset, this, "Zoom reset");
|
2010-10-17 08:43:27 +00:00
|
|
|
}
|
2011-01-06 03:46:10 +00:00
|
|
|
|
2015-01-03 02:13:21 +00:00
|
|
|
const float ZoomStep = 0.866025f;
|
|
|
|
|
2011-12-20 20:45:07 +00:00
|
|
|
void CCamera::OnReset()
|
|
|
|
{
|
|
|
|
m_Zoom = 1.0f;
|
2015-01-03 02:13:21 +00:00
|
|
|
|
2019-10-27 12:04:08 +00:00
|
|
|
if(g_Config.m_ClDefaultZoom < 10)
|
2015-01-03 02:13:21 +00:00
|
|
|
{
|
2019-10-27 12:04:08 +00:00
|
|
|
m_Zoom = pow(1/ZoomStep, 10 - g_Config.m_ClDefaultZoom);
|
2015-01-03 02:13:21 +00:00
|
|
|
}
|
2019-10-27 12:04:08 +00:00
|
|
|
else if(g_Config.m_ClDefaultZoom > 10)
|
2015-01-03 02:13:21 +00:00
|
|
|
{
|
2019-10-27 12:04:08 +00:00
|
|
|
m_Zoom = pow(ZoomStep, g_Config.m_ClDefaultZoom - 10);
|
2015-01-03 02:13:21 +00:00
|
|
|
}
|
2019-10-27 12:04:08 +00:00
|
|
|
|
2020-06-30 14:25:54 +00:00
|
|
|
m_StartZoom = m_Zoom;
|
|
|
|
m_TargetZoom = m_Zoom;
|
|
|
|
m_ZoomAnimEndTick = 0;
|
2011-12-20 20:45:07 +00:00
|
|
|
}
|
|
|
|
|
2011-08-13 00:11:06 +00:00
|
|
|
void CCamera::ConZoomPlus(IConsole::IResult *pResult, void *pUserData)
|
2011-02-16 09:20:27 +00:00
|
|
|
{
|
|
|
|
CCamera *pSelf = (CCamera *)pUserData;
|
2019-06-03 19:52:14 +00:00
|
|
|
if(pSelf->m_pClient->m_Snap.m_SpecInfo.m_Active || pSelf->GameClient()->m_GameInfo.m_AllowZoom || pSelf->Client()->State() == IClient::STATE_DEMOPLAYBACK)
|
2020-06-30 14:25:54 +00:00
|
|
|
{
|
|
|
|
if(g_Config.m_ClSmoothZoom)
|
|
|
|
pSelf->StartSmoothZoom(ZoomStep);
|
|
|
|
else
|
|
|
|
pSelf->m_Zoom *= ZoomStep;
|
|
|
|
}
|
2011-01-06 03:46:10 +00:00
|
|
|
}
|
2011-08-13 00:11:06 +00:00
|
|
|
void CCamera::ConZoomMinus(IConsole::IResult *pResult, void *pUserData)
|
2011-02-16 09:20:27 +00:00
|
|
|
{
|
|
|
|
CCamera *pSelf = (CCamera *)pUserData;
|
2019-06-03 19:52:14 +00:00
|
|
|
if(pSelf->m_pClient->m_Snap.m_SpecInfo.m_Active || pSelf->GameClient()->m_GameInfo.m_AllowZoom || pSelf->Client()->State() == IClient::STATE_DEMOPLAYBACK)
|
2017-09-13 18:33:58 +00:00
|
|
|
{
|
2020-06-30 14:25:54 +00:00
|
|
|
if(pSelf->m_Zoom < 500.0f/ZoomStep)
|
2017-09-13 18:33:58 +00:00
|
|
|
{
|
2020-06-30 14:25:54 +00:00
|
|
|
if(g_Config.m_ClSmoothZoom)
|
|
|
|
pSelf->StartSmoothZoom(1/ZoomStep);
|
|
|
|
else
|
|
|
|
pSelf->m_Zoom *= 1/ZoomStep;
|
2017-09-12 18:14:34 +00:00
|
|
|
}
|
|
|
|
}
|
2011-02-16 09:20:27 +00:00
|
|
|
}
|
2011-08-13 00:11:06 +00:00
|
|
|
void CCamera::ConZoomReset(IConsole::IResult *pResult, void *pUserData)
|
2011-02-16 09:20:27 +00:00
|
|
|
{
|
2015-01-03 02:13:21 +00:00
|
|
|
((CCamera *)pUserData)->OnReset();
|
2011-01-06 03:46:10 +00:00
|
|
|
}
|
2020-06-30 14:25:54 +00:00
|
|
|
|
|
|
|
void CCamera::StartSmoothZoom(float ZoomStep)
|
|
|
|
{
|
|
|
|
m_StartZoom = m_Zoom;
|
|
|
|
m_TargetZoom = m_StartZoom * ZoomStep;
|
|
|
|
m_ZoomAnimStartTick = Client()->GameTick(g_Config.m_ClDummy);
|
|
|
|
m_ZoomAnimEndTick = m_ZoomAnimStartTick + g_Config.m_ClSmoothZoomLength / 1000.f * Client()->GameTickSpeed();
|
2020-06-30 14:30:40 +00:00
|
|
|
}
|