From 4116ad033c6e2f2fe50e19316d49d41585fb17a0 Mon Sep 17 00:00:00 2001 From: Laurent Gomila Date: Thu, 19 Jan 2012 23:51:06 +0100 Subject: [PATCH] Added the sf::Time class --- examples/X11/X11.cpp | 16 +- examples/opengl/OpenGL.cpp | 6 +- examples/pong/Pong.cpp | 7 +- examples/shader/Shader.cpp | 2 +- examples/sound/Sound.cpp | 20 +- examples/sound_capture/SoundCapture.cpp | 10 +- examples/voip/Server.cpp | 10 +- examples/win32/Win32.cpp | 6 +- examples/window/Window.cpp | 6 +- include/SFML/Audio/Music.hpp | 11 +- include/SFML/Audio/Sound.hpp | 9 +- include/SFML/Audio/SoundBuffer.hpp | 7 +- include/SFML/Audio/SoundStream.hpp | 13 +- include/SFML/Network/Ftp.hpp | 7 +- include/SFML/Network/Http.hpp | 7 +- include/SFML/Network/IpAddress.hpp | 5 +- include/SFML/Network/SocketSelector.hpp | 5 +- include/SFML/Network/TcpSocket.hpp | 5 +- include/SFML/System/Clock.hpp | 37 +- include/SFML/System/Sleep.hpp | 5 +- include/SFML/System/Time.hpp | 452 ++++++++++++++++++++++ include/SFML/Window/Window.hpp | 14 - src/SFML/Audio/AudioDevice.cpp | 3 - src/SFML/Audio/Music.cpp | 9 +- src/SFML/Audio/Sound.cpp | 8 +- src/SFML/Audio/SoundBuffer.cpp | 6 +- src/SFML/Audio/SoundFile.cpp | 7 +- src/SFML/Audio/SoundFile.hpp | 5 +- src/SFML/Audio/SoundRecorder.cpp | 5 +- src/SFML/Audio/SoundStream.cpp | 14 +- src/SFML/Graphics/Font.cpp | 3 - src/SFML/Graphics/ImageLoader.cpp | 3 - src/SFML/Graphics/Shader.cpp | 3 - src/SFML/Graphics/Shape.cpp | 3 - src/SFML/Graphics/Texture.cpp | 3 - src/SFML/Network/Ftp.cpp | 2 +- src/SFML/Network/Http.cpp | 5 +- src/SFML/Network/IpAddress.cpp | 2 +- src/SFML/Network/SocketSelector.cpp | 8 +- src/SFML/Network/TcpSocket.cpp | 8 +- src/SFML/System/CMakeLists.txt | 2 + src/SFML/System/Clock.cpp | 17 +- src/SFML/System/Err.cpp | 3 - src/SFML/System/Sleep.cpp | 5 +- src/SFML/System/Time.cpp | 239 ++++++++++++ src/SFML/System/Unix/ClockImpl.cpp | 6 +- src/SFML/System/Unix/ClockImpl.hpp | 5 +- src/SFML/System/Unix/SleepImpl.cpp | 10 +- src/SFML/System/Unix/SleepImpl.hpp | 5 +- src/SFML/System/Win32/ClockImpl.cpp | 8 +- src/SFML/System/Win32/ClockImpl.hpp | 5 +- src/SFML/System/Win32/SleepImpl.cpp | 4 +- src/SFML/System/Win32/SleepImpl.hpp | 5 +- src/SFML/Window/GlContext.cpp | 3 - src/SFML/Window/GlResource.cpp | 3 - src/SFML/Window/Win32/WindowImplWin32.cpp | 3 - src/SFML/Window/Window.cpp | 25 +- src/SFML/Window/WindowImpl.cpp | 2 +- 58 files changed, 880 insertions(+), 227 deletions(-) create mode 100644 include/SFML/System/Time.hpp create mode 100644 src/SFML/System/Time.cpp diff --git a/examples/X11/X11.cpp b/examples/X11/X11.cpp index 28eae8626..47939e940 100644 --- a/examples/X11/X11.cpp +++ b/examples/X11/X11.cpp @@ -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(); diff --git a/examples/opengl/OpenGL.cpp b/examples/opengl/OpenGL.cpp index f5681f954..5f408e5b2 100644 --- a/examples/opengl/OpenGL.cpp +++ b/examples/opengl/OpenGL.cpp @@ -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; diff --git a/examples/pong/Pong.cpp b/examples/pong/Pong.cpp index d5faa199c..e1f487b3e 100644 --- a/examples/pong/Pong.cpp +++ b/examples/pong/Pong.cpp @@ -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) diff --git a/examples/shader/Shader.cpp b/examples/shader/Shader.cpp index 2f388c43f..cb5c72cbe 100644 --- a/examples/shader/Shader.cpp +++ b/examples/shader/Shader.cpp @@ -351,7 +351,7 @@ int main() // Update the current example float x = static_cast(sf::Mouse::GetPosition(window).x) / window.GetWidth(); float y = static_cast(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)); diff --git a/examples/sound/Sound.cpp b/examples/sound/Sound.cpp index 89537351c..8ea7c9fb4 100644 --- a/examples/sound/Sound.cpp +++ b/examples/sound/Sound.cpp @@ -20,9 +20,9 @@ void PlaySound() // Display sound informations std::cout << "canary.wav :" << std::endl; - std::cout << " " << buffer.GetDuration() / 1000.f << " seconds" << std::endl; - std::cout << " " << buffer.GetSampleRate() << " samples / sec" << std::endl; - std::cout << " " << buffer.GetChannelCount() << " channels" << 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; // Create a sound instance and play it sf::Sound sound(buffer); @@ -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,9 +55,9 @@ void PlayMusic() // Display music informations std::cout << "orchestral.ogg :" << std::endl; - std::cout << " " << music.GetDuration() / 1000.f << " seconds" << std::endl; - std::cout << " " << music.GetSampleRate() << " samples / sec" << std::endl; - std::cout << " " << music.GetChannelCount() << " channels" << 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; // Play it music.Play(); @@ -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; diff --git a/examples/sound_capture/SoundCapture.cpp b/examples/sound_capture/SoundCapture.cpp index c6fa35871..36e842884 100644 --- a/examples/sound_capture/SoundCapture.cpp +++ b/examples/sound_capture/SoundCapture.cpp @@ -46,9 +46,9 @@ int main() // Display captured sound informations std::cout << "Sound information :" << std::endl; - std::cout << " " << buffer.GetDuration() / 1000.f << " seconds" << std::endl; - std::cout << " " << buffer.GetSampleRate() << " samples / seconds" << std::endl; - std::cout << " " << buffer.GetChannelCount() << " channels" << 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; // Choose what to do with the recorded sound data char choice; @@ -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)); } } diff --git a/examples/voip/Server.cpp b/examples/voip/Server.cpp index 018541887..b43f09497 100644 --- a/examples/voip/Server.cpp +++ b/examples/voip/Server.cpp @@ -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)); } } diff --git a/examples/win32/Win32.cpp b/examples/win32/Win32.cpp index 34db79070..a8b2ae25e 100644 --- a/examples/win32/Win32.cpp +++ b/examples/win32/Win32.cpp @@ -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 diff --git a/examples/window/Window.cpp b/examples/window/Window.cpp index c5e524837..1707b738e 100644 --- a/examples/window/Window.cpp +++ b/examples/window/Window.cpp @@ -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); diff --git a/include/SFML/Audio/Music.hpp b/include/SFML/Audio/Music.hpp index 764540710..83367f561 100644 --- a/include/SFML/Audio/Music.hpp +++ b/include/SFML/Audio/Music.hpp @@ -30,6 +30,7 @@ //////////////////////////////////////////////////////////// #include #include +#include #include #include @@ -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 mySamples; ///< Temporary buffer of samples Mutex myMutex; ///< Mutex protecting the data }; diff --git a/include/SFML/Audio/Sound.hpp b/include/SFML/Audio/Sound.hpp index 47ce64546..8985a2b01 100644 --- a/include/SFML/Audio/Sound.hpp +++ b/include/SFML/Audio/Sound.hpp @@ -29,6 +29,7 @@ // Headers //////////////////////////////////////////////////////////// #include +#include #include @@ -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) diff --git a/include/SFML/Audio/SoundBuffer.hpp b/include/SFML/Audio/SoundBuffer.hpp index 14956a9ef..2144bbfc7 100644 --- a/include/SFML/Audio/SoundBuffer.hpp +++ b/include/SFML/Audio/SoundBuffer.hpp @@ -29,6 +29,7 @@ // Headers //////////////////////////////////////////////////////////// #include +#include #include #include #include @@ -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 mySamples; ///< Samples buffer - Uint32 myDuration; ///< Sound duration, in milliseconds + Time myDuration; ///< Sound duration mutable SoundList mySounds; ///< List of sounds that are using this buffer }; diff --git a/include/SFML/Audio/SoundStream.hpp b/include/SFML/Audio/SoundStream.hpp index 4c4a2a3ec..ff6d0c239 100644 --- a/include/SFML/Audio/SoundStream.hpp +++ b/include/SFML/Audio/SoundStream.hpp @@ -30,6 +30,7 @@ //////////////////////////////////////////////////////////// #include #include +#include #include @@ -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 diff --git a/include/SFML/Network/Ftp.hpp b/include/SFML/Network/Ftp.hpp index e4a81d79e..c6466058a 100644 --- a/include/SFML/Network/Ftp.hpp +++ b/include/SFML/Network/Ftp.hpp @@ -29,6 +29,7 @@ // Headers //////////////////////////////////////////////////////////// #include +#include #include #include #include @@ -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 diff --git a/include/SFML/Network/Http.hpp b/include/SFML/Network/Http.hpp index 02f0d2573..bfecb0757 100644 --- a/include/SFML/Network/Http.hpp +++ b/include/SFML/Network/Http.hpp @@ -29,6 +29,7 @@ // Headers //////////////////////////////////////////////////////////// #include +#include #include #include #include @@ -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 : diff --git a/include/SFML/Network/IpAddress.hpp b/include/SFML/Network/IpAddress.hpp index a2a6a1caa..a919ebcdb 100644 --- a/include/SFML/Network/IpAddress.hpp +++ b/include/SFML/Network/IpAddress.hpp @@ -29,6 +29,7 @@ // Headers //////////////////////////////////////////////////////////// #include +#include #include #include #include @@ -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 diff --git a/include/SFML/Network/SocketSelector.hpp b/include/SFML/Network/SocketSelector.hpp index 3f3747f2a..50d915f23 100644 --- a/include/SFML/Network/SocketSelector.hpp +++ b/include/SFML/Network/SocketSelector.hpp @@ -29,6 +29,7 @@ // Headers //////////////////////////////////////////////////////////// #include +#include 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 diff --git a/include/SFML/Network/TcpSocket.hpp b/include/SFML/Network/TcpSocket.hpp index 94cc87535..23125e8b3 100644 --- a/include/SFML/Network/TcpSocket.hpp +++ b/include/SFML/Network/TcpSocket.hpp @@ -29,6 +29,7 @@ // Headers //////////////////////////////////////////////////////////// #include +#include 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 diff --git a/include/SFML/System/Clock.hpp b/include/SFML/System/Clock.hpp index e854c2f99..344b41ef9 100644 --- a/include/SFML/System/Clock.hpp +++ b/include/SFML/System/Clock.hpp @@ -29,12 +29,13 @@ // Headers //////////////////////////////////////////////////////////// #include +#include 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 +/// //////////////////////////////////////////////////////////// diff --git a/include/SFML/System/Sleep.hpp b/include/SFML/System/Sleep.hpp index ff07549f2..9904fbeca 100644 --- a/include/SFML/System/Sleep.hpp +++ b/include/SFML/System/Sleep.hpp @@ -29,6 +29,7 @@ // Headers //////////////////////////////////////////////////////////// #include +#include 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 diff --git a/include/SFML/System/Time.hpp b/include/SFML/System/Time.hpp new file mode 100644 index 000000000..053db88ec --- /dev/null +++ b/include/SFML/System/Time.hpp @@ -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 + + +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 +/// +//////////////////////////////////////////////////////////// diff --git a/include/SFML/Window/Window.hpp b/include/SFML/Window/Window.hpp index 5933e11a8..f226522f2 100644 --- a/include/SFML/Window/Window.hpp +++ b/include/SFML/Window/Window.hpp @@ -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 }; diff --git a/src/SFML/Audio/AudioDevice.cpp b/src/SFML/Audio/AudioDevice.cpp index 5c39e4a50..57cb3e8c8 100644 --- a/src/SFML/Audio/AudioDevice.cpp +++ b/src/SFML/Audio/AudioDevice.cpp @@ -31,9 +31,6 @@ #include -//////////////////////////////////////////////////////////// -// Private data -//////////////////////////////////////////////////////////// namespace { ALCdevice* audioDevice = NULL; diff --git a/src/SFML/Audio/Music.cpp b/src/SFML/Audio/Music.cpp index be961cd44..5926c6bed 100644 --- a/src/SFML/Audio/Music.cpp +++ b/src/SFML/Audio/Music.cpp @@ -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(1000 * samples / myFile->GetSampleRate() / myFile->GetChannelCount()); + myDuration = Seconds(static_cast(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()); diff --git a/src/SFML/Audio/Sound.cpp b/src/SFML/Audio/Sound.cpp index 961fd2f13..025979c85 100644 --- a/src/SFML/Audio/Sound.cpp +++ b/src/SFML/Audio/Sound.cpp @@ -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(seconds * 1000); + return Seconds(seconds); } diff --git a/src/SFML/Audio/SoundBuffer.cpp b/src/SFML/Audio/SoundBuffer.cpp index 016443c9b..daa6a2f7a 100644 --- a/src/SFML/Audio/SoundBuffer.cpp +++ b/src/SFML/Audio/SoundBuffer.cpp @@ -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(1000 * mySamples.size() / sampleRate / channelCount); + myDuration = Milliseconds(1000 * mySamples.size() / sampleRate / channelCount); return true; } diff --git a/src/SFML/Audio/SoundFile.cpp b/src/SFML/Audio/SoundFile.cpp index 9b9ec3085..16052e771 100644 --- a/src/SFML/Audio/SoundFile.cpp +++ b/src/SFML/Audio/SoundFile.cpp @@ -32,9 +32,6 @@ #include -//////////////////////////////////////////////////////////// -// 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(timeOffset) * mySampleRate / 1000; + sf_count_t frameOffset = static_cast(timeOffset.AsSeconds() * mySampleRate); sf_seek(myFile, frameOffset, SEEK_SET); } } diff --git a/src/SFML/Audio/SoundFile.hpp b/src/SFML/Audio/SoundFile.hpp index a13e3707f..11375a23a 100644 --- a/src/SFML/Audio/SoundFile.hpp +++ b/src/SFML/Audio/SoundFile.hpp @@ -29,6 +29,7 @@ // Headers //////////////////////////////////////////////////////////// #include +#include #include #include @@ -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 : diff --git a/src/SFML/Audio/SoundRecorder.cpp b/src/SFML/Audio/SoundRecorder.cpp index ce1014545..b23d9bb78 100644 --- a/src/SFML/Audio/SoundRecorder.cpp +++ b/src/SFML/Audio/SoundRecorder.cpp @@ -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 diff --git a/src/SFML/Audio/SoundStream.cpp b/src/SFML/Audio/SoundStream.cpp index 4996464b4..8f6cf3c3d 100644 --- a/src/SFML/Audio/SoundStream.cpp +++ b/src/SFML/Audio/SoundStream.cpp @@ -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(timeOffset) * mySampleRate * myChannelCount / 1000; + mySamplesProcessed = static_cast(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(1000 * seconds + 1000 * mySamplesProcessed / mySampleRate / myChannelCount); + return Seconds(seconds + static_cast(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)) diff --git a/src/SFML/Graphics/Font.cpp b/src/SFML/Graphics/Font.cpp index 82050fc5a..091e10a0a 100644 --- a/src/SFML/Graphics/Font.cpp +++ b/src/SFML/Graphics/Font.cpp @@ -37,9 +37,6 @@ #include -//////////////////////////////////////////////////////////// -// Private data -//////////////////////////////////////////////////////////// namespace { // FreeType callbacks that operate on a sf::InputStream diff --git a/src/SFML/Graphics/ImageLoader.cpp b/src/SFML/Graphics/ImageLoader.cpp index f7c425789..928cf1396 100644 --- a/src/SFML/Graphics/ImageLoader.cpp +++ b/src/SFML/Graphics/ImageLoader.cpp @@ -39,9 +39,6 @@ extern "C" #include -//////////////////////////////////////////////////////////// -// Private data -//////////////////////////////////////////////////////////// namespace { // Convert a string to lower case diff --git a/src/SFML/Graphics/Shader.cpp b/src/SFML/Graphics/Shader.cpp index 30497e148..dc4c434c8 100644 --- a/src/SFML/Graphics/Shader.cpp +++ b/src/SFML/Graphics/Shader.cpp @@ -35,9 +35,6 @@ #include -//////////////////////////////////////////////////////////// -// Private data -//////////////////////////////////////////////////////////// namespace { // Retrieve the maximum number of texture units available diff --git a/src/SFML/Graphics/Shape.cpp b/src/SFML/Graphics/Shape.cpp index 4b39b8a0f..e70d52041 100644 --- a/src/SFML/Graphics/Shape.cpp +++ b/src/SFML/Graphics/Shape.cpp @@ -32,9 +32,6 @@ #include -//////////////////////////////////////////////////////////// -// Private data -//////////////////////////////////////////////////////////// namespace { // Compute the normal of a segment diff --git a/src/SFML/Graphics/Texture.cpp b/src/SFML/Graphics/Texture.cpp index 22207fe7e..1bbf697e1 100644 --- a/src/SFML/Graphics/Texture.cpp +++ b/src/SFML/Graphics/Texture.cpp @@ -37,9 +37,6 @@ #include -//////////////////////////////////////////////////////////// -// Private data -//////////////////////////////////////////////////////////// namespace { // Thread-safe unique identifier generator, diff --git a/src/SFML/Network/Ftp.cpp b/src/SFML/Network/Ftp.cpp index b0c29a502..ffe059f25 100644 --- a/src/SFML/Network/Ftp.cpp +++ b/src/SFML/Network/Ftp.cpp @@ -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) diff --git a/src/SFML/Network/Http.cpp b/src/SFML/Network/Http.cpp index d845eba5b..51adf88f9 100644 --- a/src/SFML/Network/Http.cpp +++ b/src/SFML/Network/Http.cpp @@ -32,9 +32,6 @@ #include -//////////////////////////////////////////////////////////// -// 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); diff --git a/src/SFML/Network/IpAddress.cpp b/src/SFML/Network/IpAddress.cpp index ae3d90cb9..a6cce52c8 100644 --- a/src/SFML/Network/IpAddress.cpp +++ b/src/SFML/Network/IpAddress.cpp @@ -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. diff --git a/src/SFML/Network/SocketSelector.cpp b/src/SFML/Network/SocketSelector.cpp index 295ea7406..9872a0709 100644 --- a/src/SFML/Network/SocketSelector.cpp +++ b/src/SFML/Network/SocketSelector.cpp @@ -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(timeout.AsMicroseconds() / 1000000); + time.tv_usec = static_cast(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; } diff --git a/src/SFML/Network/TcpSocket.cpp b/src/SFML/Network/TcpSocket.cpp index 16be229ab..a4842f29c 100644 --- a/src/SFML/Network/TcpSocket.cpp +++ b/src/SFML/Network/TcpSocket.cpp @@ -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(timeout.AsMicroseconds() / 1000000); + time.tv_usec = static_cast(timeout.AsMicroseconds() % 1000000); // Wait for something to write on our socket (which means that the connection request has returned) if (select(static_cast(GetHandle() + 1), NULL, &selector, NULL, &time) > 0) diff --git a/src/SFML/System/CMakeLists.txt b/src/SFML/System/CMakeLists.txt index 60f50b176..ec6ee945a 100644 --- a/src/SFML/System/CMakeLists.txt +++ b/src/SFML/System/CMakeLists.txt @@ -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 diff --git a/src/SFML/System/Clock.cpp b/src/SFML/System/Clock.cpp index 5039195bc..5b310d7f7 100644 --- a/src/SFML/System/Clock.cpp +++ b/src/SFML/System/Clock.cpp @@ -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(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 diff --git a/src/SFML/System/Err.cpp b/src/SFML/System/Err.cpp index 16f259904..cfe68aba0 100644 --- a/src/SFML/System/Err.cpp +++ b/src/SFML/System/Err.cpp @@ -30,9 +30,6 @@ #include -//////////////////////////////////////////////////////////// -// Private data -//////////////////////////////////////////////////////////// namespace { // This class will be used as the default streambuf of sf::Err, diff --git a/src/SFML/System/Sleep.cpp b/src/SFML/System/Sleep.cpp index d13780f2e..a3652b2c9 100644 --- a/src/SFML/System/Sleep.cpp +++ b/src/SFML/System/Sleep.cpp @@ -37,9 +37,10 @@ namespace sf { //////////////////////////////////////////////////////////// -void Sleep(Uint32 duration) +void Sleep(Time duration) { - priv::SleepImpl(duration); + if (duration >= Time::Zero) + priv::SleepImpl(duration); } } // namespace sf diff --git a/src/SFML/System/Time.cpp b/src/SFML/System/Time.cpp new file mode 100644 index 000000000..548f55ab9 --- /dev/null +++ b/src/SFML/System/Time.cpp @@ -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 + + +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(myMicroseconds / 1000); +} + + +//////////////////////////////////////////////////////////// +Int64 Time::AsMicroseconds() const +{ + return myMicroseconds; +} + + +//////////////////////////////////////////////////////////// +Time::Time(Int64 microseconds) : +myMicroseconds(microseconds) +{ +} + + +//////////////////////////////////////////////////////////// +Time Seconds(float amount) +{ + return Time(static_cast(amount * 1000000)); +} + + +//////////////////////////////////////////////////////////// +Time Milliseconds(Int32 amount) +{ + return Time(static_cast(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 diff --git a/src/SFML/System/Unix/ClockImpl.cpp b/src/SFML/System/Unix/ClockImpl.cpp index 4e1b683b4..ff7f6e749 100644 --- a/src/SFML/System/Unix/ClockImpl.cpp +++ b/src/SFML/System/Unix/ClockImpl.cpp @@ -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(time.tv_sec) * 1000000 + time.tv_nsec / 1000); #endif } diff --git a/src/SFML/System/Unix/ClockImpl.hpp b/src/SFML/System/Unix/ClockImpl.hpp index 5ee3ea91e..96d415889 100644 --- a/src/SFML/System/Unix/ClockImpl.hpp +++ b/src/SFML/System/Unix/ClockImpl.hpp @@ -29,6 +29,7 @@ // Headers //////////////////////////////////////////////////////////// #include +#include 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 diff --git a/src/SFML/System/Unix/SleepImpl.cpp b/src/SFML/System/Unix/SleepImpl.cpp index f5cbc29ed..0b3344d26 100644 --- a/src/SFML/System/Unix/SleepImpl.cpp +++ b/src/SFML/System/Unix/SleepImpl.cpp @@ -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 diff --git a/src/SFML/System/Unix/SleepImpl.hpp b/src/SFML/System/Unix/SleepImpl.hpp index e3a5cf781..e0b8b7c33 100644 --- a/src/SFML/System/Unix/SleepImpl.hpp +++ b/src/SFML/System/Unix/SleepImpl.hpp @@ -29,6 +29,7 @@ // Headers //////////////////////////////////////////////////////////// #include +#include 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 diff --git a/src/SFML/System/Win32/ClockImpl.cpp b/src/SFML/System/Win32/ClockImpl.cpp index f69d1aa42..1b10e4c14 100644 --- a/src/SFML/System/Win32/ClockImpl.cpp +++ b/src/SFML/System/Win32/ClockImpl.cpp @@ -27,11 +27,9 @@ //////////////////////////////////////////////////////////// #include #include +#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 diff --git a/src/SFML/System/Win32/ClockImpl.hpp b/src/SFML/System/Win32/ClockImpl.hpp index 1052779c8..eb9923c8f 100644 --- a/src/SFML/System/Win32/ClockImpl.hpp +++ b/src/SFML/System/Win32/ClockImpl.hpp @@ -29,6 +29,7 @@ // Headers //////////////////////////////////////////////////////////// #include +#include 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 diff --git a/src/SFML/System/Win32/SleepImpl.cpp b/src/SFML/System/Win32/SleepImpl.cpp index bbc67c6b4..de77823f8 100644 --- a/src/SFML/System/Win32/SleepImpl.cpp +++ b/src/SFML/System/Win32/SleepImpl.cpp @@ -34,9 +34,9 @@ namespace sf namespace priv { //////////////////////////////////////////////////////////// -void SleepImpl(Uint32 time) +void SleepImpl(Time time) { - ::Sleep(time); + ::Sleep(time.AsMilliseconds()); } } // namespace priv diff --git a/src/SFML/System/Win32/SleepImpl.hpp b/src/SFML/System/Win32/SleepImpl.hpp index b4f40f7fa..7c044afcb 100644 --- a/src/SFML/System/Win32/SleepImpl.hpp +++ b/src/SFML/System/Win32/SleepImpl.hpp @@ -29,6 +29,7 @@ // Headers //////////////////////////////////////////////////////////// #include +#include 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 diff --git a/src/SFML/Window/GlContext.cpp b/src/SFML/Window/GlContext.cpp index 196ed9c8b..73b7f897f 100644 --- a/src/SFML/Window/GlContext.cpp +++ b/src/SFML/Window/GlContext.cpp @@ -54,9 +54,6 @@ #endif -//////////////////////////////////////////////////////////// -// Private data -//////////////////////////////////////////////////////////// namespace { // This per-thread variable holds the current context for each thread diff --git a/src/SFML/Window/GlResource.cpp b/src/SFML/Window/GlResource.cpp index 0dd56c5ec..e1fd939ee 100644 --- a/src/SFML/Window/GlResource.cpp +++ b/src/SFML/Window/GlResource.cpp @@ -31,9 +31,6 @@ #include -//////////////////////////////////////////////////////////// -// Private data -//////////////////////////////////////////////////////////// namespace { // OpenGL resources counter and its mutex diff --git a/src/SFML/Window/Win32/WindowImplWin32.cpp b/src/SFML/Window/Win32/WindowImplWin32.cpp index f948723d1..debd804a9 100644 --- a/src/SFML/Window/Win32/WindowImplWin32.cpp +++ b/src/SFML/Window/Win32/WindowImplWin32.cpp @@ -47,9 +47,6 @@ #endif -//////////////////////////////////////////////////////////// -// Private data -//////////////////////////////////////////////////////////// namespace { unsigned int WindowCount = 0; diff --git a/src/SFML/Window/Window.cpp b/src/SFML/Window/Window.cpp index 1042dfe03..5c9eb00a0 100644 --- a/src/SFML/Window/Window.cpp +++ b/src/SFML/Window/Window.cpp @@ -32,9 +32,6 @@ #include -//////////////////////////////////////////////////////////// -// 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) - Sleep(remainingTime); + 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(); diff --git a/src/SFML/Window/WindowImpl.cpp b/src/SFML/Window/WindowImpl.cpp index 228018581..3f414e27e 100644 --- a/src/SFML/Window/WindowImpl.cpp +++ b/src/SFML/Window/WindowImpl.cpp @@ -132,7 +132,7 @@ bool WindowImpl::PopEvent(Event& event, bool block) { ProcessJoystickEvents(); ProcessEvents(); - Sleep(10); + Sleep(Milliseconds(10)); } } }