Merge branch 'gles'
[neverball] / share / sync.c
1 /*
2  * Copyright (C) 2003 Robert Kooima
3  *
4  * NEVERBALL is  free software; you can redistribute  it and/or modify
5  * it under the  terms of the GNU General  Public License as published
6  * by the Free  Software Foundation; either version 2  of the License,
7  * or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT  ANY  WARRANTY;  without   even  the  implied  warranty  of
11  * MERCHANTABILITY or  FITNESS FOR A PARTICULAR PURPOSE.   See the GNU
12  * General Public License for more details.
13  */
14
15 #include <SDL_syswm.h>
16 #include "glext.h"
17
18 /*---------------------------------------------------------------------------*/
19 #if defined(_WIN32)
20
21 void sync_init(void)
22 {
23     return;
24 }
25
26 /*---------------------------------------------------------------------------*/
27 #elif defined(__APPLE__)
28
29 #include <OpenGL/OpenGL.h>
30
31 void sync_init(void)
32 {
33     GLint swap = 1;
34     CGLSetParameter(CGLGetCurrentContext(),  kCGLCPSwapInterval, &swap);
35 }
36
37 /*---------------------------------------------------------------------------*/
38 #elif SDL_VIDEO_DRIVER_X11
39
40 #include <GL/glx.h>
41
42 static int search(const char *haystack, const char *needle)
43 {
44     const char *c;
45
46     for (; *haystack; haystack++)
47     {
48         for (c = needle; *c && *haystack; c++, haystack++)
49             if (*c != *haystack)
50                 break;
51
52         if ((*c == 0) && (*haystack == ' ' || *haystack == '\0'))
53             return 1;
54     }
55
56     return 0;
57 }
58
59 void sync_init(void)
60 {
61     SDL_SysWMinfo info;
62     Display *dpy;
63
64     SDL_VERSION(&info.version);
65
66     if (SDL_GetWMInfo(&info) != 1)
67         return;
68
69     if (info.subsystem != SDL_SYSWM_X11)
70         return;
71
72     dpy = info.info.x11.display;
73
74     info.info.x11.lock_func();
75     {
76         int scr = DefaultScreen(dpy);
77
78         PFNGLXSWAPINTERVALSGIPROC _glXSwapIntervalSGI = NULL;
79
80         if (search(glXQueryExtensionsString(dpy, scr), "GLX_SGI_swap_control"))
81         {
82             if ((_glXSwapIntervalSGI = (PFNGLXSWAPINTERVALSGIPROC)
83                  glXGetProcAddress((const GLubyte *) "glXSwapIntervalSGI")))
84                 _glXSwapIntervalSGI(1);
85         }
86     }
87     info.info.x11.unlock_func();
88 }
89
90 #endif
91
92 /*---------------------------------------------------------------------------*/