1 #ifndef WIN32 2 #include <sys/time.h> 3 #else 4 #endif /** !WIN32 */ 5 #include <stdlib.h> 6 #ifdef LINUX 7 #include <GL/gl.h> 8 #endif 9 #ifdef WIN32 10 #include "glew.h" 11 #endif 12 13 #ifdef __APPLE__ 14 #include <OpenGL/gl.h> 15 #endif 16 17 #include "TimeKeeper.hpp" 18 #include "RandomNumberGenerators.hpp" 19 TimeKeeper(double presetDuration,double smoothDuration,double easterEgg)20TimeKeeper::TimeKeeper(double presetDuration, double smoothDuration, double easterEgg) 21 { 22 _smoothDuration = smoothDuration; 23 _presetDuration = presetDuration; 24 _easterEgg = easterEgg; 25 26 #ifndef WIN32 27 gettimeofday ( &this->startTime, NULL ); 28 #else 29 startTime = GetTickCount(); 30 #endif /** !WIN32 */ 31 32 UpdateTimers(); 33 } 34 UpdateTimers()35 void TimeKeeper::UpdateTimers() 36 { 37 #ifndef WIN32 38 _currentTime = getTicks ( &startTime ) * 0.001; 39 #else 40 _currentTime = getTicks ( startTime ) * 0.001; 41 #endif /** !WIN32 */ 42 43 _presetFrameA++; 44 _presetFrameB++; 45 46 } 47 StartPreset()48 void TimeKeeper::StartPreset() 49 { 50 _isSmoothing = false; 51 _presetTimeA = _currentTime; 52 _presetFrameA = 1; 53 _presetDurationA = sampledPresetDuration(); 54 } StartSmoothing()55 void TimeKeeper::StartSmoothing() 56 { 57 _isSmoothing = true; 58 _presetTimeB = _currentTime; 59 _presetFrameB = 1; 60 _presetDurationB = sampledPresetDuration(); 61 } EndSmoothing()62 void TimeKeeper::EndSmoothing() 63 { 64 _isSmoothing = false; 65 _presetTimeA = _presetTimeB; 66 _presetFrameA = _presetFrameB; 67 _presetDurationA = _presetDurationB; 68 } 69 CanHardCut()70 bool TimeKeeper::CanHardCut() 71 { 72 return ((_currentTime - _presetTimeA) > HARD_CUT_DELAY); 73 } 74 SmoothRatio()75 double TimeKeeper::SmoothRatio() 76 { 77 return (_currentTime - _presetTimeB) / _smoothDuration; 78 } IsSmoothing()79 bool TimeKeeper::IsSmoothing() 80 { 81 return _isSmoothing; 82 } 83 GetRunningTime()84 double TimeKeeper::GetRunningTime() 85 { 86 return _currentTime; 87 } 88 PresetProgressA()89 double TimeKeeper::PresetProgressA() 90 { 91 if (_isSmoothing) return 1.0; 92 else return (_currentTime - _presetTimeA) / _presetDurationA; 93 } PresetProgressB()94 double TimeKeeper::PresetProgressB() 95 { 96 return (_currentTime - _presetTimeB) / _presetDurationB; 97 } 98 PresetFrameB()99int TimeKeeper::PresetFrameB() 100 { 101 return _presetFrameB; 102 } 103 PresetFrameA()104int TimeKeeper::PresetFrameA() 105 { 106 return _presetFrameA; 107 } 108 sampledPresetDuration()109double TimeKeeper::sampledPresetDuration() { 110 #ifdef WIN32 111 return _presetDuration; 112 #else 113 return fmax(1, fmin(60, RandomNumberGenerators::gaussian 114 (_presetDuration, _easterEgg))); 115 #endif 116 } 117