diff --git a/src/client/Client.cc b/src/client/Client.cc index 9b78caf..5338922 100644 --- a/src/client/Client.cc +++ b/src/client/Client.cc @@ -1,7 +1,6 @@ #include #include "Client.h" #include "Types.h" -#include "Timer.h" /* TODO: this should be moved to common somewhere */ #define MAX_SHOT_DISTANCE 250.0 @@ -23,11 +22,9 @@ Client::~Client() { // Send disconnect message bool connection_closed = false; - double close_timer; sf::Packet client_packet; sf::Uint8 packet_type = PLAYER_DISCONNECT; - Timer client_timer; - client_timer.Init(); + sf::Clock timeout_clock; client_packet.clear(); client_packet << packet_type; client_packet << m_current_player; @@ -36,14 +33,8 @@ Client::~Client() // No time out needed here, since the // message will timeout after a couple of attempts // then exit anyway. - close_timer = Timer::GetTimeDouble(); while(!connection_closed) { - // Time must be updated before any messages are sent - // Especially guaranteed messages, since the time needs to be - // non zero. - client_timer.Update(); - m_net_client->Receive(); while(m_net_client->getData(client_packet)) @@ -74,7 +65,7 @@ Client::~Client() // If the server does not respond within one second just close // and the server can deal with the problems. - if((Timer::GetTimeDouble() - close_timer) > 1.0) + if(timeout_clock.getElapsedTime().asSeconds() > 1.0) { connection_closed = true; } @@ -87,8 +78,6 @@ Client::~Client() void Client::run(bool fullscreen, int width, int height, std::string pname) { - Timer client_timer; - client_timer.Init(); m_current_player_name = pname; if (!create_window(fullscreen, width, height)) return; @@ -163,11 +152,6 @@ void Client::run(bool fullscreen, int width, int height, std::string pname) } } - // Time must be updated before any messages are sent - // Especially guaranteed messages, since the time needs to be - // non zero. - client_timer.Update(); - update(elapsed_time); redraw(); last_time = current_time; diff --git a/src/common/Network.cc b/src/common/Network.cc index da8da37..7d8bff1 100644 --- a/src/common/Network.cc +++ b/src/common/Network.cc @@ -1,5 +1,4 @@ #include "Network.h" -#include "Timer.h" #include #include #include @@ -11,6 +10,7 @@ char Network::rxbuff[RECEIVE_BUFFER_SIZE]; std::map Network::transmit_queue; Client_t Network::clients[MAX_NUM_CLIENTS]; sf::Clock Network::message_timer; +sf::Clock Network::network_timer; sf::Uint32 Network::getUniqueMessageId( void ) { @@ -216,10 +216,10 @@ void Network::Receive() { if(MAX_NUM_CLIENTS > client_id) { - clients[client_id].ping = Timer::GetTimeDouble() - transmit_queue[msg_id]->TimeStarted; + clients[client_id].ping = network_timer.getElapsedTime().asSeconds() - transmit_queue[msg_id]->TimeStarted; // Need to also register that a ping message was received. - transmit_queue[msg_id]->Responses[&clients[client_id]] = Timer::GetTimeDouble(); + transmit_queue[msg_id]->Responses[&clients[client_id]] = network_timer.getElapsedTime().asSeconds(); // Received a response, so reset send attempts. clients[client_id].num_send_attempts = 0u; } @@ -231,7 +231,7 @@ void Network::Receive() // Set that the message was acknowledged by the client if(MAX_NUM_CLIENTS > client_id) { - transmit_queue[msg_id]->Responses[&clients[client_id]] = Timer::GetTimeDouble(); + transmit_queue[msg_id]->Responses[&clients[client_id]] = network_timer.getElapsedTime().asSeconds(); // Received a response, so reset send attempts. clients[client_id].num_send_attempts = 0u; @@ -284,8 +284,8 @@ void Network::Transmit() { // Broadcast the mesages to all clients sf::Uint32 msg_id = 0; - static double ping_timer = Timer::GetTimeDouble(); - double current_time = Timer::GetTimeDouble(); + static double ping_timer = network_timer.getElapsedTime().asSeconds(); + double current_time = network_timer.getElapsedTime().asSeconds(); // Every five seconds, send ping messages // Note this time must be longer than the combined @@ -313,7 +313,7 @@ void Network::Transmit() // Send any pending messages while(transmit_queue.find(msg_id) != transmit_queue.end()) { - double curTime = Timer::GetTimeDouble(); + double curTime = network_timer.getElapsedTime().asSeconds(); Transmit_Message_t * message = transmit_queue[msg_id]; switch(message->msg_type) { @@ -324,7 +324,7 @@ void Network::Transmit() // send the message and update the sent times. if(0.0 == message->TimeStarted) { - message->TimeStarted = Timer::GetTimeDouble(); + message->TimeStarted = network_timer.getElapsedTime().asSeconds(); for(int i = 0; i < MAX_NUM_CLIENTS; i++) { if((clients[i].addr != sf::IpAddress::None) && (clients[i].port != 0)) @@ -360,7 +360,7 @@ void Network::Transmit() if(MAX_NUM_SEND_ATTEMPTS < iter->first->num_send_attempts) { // Fake a receive message so that it will complete and be removed from the queue - message->Responses[iter->first] = Timer::GetTimeDouble(); + message->Responses[iter->first] = network_timer.getElapsedTime().asSeconds(); iter->first->disconnect = TIMEOUT_DISCONNECT; } } diff --git a/src/common/Network.h b/src/common/Network.h index b4ba324..1c8dac2 100644 --- a/src/common/Network.h +++ b/src/common/Network.h @@ -81,6 +81,7 @@ class Network{ static std::map transmit_queue; static char rxbuff[RECEIVE_BUFFER_SIZE]; static sf::Clock message_timer; + static sf::Clock network_timer; static sf::Uint32 getUniqueMessageId(); static int addClients(Client_t *client, sf::Uint16 *curcl); static int findClient(Client_t *client); diff --git a/src/common/Timer.cc b/src/common/Timer.cc deleted file mode 100644 index 727a6a0..0000000 --- a/src/common/Timer.cc +++ /dev/null @@ -1,58 +0,0 @@ -#include "Timer.h" - -// The number of time steps per second -const float STEPS_PER_SECOND = 60.0f; - -double Timer::totalElapsedTime; -float Timer::stepTime; -sf::Uint32 Timer::curTimeStep; - -void Timer::Init(void) -{ - // Reset the clock - myClock.restart(); - - // Set the time keepers to zero - totalElapsedTime = 0; - - // Reset the game speed - gameSpeed = 1.0f; -} - -void Timer::Update(void) -{ - // Record the time step - stepTime = (myClock.getElapsedTime().asSeconds() * gameSpeed); - myClock.restart(); - - // Add the time to the total time - totalElapsedTime += stepTime; - - // Calculate the game step - curTimeStep = (sf::Uint32)(totalElapsedTime * STEPS_PER_SECOND); -} - -sf::Uint32 Timer::GetTime(void) -{ - return curTimeStep; -} - -float Timer::GetStepTime(void) -{ - return stepTime; -} - -sf::Uint32 Timer::GetTotalTime(void) -{ - return (sf::Uint32)(totalElapsedTime); -} - -double Timer::GetTimeDouble(void) -{ - return totalElapsedTime; -} - -float Timer::GetElapsedTime(sf::Uint32 baseTime) -{ - return (totalElapsedTime - ((double)baseTime / STEPS_PER_SECOND)); -} diff --git a/src/common/Timer.h b/src/common/Timer.h deleted file mode 100644 index 8c5b6e8..0000000 --- a/src/common/Timer.h +++ /dev/null @@ -1,47 +0,0 @@ -#ifndef _TIMER_H -#define _TIMER_H - -#include - - -class Timer -{ -public: - // Initializes the time module - void Init(void); - - // Updates the time module - void Update(); - - // Returns the current time step - static sf::Uint32 GetTime(void); - - // Returns the time that elapsed this step - static float GetStepTime(void); - - // Returns the total time elapsed in milliseconds - static sf::Uint32 GetTotalTime(void); - - // Returns the total time in seconds as a double - static double GetTimeDouble(void); - - // Returns the difference between the current time and the given time in seconds - static float GetElapsedTime(sf::Uint32 baseTime); - -private: - // The clock used to take the time measurements - sf::Clock myClock; - - // The game speed - float gameSpeed; - - // The total elapsed time since the start of the game - static double totalElapsedTime; - - // The time that elapsed this step - static float stepTime; - - // The number of time steps since the start of the game - static sf::Uint32 curTimeStep; -}; -#endif diff --git a/src/server/Server.cc b/src/server/Server.cc index 905eb9c..25a3552 100644 --- a/src/server/Server.cc +++ b/src/server/Server.cc @@ -1,7 +1,6 @@ #include "Server.h" #include "Types.h" #include -#include "Timer.h" Server::Server(sf::Uint16 port) { @@ -20,18 +19,11 @@ void Server::run( void ) double current_time; double elapsed_time; double last_time = 0.0; - Timer server_timer; - server_timer.Init(); while(1) { current_time = m_clock.getElapsedTime().asSeconds(); elapsed_time = current_time - last_time; - // Time must be updated before any messages are sent - // Especially guaranteed messages, since the time needs to be - // non zero. - server_timer.Update(); - update( elapsed_time ); last_time = current_time;