Ion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
setswapinterval.cc
Go to the documentation of this file.
1 
18 #if defined(ION_PLATFORM_MAC)
19 # include <OpenGL/OpenGL.h> // For CGLContext.
20 #endif
21 
23 
25 #include "ion/portgfx/glheaders.h"
26 
27 namespace ion {
28 namespace portgfx {
29 
43 bool SetSwapInterval(int interval) {
44  if (interval < 0)
45  return false;
46 #if defined(ION_PLATFORM_IOS) || defined(ION_PLATFORM_ANDROID) || \
47  defined(ION_PLATFORM_ASMJS) || defined(ION_PLATFORM_NACL) || \
48  defined(ION_PLATFORM_GENERIC_ARM)
49  return true;
50 #elif defined(ION_PLATFORM_LINUX)
51  typedef int (ION_APIENTRY *SwapIntervalProc)(int interval);
52  SwapIntervalProc vsync_func = reinterpret_cast<SwapIntervalProc>(
53  GetGlProcAddress("glXSwapIntervalSGI", false));
54  return vsync_func && vsync_func(interval) == 0;
55 #elif defined(ION_PLATFORM_MAC)
56  if (CGLContextObj context = CGLGetCurrentContext()) {
57  CGLSetParameter(context, kCGLCPSwapInterval, &interval);
58  int new_interval = -1;
59  CGLGetParameter(context, kCGLCPSwapInterval, &new_interval);
60  return interval == new_interval;
61  }
62  return false;
63 #elif defined(ION_PLATFORM_WINDOWS) && !defined(ION_ANGLE)
64  typedef BOOL (ION_APIENTRY *SwapIntervalProc)(int interval);
65  typedef int (ION_APIENTRY *GetSwapIntervalProc)();
66  SwapIntervalProc vsync_set_func = reinterpret_cast<SwapIntervalProc>(
67  GetGlProcAddress("wglSwapIntervalEXT", false));
68  GetSwapIntervalProc vsync_get_func = reinterpret_cast<GetSwapIntervalProc>(
69  GetGlProcAddress("wglGetSwapIntervalEXT", false));
70  if (vsync_get_func && vsync_set_func) {
71  BOOL set_val = vsync_set_func(interval);
72  int get_val = vsync_get_func();
73  return set_val == TRUE && get_val == interval;
74  }
75  return false;
76 #elif defined(ION_GFX_OGLES20) || defined(ION_ANGLE)
77  EGLDisplay display = eglGetCurrentDisplay();
78  return display != EGL_NO_DISPLAY &&
79  eglSwapInterval(display, interval) == EGL_TRUE;
80 #endif
81 }
82 
83 } // namespace portgfx
84 } // namespace ion
void * GetGlProcAddress(const char *name, bool is_core)
Returns a generic pointer to an OpenGL function or OpenGL extension function with the given name...
bool SetSwapInterval(int interval)
Setting the swap interval is very platform-dependent, so we define a wrapper function for it here...