Timer.cpp
1 /******************************************************************************
2 
3  nomlib - C++11 cross-platform game engine
4 
5 Copyright (c) 2013, Jeffrey Carpenter <jeffrey.carp@gmail.com>
6 All rights reserved.
7 
8 Redistribution and use in source and binary forms, with or without
9 modification, are permitted provided that the following conditions are met:
10 
11 1. Redistributions of source code must retain the above copyright notice, this
12  list of conditions and the following disclaimer.
13 2. Redistributions in binary form must reproduce the above copyright notice,
14  this list of conditions and the following disclaimer in the documentation
15  and/or other materials provided with the distribution.
16 
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
21 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 
28 ******************************************************************************/
29 #include "nomlib/system/Timer.hpp"
30 
31 namespace nom {
32 
33 Timer::Timer ( void )
34 {
35 //NOM_LOG_TRACE ( NOM );
36 
37  if ( SDL_WasInit( SDL_INIT_TIMER ) == false )
38  {
39  if ( SDL_InitSubSystem ( SDL_INIT_TIMER ) == -1 )
40  {
41 NOM_LOG_ERR ( NOM, SDL_GetError() );
42  return;
43  }
44  }
45 
46  this->timer_started = false;
47  this->timer_paused = false;
48  this->elapsed_ticks = 0;
49  this->paused_ticks = 0;
50 }
51 
52 Timer::~Timer ( void )
53 {
54 //NOM_LOG_TRACE ( NOM );
55 
56  SDL_QuitSubSystem ( SDL_INIT_TIMER );
57 }
58 
59 void Timer::start ( void )
60 {
61  this->elapsed_ticks = SDL_GetTicks();
62  this->timer_started = true;
63  this->timer_paused = false;
64 }
65 
66 void Timer::stop ( void )
67 {
68  this->elapsed_ticks = 0;
69  this->timer_started = false;
70  this->timer_paused = false;
71 }
72 
73 void Timer::restart ( void )
74 {
75  this->start();
76 }
77 
78 void Timer::pause ( void )
79 {
80  if ( ( this->timer_started == true ) && ( this->timer_paused == false ) )
81  {
82  this->timer_paused = true;
83  this->paused_ticks = SDL_GetTicks() - this->elapsed_ticks;
84  }
85 }
86 
87 void Timer::unpause ( void )
88 {
89  if ( this->timer_paused == true )
90  {
91  this->timer_paused = false;
92  this->elapsed_ticks = SDL_GetTicks() - this->paused_ticks;
93  this->paused_ticks = 0;
94  }
95 }
96 
97 uint32 Timer::ticks ( void ) const
98 {
99  if ( this->timer_started == true )
100  {
101  if ( this->timer_paused == true )
102  {
103  return this->paused_ticks;
104  }
105  else
106  {
107  return SDL_GetTicks() - this->elapsed_ticks;
108  }
109  }
110 
111  // Timer is not running
112  return 0;
113 }
114 
115 bool Timer::started ( void ) const
116 {
117  return this->timer_started;
118 }
119 
120 bool Timer::paused ( void ) const
121 {
122  return this->timer_paused;
123 }
124 
125 const std::string Timer::ticksAsString ( void ) const
126 {
127  return std::to_string ( static_cast<uint32> ( this->ticks() ) );
128 }
129 
130 uint32 Timer::seconds ( float milliseconds ) const
131 {
132  return static_cast<uint32> ( milliseconds * 1000.f );
133 }
134 
135 
136 } // namespace nom
uint32 paused_ticks
Holds ticks while paused.
Definition: Timer.hpp:71
void restart(void)
Alias for start.
Definition: Timer.cpp:73
bool timer_paused
Tracks whether we are paused or not.
Definition: Timer.hpp:74
uint32 elapsed_ticks
Milliseconds since timer start.
Definition: Timer.hpp:68
uint32 seconds(float milliseconds) const
Helper method; conversion from milliseconds to seconds.
Definition: Timer.cpp:130
bool timer_started
Tracks whether we are started or not.
Definition: Timer.hpp:77