Merge branch 'ryzom/ark-features' into main/gingo-test
[ryzomcore.git] / nel / src / 3d / water_pool_manager.cpp
blob361264f81d8813080eb19c3efb19b380f34cb21e
1 // NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
2 // Copyright (C) 2010 Winch Gate Property Limited
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as
6 // published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Affero General Public License for more details.
14 // You should have received a copy of the GNU Affero General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #include "std3d.h"
19 #include "nel/3d/water_pool_manager.h"
20 #include "nel/3d/texture_blend.h"
21 #include "nel/3d/water_shape.h"
22 #include "nel/misc/command.h"
23 #include "nel/3d/water_height_map.h"
26 #ifdef DEBUG_NEW
27 #define new DEBUG_NEW
28 #endif
30 namespace NL3D {
34 NLMISC_COMMAND(setWaterPool, "Setup a pool of water in the water pool manager",
35 "<ID>,<Size>,<Damping>,<FilterWeight>,<UnitSize>,<WaveIntensity>,<WavePeriod>")
37 CWaterPoolManager::CWaterHeightMapBuild whmb;
38 const uint numArgs = args.size();
39 if (numArgs == 0) return false;
40 if (numArgs == 1)
42 NLMISC::fromString(args[0], whmb.ID);
44 if (numArgs == 2)
46 NLMISC::fromString(args[1], whmb.Size);
48 if (numArgs == 3)
50 NLMISC::fromString(args[2], whmb.FilterWeight);
52 if (numArgs == 4)
54 NLMISC::fromString(args[3], whmb.UnitSize);
56 if (numArgs == 5)
58 NLMISC::fromString(args[4], whmb.WaveIntensity);
60 if (numArgs == 4)
62 NLMISC::fromString(args[5], whmb.WavePeriod);
64 // create the water pool
65 GetWaterPoolManager().createWaterPool(whmb);
66 return true;
70 //===============================================================================================
72 CWaterPoolManager &GetWaterPoolManager()
74 static CWaterPoolManager singleton;
75 return singleton;
78 //===============================================================================================
80 bool CWaterPoolManager::hasPool(uint32 ID) const
82 return _PoolMap.count(ID) != 0;
85 //===============================================================================================
87 CWaterHeightMap *CWaterPoolManager::createWaterPool(const CWaterHeightMapBuild &params)
89 CWaterHeightMap *whm = _PoolMap.count(params.ID) == 0 ? new CWaterHeightMap : _PoolMap[params.ID];
90 whm->setDamping(params.Damping);
91 whm->setFilterWeight(params.FilterWeight);
92 whm->setSize(params.Size);
93 whm->setUnitSize(params.UnitSize);
94 whm->setWaves(params.WaveIntensity, params.WavePeriod, params.WaveRadius, params.BorderWaves);
95 whm->enableWaves(params.WavesEnabled);
96 whm->setName(params.Name);
97 _PoolMap[params.ID] = whm; // in case it was just created
98 return whm;
101 //===============================================================================================
103 CWaterHeightMap &CWaterPoolManager::getPoolByID(uint32 ID)
105 if(_PoolMap.count(ID))
107 return *_PoolMap[ID];
109 else
111 return *createWaterPool();
115 //===============================================================================================
117 void CWaterPoolManager::reset()
119 for (TPoolMap::iterator it = _PoolMap.begin(); it != _PoolMap.end(); ++it)
121 delete it->second;
123 _PoolMap.clear ();
127 //===============================================================================================
129 void CWaterPoolManager::registerWaterShape(CWaterShape *shape)
131 nlassert(std::find(_WaterShapes.begin(), _WaterShapes.end(), shape) == _WaterShapes.end()); // Shape registered twice!
132 _WaterShapes.push_back(shape);
135 //===============================================================================================
137 void CWaterPoolManager::unRegisterWaterShape(CWaterShape *shape)
139 TWaterShapeVect::iterator it = std::find(_WaterShapes.begin(), _WaterShapes.end(), shape);
140 // nlassert(it != _WaterShapes.end()); // shape not registered!
141 if (it != _WaterShapes.end())
142 _WaterShapes.erase(it);
145 //===============================================================================================
146 void CWaterPoolManager::setBlendFactor(IDriver *drv, float factor)
148 nlassert(factor >= 0 && factor <= 1);
149 for (TWaterShapeVect::iterator it = _WaterShapes.begin(); it != _WaterShapes.end(); ++it)
151 CTextureBlend *tb;
152 for (uint k = 0; k < 2; ++k)
154 tb = dynamic_cast<CTextureBlend *>((*it)->getEnvMap(k));
155 if (tb && tb->setBlendFactor((uint16) (256.f * factor)))
157 tb->setReleasable(false);
158 drv->setupTexture(*tb);
164 //===============================================================================================
165 void CWaterPoolManager::releaseBlendTextures()
167 for (TWaterShapeVect::iterator it = _WaterShapes.begin(); it != _WaterShapes.end(); ++it)
169 CTextureBlend *tb;
170 for (uint k = 0; k < 2; ++k)
172 tb = dynamic_cast<CTextureBlend *>((*it)->getEnvMap(k));
173 if (tb)
175 tb->setReleasable(true);
176 tb->release();
182 //===============================================================================================
184 bool CWaterPoolManager::isWaterShapeObserver(const CWaterShape *shape) const
186 return std::find(_WaterShapes.begin(), _WaterShapes.end(), shape) != _WaterShapes.end();
189 //===============================================================================================
191 uint CWaterPoolManager::getNumPools() const
193 return (uint)_PoolMap.size();
196 //===============================================================================================
198 uint CWaterPoolManager::getPoolID(uint i) const
200 nlassert(i < getNumPools());
201 TPoolMap::const_iterator it = _PoolMap.begin();
202 while (i--) ++it;
203 return it->first;
206 //===============================================================================================
208 void CWaterPoolManager::removePool(uint32 ID)
210 nlassert(hasPool(ID));
211 TPoolMap::iterator it = _PoolMap.find(ID);
212 delete it->second;
213 _PoolMap.erase(_PoolMap.find(ID));
216 //===============================================================================================
217 void CWaterPoolManager::serial(NLMISC::IStream &f)
219 f.xmlPush("WaterPoolManager");
220 (void)f.serialVersion(0);
221 uint32 size;
222 TPoolMap::iterator it;
223 if (!f.isReading())
225 size = (uint32)_PoolMap.size();
226 it = _PoolMap.begin();
228 else
230 reset();
232 f.xmlSerial(size, "NUM_POOLS");
233 while (size --)
235 f.xmlPush("PoolDesc");
236 if (f.isReading())
238 CWaterHeightMap *whm = NULL;
239 uint32 id;
240 f.xmlSerial(id, "POOL_ID");
241 f.serialPtr(whm);
242 _PoolMap[id] = whm;
244 else
246 uint32 id = it->first;
247 f.xmlSerial(id, "POOL_ID");
248 f.serialPtr(it->second);
249 ++it;
251 f.xmlPop();
253 f.xmlPop();
256 } // NL3D