FrameTime.cpp
Go to the documentation of this file.00001 //*** FrameTime.cpp ** 00002 00003 #include "FrameTime.h" 00004 #include "Platform_Time.h" 00005 00006 00007 //*** Constructor *** 00008 00009 FrameTime::FrameTime(): 00010 initialized_(false), 00011 previousFrameTime_(0), 00012 deltaTime_(0), 00013 frameCounter_(0) 00014 { 00015 } 00016 00017 00018 //*** Update *** 00019 00020 float FrameTime::Update() 00021 { 00022 // If this is the first call to Update, we need to initialize previousFrameTime_ 00023 if (!initialized_ && Platform::GetPlatform_Time()) 00024 { 00025 previousFrameTime_=Platform::GetPlatform_Time()->GetTime(); 00026 initialized_=true; 00027 } 00028 00029 // Increase framecounter 00030 frameCounter_++; 00031 00032 // Calculate deltatime 00033 float currentFrameTime=0; 00034 if (Platform::GetPlatform_Time()) 00035 { 00036 currentFrameTime=Platform::GetPlatform_Time()->GetTime(); 00037 } 00038 00039 deltaTime_=currentFrameTime-previousFrameTime_; 00040 previousFrameTime_=currentFrameTime; 00041 00042 // Cap deltatime if it is too high (or things will jump like crazy on occasional long stalls) 00043 if (deltaTime_>1.0f/10.0f) 00044 { 00045 deltaTime_=1.0f/60.0f; 00046 } 00047 00048 // Cap deltatime if it is negative or zero (this can actually happen due to floating point imprecisions) 00049 else if (deltaTime_<=0) 00050 { 00051 deltaTime_=1.0f/60.0f; 00052 } 00053 00054 return deltaTime_; 00055 } 00056 00057 00058 //*** GetDeltaTime *** 00059 00060 float FrameTime::GetDeltaTime() 00061 { 00062 return deltaTime_; 00063 } 00064 00065 00066 //*** GetFrameCounter *** 00067 00068 int FrameTime::GetFrameCounter() 00069 { 00070 return frameCounter_; 00071 }
Reproduction/republishing of any material on this site without permission is strictly prohibited.
