main.h

Go to the documentation of this file.
00001 ////////////////////////////////////////////////////////////////////////////////
00002 //    Scorched3D (c) 2000-2009
00003 //
00004 //    This file is part of Scorched3D.
00005 //
00006 //    Scorched3D is free software; you can redistribute it and/or modify
00007 //    it under the terms of the GNU General Public License as published by
00008 //    the Free Software Foundation; either version 2 of the License, or
00009 //    (at your option) any later version.
00010 //
00011 //    Scorched3D is distributed in the hope that it will be useful,
00012 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 //    GNU General Public License for more details.
00015 //
00016 //    You should have received a copy of the GNU General Public License
00017 //    along with Scorched3D; if not, write to the Free Software
00018 //    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 ////////////////////////////////////////////////////////////////////////////////
00020 
00021 #define WIN32_LEAN_AND_MEAN
00022 
00023 #include <windows.h>
00024 
00025 #ifdef HAVE_UNISTD_H
00026 #include <unistd.h>
00027 #endif
00028 
00029 static ARGParser aParser;
00030 char scorched3dAppName[128];
00031 
00032 // Compilers from Borland report floating-point exceptions in a manner 
00033 // that is incompatible with Microsoft Direct3D.
00034 int _matherr(struct _exception  *e)
00035 {
00036     e;               // Dummy reference to catch the warning.
00037     return 1;        // Error has been handled.
00038 }
00039 
00040 void _no_storage()
00041 {
00042         printf("Failed to allocate memory!!");
00043         std::exit(1);
00044 }
00045 
00046 void run_main(int argc, char *argv[], OptionsParameters &params)
00047 {
00048         std::set_new_handler(&_no_storage);
00049 
00050         // Set the path the executable was run with
00051         S3D::setExeName((const char *) argv[0]);
00052 
00053         // Generate the version
00054         snprintf(scorched3dAppName, 128, "Scorched3D - Version %s (%s)", 
00055                 S3D::ScorchedVersion.c_str(), S3D::ScorchedProtocolVersion.c_str());
00056 
00057         srand((unsigned)time(0));
00058 
00059         // Parse command line
00060         // Read options from command line
00061         if (!OptionEntryHelper::addToArgParser(
00062                 params.getOptions(), aParser)) exit(64);
00063         std::list<OptionEntry *>::iterator nonParamItor;
00064         for (nonParamItor = params.getNonParamOptions().begin();
00065                 nonParamItor != params.getNonParamOptions().end();
00066                 nonParamItor++)
00067         {
00068                 OptionEntryString *str = (OptionEntryString *) *nonParamItor;
00069                 aParser.addNonParamEntry(
00070                         (char *) str->getName(), str, 
00071                         (char *) str->getDescription());
00072         }
00073         if (!aParser.parse(argc, argv)) exit(64);
00074         S3D::setSettingsDir(params.getSettingsDir());
00075 
00076         // Check we are in the correct directory
00077         std::string fileName = S3D::getDataFile("data/autoexec.xml");
00078         FILE *checkfile = fopen(fileName.c_str(), "r");
00079         if (!checkfile)
00080         {
00081                 // Perhaps we can get the directory from the executables path name
00082                 char path[1024];
00083                 snprintf(path, sizeof(path), "%s", argv[0]);
00084                 char *sep = strrchr(path, '/');
00085                 if (sep)
00086                 {
00087                         // Change into this new direcotry
00088                         *sep = '\0';
00089 #ifdef _WIN32
00090                         SetCurrentDirectory(path);
00091 #else
00092                         chdir(path);
00093 #endif // _WIN32
00094                 }
00095 
00096                 // Now try again for the correct directory
00097                 std::string execFile = S3D::getDataFile("data/autoexec.xml");
00098                 checkfile = fopen(execFile.c_str(), "r");
00099                 if (!checkfile)
00100                 {       
00101 #ifdef _WIN32
00102                         GetCurrentDirectory(sizeof(path), path);
00103 #else
00104                         getcwd(path, sizeof(path));
00105 #endif // _WIN32
00106                         std::string dataPath = S3D::getDataFile("data");
00107                         S3D::dialogExit(
00108                                 scorched3dAppName, S3D::formatStringBuffer(
00109                                 "Error: This game requires the Scorched3D data directory to run.\n"
00110                                 "Your machine does not appear to have the Scorched3D data directory in\n"
00111                                 "the required location.\n"
00112                                 "The data directory is set to \"%s\" which does not exist.\n"
00113                                 "(Current working directory %s)\n\n"
00114                                 "If Scorched3D does not run please re-install Scorched3D.",
00115                                 dataPath.c_str(), path));
00116                 }
00117         }
00118         else fclose(checkfile);
00119 
00120         // Check that the mods are uptodate with the current scorched3d
00121         // version
00122         ModDirs dirs;
00123         dirs.loadModDirs();
00124 
00125 #ifndef _WIN32
00126         // Tells Linux not to issue a sig pipe when writting to a closed socket
00127         // Why does it have to be dificult!
00128         signal(SIGPIPE, SIG_IGN);
00129         signal(SIGFPE, SIG_IGN);
00130 #endif
00131 
00132         if (setlocale(LC_ALL, "C") == 0)
00133         {
00134                 S3D::dialogMessage(
00135                         scorched3dAppName,
00136                         "Warning: Failed to set client locale");
00137         }
00138 }

Generated on Mon Feb 16 15:14:49 2009 for Scorched3D by  doxygen 1.5.3