Added the sf::Time class

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

View file

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

View file

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

View file

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

View file

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

View file

@ -20,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;

View file

@ -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));
}
}

View file

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

View file

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

View file

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