Added the sf::Time class

This commit is contained in:
Laurent Gomila 2012-01-19 23:51:06 +01:00
parent e775bd0169
commit 4116ad033c
58 changed files with 880 additions and 227 deletions

View File

@ -12,7 +12,7 @@
////////////////////////////////////////////////////////////
/// Initialize OpenGL states into the specified view
///
/// \param Window : Target window to initialize
/// \param Window Target window to initialize
///
////////////////////////////////////////////////////////////
void Initialize(sf::Window& window)
@ -39,8 +39,8 @@ void Initialize(sf::Window& window)
/// Draw the OpenGL scene (a rotating cube) into
/// the specified view
///
/// \param Window : Target window for rendering
/// \param ElapsedTime : Time elapsed since the last draw
/// \param Window Target window for rendering
/// \param ElapsedTime Time elapsed since the last draw
///
////////////////////////////////////////////////////////////
void Draw(sf::Window& window, float elapsedTime)
@ -55,9 +55,9 @@ void Draw(sf::Window& window, float elapsedTime)
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.f, 0.f, -200.f);
glRotatef(elapsedTime * 50, 1.f, 0.f, 0.f);
glRotatef(elapsedTime * 30, 0.f, 1.f, 0.f);
glRotatef(elapsedTime * 90, 0.f, 0.f, 1.f);
glRotatef(elapsedTime * 0.05f, 1.f, 0.f, 0.f);
glRotatef(elapsedTime * 0.03f, 0.f, 1.f, 0.f);
glRotatef(elapsedTime * 0.09f, 0.f, 0.f, 1.f);
// Draw a cube
glBegin(GL_QUADS);
@ -184,8 +184,8 @@ int main()
}
// Draw something into our views
Draw(SFMLView1, clock.GetElapsedTime());
Draw(SFMLView2, clock.GetElapsedTime() * 0.3f);
Draw(SFMLView1, clock.GetElapsedTime().AsSeconds());
Draw(SFMLView2, clock.GetElapsedTime().AsSeconds() * 0.3f);
// Display the views on screen
SFMLView1.Display();

View File

@ -97,9 +97,9 @@ int main()
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(x, y, -100.f);
glRotatef(clock.GetElapsedTime() * 0.05f, 1.f, 0.f, 0.f);
glRotatef(clock.GetElapsedTime() * 0.03f, 0.f, 1.f, 0.f);
glRotatef(clock.GetElapsedTime() * 0.09f, 0.f, 0.f, 1.f);
glRotatef(clock.GetElapsedTime().AsSeconds() * 0.05f, 1.f, 0.f, 0.f);
glRotatef(clock.GetElapsedTime().AsSeconds() * 0.03f, 0.f, 1.f, 0.f);
glRotatef(clock.GetElapsedTime().AsSeconds() * 0.09f, 0.f, 0.f, 1.f);
// Draw a cube
float size = 20.f;

View File

@ -75,12 +75,13 @@ int main()
// Define the paddles properties
sf::Clock AITimer;
const sf::Uint32 AITime = 300;
const sf::Time AITime = sf::Seconds(0.1f);
const float paddleSpeed = 400.f;
float rightPaddleSpeed = 0.f;
const float ballSpeed = 400.f;
float ballAngle = 0.f; // to be changed later
sf::Clock clock;
bool isPlaying = false;
while (window.IsOpen())
{
@ -122,7 +123,7 @@ int main()
if (isPlaying)
{
float deltaTime = window.GetFrameTime() / 1000.f;
float deltaTime = clock.Restart().AsSeconds();
// Move the player's paddle
if (sf::Keyboard::IsKeyPressed(sf::Keyboard::Up) &&
@ -146,7 +147,7 @@ int main()
// Update the computer's paddle direction according to the ball position
if (AITimer.GetElapsedTime() > AITime)
{
AITimer.Reset();
AITimer.Restart();
if (ball.GetPosition().y + ballRadius > rightPaddle.GetPosition().y + paddleSize.y / 2)
rightPaddleSpeed = paddleSpeed;
else if (ball.GetPosition().y - ballRadius < rightPaddle.GetPosition().y - paddleSize.y / 2)

View File

@ -351,7 +351,7 @@ int main()
// Update the current example
float x = static_cast<float>(sf::Mouse::GetPosition(window).x) / window.GetWidth();
float y = static_cast<float>(sf::Mouse::GetPosition(window).y) / window.GetHeight();
effects[current]->Update(clock.GetElapsedTime() / 1000.f, x, y);
effects[current]->Update(clock.GetElapsedTime().AsSeconds(), x, y);
// Clear the window
window.Clear(sf::Color(255, 128, 0));

View File

@ -20,7 +20,7 @@ void PlaySound()
// Display sound informations
std::cout << "canary.wav :" << std::endl;
std::cout << " " << buffer.GetDuration() / 1000.f << " seconds" << std::endl;
std::cout << " " << buffer.GetDuration().AsSeconds() << " seconds" << std::endl;
std::cout << " " << buffer.GetSampleRate() << " samples / sec" << std::endl;
std::cout << " " << buffer.GetChannelCount() << " channels" << std::endl;
@ -32,10 +32,10 @@ void PlaySound()
while (sound.GetStatus() == sf::Sound::Playing)
{
// Leave some CPU time for other processes
sf::Sleep(100);
sf::Sleep(sf::Milliseconds(100));
// Display the playing position
std::cout << "\rPlaying... " << std::fixed << std::setprecision(2) << sound.GetPlayingOffset() / 1000.f << " sec ";
std::cout << "\rPlaying... " << std::fixed << std::setprecision(2) << sound.GetPlayingOffset().AsSeconds() << " sec ";
std::cout << std::flush;
}
std::cout << std::endl << std::endl;
@ -55,7 +55,7 @@ void PlayMusic()
// Display music informations
std::cout << "orchestral.ogg :" << std::endl;
std::cout << " " << music.GetDuration() / 1000.f << " seconds" << std::endl;
std::cout << " " << music.GetDuration().AsSeconds() << " seconds" << std::endl;
std::cout << " " << music.GetSampleRate() << " samples / sec" << std::endl;
std::cout << " " << music.GetChannelCount() << " channels" << std::endl;
@ -66,10 +66,10 @@ void PlayMusic()
while (music.GetStatus() == sf::Music::Playing)
{
// Leave some CPU time for other processes
sf::Sleep(100);
sf::Sleep(sf::Milliseconds(100));
// Display the playing position
std::cout << "\rPlaying... " << std::fixed << std::setprecision(2) << music.GetPlayingOffset() / 1000.f << " sec ";
std::cout << "\rPlaying... " << std::fixed << std::setprecision(2) << music.GetPlayingOffset().AsSeconds() << " sec ";
std::cout << std::flush;
}
std::cout << std::endl;

View File

@ -46,7 +46,7 @@ int main()
// Display captured sound informations
std::cout << "Sound information :" << std::endl;
std::cout << " " << buffer.GetDuration() / 1000.f << " seconds" << std::endl;
std::cout << " " << buffer.GetDuration().AsSeconds() << " seconds" << std::endl;
std::cout << " " << buffer.GetSampleRate() << " samples / seconds" << std::endl;
std::cout << " " << buffer.GetChannelCount() << " channels" << std::endl;
@ -76,11 +76,11 @@ int main()
while (sound.GetStatus() == sf::Sound::Playing)
{
// Display the playing position
std::cout << "\rPlaying... " << std::fixed << std::setprecision(2) << sound.GetPlayingOffset() / 1000.f << " sec";
std::cout << "\rPlaying... " << std::fixed << std::setprecision(2) << sound.GetPlayingOffset().AsSeconds() << " sec";
std::cout << std::flush;
// Leave some CPU time for other threads
sf::Sleep(100);
sf::Sleep(sf::Milliseconds(100));
}
}

View File

@ -78,7 +78,7 @@ private :
// No new data has arrived since last update : wait until we get some
while ((myOffset >= mySamples.size()) && !myHasFinished)
sf::Sleep(10);
sf::Sleep(sf::Milliseconds(10));
// Copy samples into a local buffer to avoid synchronization problems
// (don't forget that we run in two separate threads)
@ -101,9 +101,9 @@ private :
/// /see SoundStream::OnSeek
///
////////////////////////////////////////////////////////////
virtual void OnSeek(sf::Uint32 timeOffset)
virtual void OnSeek(sf::Time timeOffset)
{
myOffset = timeOffset * GetSampleRate() * GetChannelCount() / 1000;
myOffset = timeOffset.AsMilliseconds() * GetSampleRate() * GetChannelCount() / 1000;
}
////////////////////////////////////////////////////////////
@ -179,7 +179,7 @@ void DoServer(unsigned short port)
while (audioStream.GetStatus() != sf::SoundStream::Stopped)
{
// Leave some CPU time for other threads
sf::Sleep(100);
sf::Sleep(sf::Milliseconds(100));
}
std::cin.ignore(10000, '\n');
@ -195,6 +195,6 @@ void DoServer(unsigned short port)
while (audioStream.GetStatus() != sf::SoundStream::Stopped)
{
// Leave some CPU time for other threads
sf::Sleep(100);
sf::Sleep(sf::Milliseconds(100));
}
}

View File

@ -100,18 +100,18 @@ INT WINAPI WinMain(HINSTANCE instance, HINSTANCE, LPSTR, INT)
}
else
{
sf::Uint32 time = clock.GetElapsedTime();
float time = clock.GetElapsedTime().AsSeconds();
// Clear views
SFMLView1.Clear();
SFMLView2.Clear();
// Draw sprite 1 on view 1
sprite1.SetRotation(time * 0.1f);
sprite1.SetRotation(time * 100);
SFMLView1.Draw(sprite1);
// Draw sprite 2 on view 2
sprite2.SetPosition(std::cos(time * 0.001f) * 100.f, 0.f);
sprite2.SetPosition(std::cos(time) * 100.f, 0.f);
SFMLView2.Draw(sprite2);
// Display each view on screen

View File

@ -65,9 +65,9 @@ int main()
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.f, 0.f, -200.f);
glRotatef(clock.GetElapsedTime() * 0.05f, 1.f, 0.f, 0.f);
glRotatef(clock.GetElapsedTime() * 0.03f, 0.f, 1.f, 0.f);
glRotatef(clock.GetElapsedTime() * 0.09f, 0.f, 0.f, 1.f);
glRotatef(clock.GetElapsedTime().AsSeconds() * 0.05f, 1.f, 0.f, 0.f);
glRotatef(clock.GetElapsedTime().AsSeconds() * 0.03f, 0.f, 1.f, 0.f);
glRotatef(clock.GetElapsedTime().AsSeconds() * 0.09f, 0.f, 0.f, 1.f);
// Draw a cube
glBegin(GL_QUADS);

View File

@ -30,6 +30,7 @@
////////////////////////////////////////////////////////////
#include <SFML/Audio/SoundStream.hpp>
#include <SFML/System/Mutex.hpp>
#include <SFML/System/Time.hpp>
#include <string>
#include <vector>
@ -121,10 +122,10 @@ public :
////////////////////////////////////////////////////////////
/// \brief Get the total duration of the music
///
/// \return Music duration, in milliseconds
/// \return Music duration
///
////////////////////////////////////////////////////////////
Uint32 GetDuration() const;
Time GetDuration() const;
protected :
@ -144,10 +145,10 @@ protected :
////////////////////////////////////////////////////////////
/// \brief Change the current playing position in the stream source
///
/// \param timeOffset New playing position, in milliseconds
/// \param timeOffset New playing position, from the beginning of the music
///
////////////////////////////////////////////////////////////
virtual void OnSeek(Uint32 timeOffset);
virtual void OnSeek(Time timeOffset);
private :
@ -161,7 +162,7 @@ private :
// Member data
////////////////////////////////////////////////////////////
priv::SoundFile* myFile; ///< Sound file
Uint32 myDuration; ///< Music duration, in milliseconds
Time myDuration; ///< Music duration
std::vector<Int16> mySamples; ///< Temporary buffer of samples
Mutex myMutex; ///< Mutex protecting the data
};

View File

@ -29,6 +29,7 @@
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Audio/SoundSource.hpp>
#include <SFML/System/Time.hpp>
#include <cstdlib>
@ -144,12 +145,12 @@ public :
/// The playing position can be changed when the sound is
/// either paused or playing.
///
/// \param timeOffset New playing position, in milliseconds
/// \param timeOffset New playing position, from the beginning of the sound
///
/// \see GetPlayingOffset
///
////////////////////////////////////////////////////////////
void SetPlayingOffset(Uint32 timeOffset);
void SetPlayingOffset(Time timeOffset);
////////////////////////////////////////////////////////////
/// \brief Get the audio buffer attached to the sound
@ -172,12 +173,12 @@ public :
////////////////////////////////////////////////////////////
/// \brief Get the current playing position of the sound
///
/// \return Current playing position, in milliseconds
/// \return Current playing position, from the beginning of the sound
///
/// \see SetPlayingOffset
///
////////////////////////////////////////////////////////////
Uint32 GetPlayingOffset() const;
Time GetPlayingOffset() const;
////////////////////////////////////////////////////////////
/// \brief Get the current status of the sound (stopped, paused, playing)

View File

@ -29,6 +29,7 @@
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Config.hpp>
#include <SFML/System/Time.hpp>
#include <string>
#include <vector>
#include <set>
@ -212,12 +213,12 @@ public :
////////////////////////////////////////////////////////////
/// \brief Get the total duration of the sound
///
/// \return Sound duration, in milliseconds
/// \return Sound duration
///
/// \see GetSampleRate, GetChannelCount
///
////////////////////////////////////////////////////////////
Uint32 GetDuration() const;
Time GetDuration() const;
////////////////////////////////////////////////////////////
/// \brief Overload of assignment operator
@ -280,7 +281,7 @@ private :
////////////////////////////////////////////////////////////
unsigned int myBuffer; ///< OpenAL buffer identifier
std::vector<Int16> mySamples; ///< Samples buffer
Uint32 myDuration; ///< Sound duration, in milliseconds
Time myDuration; ///< Sound duration
mutable SoundList mySounds; ///< List of sounds that are using this buffer
};

View File

@ -30,6 +30,7 @@
////////////////////////////////////////////////////////////
#include <SFML/Audio/SoundSource.hpp>
#include <SFML/System/Thread.hpp>
#include <SFML/System/Time.hpp>
#include <cstdlib>
@ -131,22 +132,22 @@ public :
/// The playing position can be changed when the stream is
/// either paused or playing.
///
/// \param timeOffset New playing position, in milliseconds
/// \param timeOffset New playing position, from the beginning of the stream
///
/// \see GetPlayingOffset
///
////////////////////////////////////////////////////////////
void SetPlayingOffset(Uint32 timeOffset);
void SetPlayingOffset(Time timeOffset);
////////////////////////////////////////////////////////////
/// \brief Get the current playing position of the stream
///
/// \return Current playing position, in milliseconds
/// \return Current playing position, from the beginning of the stream
///
/// \see SetPlayingOffset
///
////////////////////////////////////////////////////////////
Uint32 GetPlayingOffset() const;
Time GetPlayingOffset() const;
////////////////////////////////////////////////////////////
/// \brief Set whether or not the stream should loop after reaching the end
@ -232,10 +233,10 @@ private :
/// This function must be overriden by derived classes to
/// allow random seeking into the stream source.
///
/// \param timeOffset New playing position, in milliseconds
/// \param timeOffset New playing position, relative to the beginning of the stream
///
////////////////////////////////////////////////////////////
virtual void OnSeek(Uint32 timeOffset) = 0;
virtual void OnSeek(Time timeOffset) = 0;
////////////////////////////////////////////////////////////
/// \brief Fill a new buffer with audio samples, and append

View File

@ -29,6 +29,7 @@
// Headers
////////////////////////////////////////////////////////////
#include <SFML/System/NonCopyable.hpp>
#include <SFML/System/Time.hpp>
#include <SFML/Network/TcpSocket.hpp>
#include <string>
#include <vector>
@ -264,19 +265,19 @@ public :
/// This function tries to connect to the server so it may take
/// a while to complete, especially if the server is not
/// reachable. To avoid blocking your application for too long,
/// you can use a timeout. The default value, 0, means that the
/// you can use a timeout. The default value, Time::Zero, means that the
/// system timeout will be used (which is usually pretty long).
///
/// \param server Name or address of the FTP server to connect to
/// \param port Port used for the connection
/// \param timeout Maximum time to wait, in milliseconds
/// \param timeout Maximum time to wait
///
/// \return Server response to the request
///
/// \see Disconnect
///
////////////////////////////////////////////////////////////
Response Connect(const IpAddress& server, unsigned short port = 21, Uint32 timeout = 0);
Response Connect(const IpAddress& server, unsigned short port = 21, Time timeout = Time::Zero);
////////////////////////////////////////////////////////////
/// \brief Close the connection with the server

View File

@ -29,6 +29,7 @@
// Headers
////////////////////////////////////////////////////////////
#include <SFML/System/NonCopyable.hpp>
#include <SFML/System/Time.hpp>
#include <SFML/Network/IpAddress.hpp>
#include <SFML/Network/TcpSocket.hpp>
#include <map>
@ -377,16 +378,16 @@ public :
/// Warning: this function waits for the server's response and may
/// not return instantly; use a thread if you don't want to block your
/// application, or use a timeout to limit the time to wait. A value
/// of 0 means that the client will use the system defaut timeout
/// of Time::Zero means that the client will use the system defaut timeout
/// (which is usually pretty long).
///
/// \param request Request to send
/// \param timeout Maximum time to wait, in milliseconds
/// \param timeout Maximum time to wait
///
/// \return Server's response
///
////////////////////////////////////////////////////////////
Response SendRequest(const Request& request, Uint32 timeout = 0);
Response SendRequest(const Request& request, Time timeout = Time::Zero);
private :

View File

@ -29,6 +29,7 @@
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Config.hpp>
#include <SFML/System/Time.hpp>
#include <istream>
#include <ostream>
#include <string>
@ -168,14 +169,14 @@ public :
/// to be possibly stuck waiting in case there is a problem; this
/// limit is deactivated by default.
///
/// \param timeout Maximum time to wait, in milliseconds
/// \param timeout Maximum time to wait
///
/// \return Public IP address of the computer
///
/// \see GetLocalAddress
///
////////////////////////////////////////////////////////////
static IpAddress GetPublicAddress(Uint32 timeout = 0);
static IpAddress GetPublicAddress(Time timeout = Time::Zero);
////////////////////////////////////////////////////////////
// Static member data

View File

@ -29,6 +29,7 @@
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Config.hpp>
#include <SFML/System/Time.hpp>
namespace sf
@ -111,14 +112,14 @@ public :
/// If you use a timeout and no socket is ready before the timeout
/// is over, the function returns false.
///
/// \param timeout Maximum time to wait, in milliseconds (use 0 for infinity)
/// \param timeout Maximum time to wait, (use Time::Zero for infinity)
///
/// \return True if there are sockets ready, false otherwise
///
/// \see IsReady
///
////////////////////////////////////////////////////////////
bool Wait(Uint32 timeout = 0);
bool Wait(Time timeout = Time::Zero);
////////////////////////////////////////////////////////////
/// \brief Test a socket to know if it is ready to receive data

View File

@ -29,6 +29,7 @@
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Network/Socket.hpp>
#include <SFML/System/Time.hpp>
namespace sf
@ -99,14 +100,14 @@ public :
///
/// \param remoteAddress Address of the remote peer
/// \param remotePort Port of the remote peer
/// \param timeout Optional maximum time to wait, in milliseconds
/// \param timeout Optional maximum time to wait
///
/// \return Status code
///
/// \see Disconnect
///
////////////////////////////////////////////////////////////
Status Connect(const IpAddress& remoteAddress, unsigned short remotePort, Uint32 timeout = 0);
Status Connect(const IpAddress& remoteAddress, unsigned short remotePort, Time timeout = Time::Zero);
////////////////////////////////////////////////////////////
/// \brief Disconnect the socket from its remote peer

View File

@ -29,12 +29,13 @@
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Config.hpp>
#include <SFML/System/Time.hpp>
namespace sf
{
////////////////////////////////////////////////////////////
/// \brief Utility class for manipulating time
/// \brief Utility class that measures the elapsed time
///
////////////////////////////////////////////////////////////
class SFML_API Clock
@ -50,31 +51,34 @@ public :
Clock();
////////////////////////////////////////////////////////////
/// \brief Get the time elapsed
/// \brief Get the elapsed time
///
/// This function returns the time elapsed since the last call
/// to Reset() (or the construction of the instance if Reset()
/// to Restart() (or the construction of the instance if Restart()
/// has not been called).
///
/// \return Time elapsed, in milliseconds
/// \return Time elapsed
///
////////////////////////////////////////////////////////////
Uint32 GetElapsedTime() const;
Time GetElapsedTime() const;
////////////////////////////////////////////////////////////
/// \brief Restart the clock
///
/// This function puts the time counter back to zero.
/// It also returns the time elapsed since the clock was started.
///
/// \return Time elapsed
///
////////////////////////////////////////////////////////////
void Reset();
Time Restart();
private :
////////////////////////////////////////////////////////////
// Member data
////////////////////////////////////////////////////////////
Uint64 myStartTime; ///< Time of last reset, in microseconds
Time myStartTime; ///< Time of last reset, in microseconds
};
} // namespace sf
@ -88,17 +92,26 @@ private :
/// \ingroup system
///
/// sf::Clock is a lightweight class for measuring time.
/// Its resolution depends on the underlying OS, but you can generally
/// expect a 1 ms resolution.
///
/// Its provides the most precise time that the underlying
/// OS can achieve (generally microseconds or nanoseconds).
/// It also ensures monotonicity, which means that the returned
/// time can never go backward, even if the system time is
/// changed.
///
/// Usage example:
/// \code
/// sf::Clock clock;
/// ...
/// Uint32 time1 = clock.GetElapsedTime();
/// clock.Reset();
/// Time time1 = clock.GetElapsedTime();
/// ...
/// Uint32 time2 = clock.GetElapsedTime();
/// Time time2 = clock.Restart();
/// \endcode
///
/// The sf::Time value returned by the clock can then be
/// converted to a number of seconds, milliseconds or even
/// microseconds.
///
/// \see sf::Time
///
////////////////////////////////////////////////////////////

View File

@ -29,6 +29,7 @@
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Config.hpp>
#include <SFML/System/Time.hpp>
namespace sf
@ -40,10 +41,10 @@ namespace sf
/// sf::Sleep is the best way to block a program or one of its
/// threads, as it doesn't consume any CPU power.
///
/// \param duration Time to sleep, in milliseconds
/// \param duration Time to sleep
///
////////////////////////////////////////////////////////////
void SFML_API Sleep(Uint32 duration);
void SFML_API Sleep(Time duration);
} // namespace sf

View File

@ -0,0 +1,452 @@
////////////////////////////////////////////////////////////
//
// 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
///
////////////////////////////////////////////////////////////

View File

@ -387,19 +387,6 @@ public :
////////////////////////////////////////////////////////////
void SetFramerateLimit(unsigned int limit);
////////////////////////////////////////////////////////////
/// \brief Get the duration of the last frame
///
/// This function returns the time elapsed between the last
/// two calls to Display().
/// This can be useful for calculating the framerate, or for
/// updating the application's objects.
///
/// \return Time elapsed in last frame, in milliseconds
///
////////////////////////////////////////////////////////////
Uint32 GetFrameTime() const;
////////////////////////////////////////////////////////////
/// \brief Change the joystick threshold
///
@ -474,7 +461,6 @@ private :
priv::WindowImpl* myImpl; ///< Platform-specific implementation of the window
priv::GlContext* myContext; ///< Platform-specific implementation of the OpenGL context
Clock myClock; ///< Clock for measuring the elapsed time between frames
Uint32 myLastFrameTime; ///< Time elapsed since last frame
unsigned int myFramerateLimit; ///< Current framerate limit
};

View File

@ -31,9 +31,6 @@
#include <SFML/System/Err.hpp>
////////////////////////////////////////////////////////////
// Private data
////////////////////////////////////////////////////////////
namespace
{
ALCdevice* audioDevice = NULL;

View File

@ -38,7 +38,7 @@ namespace sf
////////////////////////////////////////////////////////////
Music::Music() :
myFile (new priv::SoundFile),
myDuration(0)
myDuration()
{
}
@ -106,7 +106,7 @@ bool Music::OpenFromStream(InputStream& stream)
////////////////////////////////////////////////////////////
Uint32 Music::GetDuration() const
Time Music::GetDuration() const
{
return myDuration;
}
@ -127,7 +127,7 @@ bool Music::OnGetData(SoundStream::Chunk& data)
////////////////////////////////////////////////////////////
void Music::OnSeek(Uint32 timeOffset)
void Music::OnSeek(Time timeOffset)
{
Lock lock(myMutex);
@ -139,8 +139,7 @@ void Music::OnSeek(Uint32 timeOffset)
void Music::Initialize()
{
// Compute the music duration
Uint64 samples = myFile->GetSampleCount();
myDuration = static_cast<Uint32>(1000 * samples / myFile->GetSampleRate() / myFile->GetChannelCount());
myDuration = Seconds(static_cast<float>(myFile->GetSampleCount()) / myFile->GetSampleRate() / myFile->GetChannelCount());
// Resize the internal buffer so that it can contain 1 second of audio samples
mySamples.resize(myFile->GetSampleRate() * myFile->GetChannelCount());

View File

@ -113,9 +113,9 @@ void Sound::SetLoop(bool Loop)
////////////////////////////////////////////////////////////
void Sound::SetPlayingOffset(Uint32 timeOffset)
void Sound::SetPlayingOffset(Time timeOffset)
{
ALCheck(alSourcef(mySource, AL_SEC_OFFSET, timeOffset / 1000.f));
ALCheck(alSourcef(mySource, AL_SEC_OFFSET, timeOffset.AsSeconds()));
}
@ -137,12 +137,12 @@ bool Sound::GetLoop() const
////////////////////////////////////////////////////////////
Uint32 Sound::GetPlayingOffset() const
Time Sound::GetPlayingOffset() const
{
ALfloat seconds = 0.f;
ALCheck(alGetSourcef(mySource, AL_SEC_OFFSET, &seconds));
return static_cast<Uint32>(seconds * 1000);
return Seconds(seconds);
}

View File

@ -39,7 +39,7 @@ namespace sf
////////////////////////////////////////////////////////////
SoundBuffer::SoundBuffer() :
myBuffer (0),
myDuration(0)
myDuration()
{
priv::EnsureALInit();
@ -189,7 +189,7 @@ unsigned int SoundBuffer::GetChannelCount() const
////////////////////////////////////////////////////////////
Uint32 SoundBuffer::GetDuration() const
Time SoundBuffer::GetDuration() const
{
return myDuration;
}
@ -253,7 +253,7 @@ bool SoundBuffer::Update(unsigned int channelCount, unsigned int sampleRate)
ALCheck(alBufferData(myBuffer, format, &mySamples[0], size, sampleRate));
// Compute the duration
myDuration = static_cast<Uint32>(1000 * mySamples.size() / sampleRate / channelCount);
myDuration = Milliseconds(1000 * mySamples.size() / sampleRate / channelCount);
return true;
}

View File

@ -32,9 +32,6 @@
#include <cctype>
////////////////////////////////////////////////////////////
// Private data
////////////////////////////////////////////////////////////
namespace
{
// Convert a string to lower case
@ -253,11 +250,11 @@ void SoundFile::Write(const Int16* data, std::size_t sampleCount)
////////////////////////////////////////////////////////////
void SoundFile::Seek(Uint32 timeOffset)
void SoundFile::Seek(Time timeOffset)
{
if (myFile)
{
sf_count_t frameOffset = static_cast<sf_count_t>(timeOffset) * mySampleRate / 1000;
sf_count_t frameOffset = static_cast<sf_count_t>(timeOffset.AsSeconds() * mySampleRate);
sf_seek(myFile, frameOffset, SEEK_SET);
}
}

View File

@ -29,6 +29,7 @@
// Headers
////////////////////////////////////////////////////////////
#include <SFML/System/NonCopyable.hpp>
#include <SFML/System/Time.hpp>
#include <sndfile.h>
#include <string>
@ -149,10 +150,10 @@ public :
////////////////////////////////////////////////////////////
/// \brief Change the current read position in the file
///
/// \param timeOffset New read position, in milliseconds
/// \param timeOffset New playing position, from the beginning of the file
///
////////////////////////////////////////////////////////////
void Seek(Uint32 timeOffset);
void Seek(Time timeOffset);
private :

View File

@ -36,9 +36,6 @@
#endif
////////////////////////////////////////////////////////////
// Private data
////////////////////////////////////////////////////////////
namespace
{
ALCdevice* captureDevice = NULL;
@ -155,7 +152,7 @@ void SoundRecorder::Record()
ProcessCapturedSamples();
// Don't bother the CPU while waiting for more captured data
Sleep(100);
Sleep(Milliseconds(100));
}
// Capture is finished : clean up everything

View File

@ -97,7 +97,7 @@ void SoundStream::Play()
}
// Move to the beginning
OnSeek(0);
OnSeek(Time::Zero);
// Start updating the stream in a separate thread to avoid blocking the application
mySamplesProcessed = 0;
@ -150,7 +150,7 @@ SoundStream::Status SoundStream::GetStatus() const
////////////////////////////////////////////////////////////
void SoundStream::SetPlayingOffset(Uint32 timeOffset)
void SoundStream::SetPlayingOffset(Time timeOffset)
{
// Stop the stream
Stop();
@ -159,19 +159,19 @@ void SoundStream::SetPlayingOffset(Uint32 timeOffset)
OnSeek(timeOffset);
// Restart streaming
mySamplesProcessed = static_cast<Uint64>(timeOffset) * mySampleRate * myChannelCount / 1000;
mySamplesProcessed = static_cast<Uint64>(timeOffset.AsSeconds() * mySampleRate * myChannelCount);
myIsStreaming = true;
myThread.Launch();
}
////////////////////////////////////////////////////////////
Uint32 SoundStream::GetPlayingOffset() const
Time SoundStream::GetPlayingOffset() const
{
ALfloat seconds = 0.f;
ALCheck(alGetSourcef(mySource, AL_SEC_OFFSET, &seconds));
return static_cast<Uint32>(1000 * seconds + 1000 * mySamplesProcessed / mySampleRate / myChannelCount);
return Seconds(seconds + static_cast<float>(mySamplesProcessed) / mySampleRate / myChannelCount);
}
@ -264,7 +264,7 @@ void SoundStream::Stream()
// Leave some time for the other threads if the stream is still playing
if (SoundSource::GetStatus() != Stopped)
Sleep(10);
Sleep(Milliseconds(10));
}
// Stop the playback
@ -295,7 +295,7 @@ bool SoundStream::FillAndPushBuffer(unsigned int bufferNum)
if (myLoop)
{
// Return to the beginning of the stream source
OnSeek(0);
OnSeek(Time::Zero);
// If we previously had no data, try to fill the buffer once again
if (!data.Samples || (data.SampleCount == 0))

View File

@ -37,9 +37,6 @@
#include <cstring>
////////////////////////////////////////////////////////////
// Private data
////////////////////////////////////////////////////////////
namespace
{
// FreeType callbacks that operate on a sf::InputStream

View File

@ -39,9 +39,6 @@ extern "C"
#include <cctype>
////////////////////////////////////////////////////////////
// Private data
////////////////////////////////////////////////////////////
namespace
{
// Convert a string to lower case

View File

@ -35,9 +35,6 @@
#include <vector>
////////////////////////////////////////////////////////////
// Private data
////////////////////////////////////////////////////////////
namespace
{
// Retrieve the maximum number of texture units available

View File

@ -32,9 +32,6 @@
#include <cmath>
////////////////////////////////////////////////////////////
// Private data
////////////////////////////////////////////////////////////
namespace
{
// Compute the normal of a segment

View File

@ -37,9 +37,6 @@
#include <cstring>
////////////////////////////////////////////////////////////
// Private data
////////////////////////////////////////////////////////////
namespace
{
// Thread-safe unique identifier generator,

View File

@ -146,7 +146,7 @@ Ftp::~Ftp()
////////////////////////////////////////////////////////////
Ftp::Response Ftp::Connect(const IpAddress& server, unsigned short port, Uint32 timeout)
Ftp::Response Ftp::Connect(const IpAddress& server, unsigned short port, Time timeout)
{
// Connect to the server
if (myCommandSocket.Connect(server, port, timeout) != Socket::Done)

View File

@ -32,9 +32,6 @@
#include <sstream>
////////////////////////////////////////////////////////////
// Private data
////////////////////////////////////////////////////////////
namespace
{
// Convert a string to lower case
@ -310,7 +307,7 @@ void Http::SetHost(const std::string& host, unsigned short port)
////////////////////////////////////////////////////////////
Http::Response Http::SendRequest(const Http::Request& request, Uint32 timeout)
Http::Response Http::SendRequest(const Http::Request& request, Time timeout)
{
// First make sure that the request is valid -- add missing mandatory fields
Request toSend(request);

View File

@ -165,7 +165,7 @@ IpAddress IpAddress::GetLocalAddress()
////////////////////////////////////////////////////////////
IpAddress IpAddress::GetPublicAddress(Uint32 timeout)
IpAddress IpAddress::GetPublicAddress(Time timeout)
{
// The trick here is more complicated, because the only way
// to get our public IP address is to get it from a distant computer.

View File

@ -100,18 +100,18 @@ void SocketSelector::Clear()
////////////////////////////////////////////////////////////
bool SocketSelector::Wait(Uint32 timeout)
bool SocketSelector::Wait(Time timeout)
{
// Setup the timeout
timeval time;
time.tv_sec = timeout / 1000;
time.tv_usec = (timeout - time.tv_sec * 1000) * 1000;
time.tv_sec = static_cast<long>(timeout.AsMicroseconds() / 1000000);
time.tv_usec = static_cast<long>(timeout.AsMicroseconds() % 1000000);
// Initialize the set that will contain the sockets that are ready
myImpl->SocketsReady = myImpl->AllSockets;
// Wait until one of the sockets is ready for reading, or timeout is reached
int count = select(myImpl->MaxSocket + 1, &myImpl->SocketsReady, NULL, NULL, timeout != 0 ? &time : NULL);
int count = select(myImpl->MaxSocket + 1, &myImpl->SocketsReady, NULL, NULL, timeout != Time::Zero ? &time : NULL);
return count > 0;
}

View File

@ -106,7 +106,7 @@ unsigned short TcpSocket::GetRemotePort() const
////////////////////////////////////////////////////////////
Socket::Status TcpSocket::Connect(const IpAddress& remoteAddress, unsigned short remotePort, Uint32 timeout)
Socket::Status TcpSocket::Connect(const IpAddress& remoteAddress, unsigned short remotePort, Time timeout)
{
// Create the internal socket if it doesn't exist
Create();
@ -114,7 +114,7 @@ Socket::Status TcpSocket::Connect(const IpAddress& remoteAddress, unsigned short
// Create the remote address
sockaddr_in address = priv::SocketImpl::CreateAddress(remoteAddress.ToInteger(), remotePort);
if (timeout == 0)
if (timeout <= Time::Zero)
{
// ----- We're not using a timeout: just try to connect -----
@ -160,8 +160,8 @@ Socket::Status TcpSocket::Connect(const IpAddress& remoteAddress, unsigned short
// Setup the timeout
timeval time;
time.tv_sec = timeout / 1000;
time.tv_usec = (timeout - time.tv_sec * 1000) * 1000;
time.tv_sec = static_cast<long>(timeout.AsMicroseconds() / 1000000);
time.tv_usec = static_cast<long>(timeout.AsMicroseconds() % 1000000);
// Wait for something to write on our socket (which means that the connection request has returned)
if (select(static_cast<int>(GetHandle() + 1), NULL, &selector, NULL, &time) > 0)

View File

@ -24,6 +24,8 @@ set(SRC
${INCROOT}/ThreadLocal.hpp
${INCROOT}/ThreadLocalPtr.hpp
${INCROOT}/ThreadLocalPtr.inl
${SRCROOT}/Time.cpp
${INCROOT}/Time.hpp
${INCROOT}/Utf.hpp
${INCROOT}/Utf.inl
${INCROOT}/Vector2.hpp

View File

@ -37,24 +37,27 @@
namespace sf
{
////////////////////////////////////////////////////////////
Clock::Clock()
Clock::Clock() :
myStartTime(priv::ClockImpl::GetCurrentTime())
{
Reset();
}
////////////////////////////////////////////////////////////
Uint32 Clock::GetElapsedTime() const
Time Clock::GetElapsedTime() const
{
Uint64 microseconds = priv::ClockImpl::GetMicroSeconds() - myStartTime;
return static_cast<Uint32>(microseconds / 1000);
return priv::ClockImpl::GetCurrentTime() - myStartTime;
}
////////////////////////////////////////////////////////////
void Clock::Reset()
Time Clock::Restart()
{
myStartTime = priv::ClockImpl::GetMicroSeconds();
Time now = priv::ClockImpl::GetCurrentTime();
Time elapsed = now - myStartTime;
myStartTime = now;
return elapsed;
}
} // namespace sf

View File

@ -30,9 +30,6 @@
#include <cstdio>
////////////////////////////////////////////////////////////
// Private data
////////////////////////////////////////////////////////////
namespace
{
// This class will be used as the default streambuf of sf::Err,

View File

@ -37,8 +37,9 @@
namespace sf
{
////////////////////////////////////////////////////////////
void Sleep(Uint32 duration)
void Sleep(Time duration)
{
if (duration >= Time::Zero)
priv::SleepImpl(duration);
}

239
src/SFML/System/Time.cpp Normal file
View File

@ -0,0 +1,239 @@
////////////////////////////////////////////////////////////
//
// 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.
//
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/System/Time.hpp>
namespace sf
{
////////////////////////////////////////////////////////////
const Time Time::Zero;
////////////////////////////////////////////////////////////
Time::Time() :
myMicroseconds(0)
{
}
////////////////////////////////////////////////////////////
float Time::AsSeconds() const
{
return myMicroseconds / 1000000.f;
}
////////////////////////////////////////////////////////////
Int32 Time::AsMilliseconds() const
{
return static_cast<Uint32>(myMicroseconds / 1000);
}
////////////////////////////////////////////////////////////
Int64 Time::AsMicroseconds() const
{
return myMicroseconds;
}
////////////////////////////////////////////////////////////
Time::Time(Int64 microseconds) :
myMicroseconds(microseconds)
{
}
////////////////////////////////////////////////////////////
Time Seconds(float amount)
{
return Time(static_cast<Uint64>(amount * 1000000));
}
////////////////////////////////////////////////////////////
Time Milliseconds(Int32 amount)
{
return Time(static_cast<Uint64>(amount) * 1000);
}
////////////////////////////////////////////////////////////
Time Microseconds(Int64 amount)
{
return Time(amount);
}
////////////////////////////////////////////////////////////
bool operator ==(const Time& left, const Time& right)
{
return left.AsMicroseconds() == right.AsMicroseconds();
}
////////////////////////////////////////////////////////////
bool operator !=(const Time& left, const Time& right)
{
return left.AsMicroseconds() != right.AsMicroseconds();
}
////////////////////////////////////////////////////////////
bool operator <(const Time& left, const Time& right)
{
return left.AsMicroseconds() < right.AsMicroseconds();
}
////////////////////////////////////////////////////////////
bool operator >(const Time& left, const Time& right)
{
return left.AsMicroseconds() > right.AsMicroseconds();
}
////////////////////////////////////////////////////////////
bool operator <=(const Time& left, const Time& right)
{
return left.AsMicroseconds() <= right.AsMicroseconds();
}
////////////////////////////////////////////////////////////
bool operator >=(const Time& left, const Time& right)
{
return left.AsMicroseconds() >= right.AsMicroseconds();
}
////////////////////////////////////////////////////////////
Time operator -(const Time& right)
{
return Microseconds(-right.AsMicroseconds());
}
////////////////////////////////////////////////////////////
Time operator +(const Time& left, const Time& right)
{
return Microseconds(left.AsMicroseconds() + right.AsMicroseconds());
}
////////////////////////////////////////////////////////////
Time& operator +=(Time& left, const Time& right)
{
return left = left + right;
}
////////////////////////////////////////////////////////////
Time operator -(const Time& left, const Time& right)
{
return Microseconds(left.AsMicroseconds() - right.AsMicroseconds());
}
////////////////////////////////////////////////////////////
Time& operator -=(Time& left, const Time& right)
{
return left = left - right;
}
////////////////////////////////////////////////////////////
Time operator *(const Time& left, float right)
{
return Seconds(left.AsSeconds() * right);
}
////////////////////////////////////////////////////////////
Time operator *(const Time& left, Int64 right)
{
return Microseconds(left.AsMicroseconds() * right);
}
////////////////////////////////////////////////////////////
Time operator *(float left, const Time& right)
{
return right * left;
}
////////////////////////////////////////////////////////////
Time operator *(Int64 left, const Time& right)
{
return right * left;
}
////////////////////////////////////////////////////////////
Time& operator *=(Time& left, float right)
{
return left = left * right;
}
////////////////////////////////////////////////////////////
Time& operator *=(Time& left, Int64 right)
{
return left = left * right;
}
////////////////////////////////////////////////////////////
Time operator /(const Time& left, float right)
{
return Seconds(left.AsSeconds() / right);
}
////////////////////////////////////////////////////////////
Time operator /(const Time& left, Int64 right)
{
return Microseconds(left.AsMicroseconds() / right);
}
////////////////////////////////////////////////////////////
Time& operator /=(Time& left, float right)
{
return left = left / right;
}
////////////////////////////////////////////////////////////
Time& operator /=(Time& left, Int64 right)
{
return left = left / right;
}
} // namespace sf

View File

@ -38,7 +38,7 @@ namespace sf
namespace priv
{
////////////////////////////////////////////////////////////
Uint64 ClockImpl::GetMicroSeconds()
Time ClockImpl::GetCurrentTime()
{
#ifdef SFML_SYSTEM_MACOS
@ -47,14 +47,14 @@ Uint64 ClockImpl::GetMicroSeconds()
if (frequency.denom == 0)
mach_timebase_info(&frequency);
Uint64 nanoseconds = mach_absolute_time() * frequency.numer / frequency.denom;
return nanoseconds / 1000;
return sf::Microseconds(nanoseconds / 1000);
#else
// POSIX implementation
timespec time;
clock_gettime(CLOCK_MONOTONIC, &time);
return time.tv_sec * 1000000 + time.tv_nsec / 1000;
return sf::Microseconds(static_cast<Uint64>(time.tv_sec) * 1000000 + time.tv_nsec / 1000);
#endif
}

View File

@ -29,6 +29,7 @@
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Config.hpp>
#include <SFML/System/Time.hpp>
namespace sf
@ -46,10 +47,10 @@ public :
////////////////////////////////////////////////////////////
/// \brief Get the current time
///
/// \return Current time, in microseconds
/// \return Current time
///
////////////////////////////////////////////////////////////
static Uint64 GetMicroSeconds();
static Time GetCurrentTime();
};
} // namespace priv

View File

@ -36,7 +36,7 @@ namespace sf
namespace priv
{
////////////////////////////////////////////////////////////
void SleepImpl(Uint32 time)
void SleepImpl(Time time)
{
// usleep is not reliable enough (it might block the
// whole process instead of just the current thread)
@ -44,14 +44,16 @@ void SleepImpl(Uint32 time)
// this implementation is inspired from Qt
// first get the current time
Uint64 usecs = time.AsMicroseconds();
// get the current time
timeval tv;
gettimeofday(&tv, NULL);
// construct the time limit (current time + time to wait)
timespec ti;
ti.tv_nsec = (tv.tv_usec + (time % 1000) * 1000) * 1000;
ti.tv_sec = tv.tv_sec + (time / 1000) + (ti.tv_nsec / 1000000000);
ti.tv_nsec = (tv.tv_usec + (usecs % 1000000)) * 1000;
ti.tv_sec = tv.tv_sec + (usecs / 1000000) + (ti.tv_nsec / 1000000000);
ti.tv_nsec %= 1000000000;
// create a mutex and thread condition

View File

@ -29,6 +29,7 @@
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Config.hpp>
#include <SFML/System/Time.hpp>
namespace sf
@ -38,10 +39,10 @@ namespace priv
////////////////////////////////////////////////////////////
/// \brief Unix implementation of sf::Sleep
///
/// \param time Time to sleep, in milliseconds
/// \param time Time to sleep
///
////////////////////////////////////////////////////////////
void SleepImpl(Uint32 time);
void SleepImpl(Time time);
} // namespace priv

View File

@ -27,11 +27,9 @@
////////////////////////////////////////////////////////////
#include <SFML/System/Win32/ClockImpl.hpp>
#include <windows.h>
#undef GetCurrentTime // Windows macros are really evil...
////////////////////////////////////////////////////////////
// Private data
////////////////////////////////////////////////////////////
namespace
{
LARGE_INTEGER GetFrequency()
@ -47,7 +45,7 @@ namespace sf
namespace priv
{
////////////////////////////////////////////////////////////
Uint64 ClockImpl::GetMicroSeconds()
Time ClockImpl::GetCurrentTime()
{
// Force the following code to run on first core
// (see http://msdn.microsoft.com/en-us/library/windows/desktop/ms644904(v=vs.85).aspx)
@ -66,7 +64,7 @@ Uint64 ClockImpl::GetMicroSeconds()
SetThreadAffinityMask(currentThread, previousMask);
// Return the current time as microseconds
return 1000000 * time.QuadPart / frequency.QuadPart;
return sf::Microseconds(1000000 * time.QuadPart / frequency.QuadPart);
}
} // namespace priv

View File

@ -29,6 +29,7 @@
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Config.hpp>
#include <SFML/System/Time.hpp>
namespace sf
@ -46,10 +47,10 @@ public :
////////////////////////////////////////////////////////////
/// \brief Get the current time
///
/// \return Current time, in microseconds
/// \return Current time
///
////////////////////////////////////////////////////////////
static Uint64 GetMicroSeconds();
static Time GetCurrentTime();
};
} // namespace priv

View File

@ -34,9 +34,9 @@ namespace sf
namespace priv
{
////////////////////////////////////////////////////////////
void SleepImpl(Uint32 time)
void SleepImpl(Time time)
{
::Sleep(time);
::Sleep(time.AsMilliseconds());
}
} // namespace priv

View File

@ -29,6 +29,7 @@
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Config.hpp>
#include <SFML/System/Time.hpp>
namespace sf
@ -38,10 +39,10 @@ namespace priv
////////////////////////////////////////////////////////////
/// \brief Windows implementation of sf::Sleep
///
/// \param time Time to sleep, in milliseconds
/// \param time Time to sleep
///
////////////////////////////////////////////////////////////
void SleepImpl(Uint32 time);
void SleepImpl(Time time);
} // namespace priv

View File

@ -54,9 +54,6 @@
#endif
////////////////////////////////////////////////////////////
// Private data
////////////////////////////////////////////////////////////
namespace
{
// This per-thread variable holds the current context for each thread

View File

@ -31,9 +31,6 @@
#include <SFML/System/Lock.hpp>
////////////////////////////////////////////////////////////
// Private data
////////////////////////////////////////////////////////////
namespace
{
// OpenGL resources counter and its mutex

View File

@ -47,9 +47,6 @@
#endif
////////////////////////////////////////////////////////////
// Private data
////////////////////////////////////////////////////////////
namespace
{
unsigned int WindowCount = 0;

View File

@ -32,9 +32,6 @@
#include <SFML/System/Err.hpp>
////////////////////////////////////////////////////////////
// Private data
////////////////////////////////////////////////////////////
namespace
{
const sf::Window* fullscreenWindow = NULL;
@ -47,7 +44,6 @@ namespace sf
Window::Window() :
myImpl (NULL),
myContext (NULL),
myLastFrameTime (0),
myFramerateLimit(0)
{
@ -58,7 +54,6 @@ myFramerateLimit(0)
Window::Window(VideoMode mode, const std::string& title, Uint32 style, const ContextSettings& settings) :
myImpl (NULL),
myContext (NULL),
myLastFrameTime (0),
myFramerateLimit(0)
{
Create(mode, title, style, settings);
@ -69,7 +64,6 @@ myFramerateLimit(0)
Window::Window(WindowHandle handle, const ContextSettings& settings) :
myImpl (NULL),
myContext (NULL),
myLastFrameTime (0),
myFramerateLimit(0)
{
Create(handle, settings);
@ -317,15 +311,10 @@ void Window::Display()
// Limit the framerate if needed
if (myFramerateLimit > 0)
{
Int32 remainingTime = 1000 / myFramerateLimit - myClock.GetElapsedTime();
if (remainingTime > 0)
Time remainingTime = Seconds(1.f / myFramerateLimit) - myClock.Restart();
Sleep(remainingTime);
}
// Measure the time elapsed since last frame
myLastFrameTime = myClock.GetElapsedTime();
myClock.Reset();
// Display the backbuffer on screen
if (SetActive())
myContext->Display();
@ -339,13 +328,6 @@ void Window::SetFramerateLimit(unsigned int limit)
}
////////////////////////////////////////////////////////////
Uint32 Window::GetFrameTime() const
{
return myLastFrameTime;
}
////////////////////////////////////////////////////////////
void Window::SetJoystickThreshold(float threshold)
{
@ -396,8 +378,7 @@ void Window::Initialize()
EnableKeyRepeat(true);
// Reset frame time
myClock.Reset();
myLastFrameTime = 0;
myClock.Restart();
// Activate the window
SetActive();

View File

@ -132,7 +132,7 @@ bool WindowImpl::PopEvent(Event& event, bool block)
{
ProcessJoystickEvents();
ProcessEvents();
Sleep(10);
Sleep(Milliseconds(10));
}
}
}