Mal auf die Ursprungsfrage bezogen:
|
C-/C++-Quelltext
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
#ifndef FPS_HPP
#define FPS_HPP
#include <chrono>
#include <ostream>
class FPS
{
public:
FPS();
unsigned int operator()(double weight=0.05);
friend std::ostream& operator<<(std::ostream &lhs, FPS& rhs);
private:
std::chrono::nanoseconds spf();
private:
typedef std::chrono::time_point<std::chrono::system_clock> snap;
snap start;
snap _end;
double lastend;
};
#endif
|
|
C-/C++-Quelltext
|
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
|
#include "FPS.hpp"
FPS::FPS() : start(std::chrono::system_clock::now()), _end(std::chrono::system_clock::now()), lastend(.0)
{
}
std::ostream& operator<<(std::ostream &lhs, FPS& rhs)
{
return lhs << rhs();
}
std::chrono::nanoseconds FPS::spf()
{
_end = std::chrono::system_clock::now();
const std::chrono::nanoseconds time(_end-start);
start = std::chrono::system_clock::now();
return time;
}
unsigned int FPS::operator()(double weight)
{
//Update every 100 milliseconds
if(std::chrono::duration_cast<std::chrono::milliseconds>(_end-start).count()>=100)
{
double sec = 60000./static_cast<double>(std::chrono::duration_cast<std::chrono::milliseconds>(spf()).count());
double temp = static_cast<unsigned int>(sec * (1-weight) + lastend * weight);
lastend = temp;
}
else
_end = std::chrono::system_clock::now();
return lastend;
}
|
Läuft hier wunderprächtig ohne Warnungen.
MfG
Check