remove src/common/Timer class, Network does its own time management
This commit is contained in:
1 parent
4c25126ba6
commit
5ba97bb089
6 files changed
+12
-140
No files matched your search
@@ -1,5 +1,4 @@
|
||||
#include "Network.h"
|
||||
#include "Timer.h"
|
||||
#include <cstring>
|
||||
#include <cstdlib>
|
||||
#include <iostream>
|
||||
@@ -11,6 +10,7 @@ char Network::rxbuff[RECEIVE_BUFFER_SIZE];
|
||||
std::map<sf::Uint32, Transmit_Message_t*> 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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -81,6 +81,7 @@ class Network{
|
||||
static std::map<sf::Uint32, Transmit_Message_t*> 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);
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
@@ -1,47 +0,0 @@
|
||||
#ifndef _TIMER_H
|
||||
#define _TIMER_H
|
||||
|
||||
#include <SFML/System/Clock.hpp>
|
||||
|
||||
|
||||
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
|
||||
Reference in new issue
Block a user