EngineConfig.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. //
  2. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "../IO/File.h"
  23. #include "../Resource/JSONFile.h"
  24. #include "../Graphics/GraphicsDefs.h"
  25. #include "EngineConfig.h"
  26. namespace Atomic
  27. {
  28. VariantMap EngineConfig::engineConfig_;
  29. bool EngineConfig::GetBoolValue(const JSONValue& jvalue, bool defaultValue)
  30. {
  31. bool value = defaultValue;
  32. if (jvalue.IsBool())
  33. value = jvalue.GetBool();
  34. return value;
  35. }
  36. int EngineConfig::GetIntValue(const JSONValue& jvalue, int defaultValue)
  37. {
  38. int value = defaultValue;
  39. if (jvalue.IsNumber())
  40. value = jvalue.GetInt();
  41. return value;
  42. }
  43. String EngineConfig::GetStringValue(const JSONValue& jvalue, const String& defaultValue)
  44. {
  45. String value = defaultValue;
  46. if (jvalue.IsString())
  47. value = jvalue.GetString();
  48. return value;
  49. }
  50. bool EngineConfig::LoadEngineConfig(const JSONValue& jengine)
  51. {
  52. if (!jengine.IsObject())
  53. return false;
  54. for (JSONObject::ConstIterator i = jengine.Begin(); i != jengine.End(); ++i)
  55. {
  56. String key = i->first_.ToLower();
  57. const JSONValue& jvalue = i->second_;
  58. if (key == "workerthreads")
  59. engineConfig_["WorkerThreads"] = GetBoolValue(jvalue, true);
  60. else if (key == "logquiet")
  61. engineConfig_["LogQuiet"] = GetBoolValue(jvalue, false);
  62. else if (key == "loglevel")
  63. engineConfig_["LogLevel"] = GetIntValue(jvalue, 1);
  64. }
  65. return true;
  66. }
  67. bool EngineConfig::LoadGraphicsConfig(const JSONValue& jgraphics)
  68. {
  69. if (!jgraphics.IsObject())
  70. return false;
  71. for (JSONObject::ConstIterator i = jgraphics.Begin(); i != jgraphics.End(); ++i)
  72. {
  73. String key = i->first_.ToLower();
  74. const JSONValue& jvalue = i->second_;
  75. if (key == "headless")
  76. engineConfig_["Headless"] = GetBoolValue(jvalue, false);
  77. else if (key == "framelimiter")
  78. engineConfig_["FrameLimiter"] = GetBoolValue(jvalue, true);
  79. else if (key == "flushgpu")
  80. engineConfig_["FlushGPU"] = GetBoolValue(jvalue, false);
  81. else if (key == "forcegl2")
  82. engineConfig_["ForceGL2"] = GetBoolValue(jvalue, false);
  83. else if (key == "orientations")
  84. engineConfig_["Orientations"] = GetStringValue(jvalue, "LandscapeLeft LandscapeRight");
  85. else if (key == "vsync")
  86. engineConfig_["VSync"] = GetBoolValue(jvalue, false);
  87. else if (key == "triplebuffer")
  88. engineConfig_["TripleBuffer"] = GetBoolValue(jvalue, false);
  89. else if (key == "multisample")
  90. engineConfig_["Multisample"] = GetIntValue(jvalue, 1);
  91. else if (key == "renderpath")
  92. {
  93. String renderPath = GetStringValue(jvalue, "forward").ToLower();
  94. if (renderPath == "forward")
  95. engineConfig_["RenderPath"] = "RenderPaths/Forward.xml";
  96. else if (renderPath == "prepass")
  97. engineConfig_["RenderPath"] = "RenderPaths/Prepass.xml";
  98. else if (renderPath == "deferred")
  99. engineConfig_["RenderPath"] = "RenderPaths/Deferred.xml";
  100. }
  101. else if (key == "shadows")
  102. engineConfig_["Shadows"] = GetBoolValue(jvalue, true);
  103. else if (key == "lowqualityshadows")
  104. engineConfig_["LowQualityShadows"] = GetBoolValue(jvalue, false);
  105. else if (key == "materialquality")
  106. {
  107. String quality = GetStringValue(jvalue, "high").ToLower();
  108. if (quality == "high")
  109. engineConfig_["MaterialQuality"] = QUALITY_HIGH;
  110. else if (quality == "medium")
  111. engineConfig_["MaterialQuality"] = QUALITY_MEDIUM;
  112. else if (quality == "low")
  113. engineConfig_["MaterialQuality"] = QUALITY_LOW;
  114. }
  115. else if (key == "texturequality")
  116. {
  117. String quality = GetStringValue(jvalue, "high").ToLower();
  118. if (quality == "high")
  119. engineConfig_["TextureQuality"] = QUALITY_HIGH;
  120. else if (quality == "medium")
  121. engineConfig_["TextureQuality"] = QUALITY_MEDIUM;
  122. else if (quality == "low")
  123. engineConfig_["TextureQuality"] = QUALITY_LOW;
  124. }
  125. else if (key == "texturefiltermode")
  126. {
  127. String mode = GetStringValue(jvalue, "trilinear").ToLower();
  128. if (mode == "trilinear")
  129. engineConfig_["TextureFilterMode"] = FILTER_TRILINEAR;
  130. else if (mode == "bilinear")
  131. engineConfig_["TextureFilterMode"] = FILTER_BILINEAR;
  132. else if (mode == "nearest")
  133. engineConfig_["TextureFilterMode"] = FILTER_NEAREST;
  134. else if (mode == "anisotropic")
  135. engineConfig_["TextureFilterMode"] = FILTER_ANISOTROPIC;
  136. }
  137. else if (key == "textureanisotropy")
  138. {
  139. engineConfig_["TextureAnisotropy"] = GetIntValue(jvalue, 4);
  140. }
  141. }
  142. return true;
  143. }
  144. bool EngineConfig::LoadDesktopConfig(JSONValue root)
  145. {
  146. const JSONValue& jdesktop = root["desktop"];
  147. if (!jdesktop.IsObject())
  148. return false;
  149. const JSONValue& jengine = jdesktop["engine"];
  150. if (jengine.IsObject())
  151. LoadEngineConfig(jengine);
  152. const JSONValue& jgraphics = jdesktop["graphics"];
  153. if (jgraphics.IsObject())
  154. LoadGraphicsConfig(jgraphics);
  155. return true;
  156. }
  157. bool EngineConfig::LoadFromJSON(const String& json)
  158. {
  159. engineConfig_.Clear();
  160. JSONValue jroot;
  161. if (!JSONFile::ParseJSON(json, jroot))
  162. return false;
  163. if (!jroot.IsObject())
  164. return false;
  165. LoadDesktopConfig(jroot);
  166. return true;
  167. }
  168. bool EngineConfig::LoadFromFile(Context *context, const String& filename)
  169. {
  170. SharedPtr<File> file(new File(context));
  171. if (!file->Open(filename))
  172. return false;
  173. String json;
  174. file->ReadText(json);
  175. return LoadFromJSON(json);
  176. }
  177. void EngineConfig::ApplyConfig(VariantMap& settings)
  178. {
  179. VariantMap::ConstIterator itr = engineConfig_.Begin();
  180. while (itr != engineConfig_.End())
  181. {
  182. settings.InsertNew(itr->first_, itr->second_);
  183. itr++;
  184. }
  185. }
  186. }