RigsofRods
Soft-body Physics Simulation
SCfgFileManager.cpp
Go to the documentation of this file.
1 /*
2 --------------------------------------------------------------------------------
3 This source file is part of SkyX.
4 Visit ---
5 
6 Copyright (C) 2008 Xavier Verguín González <xavierverguin@hotmail.com>
7  <xavyiy@gmail.com>
8 
9 This program is free software; you can redistribute it and/or modify it under
10 the terms of the GNU Lesser General Public License as published by the Free Software
11 Foundation; either version 2 of the License, or (at your option) any later
12 version.
13 
14 This program is distributed in the hope that it will be useful, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
17 
18 You should have received a copy of the GNU Lesser General Public License along with
19 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
20 Place - Suite 330, Boston, MA 02111-1307, USA, or go to
21 http://www.gnu.org/copyleft/lesser.txt.
22 --------------------------------------------------------------------------------
23 */
24 
25 #include "SCfgFileManager.h"
26 #include "SkyX.h"
27 
28 namespace SkyX
29 {
31  : mSkyX(s), mController(c), mCamera(d)
32  {
33  }
34 
36  {
37  }
38 
39  const bool CfgFileManager::load(const Ogre::String& File) const
40  {
41  std::pair<bool, Ogre::ConfigFile> CfgFileResult;
42  _loadCfgFile(File, CfgFileResult);
43 
44  if (!CfgFileResult.first)
45  {
46  return false;
47  }
48 
49  Ogre::ConfigFile &CfgFile = CfgFileResult.second;
50 
51  if (!_checkVersion(CfgFile))
52  {
53  return false;
54  }
55 
56 
57  mSkyX->setTimeMultiplier(_getFloatValue(CfgFile, "TimeMultiplier"));
58  mController->setTime(_getVector3Value(CfgFile, "Time"));
59  mController->setMoonPhase(_getFloatValue(CfgFile, "MoonPhase"));
60 
63  _getFloatValue(CfgFile, "_InnerRadius"),
64  _getFloatValue(CfgFile, "_OuterRadius"),
65  _getFloatValue(CfgFile, "_HeightPosition"),
66  _getFloatValue(CfgFile, "_RayleighMultiplier"),
67  _getFloatValue(CfgFile, "_MieMultiplier"),
68  _getFloatValue(CfgFile, "_SunIntensity"),
69  _getVector3Value(CfgFile, "_WaveLength"),
70  _getFloatValue(CfgFile, "_G"),
71  _getFloatValue(CfgFile, "_Exposure"),
72  _getFloatValue(CfgFile, "_NumerOfSamples")
73  ));
74 
75  // Layered clouds
76  if (_getBoolValue(CfgFile, "layeredclouds") && !_getBoolValue(CfgFile, "volClouds"))
77  {
78  // Create layer cloud
79  if (mSkyX->getCloudsManager()->getCloudLayers().empty())
80  {
82  _getFloatValue(CfgFile, "lHeight"),
83  _getFloatValue(CfgFile, "Scale"),
84  _getVector2Value(CfgFile, "WindDirection"),
85  _getFloatValue(CfgFile, "TimeMultiplier"),
86  _getFloatValue(CfgFile, "DistanceAttenuation"),
87  _getFloatValue(CfgFile, "DetailAttenuation"),
88  _getFloatValue(CfgFile, "HeightVolume"),
89  _getFloatValue(CfgFile, "VolumetricDisplacement")
90  ));
91  }
92  }
93  else
94  {
95  mSkyX->getVCloudsManager()->setWindSpeed(_getFloatValue(CfgFile, "WindSpeed"));
96  mSkyX->getVCloudsManager()->setAutoupdate(_getBoolValue(CfgFile, "AutoUpdate"));
97  mSkyX->getVCloudsManager()->setHeight(_getVector2Value(CfgFile, "vHeight"));
98 
100 
101  vclouds->setWindDirection(_getDegreeValue(CfgFile, "WindDirection"));
102 
103  vclouds->setAmbientColor(_getVector3Value(CfgFile, "AmbientColor"));
104  vclouds->setLightResponse(_getVector4Value(CfgFile, "LightResponse"));
105  vclouds->setAmbientFactors(_getVector4Value(CfgFile, "AmbientFactors"));
106  vclouds->setWheater(_getVector2Value(CfgFile, "Wheater").x, _getVector2Value(CfgFile, "Wheater").y, _getBoolValue(CfgFile, "DelayedResponse"));
107 
108  // Create VClouds
109  if (!mSkyX->getVCloudsManager()->isCreated())
110  {
111  // SkyX::MeshManager::getSkydomeRadius(...) works for both finite and infinite(=0) camera far clip distances
113  }
114 
115  vclouds->getLightningManager()->setEnabled(_getBoolValue(CfgFile, "lightnings"));
116 
117  if (vclouds->getLightningManager()->isEnabled())
118  {
119  vclouds->getLightningManager()->setAverageLightningApparitionTime(_getFloatValue(CfgFile, "AverageLightningApparitionTime"));
120  vclouds->getLightningManager()->setLightningColor(_getVector3Value(CfgFile, "LightningColor"));
121  vclouds->getLightningManager()->setLightningTimeMultiplier(_getFloatValue(CfgFile, "LightningTimeMultiplier"));
122  }
123  }
124  return true;
125  }
126 
127  const bool CfgFileManager::save(const Ogre::String& File, const Ogre::String& Path) const
128  {
129  Ogre::String Data =
130  "#SkyX cfg file.\n\n";
131 
132  Data += "#SkyX version field\n";
133  Data += _getVersionCfgString();
134 
135  return _saveToFile(Data, File, Path);
136  }
137 
138  const bool CfgFileManager::_saveToFile(const Ogre::String& Data, const Ogre::String& File, const Ogre::String& Path) const
139  {
140  FILE *DestinationFile = fopen((Path+"/"+File).c_str(), "w");
141 
142  if (!DestinationFile)
143  {
144  return false;
145  }
146 
147  fprintf(DestinationFile, "%s", Data.c_str());
148  fclose(DestinationFile);
149 
150  SkyXLOG(File + " saved in " + Path + " .");
151 
152  return true;
153  }
154 
155  const void CfgFileManager::_loadCfgFile(const Ogre::String& File, std::pair<bool,Ogre::ConfigFile> &Result) const
156  {
157  try
158  {
159  Result.second.load(Ogre::ResourceGroupManager::getSingleton().openResource(File, Ogre::ResourceGroupManager::AUTODETECT_RESOURCE_GROUP_NAME));
160  Result.first = true;
161 
162  } catch(...)
163  {
164  SkyXLOG("CfgFileManager::_getCfgFile(...): " + File + " not found in any resource group.");
165  Result.first = false;
166  }
167 
168  SkyXLOG(File + " loaded.");
169  }
170 
171  Ogre::String CfgFileManager::_getCfgString(const Ogre::String &Name, const int &Value)
172  {
173  return "<int>" + Name + "=" + Ogre::StringConverter::toString(Value) + "\n";
174  }
175 
176  Ogre::String CfgFileManager::_getCfgString(const Ogre::String &Name, const Ogre::Real &Value)
177  {
178  return "<float>" + Name + "=" + Ogre::StringConverter::toString(Value) + "\n";
179  }
180 
181  Ogre::String CfgFileManager::_getCfgString(const Ogre::String &Name, const bool &Value)
182  {
183  return "<bool>" + Name + "=" + Ogre::StringConverter::toString(Value) + "\n";
184  }
185 
186  Ogre::String CfgFileManager::_getCfgString(const Ogre::String &Name, const Ogre::Vector2 &Value)
187  {
188  return "<vector2>" + Name + "=" + Ogre::StringConverter::toString(Value.x) + "x" + Ogre::StringConverter::toString(Value.y) + "\n";
189  }
190 
191  Ogre::String CfgFileManager::_getCfgString(const Ogre::String &Name, const Ogre::Vector3 &Value)
192  {
193  return "<vector3>" + Name + "=" + Ogre::StringConverter::toString(Value.x) + "x" + Ogre::StringConverter::toString(Value.y) + "x" + Ogre::StringConverter::toString(Value.z) + "\n";
194  }
195 
196  Ogre::String CfgFileManager::_getCfgString(const Ogre::String &Name, const Ogre::Vector4 &Value)
197  {
198  return "<vector4>" + Name + "=" + Ogre::StringConverter::toString(Value.x) + "x" + Ogre::StringConverter::toString(Value.y) + "x" + Ogre::StringConverter::toString(Value.z) + "x" + Ogre::StringConverter::toString(Value.w) + "\n";
199  }
200 
201  Ogre::String CfgFileManager::_getCfgString(const Ogre::String &Name, const Ogre::Degree &Value)
202  {
203  return "<degree>" + Name + "=" + Ogre::StringConverter::toString(Value) + "\n";
204  }
205 
206  bool CfgFileManager::_isStringInList(const Ogre::StringVector &List, const Ogre::String &Find)
207  {
208  for (unsigned int k = 0; k < List.size(); k++)
209  {
210  if (List[k] == Find)
211  {
212  return true;
213  }
214  }
215 
216  return false;
217  }
218 
219  const Ogre::String CfgFileManager::_getVersionCfgString() const
220  {
221  return "SkyXVersion=" +
222  // Major
223  Ogre::StringConverter::toString(SKYX_VERSION_MAJOR)+"." +
224  // Minor
225  Ogre::StringConverter::toString(SKYX_VERSION_MINOR) + "." +
226  // Patch
227  Ogre::StringConverter::toString(SKYX_VERSION_PATCH) + "\n\n";
228  }
229 
230  const bool CfgFileManager::_checkVersion(Ogre::ConfigFile& CfgFile) const
231  {
232  // accept any
233 #if 0
234  if(CfgFile.getSetting("HydraxVersion") != (
235  // Major
236  Ogre::StringConverter::toString(HYDRAX_VERSION_MAJOR)+"."+
237  // Minor
238  Ogre::StringConverter::toString(HYDRAX_VERSION_MINOR)+"."+
239  // Patch
240  Ogre::StringConverter::toString(HYDRAX_VERSION_PATCH)))
241  {
242  HydraxLOG("Config file version doesn't correspond with Hydrax version.");
243 
244  return false;
245  }
246 #endif // 0
247  return true;
248  }
249 
250  int CfgFileManager::_getIntValue(Ogre::ConfigFile& CfgFile, const Ogre::String Name)
251  {
252  Ogre::String Value = CfgFile.getSetting("<int>" + Name);
253 
254  if (Value == "")
255  {
256  return 0;
257  }
258  else
259  {
260  return Ogre::StringConverter::parseInt(Value);
261  }
262  }
263 
264  Ogre::Real CfgFileManager::_getFloatValue(Ogre::ConfigFile& CfgFile, const Ogre::String Name)
265  {
266  Ogre::String Value = CfgFile.getSetting("<float>" + Name);
267 
268  if (Value == "")
269  {
270  return 0;
271  }
272  else
273  {
274  return Ogre::StringConverter::parseReal(Value);
275  }
276  }
277 
278  bool CfgFileManager::_getBoolValue(Ogre::ConfigFile& CfgFile, const Ogre::String Name)
279  {
280  Ogre::String Value = CfgFile.getSetting("<bool>" + Name);
281 
282  if (Value == "")
283  {
284  return false;
285  }
286  else
287  {
288  return Ogre::StringConverter::parseBool(Value);
289  }
290  }
291 
292  Ogre::Vector2 CfgFileManager::_getVector2Value(Ogre::ConfigFile& CfgFile, const Ogre::String Name)
293  {
294  Ogre::String Value = CfgFile.getSetting("<vector2>" + Name);
295 
296  if (Value == "")
297  {
298  return Ogre::Vector2(0,0);
299  }
300  else
301  {
302  return Ogre::Vector2(Ogre::StringConverter::parseReal(Ogre::StringUtil::split(Value, "x")[0]),
303  Ogre::StringConverter::parseReal(Ogre::StringUtil::split(Value, "x")[1]));
304  }
305  }
306 
307  Ogre::Vector3 CfgFileManager::_getVector3Value(Ogre::ConfigFile& CfgFile, const Ogre::String Name)
308  {
309  Ogre::String Value = CfgFile.getSetting("<vector3>" + Name);
310 
311  if (Value == "")
312  {
313  return Ogre::Vector3(0,0,0);
314  }
315  else
316  {
317  return Ogre::Vector3(Ogre::StringConverter::parseReal(Ogre::StringUtil::split(Value, "x")[0]),
318  Ogre::StringConverter::parseReal(Ogre::StringUtil::split(Value, "x")[1]),
319  Ogre::StringConverter::parseReal(Ogre::StringUtil::split(Value, "x")[2]));
320  }
321  }
322 
323  Ogre::Vector4 CfgFileManager::_getVector4Value(Ogre::ConfigFile& CfgFile, const Ogre::String Name)
324  {
325  Ogre::String Value = CfgFile.getSetting("<vector4>" + Name);
326 
327  if (Value == "")
328  {
329  return Ogre::Vector4(0, 0, 0, 0);
330  }
331  else
332  {
333  return Ogre::Vector4(
334  Ogre::StringConverter::parseReal(Ogre::StringUtil::split(Value, "x")[0]),
335  Ogre::StringConverter::parseReal(Ogre::StringUtil::split(Value, "x")[1]),
336  Ogre::StringConverter::parseReal(Ogre::StringUtil::split(Value, "x")[2]),
337  Ogre::StringConverter::parseReal(Ogre::StringUtil::split(Value, "x")[3])
338  );
339  }
340  }
341 
342  Ogre::Degree CfgFileManager::_getDegreeValue(Ogre::ConfigFile& CfgFile, const Ogre::String Name)
343  {
344  Ogre::String Value = CfgFile.getSetting("<degree>" + Name);
345 
346  if (Value == "")
347  {
348  return Ogre::Degree(0);
349  }
350  else
351  {
352  return Ogre::Degree(Ogre::StringConverter::parseReal(Value));
353  }
354  }
355 }
SkyX::CfgFileManager::mController
BasicController * mController
Definition: SCfgFileManager.h:129
SkyX::BasicController::setMoonPhase
void setMoonPhase(const Ogre::Real &mp)
Set moon phase.
Definition: BasicController.h:102
SkyX::VCloudsManager::getVClouds
VClouds::VClouds * getVClouds()
Get VClouds.
Definition: VCloudsManager.h:155
y
float y
Definition: (ValueTypes) quaternion.h:6
SkyX::BasicController
Basic controller class
Definition: BasicController.h:35
SkyX::CfgFileManager::_isStringInList
static bool _isStringInList(const Ogre::StringVector &List, const Ogre::String &Find)
Definition: SCfgFileManager.cpp:206
SkyX::CfgFileManager::_getBoolValue
static bool _getBoolValue(Ogre::ConfigFile &CfgFile, const Ogre::String Name)
Get bool value.
Definition: SCfgFileManager.cpp:278
SkyX::VClouds::VClouds::setLightResponse
void setLightResponse(const Ogre::Vector4 &LightResponse)
Set light response.
Definition: VClouds.cpp:287
SkyX::BasicController::setTime
void setTime(const Ogre::Vector3 &t)
Set time.
Definition: BasicController.h:52
SKYX_VERSION_MAJOR
#define SKYX_VERSION_MAJOR
SkyX defines.
Definition: Prerequisites.h:37
SkyX::CfgFileManager::_saveToFile
const bool _saveToFile(const Ogre::String &Data, const Ogre::String &File, const Ogre::String &Path) const
Save a string in file.
Definition: SCfgFileManager.cpp:138
SkyX::VClouds::LightningManager::setEnabled
void setEnabled(const bool &enable)
Enable or disable the lightning system.
Definition: LightningManager.cpp:281
SkyX::VClouds::LightningManager::isEnabled
const bool & isEnabled() const
Get whether the lightning system is enabled or not.
Definition: LightningManager.h:127
SkyX::CloudLayer::Options
Cloud layer options.
Definition: CloudsManager.h:40
SkyX::CfgFileManager::_getVector4Value
static Ogre::Vector4 _getVector4Value(Ogre::ConfigFile &CfgFile, const Ogre::String Name)
Definition: SCfgFileManager.cpp:323
SkyX
Definition: AtmosphereManager.cpp:30
SkyX::SkyX::getMeshManager
MeshManager * getMeshManager()
Get mesh manager.
Definition: SkyX.h:183
SkyX::CfgFileManager::_getVector2Value
static Ogre::Vector2 _getVector2Value(Ogre::ConfigFile &CfgFile, const Ogre::String Name)
Get vector2 value.
Definition: SCfgFileManager.cpp:292
SkyX::MeshManager::getSkydomeRadius
const float getSkydomeRadius(Ogre::Camera *c) const
Get skydome radius.
Definition: MeshManager.cpp:360
SkyX::VClouds::LightningManager::setLightningColor
void setLightningColor(const Ogre::Vector3 &c)
Set lightning color.
Definition: LightningManager.cpp:253
SkyX::CfgFileManager::_getFloatValue
static Ogre::Real _getFloatValue(Ogre::ConfigFile &CfgFile, const Ogre::String Name)
Get float value.
Definition: SCfgFileManager.cpp:264
SkyX.h
SkyX::CloudsManager::getCloudLayers
const std::vector< CloudLayer * > & getCloudLayers() const
Get cloud layers.
Definition: CloudsManager.h:270
SkyX::SkyX::getCloudsManager
CloudsManager * getCloudsManager()
Get clouds manager.
Definition: SkyX.h:215
SkyX::AtmosphereManager::Options
Atmosphere options.
Definition: AtmosphereManager.h:39
SkyX::VClouds::VClouds::getLightningManager
LightningManager * getLightningManager()
Get lightning manager.
Definition: VClouds.h:491
SkyX::VCloudsManager::setAutoupdate
void setAutoupdate(const bool &Autoupdate)
Autoupdate volumetric clouds wind depending of the SkyX time multiplier.
Definition: VCloudsManager.h:122
SkyX::VClouds::LightningManager::setAverageLightningApparitionTime
void setAverageLightningApparitionTime(const Ogre::Real &alat)
Set average lightning apparition time.
Definition: LightningManager.h:165
SkyX::CfgFileManager::load
const bool load(const Ogre::String &File) const
Load hydrax cfg file.
Definition: SCfgFileManager.cpp:39
SkyX::SkyX::getAtmosphereManager
AtmosphereManager * getAtmosphereManager()
Get atmosphere manager.
Definition: SkyX.h:191
SkyX::SkyX::setTimeMultiplier
void setTimeMultiplier(const Ogre::Real &TimeMultiplier)
Set time multiplier.
Definition: SkyX.h:166
SkyX::CfgFileManager::_getVector3Value
static Ogre::Vector3 _getVector3Value(Ogre::ConfigFile &CfgFile, const Ogre::String Name)
Get vector3 value.
Definition: SCfgFileManager.cpp:307
HydraxLOG
#define HydraxLOG(msg)
Definition: Application.h:59
SkyX::CloudsManager::add
CloudLayer * add(const CloudLayer::Options &o)
Add a cloud layer.
Definition: CloudsManager.cpp:220
SkyX::VCloudsManager::setHeight
void setHeight(const Ogre::Vector2 &Height)
Set height parameters.
Definition: VCloudsManager.h:106
SkyX::CfgFileManager::mCamera
Ogre::Camera * mCamera
Definition: SCfgFileManager.h:130
SkyX::AtmosphereManager::setOptions
void setOptions(const Options &_Options)
Set options.
Definition: AtmosphereManager.h:131
SkyX::CfgFileManager::_loadCfgFile
const void _loadCfgFile(const Ogre::String &File, std::pair< bool, Ogre::ConfigFile > &Result) const
Load a cfg file in an Ogre::ConfigFile.
Definition: SCfgFileManager.cpp:155
SkyX::CfgFileManager::CfgFileManager
CfgFileManager(SkyX *s, BasicController *c, Ogre::Camera *d)
Constructor.
Definition: SCfgFileManager.cpp:30
SkyX::CfgFileManager::_getVersionCfgString
const Ogre::String _getVersionCfgString() const
Get hydrax version cfg string.
Definition: SCfgFileManager.cpp:219
SkyX::CfgFileManager::_getCfgString
static Ogre::String _getCfgString(const Ogre::String &Name, const int &Value)
Definition: SCfgFileManager.cpp:171
SkyX::VClouds::VClouds
Definition: VClouds.h:35
SkyX::VClouds::VClouds::setAmbientFactors
void setAmbientFactors(const Ogre::Vector4 &AmbientFactors)
Set ambient factors.
Definition: VClouds.cpp:302
SkyX::VCloudsManager::isCreated
const bool & isCreated() const
Is moon manager created?
Definition: VCloudsManager.h:163
SKYX_VERSION_MINOR
#define SKYX_VERSION_MINOR
Definition: Prerequisites.h:38
SkyX::VCloudsManager::create
void create(const Ogre::Real &radius=-1)
Create all resources.
Definition: VCloudsManager.cpp:71
SKYX_VERSION_PATCH
#define SKYX_VERSION_PATCH
Definition: Prerequisites.h:39
SkyX::VClouds::VClouds::setWheater
void setWheater(const float &Humidity, const float &AverageCloudsSize, const bool &DelayedResponse)
Set wheater parameters Use this funtion to update the cloud field parameters, you'll get a smart and ...
Definition: VClouds.cpp:317
SkyX::CfgFileManager::_checkVersion
const bool _checkVersion(Ogre::ConfigFile &CfgFile) const
Check hydrax version cfg file.
Definition: SCfgFileManager.cpp:230
SCfgFileManager.h
SkyXLOG
#define SkyXLOG(msg)
Include external headers.
Definition: Prerequisites.h:34
HYDRAX_VERSION_MINOR
#define HYDRAX_VERSION_MINOR
Definition: Prerequisites.h:33
SkyX::CfgFileManager::_getDegreeValue
static Ogre::Degree _getDegreeValue(Ogre::ConfigFile &CfgFile, const Ogre::String Name)
Definition: SCfgFileManager.cpp:342
HYDRAX_VERSION_MAJOR
#define HYDRAX_VERSION_MAJOR
Include external headers.
Definition: Prerequisites.h:32
SkyX::CfgFileManager::mSkyX
SkyX * mSkyX
Hydrax parent pointer.
Definition: SCfgFileManager.h:128
SkyX::CfgFileManager::~CfgFileManager
~CfgFileManager()
Destructor.
Definition: SCfgFileManager.cpp:35
SkyX::VClouds::VClouds::setWindDirection
void setWindDirection(const Ogre::Radian &WindDirection)
Set wind direction.
Definition: VClouds.h:268
SkyX::CfgFileManager::_getIntValue
static int _getIntValue(Ogre::ConfigFile &CfgFile, const Ogre::String Name)
Get int value.
Definition: SCfgFileManager.cpp:250
HYDRAX_VERSION_PATCH
#define HYDRAX_VERSION_PATCH
Definition: Prerequisites.h:34
SkyX::CfgFileManager::save
const bool save(const Ogre::String &File, const Ogre::String &Path="") const
Save current hydrax config to a file.
Definition: SCfgFileManager.cpp:127
x
float x
Definition: (ValueTypes) quaternion.h:5
SkyX::VClouds::LightningManager::setLightningTimeMultiplier
void setLightningTimeMultiplier(const Ogre::Real &m)
Set lightning time multiplier.
Definition: LightningManager.h:149
SkyX::SkyX::getVCloudsManager
VCloudsManager * getVCloudsManager()
Get volumetric clouds manager.
Definition: SkyX.h:223
SkyX::VClouds::VClouds::setAmbientColor
void setAmbientColor(const Ogre::Vector3 &AmbientColor)
Set ambient color.
Definition: VClouds.cpp:272
SkyX::VCloudsManager::setWindSpeed
void setWindSpeed(const Ogre::Real &WindSpeed)
Set wind speed.
Definition: VCloudsManager.h:139