diff options
Diffstat (limited to 'src/core')
-rw-r--r-- | src/core/application.cc | 19 | ||||
-rw-r--r-- | src/core/application.h | 11 | ||||
-rw-r--r-- | src/core/clientstate.cc | 16 | ||||
-rw-r--r-- | src/core/cvar.cc | 25 | ||||
-rw-r--r-- | src/core/cvar.h | 1 | ||||
-rw-r--r-- | src/core/func.cc | 2 | ||||
-rw-r--r-- | src/core/gameconnection.cc | 22 | ||||
-rw-r--r-- | src/core/gameconnection.h | 11 | ||||
-rw-r--r-- | src/core/gameinterface.cc | 48 | ||||
-rw-r--r-- | src/core/gameinterface.h | 35 | ||||
-rw-r--r-- | src/core/gameserver.cc | 37 | ||||
-rw-r--r-- | src/core/gameserver.h | 11 | ||||
-rw-r--r-- | src/core/net.h | 2 | ||||
-rw-r--r-- | src/core/netconnection.cc | 9 | ||||
-rw-r--r-- | src/core/netconnection.h | 7 | ||||
-rw-r--r-- | src/core/netserver.cc | 12 | ||||
-rw-r--r-- | src/core/netserver.h | 6 | ||||
-rw-r--r-- | src/core/timer.cc | 14 | ||||
-rw-r--r-- | src/core/timer.h | 5 |
19 files changed, 142 insertions, 151 deletions
diff --git a/src/core/application.cc b/src/core/application.cc index f98d44e..837f294 100644 --- a/src/core/application.cc +++ b/src/core/application.cc @@ -147,7 +147,7 @@ Application::Application() } application_instance = this; - application_time = 0; + application_timestamp = 0; application_game = 0; module_interactive = 0; @@ -171,9 +171,8 @@ Application::~Application() void Application::init(int count, char **arguments) { - con_debug << "Debug messages enabled\n"; con_print << "^BInitializing core...\n"; - + con_debug << " debug messages enabled\n"; filesystem::init("base", ""); CommandBuffer::init(); @@ -243,6 +242,7 @@ void Application::init(int count, char **arguments) // register our engine functions Func *func = 0; + func = Func::add("help", func_help); func->set_info("help function"); @@ -258,11 +258,13 @@ void Application::init(int count, char **arguments) func = Func::add("disconnect", func_disconnect); func->set_info("leave the current game"); - func = Func::add("say",func_say); + func = Func::add("say", func_say); func->set_info("say [text] say something on the public chat"); - func = Func::add("msg",func_msg); + func = Func::add("msg", func_msg); func->set_info("msg [player] [text] send a private message to another player"); + + func = 0; } void Application::shutdown() @@ -282,6 +284,7 @@ void Application::shutdown() Module::clear(); // remove our engine functions + Func::remove("msg"); Func::remove("say"); Func::remove("help"); Func::remove("quit"); @@ -373,9 +376,9 @@ void Application::disconnect() } } -void Application::frame(float seconds) +void Application::frame(unsigned long timestamp) { - application_time += seconds; + application_timestamp = timestamp; // execute commands in the buffer CommandBuffer::exec(); @@ -384,7 +387,7 @@ void Application::frame(float seconds) return; // run a game interface frame - application_game->frame(seconds); + application_game->frame(timestamp); if (!application_game->running()) disconnect(); diff --git a/src/core/application.h b/src/core/application.h index e55c37f..229f318 100644 --- a/src/core/application.h +++ b/src/core/application.h @@ -30,9 +30,12 @@ public: /*----- inspectors ----------------------------------------------- */ - /// time the application has been running - inline float time() const { return application_time; } + /// the current application time, in microseconds + inline unsigned long timestamp() const { return application_timestamp; } + /// the current application time, in seconds + float time() const { return ((float)(timestamp()) / 1000.0f); } + /// true if the core is connected to a running game interface inline bool connected() const { return (application_game && application_game->running()); } @@ -88,7 +91,7 @@ public: protected: /// run a core frame - virtual void frame(float seconds); + virtual void frame(unsigned long timestamp); /// load cvar config void load_config(); @@ -104,7 +107,7 @@ protected: private: /// time the core has been running - float application_time; + unsigned long application_timestamp; GameInterface *application_game; static Application *application_instance; diff --git a/src/core/clientstate.cc b/src/core/clientstate.cc index 84e1fe0..298e653 100644 --- a/src/core/clientstate.cc +++ b/src/core/clientstate.cc @@ -65,10 +65,22 @@ void ClientState::clearsound() { if (state_engineloopsource) { application()->notify_remove_sound(state_engineloopsource); + } + + if (state_engineeventsource) { application()->notify_remove_sound(state_engineeventsource); - state_engineloopsource = 0; - state_engineloopbuffer = 0; } + + state_thusterloopbuffer = 0; + state_impulseloopbuffer = 0; + state_impulsestartbuffer = 0; + state_impulsestopbuffer = 0; + + state_engineloopbuffer = 0; + state_engineloopsource = 0; + + state_engineeventbuffer = 0; + state_engineeventsource = 0; } void ClientState::assign(Entity * entity) diff --git a/src/core/cvar.cc b/src/core/cvar.cc index 50043f2..7db202d 100644 --- a/src/core/cvar.cc +++ b/src/core/cvar.cc @@ -35,16 +35,10 @@ Cvar *Cvar::rconpassword = 0; Cvar::Registry Cvar::cvar_registry; -Cvar::Cvar(const char *name, const unsigned int flags) +Cvar::Cvar(const char *name, const unsigned int flags) : cvar_name(), cvar_info(), cvar_str() { cvar_flags = flags; - - if (name) - cvar_name.assign(name); - else - cvar_name.clear(); - - cvar_info.clear(); + cvar_name.assign(name); } void Cvar::set_info(const char *info) @@ -64,7 +58,8 @@ Cvar & Cvar::operator=(const std::string &other) Cvar & Cvar::operator=(const char *other) { - return ((*this) = std::string(other)); + std::string value(other); + return (this->operator=(value)); } Cvar & Cvar::operator=(float other) @@ -81,11 +76,10 @@ Cvar* Cvar::get(const char *name, const char *value, const unsigned int flags) Cvar *c = find(name); if (c) { //con_debug << "get " << name << " already exist with value " << cvar->str() << std::endl; - c->cvar_flags |= flags; } else { //con_debug << "get " << name << " " << value << std::endl; c = new Cvar(name, flags); - cvar_registry[std::string(name)] = c; + cvar_registry[c->name()] = c; (*c) = value; } c->cvar_flags |= flags; @@ -100,7 +94,7 @@ Cvar* Cvar::get(const char *name, float value, const unsigned int flags) } else { //con_debug << "get " << name << " " << value << std::endl; c = new Cvar(name, flags); - cvar_registry[std::string(name)] = c; + cvar_registry[c->name()] = c; (*c) = value; } c->cvar_flags |= flags; @@ -112,7 +106,7 @@ Cvar* Cvar::set(const char *name, const char *value, const unsigned int flags) Cvar *c = find(name); if (!c) { c = new Cvar(name, flags); - cvar_registry[std::string(name)] = c; + cvar_registry[c->name()] = c; } (*c) = value; c->cvar_flags = flags; @@ -126,7 +120,7 @@ Cvar* Cvar::set(const char *name, float value, unsigned int flags) Cvar *c = find(name); if (!c) { c = new Cvar(name, flags); - cvar_registry[std::string(name)] = c; + cvar_registry[c->name()] = c; } (*c) = value; c->cvar_flags = flags; @@ -161,7 +155,8 @@ Cvar *Cvar::find(std::string const &name) Cvar *Cvar::find(const char *name) { - return(find(std::string(name))); + std::string s(name); + return(find(s)); } void Cvar::list() diff --git a/src/core/cvar.h b/src/core/cvar.h index d934872..acb6f89 100644 --- a/src/core/cvar.h +++ b/src/core/cvar.h @@ -130,6 +130,7 @@ private: std::string cvar_name; std::string cvar_info; std::string cvar_str; + unsigned int cvar_flags; float cvar_value; diff --git a/src/core/func.cc b/src/core/func.cc index edebc8a..81175f8 100644 --- a/src/core/func.cc +++ b/src/core/func.cc @@ -39,7 +39,7 @@ Func *Func::add(const char *name, GameFuncPtr gamefunctionptr, unsigned int flag Registry::iterator it = func_registry.find(name); if (it == func_registry.end()) { func = new Func(name, (void *)gamefunctionptr, flags | Func::Game); - func_registry[std::string(name)] = func; + func_registry[func->name()] = func; //con_debug << "Function '" << name << "' registered." << std::endl; } else { con_warn << "Function '" << name << "' already registered!" << std::endl; diff --git a/src/core/gameconnection.cc b/src/core/gameconnection.cc index 114931c..eaafe91 100644 --- a/src/core/gameconnection.cc +++ b/src/core/gameconnection.cc @@ -22,7 +22,9 @@ GameConnection::GameConnection(std::string const &connectionstr) connection_instance = this; connection_network = 0; connection_running = false; - connection_frametime = 0; + + connection_timestamp = 0; + connection_netframe = 0; unsigned int port = DEFAULTPORT; std::string host(connectionstr); @@ -101,7 +103,7 @@ void GameConnection::private_message(std::string const &args) connection_network->send_private_message(args); } -void GameConnection::frame(float seconds) +void GameConnection::frame(unsigned long timestamp) { if (!running()) return; @@ -111,20 +113,21 @@ void GameConnection::frame(float seconds) return; } - update_clientstate(seconds); - - connection_network->frame(seconds); + update_clientstate(); - connection_frametime += seconds; + // get incoming messages + connection_network->frame(); float f = 0; if (core::Cvar::net_framerate->value()) { - f = 0.5f / core::Cvar::net_framerate->value(); - if (connection_frametime < f) { + f = 1000.0f / core::Cvar::net_framerate->value(); + if (connection_netframe + f > timestamp) { return; } } + connection_netframe = timestamp; + if (connection_network->state() == NetConnection::Connected) { if(localcontrol() && localcontrol()->dirty()) { @@ -139,9 +142,8 @@ void GameConnection::frame(float seconds) } } + connection_timestamp = connection_network->timestamp(); connection_network->transmit(); - - connection_frametime = 0; } } diff --git a/src/core/gameconnection.h b/src/core/gameconnection.h index 95df9f3..70e3015 100644 --- a/src/core/gameconnection.h +++ b/src/core/gameconnection.h @@ -32,12 +32,12 @@ public: inline bool interactive() const { return true; } /// return the current game time - inline float time() const { return game_clientframetime; } + inline unsigned long timestamp() const { return connection_timestamp; } /*----- mutators -------------------------------------------------- */ /// run a game connection time frame - void frame(float seconds); + void frame(unsigned long timestamp); /// forward a command line to the remote server void forward(std::string const &cmdline); @@ -59,10 +59,11 @@ protected: private: bool connection_running; - static GameConnection *connection_instance; + unsigned long connection_timestamp; // server game time + unsigned long connection_netframe; // last network frame timestamp + NetConnection *connection_network; - float connection_frametime; - + static GameConnection *connection_instance; }; inline GameConnection *connection() { return GameConnection::instance(); } diff --git a/src/core/gameinterface.cc b/src/core/gameinterface.cc index 74acced..d99ec15 100644 --- a/src/core/gameinterface.cc +++ b/src/core/gameinterface.cc @@ -118,16 +118,13 @@ void GameInterface::clear() } game_players.clear(); - - game_previousframetime = 0; - game_serverframetime = 0; - game_clientframetime = 0; } -void GameInterface::reset_clientstate(float timestamp, float prevtimestamp) +void GameInterface::reset_clientstate() { - game_previousframetime = prevtimestamp; - game_serverframetime = timestamp; +// game_previousframetime = prevtimestamp; +// game_serverframetime = timestamp; +// game_time = timestamp; for (Entity::Registry::iterator it = Entity::registry().begin(); it != Entity::registry().end(); it++) { @@ -137,10 +134,10 @@ void GameInterface::reset_clientstate(float timestamp, float prevtimestamp) entity->state()->assign(entity); } - if ( game_clientframetime < game_previousframetime) - game_clientframetime = game_previousframetime; - else if ( game_clientframetime > game_serverframetime) - game_clientframetime = game_serverframetime; +// if ( game_clientframetime < game_previousframetime) +// game_clientframetime = game_previousframetime; +// else if ( game_clientframetime > game_serverframetime) +// game_clientframetime = game_serverframetime; } @@ -150,14 +147,11 @@ void GameInterface::update_entity_clientstate(Entity *entity) if (!entity->state()) { entity->entity_clientstate = new ClientState(entity); entity->entity_clientstate->assign(entity); - } - - if(Cvar::cl_prediction->value() == 0) { - + } else entity->state()->assign(entity); - return; } +/* if (!(entity->flags() & Entity::Static)) { // clientstate location @@ -181,7 +175,7 @@ void GameInterface::update_entity_clientstate(Entity *entity) if (angle > MIN_DELTA) entity->state()->state_axis.rotate(n, -angle); } - /* + n.assign(math::crossproduct( entity->state()->axis().left(), entity->axis().left())); if (!(n.length() < MIN_DELTA)) { n.normalize(); @@ -199,7 +193,7 @@ void GameInterface::update_entity_clientstate(Entity *entity) if (angle > MIN_DELTA) entity->state()->state_axis.rotate(n, -angle); } - */ + } else { entity->state()->state_axis.assign(entity->axis()); } @@ -211,29 +205,15 @@ void GameInterface::update_entity_clientstate(Entity *entity) entity->state()->assign(entity); } } +*/ -void GameInterface::update_clientstate(float seconds) +void GameInterface::update_clientstate() { - game_clientframetime += seconds; - for (Entity::Registry::iterator it = Entity::registry().begin(); it != Entity::registry().end(); it++) { update_entity_clientstate((*it).second); } } -float GameInterface::timeoffset() { - - if (game_clientframetime > game_serverframetime) - return 1; - - float d = game_serverframetime - game_previousframetime; - if (d <= 0) - return 0; - - float t = game_clientframetime - game_previousframetime; - return t/d; -} - void GameInterface::list_players() { using namespace std; diff --git a/src/core/gameinterface.h b/src/core/gameinterface.h index fea86fe..c916d15 100644 --- a/src/core/gameinterface.h +++ b/src/core/gameinterface.h @@ -30,20 +30,6 @@ public: /// return the local player inline Player *localplayer() { return &game_localplayer; } - /// return the server time of the last received server frame - inline float serverframetime() const { return game_serverframetime; } - - /// return the server time of the previous received server frame - inline float previousframetime() const { return game_previousframetime; } - - /// return the server time of the previous received server frame - inline float clientframetime() const { return game_clientframetime; } - - /// client frame time between previousframetime and serverframetime, from 0 - 1 - float timeoffset(); - - inline float timestep() const { return game_timestep; } - inline Players & players() { return game_players; } /// show a list of connected players @@ -58,7 +44,10 @@ public: virtual bool interactive() const = 0; /// return the current game time - virtual float time() const = 0; + virtual unsigned long timestamp() const = 0; + + /// return the current game time, in seconds + float time() const { return ((float)(timestamp()) / 1000.0f); } /*----- mutators ------------------------------------------------- */ @@ -66,18 +55,18 @@ public: void clear(); /// reset the client state - void reset_clientstate(float timestamp, float prevtimestamp); + void reset_clientstate(); /// update the client state timers - void update_clientstate(float seconds); + void update_clientstate(); void update_entity_clientstate(Entity *entity); /*----- virtual mutators ------------------------------------------ */ /// run one game time frame - /// @param seconds time since the previous frame, in seconds - virtual void frame(float seconds) = 0; + /// @param timestamp current application time + virtual void frame(unsigned long timestamp) = 0; protected: /// the local player @@ -85,14 +74,6 @@ protected: /// all the players Players game_players; - - float game_serverframetime; - float game_previousframetime; - - float game_timestep; - float game_clientframetime; - - unsigned int game_serverframelength; }; /// global local player instance diff --git a/src/core/gameserver.cc b/src/core/gameserver.cc index e42d1b2..4fc3924 100644 --- a/src/core/gameserver.cc +++ b/src/core/gameserver.cc @@ -120,10 +120,10 @@ GameServer::GameServer() : GameInterface() con_print << "^BInitializing game server...\n"; server_instance = this; server_network = 0; - server_time = 0; + server_timestamp = 0; server_previoustime = 0; - server_frametime = 0.0f; server_maxplayerid = 1; + server_startup = application()->timestamp(); server_module = Module::current(); if (!server_module) { @@ -558,14 +558,11 @@ void GameServer::player_disconnect(Player *player) } } -void GameServer::frame(float seconds) +void GameServer::frame(unsigned long timestamp) { if (error()) return; - server_time += seconds; - server_frametime += seconds; - // process incoming network messages if (server_network) { server_network->receive(); @@ -580,24 +577,25 @@ void GameServer::frame(float seconds) localplayer()->update_info(); if (!Cvar::sv_dedicated->value()) { - update_clientstate(seconds); + update_clientstate(); } if ((Cvar::sv_dedicated->value() || Cvar::sv_private->value())) { if (core::Cvar::sv_framerate->value()) { - float f = 1.0f / core::Cvar::sv_framerate->value(); - if (server_frametime < f) { + float f = 1000.0f / core::Cvar::sv_framerate->value(); + if (server_startup + server_timestamp + f > timestamp) { return; } } - } + } + + server_previoustime = server_timestamp; + server_timestamp = timestamp - server_startup; + float elapsed = (float) (server_timestamp - server_previoustime) / 1000.0f; // copy the previous entity state to the client state if (!Cvar::sv_dedicated->value()) { - reset_clientstate(server_time, server_previoustime); - } else { - game_serverframetime = server_time; - game_previousframetime = server_previoustime; + reset_clientstate(); } // run a time frame on each entity @@ -605,13 +603,13 @@ void GameServer::frame(float seconds) Entity *entity = (*it).second; if ((entity->type() == Entity::Controlable) || (entity->type() == Entity::Dynamic)) { - entity->frame(server_frametime); + entity->frame(elapsed); } } // run a frame on the module if (server_module) { - server_module->frame(server_frametime); + server_module->frame(elapsed); if (server_module->error()) { abort(); return; @@ -620,7 +618,7 @@ void GameServer::frame(float seconds) if (server_network) { // send network updates - server_network->frame(server_time, server_previoustime); + server_network->frame(server_timestamp); } // mark all entities as updated @@ -643,11 +641,8 @@ void GameServer::frame(float seconds) } if (!Cvar::sv_dedicated->value()) { - update_clientstate(0); + update_clientstate(); } - - server_frametime = 0; - server_previoustime = server_time; } void GameServer::save_config() diff --git a/src/core/gameserver.h b/src/core/gameserver.h index 550dd99..4ea476b 100644 --- a/src/core/gameserver.h +++ b/src/core/gameserver.h @@ -37,7 +37,7 @@ public: virtual bool interactive() const; /// current server game time - virtual inline float time() const { return server_time; } + virtual inline unsigned long timestamp() const { return server_timestamp; } /*----- mutators -------------------------------------------------- */ @@ -48,7 +48,7 @@ public: void player_disconnect(Player *player); /// run a game server time frame - void frame(float seconds); + void frame(unsigned long timestamp); /// a player sends a chat message to the public channel void say(Player *player, std::string const &args); @@ -104,9 +104,10 @@ private: NetServer *server_network; unsigned int server_maxplayerid; - float server_frametime; - float server_time; - float server_previoustime; + + unsigned long server_timestamp; + unsigned long server_previoustime; + unsigned long server_startup; }; inline GameServer *server() { return GameServer::instance(); } diff --git a/src/core/net.h b/src/core/net.h index 4909136..c724f85 100644 --- a/src/core/net.h +++ b/src/core/net.h @@ -11,7 +11,7 @@ namespace core { /// network protocol version -const unsigned int PROTOCOLVERSION = 10; +const unsigned int PROTOCOLVERSION = 11; /// maximum lenght of a (compressed) network message block const unsigned int FRAMESIZE = 1152; diff --git a/src/core/netconnection.cc b/src/core/netconnection.cc index ae1118f..704551b 100644 --- a/src/core/netconnection.cc +++ b/src/core/netconnection.cc @@ -23,6 +23,7 @@ NetConnection::NetConnection() { connection_timeout = core::application()->time(); connection_state = Connecting; + connection_timestamp = 0; receive_compressed = false; received_compressed_size = 0; @@ -233,7 +234,7 @@ void NetConnection::receive() } -void NetConnection::frame(float seconds) +void NetConnection::frame() { timeval timeout; timeout.tv_sec = 0; @@ -458,9 +459,9 @@ void NetConnection::parse_incoming_message(const std::string & message) } else if (command == "ping") { } else if (command == "frame") { - float timestamp, prevtimestamp; - if ((msgstream >> timestamp) && (msgstream >> prevtimestamp)) { - game()->reset_clientstate(timestamp, prevtimestamp); + unsigned long timestamp; + if ((msgstream >> timestamp)) { + connection_timestamp = timestamp; } } else if (command == "die") { diff --git a/src/core/netconnection.h b/src/core/netconnection.h index cb04ce2..66f7369 100644 --- a/src/core/netconnection.h +++ b/src/core/netconnection.h @@ -48,7 +48,7 @@ public: virtual void disconnect(); /// process pending incoming messages - void frame(float seconds); + void frame(); /// send a connect message to the remote server void send_connect(); @@ -93,6 +93,9 @@ public: State connection_state; + /// return the current game time + inline unsigned long timestamp() const { return connection_timestamp; } + protected: /// add a raw network message to the send queue void send_raw(std::string const &msg); @@ -128,6 +131,8 @@ private: size_t received_compressed_size; size_t compressed_size; char zrecvbuf[BLOCKSIZE]; + + unsigned long connection_timestamp; }; } diff --git a/src/core/netserver.cc b/src/core/netserver.cc index 95b047a..cdcdad7 100644 --- a/src/core/netserver.cc +++ b/src/core/netserver.cc @@ -300,7 +300,7 @@ void NetServer::client_initialize(NetClient *client) { } // send updates to one client -void NetServer::client_frame(NetClient *client, float timestamp, float previoustimestamp) +void NetServer::client_frame(NetClient *client, unsigned long timestamp) { if (client->state() != NetClient::Connected) return; @@ -328,7 +328,7 @@ void NetServer::client_frame(NetClient *client, float timestamp, float previoust } else { // send a server frame marker - send_frame_marker(client, timestamp, previoustimestamp); + send_frame_marker(client, timestamp); // send updates for entities in the zone for (Entity::Registry::iterator it = Entity::registry().begin(); it != Entity::registry().end(); it++) { @@ -361,7 +361,7 @@ void NetServer::client_frame(NetClient *client, float timestamp, float previoust } // run a network server frame, send updates to clients -void NetServer::frame(float timestamp, float previoustimestamp) +void NetServer::frame(unsigned long timestamp) { /* FIXME Only entities within visual range should send updates (1024 game units?) @@ -376,7 +376,7 @@ void NetServer::frame(float timestamp, float previoustimestamp) client->transmit(fd()); if (client->state() == NetClient::Connected) - client_frame(client, timestamp, previoustimestamp); + client_frame(client, timestamp); // update player info always gets through if (client->player()->dirty() || client->player()->zonechange()) { @@ -449,10 +449,10 @@ void NetServer::send_disconnect(NetClient *client) } // send a "frame" message to a client -void NetServer::send_frame_marker(NetClient *client, float timestamp, float previoustimestamp) +void NetServer::send_frame_marker(NetClient *client, unsigned long timestamp) { std::ostringstream msg(""); - msg << "frame " << timestamp << " " << previoustimestamp << "\n"; + msg << "frame " << timestamp << "\n"; if (client->state() == NetClient::Connected) { client->send_raw(msg.str()); diff --git a/src/core/netserver.h b/src/core/netserver.h index 1d2e131..8877707 100644 --- a/src/core/netserver.h +++ b/src/core/netserver.h @@ -53,7 +53,7 @@ public: /*----- mutators -------------------------------------------------- */ /// run a network server frame - void frame(float timestamp, float previoustimestamp); + void frame(unsigned long timestamp); /// receive data from clients void receive(); @@ -72,7 +72,7 @@ public: protected: /// send a server frame marker - void send_frame_marker(NetClient *client, float timestamp, float previoustimestamp); + void send_frame_marker(NetClient *client, unsigned long timestamp); /// send a create entity event void send_entity_create(NetClient *client, Entity *entity); @@ -105,7 +105,7 @@ protected: void parse_incoming_message(NetClient *client, const std::string & message); /// send a server frame to a single client - void client_frame(NetClient *client, float timestamp, float previoustimestamp); + void client_frame(NetClient *client, unsigned long timestamp); private: bool netserver_error; diff --git a/src/core/timer.cc b/src/core/timer.cc index e5a808e..b614af1 100644 --- a/src/core/timer.cc +++ b/src/core/timer.cc @@ -8,6 +8,7 @@ #include <unistd.h> #include <iostream> +#include <cmath> namespace core { @@ -26,7 +27,7 @@ void Timer::mark() gettimeofday(&timer_tick, &timer_tz); } -float Timer::elapsed() +unsigned long Timer::timestamp() { struct timeval tick; struct timezone tick_tz; @@ -34,8 +35,15 @@ float Timer::elapsed() gettimeofday(&tick, &tick_tz); // calculate elapsed time in 10^-6 seconds - long delta = (tick.tv_sec - timer_tick.tv_sec) * 1000000 + (tick.tv_usec - timer_tick.tv_usec); - return( (float) delta / 1000000.0f); + unsigned long delta = 0; + delta = tick.tv_sec * 1000 + tick.tv_usec / 1000; + delta -= timer_tick.tv_sec * 1000 + timer_tick.tv_usec / 1000; + return delta; +} + +float Timer::elapsed() +{ + return ((float) timestamp() / 1000.0f); } } diff --git a/src/core/timer.h b/src/core/timer.h index 9e172bf..b878875 100644 --- a/src/core/timer.h +++ b/src/core/timer.h @@ -28,11 +28,14 @@ public: */ void mark(); - /*! return the time elapsed since the last mark + /*! return the time elapsed since the last mark, in seconds * @see mark() */ float elapsed(); + /// return timestamp since last mark, in microseconds + unsigned long timestamp(); + private: float timer_elapsed; struct timezone timer_tz; |