00001 /* 00002 * FrameRateTracker.cpp 00003 * 00004 * Part of Fly! Legacy project 00005 * 00006 * Copyright 2005 Chris Wallace 00007 * 00008 * Fly! Legacy is free software; you can redistribute it and/or modify 00009 * it under the terms of the GNU General Public License as published by 00010 * the Free Software Foundation; either version 2 of the License, or 00011 * (at your option) any later version. 00012 * 00013 * Fly! Legacy is distributed in the hope that it will be useful, 00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00016 * GNU General Public License for more details. 00017 * 00018 * You should have received a copy of the GNU General Public License 00019 * along with Fly! Legacy; if not, write to the Free Software 00020 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00021 * 00022 */ 00023 00039 #include "../Include/FrameRateTracker.h" 00040 #include "../Include/FlyLegacy.h" 00041 #include "../Include/Ui.h" 00042 00043 00044 CFrameRateTracker::CFrameRateTracker (unsigned int maxSamples) 00045 { 00046 this->maxSamples = maxSamples; 00047 this->actualSamples = 0; 00048 this->nextSample = 0; 00049 samples = new float[maxSamples]; 00050 } 00051 00052 CFrameRateTracker::~CFrameRateTracker (void) 00053 { 00054 if (samples != NULL) delete[] samples; 00055 } 00056 00057 void CFrameRateTracker::SetMaxSamples (unsigned int newMaxSamples) 00058 { 00059 // Allocate new buffer 00060 float *newSamples = new float[newMaxSamples]; 00061 00062 // Copy samples from old buffer to the new buffer; if the new number of max samples 00063 // is less than the actual number in-use in the old buffer then only the most 00064 // recent are copied 00065 unsigned int nSamples = actualSamples; 00066 if (nSamples > newMaxSamples) nSamples = newMaxSamples; 00067 00068 for (unsigned int i=0; i<nSamples; i++) { 00069 newSamples[i] = samples[(nextSample-nSamples+i) % maxSamples]; 00070 } 00071 00072 // Deallocate old samples buffer 00073 delete[] samples; 00074 00075 // Set class members to reflect new buffer state; note actualSamples does not change 00076 this->maxSamples = maxSamples; 00077 this->nextSample = nSamples; 00078 this->samples = newSamples; 00079 } 00080 00081 unsigned int CFrameRateTracker::GetMaxSamples (void) 00082 { 00083 return maxSamples; 00084 } 00085 00086 unsigned int CFrameRateTracker::GetActualSamples (void) 00087 { 00088 return actualSamples; 00089 } 00090 00091 float CFrameRateTracker::GetSample (unsigned int i) 00092 { 00093 int iSample = (maxSamples + nextSample - actualSamples + i) % maxSamples; 00094 return samples[iSample]; 00095 } 00096 00097 void CFrameRateTracker::GetSamples (float *outSamples) 00098 { 00099 for (unsigned int i=0; i<actualSamples; i++) { 00100 outSamples[i] = GetSample (i); 00101 } 00102 } 00103 00104 void CFrameRateTracker::AddSample (float f) 00105 { 00106 samples[nextSample] = f; 00107 if (actualSamples < maxSamples) { 00108 actualSamples++; 00109 } 00110 nextSample = (nextSample + 1) % maxSamples; 00111 } 00112 00113 void CFrameRateTracker::ClearSamples (void) 00114 { 00115 nextSample = 0; 00116 actualSamples = 0; 00117 }
|
|
Documentation generated by
|