Initialization.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. //========================================================================
  2. // Initialization.cpp : Defines utility functions for game initialization
  3. //
  4. // Part of the GameCode4 Application
  5. //
  6. // GameCode4 is the sample application that encapsulates much of the source code
  7. // discussed in "Game Coding Complete - 4th Edition" by Mike McShaffry and David
  8. // "Rez" Graham, published by Charles River Media.
  9. // ISBN-10: 1133776574 | ISBN-13: 978-1133776574
  10. //
  11. // If this source code has found it's way to you, and you think it has helped you
  12. // in any way, do the authors a favor and buy a new copy of the book - there are
  13. // detailed explanations in it that compliment this code well. Buy a copy at Amazon.com
  14. // by clicking here:
  15. // http://www.amazon.com/gp/product/1133776574/ref=olp_product_details?ie=UTF8&me=&seller=
  16. //
  17. // There's a companion web site at http://www.mcshaffry.com/GameCode/
  18. //
  19. // The source code is managed and maintained through Google Code:
  20. // http://code.google.com/p/gamecode4/
  21. //
  22. // (c) Copyright 2012 Michael L. McShaffry and David Graham
  23. //
  24. // This program is free software; you can redistribute it and/or
  25. // modify it under the terms of the GNU Lesser GPL v3
  26. // as published by the Free Software Foundation.
  27. //
  28. // This program is distributed in the hope that it will be useful,
  29. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  30. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  31. // http://www.gnu.org/licenses/lgpl-3.0.txt for more details.
  32. //
  33. // You should have received a copy of the GNU Lesser GPL v3
  34. // along with this program; if not, write to the Free Software
  35. // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  36. //
  37. //========================================================================
  38. #include "../Unix/GameCodeStd.h"
  39. #include "../Mainloop/Initialization.h"
  40. #include "../Debugging/Logger.h"
  41. #include <filesystem>
  42. // FUTURE WORK - Crappy free functions should belong in an OS specific class.
  43. //
  44. // CheckStorage - Chapter 5, page 139
  45. //
  46. bool CheckStorage(const size_t diskSpaceNeeded)
  47. {
  48. try
  49. {
  50. std::filesystem::space_info si = std::filesystem::space(".");
  51. if (si.available < diskSpaceNeeded)
  52. {
  53. GCC_FATAL("CheckStorage Failure: Not enough physical storage.");
  54. return false;
  55. }
  56. return true;
  57. }
  58. catch (const std::filesystem::filesystem_error& e)
  59. {
  60. GCC_ERROR(e.what());
  61. return false;
  62. }
  63. }
  64. //
  65. // ReadCPUSpeed - Chapter 5, page 140
  66. //
  67. unsigned int ReadCPUSpeed()
  68. {
  69. unsigned int kHZ = 0;
  70. // Open the sysfs file for the first CPU core
  71. FILE* file = fopen("/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq", "r");
  72. if (file) {
  73. fscanf(file, "%u", &kHZ);
  74. }
  75. fclose(file);
  76. return kHZ / 1000;
  77. }
  78. GameOptions::GameOptions()
  79. {
  80. // set all the options to decent default value
  81. m_Level = "";
  82. m_Renderer = "OpenGL 4.6";
  83. m_runFullSpeed = false;
  84. m_soundEffectsVolume = 1.0f;
  85. m_musicVolume = 1.0f;
  86. m_expectedPlayers = 1;
  87. m_listenPort = -1;
  88. std::string m_gameHost = "MrMike-m1710";
  89. m_numAIs = 1;
  90. m_maxAIs = 4;
  91. m_maxPlayers = 4;
  92. m_ScreenSize = Point(1024, 768);
  93. m_useDevelopmentDirectories = false;
  94. m_pDoc = NULL;
  95. }
  96. void GameOptions::Init(const char* xmlFileName)
  97. {
  98. // read the XML file
  99. // if needed, override the XML file with options passed in on the command line.
  100. m_pDoc = new TiXmlDocument(xmlFileName);
  101. if (m_pDoc && m_pDoc->LoadFile())
  102. {
  103. TiXmlElement *pRoot = m_pDoc->RootElement();
  104. if (!pRoot)
  105. return;
  106. // Loop through each child element and load the component
  107. TiXmlElement* pNode = NULL;
  108. pNode = pRoot->FirstChildElement("Graphics");
  109. if (pNode)
  110. {
  111. std::string attribute;
  112. attribute = pNode->Attribute("renderer");
  113. if (attribute != "OpenGL 4.6")
  114. {
  115. GCC_ASSERT(0 && "Bad Renderer setting in Graphics options.");
  116. }
  117. else
  118. {
  119. m_Renderer = attribute;
  120. }
  121. if (pNode->Attribute("width"))
  122. {
  123. m_ScreenSize.x = atoi(pNode->Attribute("width"));
  124. if (m_ScreenSize.x < 800) m_ScreenSize.x = 800;
  125. }
  126. if (pNode->Attribute("height"))
  127. {
  128. m_ScreenSize.y = atoi(pNode->Attribute("height"));
  129. if (m_ScreenSize.y < 600) m_ScreenSize.y = 600;
  130. }
  131. if (pNode->Attribute("runfullspeed"))
  132. {
  133. attribute = pNode->Attribute("runfullspeed");
  134. m_runFullSpeed = (attribute == "yes") ? true : false;
  135. }
  136. }
  137. pNode = pRoot->FirstChildElement("Sound");
  138. if (pNode)
  139. {
  140. m_musicVolume = atoi(pNode->Attribute("musicVolume")) / 100.0f;
  141. m_soundEffectsVolume = atoi(pNode->Attribute("sfxVolume")) / 100.0f;
  142. }
  143. pNode = pRoot->FirstChildElement("Multiplayer");
  144. if (pNode)
  145. {
  146. m_expectedPlayers = atoi(pNode->Attribute("expectedPlayers"));
  147. m_numAIs = atoi(pNode->Attribute("numAIs"));
  148. m_maxAIs = atoi(pNode->Attribute("maxAIs"));
  149. m_maxPlayers = atoi(pNode->Attribute("maxPlayers"));
  150. m_listenPort = atoi(pNode->Attribute("listenPort"));
  151. m_gameHost = pNode->Attribute("gameHost");
  152. }
  153. pNode = pRoot->FirstChildElement("ResCache");
  154. if (pNode)
  155. {
  156. std::string attribute(pNode->Attribute("useDevelopmentDirectories"));
  157. m_useDevelopmentDirectories = ((attribute == "yes") ? (true) : (false));
  158. }
  159. }
  160. }