PageRenderTime 65ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/src/system/Configuration.cpp

http://violetland.googlecode.com/
C++ | 130 lines | 112 code | 17 blank | 1 comment | 4 complexity | a5bd52b6d4323a754ae3b3794bcbc919 MD5 | raw file
  1. #include "Configuration.h"
  2. Configuration::Configuration(FileUtility* fileUtility) {
  3. m_fileUtility = fileUtility;
  4. Screen.Width = 800;
  5. Screen.Height = 600;
  6. Screen.Color = 16;
  7. Screen.Full = false;
  8. FrameDelay = 10;
  9. ShowFps = false;
  10. AutoReload = true;
  11. SoundVolume = 4;
  12. MusicVolume = 4;
  13. MonstersAtStart = 12;
  14. AimColorA = 0x000000;
  15. AimColorB = 0xFFFFFF;
  16. AutoWeaponPickup = true;
  17. FriendlyFire = false;
  18. PlayerInputBinding[InputHandler::Teleport].Value = SDLK_q;
  19. PlayerInputBinding[InputHandler::MoveLeft].Value = SDLK_a;
  20. PlayerInputBinding[InputHandler::MoveUp].Value = SDLK_w;
  21. PlayerInputBinding[InputHandler::MoveRight].Value = SDLK_d;
  22. PlayerInputBinding[InputHandler::MoveDown].Value = SDLK_s;
  23. PlayerInputBinding[InputHandler::Restart].Value = SDLK_RETURN;
  24. PlayerInputBinding[InputHandler::Menu].Value = SDLK_ESCAPE;
  25. PlayerInputBinding[InputHandler::MenuClickA].Type = InputHandler::Mouse;
  26. PlayerInputBinding[InputHandler::MenuClickA].Value = SDL_BUTTON_LEFT;
  27. PlayerInputBinding[InputHandler::MenuClickB].Type = InputHandler::Mouse;
  28. PlayerInputBinding[InputHandler::MenuClickB].Value = SDL_BUTTON_RIGHT;
  29. PlayerInputBinding[InputHandler::Exit].Value = SDLK_F12;
  30. PlayerInputBinding[InputHandler::ToggleLight].Value = SDLK_f;
  31. PlayerInputBinding[InputHandler::ToggleLaser].Value = SDLK_g;
  32. PlayerInputBinding[InputHandler::Pause].Value = SDLK_p;
  33. PlayerInputBinding[InputHandler::ShowChar].Value = SDLK_c;
  34. PlayerInputBinding[InputHandler::Help].Value = SDLK_F1;
  35. PlayerInputBinding[InputHandler::Pickup].Value = SDLK_e;
  36. PlayerInputBinding[InputHandler::ThrowGrenade].Value = SDLK_SPACE;
  37. PlayerInputBinding[InputHandler::Fire].Value = SDL_BUTTON_LEFT;
  38. PlayerInputBinding[InputHandler::Fire].Type = InputHandler::Mouse;
  39. PlayerInputBinding[InputHandler::Reload].Value = SDL_BUTTON_RIGHT;
  40. PlayerInputBinding[InputHandler::Reload].Type = InputHandler::Mouse;
  41. }
  42. void Configuration::read() {
  43. try {
  44. ConfigFile cFile(
  45. m_fileUtility->getFullPath(FileUtility::user, "config"));
  46. cFile.readInto(Screen.Width, "screenWidth");
  47. cFile.readInto(Screen.Height, "screenHeight");
  48. cFile.readInto(Screen.Color, "screenColor");
  49. cFile.readInto(Screen.Full, "fullScreen");
  50. cFile.readInto(FrameDelay, "frameDelay");
  51. cFile.readInto(ShowFps, "showFps");
  52. cFile.readInto(AutoReload, "autoReload");
  53. cFile.readInto(SoundVolume, "soundVolume");
  54. cFile.readInto(MusicVolume, "musicVolume");
  55. cFile.readInto(AimColorA, "aimColorA");
  56. cFile.readInto(AimColorB, "aimColorB");
  57. cFile.readInto(AutoWeaponPickup, "autoWeaponPickup");
  58. cFile.readInto(FriendlyFire, "friendlyFire");
  59. for (int i = 0; i < InputHandler::GameInputEventsCount; i++) {
  60. ReadPlayerBinding(&cFile, &PlayerInputBinding[i],
  61. InputHandler::getEventIdentifier(i));
  62. }
  63. } catch (...) {
  64. std::cout << "Can't open config file." << std::endl;
  65. }
  66. }
  67. void Configuration::ReadPlayerBinding(ConfigFile* cFile,
  68. InputHandler::Binding* binding, std::string eventIdentifier) {
  69. int type;
  70. std::string keyType = "playerInputBinding_" + eventIdentifier + "Type";
  71. std::string keyValue = "playerInputBinding_" + eventIdentifier + "Value";
  72. if (cFile->keyExists(keyType)) {
  73. cFile->readInto(type, keyType);
  74. binding->Type = (InputHandler::BindingType) type;
  75. cFile->readInto(binding->Value, keyValue);
  76. }
  77. }
  78. void Configuration::WritePlayerBinding(ConfigFile* cFile,
  79. InputHandler::Binding* binding, std::string eventIdentifier) {
  80. std::string keyType = "playerInputBinding_" + eventIdentifier + "Type";
  81. std::string keyValue = "playerInputBinding_" + eventIdentifier + "Value";
  82. cFile->add(keyType, (int) binding->Type);
  83. cFile->add(keyValue, binding->Value);
  84. }
  85. void Configuration::write() {
  86. ConfigFile cFile;
  87. cFile.add("aimColorB", AimColorB);
  88. cFile.add("aimColorA", AimColorA);
  89. cFile.add("soundVolume", SoundVolume);
  90. cFile.add("musicVolume", MusicVolume);
  91. cFile.add("autoReload", AutoReload);
  92. cFile.add("showFps", ShowFps);
  93. cFile.add("frameDelay", FrameDelay);
  94. cFile.add("fullScreen", Screen.Full);
  95. cFile.add("screenColor", Screen.Color);
  96. cFile.add("screenHeight", Screen.Height);
  97. cFile.add("screenWidth", Screen.Width);
  98. cFile.add("autoWeaponPickup", AutoWeaponPickup);
  99. cFile.add("friendlyFire", FriendlyFire);
  100. for (int i = 0; i < InputHandler::GameInputEventsCount; i++) {
  101. WritePlayerBinding(&cFile, &PlayerInputBinding[i],
  102. InputHandler::getEventIdentifier(i));
  103. }
  104. boost::filesystem::ofstream ofile(
  105. m_fileUtility->getFullPath(FileUtility::user, "config"));
  106. if (ofile) {
  107. ofile << cFile;
  108. ofile.close();
  109. }
  110. }
  111. Configuration::~Configuration() {
  112. // nothing
  113. }