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