Added the sf::Time class
This commit is contained in:
parent
e775bd0169
commit
4116ad033c
58 changed files with 880 additions and 227 deletions
|
@ -31,9 +31,6 @@
|
|||
#include <SFML/System/Err.hpp>
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
// Private data
|
||||
////////////////////////////////////////////////////////////
|
||||
namespace
|
||||
{
|
||||
ALCdevice* audioDevice = NULL;
|
||||
|
|
|
@ -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());
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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 :
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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))
|
||||
|
|
|
@ -37,9 +37,6 @@
|
|||
#include <cstring>
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
// Private data
|
||||
////////////////////////////////////////////////////////////
|
||||
namespace
|
||||
{
|
||||
// FreeType callbacks that operate on a sf::InputStream
|
||||
|
|
|
@ -39,9 +39,6 @@ extern "C"
|
|||
#include <cctype>
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
// Private data
|
||||
////////////////////////////////////////////////////////////
|
||||
namespace
|
||||
{
|
||||
// Convert a string to lower case
|
||||
|
|
|
@ -35,9 +35,6 @@
|
|||
#include <vector>
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
// Private data
|
||||
////////////////////////////////////////////////////////////
|
||||
namespace
|
||||
{
|
||||
// Retrieve the maximum number of texture units available
|
||||
|
|
|
@ -32,9 +32,6 @@
|
|||
#include <cmath>
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
// Private data
|
||||
////////////////////////////////////////////////////////////
|
||||
namespace
|
||||
{
|
||||
// Compute the normal of a segment
|
||||
|
|
|
@ -37,9 +37,6 @@
|
|||
#include <cstring>
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
// Private data
|
||||
////////////////////////////////////////////////////////////
|
||||
namespace
|
||||
{
|
||||
// Thread-safe unique identifier generator,
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -30,9 +30,6 @@
|
|||
#include <cstdio>
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
// Private data
|
||||
////////////////////////////////////////////////////////////
|
||||
namespace
|
||||
{
|
||||
// This class will be used as the default streambuf of sf::Err,
|
||||
|
|
|
@ -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
|
||||
|
|
239
src/SFML/System/Time.cpp
Normal file
239
src/SFML/System/Time.cpp
Normal 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
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -34,9 +34,9 @@ namespace sf
|
|||
namespace priv
|
||||
{
|
||||
////////////////////////////////////////////////////////////
|
||||
void SleepImpl(Uint32 time)
|
||||
void SleepImpl(Time time)
|
||||
{
|
||||
::Sleep(time);
|
||||
::Sleep(time.AsMilliseconds());
|
||||
}
|
||||
|
||||
} // namespace priv
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -54,9 +54,6 @@
|
|||
#endif
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
// Private data
|
||||
////////////////////////////////////////////////////////////
|
||||
namespace
|
||||
{
|
||||
// This per-thread variable holds the current context for each thread
|
||||
|
|
|
@ -31,9 +31,6 @@
|
|||
#include <SFML/System/Lock.hpp>
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
// Private data
|
||||
////////////////////////////////////////////////////////////
|
||||
namespace
|
||||
{
|
||||
// OpenGL resources counter and its mutex
|
||||
|
|
|
@ -47,9 +47,6 @@
|
|||
#endif
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
// Private data
|
||||
////////////////////////////////////////////////////////////
|
||||
namespace
|
||||
{
|
||||
unsigned int WindowCount = 0;
|
||||
|
|
|
@ -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)
|
||||
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();
|
||||
|
|
|
@ -132,7 +132,7 @@ bool WindowImpl::PopEvent(Event& event, bool block)
|
|||
{
|
||||
ProcessJoystickEvents();
|
||||
ProcessEvents();
|
||||
Sleep(10);
|
||||
Sleep(Milliseconds(10));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue