PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/Source/User/Settings.cpp

https://gitlab.com/akien/lugaru
C++ | 328 lines | 289 code | 17 blank | 22 comment | 111 complexity | ea0334692d332ccda0165d686cbcf77b MD5 | raw file
  1. /*
  2. Copyright (C) 2003, 2010 - Wolfire Games
  3. Copyright (C) 2010-2017 - Lugaru contributors (see AUTHORS file)
  4. This file is part of Lugaru.
  5. Lugaru is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. Lugaru 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 General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with Lugaru. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include "User/Settings.hpp"
  17. #include "Game.hpp"
  18. #include "Utils/Folders.hpp"
  19. #include "Utils/Input.hpp"
  20. using namespace Game;
  21. void DefaultSettings()
  22. {
  23. detail = 2;
  24. ismotionblur = 1;
  25. usermousesensitivity = 1;
  26. newscreenwidth = kContextWidth = 1024;
  27. newscreenheight = kContextHeight = 768;
  28. fullscreen = 0;
  29. floatjump = 0;
  30. autoslomo = 1;
  31. decalstoggle = true;
  32. invertmouse = 0;
  33. bloodtoggle = 0;
  34. foliage = 1;
  35. musictoggle = 1;
  36. trilinear = 1;
  37. gamespeed = 1;
  38. damageeffects = 0;
  39. texttoggle = 1;
  40. alwaysblur = 0;
  41. showpoints = 0;
  42. showdamagebar = 0;
  43. immediate = 0;
  44. velocityblur = 0;
  45. volume = 0.8f;
  46. ambientsound = 1;
  47. devtools = 0;
  48. crouchkey = SDL_SCANCODE_LSHIFT;
  49. jumpkey = SDL_SCANCODE_SPACE;
  50. leftkey = SDL_SCANCODE_A;
  51. forwardkey = SDL_SCANCODE_W;
  52. backkey = SDL_SCANCODE_S;
  53. rightkey = SDL_SCANCODE_D;
  54. drawkey = SDL_SCANCODE_E;
  55. throwkey = SDL_SCANCODE_Q;
  56. attackkey = MOUSEBUTTON_LEFT;
  57. consolekey = SDL_SCANCODE_GRAVE;
  58. newdetail = detail;
  59. }
  60. void SaveSettings()
  61. {
  62. if (newdetail < 0) {
  63. newdetail = 0;
  64. }
  65. if (newdetail > 2) {
  66. newdetail = 2;
  67. }
  68. if (newscreenwidth < minscreenwidth || newscreenwidth > maxscreenwidth) {
  69. newscreenwidth = screenwidth;
  70. }
  71. if (newscreenheight < minscreenheight || newscreenheight > maxscreenheight) {
  72. newscreenheight = screenheight;
  73. }
  74. errno = 0;
  75. ofstream opstream(Folders::getConfigFilePath());
  76. if (opstream.fail()) {
  77. perror(("Couldn't save config file " + Folders::getConfigFilePath()).c_str());
  78. return;
  79. }
  80. opstream << "Screenwidth:\n";
  81. opstream << newscreenwidth;
  82. opstream << "\nScreenheight:\n";
  83. opstream << newscreenheight;
  84. opstream << "\nFullscreen:\n";
  85. opstream << fullscreen;
  86. opstream << "\nMouse sensitivity:\n";
  87. opstream << usermousesensitivity;
  88. opstream << "\nBlur(0,1):\n";
  89. opstream << ismotionblur;
  90. opstream << "\nOverall Detail(0,1,2) higher=better:\n";
  91. opstream << newdetail;
  92. opstream << "\nFloating jump:\n";
  93. opstream << floatjump;
  94. opstream << "\nMouse jump:\n";
  95. opstream << mousejump;
  96. opstream << "\nAmbient sound:\n";
  97. opstream << ambientsound;
  98. opstream << "\nBlood (0,1,2):\n";
  99. opstream << bloodtoggle;
  100. opstream << "\nAuto slomo:\n";
  101. opstream << autoslomo;
  102. opstream << "\nFoliage:\n";
  103. opstream << foliage;
  104. opstream << "\nMusic:\n";
  105. opstream << musictoggle;
  106. opstream << "\nTrilinear:\n";
  107. opstream << trilinear;
  108. opstream << "\nDecals(shadows,blood puddles,etc):\n";
  109. opstream << decalstoggle;
  110. opstream << "\nInvert mouse:\n";
  111. opstream << invertmouse;
  112. opstream << "\nGamespeed:\n";
  113. if (oldgamespeed == 0) {
  114. oldgamespeed = 1;
  115. }
  116. opstream << oldgamespeed;
  117. opstream << "\nDamage effects(blackout, doublevision):\n";
  118. opstream << damageeffects;
  119. opstream << "\nText:\n";
  120. opstream << texttoggle;
  121. opstream << "\nShow Points:\n";
  122. opstream << showpoints;
  123. opstream << "\nAlways Blur:\n";
  124. opstream << alwaysblur;
  125. opstream << "\nImmediate mode (turn on on G5):\n";
  126. opstream << immediate;
  127. opstream << "\nVelocity blur:\n";
  128. opstream << velocityblur;
  129. opstream << "\nVolume:\n";
  130. opstream << volume;
  131. opstream << "\nForward key:\n";
  132. opstream << forwardkey;
  133. opstream << "\nBack key:\n";
  134. opstream << backkey;
  135. opstream << "\nLeft key:\n";
  136. opstream << leftkey;
  137. opstream << "\nRight key:\n";
  138. opstream << rightkey;
  139. opstream << "\nJump key:\n";
  140. opstream << jumpkey;
  141. opstream << "\nCrouch key:\n";
  142. opstream << crouchkey;
  143. opstream << "\nDraw key:\n";
  144. opstream << drawkey;
  145. opstream << "\nThrow key:\n";
  146. opstream << throwkey;
  147. opstream << "\nAttack key:\n";
  148. opstream << attackkey;
  149. opstream << "\nConsole key:\n";
  150. opstream << consolekey;
  151. opstream << "\nDamage bar:\n";
  152. opstream << showdamagebar;
  153. opstream << "\nStereoMode:\n";
  154. opstream << stereomode;
  155. opstream << "\nStereoSeparation:\n";
  156. opstream << stereoseparation;
  157. opstream << "\nStereoReverse:\n";
  158. opstream << stereoreverse;
  159. opstream << "\n";
  160. opstream.close();
  161. }
  162. bool LoadSettings()
  163. {
  164. errno = 0;
  165. ifstream ipstream(Folders::getConfigFilePath(), std::ios::in);
  166. if (ipstream.fail()) {
  167. perror(("Couldn't read config file " + Folders::getConfigFilePath()).c_str());
  168. return false;
  169. }
  170. char setting[256];
  171. char string[256];
  172. printf("Loading config\n");
  173. while (!ipstream.eof()) {
  174. ipstream.getline(setting, sizeof(setting));
  175. // skip blank lines
  176. // assume lines starting with spaces are all blank
  177. if (strlen(setting) == 0 || setting[0] == ' ' || setting[0] == '\t') {
  178. continue;
  179. }
  180. //~ printf("setting : %s\n",setting);
  181. if (ipstream.eof() || ipstream.fail()) {
  182. fprintf(stderr, "Error reading config file: Got setting name '%s', but value can't be read\n", setting);
  183. ipstream.close();
  184. return false;
  185. }
  186. if (!strncmp(setting, "Screenwidth", 11)) {
  187. ipstream >> kContextWidth;
  188. if (kContextWidth < (int)minscreenwidth || kContextWidth > (int)maxscreenwidth) {
  189. kContextWidth = (int)minscreenwidth;
  190. }
  191. } else if (!strncmp(setting, "Screenheight", 12)) {
  192. ipstream >> kContextHeight;
  193. if (kContextHeight < (int)minscreenheight || kContextHeight > (int)maxscreenheight) {
  194. kContextHeight = (int)minscreenheight;
  195. }
  196. } else if (!strncmp(setting, "Fullscreen", 10)) {
  197. ipstream >> fullscreen;
  198. } else if (!strncmp(setting, "Mouse sensitivity", 17)) {
  199. ipstream >> usermousesensitivity;
  200. } else if (!strncmp(setting, "Blur", 4)) {
  201. ipstream >> ismotionblur;
  202. } else if (!strncmp(setting, "Overall Detail", 14)) {
  203. ipstream >> detail;
  204. } else if (!strncmp(setting, "Floating jump", 13)) {
  205. ipstream >> floatjump;
  206. } else if (!strncmp(setting, "Mouse jump", 10)) {
  207. ipstream >> mousejump;
  208. } else if (!strncmp(setting, "Ambient sound", 13)) {
  209. ipstream >> ambientsound;
  210. } else if (!strncmp(setting, "Blood", 5)) {
  211. ipstream >> bloodtoggle;
  212. } else if (!strncmp(setting, "Auto slomo", 10)) {
  213. ipstream >> autoslomo;
  214. } else if (!strncmp(setting, "Foliage", 7)) {
  215. ipstream >> foliage;
  216. } else if (!strncmp(setting, "Music", 5)) {
  217. ipstream >> musictoggle;
  218. } else if (!strncmp(setting, "Trilinear", 9)) {
  219. ipstream >> trilinear;
  220. } else if (!strncmp(setting, "Decals", 6)) {
  221. ipstream >> decalstoggle;
  222. } else if (!strncmp(setting, "Invert mouse", 12)) {
  223. ipstream >> invertmouse;
  224. } else if (!strncmp(setting, "Gamespeed", 9)) {
  225. ipstream >> gamespeed;
  226. oldgamespeed = gamespeed;
  227. if (oldgamespeed == 0) {
  228. gamespeed = 1;
  229. oldgamespeed = 1;
  230. }
  231. } else if (!strncmp(setting, "Damage effects", 14)) {
  232. ipstream >> damageeffects;
  233. } else if (!strncmp(setting, "Text", 4)) {
  234. ipstream >> texttoggle;
  235. } else if (!strncmp(setting, "Devtools", 8)) {
  236. ipstream >> devtools;
  237. } else if (!strncmp(setting, "Show Points", 11)) {
  238. ipstream >> showpoints;
  239. } else if (!strncmp(setting, "Always Blur", 11)) {
  240. ipstream >> alwaysblur;
  241. } else if (!strncmp(setting, "Immediate mode ", 15)) {
  242. ipstream >> immediate;
  243. } else if (!strncmp(setting, "Velocity blur", 13)) {
  244. ipstream >> velocityblur;
  245. } else if (!strncmp(setting, "Volume", 6)) {
  246. ipstream >> volume;
  247. } else if (!strncmp(setting, "Forward key", 11)) {
  248. ipstream >> forwardkey;
  249. } else if (!strncmp(setting, "Back key", 8)) {
  250. ipstream >> backkey;
  251. } else if (!strncmp(setting, "Left key", 8)) {
  252. ipstream >> leftkey;
  253. } else if (!strncmp(setting, "Right key", 9)) {
  254. ipstream >> rightkey;
  255. } else if (!strncmp(setting, "Jump key", 8)) {
  256. ipstream >> jumpkey;
  257. } else if (!strncmp(setting, "Crouch key", 10)) {
  258. ipstream >> crouchkey;
  259. } else if (!strncmp(setting, "Draw key", 8)) {
  260. ipstream >> drawkey;
  261. } else if (!strncmp(setting, "Throw key", 9)) {
  262. ipstream >> throwkey;
  263. } else if (!strncmp(setting, "Attack key", 10)) {
  264. ipstream >> attackkey;
  265. } else if (!strncmp(setting, "Console key", 11)) {
  266. ipstream >> consolekey;
  267. } else if (!strncmp(setting, "Damage bar", 10)) {
  268. ipstream >> showdamagebar;
  269. } else if (!strncmp(setting, "StereoMode", 10)) {
  270. int i;
  271. ipstream >> i;
  272. stereomode = (StereoMode)i;
  273. } else if (!strncmp(setting, "StereoSeparation", 16)) {
  274. ipstream >> stereoseparation;
  275. } else if (!strncmp(setting, "StereoReverse", 13)) {
  276. ipstream >> stereoreverse;
  277. } else {
  278. ipstream >> string;
  279. fprintf(stderr, "Unknown config option '%s' with value '%s'. Ignoring.\n", setting, string);
  280. }
  281. if (ipstream.fail()) {
  282. fprintf(stderr, "Error reading config file: EOF reached when trying to read value for setting '%s'.\n", setting);
  283. ipstream.close();
  284. return false;
  285. }
  286. if (ipstream.bad()) {
  287. fprintf(stderr, "Error reading config file: Failed to read value for setting '%s'.\n", setting);
  288. ipstream.close();
  289. return false;
  290. }
  291. }
  292. ipstream.close();
  293. if (detail > 2) {
  294. detail = 2;
  295. }
  296. if (detail < 0) {
  297. detail = 0;
  298. }
  299. if (screenwidth < minscreenwidth || screenwidth > maxscreenwidth) {
  300. screenwidth = 1024;
  301. }
  302. if (screenheight < minscreenheight || screenheight > maxscreenheight) {
  303. screenheight = 768;
  304. }
  305. newdetail = detail;
  306. return true;
  307. }