1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
|
#include <SDL.h>
#include <iostream>
#include <math.h>
#include "timer.h"
#include "console.h"
using namespace std;
Timer::Timer( Type t )
:type(t)
{
starton = false;
doneon = false;
}
void Timer::setType( Type t )
{
type = t;
}
/** Set the value range.
*
* @param s Start value of the range
* @param e End value of the range
*/
void Timer::setRange( double s, double e )
{
// Parameters can not be modified while the timer is running
if( starton )
return;
startval = s;
endval = e;
}
/** Returns the start value of the range.
*
*/
double Timer::startv() const
{
return startval;
}
/** Returns the end value of the range.
*
*/
double Timer::end() const
{
return endval;
}
/** Sets the progress partway through the timer
*
*/
void Timer::setProgress( double progval )
{
if( starton )
return;
dur *= ( endval - progval ) / ( endval - startval );
startval = progval;
}
/** Set the duration of the timer.
*
* @param dursecs The amount of time the timer should run, in seconds
*/
void Timer::setDuration( double dursecs )
{
// Parameters can not be modified while the timer is running
if( starton )
return;
dur = dursecs;
}
/** Returns the duration of the timer in seconds.
*
*/
double Timer::duration() const
{
return dur;
}
/** Returns how much time has elapsed.
* Returns the number of seconds the timer has been running.
*/
double Timer::timeElapsed() const
{
return ( lastTicks - startTicks ) / 1000.0;
}
/** Returns how much time is left.
* Returns the number of seconds the timer has until completion.
*/
double Timer::timeLeft() const
{
return ( endTicks - lastTicks ) / 1000.0;
}
/** Start the timer.
*
*/
void Timer::start()
{
// Can't start something twice
if( starton )
return;
// Store the starting time
startTicks = SDL_GetTicks();
lastTicks = startTicks;
endTicks = int( dur * 1000 ) + startTicks;
// Initialize the current and previous values
val = startval;
lastval = startval;
// See how much the value changes by for each tick
calculateStep();
starton = true;
doneon = false;
}
bool Timer::started()
{
return starton;
}
void Timer::stop()
{
starton = false;
}
bool Timer::done()
{
return doneon;
}
void Timer::resetDone()
{
doneon = false;
}
/** Update the timer.
* Modifies the value to reflect the time elapsed.
*/
void Timer::update()
{
// Timer must be running to update
if( !starton )
return;
lastval = val;
int ticksChange = SDL_GetTicks() - lastTicks;
lastTicks += ticksChange;
// As long as this isn't the end of the timer
if( lastTicks < endTicks ) {
val += step * ticksChange;
if( type == LOGARITHMIC ) {
val = ((endval - startval)/log(11.0))*log(10*double(lastTicks - startTicks)/
(endTicks - startTicks) + 1.0) + startval;
}
} else {
// If this is the end, just update to the end value
val = endval;
lastval = endval;
starton = false;
doneon = true;
}
}
/** Update the timer.
*
*/
void Timer::operator++(int)
{
update();
}
/** Returns the change in the value since the last update.
*
*/
double Timer::change() const
{
// No change if the timer isn't running
if( !starton )
return 0;
return val - lastval;
}
/** Returns the current value within the range.
* If the timer isn't running, returns the start of the range.
*/
double Timer::value() const
{
// Still at the start if not running
if( !starton )
return startval;
return val;
}
void Timer::calculateStep()
{
step = endval - startval;
// Prevent divide by zero
if( dur != 0 )
step /= 1000 * dur;
else
step = 0;
}
|