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 <base/math.h>
|
|
|
|
#include <engine/graphics.h>
|
2010-10-19 11:37:36 +00:00
|
|
|
#include <engine/demo.h>
|
2009-10-27 14:38:53 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
#include <game/generated/client_data.h>
|
|
|
|
#include <game/client/render.h>
|
|
|
|
#include <game/gamecore.h>
|
|
|
|
#include "particles.h"
|
2008-01-29 21:39:41 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
CParticles::CParticles()
|
2008-01-29 21:39:41 +00:00
|
|
|
{
|
2010-05-29 07:25:38 +00:00
|
|
|
OnReset();
|
|
|
|
m_RenderTrail.m_pParts = this;
|
|
|
|
m_RenderExplosions.m_pParts = this;
|
|
|
|
m_RenderGeneral.m_pParts = this;
|
2008-08-27 15:48:50 +00:00
|
|
|
}
|
2008-01-29 21:39:41 +00:00
|
|
|
|
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
void CParticles::OnReset()
|
2008-01-29 21:39:41 +00:00
|
|
|
{
|
|
|
|
// reset particles
|
|
|
|
for(int i = 0; i < MAX_PARTICLES; i++)
|
|
|
|
{
|
2010-05-29 07:25:38 +00:00
|
|
|
m_aParticles[i].m_PrevPart = i-1;
|
|
|
|
m_aParticles[i].m_NextPart = i+1;
|
2008-01-29 21:39:41 +00:00
|
|
|
}
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
m_aParticles[0].m_PrevPart = 0;
|
|
|
|
m_aParticles[MAX_PARTICLES-1].m_NextPart = -1;
|
|
|
|
m_FirstFree = 0;
|
2008-01-29 21:39:41 +00:00
|
|
|
|
2008-08-27 15:48:50 +00:00
|
|
|
for(int i = 0; i < NUM_GROUPS; i++)
|
2010-05-29 07:25:38 +00:00
|
|
|
m_aFirstPart[i] = -1;
|
2008-01-29 21:39:41 +00:00
|
|
|
}
|
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
void CParticles::Add(int Group, CParticle *pPart)
|
2008-01-29 21:39:41 +00:00
|
|
|
{
|
2010-11-16 23:38:20 +00:00
|
|
|
if(Client()->State() == IClient::STATE_DEMOPLAYBACK)
|
|
|
|
{
|
2011-04-13 18:37:12 +00:00
|
|
|
const IDemoPlayer::CInfo *pInfo = DemoPlayer()->BaseInfo();
|
2010-11-16 23:38:20 +00:00
|
|
|
if(pInfo->m_Paused)
|
|
|
|
return;
|
|
|
|
}
|
2012-01-09 23:49:31 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if(m_pClient->m_Snap.m_pGameInfoObj && m_pClient->m_Snap.m_pGameInfoObj->m_GameStateFlags&GAMESTATEFLAG_PAUSED)
|
|
|
|
return;
|
|
|
|
}
|
2010-11-16 23:38:20 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
if (m_FirstFree == -1)
|
2008-01-29 21:39:41 +00:00
|
|
|
return;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2008-01-29 21:39:41 +00:00
|
|
|
// remove from the free list
|
2010-05-29 07:25:38 +00:00
|
|
|
int Id = m_FirstFree;
|
|
|
|
m_FirstFree = m_aParticles[Id].m_NextPart;
|
2010-11-24 00:08:51 +00:00
|
|
|
if(m_FirstFree != -1)
|
|
|
|
m_aParticles[m_FirstFree].m_PrevPart = -1;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2008-01-29 21:39:41 +00:00
|
|
|
// copy data
|
2010-05-29 07:25:38 +00:00
|
|
|
m_aParticles[Id] = *pPart;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2008-01-29 21:39:41 +00:00
|
|
|
// insert to the group list
|
2010-05-29 07:25:38 +00:00
|
|
|
m_aParticles[Id].m_PrevPart = -1;
|
|
|
|
m_aParticles[Id].m_NextPart = m_aFirstPart[Group];
|
|
|
|
if(m_aFirstPart[Group] != -1)
|
|
|
|
m_aParticles[m_aFirstPart[Group]].m_PrevPart = Id;
|
|
|
|
m_aFirstPart[Group] = Id;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2008-01-29 21:39:41 +00:00
|
|
|
// set some parameters
|
2010-05-29 07:25:38 +00:00
|
|
|
m_aParticles[Id].m_Life = 0;
|
2008-01-29 21:39:41 +00:00
|
|
|
}
|
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
void CParticles::Update(float TimePassed)
|
2008-01-29 21:39:41 +00:00
|
|
|
{
|
2010-05-29 07:25:38 +00:00
|
|
|
static float FrictionFraction = 0;
|
|
|
|
FrictionFraction += TimePassed;
|
2008-01-29 21:39:41 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
if(FrictionFraction > 2.0f) // safty messure
|
|
|
|
FrictionFraction = 0;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
int FrictionCount = 0;
|
|
|
|
while(FrictionFraction > 0.05f)
|
2008-01-29 21:39:41 +00:00
|
|
|
{
|
2010-05-29 07:25:38 +00:00
|
|
|
FrictionCount++;
|
|
|
|
FrictionFraction -= 0.05f;
|
2008-01-29 21:39:41 +00:00
|
|
|
}
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2008-08-27 15:48:50 +00:00
|
|
|
for(int g = 0; g < NUM_GROUPS; g++)
|
2008-01-29 21:39:41 +00:00
|
|
|
{
|
2010-05-29 07:25:38 +00:00
|
|
|
int i = m_aFirstPart[g];
|
2008-01-29 21:39:41 +00:00
|
|
|
while(i != -1)
|
|
|
|
{
|
2010-05-29 07:25:38 +00:00
|
|
|
int Next = m_aParticles[i].m_NextPart;
|
|
|
|
//m_aParticles[i].vel += flow_get(m_aParticles[i].pos)*time_passed * m_aParticles[i].flow_affected;
|
|
|
|
m_aParticles[i].m_Vel.y += m_aParticles[i].m_Gravity*TimePassed;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
for(int f = 0; f < FrictionCount; f++) // apply friction
|
|
|
|
m_aParticles[i].m_Vel *= m_aParticles[i].m_Friction;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2008-01-29 21:39:41 +00:00
|
|
|
// move the point
|
2010-05-29 07:25:38 +00:00
|
|
|
vec2 Vel = m_aParticles[i].m_Vel*TimePassed;
|
|
|
|
Collision()->MovePoint(&m_aParticles[i].m_Pos, &Vel, 0.1f+0.9f*frandom(), NULL);
|
|
|
|
m_aParticles[i].m_Vel = Vel* (1.0f/TimePassed);
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
m_aParticles[i].m_Life += TimePassed;
|
|
|
|
m_aParticles[i].m_Rot += TimePassed * m_aParticles[i].m_Rotspeed;
|
2008-01-29 21:39:41 +00:00
|
|
|
|
|
|
|
// check particle death
|
2010-05-29 07:25:38 +00:00
|
|
|
if(m_aParticles[i].m_Life > m_aParticles[i].m_LifeSpan)
|
2008-01-29 21:39:41 +00:00
|
|
|
{
|
|
|
|
// remove it from the group list
|
2010-05-29 07:25:38 +00:00
|
|
|
if(m_aParticles[i].m_PrevPart != -1)
|
|
|
|
m_aParticles[m_aParticles[i].m_PrevPart].m_NextPart = m_aParticles[i].m_NextPart;
|
2008-01-29 21:39:41 +00:00
|
|
|
else
|
2010-05-29 07:25:38 +00:00
|
|
|
m_aFirstPart[g] = m_aParticles[i].m_NextPart;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
if(m_aParticles[i].m_NextPart != -1)
|
|
|
|
m_aParticles[m_aParticles[i].m_NextPart].m_PrevPart = m_aParticles[i].m_PrevPart;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2008-01-29 21:39:41 +00:00
|
|
|
// insert to the free list
|
2010-05-29 07:25:38 +00:00
|
|
|
if(m_FirstFree != -1)
|
|
|
|
m_aParticles[m_FirstFree].m_PrevPart = i;
|
|
|
|
m_aParticles[i].m_PrevPart = -1;
|
|
|
|
m_aParticles[i].m_NextPart = m_FirstFree;
|
|
|
|
m_FirstFree = i;
|
2008-01-29 21:39:41 +00:00
|
|
|
}
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
i = Next;
|
2008-01-29 21:39:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
void CParticles::OnRender()
|
2008-08-27 15:48:50 +00:00
|
|
|
{
|
2011-04-08 09:22:44 +00:00
|
|
|
if(Client()->State() < IClient::STATE_ONLINE)
|
|
|
|
return;
|
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
static int64 LastTime = 0;
|
2008-08-27 15:48:50 +00:00
|
|
|
int64 t = time_get();
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-10-19 11:37:36 +00:00
|
|
|
if(Client()->State() == IClient::STATE_DEMOPLAYBACK)
|
|
|
|
{
|
2011-04-13 18:37:12 +00:00
|
|
|
const IDemoPlayer::CInfo *pInfo = DemoPlayer()->BaseInfo();
|
2010-10-19 11:37:36 +00:00
|
|
|
if(!pInfo->m_Paused)
|
|
|
|
Update((float)((t-LastTime)/(double)time_freq())*pInfo->m_Speed);
|
|
|
|
}
|
|
|
|
else
|
2012-01-09 23:49:31 +00:00
|
|
|
{
|
|
|
|
if(m_pClient->m_Snap.m_pGameInfoObj && !(m_pClient->m_Snap.m_pGameInfoObj->m_GameStateFlags&GAMESTATEFLAG_PAUSED))
|
|
|
|
Update((float)((t-LastTime)/(double)time_freq()));
|
|
|
|
}
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
LastTime = t;
|
2008-08-27 15:48:50 +00:00
|
|
|
}
|
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
void CParticles::RenderGroup(int Group)
|
2008-01-29 21:39:41 +00:00
|
|
|
{
|
2009-10-27 14:38:53 +00:00
|
|
|
Graphics()->BlendNormal();
|
2008-01-29 21:39:41 +00:00
|
|
|
//gfx_blend_additive();
|
2010-05-29 07:25:38 +00:00
|
|
|
Graphics()->TextureSet(g_pData->m_aImages[IMAGE_PARTICLES].m_Id);
|
2009-10-27 14:38:53 +00:00
|
|
|
Graphics()->QuadsBegin();
|
2008-01-29 21:39:41 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
int i = m_aFirstPart[Group];
|
2008-01-29 21:39:41 +00:00
|
|
|
while(i != -1)
|
|
|
|
{
|
2010-05-29 07:25:38 +00:00
|
|
|
RenderTools()->SelectSprite(m_aParticles[i].m_Spr);
|
|
|
|
float a = m_aParticles[i].m_Life / m_aParticles[i].m_LifeSpan;
|
|
|
|
vec2 p = m_aParticles[i].m_Pos;
|
|
|
|
float Size = mix(m_aParticles[i].m_StartSize, m_aParticles[i].m_EndSize, a);
|
2008-01-29 21:39:41 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
Graphics()->QuadsSetRotation(m_aParticles[i].m_Rot);
|
2008-01-29 21:39:41 +00:00
|
|
|
|
2009-10-27 14:38:53 +00:00
|
|
|
Graphics()->SetColor(
|
2010-05-29 07:25:38 +00:00
|
|
|
m_aParticles[i].m_Color.r,
|
|
|
|
m_aParticles[i].m_Color.g,
|
|
|
|
m_aParticles[i].m_Color.b,
|
2011-04-13 18:37:12 +00:00
|
|
|
m_aParticles[i].m_Color.a); // pow(a, 0.75f) *
|
2008-01-29 21:39:41 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
IGraphics::CQuadItem QuadItem(p.x, p.y, Size, Size);
|
|
|
|
Graphics()->QuadsDraw(&QuadItem, 1);
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
i = m_aParticles[i].m_NextPart;
|
2008-01-29 21:39:41 +00:00
|
|
|
}
|
2009-10-27 14:38:53 +00:00
|
|
|
Graphics()->QuadsEnd();
|
|
|
|
Graphics()->BlendNormal();
|
2008-01-29 21:39:41 +00:00
|
|
|
}
|