From 3dabe543b983aaa89ff3a85f790d6e1d60d93732 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jos=C3=A9=20Fonseca?= Date: Thu, 19 May 2011 17:57:18 +0100 Subject: [PATCH] Determine drawable sizes on MacOSX. Not sure it works correctly on Cocoa apps. --- CMakeLists.txt | 3 ++- glimports.hpp | 24 ++++++++++++++---------- glproc.py | 17 +++++++++-------- glstate.cpp | 47 ++++++++++++++++++++++++++++++++++++++++++++--- glws_glx.cpp | 3 +++ 5 files changed, 72 insertions(+), 22 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 44b4c84..35eccca 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -347,7 +347,8 @@ if (NOT WIN32) # We use GLX on MacOSX, which is in a separate library if (APPLE) find_library (X11_GL_LIB GL ${X11_LIB_SEARCH_PATH}) - target_link_libraries (glretrace ${X11_GL_LIB}) + find_library (APPLICATIONSERVICES ApplicationServices) + target_link_libraries (glretrace ${X11_GL_LIB} ${APPLICATIONSERVICES}) endif (APPLE) endif (NOT WIN32) diff --git a/glimports.hpp b/glimports.hpp index 720e48a..5cb276d 100644 --- a/glimports.hpp +++ b/glimports.hpp @@ -30,7 +30,8 @@ #ifndef _GLIMPORTS_HPP_ #define _GLIMPORTS_HPP_ -#ifdef _WIN32 + +#if defined(_WIN32) #ifndef WIN32_LEAN_AND_MEAN #define WIN32_LEAN_AND_MEAN 1 @@ -38,12 +39,15 @@ #include -#else /* !_WIN32 */ +#elif defined(__APPLE__) + +#else #include #endif /* !_WIN32 */ + // Prevent including system's glext.h #define __glext_h_ @@ -57,7 +61,8 @@ #define GL_TEXTURE_INDEX_SIZE_EXT 0x80ED #endif -#ifdef _WIN32 + +#if defined(_WIN32) #include "glext/wglext.h" @@ -83,7 +88,11 @@ typedef struct _WGLSWAP #endif /* !WGL_SWAPMULTIPLE_MAX */ -#else /* !_WIN32 */ +#elif defined(__APPLE__) + +#include + +#else #include #include "glext/glxext.h" @@ -91,12 +100,7 @@ typedef struct _WGLSWAP /* Prevent collision with Trace::Bool */ #undef Bool -#endif /* !_WIN32 */ - -#ifdef __APPLE__ - -#include +#endif -#endif /* __APPLE__ */ #endif /* _GLIMPORTS_HPP_ */ diff --git a/glproc.py b/glproc.py index 067017b..b036b35 100644 --- a/glproc.py +++ b/glproc.py @@ -411,8 +411,11 @@ class GlDispatcher(Dispatcher): def header(self): print '#ifdef RETRACE' - print '# ifdef _WIN32' + print '# if defined(_WIN32)' print '# define __getPrivateProcAddress(name) wglGetProcAddress(name)' + print '# elif defined(__APPLE__)' + print '# include ' + print '# define __getPrivateProcAddress(name) dlsym(RTLD_DEFAULT, name)' print '# else' print '# define __getPrivateProcAddress(name) glXGetProcAddressARB((const GLubyte *)(name))' print '# endif' @@ -450,17 +453,15 @@ if __name__ == '__main__': print dispatcher = GlDispatcher() dispatcher.header() - print '#ifdef _WIN32' + print '#if defined(_WIN32)' print dispatcher.dispatch_api(wglapi) - print '#else /* !_WIN32 */' + print '#elif defined(__APPLE__)' + dispatcher.dispatch_api(cglapi) + print '#else' print dispatcher.dispatch_api(glxapi) - print '#endif /* !_WIN32 */' - print - print '#ifdef __APPLE__' - dispatcher.dispatch_api(cglapi) - print '#endif /* __APPLE__ */' + print '#endif' print dispatcher.dispatch_api(glapi) print diff --git a/glstate.cpp b/glstate.cpp index a60c687..6dc6f80 100644 --- a/glstate.cpp +++ b/glstate.cpp @@ -35,6 +35,28 @@ #include "glstate.hpp" +#ifdef __APPLE__ + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +typedef int CGSConnectionID; +typedef int CGSWindowID; +typedef int CGSSurfaceID; + +CGLError CGLGetSurface(CGLContextObj, CGSConnectionID*, CGSWindowID*, CGSSurfaceID*); +OSStatus CGSGetSurfaceBounds(CGSConnectionID, CGWindowID, CGSSurfaceID, CGRect *); + +#ifdef __cplusplus +} +#endif + +#endif /* __APPLE__ */ + + namespace glstate { @@ -328,10 +350,29 @@ getDrawableBounds(GLint *width, GLint *height) { *width = rect.right - rect.left; *height = rect.bottom - rect.top; -#elif 0 /* __APPLE__ */ +#elif defined(__APPLE__) + + CGLContextObj ctx = CGLGetCurrentContext(); + if (ctx == NULL) { + return false; + } + + CGSConnectionID cid; + CGSWindowID wid; + CGSSurfaceID sid; + + if (CGLGetSurface(ctx, &cid, &wid, &sid) != kCGLNoError) { + return false; + } + + CGRect rect; + + if (CGSGetSurfaceBounds(cid, wid, sid, &rect) != 0) { + return false; + } - CGLError CGLGetSurface(CGLContextObj, CGSConnectionID*, CGSWindowID*, CGSSurfaceID*); - CGError CGSGetWindowBounds(CGSConnectionID, CGWindowID, CGRect *ret); + *width = rect.size.width; + *height = rect.size.height; #else diff --git a/glws_glx.cpp b/glws_glx.cpp index 62bbb06..bba108a 100644 --- a/glws_glx.cpp +++ b/glws_glx.cpp @@ -29,6 +29,9 @@ #include "glimports.hpp" #include "glws.hpp" +#include +#include + namespace glws { -- 2.45.2