├── examples ├── glfw.rc ├── glfw.icns ├── glfw.ico ├── windows.c ├── CMakeLists.txt ├── offscreen.c ├── triangle-opengl.c ├── triangle-opengles.c └── sharing.c ├── CMake ├── glfw3Config.cmake.in ├── glfw3.pc.in ├── modules │ ├── FindOSMesa.cmake │ └── FindEpollShim.cmake ├── i686-w64-mingw32.cmake ├── i686-w64-mingw32-clang.cmake ├── x86_64-w64-mingw32.cmake ├── x86_64-w64-mingw32-clang.cmake ├── cmake_uninstall.cmake.in ├── Info.plist.in └── GenerateMappings.cmake ├── docs ├── footer.html ├── SUPPORT.md ├── header.html ├── CMakeLists.txt ├── main.dox ├── DoxygenLayout.xml ├── extra.css.map ├── internal.dox └── extra.css ├── .github ├── CODEOWNERS └── workflows │ └── build.yml ├── .mailmap ├── src ├── glfw.rc.in ├── xkb_unicode.h ├── cocoa_time.h ├── null_joystick.h ├── posix_poll.h ├── win32_time.h ├── posix_time.h ├── posix_thread.h ├── win32_thread.h ├── cocoa_joystick.h ├── win32_joystick.h ├── null_joystick.c ├── win32_module.c ├── posix_module.c ├── cocoa_time.c ├── win32_time.c ├── linux_joystick.h ├── posix_time.c ├── posix_poll.c ├── win32_thread.c ├── posix_thread.c ├── mappings.h.in ├── null_monitor.c ├── platform.c ├── platform.h └── win32_polyfill.c ├── LICENSE.md ├── deps ├── getopt.h └── mingw │ └── _mingw_dxhelper.h ├── tests ├── title.c ├── timeout.c ├── empty.c ├── CMakeLists.txt ├── clipboard.c ├── icon.c ├── allocator.c ├── threads.c ├── gamma.c ├── msaa.c └── reopen.c ├── CONTRIBUTORS.md └── CMakeLists.txt /examples/glfw.rc: -------------------------------------------------------------------------------- 1 | 2 | GLFW_ICON ICON "glfw.ico" 3 | 4 | -------------------------------------------------------------------------------- /examples/glfw.icns: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rndtrash/glfw/master/examples/glfw.icns -------------------------------------------------------------------------------- /examples/glfw.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rndtrash/glfw/master/examples/glfw.ico -------------------------------------------------------------------------------- /CMake/glfw3Config.cmake.in: -------------------------------------------------------------------------------- 1 | include(CMakeFindDependencyMacro) 2 | find_dependency(Threads) 3 | include("${CMAKE_CURRENT_LIST_DIR}/glfw3Targets.cmake") 4 | -------------------------------------------------------------------------------- /docs/footer.html: -------------------------------------------------------------------------------- 1 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /.github/CODEOWNERS: -------------------------------------------------------------------------------- 1 | 2 | * @elmindreda 3 | 4 | src/wl_* @linkmauve 5 | 6 | docs/*.css @glfw/webdev 7 | docs/*.scss @glfw/webdev 8 | docs/*.html @glfw/webdev 9 | docs/*.xml @glfw/webdev 10 | 11 | -------------------------------------------------------------------------------- /.mailmap: -------------------------------------------------------------------------------- 1 | Camilla Löwy 2 | Camilla Löwy 3 | Camilla Löwy 4 | 5 | Emmanuel Gil Peyrot 6 | 7 | Marcus Geelnard 8 | Marcus Geelnard 9 | Marcus Geelnard 10 | 11 | -------------------------------------------------------------------------------- /CMake/glfw3.pc.in: -------------------------------------------------------------------------------- 1 | prefix=@CMAKE_INSTALL_PREFIX@ 2 | exec_prefix=${prefix} 3 | includedir=@CMAKE_INSTALL_FULL_INCLUDEDIR@ 4 | libdir=@CMAKE_INSTALL_FULL_LIBDIR@ 5 | 6 | Name: GLFW 7 | Description: A multi-platform library for OpenGL, window and input 8 | Version: @GLFW_VERSION@ 9 | URL: https://www.glfw.org/ 10 | Requires.private: @GLFW_PKG_CONFIG_REQUIRES_PRIVATE@ 11 | Libs: -L${libdir} -l@GLFW_LIB_NAME@ 12 | Libs.private: @GLFW_PKG_CONFIG_LIBS_PRIVATE@ 13 | Cflags: -I${includedir} 14 | -------------------------------------------------------------------------------- /CMake/modules/FindOSMesa.cmake: -------------------------------------------------------------------------------- 1 | # Try to find OSMesa on a Unix system 2 | # 3 | # This will define: 4 | # 5 | # OSMESA_LIBRARIES - Link these to use OSMesa 6 | # OSMESA_INCLUDE_DIR - Include directory for OSMesa 7 | # 8 | # Copyright (c) 2014 Brandon Schaefer 9 | 10 | if (NOT WIN32) 11 | 12 | find_package (PkgConfig) 13 | pkg_check_modules (PKG_OSMESA QUIET osmesa) 14 | 15 | set (OSMESA_INCLUDE_DIR ${PKG_OSMESA_INCLUDE_DIRS}) 16 | set (OSMESA_LIBRARIES ${PKG_OSMESA_LIBRARIES}) 17 | 18 | endif () 19 | -------------------------------------------------------------------------------- /docs/SUPPORT.md: -------------------------------------------------------------------------------- 1 | # Support resources 2 | 3 | See the [latest documentation](https://www.glfw.org/docs/latest/) for tutorials, 4 | guides and the API reference. 5 | 6 | If you have questions about using GLFW, we have a 7 | [forum](https://discourse.glfw.org/), and the `#glfw` IRC channel on 8 | [Libera.Chat](https://libera.chat/). 9 | 10 | Bugs are reported to our [issue tracker](https://github.com/glfw/glfw/issues). 11 | Please check the [contribution 12 | guide](https://github.com/glfw/glfw/blob/master/docs/CONTRIBUTING.md) for 13 | information on what to include when reporting a bug. 14 | 15 | -------------------------------------------------------------------------------- /CMake/i686-w64-mingw32.cmake: -------------------------------------------------------------------------------- 1 | # Define the environment for cross-compiling with 32-bit MinGW-w64 GCC 2 | SET(CMAKE_SYSTEM_NAME Windows) # Target system name 3 | SET(CMAKE_SYSTEM_VERSION 1) 4 | SET(CMAKE_C_COMPILER "i686-w64-mingw32-gcc") 5 | SET(CMAKE_CXX_COMPILER "i686-w64-mingw32-g++") 6 | SET(CMAKE_RC_COMPILER "i686-w64-mingw32-windres") 7 | SET(CMAKE_RANLIB "i686-w64-mingw32-ranlib") 8 | 9 | # Configure the behaviour of the find commands 10 | SET(CMAKE_FIND_ROOT_PATH "/usr/i686-w64-mingw32") 11 | SET(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER) 12 | SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY) 13 | SET(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY) 14 | -------------------------------------------------------------------------------- /CMake/i686-w64-mingw32-clang.cmake: -------------------------------------------------------------------------------- 1 | # Define the environment for cross-compiling with 32-bit MinGW-w64 Clang 2 | SET(CMAKE_SYSTEM_NAME Windows) # Target system name 3 | SET(CMAKE_SYSTEM_VERSION 1) 4 | SET(CMAKE_C_COMPILER "i686-w64-mingw32-clang") 5 | SET(CMAKE_CXX_COMPILER "i686-w64-mingw32-clang++") 6 | SET(CMAKE_RC_COMPILER "i686-w64-mingw32-windres") 7 | SET(CMAKE_RANLIB "i686-w64-mingw32-ranlib") 8 | 9 | # Configure the behaviour of the find commands 10 | SET(CMAKE_FIND_ROOT_PATH "/usr/i686-w64-mingw32") 11 | SET(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER) 12 | SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY) 13 | SET(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY) 14 | -------------------------------------------------------------------------------- /CMake/x86_64-w64-mingw32.cmake: -------------------------------------------------------------------------------- 1 | # Define the environment for cross-compiling with 64-bit MinGW-w64 GCC 2 | SET(CMAKE_SYSTEM_NAME Windows) # Target system name 3 | SET(CMAKE_SYSTEM_VERSION 1) 4 | SET(CMAKE_C_COMPILER "x86_64-w64-mingw32-gcc") 5 | SET(CMAKE_CXX_COMPILER "x86_64-w64-mingw32-g++") 6 | SET(CMAKE_RC_COMPILER "x86_64-w64-mingw32-windres") 7 | SET(CMAKE_RANLIB "x86_64-w64-mingw32-ranlib") 8 | 9 | # Configure the behaviour of the find commands 10 | SET(CMAKE_FIND_ROOT_PATH "/usr/x86_64-w64-mingw32") 11 | SET(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER) 12 | SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY) 13 | SET(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY) 14 | -------------------------------------------------------------------------------- /CMake/x86_64-w64-mingw32-clang.cmake: -------------------------------------------------------------------------------- 1 | # Define the environment for cross-compiling with 64-bit MinGW-w64 Clang 2 | SET(CMAKE_SYSTEM_NAME Windows) # Target system name 3 | SET(CMAKE_SYSTEM_VERSION 1) 4 | SET(CMAKE_C_COMPILER "x86_64-w64-mingw32-clang") 5 | SET(CMAKE_CXX_COMPILER "x86_64-w64-mingw32-clang++") 6 | SET(CMAKE_RC_COMPILER "x86_64-w64-mingw32-windres") 7 | SET(CMAKE_RANLIB "x86_64-w64-mingw32-ranlib") 8 | 9 | # Configure the behaviour of the find commands 10 | SET(CMAKE_FIND_ROOT_PATH "/usr/x86_64-w64-mingw32") 11 | SET(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER) 12 | SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY) 13 | SET(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY) 14 | -------------------------------------------------------------------------------- /CMake/modules/FindEpollShim.cmake: -------------------------------------------------------------------------------- 1 | # Find EpollShim 2 | # Once done, this will define 3 | # 4 | # EPOLLSHIM_FOUND - System has EpollShim 5 | # EPOLLSHIM_INCLUDE_DIRS - The EpollShim include directories 6 | # EPOLLSHIM_LIBRARIES - The libraries needed to use EpollShim 7 | 8 | find_path(EPOLLSHIM_INCLUDE_DIRS NAMES sys/epoll.h sys/timerfd.h HINTS /usr/local/include/libepoll-shim) 9 | find_library(EPOLLSHIM_LIBRARIES NAMES epoll-shim libepoll-shim HINTS /usr/local/lib) 10 | 11 | if (EPOLLSHIM_INCLUDE_DIRS AND EPOLLSHIM_LIBRARIES) 12 | set(EPOLLSHIM_FOUND TRUE) 13 | endif (EPOLLSHIM_INCLUDE_DIRS AND EPOLLSHIM_LIBRARIES) 14 | 15 | include(FindPackageHandleStandardArgs) 16 | find_package_handle_standard_args(EpollShim DEFAULT_MSG EPOLLSHIM_LIBRARIES EPOLLSHIM_INCLUDE_DIRS) 17 | mark_as_advanced(EPOLLSHIM_INCLUDE_DIRS EPOLLSHIM_LIBRARIES) 18 | -------------------------------------------------------------------------------- /src/glfw.rc.in: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | 4 | VS_VERSION_INFO VERSIONINFO 5 | FILEVERSION @GLFW_VERSION_MAJOR@,@GLFW_VERSION_MINOR@,@GLFW_VERSION_PATCH@,0 6 | PRODUCTVERSION @GLFW_VERSION_MAJOR@,@GLFW_VERSION_MINOR@,@GLFW_VERSION_PATCH@,0 7 | FILEFLAGSMASK VS_FFI_FILEFLAGSMASK 8 | FILEFLAGS 0 9 | FILEOS VOS_NT_WINDOWS32 10 | FILETYPE VFT_DLL 11 | FILESUBTYPE 0 12 | { 13 | BLOCK "StringFileInfo" 14 | { 15 | BLOCK "040904B0" 16 | { 17 | VALUE "CompanyName", "GLFW" 18 | VALUE "FileDescription", "GLFW @GLFW_VERSION@ DLL" 19 | VALUE "FileVersion", "@GLFW_VERSION@" 20 | VALUE "OriginalFilename", "glfw3.dll" 21 | VALUE "ProductName", "GLFW" 22 | VALUE "ProductVersion", "@GLFW_VERSION@" 23 | } 24 | } 25 | BLOCK "VarFileInfo" 26 | { 27 | VALUE "Translation", 0x409, 1200 28 | } 29 | } 30 | 31 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | Copyright (c) 2002-2006 Marcus Geelnard 2 | 3 | Copyright (c) 2006-2019 Camilla Löwy 4 | 5 | This software is provided 'as-is', without any express or implied 6 | warranty. In no event will the authors be held liable for any damages 7 | arising from the use of this software. 8 | 9 | Permission is granted to anyone to use this software for any purpose, 10 | including commercial applications, and to alter it and redistribute it 11 | freely, subject to the following restrictions: 12 | 13 | 1. The origin of this software must not be misrepresented; you must not 14 | claim that you wrote the original software. If you use this software 15 | in a product, an acknowledgment in the product documentation would 16 | be appreciated but is not required. 17 | 18 | 2. Altered source versions must be plainly marked as such, and must not 19 | be misrepresented as being the original software. 20 | 21 | 3. This notice may not be removed or altered from any source 22 | distribution. 23 | 24 | -------------------------------------------------------------------------------- /CMake/cmake_uninstall.cmake.in: -------------------------------------------------------------------------------- 1 | 2 | if (NOT EXISTS "@GLFW_BINARY_DIR@/install_manifest.txt") 3 | message(FATAL_ERROR "Cannot find install manifest: \"@GLFW_BINARY_DIR@/install_manifest.txt\"") 4 | endif() 5 | 6 | file(READ "@GLFW_BINARY_DIR@/install_manifest.txt" files) 7 | string(REGEX REPLACE "\n" ";" files "${files}") 8 | 9 | foreach (file ${files}) 10 | message(STATUS "Uninstalling \"$ENV{DESTDIR}${file}\"") 11 | if (EXISTS "$ENV{DESTDIR}${file}") 12 | exec_program("@CMAKE_COMMAND@" ARGS "-E remove \"$ENV{DESTDIR}${file}\"" 13 | OUTPUT_VARIABLE rm_out 14 | RETURN_VALUE rm_retval) 15 | if (NOT "${rm_retval}" STREQUAL 0) 16 | MESSAGE(FATAL_ERROR "Problem when removing \"$ENV{DESTDIR}${file}\"") 17 | endif() 18 | elseif (IS_SYMLINK "$ENV{DESTDIR}${file}") 19 | EXEC_PROGRAM("@CMAKE_COMMAND@" ARGS "-E remove \"$ENV{DESTDIR}${file}\"" 20 | OUTPUT_VARIABLE rm_out 21 | RETURN_VALUE rm_retval) 22 | if (NOT "${rm_retval}" STREQUAL 0) 23 | message(FATAL_ERROR "Problem when removing symlink \"$ENV{DESTDIR}${file}\"") 24 | endif() 25 | else() 26 | message(STATUS "File \"$ENV{DESTDIR}${file}\" does not exist.") 27 | endif() 28 | endforeach() 29 | 30 | -------------------------------------------------------------------------------- /src/xkb_unicode.h: -------------------------------------------------------------------------------- 1 | //======================================================================== 2 | // GLFW 3.4 Linux - www.glfw.org 3 | //------------------------------------------------------------------------ 4 | // Copyright (c) 2014 Jonas Ådahl 5 | // 6 | // This software is provided 'as-is', without any express or implied 7 | // warranty. In no event will the authors be held liable for any damages 8 | // arising from the use of this software. 9 | // 10 | // Permission is granted to anyone to use this software for any purpose, 11 | // including commercial applications, and to alter it and redistribute it 12 | // freely, subject to the following restrictions: 13 | // 14 | // 1. The origin of this software must not be misrepresented; you must not 15 | // claim that you wrote the original software. If you use this software 16 | // in a product, an acknowledgment in the product documentation would 17 | // be appreciated but is not required. 18 | // 19 | // 2. Altered source versions must be plainly marked as such, and must not 20 | // be misrepresented as being the original software. 21 | // 22 | // 3. This notice may not be removed or altered from any source 23 | // distribution. 24 | // 25 | //======================================================================== 26 | 27 | #define GLFW_INVALID_CODEPOINT 0xffffffffu 28 | 29 | uint32_t _glfwKeySym2Unicode(unsigned int keysym); 30 | 31 | -------------------------------------------------------------------------------- /docs/header.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | $projectname: $title 9 | $title 10 | 11 | 12 | 13 | $treeview 14 | $search 15 | $mathjax 16 | 17 | $extrastylesheet 18 | 19 | 20 |
21 | 22 | 23 |
24 |
25 | GLFW 26 | 31 |
32 |
33 | 34 | 35 | -------------------------------------------------------------------------------- /CMake/Info.plist.in: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | English 7 | CFBundleExecutable 8 | ${MACOSX_BUNDLE_EXECUTABLE_NAME} 9 | CFBundleGetInfoString 10 | ${MACOSX_BUNDLE_INFO_STRING} 11 | CFBundleIconFile 12 | ${MACOSX_BUNDLE_ICON_FILE} 13 | CFBundleIdentifier 14 | ${MACOSX_BUNDLE_GUI_IDENTIFIER} 15 | CFBundleInfoDictionaryVersion 16 | 6.0 17 | CFBundleLongVersionString 18 | ${MACOSX_BUNDLE_LONG_VERSION_STRING} 19 | CFBundleName 20 | ${MACOSX_BUNDLE_BUNDLE_NAME} 21 | CFBundlePackageType 22 | APPL 23 | CFBundleShortVersionString 24 | ${MACOSX_BUNDLE_SHORT_VERSION_STRING} 25 | CFBundleSignature 26 | ???? 27 | CFBundleVersion 28 | ${MACOSX_BUNDLE_BUNDLE_VERSION} 29 | CSResourcesFileMapped 30 | 31 | LSRequiresCarbon 32 | 33 | NSHumanReadableCopyright 34 | ${MACOSX_BUNDLE_COPYRIGHT} 35 | NSHighResolutionCapable 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /src/cocoa_time.h: -------------------------------------------------------------------------------- 1 | //======================================================================== 2 | // GLFW 3.4 macOS - www.glfw.org 3 | //------------------------------------------------------------------------ 4 | // Copyright (c) 2009-2021 Camilla Löwy 5 | // 6 | // This software is provided 'as-is', without any express or implied 7 | // warranty. In no event will the authors be held liable for any damages 8 | // arising from the use of this software. 9 | // 10 | // Permission is granted to anyone to use this software for any purpose, 11 | // including commercial applications, and to alter it and redistribute it 12 | // freely, subject to the following restrictions: 13 | // 14 | // 1. The origin of this software must not be misrepresented; you must not 15 | // claim that you wrote the original software. If you use this software 16 | // in a product, an acknowledgment in the product documentation would 17 | // be appreciated but is not required. 18 | // 19 | // 2. Altered source versions must be plainly marked as such, and must not 20 | // be misrepresented as being the original software. 21 | // 22 | // 3. This notice may not be removed or altered from any source 23 | // distribution. 24 | // 25 | //======================================================================== 26 | 27 | #define GLFW_COCOA_LIBRARY_TIMER_STATE _GLFWtimerNS ns; 28 | 29 | // Cocoa-specific global timer data 30 | // 31 | typedef struct _GLFWtimerNS 32 | { 33 | uint64_t frequency; 34 | } _GLFWtimerNS; 35 | 36 | -------------------------------------------------------------------------------- /src/null_joystick.h: -------------------------------------------------------------------------------- 1 | //======================================================================== 2 | // GLFW 3.4 - www.glfw.org 3 | //------------------------------------------------------------------------ 4 | // Copyright (c) 2006-2017 Camilla Löwy 5 | // 6 | // This software is provided 'as-is', without any express or implied 7 | // warranty. In no event will the authors be held liable for any damages 8 | // arising from the use of this software. 9 | // 10 | // Permission is granted to anyone to use this software for any purpose, 11 | // including commercial applications, and to alter it and redistribute it 12 | // freely, subject to the following restrictions: 13 | // 14 | // 1. The origin of this software must not be misrepresented; you must not 15 | // claim that you wrote the original software. If you use this software 16 | // in a product, an acknowledgment in the product documentation would 17 | // be appreciated but is not required. 18 | // 19 | // 2. Altered source versions must be plainly marked as such, and must not 20 | // be misrepresented as being the original software. 21 | // 22 | // 3. This notice may not be removed or altered from any source 23 | // distribution. 24 | // 25 | //======================================================================== 26 | 27 | GLFWbool _glfwInitJoysticksNull(void); 28 | void _glfwTerminateJoysticksNull(void); 29 | GLFWbool _glfwPollJoystickNull(_GLFWjoystick* js, int mode); 30 | const char* _glfwGetMappingNameNull(void); 31 | void _glfwUpdateGamepadGUIDNull(char* guid); 32 | 33 | -------------------------------------------------------------------------------- /src/posix_poll.h: -------------------------------------------------------------------------------- 1 | //======================================================================== 2 | // GLFW 3.4 POSIX - www.glfw.org 3 | //------------------------------------------------------------------------ 4 | // Copyright (c) 2022 Camilla Löwy 5 | // 6 | // This software is provided 'as-is', without any express or implied 7 | // warranty. In no event will the authors be held liable for any damages 8 | // arising from the use of this software. 9 | // 10 | // Permission is granted to anyone to use this software for any purpose, 11 | // including commercial applications, and to alter it and redistribute it 12 | // freely, subject to the following restrictions: 13 | // 14 | // 1. The origin of this software must not be misrepresented; you must not 15 | // claim that you wrote the original software. If you use this software 16 | // in a product, an acknowledgment in the product documentation would 17 | // be appreciated but is not required. 18 | // 19 | // 2. Altered source versions must be plainly marked as such, and must not 20 | // be misrepresented as being the original software. 21 | // 22 | // 3. This notice may not be removed or altered from any source 23 | // distribution. 24 | // 25 | //======================================================================== 26 | // It is fine to use C99 in this file because it will not be built with VS 27 | //======================================================================== 28 | 29 | #include 30 | 31 | GLFWbool _glfwPollPOSIX(struct pollfd* fds, nfds_t count, double* timeout); 32 | 33 | -------------------------------------------------------------------------------- /docs/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | 2 | # NOTE: The order of this list determines the order of items in the Guides 3 | # (i.e. Pages) list in the generated documentation 4 | set(source_files 5 | main.dox 6 | news.dox 7 | quick.dox 8 | moving.dox 9 | compile.dox 10 | build.dox 11 | intro.dox 12 | context.dox 13 | monitor.dox 14 | window.dox 15 | input.dox 16 | vulkan.dox 17 | compat.dox 18 | internal.dox) 19 | 20 | set(extra_files DoxygenLayout.xml header.html footer.html extra.css spaces.svg) 21 | 22 | set(header_paths 23 | "${GLFW_SOURCE_DIR}/include/GLFW/glfw3.h" 24 | "${GLFW_SOURCE_DIR}/include/GLFW/glfw3native.h") 25 | 26 | # Format the source list into a Doxyfile INPUT value that Doxygen can parse 27 | foreach(path IN LISTS header_paths) 28 | string(APPEND GLFW_DOXYGEN_INPUT " \\\n\"${path}\"") 29 | endforeach() 30 | foreach(file IN LISTS source_files) 31 | string(APPEND GLFW_DOXYGEN_INPUT " \\\n\"${CMAKE_CURRENT_SOURCE_DIR}/${file}\"") 32 | endforeach() 33 | 34 | configure_file(Doxyfile.in Doxyfile @ONLY) 35 | 36 | add_custom_command(OUTPUT "html/index.html" 37 | COMMAND "${DOXYGEN_EXECUTABLE}" 38 | WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}" 39 | MAIN_DEPENDENCY Doxyfile 40 | DEPENDS ${header_paths} ${source_files} ${extra_files} 41 | COMMENT "Generating HTML documentation" 42 | VERBATIM) 43 | 44 | add_custom_target(docs ALL SOURCES "html/index.html") 45 | set_target_properties(docs PROPERTIES FOLDER "GLFW3") 46 | 47 | -------------------------------------------------------------------------------- /src/win32_time.h: -------------------------------------------------------------------------------- 1 | //======================================================================== 2 | // GLFW 3.4 Win32 - www.glfw.org 3 | //------------------------------------------------------------------------ 4 | // Copyright (c) 2002-2006 Marcus Geelnard 5 | // Copyright (c) 2006-2017 Camilla Löwy 6 | // 7 | // This software is provided 'as-is', without any express or implied 8 | // warranty. In no event will the authors be held liable for any damages 9 | // arising from the use of this software. 10 | // 11 | // Permission is granted to anyone to use this software for any purpose, 12 | // including commercial applications, and to alter it and redistribute it 13 | // freely, subject to the following restrictions: 14 | // 15 | // 1. The origin of this software must not be misrepresented; you must not 16 | // claim that you wrote the original software. If you use this software 17 | // in a product, an acknowledgment in the product documentation would 18 | // be appreciated but is not required. 19 | // 20 | // 2. Altered source versions must be plainly marked as such, and must not 21 | // be misrepresented as being the original software. 22 | // 23 | // 3. This notice may not be removed or altered from any source 24 | // distribution. 25 | // 26 | //======================================================================== 27 | 28 | #include 29 | 30 | #define GLFW_WIN32_LIBRARY_TIMER_STATE _GLFWtimerWin32 win32; 31 | 32 | // Win32-specific global timer data 33 | // 34 | typedef struct _GLFWtimerWin32 35 | { 36 | uint64_t frequency; 37 | } _GLFWtimerWin32; 38 | 39 | -------------------------------------------------------------------------------- /src/posix_time.h: -------------------------------------------------------------------------------- 1 | //======================================================================== 2 | // GLFW 3.4 POSIX - www.glfw.org 3 | //------------------------------------------------------------------------ 4 | // Copyright (c) 2002-2006 Marcus Geelnard 5 | // Copyright (c) 2006-2017 Camilla Löwy 6 | // 7 | // This software is provided 'as-is', without any express or implied 8 | // warranty. In no event will the authors be held liable for any damages 9 | // arising from the use of this software. 10 | // 11 | // Permission is granted to anyone to use this software for any purpose, 12 | // including commercial applications, and to alter it and redistribute it 13 | // freely, subject to the following restrictions: 14 | // 15 | // 1. The origin of this software must not be misrepresented; you must not 16 | // claim that you wrote the original software. If you use this software 17 | // in a product, an acknowledgment in the product documentation would 18 | // be appreciated but is not required. 19 | // 20 | // 2. Altered source versions must be plainly marked as such, and must not 21 | // be misrepresented as being the original software. 22 | // 23 | // 3. This notice may not be removed or altered from any source 24 | // distribution. 25 | // 26 | //======================================================================== 27 | 28 | #define GLFW_POSIX_LIBRARY_TIMER_STATE _GLFWtimerPOSIX posix; 29 | 30 | #include 31 | #include 32 | 33 | 34 | // POSIX-specific global timer data 35 | // 36 | typedef struct _GLFWtimerPOSIX 37 | { 38 | clockid_t clock; 39 | uint64_t frequency; 40 | } _GLFWtimerPOSIX; 41 | 42 | -------------------------------------------------------------------------------- /CMake/GenerateMappings.cmake: -------------------------------------------------------------------------------- 1 | # Usage: 2 | # cmake -P GenerateMappings.cmake 3 | 4 | set(source_url "https://raw.githubusercontent.com/gabomdq/SDL_GameControllerDB/master/gamecontrollerdb.txt") 5 | set(source_path "${CMAKE_CURRENT_BINARY_DIR}/gamecontrollerdb.txt") 6 | set(template_path "${CMAKE_ARGV3}") 7 | set(target_path "${CMAKE_ARGV4}") 8 | 9 | if (NOT EXISTS "${template_path}") 10 | message(FATAL_ERROR "Failed to find template file ${template_path}") 11 | endif() 12 | 13 | file(DOWNLOAD "${source_url}" "${source_path}" 14 | STATUS download_status 15 | TLS_VERIFY on) 16 | 17 | list(GET download_status 0 status_code) 18 | list(GET download_status 1 status_message) 19 | 20 | if (status_code) 21 | message(FATAL_ERROR "Failed to download ${source_url}: ${status_message}") 22 | endif() 23 | 24 | file(STRINGS "${source_path}" lines) 25 | foreach(line ${lines}) 26 | if (line MATCHES "^[0-9a-fA-F]") 27 | if (line MATCHES "platform:Windows") 28 | if (GLFW_WIN32_MAPPINGS) 29 | string(APPEND GLFW_WIN32_MAPPINGS "\n") 30 | endif() 31 | string(APPEND GLFW_WIN32_MAPPINGS "\"${line}\",") 32 | elseif (line MATCHES "platform:Mac OS X") 33 | if (GLFW_COCOA_MAPPINGS) 34 | string(APPEND GLFW_COCOA_MAPPINGS "\n") 35 | endif() 36 | string(APPEND GLFW_COCOA_MAPPINGS "\"${line}\",") 37 | elseif (line MATCHES "platform:Linux") 38 | if (GLFW_LINUX_MAPPINGS) 39 | string(APPEND GLFW_LINUX_MAPPINGS "\n") 40 | endif() 41 | string(APPEND GLFW_LINUX_MAPPINGS "\"${line}\",") 42 | endif() 43 | endif() 44 | endforeach() 45 | 46 | configure_file("${template_path}" "${target_path}" @ONLY NEWLINE_STYLE UNIX) 47 | file(REMOVE "${source_path}") 48 | 49 | -------------------------------------------------------------------------------- /src/posix_thread.h: -------------------------------------------------------------------------------- 1 | //======================================================================== 2 | // GLFW 3.4 POSIX - www.glfw.org 3 | //------------------------------------------------------------------------ 4 | // Copyright (c) 2002-2006 Marcus Geelnard 5 | // Copyright (c) 2006-2017 Camilla Löwy 6 | // 7 | // This software is provided 'as-is', without any express or implied 8 | // warranty. In no event will the authors be held liable for any damages 9 | // arising from the use of this software. 10 | // 11 | // Permission is granted to anyone to use this software for any purpose, 12 | // including commercial applications, and to alter it and redistribute it 13 | // freely, subject to the following restrictions: 14 | // 15 | // 1. The origin of this software must not be misrepresented; you must not 16 | // claim that you wrote the original software. If you use this software 17 | // in a product, an acknowledgment in the product documentation would 18 | // be appreciated but is not required. 19 | // 20 | // 2. Altered source versions must be plainly marked as such, and must not 21 | // be misrepresented as being the original software. 22 | // 23 | // 3. This notice may not be removed or altered from any source 24 | // distribution. 25 | // 26 | //======================================================================== 27 | 28 | #include 29 | 30 | #define GLFW_POSIX_TLS_STATE _GLFWtlsPOSIX posix; 31 | #define GLFW_POSIX_MUTEX_STATE _GLFWmutexPOSIX posix; 32 | 33 | 34 | // POSIX-specific thread local storage data 35 | // 36 | typedef struct _GLFWtlsPOSIX 37 | { 38 | GLFWbool allocated; 39 | pthread_key_t key; 40 | } _GLFWtlsPOSIX; 41 | 42 | // POSIX-specific mutex data 43 | // 44 | typedef struct _GLFWmutexPOSIX 45 | { 46 | GLFWbool allocated; 47 | pthread_mutex_t handle; 48 | } _GLFWmutexPOSIX; 49 | 50 | -------------------------------------------------------------------------------- /src/win32_thread.h: -------------------------------------------------------------------------------- 1 | //======================================================================== 2 | // GLFW 3.4 Win32 - www.glfw.org 3 | //------------------------------------------------------------------------ 4 | // Copyright (c) 2002-2006 Marcus Geelnard 5 | // Copyright (c) 2006-2017 Camilla Löwy 6 | // 7 | // This software is provided 'as-is', without any express or implied 8 | // warranty. In no event will the authors be held liable for any damages 9 | // arising from the use of this software. 10 | // 11 | // Permission is granted to anyone to use this software for any purpose, 12 | // including commercial applications, and to alter it and redistribute it 13 | // freely, subject to the following restrictions: 14 | // 15 | // 1. The origin of this software must not be misrepresented; you must not 16 | // claim that you wrote the original software. If you use this software 17 | // in a product, an acknowledgment in the product documentation would 18 | // be appreciated but is not required. 19 | // 20 | // 2. Altered source versions must be plainly marked as such, and must not 21 | // be misrepresented as being the original software. 22 | // 23 | // 3. This notice may not be removed or altered from any source 24 | // distribution. 25 | // 26 | //======================================================================== 27 | 28 | #include 29 | 30 | #define GLFW_WIN32_TLS_STATE _GLFWtlsWin32 win32; 31 | #define GLFW_WIN32_MUTEX_STATE _GLFWmutexWin32 win32; 32 | 33 | // Win32-specific thread local storage data 34 | // 35 | typedef struct _GLFWtlsWin32 36 | { 37 | GLFWbool allocated; 38 | DWORD index; 39 | } _GLFWtlsWin32; 40 | 41 | // Win32-specific mutex data 42 | // 43 | typedef struct _GLFWmutexWin32 44 | { 45 | GLFWbool allocated; 46 | CRITICAL_SECTION section; 47 | } _GLFWmutexWin32; 48 | 49 | -------------------------------------------------------------------------------- /docs/main.dox: -------------------------------------------------------------------------------- 1 | /*! 2 | 3 | @mainpage notitle 4 | 5 | @section main_intro Introduction 6 | 7 | GLFW is a free, Open Source, multi-platform library for OpenGL, OpenGL ES and 8 | Vulkan application development. It provides a simple, platform-independent API 9 | for creating windows, contexts and surfaces, reading input, handling events, etc. 10 | 11 | @ref news_34 list new features, caveats and deprecations. 12 | 13 | @ref quick_guide is a guide for users new to GLFW. It takes you through how to 14 | write a small but complete program. 15 | 16 | There are guides for each section of the API: 17 | 18 | - @ref intro_guide – initialization, error handling and high-level design 19 | - @ref window_guide – creating and working with windows and framebuffers 20 | - @ref context_guide – working with OpenGL and OpenGL ES contexts 21 | - @ref vulkan_guide - working with Vulkan objects and extensions 22 | - @ref monitor_guide – enumerating and working with monitors and video modes 23 | - @ref input_guide – receiving events, polling and processing input 24 | 25 | Once you have written a program, see @ref compile_guide and @ref build_guide. 26 | 27 | The [reference documentation](modules.html) provides more detailed information 28 | about specific functions. 29 | 30 | @ref moving_guide explains what has changed and how to update existing code to 31 | use the new API. 32 | 33 | There is a section on @ref guarantees_limitations for pointer lifetimes, 34 | reentrancy, thread safety, event order and backward and forward compatibility. 35 | 36 | The [FAQ](https://www.glfw.org/faq.html) answers many common questions about the 37 | design, implementation and use of GLFW. 38 | 39 | Finally, @ref compat_guide explains what APIs, standards and protocols GLFW uses 40 | and what happens when they are not present on a given machine. 41 | 42 | This documentation was generated with Doxygen. The sources for it are available 43 | in both the [source distribution](https://www.glfw.org/download.html) and 44 | [GitHub repository](https://github.com/glfw/glfw). 45 | 46 | */ 47 | -------------------------------------------------------------------------------- /src/cocoa_joystick.h: -------------------------------------------------------------------------------- 1 | //======================================================================== 2 | // GLFW 3.4 Cocoa - www.glfw.org 3 | //------------------------------------------------------------------------ 4 | // Copyright (c) 2006-2017 Camilla Löwy 5 | // 6 | // This software is provided 'as-is', without any express or implied 7 | // warranty. In no event will the authors be held liable for any damages 8 | // arising from the use of this software. 9 | // 10 | // Permission is granted to anyone to use this software for any purpose, 11 | // including commercial applications, and to alter it and redistribute it 12 | // freely, subject to the following restrictions: 13 | // 14 | // 1. The origin of this software must not be misrepresented; you must not 15 | // claim that you wrote the original software. If you use this software 16 | // in a product, an acknowledgment in the product documentation would 17 | // be appreciated but is not required. 18 | // 19 | // 2. Altered source versions must be plainly marked as such, and must not 20 | // be misrepresented as being the original software. 21 | // 22 | // 3. This notice may not be removed or altered from any source 23 | // distribution. 24 | // 25 | //======================================================================== 26 | 27 | #include 28 | #include 29 | #include 30 | 31 | #define GLFW_COCOA_JOYSTICK_STATE _GLFWjoystickNS ns; 32 | #define GLFW_COCOA_LIBRARY_JOYSTICK_STATE 33 | 34 | // Cocoa-specific per-joystick data 35 | // 36 | typedef struct _GLFWjoystickNS 37 | { 38 | IOHIDDeviceRef device; 39 | CFMutableArrayRef axes; 40 | CFMutableArrayRef buttons; 41 | CFMutableArrayRef hats; 42 | } _GLFWjoystickNS; 43 | 44 | GLFWbool _glfwInitJoysticksCocoa(void); 45 | void _glfwTerminateJoysticksCocoa(void); 46 | GLFWbool _glfwPollJoystickCocoa(_GLFWjoystick* js, int mode); 47 | const char* _glfwGetMappingNameCocoa(void); 48 | void _glfwUpdateGamepadGUIDCocoa(char* guid); 49 | 50 | -------------------------------------------------------------------------------- /src/win32_joystick.h: -------------------------------------------------------------------------------- 1 | //======================================================================== 2 | // GLFW 3.4 Win32 - www.glfw.org 3 | //------------------------------------------------------------------------ 4 | // Copyright (c) 2006-2017 Camilla Löwy 5 | // 6 | // This software is provided 'as-is', without any express or implied 7 | // warranty. In no event will the authors be held liable for any damages 8 | // arising from the use of this software. 9 | // 10 | // Permission is granted to anyone to use this software for any purpose, 11 | // including commercial applications, and to alter it and redistribute it 12 | // freely, subject to the following restrictions: 13 | // 14 | // 1. The origin of this software must not be misrepresented; you must not 15 | // claim that you wrote the original software. If you use this software 16 | // in a product, an acknowledgment in the product documentation would 17 | // be appreciated but is not required. 18 | // 19 | // 2. Altered source versions must be plainly marked as such, and must not 20 | // be misrepresented as being the original software. 21 | // 22 | // 3. This notice may not be removed or altered from any source 23 | // distribution. 24 | // 25 | //======================================================================== 26 | 27 | #define GLFW_WIN32_JOYSTICK_STATE _GLFWjoystickWin32 win32; 28 | #define GLFW_WIN32_LIBRARY_JOYSTICK_STATE 29 | 30 | // Joystick element (axis, button or slider) 31 | // 32 | typedef struct _GLFWjoyobjectWin32 33 | { 34 | int offset; 35 | int type; 36 | } _GLFWjoyobjectWin32; 37 | 38 | // Win32-specific per-joystick data 39 | // 40 | typedef struct _GLFWjoystickWin32 41 | { 42 | _GLFWjoyobjectWin32* objects; 43 | int objectCount; 44 | IDirectInputDevice8W* device; 45 | DWORD index; 46 | GUID guid; 47 | } _GLFWjoystickWin32; 48 | 49 | void _glfwDetectJoystickConnectionWin32(void); 50 | void _glfwDetectJoystickDisconnectionWin32(void); 51 | 52 | -------------------------------------------------------------------------------- /src/null_joystick.c: -------------------------------------------------------------------------------- 1 | //======================================================================== 2 | // GLFW 3.4 - www.glfw.org 3 | //------------------------------------------------------------------------ 4 | // Copyright (c) 2016-2017 Camilla Löwy 5 | // 6 | // This software is provided 'as-is', without any express or implied 7 | // warranty. In no event will the authors be held liable for any damages 8 | // arising from the use of this software. 9 | // 10 | // Permission is granted to anyone to use this software for any purpose, 11 | // including commercial applications, and to alter it and redistribute it 12 | // freely, subject to the following restrictions: 13 | // 14 | // 1. The origin of this software must not be misrepresented; you must not 15 | // claim that you wrote the original software. If you use this software 16 | // in a product, an acknowledgment in the product documentation would 17 | // be appreciated but is not required. 18 | // 19 | // 2. Altered source versions must be plainly marked as such, and must not 20 | // be misrepresented as being the original software. 21 | // 22 | // 3. This notice may not be removed or altered from any source 23 | // distribution. 24 | // 25 | //======================================================================== 26 | // It is fine to use C99 in this file because it will not be built with VS 27 | //======================================================================== 28 | 29 | #include "internal.h" 30 | 31 | 32 | ////////////////////////////////////////////////////////////////////////// 33 | ////// GLFW platform API ////// 34 | ////////////////////////////////////////////////////////////////////////// 35 | 36 | GLFWbool _glfwInitJoysticksNull(void) 37 | { 38 | return GLFW_TRUE; 39 | } 40 | 41 | void _glfwTerminateJoysticksNull(void) 42 | { 43 | } 44 | 45 | GLFWbool _glfwPollJoystickNull(_GLFWjoystick* js, int mode) 46 | { 47 | return GLFW_FALSE; 48 | } 49 | 50 | const char* _glfwGetMappingNameNull(void) 51 | { 52 | return ""; 53 | } 54 | 55 | void _glfwUpdateGamepadGUIDNull(char* guid) 56 | { 57 | } 58 | 59 | -------------------------------------------------------------------------------- /src/win32_module.c: -------------------------------------------------------------------------------- 1 | //======================================================================== 2 | // GLFW 3.4 Win32 - www.glfw.org 3 | //------------------------------------------------------------------------ 4 | // Copyright (c) 2021 Camilla Löwy 5 | // 6 | // This software is provided 'as-is', without any express or implied 7 | // warranty. In no event will the authors be held liable for any damages 8 | // arising from the use of this software. 9 | // 10 | // Permission is granted to anyone to use this software for any purpose, 11 | // including commercial applications, and to alter it and redistribute it 12 | // freely, subject to the following restrictions: 13 | // 14 | // 1. The origin of this software must not be misrepresented; you must not 15 | // claim that you wrote the original software. If you use this software 16 | // in a product, an acknowledgment in the product documentation would 17 | // be appreciated but is not required. 18 | // 19 | // 2. Altered source versions must be plainly marked as such, and must not 20 | // be misrepresented as being the original software. 21 | // 22 | // 3. This notice may not be removed or altered from any source 23 | // distribution. 24 | // 25 | //======================================================================== 26 | // Please use C89 style variable declarations in this file because VS 2010 27 | //======================================================================== 28 | 29 | #include "internal.h" 30 | 31 | #if defined(GLFW_BUILD_WIN32_MODULE) 32 | 33 | ////////////////////////////////////////////////////////////////////////// 34 | ////// GLFW platform API ////// 35 | ////////////////////////////////////////////////////////////////////////// 36 | 37 | void* _glfwPlatformLoadModule(const char* path) 38 | { 39 | return LoadLibraryA(path); 40 | } 41 | 42 | void _glfwPlatformFreeModule(void* module) 43 | { 44 | FreeLibrary((HMODULE) module); 45 | } 46 | 47 | GLFWproc _glfwPlatformGetModuleSymbol(void* module, const char* name) 48 | { 49 | return (GLFWproc) GetProcAddress((HMODULE) module, name); 50 | } 51 | 52 | #endif // GLFW_BUILD_WIN32_MODULE 53 | 54 | -------------------------------------------------------------------------------- /src/posix_module.c: -------------------------------------------------------------------------------- 1 | //======================================================================== 2 | // GLFW 3.4 POSIX - www.glfw.org 3 | //------------------------------------------------------------------------ 4 | // Copyright (c) 2021 Camilla Löwy 5 | // 6 | // This software is provided 'as-is', without any express or implied 7 | // warranty. In no event will the authors be held liable for any damages 8 | // arising from the use of this software. 9 | // 10 | // Permission is granted to anyone to use this software for any purpose, 11 | // including commercial applications, and to alter it and redistribute it 12 | // freely, subject to the following restrictions: 13 | // 14 | // 1. The origin of this software must not be misrepresented; you must not 15 | // claim that you wrote the original software. If you use this software 16 | // in a product, an acknowledgment in the product documentation would 17 | // be appreciated but is not required. 18 | // 19 | // 2. Altered source versions must be plainly marked as such, and must not 20 | // be misrepresented as being the original software. 21 | // 22 | // 3. This notice may not be removed or altered from any source 23 | // distribution. 24 | // 25 | //======================================================================== 26 | // It is fine to use C99 in this file because it will not be built with VS 27 | //======================================================================== 28 | 29 | #include "internal.h" 30 | 31 | #if defined(GLFW_BUILD_POSIX_MODULE) 32 | 33 | #include 34 | 35 | ////////////////////////////////////////////////////////////////////////// 36 | ////// GLFW platform API ////// 37 | ////////////////////////////////////////////////////////////////////////// 38 | 39 | void* _glfwPlatformLoadModule(const char* path) 40 | { 41 | return dlopen(path, RTLD_LAZY | RTLD_LOCAL); 42 | } 43 | 44 | void _glfwPlatformFreeModule(void* module) 45 | { 46 | dlclose(module); 47 | } 48 | 49 | GLFWproc _glfwPlatformGetModuleSymbol(void* module, const char* name) 50 | { 51 | return dlsym(module, name); 52 | } 53 | 54 | #endif // GLFW_BUILD_POSIX_MODULE 55 | 56 | -------------------------------------------------------------------------------- /src/cocoa_time.c: -------------------------------------------------------------------------------- 1 | //======================================================================== 2 | // GLFW 3.4 macOS - www.glfw.org 3 | //------------------------------------------------------------------------ 4 | // Copyright (c) 2009-2016 Camilla Löwy 5 | // 6 | // This software is provided 'as-is', without any express or implied 7 | // warranty. In no event will the authors be held liable for any damages 8 | // arising from the use of this software. 9 | // 10 | // Permission is granted to anyone to use this software for any purpose, 11 | // including commercial applications, and to alter it and redistribute it 12 | // freely, subject to the following restrictions: 13 | // 14 | // 1. The origin of this software must not be misrepresented; you must not 15 | // claim that you wrote the original software. If you use this software 16 | // in a product, an acknowledgment in the product documentation would 17 | // be appreciated but is not required. 18 | // 19 | // 2. Altered source versions must be plainly marked as such, and must not 20 | // be misrepresented as being the original software. 21 | // 22 | // 3. This notice may not be removed or altered from any source 23 | // distribution. 24 | // 25 | //======================================================================== 26 | // It is fine to use C99 in this file because it will not be built with VS 27 | //======================================================================== 28 | 29 | #include "internal.h" 30 | 31 | #if defined(GLFW_BUILD_COCOA_TIMER) 32 | 33 | #include 34 | 35 | 36 | ////////////////////////////////////////////////////////////////////////// 37 | ////// GLFW platform API ////// 38 | ////////////////////////////////////////////////////////////////////////// 39 | 40 | void _glfwPlatformInitTimer(void) 41 | { 42 | mach_timebase_info_data_t info; 43 | mach_timebase_info(&info); 44 | 45 | _glfw.timer.ns.frequency = (info.denom * 1e9) / info.numer; 46 | } 47 | 48 | uint64_t _glfwPlatformGetTimerValue(void) 49 | { 50 | return mach_absolute_time(); 51 | } 52 | 53 | uint64_t _glfwPlatformGetTimerFrequency(void) 54 | { 55 | return _glfw.timer.ns.frequency; 56 | } 57 | 58 | #endif // GLFW_BUILD_COCOA_TIMER 59 | 60 | -------------------------------------------------------------------------------- /src/win32_time.c: -------------------------------------------------------------------------------- 1 | //======================================================================== 2 | // GLFW 3.4 Win32 - www.glfw.org 3 | //------------------------------------------------------------------------ 4 | // Copyright (c) 2002-2006 Marcus Geelnard 5 | // Copyright (c) 2006-2017 Camilla Löwy 6 | // 7 | // This software is provided 'as-is', without any express or implied 8 | // warranty. In no event will the authors be held liable for any damages 9 | // arising from the use of this software. 10 | // 11 | // Permission is granted to anyone to use this software for any purpose, 12 | // including commercial applications, and to alter it and redistribute it 13 | // freely, subject to the following restrictions: 14 | // 15 | // 1. The origin of this software must not be misrepresented; you must not 16 | // claim that you wrote the original software. If you use this software 17 | // in a product, an acknowledgment in the product documentation would 18 | // be appreciated but is not required. 19 | // 20 | // 2. Altered source versions must be plainly marked as such, and must not 21 | // be misrepresented as being the original software. 22 | // 23 | // 3. This notice may not be removed or altered from any source 24 | // distribution. 25 | // 26 | //======================================================================== 27 | // Please use C89 style variable declarations in this file because VS 2010 28 | //======================================================================== 29 | 30 | #include "internal.h" 31 | 32 | #if defined(GLFW_BUILD_WIN32_TIMER) 33 | 34 | ////////////////////////////////////////////////////////////////////////// 35 | ////// GLFW platform API ////// 36 | ////////////////////////////////////////////////////////////////////////// 37 | 38 | void _glfwPlatformInitTimer(void) 39 | { 40 | QueryPerformanceFrequency((LARGE_INTEGER*) &_glfw.timer.win32.frequency); 41 | } 42 | 43 | uint64_t _glfwPlatformGetTimerValue(void) 44 | { 45 | uint64_t value; 46 | QueryPerformanceCounter((LARGE_INTEGER*) &value); 47 | return value; 48 | } 49 | 50 | uint64_t _glfwPlatformGetTimerFrequency(void) 51 | { 52 | return _glfw.timer.win32.frequency; 53 | } 54 | 55 | #endif // GLFW_BUILD_WIN32_TIMER 56 | 57 | -------------------------------------------------------------------------------- /docs/DoxygenLayout.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | -------------------------------------------------------------------------------- /deps/getopt.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2012, Kim Gräsman 2 | * All rights reserved. 3 | * 4 | * Redistribution and use in source and binary forms, with or without 5 | * modification, are permitted provided that the following conditions are met: 6 | * * Redistributions of source code must retain the above copyright notice, 7 | * this list of conditions and the following disclaimer. 8 | * * Redistributions in binary form must reproduce the above copyright notice, 9 | * this list of conditions and the following disclaimer in the documentation 10 | * and/or other materials provided with the distribution. 11 | * * Neither the name of Kim Gräsman nor the names of contributors may be used 12 | * to endorse or promote products derived from this software without specific 13 | * prior written permission. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 | * ARE DISCLAIMED. IN NO EVENT SHALL KIM GRÄSMAN BE LIABLE FOR ANY DIRECT, 19 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 22 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 | */ 26 | 27 | #ifndef INCLUDED_GETOPT_PORT_H 28 | #define INCLUDED_GETOPT_PORT_H 29 | 30 | #if defined(__cplusplus) 31 | extern "C" { 32 | #endif 33 | 34 | extern const int no_argument; 35 | extern const int required_argument; 36 | extern const int optional_argument; 37 | 38 | extern char* optarg; 39 | extern int optind, opterr, optopt; 40 | 41 | struct option { 42 | const char* name; 43 | int has_arg; 44 | int* flag; 45 | int val; 46 | }; 47 | 48 | int getopt(int argc, char* const argv[], const char* optstring); 49 | 50 | int getopt_long(int argc, char* const argv[], 51 | const char* optstring, const struct option* longopts, int* longindex); 52 | 53 | #if defined(__cplusplus) 54 | } 55 | #endif 56 | 57 | #endif // INCLUDED_GETOPT_PORT_H 58 | -------------------------------------------------------------------------------- /tests/title.c: -------------------------------------------------------------------------------- 1 | //======================================================================== 2 | // UTF-8 window title test 3 | // Copyright (c) Camilla Löwy 4 | // 5 | // This software is provided 'as-is', without any express or implied 6 | // warranty. In no event will the authors be held liable for any damages 7 | // arising from the use of this software. 8 | // 9 | // Permission is granted to anyone to use this software for any purpose, 10 | // including commercial applications, and to alter it and redistribute it 11 | // freely, subject to the following restrictions: 12 | // 13 | // 1. The origin of this software must not be misrepresented; you must not 14 | // claim that you wrote the original software. If you use this software 15 | // in a product, an acknowledgment in the product documentation would 16 | // be appreciated but is not required. 17 | // 18 | // 2. Altered source versions must be plainly marked as such, and must not 19 | // be misrepresented as being the original software. 20 | // 21 | // 3. This notice may not be removed or altered from any source 22 | // distribution. 23 | // 24 | //======================================================================== 25 | // 26 | // This test sets a UTF-8 window title 27 | // 28 | //======================================================================== 29 | 30 | #define GLAD_GL_IMPLEMENTATION 31 | #include 32 | #define GLFW_INCLUDE_NONE 33 | #include 34 | 35 | #include 36 | #include 37 | 38 | static void error_callback(int error, const char* description) 39 | { 40 | fprintf(stderr, "Error: %s\n", description); 41 | } 42 | 43 | int main(void) 44 | { 45 | GLFWwindow* window; 46 | 47 | glfwSetErrorCallback(error_callback); 48 | 49 | if (!glfwInit()) 50 | exit(EXIT_FAILURE); 51 | 52 | window = glfwCreateWindow(400, 400, "English 日本語 русский язык 官話", NULL, NULL); 53 | if (!window) 54 | { 55 | glfwTerminate(); 56 | exit(EXIT_FAILURE); 57 | } 58 | 59 | glfwMakeContextCurrent(window); 60 | gladLoadGL(glfwGetProcAddress); 61 | glfwSwapInterval(1); 62 | 63 | while (!glfwWindowShouldClose(window)) 64 | { 65 | glClear(GL_COLOR_BUFFER_BIT); 66 | glfwSwapBuffers(window); 67 | glfwWaitEvents(); 68 | } 69 | 70 | glfwTerminate(); 71 | exit(EXIT_SUCCESS); 72 | } 73 | 74 | -------------------------------------------------------------------------------- /src/linux_joystick.h: -------------------------------------------------------------------------------- 1 | //======================================================================== 2 | // GLFW 3.4 Linux - www.glfw.org 3 | //------------------------------------------------------------------------ 4 | // Copyright (c) 2014 Jonas Ådahl 5 | // 6 | // This software is provided 'as-is', without any express or implied 7 | // warranty. In no event will the authors be held liable for any damages 8 | // arising from the use of this software. 9 | // 10 | // Permission is granted to anyone to use this software for any purpose, 11 | // including commercial applications, and to alter it and redistribute it 12 | // freely, subject to the following restrictions: 13 | // 14 | // 1. The origin of this software must not be misrepresented; you must not 15 | // claim that you wrote the original software. If you use this software 16 | // in a product, an acknowledgment in the product documentation would 17 | // be appreciated but is not required. 18 | // 19 | // 2. Altered source versions must be plainly marked as such, and must not 20 | // be misrepresented as being the original software. 21 | // 22 | // 3. This notice may not be removed or altered from any source 23 | // distribution. 24 | // 25 | //======================================================================== 26 | 27 | #include 28 | #include 29 | #include 30 | 31 | #define GLFW_LINUX_JOYSTICK_STATE _GLFWjoystickLinux linjs; 32 | #define GLFW_LINUX_LIBRARY_JOYSTICK_STATE _GLFWlibraryLinux linjs; 33 | 34 | // Linux-specific joystick data 35 | // 36 | typedef struct _GLFWjoystickLinux 37 | { 38 | int fd; 39 | char path[PATH_MAX]; 40 | int keyMap[KEY_CNT - BTN_MISC]; 41 | int absMap[ABS_CNT]; 42 | struct input_absinfo absInfo[ABS_CNT]; 43 | int hats[4][2]; 44 | } _GLFWjoystickLinux; 45 | 46 | // Linux-specific joystick API data 47 | // 48 | typedef struct _GLFWlibraryLinux 49 | { 50 | int inotify; 51 | int watch; 52 | regex_t regex; 53 | GLFWbool dropped; 54 | } _GLFWlibraryLinux; 55 | 56 | void _glfwDetectJoystickConnectionLinux(void); 57 | 58 | GLFWbool _glfwInitJoysticksLinux(void); 59 | void _glfwTerminateJoysticksLinux(void); 60 | GLFWbool _glfwPollJoystickLinux(_GLFWjoystick* js, int mode); 61 | const char* _glfwGetMappingNameLinux(void); 62 | void _glfwUpdateGamepadGUIDLinux(char* guid); 63 | 64 | -------------------------------------------------------------------------------- /src/posix_time.c: -------------------------------------------------------------------------------- 1 | //======================================================================== 2 | // GLFW 3.4 POSIX - www.glfw.org 3 | //------------------------------------------------------------------------ 4 | // Copyright (c) 2002-2006 Marcus Geelnard 5 | // Copyright (c) 2006-2017 Camilla Löwy 6 | // 7 | // This software is provided 'as-is', without any express or implied 8 | // warranty. In no event will the authors be held liable for any damages 9 | // arising from the use of this software. 10 | // 11 | // Permission is granted to anyone to use this software for any purpose, 12 | // including commercial applications, and to alter it and redistribute it 13 | // freely, subject to the following restrictions: 14 | // 15 | // 1. The origin of this software must not be misrepresented; you must not 16 | // claim that you wrote the original software. If you use this software 17 | // in a product, an acknowledgment in the product documentation would 18 | // be appreciated but is not required. 19 | // 20 | // 2. Altered source versions must be plainly marked as such, and must not 21 | // be misrepresented as being the original software. 22 | // 23 | // 3. This notice may not be removed or altered from any source 24 | // distribution. 25 | // 26 | //======================================================================== 27 | // It is fine to use C99 in this file because it will not be built with VS 28 | //======================================================================== 29 | 30 | #include "internal.h" 31 | 32 | #if defined(GLFW_BUILD_POSIX_TIMER) 33 | 34 | #include 35 | #include 36 | 37 | 38 | ////////////////////////////////////////////////////////////////////////// 39 | ////// GLFW platform API ////// 40 | ////////////////////////////////////////////////////////////////////////// 41 | 42 | void _glfwPlatformInitTimer(void) 43 | { 44 | _glfw.timer.posix.clock = CLOCK_REALTIME; 45 | _glfw.timer.posix.frequency = 1000000000; 46 | 47 | #if defined(_POSIX_MONOTONIC_CLOCK) 48 | struct timespec ts; 49 | if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) 50 | _glfw.timer.posix.clock = CLOCK_MONOTONIC; 51 | #endif 52 | } 53 | 54 | uint64_t _glfwPlatformGetTimerValue(void) 55 | { 56 | struct timespec ts; 57 | clock_gettime(_glfw.timer.posix.clock, &ts); 58 | return (uint64_t) ts.tv_sec * _glfw.timer.posix.frequency + (uint64_t) ts.tv_nsec; 59 | } 60 | 61 | uint64_t _glfwPlatformGetTimerFrequency(void) 62 | { 63 | return _glfw.timer.posix.frequency; 64 | } 65 | 66 | #endif // GLFW_BUILD_POSIX_TIMER 67 | 68 | -------------------------------------------------------------------------------- /tests/timeout.c: -------------------------------------------------------------------------------- 1 | //======================================================================== 2 | // Event wait timeout test 3 | // Copyright (c) Camilla Löwy 4 | // 5 | // This software is provided 'as-is', without any express or implied 6 | // warranty. In no event will the authors be held liable for any damages 7 | // arising from the use of this software. 8 | // 9 | // Permission is granted to anyone to use this software for any purpose, 10 | // including commercial applications, and to alter it and redistribute it 11 | // freely, subject to the following restrictions: 12 | // 13 | // 1. The origin of this software must not be misrepresented; you must not 14 | // claim that you wrote the original software. If you use this software 15 | // in a product, an acknowledgment in the product documentation would 16 | // be appreciated but is not required. 17 | // 18 | // 2. Altered source versions must be plainly marked as such, and must not 19 | // be misrepresented as being the original software. 20 | // 21 | // 3. This notice may not be removed or altered from any source 22 | // distribution. 23 | // 24 | //======================================================================== 25 | // 26 | // This test is intended to verify that waiting for events with timeout works 27 | // 28 | //======================================================================== 29 | 30 | #define GLAD_GL_IMPLEMENTATION 31 | #include 32 | #define GLFW_INCLUDE_NONE 33 | #include 34 | 35 | #include 36 | #include 37 | #include 38 | #include 39 | 40 | static void error_callback(int error, const char* description) 41 | { 42 | fprintf(stderr, "Error: %s\n", description); 43 | } 44 | 45 | static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) 46 | { 47 | if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) 48 | glfwSetWindowShouldClose(window, GLFW_TRUE); 49 | } 50 | 51 | static float nrand(void) 52 | { 53 | return (float) rand() / (float) RAND_MAX; 54 | } 55 | 56 | int main(void) 57 | { 58 | GLFWwindow* window; 59 | 60 | srand((unsigned int) time(NULL)); 61 | 62 | glfwSetErrorCallback(error_callback); 63 | 64 | if (!glfwInit()) 65 | exit(EXIT_FAILURE); 66 | 67 | window = glfwCreateWindow(640, 480, "Event Wait Timeout Test", NULL, NULL); 68 | if (!window) 69 | { 70 | glfwTerminate(); 71 | exit(EXIT_FAILURE); 72 | } 73 | 74 | glfwMakeContextCurrent(window); 75 | gladLoadGL(glfwGetProcAddress); 76 | glfwSetKeyCallback(window, key_callback); 77 | 78 | while (!glfwWindowShouldClose(window)) 79 | { 80 | int width, height; 81 | float r = nrand(), g = nrand(), b = nrand(); 82 | float l = (float) sqrt(r * r + g * g + b * b); 83 | 84 | glfwGetFramebufferSize(window, &width, &height); 85 | 86 | glViewport(0, 0, width, height); 87 | glClearColor(r / l, g / l, b / l, 1.f); 88 | glClear(GL_COLOR_BUFFER_BIT); 89 | glfwSwapBuffers(window); 90 | 91 | glfwWaitEventsTimeout(1.0); 92 | } 93 | 94 | glfwDestroyWindow(window); 95 | 96 | glfwTerminate(); 97 | exit(EXIT_SUCCESS); 98 | } 99 | 100 | -------------------------------------------------------------------------------- /src/posix_poll.c: -------------------------------------------------------------------------------- 1 | //======================================================================== 2 | // GLFW 3.4 POSIX - www.glfw.org 3 | //------------------------------------------------------------------------ 4 | // Copyright (c) 2022 Camilla Löwy 5 | // 6 | // This software is provided 'as-is', without any express or implied 7 | // warranty. In no event will the authors be held liable for any damages 8 | // arising from the use of this software. 9 | // 10 | // Permission is granted to anyone to use this software for any purpose, 11 | // including commercial applications, and to alter it and redistribute it 12 | // freely, subject to the following restrictions: 13 | // 14 | // 1. The origin of this software must not be misrepresented; you must not 15 | // claim that you wrote the original software. If you use this software 16 | // in a product, an acknowledgment in the product documentation would 17 | // be appreciated but is not required. 18 | // 19 | // 2. Altered source versions must be plainly marked as such, and must not 20 | // be misrepresented as being the original software. 21 | // 22 | // 3. This notice may not be removed or altered from any source 23 | // distribution. 24 | // 25 | //======================================================================== 26 | // It is fine to use C99 in this file because it will not be built with VS 27 | //======================================================================== 28 | 29 | #define _GNU_SOURCE 30 | 31 | #include "internal.h" 32 | 33 | #if defined(GLFW_BUILD_POSIX_POLL) 34 | 35 | #include 36 | #include 37 | #include 38 | 39 | GLFWbool _glfwPollPOSIX(struct pollfd* fds, nfds_t count, double* timeout) 40 | { 41 | for (;;) 42 | { 43 | if (timeout) 44 | { 45 | const uint64_t base = _glfwPlatformGetTimerValue(); 46 | 47 | #if defined(__linux__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__CYGWIN__) 48 | const time_t seconds = (time_t) *timeout; 49 | const long nanoseconds = (long) ((*timeout - seconds) * 1e9); 50 | const struct timespec ts = { seconds, nanoseconds }; 51 | const int result = ppoll(fds, count, &ts, NULL); 52 | #elif defined(__NetBSD__) 53 | const time_t seconds = (time_t) *timeout; 54 | const long nanoseconds = (long) ((*timeout - seconds) * 1e9); 55 | const struct timespec ts = { seconds, nanoseconds }; 56 | const int result = pollts(fds, count, &ts, NULL); 57 | #else 58 | const int milliseconds = (int) (*timeout * 1e3); 59 | const int result = poll(fds, count, milliseconds); 60 | #endif 61 | const int error = errno; // clock_gettime may overwrite our error 62 | 63 | *timeout -= (_glfwPlatformGetTimerValue() - base) / 64 | (double) _glfwPlatformGetTimerFrequency(); 65 | 66 | if (result > 0) 67 | return GLFW_TRUE; 68 | else if (result == -1 && error != EINTR && error != EAGAIN) 69 | return GLFW_FALSE; 70 | else if (*timeout <= 0.0) 71 | return GLFW_FALSE; 72 | } 73 | else 74 | { 75 | const int result = poll(fds, count, -1); 76 | if (result > 0) 77 | return GLFW_TRUE; 78 | else if (result == -1 && errno != EINTR && errno != EAGAIN) 79 | return GLFW_FALSE; 80 | } 81 | } 82 | } 83 | 84 | #endif // GLFW_BUILD_POSIX_POLL 85 | 86 | -------------------------------------------------------------------------------- /.github/workflows/build.yml: -------------------------------------------------------------------------------- 1 | name: Build 2 | on: 3 | pull_request: 4 | push: 5 | branches: [ ci, master, latest, 3.3-stable ] 6 | workflow_dispatch: 7 | permissions: 8 | statuses: write 9 | contents: read 10 | 11 | jobs: 12 | build-linux-x11-clang: 13 | name: X11 (Linux, Clang) 14 | runs-on: ubuntu-latest 15 | env: 16 | CC: clang 17 | CFLAGS: -Werror 18 | steps: 19 | - uses: actions/checkout@v3 20 | - name: Install dependencies 21 | run: | 22 | sudo apt update 23 | sudo apt install libxrandr-dev libxinerama-dev libxcursor-dev libxi-dev libxext-dev 24 | 25 | - name: Configure static library 26 | run: cmake -S . -B build-static 27 | - name: Build static library 28 | run: cmake --build build-static --parallel 29 | 30 | - name: Configure shared library 31 | run: cmake -S . -B build-shared -D BUILD_SHARED_LIBS=ON 32 | - name: Build shared library 33 | run: cmake --build build-shared --parallel 34 | 35 | build-linux-full-clang: 36 | name: X11+Wayland (Linux, Clang) 37 | runs-on: ubuntu-latest 38 | env: 39 | CC: clang 40 | CFLAGS: -Werror 41 | steps: 42 | - uses: actions/checkout@v3 43 | - name: Install dependencies 44 | run: | 45 | sudo apt update 46 | sudo apt install libxrandr-dev libxinerama-dev libxcursor-dev libxi-dev libxext-dev wayland-protocols libwayland-dev libxkbcommon-dev 47 | 48 | - name: Configure static library 49 | run: cmake -S . -B build-static -D GLFW_BUILD_WAYLAND=ON 50 | - name: Build static library 51 | run: cmake --build build-static --parallel 52 | 53 | - name: Configure shared library 54 | run: cmake -S . -B build-shared -D GLFW_BUILD_WAYLAND=ON -D BUILD_SHARED_LIBS=ON 55 | - name: Build shared library 56 | run: cmake --build build-shared --parallel 57 | 58 | build-macos-cocoa-clang: 59 | name: Cocoa (macOS, Clang) 60 | runs-on: macos-latest 61 | env: 62 | CFLAGS: -Werror 63 | MACOSX_DEPLOYMENT_TARGET: 10.8 64 | steps: 65 | - uses: actions/checkout@v3 66 | 67 | - name: Configure static library 68 | run: cmake -S . -B build-static 69 | - name: Build static library 70 | run: cmake --build build-static --parallel 71 | 72 | - name: Configure shared library 73 | run: cmake -S . -B build-shared -D BUILD_SHARED_LIBS=ON 74 | - name: Build shared library 75 | run: cmake --build build-shared --parallel 76 | 77 | build-windows-win32-vs2022: 78 | name: Win32 (Windows, VS2022) 79 | runs-on: windows-latest 80 | env: 81 | CFLAGS: /WX 82 | steps: 83 | - uses: actions/checkout@v3 84 | 85 | - name: Configure static library 86 | run: cmake -S . -B build-static -G "Visual Studio 17 2022" 87 | - name: Build static library 88 | run: cmake --build build-static --parallel 89 | 90 | - name: Configure shared library 91 | run: cmake -S . -B build-shared -G "Visual Studio 17 2022" -D BUILD_SHARED_LIBS=ON 92 | - name: Build shared library 93 | run: cmake --build build-shared --parallel 94 | 95 | -------------------------------------------------------------------------------- /examples/windows.c: -------------------------------------------------------------------------------- 1 | //======================================================================== 2 | // Simple multi-window example 3 | // Copyright (c) Camilla Löwy 4 | // 5 | // This software is provided 'as-is', without any express or implied 6 | // warranty. In no event will the authors be held liable for any damages 7 | // arising from the use of this software. 8 | // 9 | // Permission is granted to anyone to use this software for any purpose, 10 | // including commercial applications, and to alter it and redistribute it 11 | // freely, subject to the following restrictions: 12 | // 13 | // 1. The origin of this software must not be misrepresented; you must not 14 | // claim that you wrote the original software. If you use this software 15 | // in a product, an acknowledgment in the product documentation would 16 | // be appreciated but is not required. 17 | // 18 | // 2. Altered source versions must be plainly marked as such, and must not 19 | // be misrepresented as being the original software. 20 | // 21 | // 3. This notice may not be removed or altered from any source 22 | // distribution. 23 | // 24 | //======================================================================== 25 | 26 | #define GLAD_GL_IMPLEMENTATION 27 | #include 28 | #define GLFW_INCLUDE_NONE 29 | #include 30 | 31 | #include 32 | #include 33 | 34 | int main(int argc, char** argv) 35 | { 36 | int xpos, ypos, height; 37 | const char* description; 38 | GLFWwindow* windows[4]; 39 | 40 | if (!glfwInit()) 41 | { 42 | glfwGetError(&description); 43 | printf("Error: %s\n", description); 44 | exit(EXIT_FAILURE); 45 | } 46 | 47 | glfwWindowHint(GLFW_DECORATED, GLFW_FALSE); 48 | 49 | glfwGetMonitorWorkarea(glfwGetPrimaryMonitor(), &xpos, &ypos, NULL, &height); 50 | 51 | for (int i = 0; i < 4; i++) 52 | { 53 | const int size = height / 5; 54 | const struct 55 | { 56 | float r, g, b; 57 | } colors[] = 58 | { 59 | { 0.95f, 0.32f, 0.11f }, 60 | { 0.50f, 0.80f, 0.16f }, 61 | { 0.f, 0.68f, 0.94f }, 62 | { 0.98f, 0.74f, 0.04f } 63 | }; 64 | 65 | if (i > 0) 66 | glfwWindowHint(GLFW_FOCUS_ON_SHOW, GLFW_FALSE); 67 | 68 | glfwWindowHint(GLFW_POSITION_X, xpos + size * (1 + (i & 1))); 69 | glfwWindowHint(GLFW_POSITION_Y, ypos + size * (1 + (i >> 1))); 70 | 71 | windows[i] = glfwCreateWindow(size, size, "Multi-Window Example", NULL, NULL); 72 | if (!windows[i]) 73 | { 74 | glfwGetError(&description); 75 | printf("Error: %s\n", description); 76 | glfwTerminate(); 77 | exit(EXIT_FAILURE); 78 | } 79 | 80 | glfwSetInputMode(windows[i], GLFW_STICKY_KEYS, GLFW_TRUE); 81 | 82 | glfwMakeContextCurrent(windows[i]); 83 | gladLoadGL(glfwGetProcAddress); 84 | glClearColor(colors[i].r, colors[i].g, colors[i].b, 1.f); 85 | } 86 | 87 | for (;;) 88 | { 89 | for (int i = 0; i < 4; i++) 90 | { 91 | glfwMakeContextCurrent(windows[i]); 92 | glClear(GL_COLOR_BUFFER_BIT); 93 | glfwSwapBuffers(windows[i]); 94 | 95 | if (glfwWindowShouldClose(windows[i]) || 96 | glfwGetKey(windows[i], GLFW_KEY_ESCAPE)) 97 | { 98 | glfwTerminate(); 99 | exit(EXIT_SUCCESS); 100 | } 101 | } 102 | 103 | glfwWaitEvents(); 104 | } 105 | } 106 | 107 | -------------------------------------------------------------------------------- /deps/mingw/_mingw_dxhelper.h: -------------------------------------------------------------------------------- 1 | /** 2 | * This file has no copyright assigned and is placed in the Public Domain. 3 | * This file is part of the mingw-w64 runtime package. 4 | * No warranty is given; refer to the file DISCLAIMER within this package. 5 | */ 6 | 7 | #if defined(_MSC_VER) && !defined(_MSC_EXTENSIONS) 8 | #define NONAMELESSUNION 1 9 | #endif 10 | #if defined(NONAMELESSSTRUCT) && \ 11 | !defined(NONAMELESSUNION) 12 | #define NONAMELESSUNION 1 13 | #endif 14 | #if defined(NONAMELESSUNION) && \ 15 | !defined(NONAMELESSSTRUCT) 16 | #define NONAMELESSSTRUCT 1 17 | #endif 18 | #if !defined(__GNU_EXTENSION) 19 | #if defined(__GNUC__) || defined(__GNUG__) 20 | #define __GNU_EXTENSION __extension__ 21 | #else 22 | #define __GNU_EXTENSION 23 | #endif 24 | #endif /* __extension__ */ 25 | 26 | #ifndef __ANONYMOUS_DEFINED 27 | #define __ANONYMOUS_DEFINED 28 | #if defined(__GNUC__) || defined(__GNUG__) 29 | #define _ANONYMOUS_UNION __extension__ 30 | #define _ANONYMOUS_STRUCT __extension__ 31 | #else 32 | #define _ANONYMOUS_UNION 33 | #define _ANONYMOUS_STRUCT 34 | #endif 35 | #ifndef NONAMELESSUNION 36 | #define _UNION_NAME(x) 37 | #define _STRUCT_NAME(x) 38 | #else /* NONAMELESSUNION */ 39 | #define _UNION_NAME(x) x 40 | #define _STRUCT_NAME(x) x 41 | #endif 42 | #endif /* __ANONYMOUS_DEFINED */ 43 | 44 | #ifndef DUMMYUNIONNAME 45 | # ifdef NONAMELESSUNION 46 | # define DUMMYUNIONNAME u 47 | # define DUMMYUNIONNAME1 u1 /* Wine uses this variant */ 48 | # define DUMMYUNIONNAME2 u2 49 | # define DUMMYUNIONNAME3 u3 50 | # define DUMMYUNIONNAME4 u4 51 | # define DUMMYUNIONNAME5 u5 52 | # define DUMMYUNIONNAME6 u6 53 | # define DUMMYUNIONNAME7 u7 54 | # define DUMMYUNIONNAME8 u8 55 | # define DUMMYUNIONNAME9 u9 56 | # else /* NONAMELESSUNION */ 57 | # define DUMMYUNIONNAME 58 | # define DUMMYUNIONNAME1 /* Wine uses this variant */ 59 | # define DUMMYUNIONNAME2 60 | # define DUMMYUNIONNAME3 61 | # define DUMMYUNIONNAME4 62 | # define DUMMYUNIONNAME5 63 | # define DUMMYUNIONNAME6 64 | # define DUMMYUNIONNAME7 65 | # define DUMMYUNIONNAME8 66 | # define DUMMYUNIONNAME9 67 | # endif 68 | #endif /* DUMMYUNIONNAME */ 69 | 70 | #if !defined(DUMMYUNIONNAME1) /* MinGW does not define this one */ 71 | # ifdef NONAMELESSUNION 72 | # define DUMMYUNIONNAME1 u1 /* Wine uses this variant */ 73 | # else 74 | # define DUMMYUNIONNAME1 /* Wine uses this variant */ 75 | # endif 76 | #endif /* DUMMYUNIONNAME1 */ 77 | 78 | #ifndef DUMMYSTRUCTNAME 79 | # ifdef NONAMELESSUNION 80 | # define DUMMYSTRUCTNAME s 81 | # define DUMMYSTRUCTNAME1 s1 /* Wine uses this variant */ 82 | # define DUMMYSTRUCTNAME2 s2 83 | # define DUMMYSTRUCTNAME3 s3 84 | # define DUMMYSTRUCTNAME4 s4 85 | # define DUMMYSTRUCTNAME5 s5 86 | # else 87 | # define DUMMYSTRUCTNAME 88 | # define DUMMYSTRUCTNAME1 /* Wine uses this variant */ 89 | # define DUMMYSTRUCTNAME2 90 | # define DUMMYSTRUCTNAME3 91 | # define DUMMYSTRUCTNAME4 92 | # define DUMMYSTRUCTNAME5 93 | # endif 94 | #endif /* DUMMYSTRUCTNAME */ 95 | 96 | /* These are for compatibility with the Wine source tree */ 97 | 98 | #ifndef WINELIB_NAME_AW 99 | # ifdef __MINGW_NAME_AW 100 | # define WINELIB_NAME_AW __MINGW_NAME_AW 101 | # else 102 | # ifdef UNICODE 103 | # define WINELIB_NAME_AW(func) func##W 104 | # else 105 | # define WINELIB_NAME_AW(func) func##A 106 | # endif 107 | # endif 108 | #endif /* WINELIB_NAME_AW */ 109 | 110 | #ifndef DECL_WINELIB_TYPE_AW 111 | # ifdef __MINGW_TYPEDEF_AW 112 | # define DECL_WINELIB_TYPE_AW __MINGW_TYPEDEF_AW 113 | # else 114 | # define DECL_WINELIB_TYPE_AW(type) typedef WINELIB_NAME_AW(type) type; 115 | # endif 116 | #endif /* DECL_WINELIB_TYPE_AW */ 117 | 118 | -------------------------------------------------------------------------------- /src/win32_thread.c: -------------------------------------------------------------------------------- 1 | //======================================================================== 2 | // GLFW 3.4 Win32 - www.glfw.org 3 | //------------------------------------------------------------------------ 4 | // Copyright (c) 2002-2006 Marcus Geelnard 5 | // Copyright (c) 2006-2017 Camilla Löwy 6 | // 7 | // This software is provided 'as-is', without any express or implied 8 | // warranty. In no event will the authors be held liable for any damages 9 | // arising from the use of this software. 10 | // 11 | // Permission is granted to anyone to use this software for any purpose, 12 | // including commercial applications, and to alter it and redistribute it 13 | // freely, subject to the following restrictions: 14 | // 15 | // 1. The origin of this software must not be misrepresented; you must not 16 | // claim that you wrote the original software. If you use this software 17 | // in a product, an acknowledgment in the product documentation would 18 | // be appreciated but is not required. 19 | // 20 | // 2. Altered source versions must be plainly marked as such, and must not 21 | // be misrepresented as being the original software. 22 | // 23 | // 3. This notice may not be removed or altered from any source 24 | // distribution. 25 | // 26 | //======================================================================== 27 | // Please use C89 style variable declarations in this file because VS 2010 28 | //======================================================================== 29 | 30 | #include "internal.h" 31 | 32 | #if defined(GLFW_BUILD_WIN32_THREAD) 33 | 34 | #include 35 | 36 | 37 | ////////////////////////////////////////////////////////////////////////// 38 | ////// GLFW platform API ////// 39 | ////////////////////////////////////////////////////////////////////////// 40 | 41 | GLFWbool _glfwPlatformCreateTls(_GLFWtls* tls) 42 | { 43 | assert(tls->win32.allocated == GLFW_FALSE); 44 | 45 | tls->win32.index = TlsAlloc(); 46 | if (tls->win32.index == TLS_OUT_OF_INDEXES) 47 | { 48 | _glfwInputError(GLFW_PLATFORM_ERROR, "Win32: Failed to allocate TLS index"); 49 | return GLFW_FALSE; 50 | } 51 | 52 | tls->win32.allocated = GLFW_TRUE; 53 | return GLFW_TRUE; 54 | } 55 | 56 | void _glfwPlatformDestroyTls(_GLFWtls* tls) 57 | { 58 | if (tls->win32.allocated) 59 | TlsFree(tls->win32.index); 60 | memset(tls, 0, sizeof(_GLFWtls)); 61 | } 62 | 63 | void* _glfwPlatformGetTls(_GLFWtls* tls) 64 | { 65 | assert(tls->win32.allocated == GLFW_TRUE); 66 | return TlsGetValue(tls->win32.index); 67 | } 68 | 69 | void _glfwPlatformSetTls(_GLFWtls* tls, void* value) 70 | { 71 | assert(tls->win32.allocated == GLFW_TRUE); 72 | TlsSetValue(tls->win32.index, value); 73 | } 74 | 75 | GLFWbool _glfwPlatformCreateMutex(_GLFWmutex* mutex) 76 | { 77 | assert(mutex->win32.allocated == GLFW_FALSE); 78 | InitializeCriticalSection(&mutex->win32.section); 79 | return mutex->win32.allocated = GLFW_TRUE; 80 | } 81 | 82 | void _glfwPlatformDestroyMutex(_GLFWmutex* mutex) 83 | { 84 | if (mutex->win32.allocated) 85 | DeleteCriticalSection(&mutex->win32.section); 86 | memset(mutex, 0, sizeof(_GLFWmutex)); 87 | } 88 | 89 | void _glfwPlatformLockMutex(_GLFWmutex* mutex) 90 | { 91 | assert(mutex->win32.allocated == GLFW_TRUE); 92 | EnterCriticalSection(&mutex->win32.section); 93 | } 94 | 95 | void _glfwPlatformUnlockMutex(_GLFWmutex* mutex) 96 | { 97 | assert(mutex->win32.allocated == GLFW_TRUE); 98 | LeaveCriticalSection(&mutex->win32.section); 99 | } 100 | 101 | #endif // GLFW_BUILD_WIN32_THREAD 102 | 103 | -------------------------------------------------------------------------------- /src/posix_thread.c: -------------------------------------------------------------------------------- 1 | //======================================================================== 2 | // GLFW 3.4 POSIX - www.glfw.org 3 | //------------------------------------------------------------------------ 4 | // Copyright (c) 2002-2006 Marcus Geelnard 5 | // Copyright (c) 2006-2017 Camilla Löwy 6 | // 7 | // This software is provided 'as-is', without any express or implied 8 | // warranty. In no event will the authors be held liable for any damages 9 | // arising from the use of this software. 10 | // 11 | // Permission is granted to anyone to use this software for any purpose, 12 | // including commercial applications, and to alter it and redistribute it 13 | // freely, subject to the following restrictions: 14 | // 15 | // 1. The origin of this software must not be misrepresented; you must not 16 | // claim that you wrote the original software. If you use this software 17 | // in a product, an acknowledgment in the product documentation would 18 | // be appreciated but is not required. 19 | // 20 | // 2. Altered source versions must be plainly marked as such, and must not 21 | // be misrepresented as being the original software. 22 | // 23 | // 3. This notice may not be removed or altered from any source 24 | // distribution. 25 | // 26 | //======================================================================== 27 | // It is fine to use C99 in this file because it will not be built with VS 28 | //======================================================================== 29 | 30 | #include "internal.h" 31 | 32 | #if defined(GLFW_BUILD_POSIX_THREAD) 33 | 34 | #include 35 | #include 36 | 37 | 38 | ////////////////////////////////////////////////////////////////////////// 39 | ////// GLFW platform API ////// 40 | ////////////////////////////////////////////////////////////////////////// 41 | 42 | GLFWbool _glfwPlatformCreateTls(_GLFWtls* tls) 43 | { 44 | assert(tls->posix.allocated == GLFW_FALSE); 45 | 46 | if (pthread_key_create(&tls->posix.key, NULL) != 0) 47 | { 48 | _glfwInputError(GLFW_PLATFORM_ERROR, 49 | "POSIX: Failed to create context TLS"); 50 | return GLFW_FALSE; 51 | } 52 | 53 | tls->posix.allocated = GLFW_TRUE; 54 | return GLFW_TRUE; 55 | } 56 | 57 | void _glfwPlatformDestroyTls(_GLFWtls* tls) 58 | { 59 | if (tls->posix.allocated) 60 | pthread_key_delete(tls->posix.key); 61 | memset(tls, 0, sizeof(_GLFWtls)); 62 | } 63 | 64 | void* _glfwPlatformGetTls(_GLFWtls* tls) 65 | { 66 | assert(tls->posix.allocated == GLFW_TRUE); 67 | return pthread_getspecific(tls->posix.key); 68 | } 69 | 70 | void _glfwPlatformSetTls(_GLFWtls* tls, void* value) 71 | { 72 | assert(tls->posix.allocated == GLFW_TRUE); 73 | pthread_setspecific(tls->posix.key, value); 74 | } 75 | 76 | GLFWbool _glfwPlatformCreateMutex(_GLFWmutex* mutex) 77 | { 78 | assert(mutex->posix.allocated == GLFW_FALSE); 79 | 80 | if (pthread_mutex_init(&mutex->posix.handle, NULL) != 0) 81 | { 82 | _glfwInputError(GLFW_PLATFORM_ERROR, "POSIX: Failed to create mutex"); 83 | return GLFW_FALSE; 84 | } 85 | 86 | return mutex->posix.allocated = GLFW_TRUE; 87 | } 88 | 89 | void _glfwPlatformDestroyMutex(_GLFWmutex* mutex) 90 | { 91 | if (mutex->posix.allocated) 92 | pthread_mutex_destroy(&mutex->posix.handle); 93 | memset(mutex, 0, sizeof(_GLFWmutex)); 94 | } 95 | 96 | void _glfwPlatformLockMutex(_GLFWmutex* mutex) 97 | { 98 | assert(mutex->posix.allocated == GLFW_TRUE); 99 | pthread_mutex_lock(&mutex->posix.handle); 100 | } 101 | 102 | void _glfwPlatformUnlockMutex(_GLFWmutex* mutex) 103 | { 104 | assert(mutex->posix.allocated == GLFW_TRUE); 105 | pthread_mutex_unlock(&mutex->posix.handle); 106 | } 107 | 108 | #endif // GLFW_BUILD_POSIX_THREAD 109 | 110 | -------------------------------------------------------------------------------- /examples/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | 2 | link_libraries(glfw) 3 | 4 | include_directories("${GLFW_SOURCE_DIR}/deps") 5 | 6 | if (MATH_LIBRARY) 7 | link_libraries("${MATH_LIBRARY}") 8 | endif() 9 | 10 | # Workaround for the MS CRT deprecating parts of the standard library 11 | if (MSVC OR CMAKE_C_SIMULATE_ID STREQUAL "MSVC") 12 | add_definitions(-D_CRT_SECURE_NO_WARNINGS) 13 | endif() 14 | 15 | if (WIN32) 16 | set(ICON glfw.rc) 17 | elseif (APPLE) 18 | set(ICON glfw.icns) 19 | endif() 20 | 21 | set(GLAD_GL "${GLFW_SOURCE_DIR}/deps/glad/gl.h") 22 | set(GLAD_GLES2 "${GLFW_SOURCE_DIR}/deps/glad/gles2.h") 23 | set(GETOPT "${GLFW_SOURCE_DIR}/deps/getopt.h" 24 | "${GLFW_SOURCE_DIR}/deps/getopt.c") 25 | set(TINYCTHREAD "${GLFW_SOURCE_DIR}/deps/tinycthread.h" 26 | "${GLFW_SOURCE_DIR}/deps/tinycthread.c") 27 | 28 | add_executable(boing WIN32 MACOSX_BUNDLE boing.c ${ICON} ${GLAD_GL}) 29 | add_executable(gears WIN32 MACOSX_BUNDLE gears.c ${ICON} ${GLAD_GL}) 30 | add_executable(heightmap WIN32 MACOSX_BUNDLE heightmap.c ${ICON} ${GLAD_GL}) 31 | add_executable(offscreen offscreen.c ${ICON} ${GLAD_GL}) 32 | add_executable(particles WIN32 MACOSX_BUNDLE particles.c ${ICON} ${TINYCTHREAD} ${GETOPT} ${GLAD_GL}) 33 | add_executable(sharing WIN32 MACOSX_BUNDLE sharing.c ${ICON} ${GLAD_GL}) 34 | add_executable(splitview WIN32 MACOSX_BUNDLE splitview.c ${ICON} ${GLAD_GL}) 35 | add_executable(triangle-opengl WIN32 MACOSX_BUNDLE triangle-opengl.c ${ICON} ${GLAD_GL}) 36 | add_executable(triangle-opengles WIN32 MACOSX_BUNDLE triangle-opengles.c ${ICON} ${GLAD_GLES2}) 37 | add_executable(wave WIN32 MACOSX_BUNDLE wave.c ${ICON} ${GLAD_GL}) 38 | add_executable(windows WIN32 MACOSX_BUNDLE windows.c ${ICON} ${GLAD_GL}) 39 | 40 | target_link_libraries(particles Threads::Threads) 41 | if (RT_LIBRARY) 42 | target_link_libraries(particles "${RT_LIBRARY}") 43 | endif() 44 | 45 | set(GUI_ONLY_BINARIES boing gears heightmap particles sharing splitview 46 | triangle-opengl triangle-opengles wave windows) 47 | set(CONSOLE_BINARIES offscreen) 48 | 49 | set_target_properties(${GUI_ONLY_BINARIES} ${CONSOLE_BINARIES} PROPERTIES 50 | C_STANDARD 99 51 | FOLDER "GLFW3/Examples") 52 | 53 | if (MSVC) 54 | # Tell MSVC to use main instead of WinMain 55 | set_target_properties(${GUI_ONLY_BINARIES} PROPERTIES 56 | LINK_FLAGS "/ENTRY:mainCRTStartup") 57 | elseif (CMAKE_C_SIMULATE_ID STREQUAL "MSVC") 58 | # Tell Clang using MS CRT to use main instead of WinMain 59 | set_target_properties(${GUI_ONLY_BINARIES} PROPERTIES 60 | LINK_FLAGS "-Wl,/entry:mainCRTStartup") 61 | endif() 62 | 63 | if (APPLE) 64 | set_target_properties(boing PROPERTIES MACOSX_BUNDLE_BUNDLE_NAME "Boing") 65 | set_target_properties(gears PROPERTIES MACOSX_BUNDLE_BUNDLE_NAME "Gears") 66 | set_target_properties(heightmap PROPERTIES MACOSX_BUNDLE_BUNDLE_NAME "Heightmap") 67 | set_target_properties(particles PROPERTIES MACOSX_BUNDLE_BUNDLE_NAME "Particles") 68 | set_target_properties(sharing PROPERTIES MACOSX_BUNDLE_BUNDLE_NAME "Sharing") 69 | set_target_properties(triangle-opengl PROPERTIES MACOSX_BUNDLE_BUNDLE_NAME "OpenGL Triangle") 70 | set_target_properties(triangle-opengles PROPERTIES MACOSX_BUNDLE_BUNDLE_NAME "OpenGL ES Triangle") 71 | set_target_properties(splitview PROPERTIES MACOSX_BUNDLE_BUNDLE_NAME "SplitView") 72 | set_target_properties(wave PROPERTIES MACOSX_BUNDLE_BUNDLE_NAME "Wave") 73 | set_target_properties(windows PROPERTIES MACOSX_BUNDLE_BUNDLE_NAME "Windows") 74 | 75 | set_source_files_properties(glfw.icns PROPERTIES 76 | MACOSX_PACKAGE_LOCATION "Resources") 77 | set_target_properties(${GUI_ONLY_BINARIES} PROPERTIES 78 | MACOSX_BUNDLE_SHORT_VERSION_STRING ${GLFW_VERSION} 79 | MACOSX_BUNDLE_LONG_VERSION_STRING ${GLFW_VERSION} 80 | MACOSX_BUNDLE_ICON_FILE glfw.icns 81 | MACOSX_BUNDLE_INFO_PLIST "${GLFW_SOURCE_DIR}/CMake/Info.plist.in") 82 | endif() 83 | 84 | -------------------------------------------------------------------------------- /tests/empty.c: -------------------------------------------------------------------------------- 1 | //======================================================================== 2 | // Empty event test 3 | // Copyright (c) Camilla Löwy 4 | // 5 | // This software is provided 'as-is', without any express or implied 6 | // warranty. In no event will the authors be held liable for any damages 7 | // arising from the use of this software. 8 | // 9 | // Permission is granted to anyone to use this software for any purpose, 10 | // including commercial applications, and to alter it and redistribute it 11 | // freely, subject to the following restrictions: 12 | // 13 | // 1. The origin of this software must not be misrepresented; you must not 14 | // claim that you wrote the original software. If you use this software 15 | // in a product, an acknowledgment in the product documentation would 16 | // be appreciated but is not required. 17 | // 18 | // 2. Altered source versions must be plainly marked as such, and must not 19 | // be misrepresented as being the original software. 20 | // 21 | // 3. This notice may not be removed or altered from any source 22 | // distribution. 23 | // 24 | //======================================================================== 25 | // 26 | // This test is intended to verify that posting of empty events works 27 | // 28 | //======================================================================== 29 | 30 | #include "tinycthread.h" 31 | 32 | #define GLAD_GL_IMPLEMENTATION 33 | #include 34 | #define GLFW_INCLUDE_NONE 35 | #include 36 | 37 | #include 38 | #include 39 | #include 40 | 41 | static volatile int running = GLFW_TRUE; 42 | 43 | static void error_callback(int error, const char* description) 44 | { 45 | fprintf(stderr, "Error: %s\n", description); 46 | } 47 | 48 | static int thread_main(void* data) 49 | { 50 | struct timespec time; 51 | 52 | while (running) 53 | { 54 | clock_gettime(CLOCK_REALTIME, &time); 55 | time.tv_sec += 1; 56 | thrd_sleep(&time, NULL); 57 | 58 | glfwPostEmptyEvent(); 59 | } 60 | 61 | return 0; 62 | } 63 | 64 | static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) 65 | { 66 | if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) 67 | glfwSetWindowShouldClose(window, GLFW_TRUE); 68 | } 69 | 70 | static float nrand(void) 71 | { 72 | return (float) rand() / (float) RAND_MAX; 73 | } 74 | 75 | int main(void) 76 | { 77 | int result; 78 | thrd_t thread; 79 | GLFWwindow* window; 80 | 81 | srand((unsigned int) time(NULL)); 82 | 83 | glfwSetErrorCallback(error_callback); 84 | 85 | if (!glfwInit()) 86 | exit(EXIT_FAILURE); 87 | 88 | window = glfwCreateWindow(640, 480, "Empty Event Test", NULL, NULL); 89 | if (!window) 90 | { 91 | glfwTerminate(); 92 | exit(EXIT_FAILURE); 93 | } 94 | 95 | glfwMakeContextCurrent(window); 96 | gladLoadGL(glfwGetProcAddress); 97 | glfwSetKeyCallback(window, key_callback); 98 | 99 | if (thrd_create(&thread, thread_main, NULL) != thrd_success) 100 | { 101 | fprintf(stderr, "Failed to create secondary thread\n"); 102 | 103 | glfwTerminate(); 104 | exit(EXIT_FAILURE); 105 | } 106 | 107 | while (running) 108 | { 109 | int width, height; 110 | float r = nrand(), g = nrand(), b = nrand(); 111 | float l = (float) sqrt(r * r + g * g + b * b); 112 | 113 | glfwGetFramebufferSize(window, &width, &height); 114 | 115 | glViewport(0, 0, width, height); 116 | glClearColor(r / l, g / l, b / l, 1.f); 117 | glClear(GL_COLOR_BUFFER_BIT); 118 | glfwSwapBuffers(window); 119 | 120 | glfwWaitEvents(); 121 | 122 | if (glfwWindowShouldClose(window)) 123 | running = GLFW_FALSE; 124 | } 125 | 126 | glfwHideWindow(window); 127 | thrd_join(thread, &result); 128 | glfwDestroyWindow(window); 129 | 130 | glfwTerminate(); 131 | exit(EXIT_SUCCESS); 132 | } 133 | 134 | -------------------------------------------------------------------------------- /tests/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | 2 | link_libraries(glfw) 3 | 4 | include_directories("${GLFW_SOURCE_DIR}/deps") 5 | 6 | if (MATH_LIBRARY) 7 | link_libraries("${MATH_LIBRARY}") 8 | endif() 9 | 10 | # Workaround for the MS CRT deprecating parts of the standard library 11 | if (MSVC OR CMAKE_C_SIMULATE_ID STREQUAL "MSVC") 12 | add_definitions(-D_CRT_SECURE_NO_WARNINGS) 13 | endif() 14 | 15 | set(GLAD_GL "${GLFW_SOURCE_DIR}/deps/glad/gl.h") 16 | set(GLAD_VULKAN "${GLFW_SOURCE_DIR}/deps/glad/vulkan.h") 17 | set(GETOPT "${GLFW_SOURCE_DIR}/deps/getopt.h" 18 | "${GLFW_SOURCE_DIR}/deps/getopt.c") 19 | set(TINYCTHREAD "${GLFW_SOURCE_DIR}/deps/tinycthread.h" 20 | "${GLFW_SOURCE_DIR}/deps/tinycthread.c") 21 | 22 | add_executable(allocator allocator.c ${GLAD_GL}) 23 | add_executable(clipboard clipboard.c ${GETOPT} ${GLAD_GL}) 24 | add_executable(events events.c ${GETOPT} ${GLAD_GL}) 25 | add_executable(msaa msaa.c ${GETOPT} ${GLAD_GL}) 26 | add_executable(glfwinfo glfwinfo.c ${GETOPT} ${GLAD_GL} ${GLAD_VULKAN}) 27 | add_executable(iconify iconify.c ${GETOPT} ${GLAD_GL}) 28 | add_executable(monitors monitors.c ${GETOPT} ${GLAD_GL}) 29 | add_executable(reopen reopen.c ${GLAD_GL}) 30 | add_executable(cursor cursor.c ${GLAD_GL}) 31 | 32 | add_executable(empty WIN32 MACOSX_BUNDLE empty.c ${TINYCTHREAD} ${GLAD_GL}) 33 | add_executable(gamma WIN32 MACOSX_BUNDLE gamma.c ${GLAD_GL}) 34 | add_executable(icon WIN32 MACOSX_BUNDLE icon.c ${GLAD_GL}) 35 | add_executable(inputlag WIN32 MACOSX_BUNDLE inputlag.c ${GETOPT} ${GLAD_GL}) 36 | add_executable(joysticks WIN32 MACOSX_BUNDLE joysticks.c ${GLAD_GL}) 37 | add_executable(tearing WIN32 MACOSX_BUNDLE tearing.c ${GLAD_GL}) 38 | add_executable(threads WIN32 MACOSX_BUNDLE threads.c ${TINYCTHREAD} ${GLAD_GL}) 39 | add_executable(timeout WIN32 MACOSX_BUNDLE timeout.c ${GLAD_GL}) 40 | add_executable(title WIN32 MACOSX_BUNDLE title.c ${GLAD_GL}) 41 | add_executable(triangle-vulkan WIN32 triangle-vulkan.c ${GLAD_VULKAN}) 42 | add_executable(window WIN32 MACOSX_BUNDLE window.c ${GLAD_GL}) 43 | 44 | target_link_libraries(empty Threads::Threads) 45 | target_link_libraries(threads Threads::Threads) 46 | if (RT_LIBRARY) 47 | target_link_libraries(empty "${RT_LIBRARY}") 48 | target_link_libraries(threads "${RT_LIBRARY}") 49 | endif() 50 | 51 | set(GUI_ONLY_BINARIES empty gamma icon inputlag joysticks tearing threads 52 | timeout title triangle-vulkan window) 53 | set(CONSOLE_BINARIES allocator clipboard events msaa glfwinfo iconify monitors 54 | reopen cursor) 55 | 56 | set_target_properties(${GUI_ONLY_BINARIES} ${CONSOLE_BINARIES} PROPERTIES 57 | C_STANDARD 99 58 | FOLDER "GLFW3/Tests") 59 | 60 | if (MSVC) 61 | # Tell MSVC to use main instead of WinMain 62 | set_target_properties(${GUI_ONLY_BINARIES} PROPERTIES 63 | LINK_FLAGS "/ENTRY:mainCRTStartup") 64 | elseif (CMAKE_C_SIMULATE_ID STREQUAL "MSVC") 65 | # Tell Clang using MS CRT to use main instead of WinMain 66 | set_target_properties(${GUI_ONLY_BINARIES} PROPERTIES 67 | LINK_FLAGS "-Wl,/entry:mainCRTStartup") 68 | endif() 69 | 70 | if (APPLE) 71 | set_target_properties(empty PROPERTIES MACOSX_BUNDLE_BUNDLE_NAME "Empty Event") 72 | set_target_properties(gamma PROPERTIES MACOSX_BUNDLE_BUNDLE_NAME "Gamma") 73 | set_target_properties(inputlag PROPERTIES MACOSX_BUNDLE_BUNDLE_NAME "Input Lag") 74 | set_target_properties(joysticks PROPERTIES MACOSX_BUNDLE_BUNDLE_NAME "Joysticks") 75 | set_target_properties(tearing PROPERTIES MACOSX_BUNDLE_BUNDLE_NAME "Tearing") 76 | set_target_properties(threads PROPERTIES MACOSX_BUNDLE_BUNDLE_NAME "Threads") 77 | set_target_properties(timeout PROPERTIES MACOSX_BUNDLE_BUNDLE_NAME "Timeout") 78 | set_target_properties(title PROPERTIES MACOSX_BUNDLE_BUNDLE_NAME "Title") 79 | set_target_properties(window PROPERTIES MACOSX_BUNDLE_BUNDLE_NAME "Window") 80 | 81 | set_target_properties(${GUI_ONLY_BINARIES} PROPERTIES 82 | MACOSX_BUNDLE_SHORT_VERSION_STRING ${GLFW_VERSION} 83 | MACOSX_BUNDLE_LONG_VERSION_STRING ${GLFW_VERSION} 84 | MACOSX_BUNDLE_INFO_PLIST "${GLFW_SOURCE_DIR}/CMake/Info.plist.in") 85 | endif() 86 | 87 | -------------------------------------------------------------------------------- /docs/extra.css.map: -------------------------------------------------------------------------------- 1 | { 2 | "version": 3, 3 | "mappings": "AA8EA,2GAA4G,CAC3G,UAAU,CAAC,IAAI,CACf,WAAW,CAAC,IAAI,CAGjB,wBAAyB,CACxB,YAAY,CAAC,2CAAsD,CAGpE,4HAA6H,CAC5H,YAAY,CAAC,wCAAuD,CAGrE,wIAAyI,CACxI,YAAY,CAAC,wCAAuD,CAGrE,kBAAmB,CAClB,UAAU,CA9EgB,IAAa,CA+EvC,WAAW,CAAC,IAAI,CAGjB,sBAAuB,CACtB,KAAK,CAzFe,OAAa,CA0FjC,WAAW,CAAC,IAAI,CAGjB,4UAA6U,CAC5U,UAAU,CAAC,IAAI,CAGhB,kJAAmJ,CAClJ,MAAM,CAAC,IAAI,CAGZ,wHAAyH,CACxH,WAAW,CAAC,IAAI,CAGjB,qBAAsB,CACrB,UAAU,CAAC,IAAI,CAGhB,2LAA4L,CAC3L,OAAO,CAAC,CAAC,CAGV,wCAAyC,CACxC,OAAO,CAAC,IAAI,CAGb,iMAAkM,CACjM,UAAU,CApGW,OAA+B,CAuGrD,IAAK,CACJ,KAAK,CA1He,OAAa,CA6HlC,SAAU,CACN,SAAS,CAAE,IAAI,CACf,MAAM,CAAE,aAAa,CAGzB,qDAAsD,CACrD,KAAK,CApHU,OAAa,CAqH5B,aAAa,CAAC,IAAI,CAGnB,EAAG,CACF,WAAW,CAAC,KAAK,CACjB,SAAS,CAAC,IAAI,CAGf,EAAG,CACF,WAAW,CAAC,KAAK,CACjB,aAAa,CAAC,CAAC,CACf,SAAS,CAAC,IAAI,CAGf,EAAG,CACF,WAAW,CAAC,KAAK,CACjB,aAAa,CAAC,CAAC,CACf,SAAS,CAAC,IAAI,CAGf,WAAY,CACX,SAAS,CAAC,IAAI,CACd,UAAU,CAAC,IAAI,CACf,SAAS,CAAC,KAAK,CACf,OAAO,CAAC,MAAM,CACd,MAAM,CAAC,MAAM,CAEb,OAAO,CAAE,IAAI,CACb,cAAc,CAAE,GAAG,CACnB,SAAS,CAAE,IAAI,CACf,eAAe,CAAE,UAAU,CAC3B,WAAW,CAAE,MAAM,CACnB,aAAa,CAAE,OAAO,CAGvB,SAAU,CACT,WAAW,CAAC,IAAI,CAChB,aAAa,CAAC,IAAI,CAClB,KAAK,CApKqB,IAAa,CAqKvC,SAAS,CAAC,KAAK,CACf,UAAU,CAAC,yDAAyD,CAGrE,WAAY,CACX,eAAe,CAAC,IAAI,CACpB,MAAM,CAAC,UAAU,CACjB,KAAK,CAAC,KAAK,CAGZ,wBAAyB,CACxB,KAAK,CAAC,IAAI,CAGX,mCAAoC,CACnC,WAAW,CAAC,IAAI,CAChB,WAAW,CAAC,GAAG,CACf,OAAO,CAAC,KAAK,CACb,KAAK,CAvLqB,IAAa,CA0LxC,WAAY,CACX,YAAY,CAAE,CAAC,CAGhB,6CAA8C,CAC7C,UAAU,CAAC,SAAS,CAGrB,kBAAmB,CAClB,KAAK,CAnMqB,IAAa,CAsMxC,cAAe,CACd,UAAU,CAAC,MAAM,CACjB,OAAO,CAAC,GAAG,CACX,UAAU,CAAC,GAAG,CAGf,IAAK,CACJ,UAAU,CA7MgB,IAAa,CAgNxC,SAAU,CACT,SAAS,CAAC,KAAK,CACf,MAAM,CAAC,MAAM,CACb,SAAS,CAAC,IAAI,CAGf,UAAW,CACV,SAAS,CAAC,KAAK,CACf,MAAM,CAAC,MAAM,CACb,SAAS,CAAC,IAAI,CAGf,SAAU,CACT,OAAO,CAAC,IAAI,CAGb,kBAAmB,CAClB,WAAW,CAAC,IAAI,CAChB,WAAW,CAAC,IAAI,CAGjB,UAAW,CACV,UAAU,CAAC,IAAI,CACf,OAAO,CAAE,IAAI,CACb,cAAc,CAAE,GAAG,CACnB,SAAS,CAAE,IAAI,CACf,eAAe,CAAE,UAAU,CAC3B,WAAW,CAAE,MAAM,CACnB,aAAa,CAAE,OAAO,CAGvB,kEAAmE,CAClE,KAAK,CApOgB,OAA+B,CAuOrD,+BAAgC,CAC/B,KAAK,CA1Pe,OAAa,CA6PlC,qCAAsC,CACrC,KAAK,CA1NoB,IAAsB,CA6NhD,wBAA2B,CAC1B,MAAM,CAAE,UAAU,CAGnB,SAAU,CACT,UAAU,CAAC,KAAK,CAGjB,uBAAwB,CACvB,SAAS,CAAC,KAAK,CACf,MAAM,CAAC,MAAM,CACb,OAAO,CAAC,MAAM,CACd,UAAU,CAAC,SAA8B,CAG1C,sDAAuD,CACtD,UAAU,CAAC,iDAAoF,CAC/F,UAAU,CAAC,mBAAuC,CAClD,WAAW,CAAC,kBAAgD,CAC5D,UAAU,CAAC,IAAI,CACf,KAAK,CAlPa,IAAe,CAqPlC,kBAAmB,CAClB,KAAK,CArPoB,IAAsB,CAsP/C,OAAO,CAAC,IAAI,CACZ,aAAa,CAAC,GAAG,CACjB,gBAAgB,CAAC,OAAiC,CAGnD,OAAQ,CACP,KAAK,CAAC,KAAK,CACX,KAAK,CAAC,GAAG,CAGV,oCAAoC,CACnC,OAAQ,CACP,KAAK,CAAC,IAAI,CACV,KAAK,CAAC,OAAO,CACb,MAAM,CAAC,CAAC,EAIV,UAAW,CACV,SAAS,CAAC,MAAM,CAGjB,UAAW,CACV,YAAY,CAAC,KAAK,CAGnB,UAAW,CACV,SAAS,CAAC,GAAG,CACb,YAAY,CAAC,CAAC,CACd,eAAe,CAAC,IAAI,CAIjB,mCAAqB,CACjB,WAAW,CAAC,KAAK,CAIzB,mCAAoC,CACnC,UAAU,CAAC,oDAAgF,CAC3F,UAAU,CAAC,sBAAqC,CAChD,WAAW,CAAC,cAA8C,CAC1D,KAAK,CArTU,OAAa,CAsT5B,MAAM,CAAC,iBAAgC,CACvC,aAAa,CAAC,GAAG,CAGlB,UAAW,CACV,KAAK,CA9RkB,OAAgC,CAiSxD,aAAc,CACb,MAAM,CAAC,cAA+B,CACtC,sBAAsB,CAAC,GAAG,CAC1B,uBAAuB,CAAC,GAAG,CAC3B,aAAa,CAAC,IAAI,CAGnB,aAAc,CACb,MAAM,CAAC,cAA+B,CACtC,0BAA0B,CAAC,GAAG,CAC9B,yBAAyB,CAAC,GAAG,CAC7B,UAAU,CAAC,IAAI,CAGhB,kCAAmC,CAClC,eAAe,CAAC,OAAO,CACvB,cAAc,CAAC,CAAC,CAChB,MAAM,CAAC,cAA+B,CACtC,aAAa,CAAC,GAAG,CAGlB,+HAAgI,CAC/H,KAAK,CA/ToB,IAAsB,CAgU/C,eAAe,CAAC,IAAI,CAGrB,aAAc,CACb,eAAe,CAAC,OAAO,CACvB,cAAc,CAAC,CAAC,CAChB,MAAM,CAAC,cAA+B,CACtC,aAAa,CAAC,GAAG,CAGlB,gBAAiB,CAChB,MAAM,CAAC,GAAG,CACV,UAAU,CAAC,gEAAiH,CAG7H,mCAAoC,CAvTnC,UAAU,CAAC,oDAAuE,CAClF,UAAU,CAAC,sBAAsC,CACjD,KAAK,CAAC,OAAwB,CAC9B,MAAM,CAAC,iBAAmD,CAwT3D,uBAAwB,CA3TvB,UAAU,CAAC,oDAAuE,CAClF,UAAU,CAAC,sBAAsC,CACjD,KAAK,CAAC,OAAwB,CAC9B,MAAM,CAAC,iBAAmD,CA4T3D,oBAAqB,CA/TpB,UAAU,CAAC,oDAAuE,CAClF,UAAU,CAAC,sBAAsC,CACjD,KAAK,CAAC,OAAwB,CAC9B,MAAM,CAAC,iBAAmD,CAgU3D,eAAgB,CAnUf,UAAU,CAAC,oDAAuE,CAClF,UAAU,CAAC,sBAAsC,CACjD,KAAK,CAAC,OAAwB,CAC9B,MAAM,CAAC,iBAAmD,CAoU3D,gGAAiG,CAChG,aAAa,CAAC,GAAG,CACjB,OAAO,CAAC,GAAG,CACX,WAAW,CAAC,cAAwB,CACpC,MAAM,CAAC,KAAK,CAGb,iRAAkR,CACjR,KAAK,CAAC,OAAO,CAGd,QAAS,CACR,WAAW,CAAC,OAAO,CAGpB,yBAA0B,CACzB,UAAU,CAAC,OAAa,CACxB,aAAa,CAAC,GAAG,CACjB,MAAM,CAAC,IAAI,CACX,OAAO,CAAC,GAAG,CACX,QAAQ,CAAC,IAAI,CACb,WAAW,CAAC,cAAuB,CACnC,MAAM,CAAC,KAAK,CAGb,8CAA+C,CAC9C,KAAK,CA7Ze,OAAa,CAgalC,8BAA+B,CAC9B,KAAK,CAAC,OAAiB,CAGxB,qBAAsB,CACrB,KAAK,CAAC,OAAgB,CAGvB,8CAA+C,CAC9C,KAAK,CAAC,OAA+B,CACrC,WAAW,CAAC,IAAI,CAGjB,kBAAmB,CAClB,KAAK,CAAC,OAAiB,CAGxB,IAAK,CACJ,OAAO,CAAC,IAAI,CACZ,aAAa,CAAC,GAAG", 4 | "sources": ["extra.scss"], 5 | "names": [], 6 | "file": "extra.css" 7 | } 8 | -------------------------------------------------------------------------------- /tests/clipboard.c: -------------------------------------------------------------------------------- 1 | //======================================================================== 2 | // Clipboard test program 3 | // Copyright (c) Camilla Löwy 4 | // 5 | // This software is provided 'as-is', without any express or implied 6 | // warranty. In no event will the authors be held liable for any damages 7 | // arising from the use of this software. 8 | // 9 | // Permission is granted to anyone to use this software for any purpose, 10 | // including commercial applications, and to alter it and redistribute it 11 | // freely, subject to the following restrictions: 12 | // 13 | // 1. The origin of this software must not be misrepresented; you must not 14 | // claim that you wrote the original software. If you use this software 15 | // in a product, an acknowledgment in the product documentation would 16 | // be appreciated but is not required. 17 | // 18 | // 2. Altered source versions must be plainly marked as such, and must not 19 | // be misrepresented as being the original software. 20 | // 21 | // 3. This notice may not be removed or altered from any source 22 | // distribution. 23 | // 24 | //======================================================================== 25 | // 26 | // This program is used to test the clipboard functionality. 27 | // 28 | //======================================================================== 29 | 30 | #define GLAD_GL_IMPLEMENTATION 31 | #include 32 | #define GLFW_INCLUDE_NONE 33 | #include 34 | 35 | #include 36 | #include 37 | 38 | #include "getopt.h" 39 | 40 | #if defined(__APPLE__) 41 | #define MODIFIER GLFW_MOD_SUPER 42 | #else 43 | #define MODIFIER GLFW_MOD_CONTROL 44 | #endif 45 | 46 | static void usage(void) 47 | { 48 | printf("Usage: clipboard [-h]\n"); 49 | } 50 | 51 | static void error_callback(int error, const char* description) 52 | { 53 | fprintf(stderr, "Error: %s\n", description); 54 | } 55 | 56 | static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) 57 | { 58 | if (action != GLFW_PRESS) 59 | return; 60 | 61 | switch (key) 62 | { 63 | case GLFW_KEY_ESCAPE: 64 | glfwSetWindowShouldClose(window, GLFW_TRUE); 65 | break; 66 | 67 | case GLFW_KEY_V: 68 | if (mods == MODIFIER) 69 | { 70 | const char* string; 71 | 72 | string = glfwGetClipboardString(NULL); 73 | if (string) 74 | printf("Clipboard contains \"%s\"\n", string); 75 | else 76 | printf("Clipboard does not contain a string\n"); 77 | } 78 | break; 79 | 80 | case GLFW_KEY_C: 81 | if (mods == MODIFIER) 82 | { 83 | const char* string = "Hello GLFW World!"; 84 | glfwSetClipboardString(NULL, string); 85 | printf("Setting clipboard to \"%s\"\n", string); 86 | } 87 | break; 88 | } 89 | } 90 | 91 | int main(int argc, char** argv) 92 | { 93 | int ch; 94 | GLFWwindow* window; 95 | 96 | while ((ch = getopt(argc, argv, "h")) != -1) 97 | { 98 | switch (ch) 99 | { 100 | case 'h': 101 | usage(); 102 | exit(EXIT_SUCCESS); 103 | 104 | default: 105 | usage(); 106 | exit(EXIT_FAILURE); 107 | } 108 | } 109 | 110 | glfwSetErrorCallback(error_callback); 111 | 112 | if (!glfwInit()) 113 | { 114 | fprintf(stderr, "Failed to initialize GLFW\n"); 115 | exit(EXIT_FAILURE); 116 | } 117 | 118 | window = glfwCreateWindow(200, 200, "Clipboard Test", NULL, NULL); 119 | if (!window) 120 | { 121 | glfwTerminate(); 122 | 123 | fprintf(stderr, "Failed to open GLFW window\n"); 124 | exit(EXIT_FAILURE); 125 | } 126 | 127 | glfwMakeContextCurrent(window); 128 | gladLoadGL(glfwGetProcAddress); 129 | glfwSwapInterval(1); 130 | 131 | glfwSetKeyCallback(window, key_callback); 132 | 133 | glClearColor(0.5f, 0.5f, 0.5f, 0); 134 | 135 | while (!glfwWindowShouldClose(window)) 136 | { 137 | glClear(GL_COLOR_BUFFER_BIT); 138 | 139 | glfwSwapBuffers(window); 140 | glfwWaitEvents(); 141 | } 142 | 143 | glfwTerminate(); 144 | exit(EXIT_SUCCESS); 145 | } 146 | 147 | -------------------------------------------------------------------------------- /tests/icon.c: -------------------------------------------------------------------------------- 1 | //======================================================================== 2 | // Window icon test program 3 | // Copyright (c) Camilla Löwy 4 | // 5 | // This software is provided 'as-is', without any express or implied 6 | // warranty. In no event will the authors be held liable for any damages 7 | // arising from the use of this software. 8 | // 9 | // Permission is granted to anyone to use this software for any purpose, 10 | // including commercial applications, and to alter it and redistribute it 11 | // freely, subject to the following restrictions: 12 | // 13 | // 1. The origin of this software must not be misrepresented; you must not 14 | // claim that you wrote the original software. If you use this software 15 | // in a product, an acknowledgment in the product documentation would 16 | // be appreciated but is not required. 17 | // 18 | // 2. Altered source versions must be plainly marked as such, and must not 19 | // be misrepresented as being the original software. 20 | // 21 | // 3. This notice may not be removed or altered from any source 22 | // distribution. 23 | // 24 | //======================================================================== 25 | // 26 | // This program is used to test the icon feature. 27 | // 28 | //======================================================================== 29 | 30 | #define GLAD_GL_IMPLEMENTATION 31 | #include 32 | #define GLFW_INCLUDE_NONE 33 | #include 34 | 35 | #include 36 | #include 37 | #include 38 | 39 | // a simple glfw logo 40 | const char* const logo[] = 41 | { 42 | "................", 43 | "................", 44 | "...0000..0......", 45 | "...0.....0......", 46 | "...0.00..0......", 47 | "...0..0..0......", 48 | "...0000..0000...", 49 | "................", 50 | "................", 51 | "...000..0...0...", 52 | "...0....0...0...", 53 | "...000..0.0.0...", 54 | "...0....0.0.0...", 55 | "...0....00000...", 56 | "................", 57 | "................" 58 | }; 59 | 60 | const unsigned char icon_colors[5][4] = 61 | { 62 | { 0, 0, 0, 255 }, // black 63 | { 255, 0, 0, 255 }, // red 64 | { 0, 255, 0, 255 }, // green 65 | { 0, 0, 255, 255 }, // blue 66 | { 255, 255, 255, 255 } // white 67 | }; 68 | 69 | static int cur_icon_color = 0; 70 | 71 | static void set_icon(GLFWwindow* window, int icon_color) 72 | { 73 | int x, y; 74 | unsigned char pixels[16 * 16 * 4]; 75 | unsigned char* target = pixels; 76 | GLFWimage img = { 16, 16, pixels }; 77 | 78 | for (y = 0; y < img.width; y++) 79 | { 80 | for (x = 0; x < img.height; x++) 81 | { 82 | if (logo[y][x] == '0') 83 | memcpy(target, icon_colors[icon_color], 4); 84 | else 85 | memset(target, 0, 4); 86 | 87 | target += 4; 88 | } 89 | } 90 | 91 | glfwSetWindowIcon(window, 1, &img); 92 | } 93 | 94 | static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) 95 | { 96 | if (action != GLFW_PRESS) 97 | return; 98 | 99 | switch (key) 100 | { 101 | case GLFW_KEY_ESCAPE: 102 | glfwSetWindowShouldClose(window, GLFW_TRUE); 103 | break; 104 | case GLFW_KEY_SPACE: 105 | cur_icon_color = (cur_icon_color + 1) % 5; 106 | set_icon(window, cur_icon_color); 107 | break; 108 | case GLFW_KEY_X: 109 | glfwSetWindowIcon(window, 0, NULL); 110 | break; 111 | } 112 | } 113 | 114 | int main(int argc, char** argv) 115 | { 116 | GLFWwindow* window; 117 | 118 | if (!glfwInit()) 119 | { 120 | fprintf(stderr, "Failed to initialize GLFW\n"); 121 | exit(EXIT_FAILURE); 122 | } 123 | 124 | window = glfwCreateWindow(200, 200, "Window Icon", NULL, NULL); 125 | if (!window) 126 | { 127 | glfwTerminate(); 128 | 129 | fprintf(stderr, "Failed to open GLFW window\n"); 130 | exit(EXIT_FAILURE); 131 | } 132 | 133 | glfwMakeContextCurrent(window); 134 | gladLoadGL(glfwGetProcAddress); 135 | 136 | glfwSetKeyCallback(window, key_callback); 137 | set_icon(window, cur_icon_color); 138 | 139 | while (!glfwWindowShouldClose(window)) 140 | { 141 | glClear(GL_COLOR_BUFFER_BIT); 142 | glfwSwapBuffers(window); 143 | glfwWaitEvents(); 144 | } 145 | 146 | glfwDestroyWindow(window); 147 | glfwTerminate(); 148 | exit(EXIT_SUCCESS); 149 | } 150 | 151 | -------------------------------------------------------------------------------- /tests/allocator.c: -------------------------------------------------------------------------------- 1 | //======================================================================== 2 | // Custom heap allocator test 3 | // Copyright (c) Camilla Löwy 4 | // 5 | // This software is provided 'as-is', without any express or implied 6 | // warranty. In no event will the authors be held liable for any damages 7 | // arising from the use of this software. 8 | // 9 | // Permission is granted to anyone to use this software for any purpose, 10 | // including commercial applications, and to alter it and redistribute it 11 | // freely, subject to the following restrictions: 12 | // 13 | // 1. The origin of this software must not be misrepresented; you must not 14 | // claim that you wrote the original software. If you use this software 15 | // in a product, an acknowledgment in the product documentation would 16 | // be appreciated but is not required. 17 | // 18 | // 2. Altered source versions must be plainly marked as such, and must not 19 | // be misrepresented as being the original software. 20 | // 21 | // 3. This notice may not be removed or altered from any source 22 | // distribution. 23 | // 24 | //======================================================================== 25 | 26 | #define GLAD_GL_IMPLEMENTATION 27 | #include 28 | #define GLFW_INCLUDE_NONE 29 | #include 30 | 31 | #include 32 | #include 33 | #include 34 | 35 | #define CALL(x) (function_name = #x, x) 36 | static const char* function_name = NULL; 37 | 38 | struct allocator_stats 39 | { 40 | size_t total; 41 | size_t current; 42 | size_t maximum; 43 | }; 44 | 45 | static void error_callback(int error, const char* description) 46 | { 47 | fprintf(stderr, "Error: %s\n", description); 48 | } 49 | 50 | static void* allocate(size_t size, void* user) 51 | { 52 | struct allocator_stats* stats = user; 53 | assert(size > 0); 54 | 55 | stats->total += size; 56 | stats->current += size; 57 | if (stats->current > stats->maximum) 58 | stats->maximum = stats->current; 59 | 60 | printf("%s: allocate %zu bytes (current %zu maximum %zu total %zu)\n", 61 | function_name, size, stats->current, stats->maximum, stats->total); 62 | 63 | size_t* real_block = malloc(size + sizeof(size_t)); 64 | assert(real_block != NULL); 65 | *real_block = size; 66 | return real_block + 1; 67 | } 68 | 69 | static void deallocate(void* block, void* user) 70 | { 71 | struct allocator_stats* stats = user; 72 | assert(block != NULL); 73 | 74 | size_t* real_block = (size_t*) block - 1; 75 | stats->current -= *real_block; 76 | 77 | printf("%s: deallocate %zu bytes (current %zu maximum %zu total %zu)\n", 78 | function_name, *real_block, stats->current, stats->maximum, stats->total); 79 | 80 | free(real_block); 81 | } 82 | 83 | static void* reallocate(void* block, size_t size, void* user) 84 | { 85 | struct allocator_stats* stats = user; 86 | assert(block != NULL); 87 | assert(size > 0); 88 | 89 | size_t* real_block = (size_t*) block - 1; 90 | stats->total += size; 91 | stats->current += size - *real_block; 92 | if (stats->current > stats->maximum) 93 | stats->maximum = stats->current; 94 | 95 | printf("%s: reallocate %zu bytes to %zu bytes (current %zu maximum %zu total %zu)\n", 96 | function_name, *real_block, size, stats->current, stats->maximum, stats->total); 97 | 98 | real_block = realloc(real_block, size + sizeof(size_t)); 99 | assert(real_block != NULL); 100 | *real_block = size; 101 | return real_block + 1; 102 | } 103 | 104 | int main(void) 105 | { 106 | struct allocator_stats stats = {0}; 107 | const GLFWallocator allocator = 108 | { 109 | .allocate = allocate, 110 | .deallocate = deallocate, 111 | .reallocate = reallocate, 112 | .user = &stats 113 | }; 114 | 115 | glfwSetErrorCallback(error_callback); 116 | glfwInitAllocator(&allocator); 117 | 118 | if (!CALL(glfwInit)()) 119 | exit(EXIT_FAILURE); 120 | 121 | GLFWwindow* window = CALL(glfwCreateWindow)(400, 400, "Custom allocator test", NULL, NULL); 122 | if (!window) 123 | { 124 | glfwTerminate(); 125 | exit(EXIT_FAILURE); 126 | } 127 | 128 | CALL(glfwMakeContextCurrent)(window); 129 | gladLoadGL(glfwGetProcAddress); 130 | CALL(glfwSwapInterval)(1); 131 | 132 | while (!CALL(glfwWindowShouldClose)(window)) 133 | { 134 | glClear(GL_COLOR_BUFFER_BIT); 135 | CALL(glfwSwapBuffers)(window); 136 | CALL(glfwWaitEvents)(); 137 | } 138 | 139 | CALL(glfwTerminate)(); 140 | exit(EXIT_SUCCESS); 141 | } 142 | 143 | -------------------------------------------------------------------------------- /tests/threads.c: -------------------------------------------------------------------------------- 1 | //======================================================================== 2 | // Multi-threading test 3 | // Copyright (c) Camilla Löwy 4 | // 5 | // This software is provided 'as-is', without any express or implied 6 | // warranty. In no event will the authors be held liable for any damages 7 | // arising from the use of this software. 8 | // 9 | // Permission is granted to anyone to use this software for any purpose, 10 | // including commercial applications, and to alter it and redistribute it 11 | // freely, subject to the following restrictions: 12 | // 13 | // 1. The origin of this software must not be misrepresented; you must not 14 | // claim that you wrote the original software. If you use this software 15 | // in a product, an acknowledgment in the product documentation would 16 | // be appreciated but is not required. 17 | // 18 | // 2. Altered source versions must be plainly marked as such, and must not 19 | // be misrepresented as being the original software. 20 | // 21 | // 3. This notice may not be removed or altered from any source 22 | // distribution. 23 | // 24 | //======================================================================== 25 | // 26 | // This test is intended to verify whether the OpenGL context part of 27 | // the GLFW API is able to be used from multiple threads 28 | // 29 | //======================================================================== 30 | 31 | #include "tinycthread.h" 32 | 33 | #define GLAD_GL_IMPLEMENTATION 34 | #include 35 | #define GLFW_INCLUDE_NONE 36 | #include 37 | 38 | #include 39 | #include 40 | #include 41 | 42 | typedef struct 43 | { 44 | GLFWwindow* window; 45 | const char* title; 46 | float r, g, b; 47 | thrd_t id; 48 | } Thread; 49 | 50 | static volatile int running = GLFW_TRUE; 51 | 52 | static void error_callback(int error, const char* description) 53 | { 54 | fprintf(stderr, "Error: %s\n", description); 55 | } 56 | 57 | static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) 58 | { 59 | if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) 60 | glfwSetWindowShouldClose(window, GLFW_TRUE); 61 | } 62 | 63 | static int thread_main(void* data) 64 | { 65 | const Thread* thread = data; 66 | 67 | glfwMakeContextCurrent(thread->window); 68 | glfwSwapInterval(1); 69 | 70 | while (running) 71 | { 72 | const float v = (float) fabs(sin(glfwGetTime() * 2.f)); 73 | glClearColor(thread->r * v, thread->g * v, thread->b * v, 0.f); 74 | 75 | glClear(GL_COLOR_BUFFER_BIT); 76 | glfwSwapBuffers(thread->window); 77 | } 78 | 79 | glfwMakeContextCurrent(NULL); 80 | return 0; 81 | } 82 | 83 | int main(void) 84 | { 85 | int i, result; 86 | Thread threads[] = 87 | { 88 | { NULL, "Red", 1.f, 0.f, 0.f, 0 }, 89 | { NULL, "Green", 0.f, 1.f, 0.f, 0 }, 90 | { NULL, "Blue", 0.f, 0.f, 1.f, 0 } 91 | }; 92 | const int count = sizeof(threads) / sizeof(Thread); 93 | 94 | glfwSetErrorCallback(error_callback); 95 | 96 | if (!glfwInit()) 97 | exit(EXIT_FAILURE); 98 | 99 | for (i = 0; i < count; i++) 100 | { 101 | glfwWindowHint(GLFW_POSITION_X, 200 + 250 * i); 102 | glfwWindowHint(GLFW_POSITION_Y, 200); 103 | 104 | threads[i].window = glfwCreateWindow(200, 200, 105 | threads[i].title, 106 | NULL, NULL); 107 | if (!threads[i].window) 108 | { 109 | glfwTerminate(); 110 | exit(EXIT_FAILURE); 111 | } 112 | 113 | glfwSetKeyCallback(threads[i].window, key_callback); 114 | } 115 | 116 | glfwMakeContextCurrent(threads[0].window); 117 | gladLoadGL(glfwGetProcAddress); 118 | glfwMakeContextCurrent(NULL); 119 | 120 | for (i = 0; i < count; i++) 121 | { 122 | if (thrd_create(&threads[i].id, thread_main, threads + i) != 123 | thrd_success) 124 | { 125 | fprintf(stderr, "Failed to create secondary thread\n"); 126 | 127 | glfwTerminate(); 128 | exit(EXIT_FAILURE); 129 | } 130 | } 131 | 132 | while (running) 133 | { 134 | glfwWaitEvents(); 135 | 136 | for (i = 0; i < count; i++) 137 | { 138 | if (glfwWindowShouldClose(threads[i].window)) 139 | running = GLFW_FALSE; 140 | } 141 | } 142 | 143 | for (i = 0; i < count; i++) 144 | glfwHideWindow(threads[i].window); 145 | 146 | for (i = 0; i < count; i++) 147 | thrd_join(threads[i].id, &result); 148 | 149 | exit(EXIT_SUCCESS); 150 | } 151 | 152 | -------------------------------------------------------------------------------- /docs/internal.dox: -------------------------------------------------------------------------------- 1 | /*! 2 | 3 | @page internals_guide Internal structure 4 | 5 | @tableofcontents 6 | 7 | There are several interfaces inside GLFW. Each interface has its own area of 8 | responsibility and its own naming conventions. 9 | 10 | 11 | @section internals_public Public interface 12 | 13 | The most well-known is the public interface, described in the glfw3.h header 14 | file. This is implemented in source files shared by all platforms and these 15 | files contain no platform-specific code. This code usually ends up calling the 16 | platform and internal interfaces to do the actual work. 17 | 18 | The public interface uses the OpenGL naming conventions except with GLFW and 19 | glfw instead of GL and gl. For struct members, where OpenGL sets no precedent, 20 | it use headless camel case. 21 | 22 | Examples: `glfwCreateWindow`, `GLFWwindow`, `GLFW_RED_BITS` 23 | 24 | 25 | @section internals_native Native interface 26 | 27 | The [native interface](@ref native) is a small set of publicly available 28 | but platform-specific functions, described in the glfw3native.h header file and 29 | used to gain access to the underlying window, context and (on some platforms) 30 | display handles used by the platform interface. 31 | 32 | The function names of the native interface are similar to those of the public 33 | interface, but embeds the name of the interface that the returned handle is 34 | from. 35 | 36 | Examples: `glfwGetX11Window`, `glfwGetWGLContext` 37 | 38 | 39 | @section internals_internal Internal interface 40 | 41 | The internal interface consists of utility functions used by all other 42 | interfaces. It is shared code implemented in the same shared source files as 43 | the public and event interfaces. The internal interface is described in the 44 | internal.h header file. 45 | 46 | The internal interface is in charge of GLFW's global data, which it stores in 47 | a `_GLFWlibrary` struct named `_glfw`. 48 | 49 | The internal interface uses the same style as the public interface, except all 50 | global names have a leading underscore. 51 | 52 | Examples: `_glfwIsValidContextConfig`, `_GLFWwindow`, `_glfw.monitorCount` 53 | 54 | 55 | @section internals_platform Platform interface 56 | 57 | The platform interface implements all platform-specific operations as a service 58 | to the public interface. This includes event processing. The platform 59 | interface is never directly called by application code and never directly calls 60 | application-provided callbacks. It is also prohibited from modifying the 61 | platform-independent part of the internal structs. Instead, it calls the event 62 | interface when events interesting to GLFW are received. 63 | 64 | The platform interface mostly mirrors those parts of the public interface that needs to 65 | perform platform-specific operations on some or all platforms. 66 | 67 | The window system bits of the platform API is called through the `_GLFWplatform` struct of 68 | function pointers, to allow runtime selection of platform. This includes the window and 69 | context creation, input and event processing, monitor and Vulkan surface creation parts of 70 | GLFW. This is located in the global `_glfw` struct. 71 | 72 | Examples: `_glfw.platform.createWindow` 73 | 74 | The timer, threading and module loading bits of the platform API are plain functions with 75 | a `_glfwPlatform` prefix, as these things are independent of what window system is being 76 | used. 77 | 78 | Examples: `_glfwPlatformGetTimerValue` 79 | 80 | The platform interface also defines structs that contain platform-specific 81 | global and per-object state. Their names mirror those of the internal 82 | interface, except that an interface-specific suffix is added. 83 | 84 | Examples: `_GLFWwindowX11`, `_GLFWcontextWGL` 85 | 86 | These structs are incorporated as members into the internal interface structs 87 | using special macros that name them after the specific interface used. This 88 | prevents shared code from accidentally using these members. 89 | 90 | Examples: `window->win32.handle`, `_glfw.x11.display` 91 | 92 | 93 | @section internals_event Event interface 94 | 95 | The event interface is implemented in the same shared source files as the public 96 | interface and is responsible for delivering the events it receives to the 97 | application, either via callbacks, via window state changes or both. 98 | 99 | The function names of the event interface use a `_glfwInput` prefix and the 100 | ObjectEvent pattern. 101 | 102 | Examples: `_glfwInputWindowFocus`, `_glfwInputCursorPos` 103 | 104 | 105 | @section internals_static Static functions 106 | 107 | Static functions may be used by any interface and have no prefixes or suffixes. 108 | These use headless camel case. 109 | 110 | Examples: `isValidElementForJoystick` 111 | 112 | 113 | @section internals_config Configuration macros 114 | 115 | GLFW uses a number of configuration macros to select at compile time which 116 | interfaces and code paths to use. They are defined in the GLFW CMake target. 117 | 118 | Configuration macros the same style as tokens in the public interface, except 119 | with a leading underscore. 120 | 121 | Examples: `_GLFW_WIN32`, `_GLFW_BUILD_DLL` 122 | 123 | */ 124 | -------------------------------------------------------------------------------- /src/mappings.h.in: -------------------------------------------------------------------------------- 1 | //======================================================================== 2 | // GLFW 3.4 - www.glfw.org 3 | //------------------------------------------------------------------------ 4 | // Copyright (c) 2006-2018 Camilla Löwy 5 | // 6 | // This software is provided 'as-is', without any express or implied 7 | // warranty. In no event will the authors be held liable for any damages 8 | // arising from the use of this software. 9 | // 10 | // Permission is granted to anyone to use this software for any purpose, 11 | // including commercial applications, and to alter it and redistribute it 12 | // freely, subject to the following restrictions: 13 | // 14 | // 1. The origin of this software must not be misrepresented; you must not 15 | // claim that you wrote the original software. If you use this software 16 | // in a product, an acknowledgment in the product documentation would 17 | // be appreciated but is not required. 18 | // 19 | // 2. Altered source versions must be plainly marked as such, and must not 20 | // be misrepresented as being the original software. 21 | // 22 | // 3. This notice may not be removed or altered from any source 23 | // distribution. 24 | // 25 | //======================================================================== 26 | // As mappings.h.in, this file is used by CMake to produce the mappings.h 27 | // header file. If you are adding a GLFW specific gamepad mapping, this is 28 | // where to put it. 29 | //======================================================================== 30 | // As mappings.h, this provides all pre-defined gamepad mappings, including 31 | // all available in SDL_GameControllerDB. Do not edit this file. Any gamepad 32 | // mappings not specific to GLFW should be submitted to SDL_GameControllerDB. 33 | // This file can be re-generated from mappings.h.in and the upstream 34 | // gamecontrollerdb.txt with the 'update_mappings' CMake target. 35 | //======================================================================== 36 | 37 | // All gamepad mappings not labeled GLFW are copied from the 38 | // SDL_GameControllerDB project under the following license: 39 | // 40 | // Simple DirectMedia Layer 41 | // Copyright (C) 1997-2013 Sam Lantinga 42 | // 43 | // This software is provided 'as-is', without any express or implied warranty. 44 | // In no event will the authors be held liable for any damages arising from the 45 | // use of this software. 46 | // 47 | // Permission is granted to anyone to use this software for any purpose, 48 | // including commercial applications, and to alter it and redistribute it 49 | // freely, subject to the following restrictions: 50 | // 51 | // 1. The origin of this software must not be misrepresented; you must not 52 | // claim that you wrote the original software. If you use this software 53 | // in a product, an acknowledgment in the product documentation would 54 | // be appreciated but is not required. 55 | // 56 | // 2. Altered source versions must be plainly marked as such, and must not be 57 | // misrepresented as being the original software. 58 | // 59 | // 3. This notice may not be removed or altered from any source distribution. 60 | 61 | const char* _glfwDefaultMappings[] = 62 | { 63 | #if defined(_GLFW_WIN32) 64 | @GLFW_WIN32_MAPPINGS@ 65 | "78696e70757401000000000000000000,XInput Gamepad (GLFW),platform:Windows,a:b0,b:b1,x:b2,y:b3,leftshoulder:b4,rightshoulder:b5,back:b6,start:b7,leftstick:b8,rightstick:b9,leftx:a0,lefty:a1,rightx:a2,righty:a3,lefttrigger:a4,righttrigger:a5,dpup:h0.1,dpright:h0.2,dpdown:h0.4,dpleft:h0.8,", 66 | "78696e70757402000000000000000000,XInput Wheel (GLFW),platform:Windows,a:b0,b:b1,x:b2,y:b3,leftshoulder:b4,rightshoulder:b5,back:b6,start:b7,leftstick:b8,rightstick:b9,leftx:a0,lefty:a1,rightx:a2,righty:a3,lefttrigger:a4,righttrigger:a5,dpup:h0.1,dpright:h0.2,dpdown:h0.4,dpleft:h0.8,", 67 | "78696e70757403000000000000000000,XInput Arcade Stick (GLFW),platform:Windows,a:b0,b:b1,x:b2,y:b3,leftshoulder:b4,rightshoulder:b5,back:b6,start:b7,leftstick:b8,rightstick:b9,leftx:a0,lefty:a1,rightx:a2,righty:a3,lefttrigger:a4,righttrigger:a5,dpup:h0.1,dpright:h0.2,dpdown:h0.4,dpleft:h0.8,", 68 | "78696e70757404000000000000000000,XInput Flight Stick (GLFW),platform:Windows,a:b0,b:b1,x:b2,y:b3,leftshoulder:b4,rightshoulder:b5,back:b6,start:b7,leftstick:b8,rightstick:b9,leftx:a0,lefty:a1,rightx:a2,righty:a3,lefttrigger:a4,righttrigger:a5,dpup:h0.1,dpright:h0.2,dpdown:h0.4,dpleft:h0.8,", 69 | "78696e70757405000000000000000000,XInput Dance Pad (GLFW),platform:Windows,a:b0,b:b1,x:b2,y:b3,leftshoulder:b4,rightshoulder:b5,back:b6,start:b7,leftstick:b8,rightstick:b9,leftx:a0,lefty:a1,rightx:a2,righty:a3,lefttrigger:a4,righttrigger:a5,dpup:h0.1,dpright:h0.2,dpdown:h0.4,dpleft:h0.8,", 70 | "78696e70757406000000000000000000,XInput Guitar (GLFW),platform:Windows,a:b0,b:b1,x:b2,y:b3,leftshoulder:b4,rightshoulder:b5,back:b6,start:b7,leftstick:b8,rightstick:b9,leftx:a0,lefty:a1,rightx:a2,righty:a3,lefttrigger:a4,righttrigger:a5,dpup:h0.1,dpright:h0.2,dpdown:h0.4,dpleft:h0.8,", 71 | "78696e70757408000000000000000000,XInput Drum Kit (GLFW),platform:Windows,a:b0,b:b1,x:b2,y:b3,leftshoulder:b4,rightshoulder:b5,back:b6,start:b7,leftstick:b8,rightstick:b9,leftx:a0,lefty:a1,rightx:a2,righty:a3,lefttrigger:a4,righttrigger:a5,dpup:h0.1,dpright:h0.2,dpdown:h0.4,dpleft:h0.8,", 72 | #endif // _GLFW_WIN32 73 | 74 | #if defined(_GLFW_COCOA) 75 | @GLFW_COCOA_MAPPINGS@ 76 | #endif // _GLFW_COCOA 77 | 78 | #if defined(GLFW_BUILD_LINUX_JOYSTICK) 79 | @GLFW_LINUX_MAPPINGS@ 80 | #endif // GLFW_BUILD_LINUX_JOYSTICK 81 | }; 82 | 83 | -------------------------------------------------------------------------------- /examples/offscreen.c: -------------------------------------------------------------------------------- 1 | //======================================================================== 2 | // Offscreen rendering example 3 | // Copyright (c) Camilla Löwy 4 | // 5 | // This software is provided 'as-is', without any express or implied 6 | // warranty. In no event will the authors be held liable for any damages 7 | // arising from the use of this software. 8 | // 9 | // Permission is granted to anyone to use this software for any purpose, 10 | // including commercial applications, and to alter it and redistribute it 11 | // freely, subject to the following restrictions: 12 | // 13 | // 1. The origin of this software must not be misrepresented; you must not 14 | // claim that you wrote the original software. If you use this software 15 | // in a product, an acknowledgment in the product documentation would 16 | // be appreciated but is not required. 17 | // 18 | // 2. Altered source versions must be plainly marked as such, and must not 19 | // be misrepresented as being the original software. 20 | // 21 | // 3. This notice may not be removed or altered from any source 22 | // distribution. 23 | // 24 | //======================================================================== 25 | 26 | #define GLAD_GL_IMPLEMENTATION 27 | #include 28 | #define GLFW_INCLUDE_NONE 29 | #include 30 | 31 | #include "linmath.h" 32 | 33 | #include 34 | #include 35 | 36 | #define STB_IMAGE_WRITE_IMPLEMENTATION 37 | #include 38 | 39 | static const struct 40 | { 41 | float x, y; 42 | float r, g, b; 43 | } vertices[3] = 44 | { 45 | { -0.6f, -0.4f, 1.f, 0.f, 0.f }, 46 | { 0.6f, -0.4f, 0.f, 1.f, 0.f }, 47 | { 0.f, 0.6f, 0.f, 0.f, 1.f } 48 | }; 49 | 50 | static const char* vertex_shader_text = 51 | "#version 110\n" 52 | "uniform mat4 MVP;\n" 53 | "attribute vec3 vCol;\n" 54 | "attribute vec2 vPos;\n" 55 | "varying vec3 color;\n" 56 | "void main()\n" 57 | "{\n" 58 | " gl_Position = MVP * vec4(vPos, 0.0, 1.0);\n" 59 | " color = vCol;\n" 60 | "}\n"; 61 | 62 | static const char* fragment_shader_text = 63 | "#version 110\n" 64 | "varying vec3 color;\n" 65 | "void main()\n" 66 | "{\n" 67 | " gl_FragColor = vec4(color, 1.0);\n" 68 | "}\n"; 69 | 70 | static void error_callback(int error, const char* description) 71 | { 72 | fprintf(stderr, "Error: %s\n", description); 73 | } 74 | 75 | int main(void) 76 | { 77 | GLFWwindow* window; 78 | GLuint vertex_buffer, vertex_shader, fragment_shader, program; 79 | GLint mvp_location, vpos_location, vcol_location; 80 | float ratio; 81 | int width, height; 82 | mat4x4 mvp; 83 | char* buffer; 84 | 85 | glfwSetErrorCallback(error_callback); 86 | 87 | glfwInitHint(GLFW_COCOA_MENUBAR, GLFW_FALSE); 88 | 89 | if (!glfwInit()) 90 | exit(EXIT_FAILURE); 91 | 92 | glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2); 93 | glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0); 94 | glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE); 95 | 96 | window = glfwCreateWindow(640, 480, "Simple example", NULL, NULL); 97 | if (!window) 98 | { 99 | glfwTerminate(); 100 | exit(EXIT_FAILURE); 101 | } 102 | 103 | glfwMakeContextCurrent(window); 104 | gladLoadGL(glfwGetProcAddress); 105 | 106 | // NOTE: OpenGL error checks have been omitted for brevity 107 | 108 | glGenBuffers(1, &vertex_buffer); 109 | glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer); 110 | glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); 111 | 112 | vertex_shader = glCreateShader(GL_VERTEX_SHADER); 113 | glShaderSource(vertex_shader, 1, &vertex_shader_text, NULL); 114 | glCompileShader(vertex_shader); 115 | 116 | fragment_shader = glCreateShader(GL_FRAGMENT_SHADER); 117 | glShaderSource(fragment_shader, 1, &fragment_shader_text, NULL); 118 | glCompileShader(fragment_shader); 119 | 120 | program = glCreateProgram(); 121 | glAttachShader(program, vertex_shader); 122 | glAttachShader(program, fragment_shader); 123 | glLinkProgram(program); 124 | 125 | mvp_location = glGetUniformLocation(program, "MVP"); 126 | vpos_location = glGetAttribLocation(program, "vPos"); 127 | vcol_location = glGetAttribLocation(program, "vCol"); 128 | 129 | glEnableVertexAttribArray(vpos_location); 130 | glVertexAttribPointer(vpos_location, 2, GL_FLOAT, GL_FALSE, 131 | sizeof(vertices[0]), (void*) 0); 132 | glEnableVertexAttribArray(vcol_location); 133 | glVertexAttribPointer(vcol_location, 3, GL_FLOAT, GL_FALSE, 134 | sizeof(vertices[0]), (void*) (sizeof(float) * 2)); 135 | 136 | glfwGetFramebufferSize(window, &width, &height); 137 | ratio = width / (float) height; 138 | 139 | glViewport(0, 0, width, height); 140 | glClear(GL_COLOR_BUFFER_BIT); 141 | 142 | mat4x4_ortho(mvp, -ratio, ratio, -1.f, 1.f, 1.f, -1.f); 143 | 144 | glUseProgram(program); 145 | glUniformMatrix4fv(mvp_location, 1, GL_FALSE, (const GLfloat*) mvp); 146 | glDrawArrays(GL_TRIANGLES, 0, 3); 147 | glFinish(); 148 | 149 | buffer = calloc(4, width * height); 150 | glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, buffer); 151 | 152 | // Write image Y-flipped because OpenGL 153 | stbi_write_png("offscreen.png", 154 | width, height, 4, 155 | buffer + (width * 4 * (height - 1)), 156 | -width * 4); 157 | 158 | free(buffer); 159 | 160 | glfwDestroyWindow(window); 161 | 162 | glfwTerminate(); 163 | exit(EXIT_SUCCESS); 164 | } 165 | 166 | -------------------------------------------------------------------------------- /examples/triangle-opengl.c: -------------------------------------------------------------------------------- 1 | //======================================================================== 2 | // OpenGL triangle example 3 | // Copyright (c) Camilla Löwy 4 | // 5 | // This software is provided 'as-is', without any express or implied 6 | // warranty. In no event will the authors be held liable for any damages 7 | // arising from the use of this software. 8 | // 9 | // Permission is granted to anyone to use this software for any purpose, 10 | // including commercial applications, and to alter it and redistribute it 11 | // freely, subject to the following restrictions: 12 | // 13 | // 1. The origin of this software must not be misrepresented; you must not 14 | // claim that you wrote the original software. If you use this software 15 | // in a product, an acknowledgment in the product documentation would 16 | // be appreciated but is not required. 17 | // 18 | // 2. Altered source versions must be plainly marked as such, and must not 19 | // be misrepresented as being the original software. 20 | // 21 | // 3. This notice may not be removed or altered from any source 22 | // distribution. 23 | // 24 | //======================================================================== 25 | //! [code] 26 | 27 | #define GLAD_GL_IMPLEMENTATION 28 | #include 29 | #define GLFW_INCLUDE_NONE 30 | #include 31 | 32 | #include "linmath.h" 33 | 34 | #include 35 | #include 36 | #include 37 | 38 | typedef struct Vertex 39 | { 40 | vec2 pos; 41 | vec3 col; 42 | } Vertex; 43 | 44 | static const Vertex vertices[3] = 45 | { 46 | { { -0.6f, -0.4f }, { 1.f, 0.f, 0.f } }, 47 | { { 0.6f, -0.4f }, { 0.f, 1.f, 0.f } }, 48 | { { 0.f, 0.6f }, { 0.f, 0.f, 1.f } } 49 | }; 50 | 51 | static const char* vertex_shader_text = 52 | "#version 330\n" 53 | "uniform mat4 MVP;\n" 54 | "in vec3 vCol;\n" 55 | "in vec2 vPos;\n" 56 | "out vec3 color;\n" 57 | "void main()\n" 58 | "{\n" 59 | " gl_Position = MVP * vec4(vPos, 0.0, 1.0);\n" 60 | " color = vCol;\n" 61 | "}\n"; 62 | 63 | static const char* fragment_shader_text = 64 | "#version 330\n" 65 | "in vec3 color;\n" 66 | "out vec4 fragment;\n" 67 | "void main()\n" 68 | "{\n" 69 | " fragment = vec4(color, 1.0);\n" 70 | "}\n"; 71 | 72 | static void error_callback(int error, const char* description) 73 | { 74 | fprintf(stderr, "Error: %s\n", description); 75 | } 76 | 77 | static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) 78 | { 79 | if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) 80 | glfwSetWindowShouldClose(window, GLFW_TRUE); 81 | } 82 | 83 | int main(void) 84 | { 85 | glfwSetErrorCallback(error_callback); 86 | 87 | if (!glfwInit()) 88 | exit(EXIT_FAILURE); 89 | 90 | glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); 91 | glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); 92 | glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); 93 | 94 | GLFWwindow* window = glfwCreateWindow(640, 480, "OpenGL Triangle", NULL, NULL); 95 | if (!window) 96 | { 97 | glfwTerminate(); 98 | exit(EXIT_FAILURE); 99 | } 100 | 101 | glfwSetKeyCallback(window, key_callback); 102 | 103 | glfwMakeContextCurrent(window); 104 | gladLoadGL(glfwGetProcAddress); 105 | glfwSwapInterval(1); 106 | 107 | // NOTE: OpenGL error checks have been omitted for brevity 108 | 109 | GLuint vertex_buffer; 110 | glGenBuffers(1, &vertex_buffer); 111 | glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer); 112 | glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); 113 | 114 | const GLuint vertex_shader = glCreateShader(GL_VERTEX_SHADER); 115 | glShaderSource(vertex_shader, 1, &vertex_shader_text, NULL); 116 | glCompileShader(vertex_shader); 117 | 118 | const GLuint fragment_shader = glCreateShader(GL_FRAGMENT_SHADER); 119 | glShaderSource(fragment_shader, 1, &fragment_shader_text, NULL); 120 | glCompileShader(fragment_shader); 121 | 122 | const GLuint program = glCreateProgram(); 123 | glAttachShader(program, vertex_shader); 124 | glAttachShader(program, fragment_shader); 125 | glLinkProgram(program); 126 | 127 | const GLint mvp_location = glGetUniformLocation(program, "MVP"); 128 | const GLint vpos_location = glGetAttribLocation(program, "vPos"); 129 | const GLint vcol_location = glGetAttribLocation(program, "vCol"); 130 | 131 | GLuint vertex_array; 132 | glGenVertexArrays(1, &vertex_array); 133 | glBindVertexArray(vertex_array); 134 | glEnableVertexAttribArray(vpos_location); 135 | glVertexAttribPointer(vpos_location, 2, GL_FLOAT, GL_FALSE, 136 | sizeof(Vertex), (void*) offsetof(Vertex, pos)); 137 | glEnableVertexAttribArray(vcol_location); 138 | glVertexAttribPointer(vcol_location, 3, GL_FLOAT, GL_FALSE, 139 | sizeof(Vertex), (void*) offsetof(Vertex, col)); 140 | 141 | while (!glfwWindowShouldClose(window)) 142 | { 143 | int width, height; 144 | glfwGetFramebufferSize(window, &width, &height); 145 | const float ratio = width / (float) height; 146 | 147 | glViewport(0, 0, width, height); 148 | glClear(GL_COLOR_BUFFER_BIT); 149 | 150 | mat4x4 m, p, mvp; 151 | mat4x4_identity(m); 152 | mat4x4_rotate_Z(m, m, (float) glfwGetTime()); 153 | mat4x4_ortho(p, -ratio, ratio, -1.f, 1.f, 1.f, -1.f); 154 | mat4x4_mul(mvp, p, m); 155 | 156 | glUseProgram(program); 157 | glUniformMatrix4fv(mvp_location, 1, GL_FALSE, (const GLfloat*) &mvp); 158 | glBindVertexArray(vertex_array); 159 | glDrawArrays(GL_TRIANGLES, 0, 3); 160 | 161 | glfwSwapBuffers(window); 162 | glfwPollEvents(); 163 | } 164 | 165 | glfwDestroyWindow(window); 166 | 167 | glfwTerminate(); 168 | exit(EXIT_SUCCESS); 169 | } 170 | 171 | //! [code] 172 | -------------------------------------------------------------------------------- /src/null_monitor.c: -------------------------------------------------------------------------------- 1 | //======================================================================== 2 | // GLFW 3.4 - www.glfw.org 3 | //------------------------------------------------------------------------ 4 | // Copyright (c) 2016 Google Inc. 5 | // Copyright (c) 2016-2019 Camilla Löwy 6 | // 7 | // This software is provided 'as-is', without any express or implied 8 | // warranty. In no event will the authors be held liable for any damages 9 | // arising from the use of this software. 10 | // 11 | // Permission is granted to anyone to use this software for any purpose, 12 | // including commercial applications, and to alter it and redistribute it 13 | // freely, subject to the following restrictions: 14 | // 15 | // 1. The origin of this software must not be misrepresented; you must not 16 | // claim that you wrote the original software. If you use this software 17 | // in a product, an acknowledgment in the product documentation would 18 | // be appreciated but is not required. 19 | // 20 | // 2. Altered source versions must be plainly marked as such, and must not 21 | // be misrepresented as being the original software. 22 | // 23 | // 3. This notice may not be removed or altered from any source 24 | // distribution. 25 | // 26 | //======================================================================== 27 | // It is fine to use C99 in this file because it will not be built with VS 28 | //======================================================================== 29 | 30 | #include "internal.h" 31 | 32 | #include 33 | #include 34 | #include 35 | 36 | // The the sole (fake) video mode of our (sole) fake monitor 37 | // 38 | static GLFWvidmode getVideoMode(void) 39 | { 40 | GLFWvidmode mode; 41 | mode.width = 1920; 42 | mode.height = 1080; 43 | mode.redBits = 8; 44 | mode.greenBits = 8; 45 | mode.blueBits = 8; 46 | mode.refreshRate = 60; 47 | return mode; 48 | } 49 | 50 | ////////////////////////////////////////////////////////////////////////// 51 | ////// GLFW internal API ////// 52 | ////////////////////////////////////////////////////////////////////////// 53 | 54 | void _glfwPollMonitorsNull(void) 55 | { 56 | const float dpi = 141.f; 57 | const GLFWvidmode mode = getVideoMode(); 58 | _GLFWmonitor* monitor = _glfwAllocMonitor("Null SuperNoop 0", 59 | (int) (mode.width * 25.4f / dpi), 60 | (int) (mode.height * 25.4f / dpi)); 61 | _glfwInputMonitor(monitor, GLFW_CONNECTED, _GLFW_INSERT_FIRST); 62 | } 63 | 64 | ////////////////////////////////////////////////////////////////////////// 65 | ////// GLFW platform API ////// 66 | ////////////////////////////////////////////////////////////////////////// 67 | 68 | void _glfwFreeMonitorNull(_GLFWmonitor* monitor) 69 | { 70 | _glfwFreeGammaArrays(&monitor->null.ramp); 71 | } 72 | 73 | void _glfwGetMonitorPosNull(_GLFWmonitor* monitor, int* xpos, int* ypos) 74 | { 75 | if (xpos) 76 | *xpos = 0; 77 | if (ypos) 78 | *ypos = 0; 79 | } 80 | 81 | void _glfwGetMonitorContentScaleNull(_GLFWmonitor* monitor, 82 | float* xscale, float* yscale) 83 | { 84 | if (xscale) 85 | *xscale = 1.f; 86 | if (yscale) 87 | *yscale = 1.f; 88 | } 89 | 90 | void _glfwGetMonitorWorkareaNull(_GLFWmonitor* monitor, 91 | int* xpos, int* ypos, 92 | int* width, int* height) 93 | { 94 | const GLFWvidmode mode = getVideoMode(); 95 | 96 | if (xpos) 97 | *xpos = 0; 98 | if (ypos) 99 | *ypos = 10; 100 | if (width) 101 | *width = mode.width; 102 | if (height) 103 | *height = mode.height - 10; 104 | } 105 | 106 | GLFWvidmode* _glfwGetVideoModesNull(_GLFWmonitor* monitor, int* found) 107 | { 108 | GLFWvidmode* mode = _glfw_calloc(1, sizeof(GLFWvidmode)); 109 | *mode = getVideoMode(); 110 | *found = 1; 111 | return mode; 112 | } 113 | 114 | void _glfwGetVideoModeNull(_GLFWmonitor* monitor, GLFWvidmode* mode) 115 | { 116 | *mode = getVideoMode(); 117 | } 118 | 119 | GLFWbool _glfwGetGammaRampNull(_GLFWmonitor* monitor, GLFWgammaramp* ramp) 120 | { 121 | if (!monitor->null.ramp.size) 122 | { 123 | unsigned int i; 124 | 125 | _glfwAllocGammaArrays(&monitor->null.ramp, 256); 126 | 127 | for (i = 0; i < monitor->null.ramp.size; i++) 128 | { 129 | const float gamma = 2.2f; 130 | float value; 131 | value = i / (float) (monitor->null.ramp.size - 1); 132 | value = powf(value, 1.f / gamma) * 65535.f + 0.5f; 133 | value = _glfw_fminf(value, 65535.f); 134 | 135 | monitor->null.ramp.red[i] = (unsigned short) value; 136 | monitor->null.ramp.green[i] = (unsigned short) value; 137 | monitor->null.ramp.blue[i] = (unsigned short) value; 138 | } 139 | } 140 | 141 | _glfwAllocGammaArrays(ramp, monitor->null.ramp.size); 142 | memcpy(ramp->red, monitor->null.ramp.red, sizeof(short) * ramp->size); 143 | memcpy(ramp->green, monitor->null.ramp.green, sizeof(short) * ramp->size); 144 | memcpy(ramp->blue, monitor->null.ramp.blue, sizeof(short) * ramp->size); 145 | return GLFW_TRUE; 146 | } 147 | 148 | void _glfwSetGammaRampNull(_GLFWmonitor* monitor, const GLFWgammaramp* ramp) 149 | { 150 | if (monitor->null.ramp.size != ramp->size) 151 | { 152 | _glfwInputError(GLFW_PLATFORM_ERROR, 153 | "Null: Gamma ramp size must match current ramp size"); 154 | return; 155 | } 156 | 157 | memcpy(monitor->null.ramp.red, ramp->red, sizeof(short) * ramp->size); 158 | memcpy(monitor->null.ramp.green, ramp->green, sizeof(short) * ramp->size); 159 | memcpy(monitor->null.ramp.blue, ramp->blue, sizeof(short) * ramp->size); 160 | } 161 | 162 | -------------------------------------------------------------------------------- /examples/triangle-opengles.c: -------------------------------------------------------------------------------- 1 | //======================================================================== 2 | // OpenGL ES 2.0 triangle example 3 | // Copyright (c) Camilla Löwy 4 | // 5 | // This software is provided 'as-is', without any express or implied 6 | // warranty. In no event will the authors be held liable for any damages 7 | // arising from the use of this software. 8 | // 9 | // Permission is granted to anyone to use this software for any purpose, 10 | // including commercial applications, and to alter it and redistribute it 11 | // freely, subject to the following restrictions: 12 | // 13 | // 1. The origin of this software must not be misrepresented; you must not 14 | // claim that you wrote the original software. If you use this software 15 | // in a product, an acknowledgment in the product documentation would 16 | // be appreciated but is not required. 17 | // 18 | // 2. Altered source versions must be plainly marked as such, and must not 19 | // be misrepresented as being the original software. 20 | // 21 | // 3. This notice may not be removed or altered from any source 22 | // distribution. 23 | // 24 | //======================================================================== 25 | 26 | #define GLAD_GLES2_IMPLEMENTATION 27 | #include 28 | #define GLFW_INCLUDE_NONE 29 | #include 30 | 31 | #include "linmath.h" 32 | 33 | #include 34 | #include 35 | #include 36 | 37 | typedef struct Vertex 38 | { 39 | vec2 pos; 40 | vec3 col; 41 | } Vertex; 42 | 43 | static const Vertex vertices[3] = 44 | { 45 | { { -0.6f, -0.4f }, { 1.f, 0.f, 0.f } }, 46 | { { 0.6f, -0.4f }, { 0.f, 1.f, 0.f } }, 47 | { { 0.f, 0.6f }, { 0.f, 0.f, 1.f } } 48 | }; 49 | 50 | static const char* vertex_shader_text = 51 | "#version 100\n" 52 | "precision mediump float;\n" 53 | "uniform mat4 MVP;\n" 54 | "attribute vec3 vCol;\n" 55 | "attribute vec2 vPos;\n" 56 | "varying vec3 color;\n" 57 | "void main()\n" 58 | "{\n" 59 | " gl_Position = MVP * vec4(vPos, 0.0, 1.0);\n" 60 | " color = vCol;\n" 61 | "}\n"; 62 | 63 | static const char* fragment_shader_text = 64 | "#version 100\n" 65 | "precision mediump float;\n" 66 | "varying vec3 color;\n" 67 | "void main()\n" 68 | "{\n" 69 | " gl_FragColor = vec4(color, 1.0);\n" 70 | "}\n"; 71 | 72 | static void error_callback(int error, const char* description) 73 | { 74 | fprintf(stderr, "GLFW Error: %s\n", description); 75 | } 76 | 77 | static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) 78 | { 79 | if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) 80 | glfwSetWindowShouldClose(window, GLFW_TRUE); 81 | } 82 | 83 | int main(void) 84 | { 85 | glfwSetErrorCallback(error_callback); 86 | 87 | if (!glfwInit()) 88 | exit(EXIT_FAILURE); 89 | 90 | glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_ES_API); 91 | glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2); 92 | glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0); 93 | glfwWindowHint(GLFW_CONTEXT_CREATION_API, GLFW_EGL_CONTEXT_API); 94 | 95 | GLFWwindow* window = glfwCreateWindow(640, 480, "OpenGL ES 2.0 Triangle (EGL)", NULL, NULL); 96 | if (!window) 97 | { 98 | glfwWindowHint(GLFW_CONTEXT_CREATION_API, GLFW_NATIVE_CONTEXT_API); 99 | window = glfwCreateWindow(640, 480, "OpenGL ES 2.0 Triangle", NULL, NULL); 100 | if (!window) 101 | { 102 | glfwTerminate(); 103 | exit(EXIT_FAILURE); 104 | } 105 | } 106 | 107 | glfwSetKeyCallback(window, key_callback); 108 | 109 | glfwMakeContextCurrent(window); 110 | gladLoadGLES2(glfwGetProcAddress); 111 | glfwSwapInterval(1); 112 | 113 | GLuint vertex_buffer; 114 | glGenBuffers(1, &vertex_buffer); 115 | glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer); 116 | glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); 117 | 118 | const GLuint vertex_shader = glCreateShader(GL_VERTEX_SHADER); 119 | glShaderSource(vertex_shader, 1, &vertex_shader_text, NULL); 120 | glCompileShader(vertex_shader); 121 | 122 | const GLuint fragment_shader = glCreateShader(GL_FRAGMENT_SHADER); 123 | glShaderSource(fragment_shader, 1, &fragment_shader_text, NULL); 124 | glCompileShader(fragment_shader); 125 | 126 | const GLuint program = glCreateProgram(); 127 | glAttachShader(program, vertex_shader); 128 | glAttachShader(program, fragment_shader); 129 | glLinkProgram(program); 130 | 131 | const GLint mvp_location = glGetUniformLocation(program, "MVP"); 132 | const GLint vpos_location = glGetAttribLocation(program, "vPos"); 133 | const GLint vcol_location = glGetAttribLocation(program, "vCol"); 134 | 135 | glEnableVertexAttribArray(vpos_location); 136 | glEnableVertexAttribArray(vcol_location); 137 | glVertexAttribPointer(vpos_location, 2, GL_FLOAT, GL_FALSE, 138 | sizeof(Vertex), (void*) offsetof(Vertex, pos)); 139 | glVertexAttribPointer(vcol_location, 3, GL_FLOAT, GL_FALSE, 140 | sizeof(Vertex), (void*) offsetof(Vertex, col)); 141 | 142 | while (!glfwWindowShouldClose(window)) 143 | { 144 | int width, height; 145 | glfwGetFramebufferSize(window, &width, &height); 146 | const float ratio = width / (float) height; 147 | 148 | glViewport(0, 0, width, height); 149 | glClear(GL_COLOR_BUFFER_BIT); 150 | 151 | mat4x4 m, p, mvp; 152 | mat4x4_identity(m); 153 | mat4x4_rotate_Z(m, m, (float) glfwGetTime()); 154 | mat4x4_ortho(p, -ratio, ratio, -1.f, 1.f, 1.f, -1.f); 155 | mat4x4_mul(mvp, p, m); 156 | 157 | glUseProgram(program); 158 | glUniformMatrix4fv(mvp_location, 1, GL_FALSE, (const GLfloat*) &mvp); 159 | glDrawArrays(GL_TRIANGLES, 0, 3); 160 | 161 | glfwSwapBuffers(window); 162 | glfwPollEvents(); 163 | } 164 | 165 | glfwDestroyWindow(window); 166 | 167 | glfwTerminate(); 168 | exit(EXIT_SUCCESS); 169 | } 170 | 171 | -------------------------------------------------------------------------------- /CONTRIBUTORS.md: -------------------------------------------------------------------------------- 1 | # Acknowledgements 2 | 3 | GLFW exists because people around the world donated their time and lent their 4 | skills. This list only includes contributions to the main repository and 5 | excludes other invaluable contributions like language bindings and text and 6 | video tutorials. 7 | 8 | - Bobyshev Alexander 9 | - Laurent Aphecetche 10 | - Matt Arsenault 11 | - Takuro Ashie 12 | - ashishgamedev 13 | - David Avedissian 14 | - Luca Bacci 15 | - Keith Bauer 16 | - John Bartholomew 17 | - Coşku Baş 18 | - Niklas Behrens 19 | - Andrew Belt 20 | - Nevyn Bengtsson 21 | - Niklas Bergström 22 | - Denis Bernard 23 | - BiBi 24 | - Doug Binks 25 | - blanco 26 | - Waris Boonyasiriwat 27 | - Kyle Brenneman 28 | - Rok Breulj 29 | - TheBrokenRail 30 | - Kai Burjack 31 | - Martin Capitanio 32 | - Nicolas Caramelli 33 | - David Carlier 34 | - Arturo Castro 35 | - Chi-kwan Chan 36 | - TheChocolateOre 37 | - Ali Chraghi 38 | - Joseph Chua 39 | - Ian Clarkson 40 | - Michał Cichoń 41 | - Lambert Clara 42 | - Anna Clarke 43 | - Josh Codd 44 | - Yaron Cohen-Tal 45 | - Omar Cornut 46 | - Andrew Corrigan 47 | - Bailey Cosier 48 | - Noel Cower 49 | - CuriouserThing 50 | - Jason Daly 51 | - danhambleton 52 | - Jarrod Davis 53 | - Olivier Delannoy 54 | - Paul R. Deppe 55 | - Michael Dickens 56 | - Роман Донченко 57 | - Mario Dorn 58 | - Wolfgang Draxinger 59 | - Jonathan Dummer 60 | - Ralph Eastwood 61 | - Fredrik Ehnbom 62 | - Robin Eklind 63 | - Jan Ekström 64 | - Siavash Eliasi 65 | - Ahmad Fatoum 66 | - Nikita Fediuchin 67 | - Felipe Ferreira 68 | - Michael Fogleman 69 | - Jason Francis 70 | - Gerald Franz 71 | - Mário Freitas 72 | - GeO4d 73 | - Marcus Geelnard 74 | - ghuser404 75 | - Charles Giessen 76 | - Ryan C. Gordon 77 | - Stephen Gowen 78 | - Kovid Goyal 79 | - Kevin Grandemange 80 | - Eloi Marín Gratacós 81 | - Stefan Gustavson 82 | - Andrew Gutekanst 83 | - Stephen Gutekanst 84 | - Jonathan Hale 85 | - hdf89shfdfs 86 | - Sylvain Hellegouarch 87 | - Björn Hempel 88 | - Matthew Henry 89 | - heromyth 90 | - Lucas Hinderberger 91 | - Paul Holden 92 | - Hajime Hoshi 93 | - Warren Hu 94 | - Charles Huber 95 | - Brent Huisman 96 | - illustris 97 | - InKryption 98 | - IntellectualKitty 99 | - Aaron Jacobs 100 | - JannikGM 101 | - Erik S. V. Jansson 102 | - jjYBdx4IL 103 | - Toni Jovanoski 104 | - Arseny Kapoulkine 105 | - Cem Karan 106 | - Osman Keskin 107 | - Koray Kilinc 108 | - Josh Kilmer 109 | - Byunghoon Kim 110 | - Cameron King 111 | - Peter Knut 112 | - Christoph Kubisch 113 | - Yuri Kunde Schlesner 114 | - Rokas Kupstys 115 | - Konstantin Käfer 116 | - Eric Larson 117 | - Francis Lecavalier 118 | - Jong Won Lee 119 | - Robin Leffmann 120 | - Glenn Lewis 121 | - Shane Liesegang 122 | - Anders Lindqvist 123 | - Leon Linhart 124 | - Marco Lizza 125 | - Eyal Lotem 126 | - Aaron Loucks 127 | - Luflosi 128 | - lukect 129 | - Tristam MacDonald 130 | - Hans Mackowiak 131 | - Ramiro Magno 132 | - Дмитри Малышев 133 | - Zbigniew Mandziejewicz 134 | - Adam Marcus 135 | - Célestin Marot 136 | - Kyle McDonald 137 | - David V. McKay 138 | - David Medlock 139 | - Bryce Mehring 140 | - Jonathan Mercier 141 | - Marcel Metz 142 | - Liam Middlebrook 143 | - Ave Milia 144 | - Jonathan Miller 145 | - Kenneth Miller 146 | - Bruce Mitchener 147 | - Jack Moffitt 148 | - Ravi Mohan 149 | - Jeff Molofee 150 | - Alexander Monakov 151 | - Pierre Morel 152 | - Jon Morton 153 | - Pierre Moulon 154 | - Martins Mozeiko 155 | - Pascal Muetschard 156 | - James Murphy 157 | - Julian Møller 158 | - ndogxj 159 | - F. Nedelec 160 | - n3rdopolis 161 | - Kristian Nielsen 162 | - Joel Niemelä 163 | - Kamil Nowakowski 164 | - onox 165 | - Denis Ovod 166 | - Ozzy 167 | - Andri Pálsson 168 | - luz paz 169 | - Peoro 170 | - Braden Pellett 171 | - Christopher Pelloux 172 | - Michael Pennington 173 | - Arturo J. Pérez 174 | - Vladimir Perminov 175 | - Olivier Perret 176 | - Anthony Pesch 177 | - Orson Peters 178 | - Emmanuel Gil Peyrot 179 | - Cyril Pichard 180 | - Pilzschaf 181 | - Keith Pitt 182 | - Stanislav Podgorskiy 183 | - Konstantin Podsvirov 184 | - Nathan Poirier 185 | - Alexandre Pretyman 186 | - Pablo Prietz 187 | - przemekmirek 188 | - pthom 189 | - Martin Pulec 190 | - Guillaume Racicot 191 | - Juan Ramos 192 | - Christian Rauch 193 | - Philip Rideout 194 | - Eddie Ringle 195 | - Max Risuhin 196 | - Joe Roback 197 | - Jorge Rodriguez 198 | - Jari Ronkainen 199 | - Luca Rood 200 | - Ed Ropple 201 | - Aleksey Rybalkin 202 | - Mikko Rytkönen 203 | - Riku Salminen 204 | - Yoshinori Sano 205 | - Brandon Schaefer 206 | - Sebastian Schuberth 207 | - Christian Sdunek 208 | - Matt Sealey 209 | - Steve Sexton 210 | - Arkady Shapkin 211 | - Ali Sherief 212 | - Yoshiki Shibukawa 213 | - Dmitri Shuralyov 214 | - Joao da Silva 215 | - Daniel Sieger 216 | - Daniel Skorupski 217 | - Slemmie 218 | - Anthony Smith 219 | - Bradley Smith 220 | - Cliff Smolinsky 221 | - Patrick Snape 222 | - Erlend Sogge Heggen 223 | - Olivier Sohn 224 | - Julian Squires 225 | - Johannes Stein 226 | - Pontus Stenetorp 227 | - Michael Stocker 228 | - Justin Stoecker 229 | - Elviss Strazdins 230 | - Paul Sultana 231 | - Nathan Sweet 232 | - TTK-Bandit 233 | - Jared Tiala 234 | - Sergey Tikhomirov 235 | - Arthur Tombs 236 | - TronicLabs 237 | - Ioannis Tsakpinis 238 | - Samuli Tuomola 239 | - Matthew Turner 240 | - urraka 241 | - Elias Vanderstuyft 242 | - Stef Velzel 243 | - Jari Vetoniemi 244 | - Ricardo Vieira 245 | - Nicholas Vitovitch 246 | - Simon Voordouw 247 | - Corentin Wallez 248 | - Torsten Walluhn 249 | - Patrick Walton 250 | - Xo Wang 251 | - Andre Weissflog 252 | - Jay Weisskopf 253 | - Frank Wille 254 | - Andy Williams 255 | - Joel Winarske 256 | - Richard A. Wilkes 257 | - Tatsuya Yatagawa 258 | - Ryogo Yoshimura 259 | - Lukas Zanner 260 | - Andrey Zholos 261 | - Aihui Zhu 262 | - Santi Zupancic 263 | - Jonas Ådahl 264 | - Lasse Öörni 265 | - Leonard König 266 | - All the unmentioned and anonymous contributors in the GLFW community, for bug 267 | reports, patches, feedback, testing and encouragement 268 | 269 | -------------------------------------------------------------------------------- /docs/extra.css: -------------------------------------------------------------------------------- 1 | .sm-dox,.sm-dox a,.sm-dox a:focus,.sm-dox a:active,.sm-dox a:hover,.sm-dox a.highlighted,.sm-dox ul a:hover{background:none;text-shadow:none}.sm-dox a span.sub-arrow{border-color:#f2f2f2 transparent transparent transparent}.sm-dox a span.sub-arrow:active,.sm-dox a span.sub-arrow:focus,.sm-dox a span.sub-arrow:hover,.sm-dox a:hover span.sub-arrow{border-color:#f60 transparent transparent transparent}.sm-dox ul a span.sub-arrow:active,.sm-dox ul a span.sub-arrow:focus,.sm-dox ul a span.sub-arrow:hover,.sm-dox ul a:hover span.sub-arrow{border-color:transparent transparent transparent #f60}.sm-dox ul a:hover{background:#666;text-shadow:none}.sm-dox ul.sm-nowrap a{color:#4d4d4d;text-shadow:none}#main-nav,#main-menu,#main-menu a,#main-menu a:visited,#main-menu a:hover,#main-menu li,.memdoc,dl.reflist dd,div.toc li,.ah,span.lineno,span.lineno a,span.lineno a:hover,.note code,.pre code,.post code,.invariant code,.warning code,.attention code,.deprecated code,.bug code,.todo code,.test code,.doxtable code,.markdownTable code{background:none}#titlearea,.footer,.contents,div.header,.memdoc,table.doxtable td,table.doxtable th,table.markdownTable td,table.markdownTable th,hr,.memSeparator{border:none}#main-menu a,#main-menu a:visited,#main-menu a:hover,#main-menu li,.reflist dt a.el,.levels span,.directory .levels span{text-shadow:none}.memdoc,dl.reflist dd{box-shadow:none}div.headertitle,.note code,.pre code,.post code,.invariant code,.warning code,.attention code,.deprecated code,.bug code,.todo code,.test code,table.doxtable code,table.markdownTable code{padding:0}#nav-path,.directory .levels,span.lineno{display:none}html,#titlearea,.footer,tr.even,.directory tr.even,.doxtable tr:nth-child(even),tr.markdownTableBody:nth-child(even),.mdescLeft,.mdescRight,.memItemLeft,.memItemRight,code,.markdownTableRowEven{background:#f2f2f2}body{color:#4d4d4d}div.title{font-size:170%;margin:1em 0 0.5em 0}h1,h2,h2.groupheader,h3,div.toc h3,h4,h5,h6,strong,em{color:#1a1a1a;border-bottom:none}h1{padding-top:0.5em;font-size:150%}h2{padding-top:0.5em;margin-bottom:0;font-size:130%}h3{padding-top:0.5em;margin-bottom:0;font-size:110%}.glfwheader{font-size:16px;min-height:64px;max-width:920px;padding:0 32px;margin:0 auto;display:flex;flex-direction:row;flex-wrap:wrap;justify-content:flex-start;align-items:center;align-content:stretch}#glfwhome{line-height:64px;padding-right:48px;color:#666;font-size:2.5em;background:url("https://www.glfw.org/css/arrow.png") no-repeat right}.glfwnavbar{list-style-type:none;margin:0 0 0 auto;float:right}#glfwhome,.glfwnavbar li{float:left}.glfwnavbar a,.glfwnavbar a:visited{line-height:64px;margin-left:2em;display:block;color:#666}.glfwnavbar{padding-left:0}#glfwhome,.glfwnavbar a,.glfwnavbar a:visited{transition:.35s ease}#titlearea,.footer{color:#666}address.footer{text-align:center;padding:2em;margin-top:3em}#top{background:#666}#main-nav{max-width:960px;margin:0 auto;font-size:13px}#main-menu{max-width:920px;margin:0 auto;font-size:13px}.memtitle{display:none}.memproto,.memname{font-weight:bold;text-shadow:none}#main-menu{min-height:36px;display:flex;flex-direction:row;flex-wrap:wrap;justify-content:flex-start;align-items:center;align-content:stretch}#main-menu a,#main-menu a:visited,#main-menu a:hover,#main-menu li{color:#f2f2f2}#main-menu li ul.sm-nowrap li a{color:#4d4d4d}#main-menu li ul.sm-nowrap li a:hover{color:#f60}#main-menu>li:last-child{margin:0 0 0 auto}.contents{min-height:590px}div.contents,div.header{max-width:920px;margin:0 auto;padding:0 32px;background:#fff none}table.doxtable th,table.markdownTable th,dl.reflist dt{background:linear-gradient(to bottom, #ffa733 0%, #f60 100%);box-shadow:inset 0 0 32px #f60;text-shadow:0 -1px 1px #b34700;text-align:left;color:#fff}dl.reflist dt a.el{color:#f60;padding:.2em;border-radius:4px;background-color:#ffe0cc}div.toc{float:right;width:35%}@media screen and (max-width: 600px){div.toc{float:none;width:inherit;margin:0}}div.toc h3{font-size:1.17em}div.toc ul{padding-left:1.5em}div.toc li{font-size:1em;padding-left:0;list-style-type:disc}div.toc li.level2,div.toc li.level3{margin-left:0.5em}div.toc,.memproto,div.qindex,div.ah{background:linear-gradient(to bottom, #f2f2f2 0%, #e6e6e6 100%);box-shadow:inset 0 0 32px #e6e6e6;text-shadow:0 1px 1px #fff;color:#1a1a1a;border:2px solid #e6e6e6;border-radius:4px}.paramname{color:#803300}dl.reflist dt{border:2px solid #f60;border-top-left-radius:4px;border-top-right-radius:4px;border-bottom:none}dl.reflist dd{border:2px solid #f60;border-bottom-right-radius:4px;border-bottom-left-radius:4px;border-top:none}table.doxtable,table.markdownTable{border-collapse:inherit;border-spacing:0;border:2px solid #f60;border-radius:4px}a,a:hover,a:visited,a:visited:hover,.contents a:visited,.el,a.el:visited,#glfwhome:hover,#main-menu a:hover,span.lineno a:hover{color:#f60;text-decoration:none}div.directory{border-collapse:inherit;border-spacing:0;border:2px solid #f60;border-radius:4px}hr,.memSeparator{height:2px;background:linear-gradient(to right, #f2f2f2 0%, #d9d9d9 50%, #f2f2f2 100%)}dl.note,dl.pre,dl.post,dl.invariant{background:linear-gradient(to bottom, #ddfad1 0%, #cbf7ba 100%);box-shadow:inset 0 0 32px #baf5a3;color:#1e5309;border:2px solid #afe699}dl.warning,dl.attention{background:linear-gradient(to bottom, #fae8d1 0%, #f7ddba 100%);box-shadow:inset 0 0 32px #f5d1a3;color:#533309;border:2px solid #e6c499}dl.deprecated,dl.bug{background:linear-gradient(to bottom, #fad1e3 0%, #f7bad6 100%);box-shadow:inset 0 0 32px #f5a3c8;color:#53092a;border:2px solid #e699bb}dl.todo,dl.test{background:linear-gradient(to bottom, #d1ecfa 0%, #bae3f7 100%);box-shadow:inset 0 0 32px #a3daf5;color:#093a53;border:2px solid #99cce6}dl.note,dl.pre,dl.post,dl.invariant,dl.warning,dl.attention,dl.deprecated,dl.bug,dl.todo,dl.test{border-radius:4px;padding:1em;text-shadow:0 1px 1px #fff;margin:1em 0}.note a,.pre a,.post a,.invariant a,.warning a,.attention a,.deprecated a,.bug a,.todo a,.test a,.note a:visited,.pre a:visited,.post a:visited,.invariant a:visited,.warning a:visited,.attention a:visited,.deprecated a:visited,.bug a:visited,.todo a:visited,.test a:visited{color:inherit}div.line{line-height:inherit}div.fragment,pre.fragment{background:#f2f2f2;border-radius:4px;border:none;padding:1em;overflow:auto;border-left:4px solid #ccc;margin:1em 0}.lineno a,.lineno a:visited,.line,pre.fragment{color:#4d4d4d}span.preprocessor,span.comment{color:#007899}a.code,a.code:visited{color:#e64500}span.keyword,span.keywordtype,span.keywordflow{color:#404040;font-weight:bold}span.stringliteral{color:#360099}code{padding:.1em;border-radius:4px} 2 | /*# sourceMappingURL=extra.css.map */ 3 | -------------------------------------------------------------------------------- /tests/gamma.c: -------------------------------------------------------------------------------- 1 | //======================================================================== 2 | // Gamma correction test program 3 | // Copyright (c) Camilla Löwy 4 | // 5 | // This software is provided 'as-is', without any express or implied 6 | // warranty. In no event will the authors be held liable for any damages 7 | // arising from the use of this software. 8 | // 9 | // Permission is granted to anyone to use this software for any purpose, 10 | // including commercial applications, and to alter it and redistribute it 11 | // freely, subject to the following restrictions: 12 | // 13 | // 1. The origin of this software must not be misrepresented; you must not 14 | // claim that you wrote the original software. If you use this software 15 | // in a product, an acknowledgment in the product documentation would 16 | // be appreciated but is not required. 17 | // 18 | // 2. Altered source versions must be plainly marked as such, and must not 19 | // be misrepresented as being the original software. 20 | // 21 | // 3. This notice may not be removed or altered from any source 22 | // distribution. 23 | // 24 | //======================================================================== 25 | // 26 | // This program is used to test the gamma correction functionality for 27 | // both full screen and windowed mode windows 28 | // 29 | //======================================================================== 30 | 31 | #define GLAD_GL_IMPLEMENTATION 32 | #include 33 | #define GLFW_INCLUDE_NONE 34 | #include 35 | 36 | #define NK_IMPLEMENTATION 37 | #define NK_INCLUDE_FIXED_TYPES 38 | #define NK_INCLUDE_FONT_BAKING 39 | #define NK_INCLUDE_DEFAULT_FONT 40 | #define NK_INCLUDE_DEFAULT_ALLOCATOR 41 | #define NK_INCLUDE_VERTEX_BUFFER_OUTPUT 42 | #define NK_INCLUDE_STANDARD_VARARGS 43 | #define NK_BUTTON_TRIGGER_ON_RELEASE 44 | #include 45 | 46 | #define NK_GLFW_GL2_IMPLEMENTATION 47 | #include 48 | 49 | #include 50 | #include 51 | #include 52 | 53 | static void error_callback(int error, const char* description) 54 | { 55 | fprintf(stderr, "Error: %s\n", description); 56 | } 57 | 58 | static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) 59 | { 60 | if (action == GLFW_PRESS && key == GLFW_KEY_ESCAPE) 61 | glfwSetWindowShouldClose(window, GLFW_TRUE); 62 | } 63 | 64 | static void chart_ramp_array(struct nk_context* nk, 65 | struct nk_color color, 66 | int count, unsigned short int* values) 67 | { 68 | if (nk_chart_begin_colored(nk, NK_CHART_LINES, 69 | color, nk_rgb(255, 255, 255), 70 | count, 0, 65535)) 71 | { 72 | int i; 73 | for (i = 0; i < count; i++) 74 | { 75 | char buffer[1024]; 76 | if (nk_chart_push(nk, values[i])) 77 | { 78 | snprintf(buffer, sizeof(buffer), "#%u: %u (%0.5f) ", 79 | i, values[i], values[i] / 65535.f); 80 | nk_tooltip(nk, buffer); 81 | } 82 | } 83 | 84 | nk_chart_end(nk); 85 | } 86 | } 87 | 88 | int main(int argc, char** argv) 89 | { 90 | GLFWmonitor* monitor = NULL; 91 | GLFWwindow* window; 92 | GLFWgammaramp orig_ramp; 93 | struct nk_context* nk; 94 | struct nk_font_atlas* atlas; 95 | float gamma_value = 1.f; 96 | 97 | glfwSetErrorCallback(error_callback); 98 | 99 | if (!glfwInit()) 100 | exit(EXIT_FAILURE); 101 | 102 | monitor = glfwGetPrimaryMonitor(); 103 | 104 | glfwWindowHint(GLFW_SCALE_TO_MONITOR, GLFW_TRUE); 105 | glfwWindowHint(GLFW_WIN32_KEYBOARD_MENU, GLFW_TRUE); 106 | 107 | window = glfwCreateWindow(800, 400, "Gamma Test", NULL, NULL); 108 | if (!window) 109 | { 110 | glfwTerminate(); 111 | exit(EXIT_FAILURE); 112 | } 113 | 114 | { 115 | const GLFWgammaramp* ramp = glfwGetGammaRamp(monitor); 116 | if (!ramp) 117 | { 118 | glfwTerminate(); 119 | exit(EXIT_FAILURE); 120 | } 121 | 122 | const size_t array_size = ramp->size * sizeof(short); 123 | orig_ramp.size = ramp->size; 124 | orig_ramp.red = malloc(array_size); 125 | orig_ramp.green = malloc(array_size); 126 | orig_ramp.blue = malloc(array_size); 127 | memcpy(orig_ramp.red, ramp->red, array_size); 128 | memcpy(orig_ramp.green, ramp->green, array_size); 129 | memcpy(orig_ramp.blue, ramp->blue, array_size); 130 | } 131 | 132 | glfwMakeContextCurrent(window); 133 | gladLoadGL(glfwGetProcAddress); 134 | glfwSwapInterval(1); 135 | 136 | nk = nk_glfw3_init(window, NK_GLFW3_INSTALL_CALLBACKS); 137 | nk_glfw3_font_stash_begin(&atlas); 138 | nk_glfw3_font_stash_end(); 139 | 140 | glfwSetKeyCallback(window, key_callback); 141 | 142 | while (!glfwWindowShouldClose(window)) 143 | { 144 | int width, height; 145 | struct nk_rect area; 146 | 147 | glfwGetWindowSize(window, &width, &height); 148 | area = nk_rect(0.f, 0.f, (float) width, (float) height); 149 | nk_window_set_bounds(nk, "", area); 150 | 151 | glClear(GL_COLOR_BUFFER_BIT); 152 | nk_glfw3_new_frame(); 153 | if (nk_begin(nk, "", area, 0)) 154 | { 155 | const GLFWgammaramp* ramp; 156 | 157 | nk_layout_row_dynamic(nk, 30, 3); 158 | if (nk_slider_float(nk, 0.1f, &gamma_value, 5.f, 0.1f)) 159 | glfwSetGamma(monitor, gamma_value); 160 | nk_labelf(nk, NK_TEXT_LEFT, "%0.1f", gamma_value); 161 | if (nk_button_label(nk, "Revert")) 162 | glfwSetGammaRamp(monitor, &orig_ramp); 163 | 164 | ramp = glfwGetGammaRamp(monitor); 165 | 166 | nk_layout_row_dynamic(nk, height - 60.f, 3); 167 | chart_ramp_array(nk, nk_rgb(255, 0, 0), ramp->size, ramp->red); 168 | chart_ramp_array(nk, nk_rgb(0, 255, 0), ramp->size, ramp->green); 169 | chart_ramp_array(nk, nk_rgb(0, 0, 255), ramp->size, ramp->blue); 170 | } 171 | 172 | nk_end(nk); 173 | nk_glfw3_render(NK_ANTI_ALIASING_ON); 174 | 175 | glfwSwapBuffers(window); 176 | glfwWaitEventsTimeout(1.0); 177 | } 178 | 179 | free(orig_ramp.red); 180 | free(orig_ramp.green); 181 | free(orig_ramp.blue); 182 | 183 | nk_glfw3_shutdown(); 184 | glfwTerminate(); 185 | exit(EXIT_SUCCESS); 186 | } 187 | 188 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.4...3.20 FATAL_ERROR) 2 | 3 | project(GLFW VERSION 3.4.0 LANGUAGES C) 4 | 5 | if (POLICY CMP0069) 6 | cmake_policy(SET CMP0069 NEW) 7 | endif() 8 | 9 | if (POLICY CMP0077) 10 | cmake_policy(SET CMP0077 NEW) 11 | endif() 12 | 13 | set_property(GLOBAL PROPERTY USE_FOLDERS ON) 14 | 15 | string(COMPARE EQUAL "${CMAKE_CURRENT_SOURCE_DIR}" "${CMAKE_SOURCE_DIR}" GLFW_STANDALONE) 16 | 17 | option(BUILD_SHARED_LIBS "Build shared libraries" OFF) 18 | option(GLFW_BUILD_EXAMPLES "Build the GLFW example programs" ${GLFW_STANDALONE}) 19 | option(GLFW_BUILD_TESTS "Build the GLFW test programs" ${GLFW_STANDALONE}) 20 | option(GLFW_BUILD_DOCS "Build the GLFW documentation" ON) 21 | option(GLFW_INSTALL "Generate installation target" ON) 22 | 23 | include(GNUInstallDirs) 24 | include(CMakeDependentOption) 25 | 26 | if (GLFW_USE_OSMESA) 27 | message(FATAL_ERROR "GLFW_USE_OSMESA has been removed; set the GLFW_PLATFORM init hint") 28 | endif() 29 | 30 | cmake_dependent_option(GLFW_BUILD_WIN32 "Build support for Win32" ON "WIN32" OFF) 31 | cmake_dependent_option(GLFW_BUILD_COCOA "Build support for Cocoa" ON "APPLE" OFF) 32 | cmake_dependent_option(GLFW_BUILD_X11 "Build support for X11" ON "UNIX;NOT APPLE" OFF) 33 | cmake_dependent_option(GLFW_BUILD_WAYLAND "Build support for Wayland" 34 | "${GLFW_USE_WAYLAND}" "UNIX;NOT APPLE" OFF) 35 | 36 | cmake_dependent_option(GLFW_USE_HYBRID_HPG "Force use of high-performance GPU on hybrid systems" OFF 37 | "WIN32" OFF) 38 | cmake_dependent_option(USE_MSVC_RUNTIME_LIBRARY_DLL "Use MSVC runtime library DLL" ON 39 | "MSVC" OFF) 40 | 41 | set(GLFW_LIBRARY_TYPE "${GLFW_LIBRARY_TYPE}" CACHE STRING 42 | "Library type override for GLFW (SHARED, STATIC, OBJECT, or empty to follow BUILD_SHARED_LIBS)") 43 | 44 | if (GLFW_LIBRARY_TYPE) 45 | if (GLFW_LIBRARY_TYPE STREQUAL "SHARED") 46 | set(GLFW_BUILD_SHARED_LIBRARY TRUE) 47 | else() 48 | set(GLFW_BUILD_SHARED_LIBRARY FALSE) 49 | endif() 50 | else() 51 | set(GLFW_BUILD_SHARED_LIBRARY ${BUILD_SHARED_LIBS}) 52 | endif() 53 | 54 | list(APPEND CMAKE_MODULE_PATH "${GLFW_SOURCE_DIR}/CMake/modules") 55 | 56 | find_package(Threads REQUIRED) 57 | 58 | if (GLFW_BUILD_DOCS) 59 | set(DOXYGEN_SKIP_DOT TRUE) 60 | find_package(Doxygen) 61 | endif() 62 | 63 | #-------------------------------------------------------------------- 64 | # Report backend selection 65 | #-------------------------------------------------------------------- 66 | if (GLFW_BUILD_WIN32) 67 | message(STATUS "Including Win32 support") 68 | endif() 69 | if (GLFW_BUILD_COCOA) 70 | message(STATUS "Including Cocoa support") 71 | endif() 72 | if (GLFW_BUILD_WAYLAND) 73 | message(STATUS "Including Wayland support") 74 | endif() 75 | if (GLFW_BUILD_X11) 76 | message(STATUS "Including X11 support") 77 | endif() 78 | 79 | #-------------------------------------------------------------------- 80 | # Apply Microsoft C runtime library option 81 | # This is here because it also applies to tests and examples 82 | #-------------------------------------------------------------------- 83 | if (MSVC AND NOT USE_MSVC_RUNTIME_LIBRARY_DLL) 84 | if (CMAKE_VERSION VERSION_LESS 3.15) 85 | foreach (flag CMAKE_C_FLAGS 86 | CMAKE_C_FLAGS_DEBUG 87 | CMAKE_C_FLAGS_RELEASE 88 | CMAKE_C_FLAGS_MINSIZEREL 89 | CMAKE_C_FLAGS_RELWITHDEBINFO) 90 | 91 | if (flag MATCHES "/MD") 92 | string(REGEX REPLACE "/MD" "/MT" ${flag} "${${flag}}") 93 | endif() 94 | if (flag MATCHES "/MDd") 95 | string(REGEX REPLACE "/MDd" "/MTd" ${flag} "${${flag}}") 96 | endif() 97 | 98 | endforeach() 99 | else() 100 | set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$:Debug>") 101 | endif() 102 | endif() 103 | 104 | #-------------------------------------------------------------------- 105 | # Create generated files 106 | #-------------------------------------------------------------------- 107 | include(CMakePackageConfigHelpers) 108 | 109 | set(GLFW_CONFIG_PATH "${CMAKE_INSTALL_LIBDIR}/cmake/glfw3") 110 | 111 | configure_package_config_file(CMake/glfw3Config.cmake.in 112 | src/glfw3Config.cmake 113 | INSTALL_DESTINATION "${GLFW_CONFIG_PATH}" 114 | NO_CHECK_REQUIRED_COMPONENTS_MACRO) 115 | 116 | write_basic_package_version_file(src/glfw3ConfigVersion.cmake 117 | VERSION ${GLFW_VERSION} 118 | COMPATIBILITY SameMajorVersion) 119 | 120 | #-------------------------------------------------------------------- 121 | # Add subdirectories 122 | #-------------------------------------------------------------------- 123 | add_subdirectory(src) 124 | 125 | if (GLFW_BUILD_EXAMPLES) 126 | add_subdirectory(examples) 127 | endif() 128 | 129 | if (GLFW_BUILD_TESTS) 130 | add_subdirectory(tests) 131 | endif() 132 | 133 | if (DOXYGEN_FOUND AND GLFW_BUILD_DOCS) 134 | add_subdirectory(docs) 135 | endif() 136 | 137 | #-------------------------------------------------------------------- 138 | # Install files other than the library 139 | # The library is installed by src/CMakeLists.txt 140 | #-------------------------------------------------------------------- 141 | if (GLFW_INSTALL) 142 | install(DIRECTORY include/GLFW DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} 143 | FILES_MATCHING PATTERN glfw3.h PATTERN glfw3native.h) 144 | 145 | install(FILES "${GLFW_BINARY_DIR}/src/glfw3Config.cmake" 146 | "${GLFW_BINARY_DIR}/src/glfw3ConfigVersion.cmake" 147 | DESTINATION "${GLFW_CONFIG_PATH}") 148 | 149 | install(EXPORT glfwTargets FILE glfw3Targets.cmake 150 | EXPORT_LINK_INTERFACE_LIBRARIES 151 | DESTINATION "${GLFW_CONFIG_PATH}") 152 | install(FILES "${GLFW_BINARY_DIR}/src/glfw3.pc" 153 | DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig") 154 | 155 | if (DOXYGEN_FOUND AND GLFW_BUILD_DOCS) 156 | install(DIRECTORY "${GLFW_BINARY_DIR}/docs/html" 157 | DESTINATION "${CMAKE_INSTALL_DOCDIR}") 158 | endif() 159 | 160 | # Only generate this target if no higher-level project already has 161 | if (NOT TARGET uninstall) 162 | configure_file(CMake/cmake_uninstall.cmake.in 163 | cmake_uninstall.cmake IMMEDIATE @ONLY) 164 | 165 | add_custom_target(uninstall 166 | "${CMAKE_COMMAND}" -P 167 | "${GLFW_BINARY_DIR}/cmake_uninstall.cmake") 168 | set_target_properties(uninstall PROPERTIES FOLDER "GLFW3") 169 | endif() 170 | endif() 171 | 172 | -------------------------------------------------------------------------------- /src/platform.c: -------------------------------------------------------------------------------- 1 | //======================================================================== 2 | // GLFW 3.4 - www.glfw.org 3 | //------------------------------------------------------------------------ 4 | // Copyright (c) 2002-2006 Marcus Geelnard 5 | // Copyright (c) 2006-2018 Camilla Löwy 6 | // 7 | // This software is provided 'as-is', without any express or implied 8 | // warranty. In no event will the authors be held liable for any damages 9 | // arising from the use of this software. 10 | // 11 | // Permission is granted to anyone to use this software for any purpose, 12 | // including commercial applications, and to alter it and redistribute it 13 | // freely, subject to the following restrictions: 14 | // 15 | // 1. The origin of this software must not be misrepresented; you must not 16 | // claim that you wrote the original software. If you use this software 17 | // in a product, an acknowledgment in the product documentation would 18 | // be appreciated but is not required. 19 | // 20 | // 2. Altered source versions must be plainly marked as such, and must not 21 | // be misrepresented as being the original software. 22 | // 23 | // 3. This notice may not be removed or altered from any source 24 | // distribution. 25 | // 26 | //======================================================================== 27 | // Please use C89 style variable declarations in this file because VS 2010 28 | //======================================================================== 29 | 30 | #include "internal.h" 31 | 32 | // These construct a string literal from individual numeric constants 33 | #define _GLFW_CONCAT_VERSION(m, n, r) #m "." #n "." #r 34 | #define _GLFW_MAKE_VERSION(m, n, r) _GLFW_CONCAT_VERSION(m, n, r) 35 | 36 | ////////////////////////////////////////////////////////////////////////// 37 | ////// GLFW internal API ////// 38 | ////////////////////////////////////////////////////////////////////////// 39 | 40 | static const struct 41 | { 42 | int ID; 43 | GLFWbool (*connect)(int,_GLFWplatform*); 44 | } supportedPlatforms[] = 45 | { 46 | #if defined(_GLFW_WIN32) 47 | { GLFW_PLATFORM_WIN32, _glfwConnectWin32 }, 48 | #endif 49 | #if defined(_GLFW_COCOA) 50 | { GLFW_PLATFORM_COCOA, _glfwConnectCocoa }, 51 | #endif 52 | #if defined(_GLFW_X11) 53 | { GLFW_PLATFORM_X11, _glfwConnectX11 }, 54 | #endif 55 | #if defined(_GLFW_WAYLAND) 56 | { GLFW_PLATFORM_WAYLAND, _glfwConnectWayland }, 57 | #endif 58 | }; 59 | 60 | GLFWbool _glfwSelectPlatform(int desiredID, _GLFWplatform* platform) 61 | { 62 | const size_t count = sizeof(supportedPlatforms) / sizeof(supportedPlatforms[0]); 63 | size_t i; 64 | 65 | if (desiredID != GLFW_ANY_PLATFORM && 66 | desiredID != GLFW_PLATFORM_WIN32 && 67 | desiredID != GLFW_PLATFORM_COCOA && 68 | desiredID != GLFW_PLATFORM_WAYLAND && 69 | desiredID != GLFW_PLATFORM_X11 && 70 | desiredID != GLFW_PLATFORM_NULL) 71 | { 72 | _glfwInputError(GLFW_INVALID_ENUM, "Invalid platform ID 0x%08X", desiredID); 73 | return GLFW_FALSE; 74 | } 75 | 76 | // Only allow the Null platform if specifically requested 77 | if (desiredID == GLFW_PLATFORM_NULL) 78 | return _glfwConnectNull(desiredID, platform); 79 | else if (count == 0) 80 | { 81 | _glfwInputError(GLFW_PLATFORM_UNAVAILABLE, "This binary only supports the Null platform"); 82 | return GLFW_FALSE; 83 | } 84 | 85 | if (desiredID == GLFW_ANY_PLATFORM) 86 | { 87 | // If there is exactly one platform available for auto-selection, let it emit the 88 | // error on failure as the platform-specific error description may be more helpful 89 | if (count == 1) 90 | return supportedPlatforms[0].connect(supportedPlatforms[0].ID, platform); 91 | 92 | for (i = 0; i < count; i++) 93 | { 94 | if (supportedPlatforms[i].connect(desiredID, platform)) 95 | return GLFW_TRUE; 96 | } 97 | 98 | _glfwInputError(GLFW_PLATFORM_UNAVAILABLE, "Failed to detect any supported platform"); 99 | } 100 | else 101 | { 102 | for (i = 0; i < count; i++) 103 | { 104 | if (supportedPlatforms[i].ID == desiredID) 105 | return supportedPlatforms[i].connect(desiredID, platform); 106 | } 107 | 108 | _glfwInputError(GLFW_PLATFORM_UNAVAILABLE, "The requested platform is not supported"); 109 | } 110 | 111 | return GLFW_FALSE; 112 | } 113 | 114 | ////////////////////////////////////////////////////////////////////////// 115 | ////// GLFW public API ////// 116 | ////////////////////////////////////////////////////////////////////////// 117 | 118 | GLFWAPI int glfwGetPlatform(void) 119 | { 120 | _GLFW_REQUIRE_INIT_OR_RETURN(0); 121 | return _glfw.platform.platformID; 122 | } 123 | 124 | GLFWAPI int glfwPlatformSupported(int platformID) 125 | { 126 | const size_t count = sizeof(supportedPlatforms) / sizeof(supportedPlatforms[0]); 127 | size_t i; 128 | 129 | if (platformID != GLFW_PLATFORM_WIN32 && 130 | platformID != GLFW_PLATFORM_COCOA && 131 | platformID != GLFW_PLATFORM_WAYLAND && 132 | platformID != GLFW_PLATFORM_X11 && 133 | platformID != GLFW_PLATFORM_NULL) 134 | { 135 | _glfwInputError(GLFW_INVALID_ENUM, "Invalid platform ID 0x%08X", platformID); 136 | return GLFW_FALSE; 137 | } 138 | 139 | if (platformID == GLFW_PLATFORM_NULL) 140 | return GLFW_TRUE; 141 | 142 | for (i = 0; i < count; i++) 143 | { 144 | if (platformID == supportedPlatforms[i].ID) 145 | return GLFW_TRUE; 146 | } 147 | 148 | return GLFW_FALSE; 149 | } 150 | 151 | GLFWAPI const char* glfwGetVersionString(void) 152 | { 153 | return _GLFW_MAKE_VERSION(GLFW_VERSION_MAJOR, 154 | GLFW_VERSION_MINOR, 155 | GLFW_VERSION_REVISION) 156 | #if defined(_GLFW_WIN32) 157 | " Win32 WGL" 158 | #endif 159 | #if defined(_GLFW_COCOA) 160 | " Cocoa NSGL" 161 | #endif 162 | #if defined(_GLFW_WAYLAND) 163 | " Wayland" 164 | #endif 165 | #if defined(_GLFW_X11) 166 | " X11 GLX" 167 | #endif 168 | " Null" 169 | " EGL" 170 | " OSMesa" 171 | #if defined(__MINGW64_VERSION_MAJOR) 172 | " MinGW-w64" 173 | #elif defined(__MINGW32__) 174 | " MinGW" 175 | #elif defined(_MSC_VER) 176 | " VisualC" 177 | #endif 178 | #if defined(_GLFW_USE_HYBRID_HPG) || defined(_GLFW_USE_OPTIMUS_HPG) 179 | " hybrid-GPU" 180 | #endif 181 | #if defined(_POSIX_MONOTONIC_CLOCK) 182 | " monotonic" 183 | #endif 184 | #if defined(_GLFW_BUILD_DLL) 185 | #if defined(_WIN32) 186 | " DLL" 187 | #elif defined(__APPLE__) 188 | " dynamic" 189 | #else 190 | " shared" 191 | #endif 192 | #endif 193 | ; 194 | } 195 | 196 | -------------------------------------------------------------------------------- /src/platform.h: -------------------------------------------------------------------------------- 1 | //======================================================================== 2 | // GLFW 3.4 - www.glfw.org 3 | //------------------------------------------------------------------------ 4 | // Copyright (c) 2002-2006 Marcus Geelnard 5 | // Copyright (c) 2006-2018 Camilla Löwy 6 | // 7 | // This software is provided 'as-is', without any express or implied 8 | // warranty. In no event will the authors be held liable for any damages 9 | // arising from the use of this software. 10 | // 11 | // Permission is granted to anyone to use this software for any purpose, 12 | // including commercial applications, and to alter it and redistribute it 13 | // freely, subject to the following restrictions: 14 | // 15 | // 1. The origin of this software must not be misrepresented; you must not 16 | // claim that you wrote the original software. If you use this software 17 | // in a product, an acknowledgment in the product documentation would 18 | // be appreciated but is not required. 19 | // 20 | // 2. Altered source versions must be plainly marked as such, and must not 21 | // be misrepresented as being the original software. 22 | // 23 | // 3. This notice may not be removed or altered from any source 24 | // distribution. 25 | // 26 | //======================================================================== 27 | 28 | #if defined(GLFW_BUILD_WIN32_TIMER) || \ 29 | defined(GLFW_BUILD_WIN32_MODULE) || \ 30 | defined(GLFW_BUILD_WIN32_THREAD) || \ 31 | defined(GLFW_BUILD_COCOA_TIMER) || \ 32 | defined(GLFW_BUILD_POSIX_TIMER) || \ 33 | defined(GLFW_BUILD_POSIX_MODULE) || \ 34 | defined(GLFW_BUILD_POSIX_THREAD) || \ 35 | defined(GLFW_BUILD_POSIX_POLL) || \ 36 | defined(GLFW_BUILD_LINUX_JOYSTICK) 37 | #error "You must not define these; define zero or more _GLFW_ macros instead" 38 | #endif 39 | 40 | #include "null_platform.h" 41 | 42 | #if defined(_GLFW_WIN32) 43 | #include "win32_platform.h" 44 | #else 45 | #define GLFW_WIN32_WINDOW_STATE 46 | #define GLFW_WIN32_MONITOR_STATE 47 | #define GLFW_WIN32_CURSOR_STATE 48 | #define GLFW_WIN32_LIBRARY_WINDOW_STATE 49 | #define GLFW_WGL_CONTEXT_STATE 50 | #define GLFW_WGL_LIBRARY_CONTEXT_STATE 51 | #endif 52 | 53 | #if defined(_GLFW_COCOA) 54 | #include "cocoa_platform.h" 55 | #else 56 | #define GLFW_COCOA_WINDOW_STATE 57 | #define GLFW_COCOA_MONITOR_STATE 58 | #define GLFW_COCOA_CURSOR_STATE 59 | #define GLFW_COCOA_LIBRARY_WINDOW_STATE 60 | #define GLFW_NSGL_CONTEXT_STATE 61 | #define GLFW_NSGL_LIBRARY_CONTEXT_STATE 62 | #endif 63 | 64 | #if defined(_GLFW_WAYLAND) 65 | #include "wl_platform.h" 66 | #else 67 | #define GLFW_WAYLAND_WINDOW_STATE 68 | #define GLFW_WAYLAND_MONITOR_STATE 69 | #define GLFW_WAYLAND_CURSOR_STATE 70 | #define GLFW_WAYLAND_LIBRARY_WINDOW_STATE 71 | #endif 72 | 73 | #if defined(_GLFW_X11) 74 | #include "x11_platform.h" 75 | #else 76 | #define GLFW_X11_WINDOW_STATE 77 | #define GLFW_X11_MONITOR_STATE 78 | #define GLFW_X11_CURSOR_STATE 79 | #define GLFW_X11_LIBRARY_WINDOW_STATE 80 | #define GLFW_GLX_CONTEXT_STATE 81 | #define GLFW_GLX_LIBRARY_CONTEXT_STATE 82 | #endif 83 | 84 | #include "null_joystick.h" 85 | 86 | #if defined(_GLFW_WIN32) 87 | #include "win32_joystick.h" 88 | #else 89 | #define GLFW_WIN32_JOYSTICK_STATE 90 | #define GLFW_WIN32_LIBRARY_JOYSTICK_STATE 91 | #endif 92 | 93 | #if defined(_GLFW_COCOA) 94 | #include "cocoa_joystick.h" 95 | #else 96 | #define GLFW_COCOA_JOYSTICK_STATE 97 | #define GLFW_COCOA_LIBRARY_JOYSTICK_STATE 98 | #endif 99 | 100 | #if (defined(_GLFW_X11) || defined(_GLFW_WAYLAND)) && defined(__linux__) 101 | #define GLFW_BUILD_LINUX_JOYSTICK 102 | #endif 103 | 104 | #if defined(GLFW_BUILD_LINUX_JOYSTICK) 105 | #include "linux_joystick.h" 106 | #else 107 | #define GLFW_LINUX_JOYSTICK_STATE 108 | #define GLFW_LINUX_LIBRARY_JOYSTICK_STATE 109 | #endif 110 | 111 | #define GLFW_PLATFORM_WINDOW_STATE \ 112 | GLFW_WIN32_WINDOW_STATE \ 113 | GLFW_COCOA_WINDOW_STATE \ 114 | GLFW_WAYLAND_WINDOW_STATE \ 115 | GLFW_X11_WINDOW_STATE \ 116 | GLFW_NULL_WINDOW_STATE \ 117 | 118 | #define GLFW_PLATFORM_MONITOR_STATE \ 119 | GLFW_WIN32_MONITOR_STATE \ 120 | GLFW_COCOA_MONITOR_STATE \ 121 | GLFW_WAYLAND_MONITOR_STATE \ 122 | GLFW_X11_MONITOR_STATE \ 123 | GLFW_NULL_MONITOR_STATE \ 124 | 125 | #define GLFW_PLATFORM_CURSOR_STATE \ 126 | GLFW_WIN32_CURSOR_STATE \ 127 | GLFW_COCOA_CURSOR_STATE \ 128 | GLFW_WAYLAND_CURSOR_STATE \ 129 | GLFW_X11_CURSOR_STATE \ 130 | GLFW_NULL_CURSOR_STATE \ 131 | 132 | #define GLFW_PLATFORM_JOYSTICK_STATE \ 133 | GLFW_WIN32_JOYSTICK_STATE \ 134 | GLFW_COCOA_JOYSTICK_STATE \ 135 | GLFW_LINUX_JOYSTICK_STATE 136 | 137 | #define GLFW_PLATFORM_LIBRARY_WINDOW_STATE \ 138 | GLFW_WIN32_LIBRARY_WINDOW_STATE \ 139 | GLFW_COCOA_LIBRARY_WINDOW_STATE \ 140 | GLFW_WAYLAND_LIBRARY_WINDOW_STATE \ 141 | GLFW_X11_LIBRARY_WINDOW_STATE \ 142 | GLFW_NULL_LIBRARY_WINDOW_STATE \ 143 | 144 | #define GLFW_PLATFORM_LIBRARY_JOYSTICK_STATE \ 145 | GLFW_WIN32_LIBRARY_JOYSTICK_STATE \ 146 | GLFW_COCOA_LIBRARY_JOYSTICK_STATE \ 147 | GLFW_LINUX_LIBRARY_JOYSTICK_STATE 148 | 149 | #define GLFW_PLATFORM_CONTEXT_STATE \ 150 | GLFW_WGL_CONTEXT_STATE \ 151 | GLFW_NSGL_CONTEXT_STATE \ 152 | GLFW_GLX_CONTEXT_STATE 153 | 154 | #define GLFW_PLATFORM_LIBRARY_CONTEXT_STATE \ 155 | GLFW_WGL_LIBRARY_CONTEXT_STATE \ 156 | GLFW_NSGL_LIBRARY_CONTEXT_STATE \ 157 | GLFW_GLX_LIBRARY_CONTEXT_STATE 158 | 159 | #if defined(_WIN32) 160 | #define GLFW_BUILD_WIN32_THREAD 161 | #else 162 | #define GLFW_BUILD_POSIX_THREAD 163 | #endif 164 | 165 | #if defined(GLFW_BUILD_WIN32_THREAD) 166 | #include "win32_thread.h" 167 | #define GLFW_PLATFORM_TLS_STATE GLFW_WIN32_TLS_STATE 168 | #define GLFW_PLATFORM_MUTEX_STATE GLFW_WIN32_MUTEX_STATE 169 | #elif defined(GLFW_BUILD_POSIX_THREAD) 170 | #include "posix_thread.h" 171 | #define GLFW_PLATFORM_TLS_STATE GLFW_POSIX_TLS_STATE 172 | #define GLFW_PLATFORM_MUTEX_STATE GLFW_POSIX_MUTEX_STATE 173 | #endif 174 | 175 | #if defined(_WIN32) 176 | #define GLFW_BUILD_WIN32_TIMER 177 | #elif defined(__APPLE__) 178 | #define GLFW_BUILD_COCOA_TIMER 179 | #else 180 | #define GLFW_BUILD_POSIX_TIMER 181 | #endif 182 | 183 | #if defined(GLFW_BUILD_WIN32_TIMER) 184 | #include "win32_time.h" 185 | #define GLFW_PLATFORM_LIBRARY_TIMER_STATE GLFW_WIN32_LIBRARY_TIMER_STATE 186 | #elif defined(GLFW_BUILD_COCOA_TIMER) 187 | #include "cocoa_time.h" 188 | #define GLFW_PLATFORM_LIBRARY_TIMER_STATE GLFW_COCOA_LIBRARY_TIMER_STATE 189 | #elif defined(GLFW_BUILD_POSIX_TIMER) 190 | #include "posix_time.h" 191 | #define GLFW_PLATFORM_LIBRARY_TIMER_STATE GLFW_POSIX_LIBRARY_TIMER_STATE 192 | #endif 193 | 194 | #if defined(_WIN32) 195 | #define GLFW_BUILD_WIN32_MODULE 196 | #else 197 | #define GLFW_BUILD_POSIX_MODULE 198 | #endif 199 | 200 | #if defined(_GLFW_WAYLAND) || defined(_GLFW_X11) 201 | #define GLFW_BUILD_POSIX_POLL 202 | #endif 203 | 204 | -------------------------------------------------------------------------------- /tests/msaa.c: -------------------------------------------------------------------------------- 1 | //======================================================================== 2 | // Multisample anti-aliasing test 3 | // Copyright (c) Camilla Löwy 4 | // 5 | // This software is provided 'as-is', without any express or implied 6 | // warranty. In no event will the authors be held liable for any damages 7 | // arising from the use of this software. 8 | // 9 | // Permission is granted to anyone to use this software for any purpose, 10 | // including commercial applications, and to alter it and redistribute it 11 | // freely, subject to the following restrictions: 12 | // 13 | // 1. The origin of this software must not be misrepresented; you must not 14 | // claim that you wrote the original software. If you use this software 15 | // in a product, an acknowledgment in the product documentation would 16 | // be appreciated but is not required. 17 | // 18 | // 2. Altered source versions must be plainly marked as such, and must not 19 | // be misrepresented as being the original software. 20 | // 21 | // 3. This notice may not be removed or altered from any source 22 | // distribution. 23 | // 24 | //======================================================================== 25 | // 26 | // This test renders two high contrast, slowly rotating quads, one aliased 27 | // and one (hopefully) anti-aliased, thus allowing for visual verification 28 | // of whether MSAA is indeed enabled 29 | // 30 | //======================================================================== 31 | 32 | #define GLAD_GL_IMPLEMENTATION 33 | #include 34 | #define GLFW_INCLUDE_NONE 35 | #include 36 | 37 | #if defined(_MSC_VER) 38 | // Make MS math.h define M_PI 39 | #define _USE_MATH_DEFINES 40 | #endif 41 | 42 | #include "linmath.h" 43 | 44 | #include 45 | #include 46 | 47 | #include "getopt.h" 48 | 49 | static const vec2 vertices[4] = 50 | { 51 | { -0.6f, -0.6f }, 52 | { 0.6f, -0.6f }, 53 | { 0.6f, 0.6f }, 54 | { -0.6f, 0.6f } 55 | }; 56 | 57 | static const char* vertex_shader_text = 58 | "#version 110\n" 59 | "uniform mat4 MVP;\n" 60 | "attribute vec2 vPos;\n" 61 | "void main()\n" 62 | "{\n" 63 | " gl_Position = MVP * vec4(vPos, 0.0, 1.0);\n" 64 | "}\n"; 65 | 66 | static const char* fragment_shader_text = 67 | "#version 110\n" 68 | "void main()\n" 69 | "{\n" 70 | " gl_FragColor = vec4(1.0);\n" 71 | "}\n"; 72 | 73 | static void error_callback(int error, const char* description) 74 | { 75 | fprintf(stderr, "Error: %s\n", description); 76 | } 77 | 78 | static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) 79 | { 80 | if (action != GLFW_PRESS) 81 | return; 82 | 83 | switch (key) 84 | { 85 | case GLFW_KEY_SPACE: 86 | glfwSetTime(0.0); 87 | break; 88 | case GLFW_KEY_ESCAPE: 89 | glfwSetWindowShouldClose(window, GLFW_TRUE); 90 | break; 91 | } 92 | } 93 | 94 | static void usage(void) 95 | { 96 | printf("Usage: msaa [-h] [-s SAMPLES]\n"); 97 | } 98 | 99 | int main(int argc, char** argv) 100 | { 101 | int ch, samples = 4; 102 | GLFWwindow* window; 103 | GLuint vertex_buffer, vertex_shader, fragment_shader, program; 104 | GLint mvp_location, vpos_location; 105 | 106 | while ((ch = getopt(argc, argv, "hs:")) != -1) 107 | { 108 | switch (ch) 109 | { 110 | case 'h': 111 | usage(); 112 | exit(EXIT_SUCCESS); 113 | case 's': 114 | samples = atoi(optarg); 115 | break; 116 | default: 117 | usage(); 118 | exit(EXIT_FAILURE); 119 | } 120 | } 121 | 122 | glfwSetErrorCallback(error_callback); 123 | 124 | if (!glfwInit()) 125 | exit(EXIT_FAILURE); 126 | 127 | if (samples) 128 | printf("Requesting MSAA with %i samples\n", samples); 129 | else 130 | printf("Requesting that MSAA not be available\n"); 131 | 132 | glfwWindowHint(GLFW_SAMPLES, samples); 133 | glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2); 134 | glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0); 135 | 136 | window = glfwCreateWindow(800, 400, "Aliasing Detector", NULL, NULL); 137 | if (!window) 138 | { 139 | glfwTerminate(); 140 | exit(EXIT_FAILURE); 141 | } 142 | 143 | glfwSetKeyCallback(window, key_callback); 144 | 145 | glfwMakeContextCurrent(window); 146 | gladLoadGL(glfwGetProcAddress); 147 | glfwSwapInterval(1); 148 | 149 | glGetIntegerv(GL_SAMPLES, &samples); 150 | if (samples) 151 | printf("Context reports MSAA is available with %i samples\n", samples); 152 | else 153 | printf("Context reports MSAA is unavailable\n"); 154 | 155 | glGenBuffers(1, &vertex_buffer); 156 | glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer); 157 | glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); 158 | 159 | vertex_shader = glCreateShader(GL_VERTEX_SHADER); 160 | glShaderSource(vertex_shader, 1, &vertex_shader_text, NULL); 161 | glCompileShader(vertex_shader); 162 | 163 | fragment_shader = glCreateShader(GL_FRAGMENT_SHADER); 164 | glShaderSource(fragment_shader, 1, &fragment_shader_text, NULL); 165 | glCompileShader(fragment_shader); 166 | 167 | program = glCreateProgram(); 168 | glAttachShader(program, vertex_shader); 169 | glAttachShader(program, fragment_shader); 170 | glLinkProgram(program); 171 | 172 | mvp_location = glGetUniformLocation(program, "MVP"); 173 | vpos_location = glGetAttribLocation(program, "vPos"); 174 | 175 | glEnableVertexAttribArray(vpos_location); 176 | glVertexAttribPointer(vpos_location, 2, GL_FLOAT, GL_FALSE, 177 | sizeof(vertices[0]), (void*) 0); 178 | 179 | while (!glfwWindowShouldClose(window)) 180 | { 181 | float ratio; 182 | int width, height; 183 | mat4x4 m, p, mvp; 184 | const double angle = glfwGetTime() * M_PI / 180.0; 185 | 186 | glfwGetFramebufferSize(window, &width, &height); 187 | ratio = width / (float) height; 188 | 189 | glViewport(0, 0, width, height); 190 | glClear(GL_COLOR_BUFFER_BIT); 191 | 192 | glUseProgram(program); 193 | 194 | mat4x4_ortho(p, -ratio, ratio, -1.f, 1.f, 0.f, 1.f); 195 | 196 | mat4x4_translate(m, -1.f, 0.f, 0.f); 197 | mat4x4_rotate_Z(m, m, (float) angle); 198 | mat4x4_mul(mvp, p, m); 199 | 200 | glUniformMatrix4fv(mvp_location, 1, GL_FALSE, (const GLfloat*) mvp); 201 | glDisable(GL_MULTISAMPLE); 202 | glDrawArrays(GL_TRIANGLE_FAN, 0, 4); 203 | 204 | mat4x4_translate(m, 1.f, 0.f, 0.f); 205 | mat4x4_rotate_Z(m, m, (float) angle); 206 | mat4x4_mul(mvp, p, m); 207 | 208 | glUniformMatrix4fv(mvp_location, 1, GL_FALSE, (const GLfloat*) mvp); 209 | glEnable(GL_MULTISAMPLE); 210 | glDrawArrays(GL_TRIANGLE_FAN, 0, 4); 211 | 212 | glfwSwapBuffers(window); 213 | glfwPollEvents(); 214 | } 215 | 216 | glfwDestroyWindow(window); 217 | 218 | glfwTerminate(); 219 | exit(EXIT_SUCCESS); 220 | } 221 | 222 | -------------------------------------------------------------------------------- /src/win32_polyfill.c: -------------------------------------------------------------------------------- 1 | #include "internal.h" 2 | 3 | // Reference: https://github.com/metaxor/KernelEx/blob/31cdfc3560fc116637ee8ed7be31b12f3aacf5d1/common/common.h#L143 4 | 5 | #define STACK_AtoW(strA,strW) \ 6 | { \ 7 | strW = (LPWSTR)strA; \ 8 | if (HIWORD(strA)) \ 9 | { \ 10 | int c = lstrlenA((LPCSTR)strA); \ 11 | if (c) \ 12 | { \ 13 | strW = (LPWSTR)alloca(c * sizeof(WCHAR)); \ 14 | MultiByteToWideChar(CP_ACP, 0, (LPCSTR)strA, -1, (LPWSTR)strW, c); \ 15 | } \ 16 | } \ 17 | } 18 | 19 | // GetMonitorInfoW polyfill 20 | // https://learn.microsoft.com/ru-ru/windows/win32/api/winuser/nf-winuser-getmonitorinfow 21 | // Reference: https://github.com/metaxor/KernelEx/blob/31cdfc3560fc116637ee8ed7be31b12f3aacf5d1/apilibs/kexbasen/user32/uniuser32.c#L404 22 | // 23 | 24 | BOOL GLFW_GetMonitorInfoW(HMONITOR hMonitor, LPMONITORINFO lpmi) 25 | { 26 | // TODO: return the actual screen size 27 | return FALSE; 28 | } 29 | 30 | // SetThreadExecutionState polyfill 31 | // https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-setthreadexecutionstate 32 | // 33 | 34 | EXECUTION_STATE GLFW_SetThreadExecutionState(EXECUTION_STATE esFlags) 35 | { 36 | // TODO: figure out how to prevent the screen from turning off on Windows 2000 37 | return NULL; 38 | } 39 | 40 | // GetModuleHandleExW polyfill 41 | // https://learn.microsoft.com/ru-ru/windows/win32/api/libloaderapi/nf-libloaderapi-getmodulehandleexw 42 | // Reference: https://github.com/metaxor/KernelEx/blob/31cdfc3560fc116637ee8ed7be31b12f3aacf5d1/apilibs/kexbases/Kernel32/module.c#L108 43 | // 44 | 45 | BOOL GLFW_GetModuleHandleExW(DWORD dwFlags, LPCWSTR lpModuleName, HMODULE *phModule) 46 | { 47 | WCHAR buf[MAX_PATH]; 48 | if (!phModule) 49 | { 50 | SetLastError(ERROR_INVALID_PARAMETER); 51 | return FALSE; 52 | } 53 | 54 | *phModule = NULL; 55 | if (dwFlags & GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS) 56 | { 57 | MEMORY_BASIC_INFORMATION mbi; 58 | if (!VirtualQuery(lpModuleName, &mbi, sizeof(mbi))) 59 | return FALSE; 60 | 61 | *phModule = (HMODULE) mbi.AllocationBase; 62 | } 63 | else 64 | *phModule = GetModuleHandleW(lpModuleName); 65 | 66 | if (*phModule == NULL || !GetModuleFileNameW(*phModule, buf, MAX_PATH)) 67 | { 68 | *phModule = NULL; 69 | return FALSE; 70 | } 71 | 72 | if (!(dwFlags & GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT)) 73 | LoadLibraryW(buf); 74 | 75 | return TRUE; 76 | } 77 | 78 | // VerSetConditionMask polyfill 79 | // https://learn.microsoft.com/en-us/windows/win32/api/winnt/nf-winnt-versetconditionmask 80 | // Reference: https://github.com/metaxor/KernelEx/blob/31cdfc3560fc116637ee8ed7be31b12f3aacf5d1/apilibs/kexbases/Kernel32/version.c#L383 81 | // 82 | 83 | inline ULONGLONG GLFW_VerSetConditionMask(ULONGLONG dwlConditionMask, DWORD dwTypeBitMask, BYTE dwConditionMask) 84 | { 85 | if (dwTypeBitMask == 0) 86 | return dwlConditionMask; 87 | 88 | dwConditionMask &= 0x07; 89 | if (dwConditionMask == 0) 90 | return dwlConditionMask; 91 | 92 | if (dwTypeBitMask & VER_PRODUCT_TYPE) 93 | dwlConditionMask |= dwConditionMask << 7*3; 94 | else if (dwTypeBitMask & VER_SUITENAME) 95 | dwlConditionMask |= dwConditionMask << 6*3; 96 | else if (dwTypeBitMask & VER_SERVICEPACKMAJOR) 97 | dwlConditionMask |= dwConditionMask << 5*3; 98 | else if (dwTypeBitMask & VER_SERVICEPACKMINOR) 99 | dwlConditionMask |= dwConditionMask << 4*3; 100 | else if (dwTypeBitMask & VER_PLATFORMID) 101 | dwlConditionMask |= dwConditionMask << 3*3; 102 | else if (dwTypeBitMask & VER_BUILDNUMBER) 103 | dwlConditionMask |= dwConditionMask << 2*3; 104 | else if (dwTypeBitMask & VER_MAJORVERSION) 105 | dwlConditionMask |= dwConditionMask << 1*3; 106 | else if (dwTypeBitMask & VER_MINORVERSION) 107 | dwlConditionMask |= dwConditionMask << 0*3; 108 | return dwlConditionMask; 109 | } 110 | 111 | // EnumDisplayMonitors polyfill 112 | // https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-enumdisplaymonitors 113 | // 114 | 115 | BOOL GLFW_EnumDisplayMonitors(HDC hdc, LPCRECT lprcClip, MONITORENUMPROC lpfnEnum, LPARAM dwData) 116 | { 117 | // TODO: call the callback at least once for one monitor 118 | return FALSE; 119 | } 120 | 121 | // EnumDisplaySettingsExW polyfill 122 | // https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-enumdisplaysettingsexw 123 | // 124 | 125 | BOOL GLFW_EnumDisplaySettingsExW(LPCWSTR lpszDeviceName, DWORD iModeNum, DEVMODEW *lpDevMode, DWORD dwFlags) 126 | { 127 | // TODO: 128 | return FALSE; 129 | } 130 | 131 | // EnumDisplaySettingsW polyfill 132 | // https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-enumdisplaysettingsw 133 | // 134 | 135 | BOOL GLFW_EnumDisplaySettingsW(LPCWSTR lpszDeviceName, DWORD iModeNum, DEVMODEW *lpDevMode) 136 | { 137 | // TODO: 138 | return FALSE; 139 | } 140 | 141 | // GetLayeredWindowAttributes polyfill 142 | // https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getlayeredwindowattributes 143 | // 144 | 145 | BOOL GLFW_GetLayeredWindowAttributes(HWND hwnd, COLORREF *pcrKey, BYTE *pbAlpha, DWORD *pdwFlags) 146 | { 147 | // TODO: 148 | return FALSE; 149 | } 150 | 151 | // SetLayeredWindowAttributes polyfill 152 | // https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setlayeredwindowattributes 153 | // 154 | 155 | BOOL GLFW_SetLayeredWindowAttributes(HWND hwnd, COLORREF crKey, BYTE bAlpha, DWORD dwFlags) 156 | { 157 | // TODO: 158 | return FALSE; 159 | } 160 | 161 | // GetRawInputData polyfill 162 | // https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getrawinputdata 163 | // 164 | 165 | UINT GLFW_GetRawInputData(HRAWINPUT hRawInput, UINT uiCommand, LPVOID pData, PUINT pcbSize, UINT cbSizeHeader) 166 | { 167 | // TODO: 168 | return -1; 169 | } 170 | 171 | // GetRawInputDeviceInfoA polyfill 172 | // https://learn.microsoft.com/ru-ru/windows/win32/api/winuser/nf-winuser-getrawinputdeviceinfoa 173 | // 174 | 175 | UINT GLFW_GetRawInputDeviceInfoA(HANDLE hDevice, UINT uiCommand, LPVOID pData, PUINT pcbSize) 176 | { 177 | // TODO: 178 | return -1; 179 | } 180 | 181 | // GetRawInputDeviceList polyfill 182 | // https://learn.microsoft.com/ru-ru/windows/win32/api/winuser/nf-winuser-getrawinputdevicelist 183 | // 184 | 185 | UINT GLFW_GetRawInputDeviceList(PRAWINPUTDEVICELIST pRawInputDeviceList, PUINT puiNumDevices, UINT cbSize) 186 | { 187 | // TODO: 188 | return -1; 189 | } 190 | 191 | // MonitorFromWindow polyfill 192 | // https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-monitorfromwindow 193 | // 194 | 195 | HMONITOR GLFW_MonitorFromWindow(HWND hwnd, DWORD dwFlags) 196 | { 197 | // TODO: 198 | return NULL; 199 | } 200 | 201 | // RegisterDeviceNotificationW polyfill 202 | // https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-registerdevicenotificationw 203 | // 204 | 205 | HDEVNOTIFY GLFW_RegisterDeviceNotificationW(HANDLE hRecipient, LPVOID NotificationFilter, DWORD Flags) 206 | { 207 | // TODO: 208 | return NULL; 209 | } 210 | 211 | // RegisterRawInputDevices polyfill 212 | // https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-registerrawinputdevices 213 | // 214 | 215 | BOOL GLFW_RegisterRawInputDevices(PCRAWINPUTDEVICE pRawInputDevices, UINT uiNumDevices, UINT cbSize) 216 | { 217 | return FALSE; 218 | } 219 | 220 | // UnregisterDeviceNotification polyfill 221 | // https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-unregisterdevicenotification 222 | // 223 | 224 | BOOL GLFW_UnregisterDeviceNotification(HDEVNOTIFY Handle) 225 | { 226 | return FALSE; 227 | } -------------------------------------------------------------------------------- /tests/reopen.c: -------------------------------------------------------------------------------- 1 | //======================================================================== 2 | // Window re-opener (open/close stress test) 3 | // Copyright (c) Camilla Löwy 4 | // 5 | // This software is provided 'as-is', without any express or implied 6 | // warranty. In no event will the authors be held liable for any damages 7 | // arising from the use of this software. 8 | // 9 | // Permission is granted to anyone to use this software for any purpose, 10 | // including commercial applications, and to alter it and redistribute it 11 | // freely, subject to the following restrictions: 12 | // 13 | // 1. The origin of this software must not be misrepresented; you must not 14 | // claim that you wrote the original software. If you use this software 15 | // in a product, an acknowledgment in the product documentation would 16 | // be appreciated but is not required. 17 | // 18 | // 2. Altered source versions must be plainly marked as such, and must not 19 | // be misrepresented as being the original software. 20 | // 21 | // 3. This notice may not be removed or altered from any source 22 | // distribution. 23 | // 24 | //======================================================================== 25 | // 26 | // This test came about as the result of bug #1262773 27 | // 28 | // It closes and re-opens the GLFW window every five seconds, alternating 29 | // between windowed and full screen mode 30 | // 31 | // It also times and logs opening and closing actions and attempts to separate 32 | // user initiated window closing from its own 33 | // 34 | //======================================================================== 35 | 36 | #define GLAD_GL_IMPLEMENTATION 37 | #include 38 | #define GLFW_INCLUDE_NONE 39 | #include 40 | 41 | #include 42 | #include 43 | #include 44 | 45 | #include "linmath.h" 46 | 47 | static const char* vertex_shader_text = 48 | "#version 110\n" 49 | "uniform mat4 MVP;\n" 50 | "attribute vec2 vPos;\n" 51 | "void main()\n" 52 | "{\n" 53 | " gl_Position = MVP * vec4(vPos, 0.0, 1.0);\n" 54 | "}\n"; 55 | 56 | static const char* fragment_shader_text = 57 | "#version 110\n" 58 | "void main()\n" 59 | "{\n" 60 | " gl_FragColor = vec4(1.0);\n" 61 | "}\n"; 62 | 63 | static const vec2 vertices[4] = 64 | { 65 | { -0.5f, -0.5f }, 66 | { 0.5f, -0.5f }, 67 | { 0.5f, 0.5f }, 68 | { -0.5f, 0.5f } 69 | }; 70 | 71 | static void error_callback(int error, const char* description) 72 | { 73 | fprintf(stderr, "Error: %s\n", description); 74 | } 75 | 76 | static void window_close_callback(GLFWwindow* window) 77 | { 78 | printf("Close callback triggered\n"); 79 | } 80 | 81 | static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) 82 | { 83 | if (action != GLFW_PRESS) 84 | return; 85 | 86 | switch (key) 87 | { 88 | case GLFW_KEY_Q: 89 | case GLFW_KEY_ESCAPE: 90 | glfwSetWindowShouldClose(window, GLFW_TRUE); 91 | break; 92 | } 93 | } 94 | 95 | static void close_window(GLFWwindow* window) 96 | { 97 | double base = glfwGetTime(); 98 | glfwDestroyWindow(window); 99 | printf("Closing window took %0.3f seconds\n", glfwGetTime() - base); 100 | } 101 | 102 | int main(int argc, char** argv) 103 | { 104 | int count = 0; 105 | double base; 106 | GLFWwindow* window; 107 | 108 | srand((unsigned int) time(NULL)); 109 | 110 | glfwSetErrorCallback(error_callback); 111 | 112 | if (!glfwInit()) 113 | exit(EXIT_FAILURE); 114 | 115 | glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2); 116 | glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0); 117 | 118 | for (;;) 119 | { 120 | int width, height; 121 | GLFWmonitor* monitor = NULL; 122 | GLuint vertex_shader, fragment_shader, program, vertex_buffer; 123 | GLint mvp_location, vpos_location; 124 | 125 | if (count & 1) 126 | { 127 | int monitorCount; 128 | GLFWmonitor** monitors = glfwGetMonitors(&monitorCount); 129 | monitor = monitors[rand() % monitorCount]; 130 | } 131 | 132 | if (monitor) 133 | { 134 | const GLFWvidmode* mode = glfwGetVideoMode(monitor); 135 | width = mode->width; 136 | height = mode->height; 137 | } 138 | else 139 | { 140 | width = 640; 141 | height = 480; 142 | } 143 | 144 | base = glfwGetTime(); 145 | 146 | window = glfwCreateWindow(width, height, "Window Re-opener", monitor, NULL); 147 | if (!window) 148 | { 149 | glfwTerminate(); 150 | exit(EXIT_FAILURE); 151 | } 152 | 153 | if (monitor) 154 | { 155 | printf("Opening full screen window on monitor %s took %0.3f seconds\n", 156 | glfwGetMonitorName(monitor), 157 | glfwGetTime() - base); 158 | } 159 | else 160 | { 161 | printf("Opening regular window took %0.3f seconds\n", 162 | glfwGetTime() - base); 163 | } 164 | 165 | glfwSetWindowCloseCallback(window, window_close_callback); 166 | glfwSetKeyCallback(window, key_callback); 167 | 168 | glfwMakeContextCurrent(window); 169 | gladLoadGL(glfwGetProcAddress); 170 | glfwSwapInterval(1); 171 | 172 | vertex_shader = glCreateShader(GL_VERTEX_SHADER); 173 | glShaderSource(vertex_shader, 1, &vertex_shader_text, NULL); 174 | glCompileShader(vertex_shader); 175 | 176 | fragment_shader = glCreateShader(GL_FRAGMENT_SHADER); 177 | glShaderSource(fragment_shader, 1, &fragment_shader_text, NULL); 178 | glCompileShader(fragment_shader); 179 | 180 | program = glCreateProgram(); 181 | glAttachShader(program, vertex_shader); 182 | glAttachShader(program, fragment_shader); 183 | glLinkProgram(program); 184 | 185 | mvp_location = glGetUniformLocation(program, "MVP"); 186 | vpos_location = glGetAttribLocation(program, "vPos"); 187 | 188 | glGenBuffers(1, &vertex_buffer); 189 | glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer); 190 | glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); 191 | 192 | glEnableVertexAttribArray(vpos_location); 193 | glVertexAttribPointer(vpos_location, 2, GL_FLOAT, GL_FALSE, 194 | sizeof(vertices[0]), (void*) 0); 195 | 196 | glfwSetTime(0.0); 197 | 198 | while (glfwGetTime() < 5.0) 199 | { 200 | float ratio; 201 | int width, height; 202 | mat4x4 m, p, mvp; 203 | 204 | glfwGetFramebufferSize(window, &width, &height); 205 | ratio = width / (float) height; 206 | 207 | glViewport(0, 0, width, height); 208 | glClear(GL_COLOR_BUFFER_BIT); 209 | 210 | mat4x4_ortho(p, -ratio, ratio, -1.f, 1.f, 0.f, 1.f); 211 | 212 | mat4x4_identity(m); 213 | mat4x4_rotate_Z(m, m, (float) glfwGetTime()); 214 | mat4x4_mul(mvp, p, m); 215 | 216 | glUseProgram(program); 217 | glUniformMatrix4fv(mvp_location, 1, GL_FALSE, (const GLfloat*) mvp); 218 | glDrawArrays(GL_TRIANGLE_FAN, 0, 4); 219 | 220 | glfwSwapBuffers(window); 221 | glfwPollEvents(); 222 | 223 | if (glfwWindowShouldClose(window)) 224 | { 225 | close_window(window); 226 | printf("User closed window\n"); 227 | 228 | glfwTerminate(); 229 | exit(EXIT_SUCCESS); 230 | } 231 | } 232 | 233 | printf("Closing window\n"); 234 | close_window(window); 235 | 236 | count++; 237 | } 238 | 239 | glfwTerminate(); 240 | } 241 | 242 | -------------------------------------------------------------------------------- /examples/sharing.c: -------------------------------------------------------------------------------- 1 | //======================================================================== 2 | // Context sharing example 3 | // Copyright (c) Camilla Löwy 4 | // 5 | // This software is provided 'as-is', without any express or implied 6 | // warranty. In no event will the authors be held liable for any damages 7 | // arising from the use of this software. 8 | // 9 | // Permission is granted to anyone to use this software for any purpose, 10 | // including commercial applications, and to alter it and redistribute it 11 | // freely, subject to the following restrictions: 12 | // 13 | // 1. The origin of this software must not be misrepresented; you must not 14 | // claim that you wrote the original software. If you use this software 15 | // in a product, an acknowledgment in the product documentation would 16 | // be appreciated but is not required. 17 | // 18 | // 2. Altered source versions must be plainly marked as such, and must not 19 | // be misrepresented as being the original software. 20 | // 21 | // 3. This notice may not be removed or altered from any source 22 | // distribution. 23 | // 24 | //======================================================================== 25 | 26 | #define GLAD_GL_IMPLEMENTATION 27 | #include 28 | #define GLFW_INCLUDE_NONE 29 | #include 30 | 31 | #include 32 | #include 33 | 34 | #include "getopt.h" 35 | #include "linmath.h" 36 | 37 | static const char* vertex_shader_text = 38 | "#version 110\n" 39 | "uniform mat4 MVP;\n" 40 | "attribute vec2 vPos;\n" 41 | "varying vec2 texcoord;\n" 42 | "void main()\n" 43 | "{\n" 44 | " gl_Position = MVP * vec4(vPos, 0.0, 1.0);\n" 45 | " texcoord = vPos;\n" 46 | "}\n"; 47 | 48 | static const char* fragment_shader_text = 49 | "#version 110\n" 50 | "uniform sampler2D texture;\n" 51 | "uniform vec3 color;\n" 52 | "varying vec2 texcoord;\n" 53 | "void main()\n" 54 | "{\n" 55 | " gl_FragColor = vec4(color * texture2D(texture, texcoord).rgb, 1.0);\n" 56 | "}\n"; 57 | 58 | static const vec2 vertices[4] = 59 | { 60 | { 0.f, 0.f }, 61 | { 1.f, 0.f }, 62 | { 1.f, 1.f }, 63 | { 0.f, 1.f } 64 | }; 65 | 66 | static void error_callback(int error, const char* description) 67 | { 68 | fprintf(stderr, "Error: %s\n", description); 69 | } 70 | 71 | static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) 72 | { 73 | if (action == GLFW_PRESS && key == GLFW_KEY_ESCAPE) 74 | glfwSetWindowShouldClose(window, GLFW_TRUE); 75 | } 76 | 77 | int main(int argc, char** argv) 78 | { 79 | GLFWwindow* windows[2]; 80 | GLuint texture, program, vertex_buffer; 81 | GLint mvp_location, vpos_location, color_location, texture_location; 82 | 83 | glfwSetErrorCallback(error_callback); 84 | 85 | if (!glfwInit()) 86 | exit(EXIT_FAILURE); 87 | 88 | glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2); 89 | glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0); 90 | 91 | windows[0] = glfwCreateWindow(400, 400, "First", NULL, NULL); 92 | if (!windows[0]) 93 | { 94 | glfwTerminate(); 95 | exit(EXIT_FAILURE); 96 | } 97 | 98 | glfwSetKeyCallback(windows[0], key_callback); 99 | 100 | glfwMakeContextCurrent(windows[0]); 101 | 102 | // Only enable vsync for the first of the windows to be swapped to 103 | // avoid waiting out the interval for each window 104 | glfwSwapInterval(1); 105 | 106 | // The contexts are created with the same APIs so the function 107 | // pointers should be re-usable between them 108 | gladLoadGL(glfwGetProcAddress); 109 | 110 | // Create the OpenGL objects inside the first context, created above 111 | // All objects will be shared with the second context, created below 112 | { 113 | int x, y; 114 | char pixels[16 * 16]; 115 | GLuint vertex_shader, fragment_shader; 116 | 117 | glGenTextures(1, &texture); 118 | glBindTexture(GL_TEXTURE_2D, texture); 119 | 120 | srand((unsigned int) glfwGetTimerValue()); 121 | 122 | for (y = 0; y < 16; y++) 123 | { 124 | for (x = 0; x < 16; x++) 125 | pixels[y * 16 + x] = rand() % 256; 126 | } 127 | 128 | glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, 16, 16, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, pixels); 129 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 130 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); 131 | 132 | vertex_shader = glCreateShader(GL_VERTEX_SHADER); 133 | glShaderSource(vertex_shader, 1, &vertex_shader_text, NULL); 134 | glCompileShader(vertex_shader); 135 | 136 | fragment_shader = glCreateShader(GL_FRAGMENT_SHADER); 137 | glShaderSource(fragment_shader, 1, &fragment_shader_text, NULL); 138 | glCompileShader(fragment_shader); 139 | 140 | program = glCreateProgram(); 141 | glAttachShader(program, vertex_shader); 142 | glAttachShader(program, fragment_shader); 143 | glLinkProgram(program); 144 | 145 | mvp_location = glGetUniformLocation(program, "MVP"); 146 | color_location = glGetUniformLocation(program, "color"); 147 | texture_location = glGetUniformLocation(program, "texture"); 148 | vpos_location = glGetAttribLocation(program, "vPos"); 149 | 150 | glGenBuffers(1, &vertex_buffer); 151 | glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer); 152 | glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); 153 | } 154 | 155 | glUseProgram(program); 156 | glUniform1i(texture_location, 0); 157 | 158 | glEnable(GL_TEXTURE_2D); 159 | glBindTexture(GL_TEXTURE_2D, texture); 160 | 161 | glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer); 162 | glEnableVertexAttribArray(vpos_location); 163 | glVertexAttribPointer(vpos_location, 2, GL_FLOAT, GL_FALSE, 164 | sizeof(vertices[0]), (void*) 0); 165 | 166 | windows[1] = glfwCreateWindow(400, 400, "Second", NULL, windows[0]); 167 | if (!windows[1]) 168 | { 169 | glfwTerminate(); 170 | exit(EXIT_FAILURE); 171 | } 172 | 173 | // Place the second window to the right of the first 174 | { 175 | int xpos, ypos, left, right, width; 176 | 177 | glfwGetWindowSize(windows[0], &width, NULL); 178 | glfwGetWindowFrameSize(windows[0], &left, NULL, &right, NULL); 179 | glfwGetWindowPos(windows[0], &xpos, &ypos); 180 | 181 | glfwSetWindowPos(windows[1], xpos + width + left + right, ypos); 182 | } 183 | 184 | glfwSetKeyCallback(windows[1], key_callback); 185 | 186 | glfwMakeContextCurrent(windows[1]); 187 | 188 | // While objects are shared, the global context state is not and will 189 | // need to be set up for each context 190 | 191 | glUseProgram(program); 192 | 193 | glEnable(GL_TEXTURE_2D); 194 | glBindTexture(GL_TEXTURE_2D, texture); 195 | 196 | glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer); 197 | glEnableVertexAttribArray(vpos_location); 198 | glVertexAttribPointer(vpos_location, 2, GL_FLOAT, GL_FALSE, 199 | sizeof(vertices[0]), (void*) 0); 200 | 201 | while (!glfwWindowShouldClose(windows[0]) && 202 | !glfwWindowShouldClose(windows[1])) 203 | { 204 | int i; 205 | const vec3 colors[2] = 206 | { 207 | { 0.8f, 0.4f, 1.f }, 208 | { 0.3f, 0.4f, 1.f } 209 | }; 210 | 211 | for (i = 0; i < 2; i++) 212 | { 213 | int width, height; 214 | mat4x4 mvp; 215 | 216 | glfwGetFramebufferSize(windows[i], &width, &height); 217 | glfwMakeContextCurrent(windows[i]); 218 | 219 | glViewport(0, 0, width, height); 220 | 221 | mat4x4_ortho(mvp, 0.f, 1.f, 0.f, 1.f, 0.f, 1.f); 222 | glUniformMatrix4fv(mvp_location, 1, GL_FALSE, (const GLfloat*) mvp); 223 | glUniform3fv(color_location, 1, colors[i]); 224 | glDrawArrays(GL_TRIANGLE_FAN, 0, 4); 225 | 226 | glfwSwapBuffers(windows[i]); 227 | } 228 | 229 | glfwWaitEvents(); 230 | } 231 | 232 | glfwTerminate(); 233 | exit(EXIT_SUCCESS); 234 | } 235 | 236 | --------------------------------------------------------------------------------