RacerApp.cpp

Go to the documentation of this file.
00001 
00005 /* Copyright © 2009, 2010 James Legg.
00006     This program 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 3 of the License, or
00009     (at your option) any later version.
00010 */
00011 
00012 #include <iostream>
00013 #include <fstream>
00014 #include <string>
00015 
00016 #include <SDL.h>
00017 
00018 #include "RacerApp.h"
00019 #include "UI/TitleScene.h"
00020 #include "Engine/GameScene.h"
00021 #include "Engine/InputHandler.h"
00022 #include "Engine/AITrainer.h"
00023 #include "Graphics/Window.h"
00024 
00025 #include <config.h>
00026 #include <Debug.h>
00027 
00028 #include <libtrack/DataCheck.h>
00029 
00030 #include <glibmm/init.h>
00031 
00032 RacerApp::RacerApp(int argc, char ** argv)
00033     :   title_scene(0)
00034     ,   main_loop(0)
00035     ,   replay(0)
00036 {
00037     Glib::init();
00038     try
00039     {
00040         Track::DataCheck();
00041     }
00042     catch (Track::DataCheck::FileMissingError e)
00043     {
00044         std::cerr << "Cannot find data file: " << e.filename << std::endl;
00045         std::cerr << "Please run from the directory all the data files accessible." << std::endl;
00046         return;
00047     }
00048     
00049     // usage info
00050     if (argc == 2)
00051     {
00052         if (argv[1] == std::string("-h") || argv[1] == std::string("--help"))
00053         {
00054             show_usage();
00055             return;
00056         }
00057     }
00058     Graphics::Window::get_instance().set_size(640, 480);
00059     parse_arguments(argc, argv);
00060     
00061     if (!title_scene)
00062     {
00063         title_scene = new UI::TitleScene();
00064     }
00065     
00066     main_loop = new MainLoop();
00067     main_loop->push_scene(*title_scene);
00068     // shutdown
00069     SDL_Quit();
00070 }
00071 
00072 RacerApp::~RacerApp()
00073 {
00074     if(main_loop) delete main_loop;
00075     // A replay deletes its own scene when destroyed.
00076     if (replay) delete replay;
00077     else if (title_scene) delete title_scene;
00078 }
00079 
00080 void RacerApp::parse_arguments(int argc, char ** argv)
00081 {
00083     for (int i = 0; i < argc; i++)
00084     {
00085         DEBUG_MESSAGE("Argument " << i << " is " << argv[i]);
00086     }
00087     if (argc > 1 && argv[1][0] == 'r' && argv[1][1] == '\0')
00088     {
00089         // Load a replay.
00090         DEBUG_MESSAGE("replay");
00091         
00092         if (argc == 2)
00093         {
00094             // no file given, assume standard input.
00095             DEBUG_MESSAGE("    Replaying from standard input.");
00096             replay = new Engine::ReplayReader(std::cin);
00097             
00098         }
00099         else
00100         {
00101             DEBUG_MESSAGE("    Replaying from a file.");
00102             std::ifstream replay_file(argv[2]);
00103             if (replay_file.fail())
00104             {
00105                 // cannot load file
00106                 std::cerr << "Unable to access the replay file (" << argv[2] << "). Perhaps it doesn't exist?"
00107                           << std::endl;
00108                 exit(EXIT_FAILURE);
00109             }
00110             replay = new Engine::ReplayReader(replay_file);
00111         }
00112         title_scene = replay->get_scene();
00113     }
00114     else if (argc >= 2 && argv[1][0] == 't' && argv[1][1] == '\0')
00115     {
00116         DEBUG_MESSAGE("Train");
00117         std::cout << "Training AI on track " << argv[2] << "\n";
00118         Engine::AITrainer *spork = new Engine::AITrainer(std::string(argv[2]));
00119         delete spork;
00120     }
00121     else
00122     {
00123         if (argc == 2)
00124         {
00125             // use a game scene
00126             std::string track = argv[1];
00127             // create a player for each controller.
00128             DEBUG_MESSAGE("Using one player per avaliable controller.");
00129             std::vector<std::pair<Engine::InputHandler::iterator, unsigned int> > input_devices;
00130             input_devices.reserve(Engine::InputHandler::get_instance()
00131                                                       .get_number_of_devices());
00132             for (Engine::InputHandler::iterator it = Engine::InputHandler::get_instance().begin();
00133                  it != Engine::InputHandler::get_instance().end();
00134                  it++)
00135             {
00136                 input_devices.push_back(std::make_pair(it, 0));
00137             }
00141             Engine::LoadScene<Engine::GameScene, RacerApp> * scene;
00142             scene = new Engine::LoadScene<Engine::GameScene, RacerApp>(input_devices, track);
00143             scene->set_done_notifier(this);
00144             title_scene = scene;
00145         }
00146     }
00147 }
00148 
00149 void RacerApp::operator ()(Engine::GameScene * new_scene)
00150 {
00151     main_loop->push_scene(*new_scene);
00152 }
00153 
00154 void RacerApp::show_usage()
00155 {
00156     std::cout << "Usage:" << std::endl
00157               << "racer         start the game showing the menu." << std::endl
00158               << "racer track   play track multiplayer using all avaliable controllers." << std::endl
00159               << "racer r file  Show the replay saved in file." << std::endl
00160               << "racer r       Show replay passed in by standard input." << std::endl
00161               << "racer t track Train the AI on the track." << std::endl
00162               << "racer --help  Show this help and exit." << std::endl
00163               << "racer -h      Show this help and exit." << std::endl
00164               << std::endl
00165               << "A replay is automatically saved to a file called racer_last_replay, which is" << std::endl
00166               << "stored in your configuration directory (usually ~/.config)." << std::endl
00167               << "File paths must either be absolute, or relative to racer's path, for example" << std::endl
00168               << "'racer data/tracks/0' should work anywhere once racer is installed." << std::endl
00169               << std::endl;
00170     std::cout << PACKAGE_NAME " version " PACKAGE_VERSION << std::endl
00171               << "Built time: " BUILD_TIME "." << std::endl;
00172     #ifndef NDEBUG
00173         std::cout << "This is a debug build." << std::endl;
00174     #endif
00175 }

Get Racer at SourceForge.net. Fast, secure and Free Open Source software downloads

Generated at Mon Sep 6 00:41:13 2010 by Doxygen version 1.4.7 for Racer version svn335.