Fixed Window::SetFramerateLimit

This commit is contained in:
Laurent Gomila 2012-01-31 07:53:02 +01:00
parent feae26e15b
commit 84d75ed487
2 changed files with 15 additions and 12 deletions

View File

@ -462,7 +462,7 @@ private :
priv::WindowImpl* myImpl; ///< Platform-specific implementation of the window priv::WindowImpl* myImpl; ///< Platform-specific implementation of the window
priv::GlContext* myContext; ///< Platform-specific implementation of the OpenGL context priv::GlContext* myContext; ///< Platform-specific implementation of the OpenGL context
Clock myClock; ///< Clock for measuring the elapsed time between frames Clock myClock; ///< Clock for measuring the elapsed time between frames
unsigned int myFramerateLimit; ///< Current framerate limit Time myFrameTimeLimit; ///< Current framerate limit
}; };
} // namespace sf } // namespace sf

View File

@ -44,7 +44,7 @@ namespace sf
Window::Window() : Window::Window() :
myImpl (NULL), myImpl (NULL),
myContext (NULL), myContext (NULL),
myFramerateLimit(0) myFrameTimeLimit(Time::Zero)
{ {
} }
@ -54,7 +54,7 @@ myFramerateLimit(0)
Window::Window(VideoMode mode, const std::string& title, Uint32 style, const ContextSettings& settings) : Window::Window(VideoMode mode, const std::string& title, Uint32 style, const ContextSettings& settings) :
myImpl (NULL), myImpl (NULL),
myContext (NULL), myContext (NULL),
myFramerateLimit(0) myFrameTimeLimit(Time::Zero)
{ {
Create(mode, title, style, settings); Create(mode, title, style, settings);
} }
@ -64,7 +64,7 @@ myFramerateLimit(0)
Window::Window(WindowHandle handle, const ContextSettings& settings) : Window::Window(WindowHandle handle, const ContextSettings& settings) :
myImpl (NULL), myImpl (NULL),
myContext (NULL), myContext (NULL),
myFramerateLimit(0) myFrameTimeLimit(Time::Zero)
{ {
Create(handle, settings); Create(handle, settings);
} }
@ -308,23 +308,26 @@ bool Window::SetActive(bool active) const
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
void Window::Display() void Window::Display()
{ {
// Limit the framerate if needed
if (myFramerateLimit > 0)
{
Time remainingTime = Seconds(1.f / myFramerateLimit) - myClock.Restart();
Sleep(remainingTime);
}
// Display the backbuffer on screen // Display the backbuffer on screen
if (SetActive()) if (SetActive())
myContext->Display(); myContext->Display();
// Limit the framerate if needed
if (myFrameTimeLimit != Time::Zero)
{
Sleep(myFrameTimeLimit - myClock.GetElapsedTime());
myClock.Restart();
}
} }
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
void Window::SetFramerateLimit(unsigned int limit) void Window::SetFramerateLimit(unsigned int limit)
{ {
myFramerateLimit = limit; if (limit > 0)
myFrameTimeLimit = Seconds(1.f / limit);
else
myFrameTimeLimit = Time::Zero;
} }