BVB Source Codes

CRYENGINE Show MaterialFGManager.cpp Source code

Return Download CRYENGINE: download MaterialFGManager.cpp Source code - Download CRYENGINE Source code - Type:.cpp
  1. // Copyright 2001-2016 Crytek GmbH / Crytek Group. All rights reserved.
  2.  
  3. // -------------------------------------------------------------------------
  4. //  File name:   MaterialFGManager
  5. //  Version:     v1.00
  6. //  Created:     29/11/2006 by AlexL-Benito GR
  7. //  Compilers:   Visual Studio.NET
  8. //  Description:
  9. // -------------------------------------------------------------------------
  10. //  History:
  11. //
  12. ////////////////////////////////////////////////////////////////////////////
  13.  
  14. #include "StdAfx.h"
  15. #include "MaterialFGManager.h"
  16. #include "MaterialEffectsCVars.h"
  17.  
  18. CMaterialFGManager::CMaterialFGManager()
  19. {
  20. }
  21.  
  22. CMaterialFGManager::~CMaterialFGManager()
  23. {
  24.         m_flowGraphVector.clear();
  25. }
  26.  
  27. //------------------------------------------------------------------------
  28. void CMaterialFGManager::Reset(bool bCleanUp)
  29. {
  30.         for (int i = 0; i < m_flowGraphVector.size(); i++)
  31.         {
  32.                 CMaterialFGManager::SFlowGraphData& current = m_flowGraphVector[i];
  33.                 InternalEndFGEffect(&current, !bCleanUp);
  34.         }
  35.  
  36.         if (bCleanUp)
  37.         {
  38.                 stl::free_container(m_flowGraphVector);
  39.         }
  40. }
  41.  
  42. //------------------------------------------------------------------------
  43. void CMaterialFGManager::Serialize(TSerialize ser)
  44. {
  45.         for (int i = 0; i < m_flowGraphVector.size(); i++)
  46.         {
  47.                 CMaterialFGManager::SFlowGraphData& current = m_flowGraphVector[i];
  48.                 if (ser.BeginOptionalGroup(current.m_name, true))
  49.                 {
  50.                         ser.Value("run", current.m_bRunning);
  51.                         if (ser.BeginOptionalGroup("fg", true))
  52.                         {
  53.                                 current.m_pFlowGraph->Serialize(ser);
  54.                                 ser.EndGroup();
  55.                         }
  56.                         ser.EndGroup();
  57.                 }
  58.         }
  59. }
  60.  
  61. //------------------------------------------------------------------------
  62. // CMaterialFGManager::LoadLibs()
  63. // Iterates through all the files in the folder and load the graphs
  64. // PARAMS
  65. // - path : Folder where the FlowGraph xml files are located
  66. //------------------------------------------------------------------------
  67. bool CMaterialFGManager::LoadLibs(const char* path)
  68. {
  69.         LOADING_TIME_PROFILE_SECTION;
  70.         if (gEnv->pFlowSystem == 0)
  71.                 return false;
  72.  
  73.         Reset(true);
  74.  
  75.         ICryPak* pCryPak = gEnv->pCryPak;
  76.         _finddata_t fd;
  77.         int numLoaded = 0;
  78.  
  79.         string realPath(path);
  80.         realPath.TrimRight("/\\");
  81.         string search(realPath);
  82.         search += "/*.xml";
  83.  
  84.         intptr_t handle = pCryPak->FindFirst(search.c_str(), &fd);
  85.         if (handle != -1)
  86.         {
  87.                 do
  88.                 {
  89.                         // fd.name contains the profile name
  90.                         string filename = realPath;
  91.                         filename += "/";
  92.                         filename += fd.name;
  93.                         bool ok = LoadFG(filename);
  94.                         if (ok)
  95.                                 ++numLoaded;
  96.                         SLICE_AND_SLEEP();
  97.                 }
  98.                 while (pCryPak->FindNext(handle, &fd) >= 0);
  99.  
  100.                 pCryPak->FindClose(handle);
  101.         }
  102.  
  103.         return numLoaded > 0;
  104. }
  105.  
  106. //------------------------------------------------------------------------
  107. // CMaterialFGManager::LoadFG()
  108. // Here is where the FlowGraph is loaded, storing a pointer to it, its name
  109. // and also the IDs of the special "start" and "end" nodes
  110. // PARAMS
  111. // - filename: ...
  112. //------------------------------------------------------------------------
  113. bool CMaterialFGManager::LoadFG(const string& filename, IFlowGraphPtr* pGraphRet /*= NULL*/)
  114. {
  115.         //Create FG from the XML file
  116.         XmlNodeRef rootNode = gEnv->pSystem->LoadXmlFromFile(filename);
  117.         if (rootNode == 0)
  118.                 return false;
  119.         IFlowGraphPtr pFlowGraph = gEnv->pFlowSystem->CreateFlowGraph();
  120.         if (pFlowGraph->SerializeXML(rootNode, true) == false)
  121.         {
  122.                 // give warning
  123.                 GameWarning("MaterialFGManager on LoadFG(%s)==> FlowGraph->SerializeXML failed", filename.c_str());
  124.                 return false;
  125.         }
  126.  
  127.         //Deactivated it by default
  128.         pFlowGraph->SetEnabled(false);
  129.  
  130.         const TFlowNodeId nodeTypeId_StartFX = gEnv->pFlowSystem->GetTypeId("MaterialFX:HUDStartFX");
  131.         const TFlowNodeId nodeTypeId_EndFX = gEnv->pFlowSystem->GetTypeId("MaterialFX:HUDEndFX");
  132.  
  133.         //Store needed data...
  134.         SFlowGraphData fgData;
  135.         fgData.m_pFlowGraph = pFlowGraph;
  136.  
  137.         // search for start and end nodes
  138.         IFlowNodeIteratorPtr pNodeIter = pFlowGraph->CreateNodeIterator();
  139.         TFlowNodeId nodeId;
  140.         while (IFlowNodeData* pNodeData = pNodeIter->Next(nodeId))
  141.         {
  142.                 if (pNodeData->GetNodeTypeId() == nodeTypeId_StartFX)
  143.                 {
  144.                         fgData.m_startNodeId = nodeId;
  145.                 }
  146.                 else if (pNodeData->GetNodeTypeId() == nodeTypeId_EndFX)
  147.                 {
  148.                         fgData.m_endNodeId = nodeId;
  149.                 }
  150.         }
  151.  
  152.         if (fgData.m_startNodeId == InvalidFlowNodeId || fgData.m_endNodeId == InvalidFlowNodeId)
  153.         {
  154.                 // warning no start/end node found
  155.                 GameWarning("MaterialFGManager on LoadFG(%s) ==> No Start/End node found", filename.c_str());
  156.                 return false;
  157.         }
  158.  
  159.         //Finally store the name
  160.         fgData.m_name = PathUtil::GetFileName(filename);
  161.         fgData.m_fileName = filename;
  162.         PathUtil::RemoveExtension(fgData.m_name);
  163.         m_flowGraphVector.push_back(fgData);
  164.  
  165.         // send initialize event to allow for resource caching
  166.         if (gEnv->pCryPak->GetLvlResStatus())
  167.         {
  168.                 SFlowGraphData* pFGData = FindFG(pFlowGraph);
  169.                 if (pFGData)
  170.                         InternalEndFGEffect(pFGData, true);
  171.         }
  172.         if (pGraphRet)
  173.                 *pGraphRet = m_flowGraphVector.rbegin()->m_pFlowGraph;
  174.         return true;
  175. }
  176.  
  177. //------------------------------------------------------------------------
  178. // CMaterialFGManager::FindFG(const string& fgName)
  179. // Find a FlowGraph by name
  180. // PARAMS
  181. // - fgName: Name of the FlowGraph
  182. //------------------------------------------------------------------------
  183. CMaterialFGManager::SFlowGraphData* CMaterialFGManager::FindFG(const string& fgName)
  184. {
  185.         std::vector<SFlowGraphData>::iterator iter = m_flowGraphVector.begin();
  186.         std::vector<SFlowGraphData>::iterator iterEnd = m_flowGraphVector.end();
  187.         while (iter != iterEnd)
  188.         {
  189.                 if (fgName.compareNoCase(iter->m_name) == 0)
  190.                         return &*iter;
  191.                 ++iter;
  192.         }
  193.         return 0;
  194. }
  195.  
  196. //------------------------------------------------------------------------
  197. // CMaterialFGManager::FindFG(IFlowGraphPtr pFG)
  198. // Find a FlowGraph "by pointer"
  199. // PARAMS
  200. // - pFG: FlowGraph pointer
  201. //------------------------------------------------------------------------
  202. CMaterialFGManager::SFlowGraphData* CMaterialFGManager::FindFG(IFlowGraphPtr pFG)
  203. {
  204.         std::vector<SFlowGraphData>::iterator iter = m_flowGraphVector.begin();
  205.         std::vector<SFlowGraphData>::iterator iterEnd = m_flowGraphVector.end();
  206.         while (iter != iterEnd)
  207.         {
  208.                 if (iter->m_pFlowGraph == pFG)
  209.                         return &*iter;
  210.                 ++iter;
  211.         }
  212.         return 0;
  213. }
  214.  
  215. //------------------------------------------------------------------------
  216. // CMaterialFGManager::StartFGEffect(const string& fgName)
  217. // Activate the MaterialFX:StartHUDEffect node, this way the FG will be executed
  218. // PARAMS
  219. // - fgName: Name of the flowgraph effect
  220. //------------------------------------------------------------------------
  221. bool CMaterialFGManager::StartFGEffect(const SMFXFlowGraphParams& fgParams, float curDistance)
  222. {
  223.         SFlowGraphData* pFGData = FindFG(fgParams.fgName);
  224.         if (pFGData == 0)
  225.         {
  226.                 GameWarning("CMaterialFXManager::StartFXEffect: Can't execute FG '%s'", fgParams.fgName.c_str());
  227.         }
  228.         else if (/*pFGData && */ !pFGData->m_bRunning)
  229.         {
  230.                 IFlowGraphPtr pFG = pFGData->m_pFlowGraph;
  231.                 pFG->SetEnabled(true);
  232.                 pFG->InitializeValues();
  233.  
  234.                 TFlowInputData data;
  235.  
  236.                 // set distance output port [1]
  237.                 pFG->ActivatePort(SFlowAddress(pFGData->m_startNodeId, 1, true), curDistance);
  238.                 // set custom params [2] - [3]
  239.                 pFG->ActivatePort(SFlowAddress(pFGData->m_startNodeId, 2, true), fgParams.params[0]);
  240.                 pFG->ActivatePort(SFlowAddress(pFGData->m_startNodeId, 3, true), fgParams.params[1]);
  241.  
  242.                 // set intensity (dynamically adjusted from game if needed) [4]
  243.                 pFG->ActivatePort(SFlowAddress(pFGData->m_startNodeId, 4, true), 1.0f);
  244.  
  245.                 // set input port [0]
  246.                 pFG->ActivatePort(SFlowAddress(pFGData->m_startNodeId, 0, false), data);  // port0: InputPortConfig_Void ("Trigger")
  247.                 //data = fgName;
  248.                 //pFG->ActivatePort(SFlowAddress(pFGData->m_endNodeId, 0, false), data);                // port0: InputPortConfig<string> ("FGName")
  249.                 pFGData->m_bRunning = true;
  250.  
  251.                 if (CMaterialEffectsCVars::Get().mfx_DebugFlowGraphFX != 0)
  252.                 {
  253.                         GameWarning("Material FlowGraphFX manager: Effect %s was triggered.", fgParams.fgName.c_str());
  254.                 }
  255.  
  256.                 return true;
  257.         }
  258.         return false;
  259. }
  260.  
  261. //------------------------------------------------------------------------
  262. // CMaterialFGManager::EndFGEffect(const string& fgName)
  263. // This method will be automatically called when the effect finish
  264. // PARAMS
  265. // - fgName: Name of the FlowGraph
  266. //------------------------------------------------------------------------
  267. bool CMaterialFGManager::EndFGEffect(const string& fgName)
  268. {
  269.         SFlowGraphData* pFGData = FindFG(fgName);
  270.         if (pFGData)
  271.                 return InternalEndFGEffect(pFGData, false);
  272.         return false;
  273. }
  274.  
  275. //===========================================================
  276. // CMaterialFGManager::SetFGCustomParameter(const SMFXFlowGraphParams& fgParams, const char* customParameter, const SMFXCustomParamValue& customParameterValue)
  277. // This method allow for setting some custom, and dynamically updated outputs from the game, to adjust the FX
  278. // PARAMS
  279. //  - fgParams: Flow graph parameters
  280. //  - customParameter: Name of the custom parameter to adjust
  281. //  - customParameterValue: Contains new value for customParameter
  282. //------------------------------------------------------------------------
  283. void CMaterialFGManager::SetFGCustomParameter(const SMFXFlowGraphParams& fgParams, const char* customParameter, const SMFXCustomParamValue& customParameterValue)
  284. {
  285.         SFlowGraphData* pFGData = FindFG(fgParams.fgName);
  286.         if (pFGData == 0)
  287.         {
  288.                 GameWarning("CMaterialFXManager::StartFXEffect: Can't execute FG '%s'", fgParams.fgName.c_str());
  289.         }
  290.         else if (pFGData->m_bRunning)
  291.         {
  292.                 IFlowGraphPtr pFG = pFGData->m_pFlowGraph;
  293.  
  294.                 if (stricmp(customParameter, "Intensity") == 0)
  295.                 {
  296.                         // set intensity (dynamically adjusted from game if needed) [4]
  297.                         pFG->ActivatePort(SFlowAddress(pFGData->m_startNodeId, 4, true), customParameterValue.fValue);
  298.                 }
  299.                 else if (stricmp(customParameter, "BlendOutTime") == 0)
  300.                 {
  301.                         //Activate blend out timer [5]
  302.                         pFG->ActivatePort(SFlowAddress(pFGData->m_startNodeId, 5, true), customParameterValue.fValue);
  303.                 }
  304.  
  305.                 if (CMaterialEffectsCVars::Get().mfx_DebugFlowGraphFX != 0)
  306.                 {
  307.                         GameWarning("Material FlowGraphFX manager: Effect '%s' .Dynamic parameter '%s' set to value %.3f", fgParams.fgName.c_str(), customParameter, customParameterValue.fValue);
  308.                 }
  309.         }
  310. }
  311. //------------------------------------------------------------------------
  312. // CMaterialFGManager::EndFGEffect(IFlowGraphPtr pFG)
  313. // This method will be automatically called when the effect finish
  314. // PARAMS
  315. // - pFG: Pointer to the FlowGraph
  316. //------------------------------------------------------------------------
  317. bool CMaterialFGManager::EndFGEffect(IFlowGraphPtr pFG)
  318. {
  319.         SFlowGraphData* pFGData = FindFG(pFG);
  320.         if (pFGData)
  321.                 return InternalEndFGEffect(pFGData, false);
  322.         return false;
  323. }
  324.  
  325. //------------------------------------------------------------------------
  326. // internal method to end effect. can also initialize values
  327. //------------------------------------------------------------------------
  328. bool CMaterialFGManager::InternalEndFGEffect(SFlowGraphData* pFGData, bool bInitialize)
  329. {
  330.         if (pFGData == 0)
  331.                 return false;
  332.         if (pFGData->m_bRunning || bInitialize)
  333.         {
  334.                 IFlowGraphPtr pFG = pFGData->m_pFlowGraph;
  335.                 if (bInitialize)
  336.                 {
  337.                         if (pFG->IsEnabled() == false)
  338.                         {
  339.                                 pFG->SetEnabled(true);
  340.                                 pFG->InitializeValues();
  341.                         }
  342.                 }
  343.                 pFG->SetEnabled(false);
  344.                 pFGData->m_bRunning = false;
  345.                 return true;
  346.         }
  347.         return false;
  348. }
  349.  
  350. void CMaterialFGManager::PreLoad()
  351. {
  352. #ifndef _RELEASE
  353.  
  354.         // need to preload the missing FX for CScriptBind_Entity::PreLoadParticleEffect
  355.         //      gEnv->pParticleManager->FindEffect( "MissingFGEffect", "MissingFGEffect" );
  356.  
  357. #endif
  358.  
  359.         TFlowGraphData::iterator iBegin = m_flowGraphVector.begin();
  360.         TFlowGraphData::const_iterator iEnd = m_flowGraphVector.end();
  361.         for (TFlowGraphData::iterator i = iBegin; i != iEnd; ++i)
  362.         {
  363.                 i->m_pFlowGraph->PrecacheResources();
  364.         }
  365. }
  366.  
  367. //------------------------------------------------------------------------
  368. // CMaterialFGManager::EndFGEffect(IFlowGraphPtr pFG)
  369. // This method will be automatically called when the effect finish
  370. // PARAMS
  371. // - pFG: Pointer to the FlowGraph
  372. //------------------------------------------------------------------------
  373. bool CMaterialFGManager::IsFGEffectRunning(const string& fgName)
  374. {
  375.         SFlowGraphData* pFGData = FindFG(fgName);
  376.         if (pFGData)
  377.                 return pFGData->m_bRunning;
  378.         return false;
  379. }
  380.  
  381. //------------------------------------------------------------------------
  382. // CMaterialFGManager::ReloadFlowGraphs
  383. // Reload the FlowGraphs (invoked through console command, see MaterialEffectsCVars.cpp)
  384. //------------------------------------------------------------------------
  385. void CMaterialFGManager::ReloadFlowGraphs()
  386. {
  387.         LoadLibs();
  388. }
  389.  
  390. void CMaterialFGManager::GetMemoryUsage(ICrySizer* s) const
  391. {
  392.         SIZER_SUBCOMPONENT_NAME(s, "flowgraphs");
  393.         s->AddObject(this, sizeof(*this));
  394.         s->AddObject(m_flowGraphVector);
  395. }
  396.  
  397. int CMaterialFGManager::GetFlowGraphCount() const
  398. {
  399.         return m_flowGraphVector.size();
  400. }
  401.  
  402. IFlowGraphPtr CMaterialFGManager::GetFlowGraph(int index, string* pFileName /*= NULL*/) const
  403. {
  404.         if (index >= 0 && index < GetFlowGraphCount())
  405.         {
  406.                 const SFlowGraphData& fgData = m_flowGraphVector[index];
  407.                 if (pFileName)
  408.                         *pFileName = fgData.m_fileName;
  409.                 return fgData.m_pFlowGraph;
  410.         }
  411.         return NULL;
  412. }
  413.  
downloadMaterialFGManager.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