453 lines
14 KiB
C++
453 lines
14 KiB
C++
|
////////////////////////////////////////////////////////////
|
||
|
//
|
||
|
// SFML - Simple and Fast Multimedia Library
|
||
|
// Copyright (C) 2007-2009 Laurent Gomila (laurent.gom@gmail.com)
|
||
|
//
|
||
|
// This software is provided 'as-is', without any express or implied warranty.
|
||
|
// In no event will the authors be held liable for any damages arising from the use of this software.
|
||
|
//
|
||
|
// Permission is granted to anyone to use this software for any purpose,
|
||
|
// including commercial applications, and to alter it and redistribute it freely,
|
||
|
// subject to the following restrictions:
|
||
|
//
|
||
|
// 1. The origin of this software must not be misrepresented;
|
||
|
// you must not claim that you wrote the original software.
|
||
|
// If you use this software in a product, an acknowledgment
|
||
|
// in the product documentation would be appreciated but is not required.
|
||
|
//
|
||
|
// 2. Altered source versions must be plainly marked as such,
|
||
|
// and must not be misrepresented as being the original software.
|
||
|
//
|
||
|
// 3. This notice may not be removed or altered from any source distribution.
|
||
|
//
|
||
|
////////////////////////////////////////////////////////////
|
||
|
|
||
|
#ifndef SFML_TIME_HPP
|
||
|
#define SFML_TIME_HPP
|
||
|
|
||
|
////////////////////////////////////////////////////////////
|
||
|
// Headers
|
||
|
////////////////////////////////////////////////////////////
|
||
|
#include <SFML/Config.hpp>
|
||
|
|
||
|
|
||
|
namespace sf
|
||
|
{
|
||
|
////////////////////////////////////////////////////////////
|
||
|
/// \brief Represents a time value
|
||
|
///
|
||
|
////////////////////////////////////////////////////////////
|
||
|
class SFML_API Time
|
||
|
{
|
||
|
public :
|
||
|
|
||
|
////////////////////////////////////////////////////////////
|
||
|
/// \brief Default constructor
|
||
|
///
|
||
|
/// Sets the time value to zero.
|
||
|
///
|
||
|
////////////////////////////////////////////////////////////
|
||
|
Time();
|
||
|
|
||
|
////////////////////////////////////////////////////////////
|
||
|
/// \brief Return the time value as a number of seconds
|
||
|
///
|
||
|
/// \return Time in seconds
|
||
|
///
|
||
|
/// \see AsMilliseconds, AsMicroseconds
|
||
|
///
|
||
|
////////////////////////////////////////////////////////////
|
||
|
float AsSeconds() const;
|
||
|
|
||
|
////////////////////////////////////////////////////////////
|
||
|
/// \brief Return the time value as a number of milliseconds
|
||
|
///
|
||
|
/// \return Time in milliseconds
|
||
|
///
|
||
|
/// \see AsSeconds, AsMicroseconds
|
||
|
///
|
||
|
////////////////////////////////////////////////////////////
|
||
|
Int32 AsMilliseconds() const;
|
||
|
|
||
|
////////////////////////////////////////////////////////////
|
||
|
/// \brief Return the time value as a number of microseconds
|
||
|
///
|
||
|
/// \return Time in microseconds
|
||
|
///
|
||
|
/// \see AsSeconds, AsMilliseconds
|
||
|
///
|
||
|
////////////////////////////////////////////////////////////
|
||
|
Int64 AsMicroseconds() const;
|
||
|
|
||
|
////////////////////////////////////////////////////////////
|
||
|
// Static member data
|
||
|
////////////////////////////////////////////////////////////
|
||
|
static const Time Zero; ///< Predefined "zero" time value
|
||
|
|
||
|
private :
|
||
|
|
||
|
friend SFML_API Time Seconds(float);
|
||
|
friend SFML_API Time Milliseconds(Int32);
|
||
|
friend SFML_API Time Microseconds(Int64);
|
||
|
|
||
|
////////////////////////////////////////////////////////////
|
||
|
/// \brief Construct from a number of microseconds
|
||
|
///
|
||
|
/// This function is internal. To construct time values,
|
||
|
/// use sf::Seconds, sf::Milliseconds or sf::Microseconds instead.
|
||
|
///
|
||
|
/// \param microseconds Number of microseconds
|
||
|
///
|
||
|
////////////////////////////////////////////////////////////
|
||
|
explicit Time(Int64 microseconds);
|
||
|
|
||
|
private :
|
||
|
|
||
|
////////////////////////////////////////////////////////////
|
||
|
// Member data
|
||
|
////////////////////////////////////////////////////////////
|
||
|
Int64 myMicroseconds; ///< Time value stored as microseconds
|
||
|
};
|
||
|
|
||
|
////////////////////////////////////////////////////////////
|
||
|
/// \relates Time
|
||
|
/// \brief Construct a time value from a number of seconds
|
||
|
///
|
||
|
/// \param amount Number of seconds
|
||
|
///
|
||
|
/// \return Time value constructed from the amount of seconds
|
||
|
///
|
||
|
/// \see Milliseconds, Microseconds
|
||
|
///
|
||
|
////////////////////////////////////////////////////////////
|
||
|
SFML_API Time Seconds(float amount);
|
||
|
|
||
|
////////////////////////////////////////////////////////////
|
||
|
/// \relates Time
|
||
|
/// \brief Construct a time value from a number of milliseconds
|
||
|
///
|
||
|
/// \param amount Number of milliseconds
|
||
|
///
|
||
|
/// \return Time value constructed from the amount of milliseconds
|
||
|
///
|
||
|
/// \see Seconds, Microseconds
|
||
|
///
|
||
|
////////////////////////////////////////////////////////////
|
||
|
SFML_API Time Milliseconds(Int32 amount);
|
||
|
|
||
|
////////////////////////////////////////////////////////////
|
||
|
/// \relates Time
|
||
|
/// \brief Construct a time value from a number of microseconds
|
||
|
///
|
||
|
/// \param amount Number of microseconds
|
||
|
///
|
||
|
/// \return Time value constructed from the amount of microseconds
|
||
|
///
|
||
|
/// \see Seconds, Milliseconds
|
||
|
///
|
||
|
////////////////////////////////////////////////////////////
|
||
|
SFML_API Time Microseconds(Int64 amount);
|
||
|
|
||
|
////////////////////////////////////////////////////////////
|
||
|
/// \relates Time
|
||
|
/// \brief Overload of == operator to compare two time values
|
||
|
///
|
||
|
/// \param left Left operand (a time)
|
||
|
/// \param right Right operand (a time)
|
||
|
///
|
||
|
/// \return True if both time values are equal
|
||
|
///
|
||
|
////////////////////////////////////////////////////////////
|
||
|
SFML_API bool operator ==(const Time& left, const Time& right);
|
||
|
|
||
|
////////////////////////////////////////////////////////////
|
||
|
/// \relates Time
|
||
|
/// \brief Overload of != operator to compare two time values
|
||
|
///
|
||
|
/// \param left Left operand (a time)
|
||
|
/// \param right Right operand (a time)
|
||
|
///
|
||
|
/// \return True if both time values are different
|
||
|
///
|
||
|
////////////////////////////////////////////////////////////
|
||
|
SFML_API bool operator !=(const Time& left, const Time& right);
|
||
|
|
||
|
////////////////////////////////////////////////////////////
|
||
|
/// \relates Time
|
||
|
/// \brief Overload of < operator to compare two time values
|
||
|
///
|
||
|
/// \param left Left operand (a time)
|
||
|
/// \param right Right operand (a time)
|
||
|
///
|
||
|
/// \return True if \a left is lesser than \a right
|
||
|
///
|
||
|
////////////////////////////////////////////////////////////
|
||
|
SFML_API bool operator <(const Time& left, const Time& right);
|
||
|
|
||
|
////////////////////////////////////////////////////////////
|
||
|
/// \relates Time
|
||
|
/// \brief Overload of > operator to compare two time values
|
||
|
///
|
||
|
/// \param left Left operand (a time)
|
||
|
/// \param right Right operand (a time)
|
||
|
///
|
||
|
/// \return True if \a left is greater than \a right
|
||
|
///
|
||
|
////////////////////////////////////////////////////////////
|
||
|
SFML_API bool operator >(const Time& left, const Time& right);
|
||
|
|
||
|
////////////////////////////////////////////////////////////
|
||
|
/// \relates Time
|
||
|
/// \brief Overload of <= operator to compare two time values
|
||
|
///
|
||
|
/// \param left Left operand (a time)
|
||
|
/// \param right Right operand (a time)
|
||
|
///
|
||
|
/// \return True if \a left is lesser or equal than \a right
|
||
|
///
|
||
|
////////////////////////////////////////////////////////////
|
||
|
SFML_API bool operator <=(const Time& left, const Time& right);
|
||
|
|
||
|
////////////////////////////////////////////////////////////
|
||
|
/// \relates Time
|
||
|
/// \brief Overload of >= operator to compare two time values
|
||
|
///
|
||
|
/// \param left Left operand (a time)
|
||
|
/// \param right Right operand (a time)
|
||
|
///
|
||
|
/// \return True if \a left is greater or equal than \a right
|
||
|
///
|
||
|
////////////////////////////////////////////////////////////
|
||
|
SFML_API bool operator >=(const Time& left, const Time& right);
|
||
|
|
||
|
////////////////////////////////////////////////////////////
|
||
|
/// \relates Time
|
||
|
/// \brief Overload of unary - operator to negate a time value
|
||
|
///
|
||
|
/// \param right Right operand (a time)
|
||
|
///
|
||
|
/// \return Opposite of the time value
|
||
|
///
|
||
|
////////////////////////////////////////////////////////////
|
||
|
SFML_API Time operator -(const Time& right);
|
||
|
|
||
|
////////////////////////////////////////////////////////////
|
||
|
/// \relates Time
|
||
|
/// \brief Overload of binary + operator to add two time values
|
||
|
///
|
||
|
/// \param left Left operand (a time)
|
||
|
/// \param right Right operand (a time)
|
||
|
///
|
||
|
/// \return Sum of the two times values
|
||
|
///
|
||
|
////////////////////////////////////////////////////////////
|
||
|
SFML_API Time operator +(const Time& left, const Time& right);
|
||
|
|
||
|
////////////////////////////////////////////////////////////
|
||
|
/// \relates Time
|
||
|
/// \brief Overload of binary += operator to add/assign two time values
|
||
|
///
|
||
|
/// \param left Left operand (a time)
|
||
|
/// \param right Right operand (a time)
|
||
|
///
|
||
|
/// \return Sum of the two times values
|
||
|
///
|
||
|
////////////////////////////////////////////////////////////
|
||
|
SFML_API Time& operator +=(Time& left, const Time& right);
|
||
|
|
||
|
////////////////////////////////////////////////////////////
|
||
|
/// \relates Time
|
||
|
/// \brief Overload of binary - operator to subtract two time values
|
||
|
///
|
||
|
/// \param left Left operand (a time)
|
||
|
/// \param right Right operand (a time)
|
||
|
///
|
||
|
/// \return Difference of the two times values
|
||
|
///
|
||
|
////////////////////////////////////////////////////////////
|
||
|
SFML_API Time operator -(const Time& left, const Time& right);
|
||
|
|
||
|
////////////////////////////////////////////////////////////
|
||
|
/// \relates Time
|
||
|
/// \brief Overload of binary -= operator to subtract/assign two time values
|
||
|
///
|
||
|
/// \param left Left operand (a time)
|
||
|
/// \param right Right operand (a time)
|
||
|
///
|
||
|
/// \return Difference of the two times values
|
||
|
///
|
||
|
////////////////////////////////////////////////////////////
|
||
|
SFML_API Time& operator -=(Time& left, const Time& right);
|
||
|
|
||
|
////////////////////////////////////////////////////////////
|
||
|
/// \relates Time
|
||
|
/// \brief Overload of binary * operator to scale a time value
|
||
|
///
|
||
|
/// \param left Left operand (a time)
|
||
|
/// \param right Right operand (a number)
|
||
|
///
|
||
|
/// \return \left multiplied by \right
|
||
|
///
|
||
|
////////////////////////////////////////////////////////////
|
||
|
SFML_API Time operator *(const Time& left, float right);
|
||
|
|
||
|
////////////////////////////////////////////////////////////
|
||
|
/// \relates Time
|
||
|
/// \brief Overload of binary * operator to scale a time value
|
||
|
///
|
||
|
/// \param left Left operand (a time)
|
||
|
/// \param right Right operand (a number)
|
||
|
///
|
||
|
/// \return \left multiplied by \right
|
||
|
///
|
||
|
////////////////////////////////////////////////////////////
|
||
|
SFML_API Time operator *(const Time& left, Int64 right);
|
||
|
|
||
|
////////////////////////////////////////////////////////////
|
||
|
/// \relates Time
|
||
|
/// \brief Overload of binary * operator to scale a time value
|
||
|
///
|
||
|
/// \param left Left operand (a number)
|
||
|
/// \param right Right operand (a time)
|
||
|
///
|
||
|
/// \return \left multiplied by \right
|
||
|
///
|
||
|
////////////////////////////////////////////////////////////
|
||
|
SFML_API Time operator *(float left, const Time& right);
|
||
|
|
||
|
////////////////////////////////////////////////////////////
|
||
|
/// \relates Time
|
||
|
/// \brief Overload of binary * operator to scale a time value
|
||
|
///
|
||
|
/// \param left Left operand (a number)
|
||
|
/// \param right Right operand (a time)
|
||
|
///
|
||
|
/// \return \left multiplied by \right
|
||
|
///
|
||
|
////////////////////////////////////////////////////////////
|
||
|
SFML_API Time operator *(Int64 left, const Time& right);
|
||
|
|
||
|
////////////////////////////////////////////////////////////
|
||
|
/// \relates Time
|
||
|
/// \brief Overload of binary *= operator to scale/assign a time value
|
||
|
///
|
||
|
/// \param left Left operand (a time)
|
||
|
/// \param right Right operand (a number)
|
||
|
///
|
||
|
/// \return \left multiplied by \right
|
||
|
///
|
||
|
////////////////////////////////////////////////////////////
|
||
|
SFML_API Time& operator *=(Time& left, float right);
|
||
|
|
||
|
////////////////////////////////////////////////////////////
|
||
|
/// \relates Time
|
||
|
/// \brief Overload of binary *= operator to scale/assign a time value
|
||
|
///
|
||
|
/// \param left Left operand (a time)
|
||
|
/// \param right Right operand (a number)
|
||
|
///
|
||
|
/// \return \left multiplied by \right
|
||
|
///
|
||
|
////////////////////////////////////////////////////////////
|
||
|
SFML_API Time& operator *=(Time& left, Int64 right);
|
||
|
|
||
|
////////////////////////////////////////////////////////////
|
||
|
/// \relates Time
|
||
|
/// \brief Overload of binary / operator to scale a time value
|
||
|
///
|
||
|
/// \param left Left operand (a time)
|
||
|
/// \param right Right operand (a number)
|
||
|
///
|
||
|
/// \return \left divided by \right
|
||
|
///
|
||
|
////////////////////////////////////////////////////////////
|
||
|
SFML_API Time operator /(const Time& left, float right);
|
||
|
|
||
|
////////////////////////////////////////////////////////////
|
||
|
/// \relates Time
|
||
|
/// \brief Overload of binary / operator to scale a time value
|
||
|
///
|
||
|
/// \param left Left operand (a time)
|
||
|
/// \param right Right operand (a number)
|
||
|
///
|
||
|
/// \return \left divided by \right
|
||
|
///
|
||
|
////////////////////////////////////////////////////////////
|
||
|
SFML_API Time operator /(const Time& left, Int64 right);
|
||
|
|
||
|
////////////////////////////////////////////////////////////
|
||
|
/// \relates Time
|
||
|
/// \brief Overload of binary /= operator to scale/assign a time value
|
||
|
///
|
||
|
/// \param left Left operand (a time)
|
||
|
/// \param right Right operand (a number)
|
||
|
///
|
||
|
/// \return \left divided by \right
|
||
|
///
|
||
|
////////////////////////////////////////////////////////////
|
||
|
SFML_API Time& operator /=(Time& left, float right);
|
||
|
|
||
|
////////////////////////////////////////////////////////////
|
||
|
/// \relates Time
|
||
|
/// \brief Overload of binary /= operator to scale/assign a time value
|
||
|
///
|
||
|
/// \param left Left operand (a time)
|
||
|
/// \param right Right operand (a number)
|
||
|
///
|
||
|
/// \return \left divided by \right
|
||
|
///
|
||
|
////////////////////////////////////////////////////////////
|
||
|
SFML_API Time& operator /=(Time& left, Int64 right);
|
||
|
|
||
|
} // namespace sf
|
||
|
|
||
|
|
||
|
#endif // SFML_TIME_HPP
|
||
|
|
||
|
|
||
|
////////////////////////////////////////////////////////////
|
||
|
/// \class sf::Time
|
||
|
/// \ingroup system
|
||
|
///
|
||
|
/// sf::Time encapsulates a time value in a flexible way.
|
||
|
/// It allows to define a time value either as a number of
|
||
|
/// seconds, milliseconds or microseconds. It also works the
|
||
|
/// other way round: you can read a time value as either
|
||
|
/// a number of seconds, milliseconds or microseconds.
|
||
|
///
|
||
|
/// By using such a flexible interface, the API doesn't
|
||
|
/// impose any fixed type or resolution for time values,
|
||
|
/// and let the user choose its own favorite representation.
|
||
|
///
|
||
|
/// Time values support the usual mathematical operations:
|
||
|
/// you can add or subtract two times, multiply or divide
|
||
|
/// a time by a number, compare two times, etc.
|
||
|
///
|
||
|
/// Since they represent a time span and not an absolute time
|
||
|
/// value, times can also be negative.
|
||
|
///
|
||
|
/// Usage example:
|
||
|
/// \code
|
||
|
/// sf::Time t1 = sf::Seconds(0.1f);
|
||
|
/// Int32 milli = t1.AsMilliseconds(); // 100
|
||
|
///
|
||
|
/// sf::Time t2 = sf::Milliseconds(30);
|
||
|
/// Int64 micro = t2.AsMicroseconds(); // 30000
|
||
|
///
|
||
|
/// sf::Time t3 = sf::Microseconds(-800000);
|
||
|
/// float sec = t3.AsSeconds(); // -0.8
|
||
|
/// \endcode
|
||
|
///
|
||
|
/// \code
|
||
|
/// void Update(sf::Time elapsed)
|
||
|
/// {
|
||
|
/// position += speed * elapsed.AsSeconds();
|
||
|
/// }
|
||
|
///
|
||
|
/// Update(sf::Milliseconds(100));
|
||
|
/// \endcode
|
||
|
///
|
||
|
/// \see sf::Clock
|
||
|
///
|
||
|
////////////////////////////////////////////////////////////
|