Moved timeToString into StringUtils.

git-svn-id: svn+ssh://svn.code.sf.net/p/supertuxkart/code/main/branches/irrlicht@3711 178a84e3-b1eb-0310-8ba1-8eac791a3b58
This commit is contained in:
hikerstk
2009-07-08 12:34:39 +00:00
parent e8c70f73af
commit f332c3a345
6 changed files with 77 additions and 43 deletions

View File

@@ -30,6 +30,7 @@
#include "race/race_manager.hpp"
#include "tracks/track.hpp"
#include "utils/constants.hpp"
#include "utils/string_utils.hpp"
#include "utils/translation.hpp"
#include "irrlicht.h"
@@ -42,13 +43,19 @@ RaceGUI* getRaceGUI()
return instance;
}
void timeToString(const double TIME, char *s)
// ----------------------------------------------------------------------------
/** Converts a time into a string.
* \param time Time in seconds.
* \param s Output string.
*/
void XXtimeToString(const double TIME, char *s)
{
int min = (int) floor ( TIME / 60.0 ) ;
int sec = (int) floor ( TIME - (double) ( 60 * min ) ) ;
int tenths = (int) floor ( 10.0f * (TIME - (double)(sec + 60* min)));
sprintf ( s, "%d:%02d:%d", min, sec, tenths ) ;
} // TimeToString
// ----------------------------------------------------------------------------
RaceGUI::RaceGUI()
@@ -143,11 +150,9 @@ void RaceGUI::drawTimer ()
assert(RaceManager::getWorld() != NULL);
if(!RaceManager::getWorld()->shouldDrawTimer()) return;
char str[256];
timeToString(RaceManager::getWorld()->getTime(), str);
std::string s = StringUtils::timeToString(RaceManager::getWorld()->getTime());
#ifdef HAVE_IRRLICHT
m_time->setText(core::stringw(str).c_str());
m_time->setText(core::stringw(s.c_str()).c_str());
#else
font_race->PrintShadow(str, 60, UserConfigParams::m_width-260,
UserConfigParams::m_height-64);

View File

@@ -24,6 +24,7 @@
#include "network/network_manager.hpp"
#include "tracks/track.hpp"
#include "utils/constants.hpp"
#include "utils/string_utils.hpp"
#include "utils/translation.hpp"
//-----------------------------------------------------------------------------
@@ -254,8 +255,7 @@ void LinearWorld::newLap(unsigned int kart_index)
{
m->addMessage(_("New fastest lap"), NULL,
2.0f, 40, 100, 210, 100);
char s[20];
timeToString(time_per_lap, s);
std::string s = StringUtils::timeToString(time_per_lap);
std::ostringstream m_fastest_lap_message;
m_fastest_lap_message << s << ": " << kart->getName();
@@ -363,11 +363,10 @@ KartIconDisplayInfo* LinearWorld::getKartsDisplayInfo(const RaceGUI* caller)
(getTime() - getTimeAtLapForKart(kart->getWorldKartId())<5.0f || rank_info.lap != laps_of_leader) &&
raceHasLaps())
{ // Display for 5 seconds
char str[256];
std::string str;
if(position==1)
{
str[0]=' '; str[1]=0;
timeToString(getTimeAtLapForKart(kart->getWorldKartId()), str+1);
str = " "+StringUtils::timeToString(getTimeAtLapForKart(kart->getWorldKartId()));
}
else
{
@@ -376,8 +375,7 @@ KartIconDisplayInfo* LinearWorld::getKartsDisplayInfo(const RaceGUI* caller)
? getTimeAtLapForKart(kart->getWorldKartId())
: getTime())
- time_of_leader;
str[0]='+'; str[1]=0;
timeToString(timeBehind, str+1);
str="+"+StringUtils::timeToString(timeBehind);
}
rank_info.time = str;
}

View File

@@ -24,27 +24,6 @@
struct KartIconDisplayInfo;
class RaceGUI;
/** Some additional info that needs to be kept for each kart
* in this kind of race.
*/
struct KartInfo
{
int m_race_lap; /**<Number of finished(!) laps. */
float m_time_at_last_lap; /**<Time at finishing last lap. */
float m_lap_start_time; /**<Time at start of a new lap. */
float m_estimated_finish; /**<During last lap only:
* estimated finishing time! */
int m_track_sector; /**<Index in driveline, special values
* e.g. UNKNOWN_SECTOR can be negative!*/
int m_last_valid_sector; /* used when rescusing, e.g. for invalid shortcuts */
int m_last_valid_race_lap; /* when a kart is rescued, we need to give it back the number of lap it had */
Vec3 m_curr_track_coords;
bool m_on_road; // true if the kart is on top of the
// road path drawn by the drivelines
};
/*
* A 'linear world' is a subcategory of world used in 'standard' races, i.e.
@@ -53,6 +32,29 @@ struct KartInfo
*/
class LinearWorld : public World
{
private:
/** Some additional info that needs to be kept for each kart
* in this kind of race.
*/
struct KartInfo
{
int m_race_lap; /**<Number of finished(!) laps. */
float m_time_at_last_lap; /**<Time at finishing last lap. */
float m_lap_start_time; /**<Time at start of a new lap. */
float m_estimated_finish; /**<During last lap only:
* estimated finishing time! */
int m_track_sector; /**<Index in driveline, special values
* e.g. UNKNOWN_SECTOR can be negative!*/
int m_last_valid_sector; /* used when rescusing, e.g. for invalid shortcuts */
int m_last_valid_race_lap; /* when a kart is rescued, we need to give it back the number of lap it had */
Vec3 m_curr_track_coords;
bool m_on_road; // true if the kart is on top of the
// road path drawn by the drivelines
};
protected:
KartIconDisplayInfo* m_kart_display_info;
@@ -102,6 +104,11 @@ public:
/** Called by the race result GUI at the end of the race to know the final order
(fill in the 'order' array) */
virtual void raceResultOrder( int* order );
/** Returns true if the kart is on a valid driveline quad.
* \param kart_index Index of the kart.
*/
bool isOnRoad(unsigned int kart_index) const
{ return m_kart_info[kart_index].m_on_road; }
};
#endif

View File

@@ -289,11 +289,11 @@ void DefaultRobot::handleBraking()
}
const float MIN_SPEED = 5.0f;
KartInfo &kart_info = m_world->m_kart_info[ getWorldKartId() ];
//We may brake if we are about to get out of the road, but only if the
//kart is on top of the road, and if we won't slow down below a certain
//limit.
if ( m_crashes.m_road && kart_info.m_on_road && getVelocityLC().getY() > MIN_SPEED)
if (m_crashes.m_road && getVelocityLC().getY() > MIN_SPEED &&
m_world->isOnRoad(getWorldKartId()) )
{
float kart_ang_diff =
m_quad_graph->getAngleToNext(m_track_node,

View File

@@ -20,6 +20,7 @@
#include "utils/string_utils.hpp"
#include "math.h"
#include <algorithm>
#include <cstring>
@@ -34,7 +35,7 @@ namespace StringUtils
// g++ versions (at least 2.95.3), which have a wrong template. To
// avoid this issue, a more C-traditional way is used.
return strcmp(lhs.c_str()+(lhs.length()-rhs.length()), rhs.c_str())==0;
}
} // has_suffix
//--------------------------i---------------------------------------------------
std::string path(const std::string& filename)
@@ -47,7 +48,7 @@ namespace StringUtils
}
}
return "";
}
} // path
//-----------------------------------------------------------------------------
std::string basename(const std::string& filename)
@@ -60,7 +61,8 @@ namespace StringUtils
}
}
return filename;
}
} // basename
//-----------------------------------------------------------------------------
std::string without_extension(const std::string& filename)
{
@@ -72,7 +74,7 @@ namespace StringUtils
}
}
return filename;
}
} // without_extension
//-----------------------------------------------------------------------------
std::string extension(const std::string& filename)
@@ -85,7 +87,7 @@ namespace StringUtils
}
}
return filename;
}
} // extension
//-----------------------------------------------------------------------------
std::string upcase (const std::string& str)
@@ -93,7 +95,7 @@ namespace StringUtils
std::string name = str;
std::transform(name.begin(), name.end(), name.begin(), ::toupper);
return name;
}
} // upcase
//-----------------------------------------------------------------------------
std::string downcase (const std::string& str)
@@ -101,7 +103,8 @@ namespace StringUtils
std::string name = str;
std::transform(name.begin(), name.end(), name.begin(), ::tolower);
return name;
}
} // downcase
//-----------------------------------------------------------------------------
// Splits a string into substrings separated by a certain character, and
// returns a std::vector of all those substring. E.g.:
@@ -126,7 +129,23 @@ namespace StringUtils
}
}
return result;
}
} // split
// ------------------------------------------------------------------------
/** Converts a time in seconds into a string of the form mm:ss:hh (minutes,
* seconds, 1/100 seconds.
* \param time Time in seconds.
*/
std::string timeToString(float time)
{
int min = (int) floor ( time / 60.0 ) ;
int sec = (int) floor ( time - (double) ( 60 * min ) ) ;
int tenths = (int) floor ( 10.0f * (time - (double)(sec + 60* min)));
char s[9];
sprintf ( s, "%d:%02d:%d", min, sec, tenths ) ;
return std::string(s);
} // timeToString
} // namespace StringUtils
/* EOF */

View File

@@ -47,6 +47,11 @@ namespace StringUtils
return oss.str();
}
/** Converts a time in seconds into a string of the form mm:ss:hh (minutes,
* seconds, 1/100 seconds.
* \param time Time in seconds.
*/
std::string timeToString(float time);
/** Convert the contents in string \a rep to type \a T, if conversion
fails false is returned and the value of \a x is unchanged, if
true is returned the conversation was successfull. */