| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- //========================================================================
- // Initialization.cpp : Defines utility functions for game initialization
- //
- // Part of the GameCode4 Application
- //
- // GameCode4 is the sample application that encapsulates much of the source code
- // discussed in "Game Coding Complete - 4th Edition" by Mike McShaffry and David
- // "Rez" Graham, published by Charles River Media.
- // ISBN-10: 1133776574 | ISBN-13: 978-1133776574
- //
- // If this source code has found it's way to you, and you think it has helped you
- // in any way, do the authors a favor and buy a new copy of the book - there are
- // detailed explanations in it that compliment this code well. Buy a copy at Amazon.com
- // by clicking here:
- // http://www.amazon.com/gp/product/1133776574/ref=olp_product_details?ie=UTF8&me=&seller=
- //
- // There's a companion web site at http://www.mcshaffry.com/GameCode/
- //
- // The source code is managed and maintained through Google Code:
- // http://code.google.com/p/gamecode4/
- //
- // (c) Copyright 2012 Michael L. McShaffry and David Graham
- //
- // This program is free software; you can redistribute it and/or
- // modify it under the terms of the GNU Lesser GPL v3
- // as published by the Free Software Foundation.
- //
- // This program is distributed in the hope that it will be useful,
- // but WITHOUT ANY WARRANTY; without even the implied warranty of
- // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
- // http://www.gnu.org/licenses/lgpl-3.0.txt for more details.
- //
- // You should have received a copy of the GNU Lesser GPL v3
- // along with this program; if not, write to the Free Software
- // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- //
- //========================================================================
- #include "../Unix/GameCodeStd.h"
- #include "../Mainloop/Initialization.h"
- #include "../Debugging/Logger.h"
- #include <filesystem>
- // FUTURE WORK - Crappy free functions should belong in an OS specific class.
- //
- // CheckStorage - Chapter 5, page 139
- //
- bool CheckStorage(const size_t diskSpaceNeeded)
- {
- try
- {
- std::filesystem::space_info si = std::filesystem::space(".");
- if (si.available < diskSpaceNeeded)
- {
- GCC_FATAL("CheckStorage Failure: Not enough physical storage.");
- return false;
- }
- return true;
- }
- catch (const std::filesystem::filesystem_error& e)
- {
- GCC_ERROR(e.what());
- return false;
- }
- }
- //
- // ReadCPUSpeed - Chapter 5, page 140
- //
- unsigned int ReadCPUSpeed()
- {
- unsigned int kHZ = 0;
- // Open the sysfs file for the first CPU core
- FILE* file = fopen("/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq", "r");
- if (file) {
- fscanf(file, "%u", &kHZ);
- }
- fclose(file);
- return kHZ / 1000;
- }
- GameOptions::GameOptions()
- {
- // set all the options to decent default value
- m_Level = "";
- m_Renderer = "OpenGL 4.6";
- m_runFullSpeed = false;
- m_soundEffectsVolume = 1.0f;
- m_musicVolume = 1.0f;
- m_expectedPlayers = 1;
- m_listenPort = -1;
- std::string m_gameHost = "MrMike-m1710";
- m_numAIs = 1;
- m_maxAIs = 4;
- m_maxPlayers = 4;
- m_ScreenSize = Point(1024, 768);
- m_useDevelopmentDirectories = false;
- m_pDoc = NULL;
- }
- void GameOptions::Init(const char* xmlFileName)
- {
- // read the XML file
- // if needed, override the XML file with options passed in on the command line.
- m_pDoc = new TiXmlDocument(xmlFileName);
- if (m_pDoc && m_pDoc->LoadFile())
- {
- TiXmlElement *pRoot = m_pDoc->RootElement();
- if (!pRoot)
- return;
- // Loop through each child element and load the component
- TiXmlElement* pNode = NULL;
- pNode = pRoot->FirstChildElement("Graphics");
- if (pNode)
- {
- std::string attribute;
- attribute = pNode->Attribute("renderer");
- if (attribute != "OpenGL 4.6")
- {
- GCC_ASSERT(0 && "Bad Renderer setting in Graphics options.");
- }
- else
- {
- m_Renderer = attribute;
- }
- if (pNode->Attribute("width"))
- {
- m_ScreenSize.x = atoi(pNode->Attribute("width"));
- if (m_ScreenSize.x < 800) m_ScreenSize.x = 800;
- }
- if (pNode->Attribute("height"))
- {
- m_ScreenSize.y = atoi(pNode->Attribute("height"));
- if (m_ScreenSize.y < 600) m_ScreenSize.y = 600;
- }
- if (pNode->Attribute("runfullspeed"))
- {
- attribute = pNode->Attribute("runfullspeed");
- m_runFullSpeed = (attribute == "yes") ? true : false;
- }
- }
- pNode = pRoot->FirstChildElement("Sound");
- if (pNode)
- {
- m_musicVolume = atoi(pNode->Attribute("musicVolume")) / 100.0f;
- m_soundEffectsVolume = atoi(pNode->Attribute("sfxVolume")) / 100.0f;
- }
- pNode = pRoot->FirstChildElement("Multiplayer");
- if (pNode)
- {
- m_expectedPlayers = atoi(pNode->Attribute("expectedPlayers"));
- m_numAIs = atoi(pNode->Attribute("numAIs"));
- m_maxAIs = atoi(pNode->Attribute("maxAIs"));
- m_maxPlayers = atoi(pNode->Attribute("maxPlayers"));
-
- m_listenPort = atoi(pNode->Attribute("listenPort"));
- m_gameHost = pNode->Attribute("gameHost");
- }
- pNode = pRoot->FirstChildElement("ResCache");
- if (pNode)
- {
- std::string attribute(pNode->Attribute("useDevelopmentDirectories"));
- m_useDevelopmentDirectories = ((attribute == "yes") ? (true) : (false));
- }
- }
- }
|