src/graphic/fps.cpp

Go to the documentation of this file.
00001 /******************************************************************************
00002  *  Wormux is a convivial mass murder game.
00003  *  Copyright (C) 2001-2004 Lawrence Azzoug.
00004  *
00005  *  This program is free software; you can redistribute it and/or modify
00006  *  it under the terms of the GNU General Public License as published by
00007  *  the Free Software Foundation; either version 2 of the License, or
00008  *  (at your option) any later version.
00009  *
00010  *  This program is distributed in the hope that it will be useful,
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  *  GNU General Public License for more details.
00014  *
00015  *  You should have received a copy of the GNU General Public License
00016  *  along with this program; if not, write to the Free Software
00017  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
00018  ******************************************************************************
00019  * Calculate frame per second.
00020  *****************************************************************************/
00021 
00022 #include "fps.h"
00023 #include <SDL.h>
00024 #include <sstream>
00025 #include <iomanip>
00026 #include "colors.h"
00027 #include "video.h"
00028 #include "text.h"
00029 #include "../include/app.h"
00030 #include "../tool/i18n.h"
00031 
00032 const uint FramePerSecond::MIN_NB_VALUES = 4;
00033   
00034 FramePerSecond::~FramePerSecond(){
00035   delete text;
00036 }    
00037 
00038 FramePerSecond::FramePerSecond(){
00039   text = NULL;
00040   display = true;
00041   average = -1;
00042   
00043   for( uint i=0; i<=MIN_NB_VALUES; ++i )
00044     nb_frames.push_back (0);
00045   
00046   time_in_second = 0;
00047   nb_valid_values = -1;
00048 }
00049 
00050 void FramePerSecond::Reset(){
00051   average = -1;
00052   nb_frames.clear();
00053   
00054   for( uint i=0; i<=MIN_NB_VALUES; ++i )
00055     nb_frames.push_back (0);
00056   
00057   time_in_second = SDL_GetTicks()+1000;
00058   nb_valid_values = -1;
00059 
00060   if(text == NULL)
00061     text = new Text("");
00062 }
00063 
00064 void FramePerSecond::AddOneFrame(){
00065   ++nb_frames.front();
00066 }
00067 
00068 void FramePerSecond::Refresh()
00069 {
00070   uint nv_temps = SDL_GetTicks();   
00071    
00072   // Pas encore l'heure de recalculer : exit !
00073   if (nv_temps <= time_in_second)
00074     return;
00075 
00076   // On décale !
00077   while (time_in_second < nv_temps){
00078     time_in_second += 1000;
00079     nb_frames.pop_back();
00080     nb_frames.push_front(0);
00081     if (nb_valid_values < (int)nb_frames.size()-1)
00082       nb_valid_values++;
00083   }
00084 
00085   // Recalcule la average
00086   if (0 < nb_valid_values){
00087     average = 0;
00088     std::list<uint>::const_iterator it=nb_frames.begin();
00089     ++it;
00090     for (int i=1; i<=nb_valid_values; ++i, ++it)
00091       average += *it;
00092     average /= nb_valid_values;
00093   }
00094 }
00095 
00096 void FramePerSecond::Draw(){
00097   if( !display )
00098     return;
00099   if( average < 0 )
00100     return;
00101   
00102   char buffer[20];
00103   
00104   snprintf(buffer, sizeof(buffer)-1, "%.1f", average);
00105   buffer[sizeof(buffer)-1] = '\0';
00106   text->Set (Format(_("%s fps"), buffer));
00107   text->DrawTopRight(AppWormux::GetInstance()->video.window.GetWidth()-1,0);
00108 }
00109 

Generated on Mon Jan 1 13:10:56 2007 for Wormux by  doxygen 1.4.7