particlemanager.cpp 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559
  1. #include "particlemanager.h"
  2. #include "..\..\common_h\render.h"
  3. #include "..\..\Common_h\FileService.h"
  4. #include "..\..\common_h\core.h"
  5. #include "..\..\common_h\corecmds.h"
  6. #include "..\datacache\datacache.h"
  7. #include "..\service\particleservice.h"
  8. #include "..\system\particleprocessor\nameparser.h"
  9. #include "..\icommon\iemitter.h"
  10. #include "..\system\datasource\datastring.h"
  11. #include "..\TextFile.h"
  12. // это надо добавить в свойства проекта !!!!
  13. //#define USE_XML_FILE_FORMAT
  14. DWORD GraphRead = 0;
  15. DWORD OptGraphRead = 0;
  16. DWORD GlobalUniqueForceFieldIndex = 0;
  17. #define DONT_COLLECT_STATS_FRAMES 30
  18. #ifdef _XBOX
  19. #pragma comment(lib, "Xapilibi")
  20. #pragma comment(lib, "Xbdm")
  21. #endif
  22. ParticleManager::ParticleManager (ParticleService* service) : IParticleManager (service),
  23. DeleteQuery (_FL_),
  24. EnumUsedGeom(_FL_),
  25. ForceFields(_FL_)
  26. {
  27. AssertCoreThread
  28. bIsActiveManager = false;
  29. fTime = 0.0f;
  30. fCreatedSystems = 0.0f;
  31. fDeletedSystems = 0.0f;
  32. fCreatedCachedSystems = 0.0f;
  33. framesPassed = 0;
  34. MaxnowTickTime = 0;
  35. maxCreateTime = 0.0;
  36. maxDeleteTime = 0.0;
  37. deleteTime = 0.0;
  38. deleteCount = 0;
  39. createTime = 0.0;
  40. createCount = 0;
  41. m_fixedFPS = 1.0f / 30.0f;
  42. m_time = 0.0f;
  43. pService = service;
  44. ShowStat = false;
  45. spriteProcessor = service->getSpriteProcessor();
  46. GlobalDelete = false;
  47. TimeFromLastStatUpdate = 100.0f;
  48. pFS = (IFileService*)api->GetService("FileService");
  49. Assert (pFS != NULL);
  50. pRS = (IRender*)api->GetService("DX9Render");
  51. Assert (pRS != NULL);
  52. pDataCache = service->GetDataCache();
  53. pProjectTexture = NULL;
  54. pHazeTexture = NULL;
  55. pNormalMapProjectTexture = NULL;
  56. string FileName = "particles";
  57. tempRTForDistorsion = pRS->CreateTempRenderTarget(TRS_SCREEN_FULL_3D, TRC_FIXED_RGBA_8, _FL_);
  58. #ifdef _XBOX
  59. tempRTDForSoftParticles = pRS->CreateTempDepthStencil(TRS_SCREEN_FULL_3D, _FL_, 0);
  60. depthMap = pRS->GetTechniqueGlobalVariable("DepthMap", _FL_);
  61. particleSoftness = pRS->GetTechniqueGlobalVariable("fParticleSoftness", _FL_);
  62. mProjInverse = pRS->GetTechniqueGlobalVariable("mProjInverse", _FL_);
  63. fSoftness = 1.0f;
  64. #else
  65. fitToViewport = pRS->GetTechniqueGlobalVariable("fitToViewport", _FL_);
  66. #endif
  67. pProjectTexture = pRS->CreateTexture(_FL_, FileName.c_str());
  68. pNormalMapProjectTexture = pRS->CreateTexture(_FL_, "%snm", FileName.c_str());
  69. pHazeTexture = pRS->CreateTexture(_FL_, "haze");
  70. TextureName = FileName;
  71. pBaseTexture = pRS->GetTechniqueGlobalVariable("tParticlesTexture", _FL_);
  72. pNormalMap = pRS->GetTechniqueGlobalVariable("tParticlesNormalMap", _FL_);
  73. DistortMap = pRS->GetTechniqueGlobalVariable("tDistortMap", _FL_);
  74. ScreenSurf = pRS->GetTechniqueGlobalVariable("tScreenSurf", _FL_);
  75. DirLightProj = pRS->GetTechniqueGlobalVariable("LightDirProjection", _FL_);
  76. DirLightProjColor = pRS->GetTechniqueGlobalVariable("LightDirProjectionColor", _FL_);
  77. LightAmbient = pRS->GetTechniqueGlobalVariable("LightAmbientParticles", _FL_);
  78. for (dword n = 0; n < 32; n++)
  79. {
  80. ThreadParams.planes[n] = Plane(0.0f);
  81. }
  82. ThreadParams.fDeltaTime = 0.0f;
  83. #ifdef _XBOX
  84. OutputDebugString("ParticleManager ctor\n");
  85. #endif
  86. }
  87. ParticleManager::~ParticleManager ()
  88. {
  89. AssertCoreThread
  90. OutputDebugString("ParticleManager::~ParticleManager ---------------------------------\n");
  91. #ifdef _XBOX
  92. if (tempRTDForSoftParticles)
  93. {
  94. tempRTDForSoftParticles->Release();
  95. tempRTDForSoftParticles = NULL;
  96. }
  97. #endif
  98. if (tempRTForDistorsion)
  99. {
  100. tempRTForDistorsion->Release();
  101. tempRTForDistorsion = NULL;
  102. }
  103. if (fTime > 0.001f)
  104. {
  105. api->Trace("Particles cache create(%f) freq: %f systems per second", fCreatedCachedSystems, fCreatedCachedSystems / fTime);
  106. api->Trace("Particles create(%f) freq: %f systems per second", fCreatedSystems, fCreatedSystems / fTime);
  107. api->Trace("Particles destroy(%f) freq: %f systems per second", fDeletedSystems, fDeletedSystems / fTime);
  108. }
  109. pBaseTexture = NULL;
  110. pNormalMap = NULL;
  111. DistortMap = NULL;
  112. ScreenSurf = NULL;
  113. DirLightProj = NULL;
  114. LightAmbient = NULL;
  115. DirLightProjColor = NULL;
  116. DeleteAllSystems ();
  117. if (pProjectTexture) pProjectTexture->Release();
  118. pProjectTexture = NULL;
  119. if (pHazeTexture) pHazeTexture->Release();
  120. pHazeTexture = NULL;
  121. if (pNormalMapProjectTexture) pNormalMapProjectTexture->Release();
  122. pNormalMapProjectTexture = NULL;
  123. pDataCache = NULL;
  124. spriteProcessor = NULL;
  125. pService->RemoveManagerFromList(this);
  126. }
  127. #ifdef ENABLE_PARTICLE_THREADS
  128. void ParticleManager::Lock ()
  129. {
  130. sync_access_to_array.Enter();
  131. }
  132. void ParticleManager::Unlock ()
  133. {
  134. sync_access_to_array.Leave();
  135. }
  136. #endif
  137. bool ParticleManager::Release ()
  138. {
  139. AssertCoreThread
  140. delete this;
  141. return true;
  142. }
  143. IFileService* ParticleManager::Files ()
  144. {
  145. AssertCoreThread
  146. return pFS;
  147. }
  148. //Установить текстуру проекта
  149. /*
  150. void ParticleManager::SetProjectTexture (const char* FileName)
  151. {
  152. if (pProjectTexture)
  153. {
  154. pProjectTexture->Release();
  155. pProjectTexture = NULL;
  156. }
  157. if (pNormalMapProjectTexture)
  158. {
  159. pNormalMapProjectTexture->Release();
  160. pNormalMapProjectTexture = NULL;
  161. }
  162. pProjectTexture = pRS->CreateTexture(_FL_, FileName);
  163. string a;
  164. a.GetFileTitle(FileName);
  165. pNormalMapProjectTexture = pRS->CreateTexture(_FL_, "%snm", a.c_str());
  166. TextureName = FileName;
  167. }
  168. const char* ParticleManager::GetProjectTextureName ()
  169. {
  170. return TextureName.GetBuffer();
  171. }
  172. */
  173. //Открыть проект
  174. /*
  175. bool ParticleManager::OpenProject (const char* FileName)
  176. {
  177. CloseProject ();
  178. ShortProjectName = FileName;
  179. string LongFileName = "resource\\particles\\";
  180. LongFileName += FileName;
  181. LongFileName.AddExtention(".prj");
  182. IIniFile* IniFile = pFS->OpenIniFile(LongFileName.GetBuffer(), _FL_);
  183. if (!IniFile)
  184. {
  185. api->Trace("Can't find project '%s'", LongFileName.GetBuffer());
  186. return false;
  187. }
  188. //Устанавливаем текстуру проекта...
  189. const char* szProjTexture = IniFile->GetString("Textures", "MainTexture", "none");
  190. //api->Trace("Manager use texture: %s", IniStringBuffer);
  191. SetProjectTexture (szProjTexture);
  192. //Загружаем данные
  193. for (int n = 0; n < 9999; n++)
  194. {
  195. string Section;
  196. Section.Format("System_%04d", n);
  197. const char* szSys = IniFile->GetString("Manager", (char*)Section.GetBuffer(), NULL);
  198. if (!szSys) break;
  199. pDataCache->CacheSystem(szSys);
  200. }
  201. IniFile->Release();
  202. //FIX ME !
  203. //Если будет асинхронная загрузка это неверно...
  204. CreateGeomCache();
  205. return true;
  206. }
  207. */
  208. /*
  209. //Получить имя проекта
  210. const char* ParticleManager::GetProjectFileName ()
  211. {
  212. return ShortProjectName.GetBuffer();
  213. }
  214. //Закрыть проект
  215. void ParticleManager::CloseProject ()
  216. {
  217. BB_Processor->Clear ();
  218. MDL_Processor->Clear ();
  219. DeleteAllSystems ();
  220. if (pProjectTexture) pProjectTexture->Release();
  221. if (pNormalMapProjectTexture) pNormalMapProjectTexture->Release();
  222. pProjectTexture = NULL;
  223. pNormalMapProjectTexture = NULL;
  224. pDataCache->ResetCache();
  225. pGeomCache->ResetCache();
  226. }
  227. */
  228. //Удалить из списка ресурсов (системная)
  229. void ParticleManager::RemoveResource (IParticleSystem* pResource)
  230. {
  231. AssertCoreThread
  232. fDeletedSystems += 1.0f;
  233. if (GlobalDelete) return;
  234. for (DWORD n = 0; n < ThreadParams.SystemsProxy.Size(); n++)
  235. {
  236. ParticleSystemProxy* sys = ThreadParams.SystemsProxy[n];
  237. if (sys == pResource)
  238. {
  239. ParticleSystemProxy* ptrToDelete = sys;
  240. ParticleSystem* guid = ptrToDelete->system_GetGUID_dontUSE_for_WORK_use_LOCK();
  241. //Вдруг ее создали на этом кадре и убили, до конца кадра - надо ее почистить
  242. //================================================================================
  243. for (dword q = 0; q < ThreadParams.SystemsCreated.Size(); q++)
  244. {
  245. if (ThreadParams.SystemsCreated[q] == guid)
  246. {
  247. ThreadParams.SystemsCreated.ExtractNoShift(q);
  248. break;
  249. }
  250. }
  251. //================================================================================
  252. ThreadParams.SystemsDeleted.Add(guid);
  253. ThreadParams.SystemsProxy.ExtractNoShift(n);
  254. delete ptrToDelete;
  255. return;
  256. }
  257. }
  258. #ifdef ENABLE_PARTICLE_THREADS
  259. Unlock();
  260. #endif
  261. Assert(!"Can't find resource for delete !!!");
  262. }
  263. bool ParticleManager::Draw ()
  264. {
  265. AssertCoreThread
  266. if (!bIsActiveManager)
  267. {
  268. return false;
  269. }
  270. #ifdef ENABLE_PARTICLE_THREADS
  271. pService->WaitUntilUpdateFinished();
  272. #endif
  273. bool bRes = DrawSimpleParticles(false);
  274. return bRes;
  275. }
  276. bool ParticleManager::DrawAllParticles ()
  277. {
  278. AssertCoreThread
  279. if (!bIsActiveManager)
  280. {
  281. return false;
  282. }
  283. #ifdef ENABLE_PARTICLE_THREADS
  284. pService->WaitUntilUpdateFinished();
  285. #endif
  286. /*
  287. ParticleVB * renderData = pService->GetUpdateData();
  288. pRS->Print(0, 350, 0xFFFFFFFF, "p: %d", renderData->markup.particles_count);
  289. */
  290. bool bHaveDistorsion = DrawSimpleParticles(true);
  291. if (bHaveDistorsion)
  292. {
  293. //завершаем текущую сцену и копируем содержимое в distortTex
  294. //нужна temp текстура с экран
  295. IBaseTexture* tempTexture = tempRTForDistorsion->AsTexture ();
  296. pRS->ResolveColor(tempTexture);
  297. dword dwPartIDDist = pRS->pixBeginEvent(_FL_, "ParticleManager::DrawDistorsion");
  298. DistortMap->SetTexture(pHazeTexture);
  299. ScreenSurf->SetTexture(tempTexture);
  300. pBaseTexture->SetTexture(pHazeTexture);
  301. pNormalMap->SetTexture(pHazeTexture);
  302. RENDERSCREEN scr = pRS->GetScreenInfo3D();
  303. float fWidth = (float)scr.dwWidth;
  304. float fHeight = (float)scr.dwHeight;
  305. RENDERVIEWPORT rvp = pRS->GetViewport();
  306. Vector4 var;
  307. var.x = rvp.X / fWidth;
  308. var.y = rvp.Y / fHeight;
  309. var.z = rvp.Width / fWidth;
  310. var.w = rvp.Height / fHeight;
  311. //Матрица масштабирования
  312. Matrix mInvertY;
  313. mInvertY.BuildScale(Vector(1.0, -1.0, 1.0f));
  314. //Матрица приводящая к диапазону 0..1
  315. Matrix mNormalization;
  316. mNormalization.BuildScale(Vector(0.5f, 0.5f, 1.0f));
  317. mNormalization = (mInvertY * mNormalization) * Matrix(Vector(0.0f), Vector(0.5f, 0.5f, 1.0f));
  318. Matrix mFitToViewport;
  319. mFitToViewport.BuildScale(Vector(var.z, var.w, 1.0f));
  320. mFitToViewport = mFitToViewport * Matrix(Vector(0.0f), Vector(var.x, var.y, 1.0f));
  321. ParticleVB * renderData = pService->GetUpdateData();
  322. IIBuffer * pIndexBuffer = pService->GetCurrentIndexBuffer();
  323. spriteProcessor->DrawDistorted(*renderData, pIndexBuffer);
  324. pRS->pixEndEvent(_FL_, dwPartIDDist);
  325. } //draw distorted particles
  326. TimeFromLastStatUpdate += api->GetDeltaTime();
  327. if (TimeFromLastStatUpdate > 1.0f)
  328. {
  329. TimeFromLastStatUpdate = 0.0f;
  330. nowTickTime = 0;
  331. nowUpdateTime = 0;
  332. }
  333. if (framesPassed > DONT_COLLECT_STATS_FRAMES)
  334. {
  335. MaxnowTickTime = coremax (MaxnowTickTime, nowTickTime);
  336. }
  337. if (ShowStat)
  338. {
  339. /*
  340. IFont* pSysFont = pRS->GetSystemFont ();
  341. float Width = pSysFont->GetLength("Graph read count - %d", GraphRead);
  342. pSysFont->Release();
  343. */
  344. RS_SPRITE spr[4];
  345. float x1 = -1.0f;
  346. float x2 = ((290.0f / (float)pRS->GetViewport().Width) * 2) - 1.0f;
  347. float y1 = 1.0f - ((16.0f / (float)pRS->GetViewport().Height)*2.0f);
  348. float y2 = 1.0f - ((150.0f / (float)pRS->GetViewport().Height)* 2.0f);
  349. spr[0].vPos = Vector(x1, y1, 0.2f);
  350. spr[1].vPos = Vector(x2, y1, 0.2f);
  351. spr[2].vPos = Vector(x2, y2, 0.2f);
  352. spr[3].vPos = Vector(x1, y2, 0.2f);
  353. pRS->DrawSprites(NULL, spr, 1, "dbgInfoSprite");
  354. float posY = 100;
  355. ParticleVB * renderData = pService->GetUpdateData();
  356. //pRS->Print(0, 350, 0xFFFFFFFF, "p: %d", renderData->markup.particles_count);
  357. pRS->Print(0, posY+0, 0xFFFFFFFF, "PCount:%d", renderData->markup.particles_count);
  358. pRS->Print(0, posY+16, 0xFFFFFFFF, "Systems - act:%d inact:%d", ActiveSystems, InactiveSystems);
  359. pRS->Print(0, posY+32, 0xFFFFFFFF, "Emitters - %d", ActiveEmitters);
  360. pRS->Print(0, posY+80, 0xFFFFFFFF, "Total time - %d, max: %d", nowTickTime, MaxnowTickTime);
  361. pRS->Print(0, posY+96, 0xFFFFFFFF, "Update time - %d, Solver Iterations - %d", nowUpdateTime, 0);
  362. if (createCount > 0) pRS->Print(0, posY+144, 0xFFFFFFFF, "Max create time %3.2f, Average create time %3.2f, create count %d", maxCreateTime, createTime / (double)createCount, createCount);
  363. if (deleteCount > 0) pRS->Print(0, posY+160, 0xFFFFFFFF, "Max delete time %3.2f, Average delete time %3.2f, delete count %d", maxDeleteTime, deleteTime / (double)deleteCount, deleteCount);
  364. float ReadsPerParticle = 0.0f;
  365. pRS->Print(0, posY+112, 0xFFFFFFFF, "Graph read count - %d [%d], read per particle %3.2f", GraphRead, OptGraphRead, ReadsPerParticle);
  366. if (GraphRead != 0)
  367. {
  368. float AverageReadTime = (float)nowUpdateTime / (float)(GraphRead + OptGraphRead);
  369. pRS->Print(0, posY+128, 0xFFFFFFFF, "Average read time - %3.2f", AverageReadTime);
  370. }
  371. }
  372. if (api->DebugKeyState(VK_F3, VK_CONTROL))
  373. {
  374. ShowStat = !ShowStat;
  375. Sleep(100);
  376. }
  377. return bHaveDistorsion;
  378. }
  379. bool ParticleManager::DrawSimpleParticles (bool bSoftParticlesSupport)
  380. {
  381. AssertCoreThread
  382. dword dwPartID = pRS->pixBeginEvent(_FL_, "ParticleManager::Draw");
  383. #ifndef _XBOX
  384. RENDERVIEWPORT rvp = pRS->GetViewport();
  385. RENDERSCREEN scr = pRS->GetScreenInfo3D();
  386. float fWidth = (float)scr.dwWidth;
  387. float fHeight = (float)scr.dwHeight;
  388. Vector4 fitToViewPort;
  389. fitToViewPort.x = rvp.X / fWidth;
  390. fitToViewPort.y = rvp.Y / fHeight;
  391. fitToViewPort.z = rvp.Width / fWidth;
  392. fitToViewPort.w = rvp.Height / fHeight;
  393. fitToViewport->SetVector4(fitToViewPort);
  394. #endif
  395. #ifdef _XBOX
  396. if (bSoftParticlesSupport)
  397. {
  398. pRS->ResolveDepth(tempRTDForSoftParticles);
  399. depthMap->SetTexture(tempRTDForSoftParticles->AsTexture ());
  400. particleSoftness->SetFloat(fSoftness);
  401. Matrix mProjInv = pRS->GetProjection();
  402. mProjInv.InverseComplette4X4();
  403. mProjInverse->SetMatrix(mProjInv);
  404. /*
  405. XINPUT_STATE inputState;
  406. XInputGetState(0, &inputState);
  407. if (inputState.Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_UP)
  408. {
  409. fSoftness += 0.001f;
  410. }
  411. if (inputState.Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_DOWN)
  412. {
  413. fSoftness -= 0.001f;
  414. }
  415. if (inputState.Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_LEFT)
  416. {
  417. fSoftness += 0.01f;
  418. }
  419. if (inputState.Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_RIGHT)
  420. {
  421. fSoftness -= 0.01f;
  422. }
  423. */
  424. }
  425. #endif
  426. pBaseTexture->SetTexture(pProjectTexture);
  427. pNormalMap->SetTexture(pNormalMapProjectTexture);
  428. Vector vDir = pRS->GetGlobalLightDirection();
  429. Matrix mCurView = pRS->GetView();
  430. Vector vProjDir = mCurView.MulNormal(vDir);
  431. vProjDir.Normalize();
  432. vProjDir.z = -vProjDir.z;
  433. DirLightProj->SetVector(vProjDir);
  434. DirLightProjColor->SetVector4(pRS->GetGlobalLightColor().v4);
  435. Vector ambColor = pRS->GetAmbient().v4.v;
  436. LightAmbient->SetVector(ambColor);
  437. bool bHaveDistorsion = false;
  438. ParticleVB * renderData = pService->GetUpdateData();
  439. IIBuffer * pIndexBuffer = pService->GetCurrentIndexBuffer();
  440. bHaveDistorsion = spriteProcessor->Draw(*renderData, pIndexBuffer, bSoftParticlesSupport);
  441. pRS->pixEndEvent(_FL_, dwPartID);
  442. return bHaveDistorsion;
  443. }
  444. bool ParticleManager::Update ()
  445. {
  446. #ifdef ENABLE_PARTICLE_THREADS
  447. sync_access_to_array.Enter();
  448. #endif
  449. framesPassed++;
  450. GraphRead = 0;
  451. OptGraphRead = 0;
  452. ActiveSystems = 0;
  453. InactiveSystems = 0;
  454. ActiveEmitters = 0;
  455. dword dwIterCount = 0;
  456. m_time = m_time + ThreadParams.fDeltaTime;
  457. if (m_time >= m_fixedFPS)
  458. {
  459. for (;;)
  460. {
  461. ActiveSystems = 0;
  462. InactiveSystems = 0;
  463. for (DWORD n = 0; n < ThreadParams.SystemsForThread.Size(); n++)
  464. {
  465. ParticleSystem* pSystem = ThreadParams.SystemsForThread[n];
  466. if (pSystem->IsActive())
  467. {
  468. ActiveSystems++;
  469. ActiveEmitters += pSystem->Execute(m_fixedFPS);
  470. }
  471. else
  472. {
  473. InactiveSystems++;
  474. }
  475. }
  476. spriteProcessor->UpdateParticles(m_fixedFPS);
  477. spriteProcessor->DeleteDeadParticles();
  478. m_time = m_time - m_fixedFPS;
  479. dwIterCount++;
  480. if (m_time < m_fixedFPS) break;
  481. }
  482. }
  483. spriteProcessor->BuildVertexBuffers(ThreadParams);
  484. #ifdef ENABLE_PARTICLE_THREADS
  485. sync_access_to_array.Leave();
  486. #endif
  487. return true;
  488. }
  489. //Исполнить партиклы
  490. void ParticleManager::Execute (float DeltaTime)
  491. {
  492. AssertCoreThread
  493. if (!bIsActiveManager)
  494. {
  495. return;
  496. }
  497. fTime += DeltaTime;
  498. //------------------
  499. #ifdef ENABLE_PARTICLE_THREADS
  500. sync_access_to_array.Enter();
  501. #endif
  502. ThreadParams.updateData = pService->GetUpdateData();
  503. ThreadParams.mView = pRS->GetView();
  504. ThreadParams.fDeltaTime = DeltaTime;
  505. memcpy (ThreadParams.planes, pRS->GetFrustum(), sizeof(Plane) * pRS->GetNumFrustumPlanes());
  506. for (dword n = 0; n < ThreadParams.SystemsProxy.Size(); n++)
  507. {
  508. ThreadParams.SystemsProxy[n]->SendReceivePackets();
  509. }
  510. //Удаляем умершие системы...
  511. for (dword j = 0; j < ThreadParams.SystemsDeleted.Size(); j++)
  512. {
  513. for (dword n = 0; n < ThreadParams.SystemsForThread.Size(); n++)
  514. {
  515. ParticleSystem* toDelete = ThreadParams.SystemsForThread[n];
  516. ParticleSystem* pSys = ThreadParams.SystemsDeleted[j];
  517. if (pSys == toDelete)
  518. {
  519. ThreadParams.SystemsForThread.ExtractNoShift(n);
  520. toDelete->Release();
  521. continue;
  522. }
  523. }
  524. }
  525. ThreadParams.SystemsDeleted.DelAll();
  526. //Добавляем новые...
  527. for (dword n = 0; n < ThreadParams.SystemsCreated.Size(); n++)
  528. {
  529. ThreadParams.SystemsForThread.Add(ThreadParams.SystemsCreated[n]);
  530. }
  531. ThreadParams.SystemsCreated.DelAll();
  532. #ifdef ENABLE_PARTICLE_THREADS
  533. sync_access_to_array.Leave();
  534. pService->StartUpdateThread(this);
  535. #else
  536. ThreadParams.updateData->Lock();
  537. Update();
  538. ThreadParams.updateData->Unlock();
  539. #endif
  540. //---- это из основного ---------------
  541. for (DWORD n = 0; n < DeleteQuery.Size(); n++)
  542. {
  543. DeleteQuery[n]->Release();
  544. }
  545. DeleteQuery.DelAll();
  546. }
  547. //Узнать доступна система или нет
  548. bool ParticleManager::IsSystemAvailable (const char* FileName)
  549. {
  550. AssertCoreThread
  551. if (pDataCache->GetParticleSystemDataSource (FileName)) return true;
  552. return false;
  553. }
  554. /*
  555. //Получить глобальную текстуру проекта
  556. IBaseTexture* ParticleManager::GetProjectTexture ()
  557. {
  558. return pProjectTexture;
  559. }
  560. */
  561. void ParticleManager::AddDeleteTime (double time)
  562. {
  563. AssertCoreThread
  564. deleteTime += time;
  565. deleteCount++;
  566. maxDeleteTime = coremax(time, maxDeleteTime);
  567. }
  568. /*
  569. ParticleSystemProxy* ParticleManager::GetFreeSystem (const char* szFileName)
  570. {
  571. return NULL;
  572. dword dwSearchHash = string::HashNoCase(szFileName);
  573. for (dword n = 0; n < FreeCachedSystems.Size(); n++)
  574. {
  575. if (FreeCachedSystems[n].dwNameHash == dwSearchHash)
  576. {
  577. if (crt_stricmp(FreeCachedSystems[n].systemPtr->GetName(), szFileName) == 0)
  578. {
  579. ParticleSystemProxy* proxy = FreeCachedSystems[n].systemPtr;
  580. FreeCachedSystems.ExtractNoShift(n);
  581. return proxy;
  582. }
  583. }
  584. }
  585. return NULL;
  586. }
  587. */
  588. //Создать партикловую систему из файла (файл должен быть в проекте!!!!!)
  589. IParticleSystem* ParticleManager::CreateParticleSystemEx (const char* FileName, const char* File, int Line)
  590. {
  591. AssertCoreThread
  592. fCreatedSystems += 1.0f;
  593. bIsActiveManager = true;
  594. CoreCommand_GetMemStat memStatInit;
  595. api->ExecuteCoreCommand(memStatInit);
  596. ProfileTimer pTimer;
  597. ProfileTimer pTimer2;
  598. DataSource* pDataSource = pDataCache->GetParticleSystemDataSource (FileName);
  599. if (!pDataSource)
  600. {
  601. api->Error("Particle system '%s' can't loading. Reason: Not found in cache", FileName);
  602. return NULL;
  603. }
  604. pTimer2.Stop();
  605. ProfileTimer pTimer3;
  606. ProfileTimer pTimer4;
  607. ParticleSystemProxy* pSys = CreateParticleSystemFromDataSource (pDataSource);
  608. pTimer4.Stop();
  609. pSys->SetName(FileName);
  610. pTimer3.Stop();
  611. pTimer.Stop();
  612. createTime += pTimer.GetTime();
  613. createCount++;
  614. if (framesPassed > DONT_COLLECT_STATS_FRAMES)
  615. {
  616. maxCreateTime = coremax(pTimer.GetTime(), maxCreateTime);
  617. }
  618. return pSys;
  619. }
  620. void ParticleManager::DeleteAllSystems ()
  621. {
  622. AssertCoreThread
  623. #ifdef ENABLE_PARTICLE_THREADS
  624. sync_access_to_array.Enter();
  625. #endif
  626. GlobalDelete = true;
  627. for (DWORD n = 0; n < ThreadParams.SystemsProxy.Size(); n++)
  628. {
  629. ParticleSystemProxy* proxyToDelete = ThreadParams.SystemsProxy[n];
  630. delete proxyToDelete;
  631. }
  632. #ifdef ENABLE_PARTICLE_THREADS
  633. for (DWORD n = 0; n < ThreadParams.SystemsForThread.Size(); n++)
  634. {
  635. ThreadParams.SystemsForThread[n]->Release();
  636. }
  637. #endif
  638. for (DWORD n = 0; n < ThreadParams.SystemsCreated.Size(); n++)
  639. {
  640. ThreadParams.SystemsCreated[n]->Release();
  641. }
  642. ThreadParams.SystemsProxy.DelAll();
  643. ThreadParams.SystemsForThread.DelAll();
  644. ThreadParams.SystemsCreated.DelAll();
  645. ThreadParams.SystemsDeleted.DelAll();
  646. #ifdef ENABLE_PARTICLE_THREADS
  647. sync_access_to_array.Leave();
  648. #endif
  649. GlobalDelete = false;
  650. }
  651. BillBoardProcessor* ParticleManager::getBBProcessor ()
  652. {
  653. return spriteProcessor;
  654. }
  655. //Создать пустую партикловую систему, для редактора...
  656. IParticleSystem* ParticleManager::CreateEmptyParticleSystemEx (const char* FileName, int Line)
  657. {
  658. AssertCoreThread
  659. return NULL;
  660. }
  661. bool ParticleManager::ValidateSystem (IParticleSystem* pSystem)
  662. {
  663. AssertCoreThread
  664. for (DWORD n = 0; n < ThreadParams.SystemsProxy.Size(); n++)
  665. {
  666. if (ThreadParams.SystemsProxy[n] == pSystem) return true;
  667. }
  668. return false;
  669. }
  670. DWORD ParticleManager::GetCreatedSystemCount ()
  671. {
  672. AssertCoreThread
  673. return ThreadParams.SystemsProxy.Size();
  674. }
  675. ParticleSystemProxy* ParticleManager::GetCreatedSystemByIndex (DWORD Index)
  676. {
  677. AssertCoreThread
  678. return ThreadParams.SystemsProxy[Index];
  679. }
  680. void ParticleManager::DefferedDelete (IParticleSystem* pSys)
  681. {
  682. AssertCoreThread
  683. for (dword n = 0; n < DeleteQuery.Size(); n++)
  684. {
  685. if (DeleteQuery[n] == pSys)
  686. {
  687. api->Trace("[Need to fix this] Try to deffered delete, system that already defered deleted !!!");
  688. return;
  689. }
  690. }
  691. DeleteQuery.Add(pSys);
  692. }
  693. void ParticleManager::Editor_UpdateCachedData ()
  694. {
  695. AssertCoreThread
  696. for (DWORD n = 0; n < ThreadParams.SystemsProxy.Size(); n++)
  697. {
  698. ParticleSystem* sys = ThreadParams.SystemsProxy[n]->Lock();
  699. sys->Editor_UpdateCachedData ();
  700. ThreadParams.SystemsProxy[n]->Unlock();
  701. }
  702. }
  703. DWORD ParticleManager::GetProjectSystemCount ()
  704. {
  705. AssertCoreThread
  706. return pDataCache->GetCachedCount();
  707. }
  708. const char* ParticleManager::GetProjectSystemName (DWORD Index)
  709. {
  710. AssertCoreThread
  711. return pDataCache->GetCachedNameByIndex(Index);
  712. }
  713. const char* ParticleManager::GetFirstGeomName (const char* FileName)
  714. {
  715. AssertCoreThread
  716. IteratorIndex = 0;
  717. GeomNameParser Parser;
  718. EnumUsedGeom.DelAll ();
  719. DataSource* pDataSource = pDataCache->GetParticleSystemDataSource (FileName);
  720. if (!pDataSource) return NULL;
  721. DWORD count = pDataSource->GetEmitterCount();
  722. for (DWORD n = 0; n < count; n++)
  723. {
  724. DataSource::EmitterDesc* pDesc = pDataSource->GetEmitterDesc(n);
  725. for (DWORD i = 0; i < pDesc->Particles.Size(); i++)
  726. {
  727. DataString* pModelNames = pDesc->Particles[i].Fields.FindStringByGUID(GUID_PARTICLE_GEOM_NAMES);
  728. if (!pModelNames) continue;
  729. Parser.Tokenize(pModelNames->GetValue());
  730. DWORD TCount = Parser.GetTokensCount();
  731. if (TCount > 0)
  732. {
  733. for (DWORD j = 0; j < TCount; j++)
  734. {
  735. const char* GeomName = Parser.GetTokenByIndex(j);
  736. if (!FindInEnumUsedGeom(GeomName)) EnumUsedGeom.Add(string(GeomName));
  737. }
  738. }
  739. }
  740. }
  741. if (EnumUsedGeom.Size() == 0) return NULL;
  742. const char* FirstName = EnumUsedGeom[IteratorIndex].GetBuffer();
  743. IteratorIndex++;
  744. return FirstName;
  745. }
  746. const char* ParticleManager::GetNextGeomName ()
  747. {
  748. AssertCoreThread
  749. if (IteratorIndex >= EnumUsedGeom.Size()) return NULL;
  750. const char* FirstName = EnumUsedGeom[IteratorIndex].GetBuffer();
  751. IteratorIndex++;
  752. return FirstName;
  753. }
  754. bool ParticleManager::FindInEnumUsedGeom (const char* GeomName)
  755. {
  756. AssertCoreThread
  757. for (DWORD n = 0; n < EnumUsedGeom.Size(); n++)
  758. {
  759. const char* StoredGeomName = EnumUsedGeom[n].GetBuffer();
  760. if (crt_stricmp (StoredGeomName, GeomName) == 0) return true;
  761. }
  762. return false;
  763. }
  764. void ParticleManager::WriteSystemCache (const char* FileName)
  765. {
  766. #ifndef _XBOX
  767. AssertCoreThread
  768. DataSource* pDataSource = pDataCache->GetParticleSystemDataSource (FileName);
  769. if (!pDataSource)
  770. {
  771. api->Error("Particle system '%s' can't save. Reason: Not found in cache", FileName);
  772. return;
  773. }
  774. MemFile pMemSave;
  775. pMemSave.OpenWrite(1048576);
  776. pDataSource->Write(&pMemSave);
  777. string LongFileName = "resource\\particles\\";
  778. LongFileName+=FileName;
  779. LongFileName.AddExtention(".xps");
  780. bool res = pFS->SaveData(LongFileName.GetBuffer(), pMemSave.GetBuffer(), pMemSave.GetLength());
  781. if (!res)
  782. {
  783. ::MessageBox(NULL, "Can't save file !!!!\nWriteprotection ?", "Save error", MB_OK);
  784. }
  785. pMemSave.Close();
  786. LongFileName = "resource\\particles\\";
  787. LongFileName+=FileName;
  788. LongFileName.DeleteLastSymbol('s');
  789. LongFileName.DeleteLastSymbol('p');
  790. LongFileName.DeleteLastSymbol('x');
  791. LongFileName.DeleteLastSymbol('.');
  792. LongFileName.AddExtention(".xml");
  793. TextFile xmlFile(LongFileName.c_str());
  794. xmlFile.Write(0, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
  795. pDataSource->WriteXML(&xmlFile, 0);
  796. api->Error("Particle system '%s' saved.", FileName);
  797. #endif
  798. }
  799. void ParticleManager::WriteSystemCacheAs (const char* FileName, const char* NewName)
  800. {
  801. #ifndef _XBOX
  802. AssertCoreThread
  803. DataSource* pDataSource = pDataCache->GetParticleSystemDataSource (FileName);
  804. if (!pDataSource)
  805. {
  806. api->Error("Particle system '%s' can't save. Reason: Not found in cache", FileName);
  807. return;
  808. }
  809. MemFile pMemSave;
  810. pMemSave.OpenWrite(1048576);
  811. pDataSource->Write(&pMemSave);
  812. pFS->SaveData(NewName, pMemSave.GetBuffer(), pMemSave.GetLength());
  813. pMemSave.Close();
  814. string LongFileName = NewName;
  815. LongFileName.DeleteLastSymbol('s');
  816. LongFileName.DeleteLastSymbol('p');
  817. LongFileName.DeleteLastSymbol('x');
  818. LongFileName.DeleteLastSymbol('.');
  819. LongFileName.AddExtention(".xml");
  820. TextFile xmlFile(LongFileName.c_str());
  821. xmlFile.Write(0, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
  822. pDataSource->WriteXML(&xmlFile, 0);
  823. api->Trace("Particle system '%s' saved.", NewName);
  824. #endif
  825. }
  826. void ParticleManager::WriteSystemCache (const char* FileName, MemFile* pMemFile)
  827. {
  828. AssertCoreThread
  829. DataSource* pDataSource = pDataCache->GetParticleSystemDataSource (FileName);
  830. if (!pDataSource)
  831. {
  832. api->Error("Particle system '%s' can't save. Reason: Not found in cache", FileName);
  833. return;
  834. }
  835. pDataSource->Write(pMemFile);
  836. }
  837. void ParticleManager::LoadSystemCache (const char* FileName, MemFile* pMemFile)
  838. {
  839. AssertCoreThread
  840. DataSource* pDataSource = pDataCache->GetParticleSystemDataSource (FileName);
  841. if (!pDataSource)
  842. {
  843. api->Error("Particle system '%s' can't load. Reason: Not found in cache", FileName);
  844. return;
  845. }
  846. pDataSource->Destroy();
  847. pDataSource->Load(pMemFile);
  848. CacheReloaded ();
  849. }
  850. void ParticleManager::CacheReloaded ()
  851. {
  852. AssertCoreThread
  853. DWORD n = 0;
  854. array<CacheReloadedInfo> UsedSystems(_FL_);
  855. for (n = 0; n < ThreadParams.SystemsProxy.Size(); n++)
  856. {
  857. ParticleSystemProxy* pSystem = ThreadParams.SystemsProxy[n];
  858. CacheReloadedInfo Info;
  859. Info.Name = pSystem->GetName();
  860. Info.AutoDeleted = pSystem->IsAutoDeleted();
  861. ParticleSystem* sys = pSystem->Lock();
  862. Info.AttachedScene = pSystem->GetAttachedSceneToGeom();
  863. Info.AttachedToGeom = pSystem->GetAttachedToGeom();
  864. sys->GetTransform(Info.matWorld);
  865. pSystem->Unlock();
  866. UsedSystems.Add(Info);
  867. }
  868. DeleteAllSystems ();
  869. spriteProcessor->Clear();
  870. for (n = 0; n < UsedSystems.Size(); n++)
  871. {
  872. IParticleSystem* pSystem = CreateParticleSystemEx (UsedSystems[n].Name.GetBuffer(), _FL_);
  873. pSystem->AutoDelete(UsedSystems[n].AutoDeleted);
  874. pSystem->SetTransform(UsedSystems[n].matWorld);
  875. if (UsedSystems[n].AttachedToGeom.isValid())
  876. {
  877. pSystem->AttachTo(UsedSystems[n].AttachedScene, UsedSystems[n].AttachedToGeom, true);
  878. }
  879. }
  880. }
  881. FieldList* ParticleManager::Editor_CreatePointEmitter (const char* SystemName, const char* EmitterName)
  882. {
  883. AssertCoreThread
  884. DataSource* pDataSource = pDataCache->GetParticleSystemDataSource (SystemName);
  885. if (!pDataSource) return NULL;
  886. FieldList* pData = pDataSource->CreateEmptyPointEmitter(EmitterName);
  887. CacheReloaded ();
  888. return pData;
  889. }
  890. FieldList* ParticleManager::Editor_CreateBillBoardParticle (const char* SystemName, const char*EmitterName, const char* ParticleName)
  891. {
  892. AssertCoreThread
  893. DataSource* pDataSource = pDataCache->GetParticleSystemDataSource (SystemName);
  894. if (!pDataSource) return NULL;
  895. FieldList* pData = pDataSource->CreateBillBoardParticle (ParticleName, EmitterName);
  896. CacheReloaded ();
  897. return pData;
  898. }
  899. FieldList* ParticleManager::Editor_CreateModelParticle (const char* SystemName, const char*EmitterName, const char* ParticleName)
  900. {
  901. AssertCoreThread
  902. DataSource* pDataSource = pDataCache->GetParticleSystemDataSource (SystemName);
  903. if (!pDataSource) return NULL;
  904. FieldList* pData = pDataSource->CreateModelParticle (ParticleName, EmitterName);
  905. CacheReloaded ();
  906. return pData;
  907. }
  908. void ParticleManager::DeletePointEmitter (const char* SystemName, IEmitter* pEmitter)
  909. {
  910. AssertCoreThread
  911. DataSource* pDataSource = pDataCache->GetParticleSystemDataSource (SystemName);
  912. if (!pDataSource) return;
  913. FieldList* pEmitterData = pEmitter->GetData();
  914. pDataSource->DeletePointEmitter (pEmitterData);
  915. CacheReloaded ();
  916. }
  917. void ParticleManager::DeleteBillboard (const char* SystemName, IEmitter* pEmitter, FieldList* pParticles)
  918. {
  919. AssertCoreThread
  920. DataSource* pDataSource = pDataCache->GetParticleSystemDataSource (SystemName);
  921. if (!pDataSource) return;
  922. FieldList* pEmitterData = pEmitter->GetData();
  923. pDataSource->DeleteBillboard (pEmitterData, pParticles);
  924. CacheReloaded ();
  925. }
  926. void ParticleManager::DeleteModel (const char* SystemName, IEmitter* pEmitter, FieldList* pParticles)
  927. {
  928. AssertCoreThread
  929. DataSource* pDataSource = pDataCache->GetParticleSystemDataSource (SystemName);
  930. if (!pDataSource) return;
  931. FieldList* pEmitterData = pEmitter->GetData();
  932. pDataSource->DeleteModel (pEmitterData, pParticles);
  933. CacheReloaded ();
  934. }
  935. IParticleSystem* ParticleManager::CreateParticleSystemEx2 (const char* FileName, const Matrix& mWorld, bool bAutoDelete, const char* File, int Line)
  936. {
  937. AssertCoreThread
  938. bIsActiveManager = true;
  939. fCreatedSystems += 1.0f;
  940. ProfileTimer pTimer;
  941. IParticleSystem* pSys = CreateParticleSystemEx(FileName, File, Line);
  942. if (pSys)
  943. {
  944. pSys->SetTransform(mWorld);
  945. pSys->AutoDelete(bAutoDelete);
  946. }
  947. pTimer.Stop();
  948. createTime += pTimer.GetTime();
  949. createCount++;
  950. if (framesPassed > DONT_COLLECT_STATS_FRAMES)
  951. {
  952. maxCreateTime = coremax(pTimer.GetTime(), maxCreateTime);
  953. }
  954. if (pTimer.GetTime() > 100000.0f)
  955. {
  956. //api->Trace("CreateParticleSystemEx2::Slow create '%s', %f", FileName, (double)pTimer.time64);
  957. }
  958. if (bAutoDelete)
  959. {
  960. return NULL;
  961. }
  962. return pSys;
  963. }
  964. dword ParticleManager::RegisterForceField (const char* szName, Object* _class, FORCEFIELD_PROCESS _method)
  965. {
  966. AssertCoreThread
  967. //Такой force field уже есть...
  968. int idx = GetForceFieldIndex(szName);
  969. if (idx >= 0) return ForceFields[idx].GUID;
  970. RegistredForceFields* pField = &ForceFields[ForceFields.Add()];
  971. pField->dwNameHash = string::HashNoCase(szName);
  972. pField->Name = szName;
  973. pField->pClass = _class;
  974. pField->pMethod = _method;
  975. pField->GUID = GlobalUniqueForceFieldIndex;
  976. GlobalUniqueForceFieldIndex++;
  977. pDataCache->NewForceFieldRegistred();
  978. return pField->GUID;
  979. }
  980. void ParticleManager::UnregisterAllForceFields(Object* _class)
  981. {
  982. AssertCoreThread
  983. for (dword i = 0; i < ForceFields.Size(); i++)
  984. {
  985. if (ForceFields[i].pClass == _class)
  986. {
  987. ForceFields.ExtractNoShift (i);
  988. i--;
  989. }
  990. }
  991. }
  992. void ParticleManager::UnregisterForceField (Object* _class, FORCEFIELD_PROCESS _method)
  993. {
  994. AssertCoreThread
  995. for (dword i = 0; i < ForceFields.Size(); i++)
  996. {
  997. if (ForceFields[i].pClass == _class && ForceFields[i].pMethod == _method)
  998. {
  999. ForceFields.ExtractNoShift (i);
  1000. return;
  1001. }
  1002. }
  1003. }
  1004. int ParticleManager::GetForceFieldIndex (const char* szName)
  1005. {
  1006. AssertCoreThread
  1007. dword dwSearchedHash = string::HashNoCase(szName);
  1008. for (dword i = 0; i < ForceFields.Size(); i++)
  1009. {
  1010. if (ForceFields[i].dwNameHash == dwSearchedHash)
  1011. {
  1012. if (ForceFields[i].Name == szName) return i;
  1013. }
  1014. }
  1015. return -1;
  1016. }
  1017. dword ParticleManager::GetForceFieldUniqueID (const char* szName)
  1018. {
  1019. AssertCoreThread
  1020. int idx = GetForceFieldIndex(szName);
  1021. if (idx >= 0) return ForceFields[idx].GUID;
  1022. return 0xFFFFFFFF;
  1023. }
  1024. bool ParticleManager::ExecuteForceFields (dword dwGUID1, dword dwGUID2, dword dwGUID3, dword dwGUID4, Vector& old_pos, Vector& pos, Vector& externalforce)
  1025. {
  1026. AssertCoreThread
  1027. dword dwForceFieldsCount = 0;
  1028. if (dwGUID1 != 0xFFFFFFFF) dwForceFieldsCount++;
  1029. if (dwGUID2 != 0xFFFFFFFF) dwForceFieldsCount++;
  1030. if (dwGUID3 != 0xFFFFFFFF) dwForceFieldsCount++;
  1031. if (dwGUID4 != 0xFFFFFFFF) dwForceFieldsCount++;
  1032. dword dwProcessedForceFields = 0;
  1033. for (dword i = 0; i < ForceFields.Size(); i++)
  1034. {
  1035. if (dwGUID1 != 0xFFFFFFFF)
  1036. {
  1037. if (ForceFields[i].GUID == dwGUID1)
  1038. {
  1039. bool bRes = (ForceFields[i].pClass->*ForceFields[i].pMethod) (ForceFields[i].GUID, old_pos, pos, externalforce);
  1040. if (bRes == false) return true;
  1041. dwProcessedForceFields++;
  1042. if (dwProcessedForceFields >= dwForceFieldsCount) return false;
  1043. }
  1044. }
  1045. if (dwGUID2 != 0xFFFFFFFF)
  1046. {
  1047. if (ForceFields[i].GUID == dwGUID2)
  1048. {
  1049. bool bRes = (ForceFields[i].pClass->*ForceFields[i].pMethod) (ForceFields[i].GUID, old_pos, pos, externalforce);
  1050. if (bRes == false) return true;
  1051. dwProcessedForceFields++;
  1052. if (dwProcessedForceFields >= dwForceFieldsCount) return false;
  1053. }
  1054. }
  1055. if (dwGUID3 != 0xFFFFFFFF)
  1056. {
  1057. if (ForceFields[i].GUID == dwGUID3)
  1058. {
  1059. bool bRes = (ForceFields[i].pClass->*ForceFields[i].pMethod) (ForceFields[i].GUID, old_pos, pos, externalforce);
  1060. if (bRes == false) return true;
  1061. dwProcessedForceFields++;
  1062. if (dwProcessedForceFields >= dwForceFieldsCount) return false;
  1063. }
  1064. }
  1065. if (dwGUID4 != 0xFFFFFFFF)
  1066. {
  1067. if (ForceFields[i].GUID == dwGUID4)
  1068. {
  1069. bool bRes = (ForceFields[i].pClass->*ForceFields[i].pMethod) (ForceFields[i].GUID, old_pos, pos, externalforce);
  1070. if (bRes == false) return true;
  1071. dwProcessedForceFields++;
  1072. if (dwProcessedForceFields >= dwForceFieldsCount) return false;
  1073. }
  1074. }
  1075. }
  1076. return false;
  1077. }
  1078. void ParticleManager::LoadAllSystems ()
  1079. {
  1080. AssertCoreThread
  1081. spriteProcessor->Clear ();
  1082. DeleteAllSystems ();
  1083. pDataCache->ResetCache();
  1084. pService->LoadAllfromDisk();
  1085. }
  1086. void ParticleManager::ReloadSystems ()
  1087. {
  1088. AssertCoreThread
  1089. spriteProcessor->Clear ();
  1090. pDataCache->ResetCache();
  1091. pService->LoadAllfromDisk();
  1092. string DataSourceName;
  1093. string ShortDataSourceName;
  1094. for (DWORD n = 0; n < ThreadParams.SystemsProxy.Size(); n++)
  1095. {
  1096. DataSourceName = ThreadParams.SystemsProxy[n]->GetName();
  1097. ShortDataSourceName.GetFileName(DataSourceName);
  1098. DataSource* pDataSource = pDataCache->GetParticleSystemDataSource (ShortDataSourceName.c_str());
  1099. ParticleSystem* sys = ThreadParams.SystemsProxy[n]->Lock();
  1100. sys->ReatachToDataSource(pDataSource);
  1101. ThreadParams.SystemsProxy[n]->Unlock();
  1102. }
  1103. }