FEATURE: Entity "stance" phase (fallback)
[Tsunagari.git] / src / timer.cpp
blobe5ec7f02d533b49d691a07ea1caa66781558960a
1 /****************************
2 ** Tsunagari Tile Engine **
3 ** timer.cpp **
4 ** Copyright 2012 OmegaSDG **
5 ****************************/
7 #include <boost/format.hpp>
9 #include "python.h"
10 #include "timer.h"
11 #include "world.h"
13 static Timer pythonNewTimer()
15 return Timer();
19 Timer::Timer()
20 : running(false), prev_count(0)
24 bool Timer::isRunning() const
26 return running;
29 void Timer::setRunning(bool running)
31 if (running) {
32 this->running = true;
33 prev_time = World::instance()->time();
36 else
37 this->running = false;
40 void Timer::reset()
42 prev_count = 0;
45 double Timer::count() const
47 time_t prev_count = this->prev_count;
49 if (running) {
50 time_t now = World::instance()->time();
51 prev_count = prev_count + (now - prev_time);
54 return (double)prev_count / 1000.0;
57 double Timer::count()
59 if (running) {
60 time_t now = World::instance()->time();
61 prev_count = prev_count + (now - prev_time);
62 prev_time = now;
65 return (double)prev_count / 1000.0;
68 std::string Timer::repr() const
70 using namespace boost;
72 return str(format("<timer count=%.02fsec running=%s />")
73 % count() % (isRunning() ? "true" : "false"));
76 void exportTimer()
78 using namespace boost::python;
80 class_<Timer> ("Timer", no_init)
81 .add_property("running", &Timer::isRunning, &Timer::setRunning)
82 .add_property("count",
83 static_cast<double (Timer::*) ()> (&Timer::count))
84 .def("reset", &Timer::reset)
85 .def("__repr__", &Timer::repr)
88 pythonAddFunction("new_timer", pythonNewTimer);