From 728f11701b2224445584b659ca04e238882b4fcb Mon Sep 17 00:00:00 2001 From: Laurent Gomila Date: Fri, 8 Nov 2013 14:49:58 +0100 Subject: [PATCH] [iOS] Added (fake) implementation of vertical synchronization --- src/SFML/Window/iOS/EaglContext.hpp | 3 +++ src/SFML/Window/iOS/EaglContext.mm | 42 ++++++++++++++++++++--------- 2 files changed, 33 insertions(+), 12 deletions(-) diff --git a/src/SFML/Window/iOS/EaglContext.hpp b/src/SFML/Window/iOS/EaglContext.hpp index 6018432e7..6abc4278e 100644 --- a/src/SFML/Window/iOS/EaglContext.hpp +++ b/src/SFML/Window/iOS/EaglContext.hpp @@ -31,6 +31,7 @@ #include #include #include +#include #include @@ -153,6 +154,8 @@ private: GLuint m_framebuffer; ///< Frame buffer associated to the context GLuint m_colorbuffer; ///< Color render buffer GLuint m_depthbuffer; ///< Depth render buffer + bool m_vsyncEnabled; ///< Vertical sync activation flag + Clock m_clock; ///< Measures the elapsed time for the fake v-sync implementation }; } // namespace priv diff --git a/src/SFML/Window/iOS/EaglContext.mm b/src/SFML/Window/iOS/EaglContext.mm index 1deaee3b5..9ce0c0e5f 100644 --- a/src/SFML/Window/iOS/EaglContext.mm +++ b/src/SFML/Window/iOS/EaglContext.mm @@ -29,6 +29,7 @@ #include #include #include +#include #include #include #include @@ -41,10 +42,12 @@ namespace priv { //////////////////////////////////////////////////////////// EaglContext::EaglContext(EaglContext* shared) : -m_context (nil), -m_framebuffer(0), -m_colorbuffer(0), -m_depthbuffer(0) +m_context (nil), +m_framebuffer (0), +m_colorbuffer (0), +m_depthbuffer (0), +m_vsyncEnabled(false), +m_clock () { // Create the context if (shared) @@ -57,10 +60,12 @@ m_depthbuffer(0) //////////////////////////////////////////////////////////// EaglContext::EaglContext(EaglContext* shared, const ContextSettings& settings, const WindowImpl* owner, unsigned int bitsPerPixel) : -m_context (nil), -m_framebuffer(0), -m_colorbuffer(0), -m_depthbuffer(0) +m_context (nil), +m_framebuffer (0), +m_colorbuffer (0), +m_depthbuffer (0), +m_vsyncEnabled(false), +m_clock () { const WindowImplUIKit* window = static_cast(owner); @@ -71,10 +76,12 @@ m_depthbuffer(0) //////////////////////////////////////////////////////////// EaglContext::EaglContext(EaglContext* shared, const ContextSettings& settings, unsigned int width, unsigned int height) : -m_context (nil), -m_framebuffer(0), -m_colorbuffer(0), -m_depthbuffer(0) +m_context (nil), +m_framebuffer (0), +m_colorbuffer (0), +m_depthbuffer (0), +m_vsyncEnabled(false), +m_clock () { // This constructor shoult never be used by implementation err() << "Calling bad EaglContext constructor, please contact your developer :)" << std::endl; @@ -167,12 +174,23 @@ void EaglContext::display() { glBindRenderbufferOES(GL_RENDERBUFFER_OES, m_colorbuffer); [m_context presentRenderbuffer:GL_RENDERBUFFER_OES]; + + // The proper way of doing v-sync on iOS would be to use CADisplayLink + // notifications, but it is not compatible with the way SFML is designed; + // therefore we fake it with a manual framerate limit + if (m_vsyncEnabled) + { + static const Time frameDuration = seconds(1.f / 60.f); + sleep(frameDuration - m_clock.getElapsedTime()); + m_clock.restart(); + } } //////////////////////////////////////////////////////////// void EaglContext::setVerticalSyncEnabled(bool enabled) { + m_vsyncEnabled = enabled; }