Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-06-02 08:58:14

0001 ////////////////////////////////////////////////////////////
0002 //
0003 // SFML - Simple and Fast Multimedia Library
0004 // Copyright (C) 2007-2023 Laurent Gomila (laurent@sfml-dev.org)
0005 //
0006 // This software is provided 'as-is', without any express or implied warranty.
0007 // In no event will the authors be held liable for any damages arising from the use of this software.
0008 //
0009 // Permission is granted to anyone to use this software for any purpose,
0010 // including commercial applications, and to alter it and redistribute it freely,
0011 // subject to the following restrictions:
0012 //
0013 // 1. The origin of this software must not be misrepresented;
0014 //    you must not claim that you wrote the original software.
0015 //    If you use this software in a product, an acknowledgment
0016 //    in the product documentation would be appreciated but is not required.
0017 //
0018 // 2. Altered source versions must be plainly marked as such,
0019 //    and must not be misrepresented as being the original software.
0020 //
0021 // 3. This notice may not be removed or altered from any source distribution.
0022 //
0023 ////////////////////////////////////////////////////////////
0024 
0025 #ifndef SFML_TIME_HPP
0026 #define SFML_TIME_HPP
0027 
0028 ////////////////////////////////////////////////////////////
0029 // Headers
0030 ////////////////////////////////////////////////////////////
0031 #include <SFML/System/Export.hpp>
0032 
0033 
0034 namespace sf
0035 {
0036 ////////////////////////////////////////////////////////////
0037 /// \brief Represents a time value
0038 ///
0039 ////////////////////////////////////////////////////////////
0040 class SFML_SYSTEM_API Time
0041 {
0042 public:
0043 
0044     ////////////////////////////////////////////////////////////
0045     /// \brief Default constructor
0046     ///
0047     /// Sets the time value to zero.
0048     ///
0049     ////////////////////////////////////////////////////////////
0050     Time();
0051 
0052     ////////////////////////////////////////////////////////////
0053     /// \brief Return the time value as a number of seconds
0054     ///
0055     /// \return Time in seconds
0056     ///
0057     /// \see asMilliseconds, asMicroseconds
0058     ///
0059     ////////////////////////////////////////////////////////////
0060     float asSeconds() const;
0061 
0062     ////////////////////////////////////////////////////////////
0063     /// \brief Return the time value as a number of milliseconds
0064     ///
0065     /// \return Time in milliseconds
0066     ///
0067     /// \see asSeconds, asMicroseconds
0068     ///
0069     ////////////////////////////////////////////////////////////
0070     Int32 asMilliseconds() const;
0071 
0072     ////////////////////////////////////////////////////////////
0073     /// \brief Return the time value as a number of microseconds
0074     ///
0075     /// \return Time in microseconds
0076     ///
0077     /// \see asSeconds, asMilliseconds
0078     ///
0079     ////////////////////////////////////////////////////////////
0080     Int64 asMicroseconds() const;
0081 
0082     ////////////////////////////////////////////////////////////
0083     // Static member data
0084     ////////////////////////////////////////////////////////////
0085     static const Time Zero; //!< Predefined "zero" time value
0086 
0087 private:
0088 
0089     friend SFML_SYSTEM_API Time seconds(float);
0090     friend SFML_SYSTEM_API Time milliseconds(Int32);
0091     friend SFML_SYSTEM_API Time microseconds(Int64);
0092 
0093     ////////////////////////////////////////////////////////////
0094     /// \brief Construct from a number of microseconds
0095     ///
0096     /// This function is internal. To construct time values,
0097     /// use sf::seconds, sf::milliseconds or sf::microseconds instead.
0098     ///
0099     /// \param microseconds Number of microseconds
0100     ///
0101     ////////////////////////////////////////////////////////////
0102     explicit Time(Int64 microseconds);
0103 
0104 private:
0105 
0106     ////////////////////////////////////////////////////////////
0107     // Member data
0108     ////////////////////////////////////////////////////////////
0109     Int64 m_microseconds; //!< Time value stored as microseconds
0110 };
0111 
0112 ////////////////////////////////////////////////////////////
0113 /// \relates Time
0114 /// \brief Construct a time value from a number of seconds
0115 ///
0116 /// \param amount Number of seconds
0117 ///
0118 /// \return Time value constructed from the amount of seconds
0119 ///
0120 /// \see milliseconds, microseconds
0121 ///
0122 ////////////////////////////////////////////////////////////
0123 SFML_SYSTEM_API Time seconds(float amount);
0124 
0125 ////////////////////////////////////////////////////////////
0126 /// \relates Time
0127 /// \brief Construct a time value from a number of milliseconds
0128 ///
0129 /// \param amount Number of milliseconds
0130 ///
0131 /// \return Time value constructed from the amount of milliseconds
0132 ///
0133 /// \see seconds, microseconds
0134 ///
0135 ////////////////////////////////////////////////////////////
0136 SFML_SYSTEM_API Time milliseconds(Int32 amount);
0137 
0138 ////////////////////////////////////////////////////////////
0139 /// \relates Time
0140 /// \brief Construct a time value from a number of microseconds
0141 ///
0142 /// \param amount Number of microseconds
0143 ///
0144 /// \return Time value constructed from the amount of microseconds
0145 ///
0146 /// \see seconds, milliseconds
0147 ///
0148 ////////////////////////////////////////////////////////////
0149 SFML_SYSTEM_API Time microseconds(Int64 amount);
0150 
0151 ////////////////////////////////////////////////////////////
0152 /// \relates Time
0153 /// \brief Overload of == operator to compare two time values
0154 ///
0155 /// \param left  Left operand (a time)
0156 /// \param right Right operand (a time)
0157 ///
0158 /// \return True if both time values are equal
0159 ///
0160 ////////////////////////////////////////////////////////////
0161 SFML_SYSTEM_API bool operator ==(Time left, Time right);
0162 
0163 ////////////////////////////////////////////////////////////
0164 /// \relates Time
0165 /// \brief Overload of != operator to compare two time values
0166 ///
0167 /// \param left  Left operand (a time)
0168 /// \param right Right operand (a time)
0169 ///
0170 /// \return True if both time values are different
0171 ///
0172 ////////////////////////////////////////////////////////////
0173 SFML_SYSTEM_API bool operator !=(Time left, Time right);
0174 
0175 ////////////////////////////////////////////////////////////
0176 /// \relates Time
0177 /// \brief Overload of < operator to compare two time values
0178 ///
0179 /// \param left  Left operand (a time)
0180 /// \param right Right operand (a time)
0181 ///
0182 /// \return True if \a left is lesser than \a right
0183 ///
0184 ////////////////////////////////////////////////////////////
0185 SFML_SYSTEM_API bool operator <(Time left, Time right);
0186 
0187 ////////////////////////////////////////////////////////////
0188 /// \relates Time
0189 /// \brief Overload of > operator to compare two time values
0190 ///
0191 /// \param left  Left operand (a time)
0192 /// \param right Right operand (a time)
0193 ///
0194 /// \return True if \a left is greater than \a right
0195 ///
0196 ////////////////////////////////////////////////////////////
0197 SFML_SYSTEM_API bool operator >(Time left, Time right);
0198 
0199 ////////////////////////////////////////////////////////////
0200 /// \relates Time
0201 /// \brief Overload of <= operator to compare two time values
0202 ///
0203 /// \param left  Left operand (a time)
0204 /// \param right Right operand (a time)
0205 ///
0206 /// \return True if \a left is lesser or equal than \a right
0207 ///
0208 ////////////////////////////////////////////////////////////
0209 SFML_SYSTEM_API bool operator <=(Time left, Time right);
0210 
0211 ////////////////////////////////////////////////////////////
0212 /// \relates Time
0213 /// \brief Overload of >= operator to compare two time values
0214 ///
0215 /// \param left  Left operand (a time)
0216 /// \param right Right operand (a time)
0217 ///
0218 /// \return True if \a left is greater or equal than \a right
0219 ///
0220 ////////////////////////////////////////////////////////////
0221 SFML_SYSTEM_API bool operator >=(Time left, Time right);
0222 
0223 ////////////////////////////////////////////////////////////
0224 /// \relates Time
0225 /// \brief Overload of unary - operator to negate a time value
0226 ///
0227 /// \param right Right operand (a time)
0228 ///
0229 /// \return Opposite of the time value
0230 ///
0231 ////////////////////////////////////////////////////////////
0232 SFML_SYSTEM_API Time operator -(Time right);
0233 
0234 ////////////////////////////////////////////////////////////
0235 /// \relates Time
0236 /// \brief Overload of binary + operator to add two time values
0237 ///
0238 /// \param left  Left operand (a time)
0239 /// \param right Right operand (a time)
0240 ///
0241 /// \return Sum of the two times values
0242 ///
0243 ////////////////////////////////////////////////////////////
0244 SFML_SYSTEM_API Time operator +(Time left, Time right);
0245 
0246 ////////////////////////////////////////////////////////////
0247 /// \relates Time
0248 /// \brief Overload of binary += operator to add/assign two time values
0249 ///
0250 /// \param left  Left operand (a time)
0251 /// \param right Right operand (a time)
0252 ///
0253 /// \return Sum of the two times values
0254 ///
0255 ////////////////////////////////////////////////////////////
0256 SFML_SYSTEM_API Time& operator +=(Time& left, Time right);
0257 
0258 ////////////////////////////////////////////////////////////
0259 /// \relates Time
0260 /// \brief Overload of binary - operator to subtract two time values
0261 ///
0262 /// \param left  Left operand (a time)
0263 /// \param right Right operand (a time)
0264 ///
0265 /// \return Difference of the two times values
0266 ///
0267 ////////////////////////////////////////////////////////////
0268 SFML_SYSTEM_API Time operator -(Time left, Time right);
0269 
0270 ////////////////////////////////////////////////////////////
0271 /// \relates Time
0272 /// \brief Overload of binary -= operator to subtract/assign two time values
0273 ///
0274 /// \param left  Left operand (a time)
0275 /// \param right Right operand (a time)
0276 ///
0277 /// \return Difference of the two times values
0278 ///
0279 ////////////////////////////////////////////////////////////
0280 SFML_SYSTEM_API Time& operator -=(Time& left, Time right);
0281 
0282 ////////////////////////////////////////////////////////////
0283 /// \relates Time
0284 /// \brief Overload of binary * operator to scale a time value
0285 ///
0286 /// \param left  Left operand (a time)
0287 /// \param right Right operand (a number)
0288 ///
0289 /// \return \a left multiplied by \a right
0290 ///
0291 ////////////////////////////////////////////////////////////
0292 SFML_SYSTEM_API Time operator *(Time left, float right);
0293 
0294 ////////////////////////////////////////////////////////////
0295 /// \relates Time
0296 /// \brief Overload of binary * operator to scale a time value
0297 ///
0298 /// \param left  Left operand (a time)
0299 /// \param right Right operand (a number)
0300 ///
0301 /// \return \a left multiplied by \a right
0302 ///
0303 ////////////////////////////////////////////////////////////
0304 SFML_SYSTEM_API Time operator *(Time left, Int64 right);
0305 
0306 ////////////////////////////////////////////////////////////
0307 /// \relates Time
0308 /// \brief Overload of binary * operator to scale a time value
0309 ///
0310 /// \param left  Left operand (a number)
0311 /// \param right Right operand (a time)
0312 ///
0313 /// \return \a left multiplied by \a right
0314 ///
0315 ////////////////////////////////////////////////////////////
0316 SFML_SYSTEM_API Time operator *(float left, Time right);
0317 
0318 ////////////////////////////////////////////////////////////
0319 /// \relates Time
0320 /// \brief Overload of binary * operator to scale a time value
0321 ///
0322 /// \param left  Left operand (a number)
0323 /// \param right Right operand (a time)
0324 ///
0325 /// \return \a left multiplied by \a right
0326 ///
0327 ////////////////////////////////////////////////////////////
0328 SFML_SYSTEM_API Time operator *(Int64 left, Time right);
0329 
0330 ////////////////////////////////////////////////////////////
0331 /// \relates Time
0332 /// \brief Overload of binary *= operator to scale/assign a time value
0333 ///
0334 /// \param left  Left operand (a time)
0335 /// \param right Right operand (a number)
0336 ///
0337 /// \return \a left multiplied by \a right
0338 ///
0339 ////////////////////////////////////////////////////////////
0340 SFML_SYSTEM_API Time& operator *=(Time& left, float right);
0341 
0342 ////////////////////////////////////////////////////////////
0343 /// \relates Time
0344 /// \brief Overload of binary *= operator to scale/assign a time value
0345 ///
0346 /// \param left  Left operand (a time)
0347 /// \param right Right operand (a number)
0348 ///
0349 /// \return \a left multiplied by \a right
0350 ///
0351 ////////////////////////////////////////////////////////////
0352 SFML_SYSTEM_API Time& operator *=(Time& left, Int64 right);
0353 
0354 ////////////////////////////////////////////////////////////
0355 /// \relates Time
0356 /// \brief Overload of binary / operator to scale a time value
0357 ///
0358 /// \param left  Left operand (a time)
0359 /// \param right Right operand (a number)
0360 ///
0361 /// \return \a left divided by \a right
0362 ///
0363 ////////////////////////////////////////////////////////////
0364 SFML_SYSTEM_API Time operator /(Time left, float right);
0365 
0366 ////////////////////////////////////////////////////////////
0367 /// \relates Time
0368 /// \brief Overload of binary / operator to scale a time value
0369 ///
0370 /// \param left  Left operand (a time)
0371 /// \param right Right operand (a number)
0372 ///
0373 /// \return \a left divided by \a right
0374 ///
0375 ////////////////////////////////////////////////////////////
0376 SFML_SYSTEM_API Time operator /(Time left, Int64 right);
0377 
0378 ////////////////////////////////////////////////////////////
0379 /// \relates Time
0380 /// \brief Overload of binary /= operator to scale/assign a time value
0381 ///
0382 /// \param left  Left operand (a time)
0383 /// \param right Right operand (a number)
0384 ///
0385 /// \return \a left divided by \a right
0386 ///
0387 ////////////////////////////////////////////////////////////
0388 SFML_SYSTEM_API Time& operator /=(Time& left, float right);
0389 
0390 ////////////////////////////////////////////////////////////
0391 /// \relates Time
0392 /// \brief Overload of binary /= operator to scale/assign a time value
0393 ///
0394 /// \param left  Left operand (a time)
0395 /// \param right Right operand (a number)
0396 ///
0397 /// \return \a left divided by \a right
0398 ///
0399 ////////////////////////////////////////////////////////////
0400 SFML_SYSTEM_API Time& operator /=(Time& left, Int64 right);
0401 
0402 ////////////////////////////////////////////////////////////
0403 /// \relates Time
0404 /// \brief Overload of binary / operator to compute the ratio of two time values
0405 ///
0406 /// \param left  Left operand (a time)
0407 /// \param right Right operand (a time)
0408 ///
0409 /// \return \a left divided by \a right
0410 ///
0411 ////////////////////////////////////////////////////////////
0412 SFML_SYSTEM_API float operator /(Time left, Time right);
0413 
0414 ////////////////////////////////////////////////////////////
0415 /// \relates Time
0416 /// \brief Overload of binary % operator to compute remainder of a time value
0417 ///
0418 /// \param left  Left operand (a time)
0419 /// \param right Right operand (a time)
0420 ///
0421 /// \return \a left modulo \a right
0422 ///
0423 ////////////////////////////////////////////////////////////
0424 SFML_SYSTEM_API Time operator %(Time left, Time right);
0425 
0426 ////////////////////////////////////////////////////////////
0427 /// \relates Time
0428 /// \brief Overload of binary %= operator to compute/assign remainder of a time value
0429 ///
0430 /// \param left  Left operand (a time)
0431 /// \param right Right operand (a time)
0432 ///
0433 /// \return \a left modulo \a right
0434 ///
0435 ////////////////////////////////////////////////////////////
0436 SFML_SYSTEM_API Time& operator %=(Time& left, Time right);
0437 
0438 } // namespace sf
0439 
0440 
0441 #endif // SFML_TIME_HPP
0442 
0443 
0444 ////////////////////////////////////////////////////////////
0445 /// \class sf::Time
0446 /// \ingroup system
0447 ///
0448 /// sf::Time encapsulates a time value in a flexible way.
0449 /// It allows to define a time value either as a number of
0450 /// seconds, milliseconds or microseconds. It also works the
0451 /// other way round: you can read a time value as either
0452 /// a number of seconds, milliseconds or microseconds.
0453 ///
0454 /// By using such a flexible interface, the API doesn't
0455 /// impose any fixed type or resolution for time values,
0456 /// and let the user choose its own favorite representation.
0457 ///
0458 /// Time values support the usual mathematical operations:
0459 /// you can add or subtract two times, multiply or divide
0460 /// a time by a number, compare two times, etc.
0461 ///
0462 /// Since they represent a time span and not an absolute time
0463 /// value, times can also be negative.
0464 ///
0465 /// Usage example:
0466 /// \code
0467 /// sf::Time t1 = sf::seconds(0.1f);
0468 /// Int32 milli = t1.asMilliseconds(); // 100
0469 ///
0470 /// sf::Time t2 = sf::milliseconds(30);
0471 /// Int64 micro = t2.asMicroseconds(); // 30000
0472 ///
0473 /// sf::Time t3 = sf::microseconds(-800000);
0474 /// float sec = t3.asSeconds(); // -0.8
0475 /// \endcode
0476 ///
0477 /// \code
0478 /// void update(sf::Time elapsed)
0479 /// {
0480 ///    position += speed * elapsed.asSeconds();
0481 /// }
0482 ///
0483 /// update(sf::milliseconds(100));
0484 /// \endcode
0485 ///
0486 /// \see sf::Clock
0487 ///
0488 ////////////////////////////////////////////////////////////