2004-08-12 18:06:47 +02:00
|
|
|
// platform-independent high resolution timer and FPS measuring code
|
2004-03-03 00:56:51 +01:00
|
|
|
//
|
|
|
|
// Copyright (c) 2003 Jan Wassenberg
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful, but
|
|
|
|
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
// General Public License for more details.
|
|
|
|
//
|
|
|
|
// Contact info:
|
|
|
|
// Jan.Wassenberg@stud.uni-karlsruhe.de
|
|
|
|
// http://www.stud.uni-karlsruhe.de/~urkt/
|
|
|
|
|
|
|
|
#ifndef TIMER_H
|
|
|
|
#define TIMER_H
|
|
|
|
|
2005-01-23 18:48:32 +01:00
|
|
|
#include <string>
|
|
|
|
|
2005-01-07 01:52:05 +01:00
|
|
|
#include "sysdep/debug.h" // debug_out
|
|
|
|
|
2004-03-03 00:56:51 +01:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// high resolution (> 1 �s) timestamp [s], starting at or near 0 s.
|
2005-01-23 18:48:32 +01:00
|
|
|
extern double get_time(void);
|
2004-03-03 00:56:51 +01:00
|
|
|
|
2005-01-23 18:48:32 +01:00
|
|
|
extern double timer_res(void);
|
2004-03-03 00:56:51 +01:00
|
|
|
|
|
|
|
// calculate fps (call once per frame)
|
|
|
|
// several smooth filters (tuned for ~100 FPS)
|
|
|
|
// => less fluctuation, but rapid tracking
|
|
|
|
|
|
|
|
extern int fps;
|
|
|
|
|
2005-01-23 18:48:32 +01:00
|
|
|
extern void calc_fps(void);
|
2004-03-03 00:56:51 +01:00
|
|
|
|
2004-12-19 00:30:28 +01:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
2004-12-09 21:17:09 +01:00
|
|
|
|
2005-01-23 18:48:32 +01:00
|
|
|
|
2004-12-09 21:17:09 +01:00
|
|
|
|
|
|
|
class ScopedTimer
|
|
|
|
{
|
|
|
|
double t0;
|
|
|
|
const std::string name;
|
|
|
|
|
|
|
|
public:
|
|
|
|
ScopedTimer(const char* _name)
|
|
|
|
: name(_name)
|
|
|
|
{
|
|
|
|
t0 = get_time();
|
|
|
|
}
|
|
|
|
~ScopedTimer()
|
|
|
|
{
|
|
|
|
double t1 = get_time();
|
|
|
|
double dt = t1-t0;
|
|
|
|
|
|
|
|
// assume microseconds
|
|
|
|
double scale = 1e6;
|
|
|
|
char unit = '�';
|
|
|
|
if(dt > 1.0)
|
|
|
|
scale = 1, unit = ' ';
|
|
|
|
// milli
|
|
|
|
else if(dt > 1e-3)
|
|
|
|
scale = 1e3, unit = 'm';
|
|
|
|
|
2005-01-07 01:52:05 +01:00
|
|
|
debug_out("TIMER %s: %g %cs\n", name.c_str(), dt*scale, unit);
|
2004-12-09 21:17:09 +01:00
|
|
|
}
|
2005-01-23 18:48:32 +01:00
|
|
|
|
|
|
|
// no copy ctor, since some members are const
|
|
|
|
private:
|
|
|
|
ScopedTimer& operator=(const ScopedTimer&);
|
2004-12-09 21:17:09 +01:00
|
|
|
};
|
|
|
|
|
2005-01-23 18:48:32 +01:00
|
|
|
#define TIMER(name) ScopedTimer st##name##instance(#name);
|
2004-12-09 21:17:09 +01:00
|
|
|
|
2004-03-03 00:56:51 +01:00
|
|
|
#endif // #ifndef TIMER_H
|