BVB Source Codes

CRYENGINE Show ParticleEffect.cpp Source code

Return Download CRYENGINE: download ParticleEffect.cpp Source code - Download CRYENGINE Source code - Type:.cpp
  1. // Copyright 2001-2016 Crytek GmbH / Crytek Group. All rights reserved.
  2.  
  3. // -------------------------------------------------------------------------
  4. //  Created:     23/09/2014 by Filipe amim
  5. //  Description:
  6. // -------------------------------------------------------------------------
  7. //
  8. ////////////////////////////////////////////////////////////////////////////
  9.  
  10. #include "StdAfx.h"
  11. #include <CrySerialization/STL.h>
  12. #include <CrySerialization/IArchive.h>
  13. #include <CrySerialization/SmartPtr.h>
  14. #include <CryParticleSystem/ParticleParams.h>
  15. #include "ParticleEffect.h"
  16. #include "ParticleEmitter.h"
  17. #include "ParticleFeature.h"
  18.  
  19. CRY_PFX2_DBG
  20.  
  21. namespace pfx2
  22. {
  23.  
  24. //////////////////////////////////////////////////////////////////////////
  25. // CParticleEffect
  26.  
  27. CParticleEffect::CParticleEffect()
  28.         : m_editVersion(0)
  29.         , m_dirty(true)
  30.         , m_numRenderObjects(0)
  31. {
  32. }
  33.  
  34. cstr CParticleEffect::GetName() const
  35. {
  36.         return m_name.empty() ? nullptr : m_name.c_str();
  37. }
  38.  
  39. void CParticleEffect::Compile()
  40. {
  41.         FUNCTION_PROFILER(GetISystem(), PROFILE_PARTICLE);
  42.  
  43.         if (!m_dirty)
  44.                 return;
  45.  
  46.         m_numRenderObjects = 0;
  47.         m_attributeInstance.Reset(&m_attributes, EAttributeScope::PerEffect);
  48.         for (size_t i = 0; i < m_components.size(); ++i)
  49.         {
  50.                 m_components[i]->m_pEffect = this;
  51.                 m_components[i]->m_componentId = i;
  52.                 m_components[i]->SetChanged();
  53.                 m_components[i]->m_componentParams.Reset();
  54.                 m_components[i]->PreCompile();
  55.         }
  56.         for (auto& component : m_components)
  57.                 component->ResolveDependencies();
  58.         for (auto& component : m_components)
  59.                 component->Compile();
  60.         for (auto& component : m_components)
  61.                 component->FinalizeCompile();
  62.  
  63.         m_dirty = false;
  64. }
  65.  
  66. TComponentId CParticleEffect::FindComponentIdByName(const char* name) const
  67. {
  68.         const auto it = std::find_if(m_components.begin(), m_components.end(), [name](TComponentPtr pComponent)
  69.         {
  70.                 if (!pComponent)
  71.                         return false;
  72.                 return strcmp(pComponent->GetName(), name) == 0;
  73.         });
  74.         if (it == m_components.end())
  75.                 return gInvalidId;
  76.         return TComponentId(it - m_components.begin());
  77. }
  78.  
  79. string CParticleEffect::MakeUniqueName(TComponentId forComponentId, const char* name)
  80. {
  81.         TComponentId foundId = FindComponentIdByName(name);
  82.         if (foundId == forComponentId || foundId == gInvalidId)
  83.                 return string(name);
  84.  
  85.         CryStackStringT<char, 256> newName(name);
  86.         const uint sz = strlen(name);
  87.         if (isdigit(name[sz - 2]) && isdigit(name[sz - 1]))
  88.         {
  89.                 const uint newIdent = (name[sz - 2] - '0') * 10 + (name[sz - 1] - '0') + 1;
  90.                 newName.replace(sz - 2, 1, 1, (newIdent / 10) % 10 + '0');
  91.                 newName.replace(sz - 1, 1, 1, newIdent % 10 + '0');
  92.         }
  93.         else
  94.         {
  95.                 newName.append("01");
  96.         }
  97.         return MakeUniqueName(forComponentId, newName);
  98. }
  99.  
  100. uint CParticleEffect::AddRenderObjectId()
  101. {
  102.         return m_numRenderObjects++;
  103. }
  104.  
  105. uint CParticleEffect::GetNumRenderObjectIds() const
  106. {
  107.         return m_numRenderObjects;
  108. }
  109.  
  110. void CParticleEffect::SetName(cstr name)
  111. {
  112.         m_name = name;
  113. }
  114.  
  115. void CParticleEffect::Serialize(Serialization::IArchive& ar)
  116. {
  117.         uint documentVersion = 1;
  118.         if (ar.isOutput())
  119.                 documentVersion = gCurrentVersion;
  120.         ar(documentVersion, "Version", "");
  121.         SSerializationContext documentContext(documentVersion);
  122.         Serialization::SContext context(ar, &documentContext);
  123.  
  124.         ar(m_attributes, "Attributes");
  125.  
  126.         if (ar.isInput() && documentVersion < 3)
  127.         {
  128.                 std::vector<CParticleComponent> oldComponents;
  129.                 ar(oldComponents, "Components", "+Components");
  130.                 m_components.reserve(oldComponents.size());
  131.                 for (auto& oldComponent : oldComponents)
  132.                         m_components.push_back(new CParticleComponent(oldComponent));
  133.         }
  134.         else
  135.                 ar(m_components, "Components", "+Components");
  136.  
  137.         if (ar.isInput())
  138.         {
  139.                 auto it = std::remove_if(m_components.begin(), m_components.end(), [](TComponentPtr ptr){ return !ptr; });
  140.                 m_components.erase(it, m_components.end());
  141.                 SetChanged();
  142.                 for (auto& component : m_components)
  143.                         component->SetChanged();
  144.                 Compile();
  145.         }
  146. }
  147.  
  148. IParticleEmitter* CParticleEffect::Spawn(const ParticleLoc& loc, const SpawnParams* pSpawnParams)
  149. {
  150.         FUNCTION_PROFILER(GetISystem(), PROFILE_PARTICLE);
  151.  
  152.         PParticleEmitter pEmitter = GetPSystem()->CreateEmitter(this);
  153.         CParticleEmitter* pCEmitter = static_cast<CParticleEmitter*>(pEmitter.get());
  154.         if (pSpawnParams)
  155.                 pCEmitter->SetSpawnParams(*pSpawnParams);
  156.         pEmitter->Activate(true);
  157.         pCEmitter->SetLocation(loc);
  158.         return pEmitter;
  159. }
  160.  
  161. void CParticleEffect::AddComponent(uint componentIdx)
  162. {
  163.         uint idx = m_components.size();
  164.         CParticleComponent* pNewComponent = new CParticleComponent();
  165.         pNewComponent->m_pEffect = this;
  166.         pNewComponent->m_componentId = componentIdx;
  167.         pNewComponent->SetName("Component01");
  168.         m_components.insert(m_components.begin() + componentIdx, pNewComponent);
  169.         SetChanged();
  170. }
  171.  
  172. void CParticleEffect::RemoveComponent(uint componentIdx)
  173. {
  174.         m_components.erase(m_components.begin() + componentIdx);
  175.         SetChanged();
  176. }
  177.  
  178. void CParticleEffect::SetChanged()
  179. {
  180.         if (!m_dirty)
  181.                 ++m_editVersion;
  182.         m_dirty = true;
  183. }
  184.  
  185. Serialization::SStruct CParticleEffect::GetEffectOptionsSerializer() const
  186. {
  187.         return Serialization::SStruct(m_attributes);
  188. }
  189.  
  190. const ParticleParams& CParticleEffect::GetDefaultParams() const
  191. {
  192.         static ParticleParams paramsStandard;
  193.         return paramsStandard;
  194. }
  195.  
  196. }
  197.  
downloadParticleEffect.cpp Source code - Download CRYENGINE Source code
Related Source Codes/Software:
postal - 2017-06-11
reactide - Reactide is the first dedicated IDE for React web ... 2017-06-11
rkt - rkt is a pod-native container engine for Linux. It... 2017-06-11
uWebSockets - Tiny WebSockets https://for... 2017-06-11
realworld - TodoMVC for the RealWorld - Exemplary fullstack Me... 2017-06-11
CRYENGINE - CRYENGINE is a powerful real-time game development... 2017-06-11
goreplay - GoReplay is an open-source tool for capturing and ... 2017-06-10
pyenv - Simple Python version management 2017-06-10
redux-saga - An alternative side effect model for Redux apps ... 2017-06-10
angular-starter - 2017-06-10

 Back to top