EngineConfig.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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::LoadWindowConfig(const JSONValue& jwindow)
  145. {
  146. if (!jwindow.IsObject())
  147. return false;
  148. for (JSONObject::ConstIterator i = jwindow.Begin(); i != jwindow.End(); ++i)
  149. {
  150. String key = i->first_.ToLower();
  151. const JSONValue& jvalue = i->second_;
  152. if (key == "title")
  153. engineConfig_["WindowTitle"] = GetStringValue(jvalue, "Atomic");
  154. else if (key == "fullscreen")
  155. engineConfig_["FullScreen"] = GetBoolValue(jvalue, false);
  156. else if (key == "borderless")
  157. engineConfig_["Borderless"] = GetBoolValue(jvalue, false);
  158. else if (key == "resizable")
  159. engineConfig_["WindowResizable"] = GetBoolValue(jvalue, false);
  160. else if (key == "width")
  161. engineConfig_["WindowWidth"] = GetIntValue(jvalue, false);
  162. else if (key == "height")
  163. engineConfig_["WindowHeight"] = GetIntValue(jvalue, false);
  164. else if (key == "positionx")
  165. engineConfig_["WindowPositionX"] = GetIntValue(jvalue, false);
  166. else if (key == "positiony")
  167. engineConfig_["WindowPositionY"] = GetIntValue(jvalue, false);
  168. }
  169. return true;
  170. }
  171. bool EngineConfig::LoadSoundConfig(const JSONValue& jsound)
  172. {
  173. if (!jsound.IsObject())
  174. return false;
  175. for (JSONObject::ConstIterator i = jsound.Begin(); i != jsound.End(); ++i)
  176. {
  177. String key = i->first_.ToLower();
  178. const JSONValue& jvalue = i->second_;
  179. if (key == "enabled")
  180. engineConfig_["Sound"] = GetBoolValue(jvalue, true);
  181. else if (key == "interpolation")
  182. engineConfig_["SoundInterpolation"] = GetBoolValue(jvalue, true);
  183. else if (key == "stereo")
  184. engineConfig_["SoundStereo"] = GetBoolValue(jvalue, true);
  185. else if (key == "bufferms")
  186. engineConfig_["SoundBuffer"] = GetIntValue(jvalue, 100);
  187. else if (key == "mixrate")
  188. engineConfig_["SoundMixRate"] = GetIntValue(jvalue, 44100);
  189. }
  190. return true;
  191. }
  192. bool EngineConfig::LoadInputConfig(const JSONValue& jinput)
  193. {
  194. if (!jinput.IsObject())
  195. return false;
  196. for (JSONObject::ConstIterator i = jinput.Begin(); i != jinput.End(); ++i)
  197. {
  198. String key = i->first_.ToLower();
  199. const JSONValue& jvalue = i->second_;
  200. if (key == "touchemulation")
  201. engineConfig_["TouchEmulation"] = GetBoolValue(jvalue, false);
  202. }
  203. return true;
  204. }
  205. bool EngineConfig::LoadDesktopConfig(JSONValue root)
  206. {
  207. const JSONValue& jdesktop = root["desktop"];
  208. if (!jdesktop.IsObject())
  209. return false;
  210. const JSONValue& jengine = jdesktop["engine"];
  211. if (jengine.IsObject())
  212. LoadEngineConfig(jengine);
  213. const JSONValue& jgraphics = jdesktop["graphics"];
  214. if (jgraphics.IsObject())
  215. LoadGraphicsConfig(jgraphics);
  216. const JSONValue& jwindow = jdesktop["window"];
  217. if (jwindow.IsObject())
  218. LoadWindowConfig(jwindow);
  219. const JSONValue& jsound = jdesktop["sound"];
  220. if (jsound.IsObject())
  221. LoadSoundConfig(jsound);
  222. const JSONValue& jinput = jdesktop["input"];
  223. if (jinput.IsObject())
  224. LoadInputConfig(jinput);
  225. return true;
  226. }
  227. bool EngineConfig::LoadFromJSON(const String& json)
  228. {
  229. engineConfig_.Clear();
  230. JSONValue jroot;
  231. if (!JSONFile::ParseJSON(json, jroot))
  232. return false;
  233. if (!jroot.IsObject())
  234. return false;
  235. if (!LoadDesktopConfig(jroot))
  236. return false;
  237. return true;
  238. }
  239. bool EngineConfig::LoadFromFile(Context *context, const String& filename)
  240. {
  241. SharedPtr<File> file(new File(context));
  242. if (!file->Open(filename))
  243. return false;
  244. String json;
  245. file->ReadText(json);
  246. return LoadFromJSON(json);
  247. }
  248. void EngineConfig::ApplyConfig(VariantMap& settings)
  249. {
  250. VariantMap::ConstIterator itr = engineConfig_.Begin();
  251. while (itr != engineConfig_.End())
  252. {
  253. settings.InsertNew(itr->first_, itr->second_);
  254. itr++;
  255. }
  256. }
  257. }