src/timer.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *            timer.cpp
00003  *
00004  *  Wed Jun  1 21:31:45 2005
00005  *  Copyright  2005  Joe Venzon
00006  *  joe@venzon.net
00007  ****************************************************************************/
00008 
00009 /*
00010  *  This program is free software; you can redistribute it and/or modify
00011  *  it under the terms of the GNU General Public License as published by
00012  *  the Free Software Foundation; either version 2 of the License, or
00013  *  (at your option) any later version.
00014  *
00015  *  This program is distributed in the hope that it will be useful,
00016  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  *  GNU Library General Public License for more details.
00019  *
00020  *  You should have received a copy of the GNU General Public License
00021  *  along with this program; if not, write to the Free Software
00022  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00023  */
00024  
00025 #include "timer.h"
00026 
00027 TIMER::TIMER()
00028 {
00029         time = 0.0;
00030         pretime = 0.0;
00031         lastlap = 0.0;
00032         bestlap = 0.0;
00033         
00034         ifstream blf;
00035         blf.open((settings.GetSettingsDir() + "/bestlap.txt").c_str());
00036         if (blf)
00037         {
00038                 blf >> bestlap;
00039                 blf.close();
00040         }
00041         
00042         loaded = false;
00043 }
00044 
00045 TIMER::~TIMER()
00046 {
00047         //if (loaded)
00048                 //glDeleteTextures(1, &timerbox);
00049         //textures.Delete(timerbox);
00050         //textures.Delete("hud/timerbox.png");
00051         timerbox.Unload();
00052 }
00053 
00054 void TIMER::Lap(bool storelastlap)
00055 {       
00056         if (time > 0.5 && storelastlap)
00057         {
00058                 lastlap = time;
00059         
00060                 if (lastlap < bestlap || bestlap == 0.0)
00061                 {
00062                         bestlap = lastlap;
00063                         ofstream blf;
00064                         blf.open((settings.GetSettingsDir() + "/bestlap.txt").c_str());
00065                         blf << bestlap << endl;
00066                         blf.close();
00067                 }
00068         }
00069         
00070         time = 0.0;
00071 }
00072 
00073 float TIMER::GetTimer()
00074 {
00075         return time;
00076 }
00077 
00078 void TIMER::TickTimer(float timefactor, float fps)
00079 {
00080         if (state.GetGameState() == STATE_PLAYING)
00081                 time += timefactor / fps;
00082         else if (state.GetGameState() == STATE_STAGING)
00083         {
00084                 pretime += timefactor / fps;
00085                 
00086                 if (pretime > STAGING_TIME)
00087                 {
00088                         time = 0.0;
00089                         pretime = 0.0;
00090                         state.SetGameState(STATE_PLAYING);
00091                 }
00092         }
00093 }
00094 
00095 void TIMER::Draw()
00096 {
00097         float boxx, boxy;
00098         
00099         boxx = 0.01;
00100         boxy = 0.01;
00101         
00102         utility.Draw2D(boxx, boxy, 0.5+boxx, 0.5+boxy, &timerbox);
00103         
00104         char tempchar[256];
00105         
00106         float secs;
00107         int min;
00108         
00109         float r,g,b;
00110         r=g=b=0.0;
00111         
00112         float cx, cy;
00113 //      float vertspacing = 0.03;
00114         cx = 0.03+boxx;
00115         //cy = 0.005;
00116         cy = 0.0214+boxy;
00117         if (state.GetGameState() == STATE_STAGING)
00118         {
00119                 SecsToSecsMin(STAGING_TIME - pretime, secs, min);
00120                 sprintf(tempchar, "%02d:%06.3f", min, secs);
00121                 font.Print(cx, cy, tempchar, 0, 6, 1,0,0);
00122         }
00123         else
00124         {
00125                 SecsToSecsMin(time, secs, min);
00126                 sprintf(tempchar, "%02d:%06.3f", min, secs);
00127                 font.Print(cx, cy, tempchar, 0, 6, r,g,b);
00128         }
00129         //cy += vertspacing;
00130         
00131         {
00132                 //cx = 0.195+boxx;
00133                 cx = 0.193+boxx;
00134                 SecsToSecsMin(lastlap, secs, min);
00135                 if (lastlap != 0.0)
00136                         sprintf(tempchar, "%02d:%06.3f", min, secs);
00137                 else
00138                         sprintf(tempchar, "--:--.---");
00139                 font.Print(cx, cy, tempchar, 0, 6, r,g,b);
00140                 //cy += vertspacing;
00141         }
00142         
00143         {
00144                 cx = 0.35+boxx;
00145                 SecsToSecsMin(bestlap, secs, min);
00146                 if (bestlap != 0.0)
00147                         sprintf(tempchar, "%02d:%06.3f", min, secs);
00148                 else
00149                         sprintf(tempchar, "--:--.---");
00150                 font.Print(cx, cy, tempchar, 0, 6, r,g,b);
00151                 //cy += vertspacing;
00152         }
00153         
00154         /*if (state.GetGameState() == STATE_STAGING)
00155         {
00156                 cx = 0.03+boxx;
00157                 cy = 0.0214+boxy;
00158                 cy += 0.1;
00159                 SecsToSecsMin(pretime, secs, min);
00160                 sprintf(tempchar, "%02d:%06.3f", min, secs);
00161                 font.Print(cx, cy, tempchar, 0, 6, 1,0,0);
00162         }*/
00163 }
00164 
00165 void TIMER::SecsToSecsMin(double t, float &secs, int &min)
00166 {
00167         min = (int) t / 60;
00168         secs = t - min*60;
00169 }
00170 
00171 void TIMER::ResetBest()
00172 {
00173         bestlap = 0.0;
00174         //bestlap = lastlap;
00175         ofstream blf;
00176         blf.open((settings.GetSettingsDir() + "bestlap.txt").c_str());
00177         blf << bestlap << endl;
00178         blf.close();
00179 }
00180 
00181 void TIMER::ResetLast()
00182 {
00183         lastlap = 0.0;
00184 }
00185 
00186 void TIMER::Reset()
00187 {
00188         time = 0.0;
00189         pretime = 0.0;
00190 }
00191 
00192 void TIMER::Load()
00193 {
00194         //if (loaded)
00195                 //glDeleteTextures(1, &timerbox);
00196         
00197         timerbox.Load("hud/timerbox.png", false);
00198         
00199         loaded = true;
00200         
00201         loaded = true;
00202 }

Generated on Thu Oct 19 04:05:54 2006 by  doxygen 1.4.6