Pol  Revision:cb584c9
gameclck.cpp
Go to the documentation of this file.
1 
7 #include "gameclck.h"
8 
9 #include <ctime>
10 #include <string>
11 
12 #include "../clib/spinlock.h"
13 #include "../clib/stlutil.h"
14 #include "globals/uvars.h"
15 #include "polclock.h"
16 
17 namespace Pol
18 {
19 namespace Core
20 {
22 
23 static time_t last_read;
24 
26 
28 {
29  Clib::SpinLockGuard lock( _gameclock_lock );
30  std::string gameclock_str;
31  if ( gamestate.global_properties->getprop( "gameclock", gameclock_str ) )
32  {
33  char ch_s;
34  ISTRINGSTREAM is( gameclock_str );
35  is >> ch_s >> gameclock;
36  }
37  else
38  {
39  gameclock = 0;
40  }
41 
42  last_read = poltime();
43 }
44 
46 {
47  OSTRINGSTREAM os;
48  os << "s" << read_gameclock();
49  gamestate.global_properties->setprop( "gameclock", OSTRINGSTREAM_STR( os ) );
50 }
51 
53 {
55 }
56 
58 {
59  Clib::SpinLockGuard lock( _gameclock_lock );
60  time_t new_last_read = poltime();
61  unsigned int diff = static_cast<unsigned int>( new_last_read - last_read );
62  gameclock += diff;
63  last_read = new_last_read;
64  return gameclock;
65 }
66 }
67 }
unsigned int gameclock_t
Definition: gameclck.h:14
#define OSTRINGSTREAM_STR(x)
Definition: stlutil.h:76
std::unique_ptr< Core::PropertyList > global_properties
Definition: uvars.h:142
static Clib::SpinLock _gameclock_lock
Definition: gameclck.cpp:25
static gameclock_t gameclock
Definition: gameclck.cpp:21
static time_t last_read
Definition: gameclck.cpp:23
void stop_gameclock()
Stops the game clock and saves the current value into "gameclock".
Definition: gameclck.cpp:52
#define OSTRINGSTREAM
Definition: stlutil.h:75
gameclock_t read_gameclock()
Reads the current value of the game clock.
Definition: gameclck.cpp:57
GameState gamestate
Definition: uvars.cpp:74
time_t poltime()
Definition: polclock.cpp:102
#define ISTRINGSTREAM
Definition: stlutil.h:73
void start_gameclock()
The functions below deal with reading and updating the gameclock. The state is protected by a mutex...
Definition: gameclck.cpp:27
void update_gameclock()
Saves the current value into the global cprop "gameclock".
Definition: gameclck.cpp:45
std::lock_guard< SpinLock > SpinLockGuard
Definition: spinlock.h:33
Definition: berror.cpp:12