C:/Documents and Settings/mtauraso/Desktop/proj3/projectX/CommandManager.cpp

Go to the documentation of this file.
00001 #include "CommandManager.h"
00002 
00003 #include "Command.h"
00004 #include "List.h"
00005 #include "Parser.h"
00006 #include "SDL.h"
00007 #include "Screen.h"
00008 
00009 #include <fstream>
00010 
00011 
00012 using namespace std;
00013 
00014 CommandManager *CommandManager::_instance = NULL;
00015 
00016 CommandManager::CommandManager()
00017 :_recording(false),_playing(false)
00018 {
00019         _instance = this;
00020 }
00021 
00022 void CommandManager::execute(Command *command)
00023 {
00024         command->execute();
00025         
00026         if(_recording) {
00027                 CommandTime demo;
00028                 demo.c = command;
00029                 demo.time = _demoTime;
00030                 _demoCommands.push(demo);
00031         }
00032 }
00033 
00034 void CommandManager::execute(std::queue<Command *> commands)
00035 {
00036         while(!commands.empty()) {
00037                 commands.front()->execute();
00038                 commands.pop();
00039         }
00040 }
00041 
00042 void CommandManager::startPlaying(std::priority_queue<CommandTime,std::vector<CommandTime>,std::greater<CommandTime> > &commands)
00043 {
00044         if(!_playing) {
00045                 _playing = true;
00046                 _playTime = 0;
00047         }
00048 
00049         while(!commands.empty()) {
00050                 commands.top().time += _playTime;
00051                 _playCommands.push(commands.top());
00052                 commands.pop();
00053         }
00054 }
00055 
00056 void CommandManager::startPlaying(List *commands)
00057 {
00058         priority_queue<CommandTime,std::vector<CommandTime>,std::greater<CommandTime> > newCommands;
00059 
00060         while(!commands->empty()) {
00061                 CommandTime commandTime;
00062                 commandTime.time = atoi(commands->firstString().c_str()) ;//* Screen::FRAMERATE / 1000;
00063                 commands = commands->rest();
00064                 List *command = commands->firstList();
00065                 commandTime.c = Command::loadCommand(command);
00066 
00067                 newCommands.push(commandTime);
00068                 commands = commands->rest();
00069         }
00070 
00071         startPlaying(newCommands);
00072 }
00073 
00074 void CommandManager::startPlaying(std::string filename)
00075 {
00076         ifstream file(filename.c_str());
00077         List *commands = Parser::parse(filename);
00078         startPlaying(commands);
00079         delete commands;
00080 }
00081 
00082 void CommandManager::stopPlaying()
00083 {
00084         _playing = false;
00085 }
00086 
00087 CommandManager *CommandManager::instance()
00088 {
00089         if(!_instance)
00090                 return new CommandManager();
00091         else
00092                 return _instance;
00093 }
00094 
00095 void CommandManager::startRecording()
00096 {
00097         _demoCommands.empty();
00098         _recording = true;
00099         _demoTime = 0;
00100 }
00101 
00102 void CommandManager::stopRecording()
00103 {
00104         _recording = false;
00105 }
00106 
00107 void CommandManager::saveRecording(std::string filename)
00108 {
00109         ofstream file(filename.c_str());
00110         file << Parser::open << endl;
00111         priority_queue<CommandTime,std::vector<CommandTime>,std::greater<CommandTime> > writeCommands(_demoCommands);
00112         while(!writeCommands.empty()) {
00113                 file << writeCommands.top().time  << " ";//* 1000 / Screen::FRAMERATE << " ";
00114                 file << writeCommands.top().c->getString() << endl;
00115                 writeCommands.pop();
00116         }
00117         file << Parser::close;
00118 }
00119 
00120 void CommandManager::startTimestep()
00121 {
00122         if(_recording)
00123                 _demoTime++;
00124         if(_playing && !_playCommands.empty()) {
00125                 _playTime++;
00126                 CommandTime top = _playCommands.top();
00127                 while(top.time < _playTime) {
00128                         execute(top.c);
00129                         _playCommands.pop();
00130                         if(_playCommands.empty()) {
00131                                 _playing = false;
00132                                 break;
00133                         }
00134                         else
00135                                 top = _playCommands.top();
00136                 }
00137         }
00138 }

Generated on Sat Apr 22 15:05:20 2006 for ProjectX by  doxygen 1.4.6-NO