Show tempo.cpp syntax highlighted
/***************************************************************************
tempo.cpp
-------------------
begin : Tue Jun 15 2004
copyright : (C) 2004 by Brendon Higgins
email : freepop-devel@lists.sourceforge.net
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include <tempo.h>
#include <ClanLib/core.h>
#include <iostream>
/**
* Helper for Tempo.
*/
class Ticker: public CL_KeepAlive {
public:
/**
* Constructor.
*/
Ticker(Tempo* tempo);
/**
* Ticks the clock in Tempo.
*/
virtual void keep_alive();
private:
/**
* The Tempo I'm attached to.
*/
Tempo* t;
};
Ticker::Ticker(Tempo* tempo): t(tempo) {
}
void Ticker::keep_alive() {
t->tick();
}
Tempo::Tempo(): thisFrames(0), prevFrames(0), framesTime(0),
prevTime(0), thisTime(0), delay(0) {
}
void Tempo::start() {
thisTime = CL_System::get_time();
delay = 0;
t = new Ticker(this);
}
void Tempo::tick() {
prevTime = thisTime;
thisTime = CL_System::get_time();
delay = thisTime - prevTime;
++thisFrames;
framesTime += delay;
if (framesTime >= 1000) {
do {
framesTime -= 1000;
prevFrames = thisFrames;
thisFrames = 0;
} while (framesTime >= 1000);
// If we skip some seconds, only send the one signal.
framesSignal(prevFrames);
}
}
void Tempo::stop() {
delete t;
t = 0;
delay = 0;
}
unsigned int Tempo::getDelay() const {
return delay;
}
unsigned int Tempo::getFrames() const {
return prevFrames;
}
CL_Signal_v1<int>& Tempo::getFramesSignal() {
return framesSignal;
}
unsigned int Tempo::getStamp() const {
return thisTime;
}
void Tempo::log(const std::string& s) const {
std::cout << "T+" << thisTime << ": " << s << std::endl;
}
See more files for this project here