C:/Documents and Settings/jegan/Desktop/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),_flush(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         else
00033                 delete command;
00034 }
00035 
00036 void CommandManager::execute(std::queue<Command *> commands)
00037 {
00038         while(!commands.empty()) {
00039                 commands.front()->execute();
00040                 commands.pop();
00041         }
00042 }
00043 
00044 void CommandManager::startPlaying(std::priority_queue<CommandTime,std::vector<CommandTime>,std::greater<CommandTime> > &commands)
00045 {
00046         if(!_playing) {
00047                 _playing = true;
00048                 _playTime = 0;
00049         }
00050 
00051         while(!commands.empty()) {
00052                 commands.top().time += _playTime;
00053                 _playCommands.push(commands.top());
00054                 commands.pop();
00055         }
00056 }
00057 
00058 void CommandManager::startPlaying(List *commands)
00059 {
00060         priority_queue<CommandTime,std::vector<CommandTime>,std::greater<CommandTime> > newCommands;
00061 
00062         while(!commands->empty()) {
00063                 CommandTime commandTime;
00064                 commandTime.time = atoi(commands->firstString().c_str()) ;//* Screen::FRAMERATE / 1000;
00065                 commands = commands->rest();
00066                 List *command = commands->firstList();
00067                 commandTime.c = Command::loadCommand(command);
00068 
00069                 newCommands.push(commandTime);
00070                 commands = commands->rest();
00071         }
00072 
00073         startPlaying(newCommands);
00074 }
00075 
00076 void CommandManager::startPlaying(std::string filename)
00077 {
00078         ifstream file(filename.c_str());
00079         List *commands = Parser::parse(filename);
00080         startPlaying(commands);
00081         delete commands;
00082 }
00083 
00084 void CommandManager::stopPlaying()
00085 {
00086         _playing = false;
00087 }
00088 
00089 CommandManager *CommandManager::instance()
00090 {
00091         if(!_instance)
00092                 return new CommandManager();
00093         else
00094                 return _instance;
00095 }
00096 
00097 void CommandManager::startRecording()
00098 {
00099         _demoCommands.empty();
00100         _recording = true;
00101         _demoTime = 0;
00102 }
00103 
00104 void CommandManager::stopRecording()
00105 {
00106         _recording = false;
00107 }
00108 
00109 void CommandManager::saveRecording(std::string filename)
00110 {
00111         ofstream file(filename.c_str());
00112         file << Parser::open << endl;
00113         priority_queue<CommandTime,std::vector<CommandTime>,std::greater<CommandTime> > writeCommands(_demoCommands);
00114         while(!writeCommands.empty()) {
00115                 file << writeCommands.top().time  << " ";//* 1000 / Screen::FRAMERATE << " ";
00116                 file << writeCommands.top().c->getString() << endl;
00117                 writeCommands.pop();
00118         }
00119         file << Parser::close;
00120 }
00121 
00122 void CommandManager::flush()
00123 {
00124         _flush = true;
00125 }
00126 
00127 void CommandManager::startTimestep()
00128 {
00129         if(_flush) {
00130                 while(!_playCommands.empty()) {
00131                         delete _playCommands.top().c;
00132                         _playCommands.pop();
00133                 }
00134                 //_playCommands.c.clear();
00135                 _playing = false;
00136                 _flush = false;
00137         }
00138         if(_recording)
00139                 _demoTime++;
00140         if(_playing && !_playCommands.empty()) {
00141                 _playTime++;
00142                 CommandTime top = _playCommands.top();
00143                 while(top.time < _playTime) {
00144                         //execute(top.c);
00145                         top.c->execute();
00146                         delete top.c;
00147                         _playCommands.pop();
00148                         if(_playCommands.empty()) {
00149                                 _playing = false;
00150                                 break;
00151                         }
00152                         else
00153                                 top = _playCommands.top();
00154                 }
00155         }
00156 }

Generated on Fri May 5 00:20:18 2006 for ProjectX by  doxygen 1.4.6-NO