00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
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
00048
00049
00050
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
00114 cx = 0.03+boxx;
00115
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
00130
00131 {
00132
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
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
00152 }
00153
00154
00155
00156
00157
00158
00159
00160
00161
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
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
00195
00196
00197 timerbox.Load("hud/timerbox.png", false);
00198
00199 loaded = true;
00200
00201 loaded = true;
00202 }