diff --git a/include/SFML/Window/Context.hpp b/include/SFML/Window/Context.hpp index 66e43259..29df67c0 100644 --- a/include/SFML/Window/Context.hpp +++ b/include/SFML/Window/Context.hpp @@ -41,6 +41,8 @@ namespace priv class GlContext; } +typedef void (*GlFunctionPointer)(); + //////////////////////////////////////////////////////////// /// \brief Class holding a valid drawing context /// @@ -76,6 +78,15 @@ public: bool setActive(bool active); public: + //////////////////////////////////////////////////////////// + /// \brief Get the address of an OpenGL function + /// + /// \param name Name of the function to get the address of + /// + /// \return Address of the OpenGL function, 0 on failure + /// + //////////////////////////////////////////////////////////// + static GlFunctionPointer getFunction(const char* name); //////////////////////////////////////////////////////////// /// \brief Construct a in-memory context diff --git a/src/SFML/Graphics/GLLoader.cpp b/src/SFML/Graphics/GLLoader.cpp index 8a52abff..33abd6a1 100644 --- a/src/SFML/Graphics/GLLoader.cpp +++ b/src/SFML/Graphics/GLLoader.cpp @@ -1,91 +1,17 @@ -#include -#include -#include + +//////////////////////////////////////////////////////////// +// Headers +//////////////////////////////////////////////////////////// #include +#include +#include +#include +#include -#if defined(__APPLE__) -#include - -static void* AppleGLGetProcAddress (const char *name) +static sf::GlFunctionPointer IntGetProcAddress(const char* name) { - static void* image = NULL; - - if (NULL == image) - image = dlopen("/System/Library/Frameworks/OpenGL.framework/Versions/Current/OpenGL", RTLD_LAZY); - - return (image ? dlsym(image, name) : NULL); + return sf::Context::getFunction(name); } -#define IntGetProcAddress(name) AppleGLGetProcAddress(name) -#endif /* __APPLE__ */ - -#if defined(__sgi) || defined (__sun) -#include -#include - -static void* SunGetProcAddress (const GLubyte* name) -{ - static void* h = NULL; - static void* gpa; - - if (h == NULL) - { - if ((h = dlopen(NULL, RTLD_LAZY | RTLD_LOCAL)) == NULL) return NULL; - gpa = dlsym(h, "glXGetProcAddress"); - } - - if (gpa != NULL) - return ((void*(*)(const GLubyte*))gpa)(name); - else - return dlsym(h, (const char*)name); -} -#endif /* __sgi || __sun */ - -#if defined(_WIN32) - -#ifdef _MSC_VER -#pragma warning(disable: 4055) -#pragma warning(disable: 4054) -#pragma warning(disable: 4996) -#endif - -static int TestPointer(const PROC pTest) -{ - ptrdiff_t iTest; - if(!pTest) return 0; - iTest = (ptrdiff_t)pTest; - - if(iTest == 1 || iTest == 2 || iTest == 3 || iTest == -1) return 0; - - return 1; -} - -static PROC WinGetProcAddress(const char *name) -{ - static HMODULE glMod = NULL; - PROC pFunc = wglGetProcAddress((LPCSTR)name); - - if (TestPointer(pFunc)) return pFunc; - - if (NULL == glMod) - glMod = GetModuleHandleA("OpenGL32.dll"); - - return (PROC)GetProcAddress(glMod, (LPCSTR)name); -} - -#define IntGetProcAddress(name) WinGetProcAddress(name) -#else - #if defined(__APPLE__) - #define IntGetProcAddress(name) AppleGLGetProcAddress(name) - #else - #if defined(__sgi) || defined(__sun) - #define IntGetProcAddress(name) SunGetProcAddress(name) - #else /* GLX */ - #include - - #define IntGetProcAddress(name) (*glXGetProcAddressARB)((const GLubyte*)name) - #endif - #endif -#endif int sfogl_ext_EXT_blend_minmax = sfogl_LOAD_FAILED; int sfogl_ext_EXT_blend_subtract = sfogl_LOAD_FAILED; diff --git a/src/SFML/Window/Context.cpp b/src/SFML/Window/Context.cpp index f45216d4..0ec500a7 100644 --- a/src/SFML/Window/Context.cpp +++ b/src/SFML/Window/Context.cpp @@ -53,6 +53,13 @@ bool Context::setActive(bool active) } +//////////////////////////////////////////////////////////// +GlFunctionPointer Context::getFunction(const char* name) +{ + return priv::GlContext::getFunction(name); +} + + //////////////////////////////////////////////////////////// Context::Context(const ContextSettings& settings, unsigned int width, unsigned int height) { diff --git a/src/SFML/Window/GlContext.cpp b/src/SFML/Window/GlContext.cpp index ab5b1ee5..f0d44edd 100644 --- a/src/SFML/Window/GlContext.cpp +++ b/src/SFML/Window/GlContext.cpp @@ -159,7 +159,7 @@ void GlContext::globalCleanup() sharedContext = NULL; // Destroy the internal contexts - sf::Lock lock(internalContextsMutex); + Lock internalContextsLock(internalContextsMutex); for (std::set::iterator it = internalContexts.begin(); it != internalContexts.end(); ++it) delete *it; internalContexts.clear(); @@ -213,6 +213,21 @@ GlContext* GlContext::create(const ContextSettings& settings, unsigned int width } +//////////////////////////////////////////////////////////// +GlFunctionPointer GlContext::getFunction(const char* name) +{ +#if !defined(SFML_OPENGL_ES) + + return ContextType::getFunction(name); + +#else + + return 0; + +#endif +} + + //////////////////////////////////////////////////////////// GlContext::~GlContext() { diff --git a/src/SFML/Window/GlContext.hpp b/src/SFML/Window/GlContext.hpp index 866ed684..8eec793f 100644 --- a/src/SFML/Window/GlContext.hpp +++ b/src/SFML/Window/GlContext.hpp @@ -29,6 +29,7 @@ // Headers //////////////////////////////////////////////////////////// #include +#include #include #include @@ -119,6 +120,15 @@ public: static GlContext* create(const ContextSettings& settings, unsigned int width, unsigned int height); public: + //////////////////////////////////////////////////////////// + /// \brief Get the address of an OpenGL function + /// + /// \param name Name of the function to get the address of + /// + /// \return Address of the OpenGL function, 0 on failure + /// + //////////////////////////////////////////////////////////// + static GlFunctionPointer getFunction(const char* name); //////////////////////////////////////////////////////////// /// \brief Destructor diff --git a/src/SFML/Window/OSX/SFContext.hpp b/src/SFML/Window/OSX/SFContext.hpp index b1a8096a..761c12e7 100644 --- a/src/SFML/Window/OSX/SFContext.hpp +++ b/src/SFML/Window/OSX/SFContext.hpp @@ -103,6 +103,16 @@ public: //////////////////////////////////////////////////////////// ~SFContext(); + //////////////////////////////////////////////////////////// + /// \brief Get the address of an OpenGL function + /// + /// \param name Name of the function to get the address of + /// + /// \return Address of the OpenGL function, 0 on failure + /// + //////////////////////////////////////////////////////////// + static GlFunctionPointer getFunction(const char* name); + //////////////////////////////////////////////////////////// /// \brief Display what has been rendered to the context so far /// diff --git a/src/SFML/Window/OSX/SFContext.mm b/src/SFML/Window/OSX/SFContext.mm index abbd4efe..810bcb48 100644 --- a/src/SFML/Window/OSX/SFContext.mm +++ b/src/SFML/Window/OSX/SFContext.mm @@ -29,6 +29,8 @@ #include #include #include +#include +#include #import @@ -111,6 +113,18 @@ SFContext::~SFContext() } +//////////////////////////////////////////////////////////// +GlFunctionPointer SFContext::getFunction(const char* name) +{ + static void* image = NULL; + + if (!image) + image = dlopen("/System/Library/Frameworks/OpenGL.framework/Versions/Current/OpenGL", RTLD_LAZY); + + return (image ? reinterpret_cast(reinterpret_cast(dlsym(image, name))) : 0); +} + + //////////////////////////////////////////////////////////// bool SFContext::makeCurrent() { diff --git a/src/SFML/Window/Unix/GlxContext.cpp b/src/SFML/Window/Unix/GlxContext.cpp index 906089b0..c92e8b6a 100644 --- a/src/SFML/Window/Unix/GlxContext.cpp +++ b/src/SFML/Window/Unix/GlxContext.cpp @@ -177,6 +177,13 @@ GlxContext::~GlxContext() } +//////////////////////////////////////////////////////////// +GlFunctionPointer GlxContext::getFunction(const char* name) +{ + return reinterpret_cast(glXGetProcAddressARB(reinterpret_cast(name))); +} + + //////////////////////////////////////////////////////////// bool GlxContext::makeCurrent() { diff --git a/src/SFML/Window/Unix/GlxContext.hpp b/src/SFML/Window/Unix/GlxContext.hpp index 62ddef0a..e1ad8998 100644 --- a/src/SFML/Window/Unix/GlxContext.hpp +++ b/src/SFML/Window/Unix/GlxContext.hpp @@ -81,6 +81,16 @@ public: //////////////////////////////////////////////////////////// ~GlxContext(); + //////////////////////////////////////////////////////////// + /// \brief Get the address of an OpenGL function + /// + /// \param name Name of the function to get the address of + /// + /// \return Address of the OpenGL function, 0 on failure + /// + //////////////////////////////////////////////////////////// + static GlFunctionPointer getFunction(const char* name); + //////////////////////////////////////////////////////////// /// \brief Activate the context as the current target for rendering /// diff --git a/src/SFML/Window/Unix/GlxExtensions.cpp b/src/SFML/Window/Unix/GlxExtensions.cpp index 7a258a59..a80c0edb 100644 --- a/src/SFML/Window/Unix/GlxExtensions.cpp +++ b/src/SFML/Window/Unix/GlxExtensions.cpp @@ -1,11 +1,17 @@ -#include -#include -#include + +//////////////////////////////////////////////////////////// +// Headers +//////////////////////////////////////////////////////////// #include +#include +#include +#include +#include -#include - -#define IntGetProcAddress(name) (*glXGetProcAddressARB)((const GLubyte*)name) +static sf::GlFunctionPointer IntGetProcAddress(const char* name) +{ + return sf::Context::getFunction(name); +} int sfglx_ext_EXT_swap_control = sfglx_LOAD_FAILED; int sfglx_ext_MESA_swap_control = sfglx_LOAD_FAILED; diff --git a/src/SFML/Window/Win32/WglContext.cpp b/src/SFML/Window/Win32/WglContext.cpp index bcdd9448..97ae3896 100644 --- a/src/SFML/Window/Win32/WglContext.cpp +++ b/src/SFML/Window/Win32/WglContext.cpp @@ -153,6 +153,32 @@ WglContext::~WglContext() } +//////////////////////////////////////////////////////////// +GlFunctionPointer WglContext::getFunction(const char* name) +{ + GlFunctionPointer address = reinterpret_cast(wglGetProcAddress(reinterpret_cast(name))); + + if (address) + { + // Test whether the returned value is a valid error code + ptrdiff_t errorCode = reinterpret_cast(address); + + if ((errorCode != -1) && (errorCode != 1) && (errorCode != 2) && (errorCode != 3)) + return address; + } + + static HMODULE module = NULL; + + if (!module) + module = GetModuleHandleA("OpenGL32.dll"); + + if (module) + return reinterpret_cast(GetProcAddress(module, reinterpret_cast(name))); + + return 0; +} + + //////////////////////////////////////////////////////////// bool WglContext::makeCurrent() { diff --git a/src/SFML/Window/Win32/WglContext.hpp b/src/SFML/Window/Win32/WglContext.hpp index a4e3f3bd..6aed1b5b 100644 --- a/src/SFML/Window/Win32/WglContext.hpp +++ b/src/SFML/Window/Win32/WglContext.hpp @@ -81,6 +81,16 @@ public: //////////////////////////////////////////////////////////// ~WglContext(); + //////////////////////////////////////////////////////////// + /// \brief Get the address of an OpenGL function + /// + /// \param name Name of the function to get the address of + /// + /// \return Address of the OpenGL function, 0 on failure + /// + //////////////////////////////////////////////////////////// + static GlFunctionPointer getFunction(const char* name); + //////////////////////////////////////////////////////////// /// \brief Activate the context as the current target for rendering /// diff --git a/src/SFML/Window/Win32/WglExtensions.cpp b/src/SFML/Window/Win32/WglExtensions.cpp index f2d2f557..f998a36c 100644 --- a/src/SFML/Window/Win32/WglExtensions.cpp +++ b/src/SFML/Window/Win32/WglExtensions.cpp @@ -1,29 +1,18 @@ -#include -#include -#include + +//////////////////////////////////////////////////////////// +// Headers +//////////////////////////////////////////////////////////// #include +#include +#include +#include +#include -#ifdef _MSC_VER -#pragma warning(disable: 4055) -#pragma warning(disable: 4054) -#pragma warning(disable: 4996) -#endif - -static PROC WinGetProcAddress(const char *name) +static sf::GlFunctionPointer IntGetProcAddress(const char* name) { - static HMODULE glMod = NULL; - PROC pFunc = wglGetProcAddress((LPCSTR)name); - - if (pFunc) return pFunc; - - if (NULL == glMod) - glMod = GetModuleHandleA("OpenGL32.dll"); - - return (PROC)GetProcAddress(glMod, (LPCSTR)name); + return sf::Context::getFunction(name); } -#define IntGetProcAddress(name) WinGetProcAddress(name) - int sfwgl_ext_EXT_swap_control = sfwgl_LOAD_FAILED; int sfwgl_ext_ARB_multisample = sfwgl_LOAD_FAILED; int sfwgl_ext_ARB_pixel_format = sfwgl_LOAD_FAILED;