From 34d383399c222af661c6d408f3c583e455c2d2f1 Mon Sep 17 00:00:00 2001 From: Camilla Berglund Date: Tue, 27 Nov 2012 15:02:26 +0100 Subject: [PATCH] Finished initial window/context backend split. --- CMakeLists.txt | 108 +++++++++++++++----------- src/CMakeLists.txt | 46 ++++++----- src/cocoa_platform.h | 36 ++------- src/config.h.in | 23 ++++-- src/egl_opengl.c | 2 +- src/egl_platform.h | 2 +- src/internal.h | 6 +- src/{cocoa_opengl.m => nsgl_opengl.m} | 0 src/nsgl_platform.h | 62 +++++++++++++++ src/win32_init.c | 5 ++ src/win32_platform.h | 4 +- src/x11_fullscreen.c | 2 +- src/x11_init.c | 4 +- src/x11_platform.h | 4 +- 14 files changed, 191 insertions(+), 113 deletions(-) rename src/{cocoa_opengl.m => nsgl_opengl.m} (100%) create mode 100644 src/nsgl_platform.h diff --git a/CMakeLists.txt b/CMakeLists.txt index b920fa30..5797c99f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -19,6 +19,10 @@ option(GLFW_USE_EGL "Build for EGL and OpenGL ES platform (Currently only X11)" if (GLFW_USE_EGL) set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/CMake/modules) find_package(EGL REQUIRED) + + set(GLFW_BUILD_EXAMPLES OFF) + set(GLFW_BUILD_TESTS OFF) + message(STATUS "NOTE: Examples and tests are disabled for EGL") else() find_package(OpenGL REQUIRED) endif() @@ -47,25 +51,39 @@ if (BUILD_SHARED_LIBS) endif() #-------------------------------------------------------------------- -# Detect and select target platform +# Detect and select target APIs #-------------------------------------------------------------------- if (WIN32) - set(_GLFW_WIN32_WGL 1) - message(STATUS "Building GLFW for WGL on a Win32 system") -elseif (UNIX AND APPLE) - set(_GLFW_COCOA_NSGL 1) - message(STATUS "Building GLFW for Cocoa and NSOpenGL on Mac OS X") -elseif (UNIX AND NOT APPLE) - set(_GLFW_X11 1) + set(_GLFW_WIN32 1) + message(STATUS "Using Win32 for window creation") + if (GLFW_USE_EGL) - set(_GLFW_X11_EGL 1) - set(GLFW_BUILD_EXAMPLES 0) - set(GLFW_BUILD_TESTS 0) - message(STATUS "Building GLFW for X11 and EGL on a Unix-like system") - message(STATUS "NOTE: Examples and tests are disabled for EGL") + set(_GLFW_EGL 1) + message(STATUS "Using EGL for context creation") else() - set(_GLFW_X11_GLX 1) - message(STATUS "Building GLFW for X11 and GLX on a Unix-like system") + set(_GLFW_WGL 1) + message(STATUS "Using WGL for context creation") + endif() +elseif (APPLE) + set(_GLFW_COCOA 1) + message(STATUS "Using Cocoa for window creation") + + if (GLFW_USE_EGL) + message(FATAL_ERROR "EGL not supported on Mac OS X") + else() + set(_GLFW_NSGL 1) + message(STATUS "Using NSGL for context creation") + endif() +elseif (UNIX) + set(_GLFW_X11 1) + message(STATUS "Using X11 for window creation") + + if (GLFW_USE_EGL) + set(_GLFW_EGL 1) + message(STATUS "Using EGL for context creation") + else() + set(_GLFW_GLX 1) + message(STATUS "Using GLX for context creation") endif() else() message(FATAL_ERROR "No supported platform was detected") @@ -74,7 +92,7 @@ endif() #-------------------------------------------------------------------- # Set up GLFW for Win32 and WGL on Windows #-------------------------------------------------------------------- -if (_GLFW_WIN32_WGL) +if (_GLFW_WIN32) # Set up library and include paths list(APPEND glfw_INCLUDE_DIRS ${OPENGL_INCLUDE_DIR}) @@ -160,16 +178,12 @@ if (_GLFW_X11) set(GLFW_PKG_LIBS "${GLFW_PKG_LIBS} -lm") endif() - if (${CMAKE_SYSTEM_NAME} STREQUAL "Linux") - set(_GLFW_USE_LINUX_JOYSTICKS 1) - endif() - endif() #-------------------------------------------------------------------- # GLX Context #-------------------------------------------------------------------- -if (_GLFW_X11_GLX) +if (_GLFW_GLX) # Set up library and include paths list(APPEND glfw_INCLUDE_DIRS ${OPENGL_INCLUDE_DIR}) @@ -223,42 +237,44 @@ endif() #-------------------------------------------------------------------- # EGL Context #-------------------------------------------------------------------- -if (_GLFW_X11_EGL) +if (_GLFW_EGL) # Set up library and include paths list(APPEND glfw_INCLUDE_DIRS ${EGL_INCLUDE_DIR}) list(APPEND glfw_LIBRARIES ${EGL_LIBRARY}) - set(GLFW_PKG_DEPS "${GLFW_PKG_DEPS} egl") - - include(CheckFunctionExists) - set(CMAKE_REQUIRED_LIBRARIES ${EGL_LIBRARY}) - check_function_exists(eglGetProcAddress _GLFW_HAS_EGLGETPROCADDRESS) + if (_GLFW_X11) + set(GLFW_PKG_DEPS "${GLFW_PKG_DEPS} egl") - if (NOT _GLFW_HAS_EGLGETPROCADDRESS) - message(WARNING "No eglGetProcAddress found") + include(CheckFunctionExists) - # Check for dlopen support as a fallback + check_function_exists(eglGetProcAddress _GLFW_HAS_EGLGETPROCADDRESS) - find_library(DL_LIBRARY dl) - mark_as_advanced(DL_LIBRARY) - if (DL_LIBRARY) - set(CMAKE_REQUIRED_LIBRARIES ${DL_LIBRARY}) - else() - set(CMAKE_REQUIRED_LIBRARIES "") - endif() + if (NOT _GLFW_HAS_EGLGETPROCADDRESS) + message(WARNING "No eglGetProcAddress found") - check_function_exists(dlopen _GLFW_HAS_DLOPEN) + # Check for dlopen support as a fallback - if (NOT _GLFW_HAS_DLOPEN) - message(FATAL_ERROR "No entry point retrieval mechanism found") - endif() + find_library(DL_LIBRARY dl) + mark_as_advanced(DL_LIBRARY) + if (DL_LIBRARY) + set(CMAKE_REQUIRED_LIBRARIES ${DL_LIBRARY}) + else() + set(CMAKE_REQUIRED_LIBRARIES "") + endif() - if (DL_LIBRARY) - list(APPEND glfw_LIBRARIES ${DL_LIBRARY}) - set(GLFW_PKG_LIBS "${GLFW_PKG_LIBS} -ldl") + check_function_exists(dlopen _GLFW_HAS_DLOPEN) + + if (NOT _GLFW_HAS_DLOPEN) + message(FATAL_ERROR "No entry point retrieval mechanism found") + endif() + + if (DL_LIBRARY) + list(APPEND glfw_LIBRARIES ${DL_LIBRARY}) + set(GLFW_PKG_LIBS "${GLFW_PKG_LIBS} -ldl") + endif() endif() endif() @@ -267,7 +283,7 @@ endif() #-------------------------------------------------------------------- # Set up GLFW for Cocoa and NSOpenGL on Mac OS X #-------------------------------------------------------------------- -if (_GLFW_COCOA_NSGL) +if (_GLFW_COCOA AND _GLFW_NSGL) option(GLFW_BUILD_UNIVERSAL "Build GLFW as a Universal Binary" OFF) @@ -349,7 +365,7 @@ install(FILES COPYING.txt readme.html #-------------------------------------------------------------------- # Create and install pkg-config file on supported platforms #-------------------------------------------------------------------- -if (_GLFW_X11_GLX OR _GLFW_COCOA_NSGL) +if (UNIX) configure_file(${GLFW_SOURCE_DIR}/src/glfw3.pc.in ${GLFW_BINARY_DIR}/src/glfw3.pc @ONLY) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 485f5c13..08602a9d 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -10,11 +10,11 @@ set(common_HEADERS ${GLFW_SOURCE_DIR}/include/GL/glfw3.h internal.h) set(common_SOURCES clipboard.c fullscreen.c gamma.c init.c input.c joystick.c opengl.c time.c window.c) -if (_GLFW_COCOA_NSGL) +if (_GLFW_COCOA) set(glfw_HEADERS ${common_HEADERS} cocoa_platform.h) set(glfw_SOURCES ${common_SOURCES} cocoa_clipboard.m cocoa_fullscreen.m - cocoa_gamma.c cocoa_init.m cocoa_joystick.m - cocoa_opengl.m cocoa_time.c cocoa_window.m) + cocoa_gamma.c cocoa_init.m cocoa_joystick.m cocoa_time.c + cocoa_window.m) if (GLFW_NATIVE_API) list(APPEND glfw_SOURCES cocoa_native.m) @@ -22,30 +22,38 @@ if (_GLFW_COCOA_NSGL) # For some reason, CMake doesn't know about .m set_source_files_properties(${glfw_SOURCES} PROPERTIES LANGUAGE C) -elseif (_GLFW_WIN32_WGL) +elseif (_GLFW_WIN32) set(glfw_HEADERS ${common_HEADERS} win32_platform.h) - set(glfw_SOURCES ${common_SOURCES} wgl_opengl.c win32_clipboard.c - win32_fullscreen.c win32_gamma.c win32_init.c - win32_joystick.c win32_time.c win32_window.c) + set(glfw_SOURCES ${common_SOURCES} win32_clipboard.c win32_fullscreen.c + win32_gamma.c win32_init.c win32_joystick.c win32_time.c + win32_window.c) if (GLFW_NATIVE_API) list(APPEND glfw_SOURCES win32_native.c) endif() -elseif (_GLFW_X11_GLX) +elseif (_GLFW_X11) set(glfw_HEADERS ${common_HEADERS} x11_platform.h) - set(glfw_SOURCES ${common_SOURCES} glx_opengl.c x11_clipboard.c - x11_fullscreen.c x11_gamma.c x11_init.c x11_joystick.c - x11_keysym2unicode.c x11_time.c x11_window.c) + set(glfw_SOURCES ${common_SOURCES} x11_clipboard.c x11_fullscreen.c + x11_gamma.c x11_init.c x11_joystick.c x11_keysym2unicode.c + x11_time.c x11_window.c) if (GLFW_NATIVE_API) list(APPEND glfw_SOURCES x11_native.c) endif() -elseif (_GLFW_X11_EGL) - set(glfw_HEADERS ${common_HEADERS} x11_platform.h egl_platform.h) - set(glfw_SOURCES ${common_SOURCES} egl_opengl.c x11_clipboard.c - x11_fullscreen.c x11_gamma.c x11_init.c - x11_joystick.c x11_keysym2unicode.c x11_time.c - x11_window.c) +endif() + +if (_GLFW_EGL) + list(APPEND glfw_HEADERS ${common_HEADERS} egl_platform.h) + list(APPEND glfw_SOURCES ${common_SOURCES} egl_opengl.c) +elseif (_GLFW_NSGL) + list(APPEND glfw_HEADERS ${common_HEADERS} nsgl_platform.h) + list(APPEND glfw_SOURCES ${common_SOURCES} nsgl_opengl.c) +elseif (_GLFW_WGL) + list(APPEND glfw_HEADERS ${common_HEADERS} wgl_platform.h) + list(APPEND glfw_SOURCES ${common_SOURCES} wgl_opengl.c) +elseif (_GLFW_X11) + list(APPEND glfw_HEADERS ${common_HEADERS} glx_platform.h) + list(APPEND glfw_SOURCES ${common_SOURCES} glx_opengl.c) endif() add_library(glfw ${glfw_SOURCES} ${glfw_HEADERS}) @@ -58,7 +66,7 @@ if (BUILD_SHARED_LIBS) set_target_properties(glfw PROPERTIES SOVERSION ${GLFW_VERSION_MAJOR}) endif() - if (_GLFW_WIN32_WGL) + if (_GLFW_WIN32) # The GLFW DLL needs a special compile-time macro and import library name set_target_properties(glfw PROPERTIES PREFIX "" IMPORT_PREFIX "") @@ -67,7 +75,7 @@ if (BUILD_SHARED_LIBS) else() set_target_properties(glfw PROPERTIES IMPORT_SUFFIX "dll.lib") endif() - elseif (_GLFW_COCOA_NSGL) + elseif (_GLFW_COCOA) # Append -fno-common to the compile flags to work around a bug in the Apple GCC get_target_property(glfw_CFLAGS glfw COMPILE_FLAGS) if (NOT glfw_CFLAGS) diff --git a/src/cocoa_platform.h b/src/cocoa_platform.h index 46957e53..57a27487 100644 --- a/src/cocoa_platform.h +++ b/src/cocoa_platform.h @@ -1,6 +1,6 @@ //======================================================================== // GLFW - An OpenGL library -// Platform: Cocoa/NSOpenGL +// Platform: Cocoa // API Version: 3.0 // WWW: http://www.glfw.org/ //------------------------------------------------------------------------ @@ -27,13 +27,12 @@ // //======================================================================== -#ifndef _platform_h_ -#define _platform_h_ +#ifndef _cocoa_platform_h_ +#define _cocoa_platform_h_ #include - #if defined(__OBJC__) #import #else @@ -41,12 +40,13 @@ typedef void* id; #endif +#if defined(_GLFW_NSGL) + #include "nsgl_platform.h" +#endif + #define _GLFW_PLATFORM_WINDOW_STATE _GLFWwindowNS NS #define _GLFW_PLATFORM_LIBRARY_WINDOW_STATE _GLFWlibraryNS NS -#define _GLFW_PLATFORM_CONTEXT_STATE _GLFWcontextNSGL NSGL -#define _GLFW_PLATFORM_LIBRARY_OPENGL_STATE _GLFWlibraryNSGL NSGL - //======================================================================== // GLFW platform specific types @@ -58,16 +58,6 @@ typedef void* id; typedef intptr_t GLFWintptr; -//------------------------------------------------------------------------ -// Platform-specific OpenGL context structure -//------------------------------------------------------------------------ -typedef struct _GLFWcontextNSGL -{ - id pixelFormat; - id context; -} _GLFWcontextNSGL; - - //------------------------------------------------------------------------ // Platform-specific window structure //------------------------------------------------------------------------ @@ -99,16 +89,6 @@ typedef struct _GLFWlibraryNS } _GLFWlibraryNS; -//------------------------------------------------------------------------ -// Platform-specific library global data for NSGL -//------------------------------------------------------------------------ -typedef struct _GLFWlibraryNSGL -{ - // dlopen handle for dynamically loading OpenGL extension entry points - void* framework; -} _GLFWlibraryNSGL; - - //======================================================================== // Prototypes for platform specific internal functions //======================================================================== @@ -128,4 +108,4 @@ void _glfwRestoreVideoMode(void); int _glfwInitOpenGL(void); void _glfwTerminateOpenGL(void); -#endif // _platform_h_ +#endif // _cocoa_platform_h_ diff --git a/src/config.h.in b/src/config.h.in index f725f89f..d391fcdf 100644 --- a/src/config.h.in +++ b/src/config.h.in @@ -35,14 +35,21 @@ // it. Instead, you should modify the config.h.in file. //======================================================================== -// Define this to 1 if building GLFW for X11/GLX -#cmakedefine _GLFW_X11_GLX -// Define this to 1 if building GLFW for X11/EGL -#cmakedefine _GLFW_X11_EGL -// Define this to 1 if building GLFW for Win32/WGL -#cmakedefine _GLFW_WIN32_WGL -// Define this to 1 if building GLFW for Cocoa/NSOpenGL -#cmakedefine _GLFW_COCOA_NSGL +// Define this to 1 if building GLFW for X11 +#cmakedefine _GLFW_X11 +// Define this to 1 if building GLFW for Win32 +#cmakedefine _GLFW_WIN32 +// Define this to 1 if building GLFW for Cocoa +#cmakedefine _GLFW_COCOA + +// Define this to 1 if building GLFW for EGL +#cmakedefine _GLFW_EGL +// Define this to 1 if building GLFW for GLX +#cmakedefine _GLFW_GLX +// Define this to 1 if building GLFW for WGL +#cmakedefine _GLFW_WGL +// Define this to 1 if building GLFW for NSGL +#cmakedefine _GLFW_NSGL // Define this to 1 if building as a shared library / dynamic library / DLL #cmakedefine _GLFW_BUILD_DLL diff --git a/src/egl_opengl.c b/src/egl_opengl.c index 0f10745d..cf8ad77a 100644 --- a/src/egl_opengl.c +++ b/src/egl_opengl.c @@ -208,7 +208,7 @@ static int createContext(_GLFWwindow* window, // Retrieve the corresponding visual // NOTE: This is the only non-portable code in this file. // Maybe it would not hurt too much to add #ifdefs for different platforms? -#if defined(_GLFW_X11_EGL) +#if defined(_GLFW_X11) { int mask; EGLint redBits, greenBits, blueBits, alphaBits, visualID = 0; diff --git a/src/egl_platform.h b/src/egl_platform.h index cb9114dd..e416871c 100644 --- a/src/egl_platform.h +++ b/src/egl_platform.h @@ -71,7 +71,7 @@ typedef struct _GLFWcontextEGL EGLContext context; EGLSurface surface; -#if defined(_GLFW_X11_EGL) +#if defined(_GLFW_X11) XVisualInfo* visual; #endif } _GLFWcontextEGL; diff --git a/src/internal.h b/src/internal.h index 436a63ad..36b586da 100644 --- a/src/internal.h +++ b/src/internal.h @@ -65,11 +65,11 @@ typedef struct _GLFWlibrary _GLFWlibrary; // extensions and not all operating systems come with an up-to-date version #include "../support/GL/glext.h" -#if defined(_GLFW_COCOA_NSGL) +#if defined(_GLFW_COCOA) #include "cocoa_platform.h" -#elif defined(_GLFW_WIN32_WGL) +#elif defined(_GLFW_WIN32) #include "win32_platform.h" -#elif defined(_GLFW_X11_GLX) || defined(_GLFW_X11_EGL) +#elif defined(_GLFW_X11) #include "x11_platform.h" #else #error "No supported platform selected" diff --git a/src/cocoa_opengl.m b/src/nsgl_opengl.m similarity index 100% rename from src/cocoa_opengl.m rename to src/nsgl_opengl.m diff --git a/src/nsgl_platform.h b/src/nsgl_platform.h new file mode 100644 index 00000000..34ea0fb6 --- /dev/null +++ b/src/nsgl_platform.h @@ -0,0 +1,62 @@ +//======================================================================== +// GLFW - An OpenGL library +// Platform: NSOpenGL +// API Version: 3.0 +// WWW: http://www.glfw.org/ +//------------------------------------------------------------------------ +// Copyright (c) 2009-2010 Camilla Berglund +// +// This software is provided 'as-is', without any express or implied +// warranty. In no event will the authors be held liable for any damages +// arising from the use of this software. +// +// Permission is granted to anyone to use this software for any purpose, +// including commercial applications, and to alter it and redistribute it +// freely, subject to the following restrictions: +// +// 1. The origin of this software must not be misrepresented; you must not +// claim that you wrote the original software. If you use this software +// in a product, an acknowledgment in the product documentation would +// be appreciated but is not required. +// +// 2. Altered source versions must be plainly marked as such, and must not +// be misrepresented as being the original software. +// +// 3. This notice may not be removed or altered from any source +// distribution. +// +//======================================================================== + +#ifndef _nsgl_platform_h_ +#define _nsgl_platform_h_ + + +#define _GLFW_PLATFORM_CONTEXT_STATE _GLFWcontextNSGL NSGL +#define _GLFW_PLATFORM_LIBRARY_OPENGL_STATE _GLFWlibraryNSGL NSGL + + +//======================================================================== +// GLFW platform specific types +//======================================================================== + +//------------------------------------------------------------------------ +// Platform-specific OpenGL context structure +//------------------------------------------------------------------------ +typedef struct _GLFWcontextNSGL +{ + id pixelFormat; + id context; +} _GLFWcontextNSGL; + + +//------------------------------------------------------------------------ +// Platform-specific library global data for NSGL +//------------------------------------------------------------------------ +typedef struct _GLFWlibraryNSGL +{ + // dlopen handle for dynamically loading OpenGL extension entry points + void* framework; +} _GLFWlibraryNSGL; + + +#endif // _nsgl_platform_h_ diff --git a/src/win32_init.c b/src/win32_init.c index 21de415b..62d6c94b 100644 --- a/src/win32_init.c +++ b/src/win32_init.c @@ -232,6 +232,11 @@ int _glfwPlatformTerminate(void) const char* _glfwPlatformGetVersionString(void) { const char* version = _GLFW_VERSION_FULL +#if defined(_GLFW_WGL) + " WGL" +#elif defined(_GLFW_EGL) + " EGL" +#endif #if defined(__MINGW32__) " MinGW" #elif defined(_MSC_VER) diff --git a/src/win32_platform.h b/src/win32_platform.h index 20126177..758ada2c 100644 --- a/src/win32_platform.h +++ b/src/win32_platform.h @@ -105,9 +105,9 @@ typedef DWORD (WINAPI * TIMEGETTIME_T) (void); #define _GLFW_WNDCLASSNAME L"GLFW30" -#if defined(_GLFW_WIN32_WGL) +#if defined(_GLFW_WGL) #include "wgl_platform.h" -#elif defined(_GLFW_WIN32_EGL) +#elif defined(_GLFW_EGL) #define _GLFW_EGL_NATIVE_WINDOW window->Win32.handle #define _GLFW_EGL_NATIVE_DISPLAY NULL #include "egl_platform.h" diff --git a/src/x11_fullscreen.c b/src/x11_fullscreen.c index 06b161e4..9b1121fc 100644 --- a/src/x11_fullscreen.c +++ b/src/x11_fullscreen.c @@ -449,7 +449,7 @@ GLFWvidmode* _glfwPlatformGetVideoModes(int* found) rgbs = (int*) malloc(sizeof(int) * visualCount); rgbCount = 0; -#if !defined(_GLFW_X11_EGL) +#if defined(_GLFW_GLX) for (i = 0; i < visualCount; i++) { int gl, rgba, rgb, r, g, b; diff --git a/src/x11_init.c b/src/x11_init.c index b76d307e..b5631fba 100644 --- a/src/x11_init.c +++ b/src/x11_init.c @@ -693,9 +693,9 @@ int _glfwPlatformTerminate(void) const char* _glfwPlatformGetVersionString(void) { const char* version = _GLFW_VERSION_FULL -#if defined(_GLFW_X11_GLX) +#if defined(_GLFW_GLX) " GLX" -#elif defined(_GLFW_X11_EGL) +#elif defined(_GLFW_EGL) " EGL" #endif #if defined(_GLFW_HAS_XRANDR) diff --git a/src/x11_platform.h b/src/x11_platform.h index 7c4ad4ce..8c4740a0 100644 --- a/src/x11_platform.h +++ b/src/x11_platform.h @@ -53,10 +53,10 @@ #include #endif -#if defined(_GLFW_X11_GLX) +#if defined(_GLFW_GLX) #define _GLFW_X11_CONTEXT_VISUAL window->GLX.visual #include "glx_platform.h" -#elif defined(_GLFW_X11_EGL) +#elif defined(_GLFW_EGL) #define _GLFW_X11_CONTEXT_VISUAL window->EGL.visual #define _GLFW_EGL_NATIVE_WINDOW window->X11.handle #define _GLFW_EGL_NATIVE_DISPLAY _glfwLibrary.X11.display