00001 /****************************************************************************** 00002 * Wormux is a convivial mass murder game. 00003 * Copyright (C) 2001-2004 Lawrence Azzoug. 00004 * 00005 * This program is free software; you can redistribute it and/or modify 00006 * it under the terms of the GNU General Public License as published by 00007 * the Free Software Foundation; either version 2 of the License, or 00008 * (at your option) any later version. 00009 * 00010 * This program is distributed in the hope that it will be useful, 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 * GNU General Public License for more details. 00014 * 00015 * You should have received a copy of the GNU General Public License 00016 * along with this program; if not, write to the Free Software 00017 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA 00018 ****************************************************************************** 00019 * Handle the game time. The game can be paused. 00020 *****************************************************************************/ 00021 00022 #include "time.h" 00023 #include <SDL.h> 00024 #include <sstream> 00025 #include <iomanip> 00026 #include <iostream> 00027 #include "../graphic/video.h" 00028 #include "../interface/game_msg.h" 00029 #include "../tool/math_tools.h" 00030 #include "../include/app.h" 00031 #include "../network/network.h" 00032 #include "../team/teams_list.h" 00033 #include "../game/game_loop.h" 00034 00035 Time * Time::singleton = NULL; 00036 00037 Time * Time::GetInstance() { 00038 if (singleton == NULL) { 00039 singleton = new Time(); 00040 } 00041 return singleton; 00042 } 00043 00044 bool Time::IsGamePaused() const { 00045 return is_game_paused; 00046 } 00047 00048 Time::Time(){ 00049 is_game_paused = false; 00050 delta_t = 20; 00051 } 00052 00053 void Time::Reset(){ 00054 current_time = 0; 00055 is_game_paused = false; 00056 } 00057 00058 uint Time::Read() const{ 00059 return current_time; 00060 } 00061 00062 void Time::Refresh(){ 00063 /* 00064 TODO : Activate this condition later. 00065 Refresh time condition : 00066 - active team is Local 00067 - current node is server and game loop is not in Playing state 00068 - game don't use network 00069 if((ActiveTeam().IsLocal() || ActiveTeam().IsLocalAI()) || 00070 (network.IsServer() && GameLoop::GetInstance()->ReadState() != GameLoop::PLAYING) || 00071 (!network.IsServer() && !network.IsClient()) || 00072 current_time < max_time) 00073 */ 00074 current_time += delta_t; 00075 RefreshMaxTime(current_time); 00076 } 00077 00078 void Time::RefreshMaxTime(uint updated_max_time){ 00079 if(updated_max_time > max_time) 00080 max_time = updated_max_time; 00081 } 00082 00083 uint Time::ReadSec() const{ 00084 return Read() / 1000; 00085 } 00086 00087 uint Time::ReadMin() const{ 00088 return ReadSec() / 60; 00089 } 00090 00091 uint Time::GetDelta() const{ 00092 return delta_t; 00093 } 00094 00095 void Time::Pause(){ 00096 if (is_game_paused) 00097 return; 00098 is_game_paused = true; 00099 } 00100 00101 void Time::Continue(){ 00102 assert (is_game_paused); 00103 is_game_paused = false; 00104 } 00105 00106 uint Time::ClockSec(){ 00107 return ReadSec() % 60; 00108 } 00109 00110 uint Time::ClockMin(){ 00111 return ReadMin() % 60; 00112 } 00113 00114 std::string Time::GetString(){ 00115 std::ostringstream ss; 00116 00117 ss << ClockMin() << ":" << std::setfill('0') << std::setw(2) << ClockSec(); 00118 return ss.str(); 00119 }