diff --git a/.gitignore b/.gitignore index 710c7fa2..0a7b6c9f 100644 --- a/.gitignore +++ b/.gitignore @@ -45,3 +45,4 @@ ext/imgui/docs/ ext/imgui/examples/ ext/imgui/misc/ ext/imgui/.* +_codeql_build_dir/ diff --git a/.gitmodules b/.gitmodules index fd534cbe..176c4259 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,6 +1,3 @@ -[submodule "ext/glfw"] - path = ext/glfw - url = https://github.com/glfw/glfw [submodule "ext/imgui"] path = ext/imgui url = https://github.com/ocornut/imgui @@ -10,3 +7,7 @@ [submodule "ext/mdlib"] path = ext/mdlib url = https://github.com/scanberg/mdlib +[submodule "ext/SDL"] + path = ext/SDL + url = https://github.com/libsdl-org/SDL.git + branch = main diff --git a/CMakeLists.txt b/CMakeLists.txt index 2609747c..86b8c201 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -22,16 +22,13 @@ else() set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreaded$<$:Debug>) endif() -# GLFW OPTIONS -option(GLFW_BUILD_EXAMPLES OFF) -option(GLFW_BUILD_TESTS OFF) -option(GLFW_BUILD_DOCS OFF) -option(GLFW_INSTALL OFF) -option(GLFW_VULKAN_STATIC OFF) - -if (UNIX AND NOT APPLE) - option(GLFW_BUILD_WAYLAND OFF) -endif() +# SDL3 OPTIONS +set(SDL_SHARED OFF CACHE BOOL "" FORCE) +set(SDL_STATIC ON CACHE BOOL "" FORCE) +set(SDL_TEST_LIBRARY OFF CACHE BOOL "" FORCE) +set(SDL3_DISABLE_INSTALL ON CACHE BOOL "" FORCE) +set(SDL_X11_XSCRNSAVER OFF CACHE BOOL "" FORCE) +set(SDL_X11_XTEST OFF CACHE BOOL "" FORCE) # ENKI OPTIONS option(ENKITS_BUILD_C_INTERFACE OFF) @@ -71,9 +68,8 @@ endif() find_package(OpenGL REQUIRED) add_subdirectory(ext/mdlib) -add_subdirectory(ext/glfw) +add_subdirectory(ext/SDL) add_subdirectory(ext/imgui) -add_subdirectory(ext/nativefiledialog) add_subdirectory(ext/stb) add_subdirectory(ext/enkiTS) add_subdirectory(ext/ImGuiColorTextEdit) @@ -95,7 +91,7 @@ if (VIAMD_LINK_STDLIB_STATIC) if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU") set(VIAMD_STDLIBS "-static-libgcc -static-libstdc++") endif() - set(glfw_LIBRARIES ${VIAMD_STDLIBS}) + set(SDL3_LIBRARIES ${VIAMD_STDLIBS}) endif() if (CMAKE_C_COMPILER_ID STREQUAL "GNU") # GCC @@ -217,9 +213,8 @@ set_target_properties(viamd PROPERTIES target_link_options(viamd PRIVATE ${VIAMD_LINK_FLAGS} $<$:${VIAMD_LINK_FLAGS_DEB}> $<$:${VIAMD_LINK_FLAGS_REL}>) target_link_libraries(viamd - glfw + SDL3::SDL3-static imgui - nativefiledialog mdlib stb enkiTS diff --git a/_codeql_detected_source_root b/_codeql_detected_source_root new file mode 120000 index 00000000..945c9b46 --- /dev/null +++ b/_codeql_detected_source_root @@ -0,0 +1 @@ +. \ No newline at end of file diff --git a/ext/SDL b/ext/SDL new file mode 160000 index 00000000..f0d958d8 --- /dev/null +++ b/ext/SDL @@ -0,0 +1 @@ +Subproject commit f0d958d850144ae87b90d4631a4eb957088137cc diff --git a/ext/glfw b/ext/glfw deleted file mode 160000 index 7b6aead9..00000000 --- a/ext/glfw +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 7b6aead9fb88b3623e3b3725ebb42670cbe4c579 diff --git a/ext/nativefiledialog/CMakeLists.txt b/ext/nativefiledialog/CMakeLists.txt deleted file mode 100644 index 0977ed0d..00000000 --- a/ext/nativefiledialog/CMakeLists.txt +++ /dev/null @@ -1,72 +0,0 @@ -### NATIVEFILEDIALOG ### -project(nativefiledialoglib LANGUAGES C) - -cmake_minimum_required(VERSION 3.10) - -set(SRC ${CMAKE_CURRENT_SOURCE_DIR}/src/nfd_common.c) -set(LIB "") -set(INC "") -set(DEF "") -set(OPT "") - -if(UNIX AND NOT APPLE) - option(NATIVEFILEDIALOG_USE_GTK "Use GTK for window handling, fallback is Zenity" OFF) - if (NATIVEFILEDIALOG_USE_GTK) - set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake") - message(${CMAKE_MODULE_PATH}) - #include(cmake/FindGTK3.cmake) - #find_package(GTK3 REQUIRED gtk) - find_package(PkgConfig REQUIRED) - - #PKG_CHECK_MODULES(GTK REQUIRED gtk+-3.0) - #pkg_check_modules(GTK REQUIRED gtk+-3.0) - find_package(GTK3 3.0 REQUIRED gtk) - - set(SRC - ${SRC} - ${CMAKE_CURRENT_SOURCE_DIR}/src/nfd_gtk.c) - set(INC ${GTK3_INCLUDE_DIRS}) - set(LIB ${GTK3_LIBRARIES}) - set(DEF ${GTK3_CFLAGS_OTHER}) - else() - set(SRC - ${SRC} - ${CMAKE_CURRENT_SOURCE_DIR}/src/nfd_zenity.c) - endif() - set(OPT "-w") -elseif(APPLE) - find_library(APPKIT_LIBRARY AppKit) - set(SRC - ${SRC} - ${CMAKE_CURRENT_SOURCE_DIR}/src/nfd_cocoa.m) - set(LIB ${APPKIT_LIBRARY}) - set(OPT "-w") -else() #windows - set(SRC - ${SRC} - ${CMAKE_CURRENT_SOURCE_DIR}/src/nfd_win.cpp) - set(DEF "_CRT_SECURE_NO_WARNINGS") - if(MSVC) - set(OPT "/W3" "/wd4245") - endif() -endif() - -add_library(nativefiledialog ${SRC}) - -target_include_directories(nativefiledialog - PUBLIC - $ - $ - PRIVATE - ${CMAKE_CURRENT_SOURCE_DIR}/src - ${INC} -) - -target_compile_options(nativefiledialog - PRIVATE ${OPT}) - -target_compile_definitions(nativefiledialog - PRIVATE ${DEF}) - -target_link_libraries(nativefiledialog - PRIVATE ${LIB} ${VIAMD_STDLIBS}) diff --git a/ext/nativefiledialog/LICENSE b/ext/nativefiledialog/LICENSE deleted file mode 100644 index 3ab103c5..00000000 --- a/ext/nativefiledialog/LICENSE +++ /dev/null @@ -1,16 +0,0 @@ -This software is provided 'as-is', without any express or implied -warranty. In no event will the authors be held liable for any damages -arising from the use of this software. - -Permission is granted to anyone to use this software for any purpose, -including commercial applications, and to alter it and redistribute it -freely, subject to the following restrictions: - -1. The origin of this software must not be misrepresented; you must not - claim that you wrote the original software. If you use this software - in a product, an acknowledgment in the product documentation would be - appreciated but is not required. -2. Altered source versions must be plainly marked as such, and must not be - misrepresented as being the original software. -3. This notice may not be removed or altered from any source distribution. - diff --git a/ext/nativefiledialog/README.md b/ext/nativefiledialog/README.md deleted file mode 100644 index e34afa95..00000000 --- a/ext/nativefiledialog/README.md +++ /dev/null @@ -1,180 +0,0 @@ -# Native File Dialog # - -A tiny, neat C library that portably invokes native file open, folder select and save dialogs. Write dialog code once and have it pop up native dialogs on all supported platforms. Avoid linking large dependencies like wxWidgets and qt. - -Features: - - - Lean C API, static library -- no ObjC, no C++, no STL. - - Zlib licensed. - - Consistent UTF-8 support on all platforms. - - Simple universal file filter syntax. - - Paid support available. - - Multiple file selection support. - - 64-bit and 32-bit friendly. - - GCC, Clang, Xcode, Mingw and Visual Studio supported. - - No third party dependencies for building or linking. - - Support for Vista's modern `IFileDialog` on Windows. - - Support for non-deprecated Cocoa APIs on OS X. - - GTK3 dialog on Linux. - - Optional Zenity support on Linux to avoid linking GTK. - - Tested, works alongside [http://www.libsdl.org](SDL2) on all platforms, for the game developers out there. - -# Example Usage # - -```C -#include -#include -#include - -int main( void ) -{ - nfdchar_t *outPath = NULL; - nfdresult_t result = NFD_OpenDialog( NULL, NULL, &outPath ); - - if ( result == NFD_OKAY ) { - puts("Success!"); - puts(outPath); - free(outPath); - } - else if ( result == NFD_CANCEL ) { - puts("User pressed cancel."); - } - else { - printf("Error: %s\n", NFD_GetError() ); - } - - return 0; -} -``` - -See self-documenting API [NFD.h](src/include/nfd.h) for more options. - -# Screenshots # - -![Windows rendering a dialog](screens/open_win.png?raw=true) -![GTK3 on Linux rendering a dialog](screens/open_gtk3.png?raw=true) -![Cocoa on MacOS rendering a dialog](screens/open_cocoa.png?raw=true) - -## Changelog ## - - - **Major** version increments denote API or ABI departure. - - **Minor** version increments denote build or trivial departures. - - **Micro** version increments just recompile and drop-in. - -release | what's new | date ---------|-----------------------------|--------- -1.0.0 | initial | oct 2014 -1.1.0 | premake5; scons deprecated | aug 2016 -1.1.1 | mingw support, build fixes | aug 2016 -1.1.2 | test_pickfolder() added | aug 2016 -1.1.3 | zenity linux backend added | nov 2017 - | fix char type in decls | nov 2017 -1.1.4 | fix win32 memleaks | dec 2018 - | improve win32 errorhandling | dec 2018 - | macos fix focus bug | dec 2018 -1.1.5 | win32 fix com reinitialize | aug 2019 -1.1.6 | fix osx filter bug | aug 2019 - | remove deprecated scons | aug 2019 - | fix mingw compilation | aug 2019 - | -Wextra warning cleanup | aug 2019 - -## Building ## - -NFD uses [Premake5](https://premake.github.io/download.html) generated Makefiles and IDE project files. The generated project files are checked in under `build/` so you don't have to download and use Premake in most cases. - -If you need to run Premake5 directly, further [build documentation](docs/build.md) is available. - -Previously, NFD used SCons to build. As of 1.1.6, SCons support has been removed entirely. - -`nfd.a` will be built for release builds, and `nfd_d.a` will be built for debug builds. - -### Makefiles ### - -The makefile offers up to four options, with `release_x64` as the default. - - make config=release_x86 - make config=release_x64 - make config=debug_x86 - make config=debug_x64 - -### Compiling Your Programs ### - - 1. Add `src/include` to your include search path. - 2. Add `nfd.lib` or `nfd_d.lib` to the list of list of static libraries to link against (for release or debug, respectively). - 3. Add `build//` to the library search path. - -#### Linux GTK #### - -`apt-get libgtk-3-dev` installs the gtk dependency for library compilation. - -On Linux, you have the option of compiling and linking against GTK. If you use it, the recommended way to compile is to include the arguments of `pkg-config --cflags --libs gtk+-3.0`. - -#### Linux Zenity #### - -Alternatively, you can use the Zenity backend by running the Makefile in `build/gmake_linux_zenity`. Zenity runs the dialog in its own address space, but requires the user to have Zenity correctly installed and configured on their system. - -#### MacOS #### - -On Mac OS, add `AppKit` to the list of frameworks. - -#### Windows #### - -On Windows, ensure you are linking against `comctl32.lib`. - -## Usage ## - -See `NFD.h` for API calls. See `tests/*.c` for example code. - -After compiling, `build/bin` contains compiled test programs. The appropriate subdirectory under `build/lib` contains the built library. - -## File Filter Syntax ## - -There is a form of file filtering in every file dialog API, but no consistent means of supporting it. NFD provides support for filtering files by groups of extensions, providing its own descriptions (where applicable) for the extensions. - -A wildcard filter is always added to every dialog. - -### Separators ### - - - `;` Begin a new filter. - - `,` Add a separate type to the filter. - -#### Examples #### - -`txt` The default filter is for text files. There is a wildcard option in a dropdown. - -`png,jpg;psd` The default filter is for png and jpg files. A second filter is available for psd files. There is a wildcard option in a dropdown. - -`NULL` Wildcard only. - -## Iterating Over PathSets ## - -See [test_opendialogmultiple.c](test/test_opendialogmultiple.c). - -# Known Limitations # - -I accept quality code patches, or will resolve these and other matters through support. See [contributing](docs/contributing.md) for details. - - - No support for Windows XP's legacy dialogs such as `GetOpenFileName`. - - No support for file filter names -- ex: "Image Files" (*.png, *.jpg). Nameless filters are supported, however. - - GTK Zenity implementation's process exec error handling does not gracefully handle numerous error cases, choosing to abort rather than cleanup and return. - - GTK 3 spams one warning per dialog created. - -# Copyright and Credit # - -Copyright © 2014-2019 [Frogtoss Games](http://www.frogtoss.com), Inc. -File [LICENSE](LICENSE) covers all files in this repo. - -Native File Dialog by Michael Labbe - - -Tomasz Konojacki for [microutf8](http://puszcza.gnu.org.ua/software/microutf8/) - -[Denis Kolodin](https://github.com/DenisKolodin) for mingw support. - -[Tom Mason](https://github.com/wheybags) for Zenity support. - -## Support ## - -Directed support for this work is available from the original author under a paid agreement. - -[Contact Frogtoss Games](http://www.frogtoss.com/pages/contact.html). diff --git a/ext/nativefiledialog/cmake/FindGTK3.cmake b/ext/nativefiledialog/cmake/FindGTK3.cmake deleted file mode 100644 index a933c6cf..00000000 --- a/ext/nativefiledialog/cmake/FindGTK3.cmake +++ /dev/null @@ -1,566 +0,0 @@ -# - FindGTK3.cmake -# This module can find the GTK3 widget libraries and several of its other -# optional components like gtkmm, glade, and glademm. -# -# NOTE: If you intend to use version checking, CMake 2.6.2 or later is -# required. -# -# Specify one or more of the following components -# as you call this find module. See example below. -# -# gtk -# gtkmm -# glade -# glademm -# -# The following variables will be defined for your use -# -# GTK3_FOUND - Were all of your specified components found? -# GTK3_INCLUDE_DIRS - All include directories -# GTK3_LIBRARIES - All libraries -# -# GTK3_VERSION - The version of GTK3 found (x.y.z) -# GTK3_MAJOR_VERSION - The major version of GTK3 -# GTK3_MINOR_VERSION - The minor version of GTK3 -# GTK3_PATCH_VERSION - The patch version of GTK3 -# -# Optional variables you can define prior to calling this module: -# -# GTK3_DEBUG - Enables verbose debugging of the module -# GTK3_SKIP_MARK_AS_ADVANCED - Disable marking cache variables as advanced -# GTK3_ADDITIONAL_SUFFIXES - Allows defining additional directories to -# search for include files -# -#================= -# Example Usage: -# -# Call find_package() once, here are some examples to pick from: -# -# Require GTK 3.0 or later -# find_package(GTK3 3.0 REQUIRED gtk) -# -# if(GTK3_FOUND) -# include_directories(${GTK3_INCLUDE_DIRS}) -# add_executable(mygui mygui.cc) -# target_link_libraries(mygui ${GTK3_LIBRARIES}) -# endif() -# - -#============================================================================= -# Copyright 2009 Kitware, Inc. -# Copyright 2008-2009 Philip Lowman -# -# Distributed under the OSI-approved BSD License (the "License"); -# see accompanying file Copyright.txt for details. -# -# This software is distributed WITHOUT ANY WARRANTY; without even the -# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. -# See the License for more information. -#============================================================================= -# (To distribute this file outside of CMake, substitute the full -# License text for the above reference.) - - -# Version 0.1 (5/13/2011) -# * First cut at a GTK3 version (Heavily derived from -# FindGTK2.cmake) - - -#============================================================= -# _GTK3_GET_VERSION -# Internal function to parse the version number in gtkversion.h -# _OUT_major = Major version number -# _OUT_minor = Minor version number -# _OUT_micro = Micro version number -# _gtkversion_hdr = Header file to parse -#============================================================= -function(_GTK3_GET_VERSION _OUT_major _OUT_minor _OUT_micro _gtkversion_hdr) - file(READ ${_gtkversion_hdr} _contents) - if(_contents) - string(REGEX REPLACE ".*#define GTK_MAJOR_VERSION[ \t]+\\(([0-9]+)\\).*" "\\1" ${_OUT_major} "${_contents}") - string(REGEX REPLACE ".*#define GTK_MINOR_VERSION[ \t]+\\(([0-9]+)\\).*" "\\1" ${_OUT_minor} "${_contents}") - string(REGEX REPLACE ".*#define GTK_MICRO_VERSION[ \t]+\\(([0-9]+)\\).*" "\\1" ${_OUT_micro} "${_contents}") - - if(NOT ${_OUT_major} MATCHES "[0-9]+") - message(FATAL_ERROR "Version parsing failed for GTK3_MAJOR_VERSION!") - endif() - if(NOT ${_OUT_minor} MATCHES "[0-9]+") - message(FATAL_ERROR "Version parsing failed for GTK3_MINOR_VERSION!") - endif() - if(NOT ${_OUT_micro} MATCHES "[0-9]+") - message(FATAL_ERROR "Version parsing failed for GTK3_MICRO_VERSION!") - endif() - - set(${_OUT_major} ${${_OUT_major}} PARENT_SCOPE) - set(${_OUT_minor} ${${_OUT_minor}} PARENT_SCOPE) - set(${_OUT_micro} ${${_OUT_micro}} PARENT_SCOPE) - else() - message(FATAL_ERROR "Include file ${_gtkversion_hdr} does not exist") - endif() -endfunction() - -#============================================================= -# _GTK3_FIND_INCLUDE_DIR -# Internal function to find the GTK include directories -# _var = variable to set -# _hdr = header file to look for -#============================================================= -function(_GTK3_FIND_INCLUDE_DIR _var _hdr) - - if(GTK3_DEBUG) - message(STATUS "[FindGTK3.cmake:${CMAKE_CURRENT_LIST_LINE}] " - "_GTK3_FIND_INCLUDE_DIR( ${_var} ${_hdr} )") - endif() - - set(_relatives - # If these ever change, things will break. - ${GTK3_ADDITIONAL_SUFFIXES} - glibmm-2.0 - glib-2.0 - atk-1.0 - atkmm-1.0 - cairo - cairomm-1.0 - gdk-pixbuf-2.0 - gdkmm-2.4 - giomm-2.4 - gtk-3.0 - gtkmm-2.4 - libglade-2.0 - libglademm-2.4 - harfbuzz - pango-1.0 - pangomm-1.4 - sigc++-2.2 - gtk-unix-print-2.0 - ) - - set(_suffixes) - foreach(_d ${_relatives}) - list(APPEND _suffixes ${_d}) - list(APPEND _suffixes ${_d}/include) # for /usr/lib/gtk-2.0/include - endforeach() - - if(GTK3_DEBUG) - message(STATUS "[FindGTK3.cmake:${CMAKE_CURRENT_LIST_LINE}] " - "include suffixes = ${_suffixes}") - endif() - - find_path(${_var} ${_hdr} - PATHS - /usr/local/lib64 - /usr/local/lib - # fix for Ubuntu == 11.04 (Natty Narwhal) - /usr/lib/i386-linux-gnu/ - /usr/lib/x86_64-linux-gnu/ - # end - # fix for Ubuntu >= 11.10 (Oneiric Ocelot) - /usr/lib/${CMAKE_LIBRARY_ARCHITECTURE} - # end - /usr/lib64 - /usr/lib - /opt/gnome/include - /opt/gnome/lib - /opt/openwin/include - /usr/openwin/lib - /sw/include - /sw/lib - /opt/local/include - /opt/local/lib - $ENV{GTKMM_BASEPATH}/include - $ENV{GTKMM_BASEPATH}/lib - [HKEY_CURRENT_USER\\SOFTWARE\\gtkmm\\2.4;Path]/include - [HKEY_CURRENT_USER\\SOFTWARE\\gtkmm\\2.4;Path]/lib - [HKEY_LOCAL_MACHINE\\SOFTWARE\\gtkmm\\2.4;Path]/include - [HKEY_LOCAL_MACHINE\\SOFTWARE\\gtkmm\\2.4;Path]/lib - PATH_SUFFIXES - ${_suffixes} - ) - - if(${_var}) - set(GTK3_INCLUDE_DIRS ${GTK3_INCLUDE_DIRS} ${${_var}} PARENT_SCOPE) - if(NOT GTK3_SKIP_MARK_AS_ADVANCED) - mark_as_advanced(${_var}) - endif() - endif() - -endfunction(_GTK3_FIND_INCLUDE_DIR) - -#============================================================= -# _GTK3_FIND_LIBRARY -# Internal function to find libraries packaged with GTK3 -# _var = library variable to create -#============================================================= -function(_GTK3_FIND_LIBRARY _var _lib _expand_vc _append_version) - - if(GTK3_DEBUG) - message(STATUS "[FindGTK3.cmake:${CMAKE_CURRENT_LIST_LINE}] " - "_GTK3_FIND_LIBRARY( ${_var} ${_lib} ${_expand_vc} ${_append_version} )") - endif() - - # Not GTK versions per se but the versions encoded into Windows - # import libraries (GtkMM 2.14.1 has a gtkmm-vc80-2_4.lib for example) - # Also the MSVC libraries use _ for . (this is handled below) - # ********* SOMEONE WITH WINDOWS NEEDS TO CHECK THIS BIT FOR V3 ********* - # ********* the plain 3 is needed to get Debian Sid to find the libraries - set(_versions 3.0 3 2.20 2.18 2.16 2.14 2.12 - 2.10 2.8 2.6 2.4 2.2 2.0 - 1.20 1.18 1.16 1.14 1.12 - 1.10 1.8 1.6 1.4 1.2 1.0) - - set(_library) - set(_library_d) - - set(_library ${_lib}) - - if(_expand_vc AND MSVC) - # Add vc80/vc90/vc100 midfixes - if(MSVC80) - set(_library ${_library}-vc80) - elseif(MSVC90) - set(_library ${_library}-vc90) - elseif(MSVC10) - set(_library ${_library}-vc100) - endif() - set(_library_d ${_library}-d) - endif() - - if(GTK3_DEBUG) - message(STATUS "[FindGTK3.cmake:${CMAKE_CURRENT_LIST_LINE}] " - "After midfix addition = ${_library} and ${_library_d}") - endif() - - set(_lib_list) - set(_libd_list) - if(_append_version) - foreach(_ver ${_versions}) - list(APPEND _lib_list "${_library}-${_ver}") - list(APPEND _libd_list "${_library_d}-${_ver}") - endforeach() - else() - set(_lib_list ${_library}) - set(_libd_list ${_library_d}) - endif() - - if(GTK3_DEBUG) - message(STATUS "[FindGTK3.cmake:${CMAKE_CURRENT_LIST_LINE}] " - "library list = ${_lib_list} and library debug list = ${_libd_list}") - endif() - - # For some silly reason the MSVC libraries use _ instead of . - # in the version fields - if(_expand_vc AND MSVC) - set(_no_dots_lib_list) - set(_no_dots_libd_list) - foreach(_l ${_lib_list}) - string(REPLACE "." "_" _no_dots_library ${_l}) - list(APPEND _no_dots_lib_list ${_no_dots_library}) - endforeach() - # And for debug - set(_no_dots_libsd_list) - foreach(_l ${_libd_list}) - string(REPLACE "." "_" _no_dots_libraryd ${_l}) - list(APPEND _no_dots_libd_list ${_no_dots_libraryd}) - endforeach() - - # Copy list back to original names - set(_lib_list ${_no_dots_lib_list}) - set(_libd_list ${_no_dots_libd_list}) - endif() - - if(GTK3_DEBUG) - message(STATUS "[FindGTK3.cmake:${CMAKE_CURRENT_LIST_LINE}] " - "While searching for ${_var}, our proposed library list is ${_lib_list}") - endif() - - find_library(${_var} - NAMES ${_lib_list} - PATHS - /opt/gnome/lib - /opt/gnome/lib64 - /usr/openwin/lib - /usr/openwin/lib64 - /sw/lib - $ENV{GTKMM_BASEPATH}/lib - [HKEY_CURRENT_USER\\SOFTWARE\\gtkmm\\2.4;Path]/lib - [HKEY_LOCAL_MACHINE\\SOFTWARE\\gtkmm\\2.4;Path]/lib - ) - - if(_expand_vc AND MSVC) - if(GTK3_DEBUG) - message(STATUS "[FindGTK3.cmake:${CMAKE_CURRENT_LIST_LINE}] " - "While searching for ${_var}_DEBUG our proposed library list is ${_libd_list}") - endif() - - find_library(${_var}_DEBUG - NAMES ${_libd_list} - PATHS - $ENV{GTKMM_BASEPATH}/lib - [HKEY_CURRENT_USER\\SOFTWARE\\gtkmm\\2.4;Path]/lib - [HKEY_LOCAL_MACHINE\\SOFTWARE\\gtkmm\\2.4;Path]/lib - ) - - if(${_var} AND ${_var}_DEBUG) - if(NOT GTK3_SKIP_MARK_AS_ADVANCED) - mark_as_advanced(${_var}_DEBUG) - endif() - set(GTK3_LIBRARIES ${GTK3_LIBRARIES} optimized ${${_var}} debug ${${_var}_DEBUG}) - set(GTK3_LIBRARIES ${GTK3_LIBRARIES} PARENT_SCOPE) - endif() - else() - if(NOT GTK3_SKIP_MARK_AS_ADVANCED) - mark_as_advanced(${_var}) - endif() - set(GTK3_LIBRARIES ${GTK3_LIBRARIES} ${${_var}}) - set(GTK3_LIBRARIES ${GTK3_LIBRARIES} PARENT_SCOPE) - # Set debug to release - set(${_var}_DEBUG ${${_var}}) - set(${_var}_DEBUG ${${_var}} PARENT_SCOPE) - endif() -endfunction(_GTK3_FIND_LIBRARY) - -#============================================================= - -# -# main() -# - -set(GTK3_FOUND) -set(GTK3_INCLUDE_DIRS) -set(GTK3_LIBRARIES) - -if(NOT GTK3_FIND_COMPONENTS) - # Assume they only want GTK - set(GTK3_FIND_COMPONENTS gtk) -endif() - -# -# If specified, enforce version number -# -if(GTK3_FIND_VERSION) - cmake_minimum_required(VERSION 2.6.2) - set(GTK3_FAILED_VERSION_CHECK true) - if(GTK3_DEBUG) - message(STATUS "[FindGTK3.cmake:${CMAKE_CURRENT_LIST_LINE}] " - "Searching for version ${GTK3_FIND_VERSION}") - endif() - _GTK3_FIND_INCLUDE_DIR(GTK3_GTK_INCLUDE_DIR gtk/gtk.h) - if(GTK3_GTK_INCLUDE_DIR) - _GTK3_GET_VERSION(GTK3_MAJOR_VERSION - GTK3_MINOR_VERSION - GTK3_PATCH_VERSION - ${GTK3_GTK_INCLUDE_DIR}/gtk/gtkversion.h) - set(GTK3_VERSION - ${GTK3_MAJOR_VERSION}.${GTK3_MINOR_VERSION}.${GTK3_PATCH_VERSION}) - if(GTK3_FIND_VERSION_EXACT) - if(GTK3_VERSION VERSION_EQUAL GTK3_FIND_VERSION) - set(GTK3_FAILED_VERSION_CHECK false) - endif() - else() - if(GTK3_VERSION VERSION_EQUAL GTK3_FIND_VERSION OR - GTK3_VERSION VERSION_GREATER GTK3_FIND_VERSION) - set(GTK3_FAILED_VERSION_CHECK false) - endif() - endif() - else() - # If we can't find the GTK include dir, we can't do version checking - if(GTK3_FIND_REQUIRED AND NOT GTK3_FIND_QUIETLY) - message(FATAL_ERROR "Could not find GTK3 include directory") - endif() - return() - endif() - - if(GTK3_FAILED_VERSION_CHECK) - if(GTK3_FIND_REQUIRED AND NOT GTK3_FIND_QUIETLY) - if(GTK3_FIND_VERSION_EXACT) - message(FATAL_ERROR "GTK3 version check failed. Version ${GTK3_VERSION} was found, version ${GTK3_FIND_VERSION} is needed exactly.") - else() - message(FATAL_ERROR "GTK3 version check failed. Version ${GTK3_VERSION} was found, at least version ${GTK3_FIND_VERSION} is required") - endif() - endif() - - # If the version check fails, exit out of the module here - return() - endif() -endif() - -# -# Find all components -# - -find_package(Freetype) -list(APPEND GTK3_INCLUDE_DIRS ${FREETYPE_INCLUDE_DIRS}) -list(APPEND GTK3_LIBRARIES ${FREETYPE_LIBRARIES}) - -foreach(_GTK3_component ${GTK3_FIND_COMPONENTS}) - if(_GTK3_component STREQUAL "gtk") - _GTK3_FIND_INCLUDE_DIR(GTK3_GLIB_INCLUDE_DIR glib.h) - _GTK3_FIND_INCLUDE_DIR(GTK3_GLIBCONFIG_INCLUDE_DIR glibconfig.h) - _GTK3_FIND_LIBRARY (GTK3_GLIB_LIBRARY glib false true) - - _GTK3_FIND_INCLUDE_DIR(GTK3_GOBJECT_INCLUDE_DIR gobject/gobject.h) - _GTK3_FIND_LIBRARY (GTK3_GOBJECT_LIBRARY gobject false true) - - _GTK3_FIND_INCLUDE_DIR(GTK3_GDK_PIXBUF_INCLUDE_DIR gdk-pixbuf/gdk-pixbuf.h) - _GTK3_FIND_LIBRARY (GTK3_GDK_PIXBUF_LIBRARY gdk_pixbuf false true) - - _GTK3_FIND_INCLUDE_DIR(GTK3_GDK_INCLUDE_DIR gdk/gdk.h) - _GTK3_FIND_INCLUDE_DIR(GTK3_GDKCONFIG_INCLUDE_DIR gdk/gdkconfig.h) - _GTK3_FIND_INCLUDE_DIR(GTK3_GTK_INCLUDE_DIR gtk/gtk.h) - - # ********* At least on Debian the gdk & gtk libraries - # ********* don't have the -x11 suffix. - if(UNIX) - _GTK3_FIND_LIBRARY (GTK3_GDK_LIBRARY gdk false true) - _GTK3_FIND_LIBRARY (GTK3_GTK_LIBRARY gtk false true) - else() - _GTK3_FIND_LIBRARY (GTK3_GDK_LIBRARY gdk-win32 false true) - _GTK3_FIND_LIBRARY (GTK3_GTK_LIBRARY gtk-win32 false true) - endif() - - _GTK3_FIND_INCLUDE_DIR(GTK3_CAIRO_INCLUDE_DIR cairo.h) - _GTK3_FIND_LIBRARY (GTK3_CAIRO_LIBRARY cairo false false) - - _GTK3_FIND_INCLUDE_DIR(GTK3_FONTCONFIG_INCLUDE_DIR fontconfig/fontconfig.h) - - _GTK3_FIND_INCLUDE_DIR(GTK3_PANGO_INCLUDE_DIR pango/pango.h) - _GTK3_FIND_LIBRARY (GTK3_PANGO_LIBRARY pango false true) - - _GTK3_FIND_INCLUDE_DIR(GTK3_HARFBUZZ_INCLUDE_DIR hb.h) - - _GTK3_FIND_INCLUDE_DIR(GTK3_ATK_INCLUDE_DIR atk/atk.h) - _GTK3_FIND_LIBRARY (GTK3_ATK_LIBRARY atk false true) - - - elseif(_GTK3_component STREQUAL "gtkmm") - - _GTK3_FIND_INCLUDE_DIR(GTK3_GLIBMM_INCLUDE_DIR glibmm.h) - _GTK3_FIND_INCLUDE_DIR(GTK3_GLIBMMCONFIG_INCLUDE_DIR glibmmconfig.h) - _GTK3_FIND_LIBRARY (GTK3_GLIBMM_LIBRARY glibmm true true) - - _GTK3_FIND_INCLUDE_DIR(GTK3_GDKMM_INCLUDE_DIR gdkmm.h) - _GTK3_FIND_INCLUDE_DIR(GTK3_GDKMMCONFIG_INCLUDE_DIR gdkmmconfig.h) - _GTK3_FIND_LIBRARY (GTK3_GDKMM_LIBRARY gdkmm true true) - - _GTK3_FIND_INCLUDE_DIR(GTK3_GTKMM_INCLUDE_DIR gtkmm.h) - _GTK3_FIND_INCLUDE_DIR(GTK3_GTKMMCONFIG_INCLUDE_DIR gtkmmconfig.h) - _GTK3_FIND_LIBRARY (GTK3_GTKMM_LIBRARY gtkmm true true) - - _GTK3_FIND_INCLUDE_DIR(GTK3_CAIROMM_INCLUDE_DIR cairomm/cairomm.h) - _GTK3_FIND_LIBRARY (GTK3_CAIROMM_LIBRARY cairomm true true) - - _GTK3_FIND_INCLUDE_DIR(GTK3_PANGOMM_INCLUDE_DIR pangomm.h) - _GTK3_FIND_INCLUDE_DIR(GTK3_PANGOMMCONFIG_INCLUDE_DIR pangommconfig.h) - _GTK3_FIND_LIBRARY (GTK3_PANGOMM_LIBRARY pangomm true true) - - _GTK3_FIND_INCLUDE_DIR(GTK3_SIGC++_INCLUDE_DIR sigc++/sigc++.h) - _GTK3_FIND_INCLUDE_DIR(GTK3_SIGC++CONFIG_INCLUDE_DIR sigc++config.h) - _GTK3_FIND_LIBRARY (GTK3_SIGC++_LIBRARY sigc true true) - - _GTK3_FIND_INCLUDE_DIR(GTK3_GIOMM_INCLUDE_DIR giomm.h) - _GTK3_FIND_INCLUDE_DIR(GTK3_GIOMMCONFIG_INCLUDE_DIR giommconfig.h) - _GTK3_FIND_LIBRARY (GTK3_GIOMM_LIBRARY giomm true true) - - _GTK3_FIND_INCLUDE_DIR(GTK3_ATKMM_INCLUDE_DIR atkmm.h) - _GTK3_FIND_LIBRARY (GTK3_ATKMM_LIBRARY atkmm true true) - - elseif(_GTK3_component STREQUAL "glade") - - _GTK3_FIND_INCLUDE_DIR(GTK3_GLADE_INCLUDE_DIR glade/glade.h) - _GTK3_FIND_LIBRARY (GTK3_GLADE_LIBRARY glade false true) - - elseif(_GTK3_component STREQUAL "glademm") - - _GTK3_FIND_INCLUDE_DIR(GTK3_GLADEMM_INCLUDE_DIR libglademm.h) - _GTK3_FIND_INCLUDE_DIR(GTK3_GLADEMMCONFIG_INCLUDE_DIR libglademmconfig.h) - _GTK3_FIND_LIBRARY (GTK3_GLADEMM_LIBRARY glademm true true) - - else() - message(FATAL_ERROR "Unknown GTK3 component ${_component}") - endif() -endforeach() - -# -# Solve for the GTK3 version if we haven't already -# -if(NOT GTK3_FIND_VERSION AND GTK3_GTK_INCLUDE_DIR) - _GTK3_GET_VERSION(GTK3_MAJOR_VERSION - GTK3_MINOR_VERSION - GTK3_PATCH_VERSION - ${GTK3_GTK_INCLUDE_DIR}/gtk/gtkversion.h) - set(GTK3_VERSION ${GTK3_MAJOR_VERSION}.${GTK3_MINOR_VERSION}.${GTK3_PATCH_VERSION}) -endif() - -# -# Try to enforce components -# - -set(_GTK3_did_we_find_everything true) # This gets set to GTK3_FOUND - -include(FindPackageHandleStandardArgs) -#include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake) - -foreach(_GTK3_component ${GTK3_FIND_COMPONENTS}) - string(TOUPPER ${_GTK3_component} _COMPONENT_UPPER) - - if(_GTK3_component STREQUAL "gtk") - FIND_PACKAGE_HANDLE_STANDARD_ARGS(GTK3_${_COMPONENT_UPPER} "Some or all of the gtk libraries were not found." - GTK3_GTK_LIBRARY - GTK3_GTK_INCLUDE_DIR - - GTK3_GLIB_INCLUDE_DIR - GTK3_GLIBCONFIG_INCLUDE_DIR - GTK3_GLIB_LIBRARY - - GTK3_GDK_INCLUDE_DIR - GTK3_GDKCONFIG_INCLUDE_DIR - GTK3_GDK_LIBRARY - ) - elseif(_GTK3_component STREQUAL "gtkmm") - FIND_PACKAGE_HANDLE_STANDARD_ARGS(GTK3_${_COMPONENT_UPPER} "Some or all of the gtkmm libraries were not found." - GTK3_GTKMM_LIBRARY - GTK3_GTKMM_INCLUDE_DIR - GTK3_GTKMMCONFIG_INCLUDE_DIR - - GTK3_GLIBMM_INCLUDE_DIR - GTK3_GLIBMMCONFIG_INCLUDE_DIR - GTK3_GLIBMM_LIBRARY - - GTK3_GDKMM_INCLUDE_DIR - GTK3_GDKMMCONFIG_INCLUDE_DIR - GTK3_GDKMM_LIBRARY - ) - elseif(_GTK3_component STREQUAL "glade") - FIND_PACKAGE_HANDLE_STANDARD_ARGS(GTK3_${_COMPONENT_UPPER} "The glade library was not found." - GTK3_GLADE_LIBRARY - GTK3_GLADE_INCLUDE_DIR - ) - elseif(_GTK3_component STREQUAL "glademm") - FIND_PACKAGE_HANDLE_STANDARD_ARGS(GTK3_${_COMPONENT_UPPER} "The glademm library was not found." - GTK3_GLADEMM_LIBRARY - GTK3_GLADEMM_INCLUDE_DIR - GTK3_GLADEMMCONFIG_INCLUDE_DIR - ) - endif() - - if(NOT GTK3_${_COMPONENT_UPPER}_FOUND) - set(_GTK3_did_we_find_everything false) - endif() -endforeach() - -if(_GTK3_did_we_find_everything AND NOT GTK3_VERSION_CHECK_FAILED) - set(GTK3_FOUND true) -else() - # Unset our variables. - set(GTK3_FOUND false) - set(GTK3_VERSION) - set(GTK3_VERSION_MAJOR) - set(GTK3_VERSION_MINOR) - set(GTK3_VERSION_PATCH) - set(GTK3_INCLUDE_DIRS) - set(GTK3_LIBRARIES) -endif() - -if(GTK3_INCLUDE_DIRS) - list(REMOVE_DUPLICATES GTK3_INCLUDE_DIRS) -endif() diff --git a/ext/nativefiledialog/src/common.h b/ext/nativefiledialog/src/common.h deleted file mode 100644 index 7745d323..00000000 --- a/ext/nativefiledialog/src/common.h +++ /dev/null @@ -1,21 +0,0 @@ -/* - Native File Dialog - - Internal, common across platforms - - http://www.frogtoss.com/labs - */ - - -#ifndef _NFD_COMMON_H -#define _NFD_COMMON_H - -#define NFD_MAX_STRLEN 256 -#define _NFD_UNUSED(x) ((void)x) - -void *NFDi_Malloc( size_t bytes ); -void NFDi_Free( void *ptr ); -void NFDi_SetError( const char *msg ); -void NFDi_SafeStrncpy( char *dst, const char *src, size_t maxCopy ); - -#endif diff --git a/ext/nativefiledialog/src/include/nfd.h b/ext/nativefiledialog/src/include/nfd.h deleted file mode 100644 index 74c92743..00000000 --- a/ext/nativefiledialog/src/include/nfd.h +++ /dev/null @@ -1,74 +0,0 @@ -/* - Native File Dialog - - User API - - http://www.frogtoss.com/labs - */ - - -#ifndef _NFD_H -#define _NFD_H - -#ifdef __cplusplus -extern "C" { -#endif - -#include - -/* denotes UTF-8 char */ -typedef char nfdchar_t; - -/* opaque data structure -- see NFD_PathSet_* */ -typedef struct { - nfdchar_t *buf; - size_t *indices; /* byte offsets into buf */ - size_t count; /* number of indices into buf */ -}nfdpathset_t; - -typedef enum { - NFD_ERROR, /* programmatic error */ - NFD_OKAY, /* user pressed okay, or successful return */ - NFD_CANCEL /* user pressed cancel */ -}nfdresult_t; - - -/* nfd_.c */ - -/* single file open dialog */ -nfdresult_t NFD_OpenDialog( const nfdchar_t *filterList, - const nfdchar_t *defaultPath, - nfdchar_t **outPath ); - -/* multiple file open dialog */ -nfdresult_t NFD_OpenDialogMultiple( const nfdchar_t *filterList, - const nfdchar_t *defaultPath, - nfdpathset_t *outPaths ); - -/* save dialog */ -nfdresult_t NFD_SaveDialog( const nfdchar_t *filterList, - const nfdchar_t *defaultPath, - nfdchar_t **outPath ); - - -/* select folder dialog */ -nfdresult_t NFD_PickFolder( const nfdchar_t *defaultPath, - nfdchar_t **outPath); - -/* nfd_common.c */ - -/* get last error -- set when nfdresult_t returns NFD_ERROR */ -const char *NFD_GetError( void ); -/* get the number of entries stored in pathSet */ -size_t NFD_PathSet_GetCount( const nfdpathset_t *pathSet ); -/* Get the UTF-8 path at offset index */ -nfdchar_t *NFD_PathSet_GetPath( const nfdpathset_t *pathSet, size_t index ); -/* Free the pathSet */ -void NFD_PathSet_Free( nfdpathset_t *pathSet ); - - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/ext/nativefiledialog/src/nfd_cocoa.m b/ext/nativefiledialog/src/nfd_cocoa.m deleted file mode 100644 index 776152d4..00000000 --- a/ext/nativefiledialog/src/nfd_cocoa.m +++ /dev/null @@ -1,286 +0,0 @@ -/* - Native File Dialog - - http://www.frogtoss.com/labs - */ - -#include -#include "nfd.h" -#include "nfd_common.h" - -static NSArray *BuildAllowedFileTypes( const char *filterList ) -{ - // Commas and semicolons are the same thing on this platform - - NSMutableArray *buildFilterList = [[NSMutableArray alloc] init]; - - char typebuf[NFD_MAX_STRLEN] = {0}; - - size_t filterListLen = strlen(filterList); - char *p_typebuf = typebuf; - for ( size_t i = 0; i < filterListLen+1; ++i ) - { - if ( filterList[i] == ',' || filterList[i] == ';' || filterList[i] == '\0' ) - { - if (filterList[i] != '\0') - ++p_typebuf; - *p_typebuf = '\0'; - - NSString *thisType = [NSString stringWithUTF8String: typebuf]; - [buildFilterList addObject:thisType]; - p_typebuf = typebuf; - *p_typebuf = '\0'; - } - else - { - *p_typebuf = filterList[i]; - ++p_typebuf; - - } - } - - NSArray *returnArray = [NSArray arrayWithArray:buildFilterList]; - - [buildFilterList release]; - return returnArray; -} - -static void AddFilterListToDialog( NSSavePanel *dialog, const char *filterList ) -{ - if ( !filterList || strlen(filterList) == 0 ) - return; - - NSArray *allowedFileTypes = BuildAllowedFileTypes( filterList ); - if ( [allowedFileTypes count] != 0 ) - { - [dialog setAllowedFileTypes:allowedFileTypes]; - } -} - -static void SetDefaultPath( NSSavePanel *dialog, const nfdchar_t *defaultPath ) -{ - if ( !defaultPath || strlen(defaultPath) == 0 ) - return; - - NSString *defaultPathString = [NSString stringWithUTF8String: defaultPath]; - NSURL *url = [NSURL fileURLWithPath:defaultPathString isDirectory:YES]; - [dialog setDirectoryURL:url]; -} - - -/* fixme: pathset should be pathSet */ -static nfdresult_t AllocPathSet( NSArray *urls, nfdpathset_t *pathset ) -{ - assert(pathset); - assert([urls count]); - - pathset->count = (size_t)[urls count]; - pathset->indices = NFDi_Malloc( sizeof(size_t)*pathset->count ); - if ( !pathset->indices ) - { - return NFD_ERROR; - } - - // count the total space needed for buf - size_t bufsize = 0; - for ( NSURL *url in urls ) - { - NSString *path = [url path]; - bufsize += [path lengthOfBytesUsingEncoding:NSUTF8StringEncoding] + 1; - } - - pathset->buf = NFDi_Malloc( sizeof(nfdchar_t) * bufsize ); - if ( !pathset->buf ) - { - return NFD_ERROR; - } - - // fill buf - nfdchar_t *p_buf = pathset->buf; - size_t count = 0; - for ( NSURL *url in urls ) - { - NSString *path = [url path]; - const nfdchar_t *utf8Path = [path UTF8String]; - size_t byteLen = [path lengthOfBytesUsingEncoding:NSUTF8StringEncoding] + 1; - memcpy( p_buf, utf8Path, byteLen ); - - ptrdiff_t index = p_buf - pathset->buf; - assert( index >= 0 ); - pathset->indices[count] = (size_t)index; - - p_buf += byteLen; - ++count; - } - - return NFD_OKAY; -} - -/* public */ - - -nfdresult_t NFD_OpenDialog( const nfdchar_t *filterList, - const nfdchar_t *defaultPath, - nfdchar_t **outPath ) -{ - NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; - - NSWindow *keyWindow = [[NSApplication sharedApplication] keyWindow]; - NSOpenPanel *dialog = [NSOpenPanel openPanel]; - [dialog setAllowsMultipleSelection:NO]; - - // Build the filter list - AddFilterListToDialog(dialog, filterList); - - // Set the starting directory - SetDefaultPath(dialog, defaultPath); - - nfdresult_t nfdResult = NFD_CANCEL; - if ( [dialog runModal] == NSModalResponseOK ) - { - NSURL *url = [dialog URL]; - const char *utf8Path = [[url path] UTF8String]; - - // byte count, not char count - size_t len = strlen(utf8Path);//NFDi_UTF8_Strlen(utf8Path); - - *outPath = NFDi_Malloc( len+1 ); - if ( !*outPath ) - { - [pool release]; - [keyWindow makeKeyAndOrderFront:nil]; - return NFD_ERROR; - } - memcpy( *outPath, utf8Path, len+1 ); /* copy null term */ - nfdResult = NFD_OKAY; - } - [pool release]; - - [keyWindow makeKeyAndOrderFront:nil]; - return nfdResult; -} - - -nfdresult_t NFD_OpenDialogMultiple( const nfdchar_t *filterList, - const nfdchar_t *defaultPath, - nfdpathset_t *outPaths ) -{ - NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; - NSWindow *keyWindow = [[NSApplication sharedApplication] keyWindow]; - - NSOpenPanel *dialog = [NSOpenPanel openPanel]; - [dialog setAllowsMultipleSelection:YES]; - - // Build the fiter list. - AddFilterListToDialog(dialog, filterList); - - // Set the starting directory - SetDefaultPath(dialog, defaultPath); - - nfdresult_t nfdResult = NFD_CANCEL; - if ( [dialog runModal] == NSModalResponseOK ) - { - NSArray *urls = [dialog URLs]; - - if ( [urls count] == 0 ) - { - [pool release]; - [keyWindow makeKeyAndOrderFront:nil]; - return NFD_CANCEL; - } - - if ( AllocPathSet( urls, outPaths ) == NFD_ERROR ) - { - [pool release]; - [keyWindow makeKeyAndOrderFront:nil]; - return NFD_ERROR; - } - - nfdResult = NFD_OKAY; - } - [pool release]; - - [keyWindow makeKeyAndOrderFront:nil]; - return nfdResult; -} - - -nfdresult_t NFD_SaveDialog( const nfdchar_t *filterList, - const nfdchar_t *defaultPath, - nfdchar_t **outPath ) -{ - NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; - NSWindow *keyWindow = [[NSApplication sharedApplication] keyWindow]; - - NSSavePanel *dialog = [NSSavePanel savePanel]; - [dialog setExtensionHidden:NO]; - - // Build the filter list. - AddFilterListToDialog(dialog, filterList); - - // Set the starting directory - SetDefaultPath(dialog, defaultPath); - - nfdresult_t nfdResult = NFD_CANCEL; - if ( [dialog runModal] == NSModalResponseOK ) - { - NSURL *url = [dialog URL]; - const char *utf8Path = [[url path] UTF8String]; - - size_t byteLen = [url.path lengthOfBytesUsingEncoding:NSUTF8StringEncoding] + 1; - - *outPath = NFDi_Malloc( byteLen ); - if ( !*outPath ) - { - [pool release]; - [keyWindow makeKeyAndOrderFront:nil]; - return NFD_ERROR; - } - memcpy( *outPath, utf8Path, byteLen ); - nfdResult = NFD_OKAY; - } - - [pool release]; - [keyWindow makeKeyAndOrderFront:nil]; - return nfdResult; -} - -nfdresult_t NFD_PickFolder(const nfdchar_t *defaultPath, - nfdchar_t **outPath) -{ - NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; - - NSWindow *keyWindow = [[NSApplication sharedApplication] keyWindow]; - NSOpenPanel *dialog = [NSOpenPanel openPanel]; - [dialog setAllowsMultipleSelection:NO]; - [dialog setCanChooseDirectories:YES]; - [dialog setCanCreateDirectories:YES]; - [dialog setCanChooseFiles:NO]; - - // Set the starting directory - SetDefaultPath(dialog, defaultPath); - - nfdresult_t nfdResult = NFD_CANCEL; - if ( [dialog runModal] == NSModalResponseOK ) - { - NSURL *url = [dialog URL]; - const char *utf8Path = [[url path] UTF8String]; - - // byte count, not char count - size_t len = strlen(utf8Path);//NFDi_UTF8_Strlen(utf8Path); - - *outPath = NFDi_Malloc( len+1 ); - if ( !*outPath ) - { - [pool release]; - [keyWindow makeKeyAndOrderFront:nil]; - return NFD_ERROR; - } - memcpy( *outPath, utf8Path, len+1 ); /* copy null term */ - nfdResult = NFD_OKAY; - } - [pool release]; - - [keyWindow makeKeyAndOrderFront:nil]; - return nfdResult; -} diff --git a/ext/nativefiledialog/src/nfd_common.c b/ext/nativefiledialog/src/nfd_common.c deleted file mode 100644 index 55517f5d..00000000 --- a/ext/nativefiledialog/src/nfd_common.c +++ /dev/null @@ -1,142 +0,0 @@ -/* - Native File Dialog - - http://www.frogtoss.com/labs - */ - -#include -#include -#include -#include "nfd_common.h" - -static char g_errorstr[NFD_MAX_STRLEN] = {0}; - -/* public routines */ - -const char *NFD_GetError( void ) -{ - return g_errorstr; -} - -size_t NFD_PathSet_GetCount( const nfdpathset_t *pathset ) -{ - assert(pathset); - return pathset->count; -} - -nfdchar_t *NFD_PathSet_GetPath( const nfdpathset_t *pathset, size_t num ) -{ - assert(pathset); - assert(num < pathset->count); - - return pathset->buf + pathset->indices[num]; -} - -void NFD_PathSet_Free( nfdpathset_t *pathset ) -{ - assert(pathset); - NFDi_Free( pathset->indices ); - NFDi_Free( pathset->buf ); -} - -/* internal routines */ - -void *NFDi_Malloc( size_t bytes ) -{ - void *ptr = malloc(bytes); - if ( !ptr ) - NFDi_SetError("NFDi_Malloc failed."); - - return ptr; -} - -void NFDi_Free( void *ptr ) -{ - assert(ptr); - free(ptr); -} - -void NFDi_SetError( const char *msg ) -{ - int bTruncate = NFDi_SafeStrncpy( g_errorstr, msg, NFD_MAX_STRLEN ); - assert( !bTruncate ); _NFD_UNUSED(bTruncate); -} - - -int NFDi_SafeStrncpy( char *dst, const char *src, size_t maxCopy ) -{ - size_t n = maxCopy; - char *d = dst; - - assert( src ); - assert( dst ); - - while ( n > 0 && *src != '\0' ) - { - *d++ = *src++; - --n; - } - - /* Truncation case - - terminate string and return true */ - if ( n == 0 ) - { - dst[maxCopy-1] = '\0'; - return 1; - } - - /* No truncation. Append a single NULL and return. */ - *d = '\0'; - return 0; -} - - -/* adapted from microutf8 */ -int32_t NFDi_UTF8_Strlen( const nfdchar_t *str ) -{ - /* This function doesn't properly check validity of UTF-8 character - sequence, it is supposed to use only with valid UTF-8 strings. */ - - int32_t character_count = 0; - int32_t i = 0; /* Counter used to iterate over string. */ - nfdchar_t maybe_bom[4]; - - /* If there is UTF-8 BOM ignore it. */ - if (strlen(str) > 2) - { - strncpy(maybe_bom, str, 3); - maybe_bom[3] = 0; - if (strcmp(maybe_bom, (nfdchar_t*)NFD_UTF8_BOM) == 0) - i += 3; - } - - while(str[i]) - { - if (str[i] >> 7 == 0) - { - /* If bit pattern begins with 0 we have ascii character. */ - ++character_count; - } - else if (str[i] >> 6 == 3) - { - /* If bit pattern begins with 11 it is beginning of UTF-8 byte sequence. */ - ++character_count; - } - else if (str[i] >> 6 == 2) - ; /* If bit pattern begins with 10 it is middle of utf-8 byte sequence. */ - else - { - /* In any other case this is not valid UTF-8. */ - return -1; - } - ++i; - } - - return character_count; -} - -int NFDi_IsFilterSegmentChar( char ch ) -{ - return (ch==','||ch==';'||ch=='\0'); -} - diff --git a/ext/nativefiledialog/src/nfd_common.h b/ext/nativefiledialog/src/nfd_common.h deleted file mode 100644 index 1a9ab162..00000000 --- a/ext/nativefiledialog/src/nfd_common.h +++ /dev/null @@ -1,39 +0,0 @@ -/* - Native File Dialog - - Internal, common across platforms - - http://www.frogtoss.com/labs - */ - - -#ifndef _NFD_COMMON_H -#define _NFD_COMMON_H - -#include "nfd.h" - -#include - -#ifdef __cplusplus -extern "C" { -#endif - -#define NFD_MAX_STRLEN 256 -#define _NFD_UNUSED(x) ((void)x) - -#define NFD_UTF8_BOM "\xEF\xBB\xBF" - - -void *NFDi_Malloc( size_t bytes ); -void NFDi_Free( void *ptr ); -void NFDi_SetError( const char *msg ); -int NFDi_SafeStrncpy( char *dst, const char *src, size_t maxCopy ); -int32_t NFDi_UTF8_Strlen( const nfdchar_t *str ); -int NFDi_IsFilterSegmentChar( char ch ); - -#ifdef __cplusplus -} -#endif - - -#endif diff --git a/ext/nativefiledialog/src/nfd_gtk.c b/ext/nativefiledialog/src/nfd_gtk.c deleted file mode 100644 index 7a9958ed..00000000 --- a/ext/nativefiledialog/src/nfd_gtk.c +++ /dev/null @@ -1,379 +0,0 @@ -/* - Native File Dialog - - http://www.frogtoss.com/labs -*/ - -#include -#include -#include -#include -#include "nfd.h" -#include "nfd_common.h" - - -const char INIT_FAIL_MSG[] = "gtk_init_check failed to initilaize GTK+"; - - -static void AddTypeToFilterName( const char *typebuf, char *filterName, size_t bufsize ) -{ - const char SEP[] = ", "; - - size_t len = strlen(filterName); - if ( len != 0 ) - { - strncat( filterName, SEP, bufsize - len - 1 ); - len += strlen(SEP); - } - - strncat( filterName, typebuf, bufsize - len - 1 ); -} - -static void AddFiltersToDialog( GtkWidget *dialog, const char *filterList ) -{ - GtkFileFilter *filter; - char typebuf[NFD_MAX_STRLEN] = {0}; - const char *p_filterList = filterList; - char *p_typebuf = typebuf; - char filterName[NFD_MAX_STRLEN] = {0}; - - if ( !filterList || strlen(filterList) == 0 ) - return; - - filter = gtk_file_filter_new(); - while ( 1 ) - { - - if ( NFDi_IsFilterSegmentChar(*p_filterList) ) - { - char typebufWildcard[NFD_MAX_STRLEN]; - /* add another type to the filter */ - assert( strlen(typebuf) > 0 ); - assert( strlen(typebuf) < NFD_MAX_STRLEN-1 ); - - snprintf( typebufWildcard, NFD_MAX_STRLEN, "*.%s", typebuf ); - AddTypeToFilterName( typebuf, filterName, NFD_MAX_STRLEN ); - - gtk_file_filter_add_pattern( filter, typebufWildcard ); - - p_typebuf = typebuf; - memset( typebuf, 0, sizeof(char) * NFD_MAX_STRLEN ); - } - - if ( *p_filterList == ';' || *p_filterList == '\0' ) - { - /* end of filter -- add it to the dialog */ - - gtk_file_filter_set_name( filter, filterName ); - gtk_file_chooser_add_filter( GTK_FILE_CHOOSER(dialog), filter ); - - filterName[0] = '\0'; - - if ( *p_filterList == '\0' ) - break; - - filter = gtk_file_filter_new(); - } - - if ( !NFDi_IsFilterSegmentChar( *p_filterList ) ) - { - *p_typebuf = *p_filterList; - p_typebuf++; - } - - p_filterList++; - } - - /* always append a wildcard option to the end*/ - - filter = gtk_file_filter_new(); - gtk_file_filter_set_name( filter, "*.*" ); - gtk_file_filter_add_pattern( filter, "*" ); - gtk_file_chooser_add_filter( GTK_FILE_CHOOSER(dialog), filter ); -} - -static void SetDefaultPath( GtkWidget *dialog, const char *defaultPath ) -{ - if ( !defaultPath || strlen(defaultPath) == 0 ) - return; - - /* GTK+ manual recommends not specifically setting the default path. - We do it anyway in order to be consistent across platforms. - - If consistency with the native OS is preferred, this is the line - to comment out. -ml */ - gtk_file_chooser_set_current_folder( GTK_FILE_CHOOSER(dialog), defaultPath ); -} - -static nfdresult_t AllocPathSet( GSList *fileList, nfdpathset_t *pathSet ) -{ - size_t bufSize = 0; - GSList *node; - nfdchar_t *p_buf; - size_t count = 0; - - assert(fileList); - assert(pathSet); - - pathSet->count = (size_t)g_slist_length( fileList ); - assert( pathSet->count > 0 ); - - pathSet->indices = NFDi_Malloc( sizeof(size_t)*pathSet->count ); - if ( !pathSet->indices ) - { - return NFD_ERROR; - } - - /* count the total space needed for buf */ - for ( node = fileList; node; node = node->next ) - { - assert(node->data); - bufSize += strlen( (const gchar*)node->data ) + 1; - } - - pathSet->buf = NFDi_Malloc( sizeof(nfdchar_t) * bufSize ); - - /* fill buf */ - p_buf = pathSet->buf; - for ( node = fileList; node; node = node->next ) - { - nfdchar_t *path = (nfdchar_t*)(node->data); - size_t byteLen = strlen(path)+1; - ptrdiff_t index; - - memcpy( p_buf, path, byteLen ); - g_free(node->data); - - index = p_buf - pathSet->buf; - assert( index >= 0 ); - pathSet->indices[count] = (size_t)index; - - p_buf += byteLen; - ++count; - } - - g_slist_free( fileList ); - - return NFD_OKAY; -} - -static void WaitForCleanup(void) -{ - while (gtk_events_pending()) - gtk_main_iteration(); -} - -/* public */ - -nfdresult_t NFD_OpenDialog( const nfdchar_t *filterList, - const nfdchar_t *defaultPath, - nfdchar_t **outPath ) -{ - GtkWidget *dialog; - nfdresult_t result; - - if ( !gtk_init_check( NULL, NULL ) ) - { - NFDi_SetError(INIT_FAIL_MSG); - return NFD_ERROR; - } - - dialog = gtk_file_chooser_dialog_new( "Open File", - NULL, - GTK_FILE_CHOOSER_ACTION_OPEN, - "_Cancel", GTK_RESPONSE_CANCEL, - "_Open", GTK_RESPONSE_ACCEPT, - NULL ); - - /* Build the filter list */ - AddFiltersToDialog(dialog, filterList); - - /* Set the default path */ - SetDefaultPath(dialog, defaultPath); - - result = NFD_CANCEL; - if ( gtk_dialog_run( GTK_DIALOG(dialog) ) == GTK_RESPONSE_ACCEPT ) - { - char *filename; - - filename = gtk_file_chooser_get_filename( GTK_FILE_CHOOSER(dialog) ); - - { - size_t len = strlen(filename); - *outPath = NFDi_Malloc( len + 1 ); - memcpy( *outPath, filename, len + 1 ); - if ( !*outPath ) - { - g_free( filename ); - gtk_widget_destroy(dialog); - return NFD_ERROR; - } - } - g_free( filename ); - - result = NFD_OKAY; - } - - WaitForCleanup(); - gtk_widget_destroy(dialog); - WaitForCleanup(); - - return result; -} - - -nfdresult_t NFD_OpenDialogMultiple( const nfdchar_t *filterList, - const nfdchar_t *defaultPath, - nfdpathset_t *outPaths ) -{ - GtkWidget *dialog; - nfdresult_t result; - - if ( !gtk_init_check( NULL, NULL ) ) - { - NFDi_SetError(INIT_FAIL_MSG); - return NFD_ERROR; - } - - dialog = gtk_file_chooser_dialog_new( "Open Files", - NULL, - GTK_FILE_CHOOSER_ACTION_OPEN, - "_Cancel", GTK_RESPONSE_CANCEL, - "_Open", GTK_RESPONSE_ACCEPT, - NULL ); - gtk_file_chooser_set_select_multiple( GTK_FILE_CHOOSER(dialog), TRUE ); - - /* Build the filter list */ - AddFiltersToDialog(dialog, filterList); - - /* Set the default path */ - SetDefaultPath(dialog, defaultPath); - - result = NFD_CANCEL; - if ( gtk_dialog_run( GTK_DIALOG(dialog) ) == GTK_RESPONSE_ACCEPT ) - { - GSList *fileList = gtk_file_chooser_get_filenames( GTK_FILE_CHOOSER(dialog) ); - if ( AllocPathSet( fileList, outPaths ) == NFD_ERROR ) - { - gtk_widget_destroy(dialog); - return NFD_ERROR; - } - - result = NFD_OKAY; - } - - WaitForCleanup(); - gtk_widget_destroy(dialog); - WaitForCleanup(); - - return result; -} - -nfdresult_t NFD_SaveDialog( const nfdchar_t *filterList, - const nfdchar_t *defaultPath, - nfdchar_t **outPath ) -{ - GtkWidget *dialog; - nfdresult_t result; - - if ( !gtk_init_check( NULL, NULL ) ) - { - NFDi_SetError(INIT_FAIL_MSG); - return NFD_ERROR; - } - - dialog = gtk_file_chooser_dialog_new( "Save File", - NULL, - GTK_FILE_CHOOSER_ACTION_SAVE, - "_Cancel", GTK_RESPONSE_CANCEL, - "_Save", GTK_RESPONSE_ACCEPT, - NULL ); - gtk_file_chooser_set_do_overwrite_confirmation( GTK_FILE_CHOOSER(dialog), TRUE ); - - /* Build the filter list */ - AddFiltersToDialog(dialog, filterList); - - /* Set the default path */ - SetDefaultPath(dialog, defaultPath); - - result = NFD_CANCEL; - if ( gtk_dialog_run( GTK_DIALOG(dialog) ) == GTK_RESPONSE_ACCEPT ) - { - char *filename; - filename = gtk_file_chooser_get_filename( GTK_FILE_CHOOSER(dialog) ); - - { - size_t len = strlen(filename); - *outPath = NFDi_Malloc( len + 1 ); - memcpy( *outPath, filename, len + 1 ); - if ( !*outPath ) - { - g_free( filename ); - gtk_widget_destroy(dialog); - return NFD_ERROR; - } - } - g_free(filename); - - result = NFD_OKAY; - } - - WaitForCleanup(); - gtk_widget_destroy(dialog); - WaitForCleanup(); - - return result; -} - -nfdresult_t NFD_PickFolder(const nfdchar_t *defaultPath, - nfdchar_t **outPath) -{ - GtkWidget *dialog; - nfdresult_t result; - - if (!gtk_init_check(NULL, NULL)) - { - NFDi_SetError(INIT_FAIL_MSG); - return NFD_ERROR; - } - - dialog = gtk_file_chooser_dialog_new( "Select folder", - NULL, - GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER, - "_Cancel", GTK_RESPONSE_CANCEL, - "_Select", GTK_RESPONSE_ACCEPT, - NULL ); - gtk_file_chooser_set_do_overwrite_confirmation( GTK_FILE_CHOOSER(dialog), TRUE ); - - - /* Set the default path */ - SetDefaultPath(dialog, defaultPath); - - result = NFD_CANCEL; - if ( gtk_dialog_run( GTK_DIALOG(dialog) ) == GTK_RESPONSE_ACCEPT ) - { - char *filename; - filename = gtk_file_chooser_get_filename( GTK_FILE_CHOOSER(dialog) ); - - { - size_t len = strlen(filename); - *outPath = NFDi_Malloc( len + 1 ); - memcpy( *outPath, filename, len + 1 ); - if ( !*outPath ) - { - g_free( filename ); - gtk_widget_destroy(dialog); - return NFD_ERROR; - } - } - g_free(filename); - - result = NFD_OKAY; - } - - WaitForCleanup(); - gtk_widget_destroy(dialog); - WaitForCleanup(); - - return result; -} diff --git a/ext/nativefiledialog/src/nfd_win.cpp b/ext/nativefiledialog/src/nfd_win.cpp deleted file mode 100644 index 949da2b5..00000000 --- a/ext/nativefiledialog/src/nfd_win.cpp +++ /dev/null @@ -1,762 +0,0 @@ -/* - Native File Dialog - - http://www.frogtoss.com/labs - */ - - -#ifdef __MINGW32__ -// Explicitly setting NTDDI version, this is necessary for the MinGW compiler -#define NTDDI_VERSION NTDDI_VISTA -#define _WIN32_WINNT _WIN32_WINNT_VISTA -#endif - -#define _CRTDBG_MAP_ALLOC -#include -#include - -/* only locally define UNICODE in this compilation unit */ -#ifndef UNICODE -#define UNICODE -#endif - -#include -#include -#include -#include -#include -#include "nfd_common.h" - - -#define COM_INITFLAGS ::COINIT_APARTMENTTHREADED | ::COINIT_DISABLE_OLE1DDE - -static BOOL COMIsInitialized(HRESULT coResult) -{ - if (coResult == RPC_E_CHANGED_MODE) - { - // If COM was previously initialized with different init flags, - // NFD still needs to operate. Eat this warning. - return TRUE; - } - - return SUCCEEDED(coResult); -} - -static HRESULT COMInit(void) -{ - return ::CoInitializeEx(NULL, COM_INITFLAGS); -} - -static void COMUninit(HRESULT coResult) -{ - // do not uninitialize if RPC_E_CHANGED_MODE occurred -- this - // case does not refcount COM. - if (SUCCEEDED(coResult)) - ::CoUninitialize(); -} - -// allocs the space in outPath -- call free() -static void CopyWCharToNFDChar( const wchar_t *inStr, nfdchar_t **outStr ) -{ - int inStrCharacterCount = static_cast(wcslen(inStr)); - int bytesNeeded = WideCharToMultiByte( CP_UTF8, 0, - inStr, inStrCharacterCount, - NULL, 0, NULL, NULL ); - assert( bytesNeeded ); - bytesNeeded += 1; - - *outStr = (nfdchar_t*)NFDi_Malloc( bytesNeeded ); - if ( !*outStr ) - return; - - int bytesWritten = WideCharToMultiByte( CP_UTF8, 0, - inStr, -1, - *outStr, bytesNeeded, - NULL, NULL ); - assert( bytesWritten ); _NFD_UNUSED( bytesWritten ); -} - -/* includes NULL terminator byte in return */ -static size_t GetUTF8ByteCountForWChar( const wchar_t *str ) -{ - size_t bytesNeeded = WideCharToMultiByte( CP_UTF8, 0, - str, -1, - NULL, 0, NULL, NULL ); - assert( bytesNeeded ); - return bytesNeeded+1; -} - -// write to outPtr -- no free() necessary. -static int CopyWCharToExistingNFDCharBuffer( const wchar_t *inStr, nfdchar_t *outPtr ) -{ - int bytesNeeded = static_cast(GetUTF8ByteCountForWChar( inStr )); - - /* invocation copies null term */ - int bytesWritten = WideCharToMultiByte( CP_UTF8, 0, - inStr, -1, - outPtr, bytesNeeded, - NULL, 0 ); - assert( bytesWritten ); - - return bytesWritten; - -} - - -// allocs the space in outStr -- call free() -static void CopyNFDCharToWChar( const nfdchar_t *inStr, wchar_t **outStr ) -{ - int inStrByteCount = static_cast(strlen(inStr)); - int charsNeeded = MultiByteToWideChar(CP_UTF8, 0, - inStr, inStrByteCount, - NULL, 0 ); - assert( charsNeeded ); - assert( !*outStr ); - charsNeeded += 1; // terminator - - *outStr = (wchar_t*)NFDi_Malloc( charsNeeded * sizeof(wchar_t) ); - if ( !*outStr ) - return; - - int ret = MultiByteToWideChar(CP_UTF8, 0, - inStr, inStrByteCount, - *outStr, charsNeeded); - (*outStr)[charsNeeded-1] = '\0'; - -#ifdef _DEBUG - int inStrCharacterCount = static_cast(NFDi_UTF8_Strlen(inStr)); - assert( ret == inStrCharacterCount ); -#else - _NFD_UNUSED(ret); -#endif -} - - -/* ext is in format "jpg", no wildcards or separators */ -static int AppendExtensionToSpecBuf( const char *ext, char *specBuf, size_t specBufLen ) -{ - const char SEP[] = ";"; - assert( specBufLen > strlen(ext)+3 ); - - if ( strlen(specBuf) > 0 ) - { - strncat( specBuf, SEP, specBufLen - strlen(specBuf) - 1 ); - specBufLen += strlen(SEP); - } - - char extWildcard[NFD_MAX_STRLEN]; - int bytesWritten = sprintf_s( extWildcard, NFD_MAX_STRLEN, "*.%s", ext ); - assert( bytesWritten == (int)(strlen(ext)+2) ); - _NFD_UNUSED(bytesWritten); - - strncat( specBuf, extWildcard, specBufLen - strlen(specBuf) - 1 ); - - return NFD_OKAY; -} - -static nfdresult_t AddFiltersToDialog( ::IFileDialog *fileOpenDialog, const char *filterList ) -{ - const wchar_t WILDCARD[] = L"*.*"; - - if ( !filterList || strlen(filterList) == 0 ) - return NFD_OKAY; - - // Count rows to alloc - UINT filterCount = 1; /* guaranteed to have one filter on a correct, non-empty parse */ - const char *p_filterList; - for ( p_filterList = filterList; *p_filterList; ++p_filterList ) - { - if ( *p_filterList == ';' ) - ++filterCount; - } - - assert(filterCount); - if ( !filterCount ) - { - NFDi_SetError("Error parsing filters."); - return NFD_ERROR; - } - - /* filterCount plus 1 because we hardcode the *.* wildcard after the while loop */ - COMDLG_FILTERSPEC *specList = (COMDLG_FILTERSPEC*)NFDi_Malloc( sizeof(COMDLG_FILTERSPEC) * ((size_t)filterCount + 1) ); - if ( !specList ) - { - return NFD_ERROR; - } - for (UINT i = 0; i < filterCount+1; ++i ) - { - specList[i].pszName = NULL; - specList[i].pszSpec = NULL; - } - - size_t specIdx = 0; - p_filterList = filterList; - char typebuf[NFD_MAX_STRLEN] = {0}; /* one per comma or semicolon */ - char *p_typebuf = typebuf; - - char specbuf[NFD_MAX_STRLEN] = {0}; /* one per semicolon */ - - while ( 1 ) - { - if ( NFDi_IsFilterSegmentChar(*p_filterList) ) - { - /* append a type to the specbuf (pending filter) */ - AppendExtensionToSpecBuf( typebuf, specbuf, NFD_MAX_STRLEN ); - - p_typebuf = typebuf; - memset( typebuf, 0, sizeof(char)*NFD_MAX_STRLEN ); - } - - if ( *p_filterList == ';' || *p_filterList == '\0' ) - { - /* end of filter -- add it to specList */ - - CopyNFDCharToWChar( specbuf, (wchar_t**)&specList[specIdx].pszName ); - CopyNFDCharToWChar( specbuf, (wchar_t**)&specList[specIdx].pszSpec ); - - memset( specbuf, 0, sizeof(char)*NFD_MAX_STRLEN ); - ++specIdx; - if ( specIdx == filterCount ) - break; - } - - if ( !NFDi_IsFilterSegmentChar( *p_filterList )) - { - *p_typebuf = *p_filterList; - ++p_typebuf; - } - - ++p_filterList; - } - - /* Add wildcard */ - specList[specIdx].pszSpec = WILDCARD; - specList[specIdx].pszName = WILDCARD; - - fileOpenDialog->SetFileTypes( filterCount+1, specList ); - - /* free speclist */ - for ( size_t i = 0; i < filterCount; ++i ) - { - NFDi_Free( (void*)specList[i].pszSpec ); - } - NFDi_Free( specList ); - - return NFD_OKAY; -} - -static nfdresult_t AllocPathSet( IShellItemArray *shellItems, nfdpathset_t *pathSet ) -{ - const char ERRORMSG[] = "Error allocating pathset."; - - assert(shellItems); - assert(pathSet); - - // How many items in shellItems? - DWORD numShellItems; - HRESULT result = shellItems->GetCount(&numShellItems); - if ( !SUCCEEDED(result) ) - { - NFDi_SetError(ERRORMSG); - return NFD_ERROR; - } - - pathSet->count = static_cast(numShellItems); - assert( pathSet->count > 0 ); - - pathSet->indices = (size_t*)NFDi_Malloc( sizeof(size_t)*pathSet->count ); - if ( !pathSet->indices ) - { - return NFD_ERROR; - } - - /* count the total bytes needed for buf */ - size_t bufSize = 0; - for ( DWORD i = 0; i < numShellItems; ++i ) - { - ::IShellItem *shellItem; - result = shellItems->GetItemAt(i, &shellItem); - if ( !SUCCEEDED(result) ) - { - NFDi_SetError(ERRORMSG); - return NFD_ERROR; - } - - // Confirm SFGAO_FILESYSTEM is true for this shellitem, or ignore it. - SFGAOF attribs; - result = shellItem->GetAttributes( SFGAO_FILESYSTEM, &attribs ); - if ( !SUCCEEDED(result) ) - { - NFDi_SetError(ERRORMSG); - return NFD_ERROR; - } - if ( !(attribs & SFGAO_FILESYSTEM) ) - continue; - - LPWSTR name; - shellItem->GetDisplayName(SIGDN_FILESYSPATH, &name); - - // Calculate length of name with UTF-8 encoding - bufSize += GetUTF8ByteCountForWChar( name ); - - CoTaskMemFree(name); - } - - assert(bufSize); - - pathSet->buf = (nfdchar_t*)NFDi_Malloc( sizeof(nfdchar_t) * bufSize ); - memset( pathSet->buf, 0, sizeof(nfdchar_t) * bufSize ); - - /* fill buf */ - nfdchar_t *p_buf = pathSet->buf; - for (DWORD i = 0; i < numShellItems; ++i ) - { - ::IShellItem *shellItem; - result = shellItems->GetItemAt(i, &shellItem); - if ( !SUCCEEDED(result) ) - { - NFDi_SetError(ERRORMSG); - return NFD_ERROR; - } - - // Confirm SFGAO_FILESYSTEM is true for this shellitem, or ignore it. - SFGAOF attribs; - result = shellItem->GetAttributes( SFGAO_FILESYSTEM, &attribs ); - if ( !SUCCEEDED(result) ) - { - NFDi_SetError(ERRORMSG); - return NFD_ERROR; - } - if ( !(attribs & SFGAO_FILESYSTEM) ) - continue; - - LPWSTR name; - shellItem->GetDisplayName(SIGDN_FILESYSPATH, &name); - - int bytesWritten = CopyWCharToExistingNFDCharBuffer(name, p_buf); - CoTaskMemFree(name); - - ptrdiff_t index = p_buf - pathSet->buf; - assert( index >= 0 ); - pathSet->indices[i] = static_cast(index); - - p_buf += bytesWritten; - } - - return NFD_OKAY; -} - - -static nfdresult_t SetDefaultPath( IFileDialog *dialog, const char *defaultPath ) -{ - if ( !defaultPath || strlen(defaultPath) == 0 ) - return NFD_OKAY; - - wchar_t *defaultPathW = {0}; - CopyNFDCharToWChar( defaultPath, &defaultPathW ); - - IShellItem *folder; - HRESULT result = SHCreateItemFromParsingName( defaultPathW, NULL, IID_PPV_ARGS(&folder) ); - - // Valid non results. - if ( result == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND) || result == HRESULT_FROM_WIN32(ERROR_INVALID_DRIVE) ) - { - NFDi_Free( defaultPathW ); - return NFD_OKAY; - } - - if ( !SUCCEEDED(result) ) - { - NFDi_SetError("Error creating ShellItem"); - NFDi_Free( defaultPathW ); - return NFD_ERROR; - } - - // Could also call SetDefaultFolder(), but this guarantees defaultPath -- more consistency across API. - dialog->SetFolder( folder ); - - NFDi_Free( defaultPathW ); - folder->Release(); - - return NFD_OKAY; -} - -/* public */ - - -nfdresult_t NFD_OpenDialog( const nfdchar_t *filterList, - const nfdchar_t *defaultPath, - nfdchar_t **outPath ) -{ - nfdresult_t nfdResult = NFD_ERROR; - - - HRESULT coResult = COMInit(); - if (!COMIsInitialized(coResult)) - { - NFDi_SetError("Could not initialize COM."); - return nfdResult; - } - - // Create dialog - ::IFileOpenDialog *fileOpenDialog(NULL); - HRESULT result = ::CoCreateInstance(::CLSID_FileOpenDialog, NULL, - CLSCTX_ALL, ::IID_IFileOpenDialog, - reinterpret_cast(&fileOpenDialog) ); - - if ( !SUCCEEDED(result) ) - { - NFDi_SetError("Could not create dialog."); - goto end; - } - - // Build the filter list - if ( !AddFiltersToDialog( fileOpenDialog, filterList ) ) - { - goto end; - } - - // Set the default path - if ( !SetDefaultPath( fileOpenDialog, defaultPath ) ) - { - goto end; - } - - // Show the dialog. - result = fileOpenDialog->Show(NULL); - if ( SUCCEEDED(result) ) - { - // Get the file name - ::IShellItem *shellItem(NULL); - result = fileOpenDialog->GetResult(&shellItem); - if ( !SUCCEEDED(result) ) - { - NFDi_SetError("Could not get shell item from dialog."); - goto end; - } - wchar_t *filePath(NULL); - result = shellItem->GetDisplayName(::SIGDN_FILESYSPATH, &filePath); - if ( !SUCCEEDED(result) ) - { - NFDi_SetError("Could not get file path for selected."); - shellItem->Release(); - goto end; - } - - CopyWCharToNFDChar( filePath, outPath ); - CoTaskMemFree(filePath); - if ( !*outPath ) - { - /* error is malloc-based, error message would be redundant */ - shellItem->Release(); - goto end; - } - - nfdResult = NFD_OKAY; - shellItem->Release(); - } - else if (result == HRESULT_FROM_WIN32(ERROR_CANCELLED) ) - { - nfdResult = NFD_CANCEL; - } - else - { - NFDi_SetError("File dialog box show failed."); - nfdResult = NFD_ERROR; - } - -end: - if (fileOpenDialog) - fileOpenDialog->Release(); - - COMUninit(coResult); - - return nfdResult; -} - -nfdresult_t NFD_OpenDialogMultiple( const nfdchar_t *filterList, - const nfdchar_t *defaultPath, - nfdpathset_t *outPaths ) -{ - nfdresult_t nfdResult = NFD_ERROR; - - - HRESULT coResult = COMInit(); - if (!COMIsInitialized(coResult)) - { - NFDi_SetError("Could not initialize COM."); - return nfdResult; - } - - // Create dialog - ::IFileOpenDialog *fileOpenDialog(NULL); - HRESULT result = ::CoCreateInstance(::CLSID_FileOpenDialog, NULL, - CLSCTX_ALL, ::IID_IFileOpenDialog, - reinterpret_cast(&fileOpenDialog) ); - - if ( !SUCCEEDED(result) ) - { - fileOpenDialog = NULL; - NFDi_SetError("Could not create dialog."); - goto end; - } - - // Build the filter list - if ( !AddFiltersToDialog( fileOpenDialog, filterList ) ) - { - goto end; - } - - // Set the default path - if ( !SetDefaultPath( fileOpenDialog, defaultPath ) ) - { - goto end; - } - - // Set a flag for multiple options - DWORD dwFlags; - result = fileOpenDialog->GetOptions(&dwFlags); - if ( !SUCCEEDED(result) ) - { - NFDi_SetError("Could not get options."); - goto end; - } - result = fileOpenDialog->SetOptions(dwFlags | FOS_ALLOWMULTISELECT); - if ( !SUCCEEDED(result) ) - { - NFDi_SetError("Could not set options."); - goto end; - } - - // Show the dialog. - result = fileOpenDialog->Show(NULL); - if ( SUCCEEDED(result) ) - { - IShellItemArray *shellItems; - result = fileOpenDialog->GetResults( &shellItems ); - if ( !SUCCEEDED(result) ) - { - NFDi_SetError("Could not get shell items."); - goto end; - } - - if ( AllocPathSet( shellItems, outPaths ) == NFD_ERROR ) - { - shellItems->Release(); - goto end; - } - - shellItems->Release(); - nfdResult = NFD_OKAY; - } - else if (result == HRESULT_FROM_WIN32(ERROR_CANCELLED) ) - { - nfdResult = NFD_CANCEL; - } - else - { - NFDi_SetError("File dialog box show failed."); - nfdResult = NFD_ERROR; - } - -end: - if ( fileOpenDialog ) - fileOpenDialog->Release(); - - COMUninit(coResult); - - return nfdResult; -} - -nfdresult_t NFD_SaveDialog( const nfdchar_t *filterList, - const nfdchar_t *defaultPath, - nfdchar_t **outPath ) -{ - nfdresult_t nfdResult = NFD_ERROR; - - HRESULT coResult = COMInit(); - if (!COMIsInitialized(coResult)) - { - NFDi_SetError("Could not initialize COM."); - return nfdResult; - } - - // Create dialog - ::IFileSaveDialog *fileSaveDialog(NULL); - HRESULT result = ::CoCreateInstance(::CLSID_FileSaveDialog, NULL, - CLSCTX_ALL, ::IID_IFileSaveDialog, - reinterpret_cast(&fileSaveDialog) ); - - if ( !SUCCEEDED(result) ) - { - fileSaveDialog = NULL; - NFDi_SetError("Could not create dialog."); - goto end; - } - - // Build the filter list - if ( !AddFiltersToDialog( fileSaveDialog, filterList ) ) - { - goto end; - } - - // Set the default path - if ( !SetDefaultPath( fileSaveDialog, defaultPath ) ) - { - goto end; - } - - // Show the dialog. - result = fileSaveDialog->Show(NULL); - if ( SUCCEEDED(result) ) - { - // Get the file name - ::IShellItem *shellItem; - result = fileSaveDialog->GetResult(&shellItem); - if ( !SUCCEEDED(result) ) - { - NFDi_SetError("Could not get shell item from dialog."); - goto end; - } - wchar_t *filePath(NULL); - result = shellItem->GetDisplayName(::SIGDN_FILESYSPATH, &filePath); - if ( !SUCCEEDED(result) ) - { - shellItem->Release(); - NFDi_SetError("Could not get file path for selected."); - goto end; - } - - CopyWCharToNFDChar( filePath, outPath ); - CoTaskMemFree(filePath); - if ( !*outPath ) - { - /* error is malloc-based, error message would be redundant */ - shellItem->Release(); - goto end; - } - - nfdResult = NFD_OKAY; - shellItem->Release(); - } - else if (result == HRESULT_FROM_WIN32(ERROR_CANCELLED) ) - { - nfdResult = NFD_CANCEL; - } - else - { - NFDi_SetError("File dialog box show failed."); - nfdResult = NFD_ERROR; - } - -end: - if ( fileSaveDialog ) - fileSaveDialog->Release(); - - COMUninit(coResult); - - return nfdResult; -} - - - -nfdresult_t NFD_PickFolder(const nfdchar_t *defaultPath, - nfdchar_t **outPath) -{ - nfdresult_t nfdResult = NFD_ERROR; - DWORD dwOptions = 0; - - HRESULT coResult = COMInit(); - if (!COMIsInitialized(coResult)) - { - NFDi_SetError("CoInitializeEx failed."); - return nfdResult; - } - - // Create dialog - ::IFileOpenDialog *fileDialog(NULL); - HRESULT result = CoCreateInstance(CLSID_FileOpenDialog, - NULL, - CLSCTX_ALL, - IID_PPV_ARGS(&fileDialog)); - if ( !SUCCEEDED(result) ) - { - NFDi_SetError("CoCreateInstance for CLSID_FileOpenDialog failed."); - goto end; - } - - // Set the default path - if (SetDefaultPath(fileDialog, defaultPath) != NFD_OKAY) - { - NFDi_SetError("SetDefaultPath failed."); - goto end; - } - - // Get the dialogs options - if (!SUCCEEDED(fileDialog->GetOptions(&dwOptions))) - { - NFDi_SetError("GetOptions for IFileDialog failed."); - goto end; - } - - // Add in FOS_PICKFOLDERS which hides files and only allows selection of folders - if (!SUCCEEDED(fileDialog->SetOptions(dwOptions | FOS_PICKFOLDERS))) - { - NFDi_SetError("SetOptions for IFileDialog failed."); - goto end; - } - - // Show the dialog to the user - result = fileDialog->Show(NULL); - if ( SUCCEEDED(result) ) - { - // Get the folder name - ::IShellItem *shellItem(NULL); - - result = fileDialog->GetResult(&shellItem); - if ( !SUCCEEDED(result) ) - { - NFDi_SetError("Could not get file path for selected."); - shellItem->Release(); - goto end; - } - - wchar_t *path = NULL; - result = shellItem->GetDisplayName(SIGDN_DESKTOPABSOLUTEPARSING, &path); - if ( !SUCCEEDED(result) ) - { - NFDi_SetError("GetDisplayName for IShellItem failed."); - shellItem->Release(); - goto end; - } - - CopyWCharToNFDChar(path, outPath); - CoTaskMemFree(path); - if ( !*outPath ) - { - shellItem->Release(); - goto end; - } - - nfdResult = NFD_OKAY; - shellItem->Release(); - } - else if (result == HRESULT_FROM_WIN32(ERROR_CANCELLED) ) - { - nfdResult = NFD_CANCEL; - } - else - { - NFDi_SetError("Show for IFileDialog failed."); - nfdResult = NFD_ERROR; - } - - end: - - if (fileDialog) - fileDialog->Release(); - - COMUninit(coResult); - - return nfdResult; -} diff --git a/ext/nativefiledialog/src/nfd_zenity.c b/ext/nativefiledialog/src/nfd_zenity.c deleted file mode 100644 index 5f931337..00000000 --- a/ext/nativefiledialog/src/nfd_zenity.c +++ /dev/null @@ -1,307 +0,0 @@ -/* - Native File Dialog - - http://www.frogtoss.com/labs -*/ - -#include -#include -#include -#include "nfd.h" -#include "nfd_common.h" - -#define SIMPLE_EXEC_IMPLEMENTATION -#include "simple_exec.h" - - -const char NO_ZENITY_MSG[] = "zenity not installed"; - - -static void AddTypeToFilterName( const char *typebuf, char *filterName, size_t bufsize ) -{ - size_t len = strlen(filterName); - if( len > 0 ) - strncat( filterName, " *.", bufsize - len - 1 ); - else - strncat( filterName, "--file-filter=*.", bufsize - len - 1 ); - - len = strlen(filterName); - strncat( filterName, typebuf, bufsize - len - 1 ); -} - -static void AddFiltersToCommandArgs(char** commandArgs, int commandArgsLen, const char *filterList ) -{ - char typebuf[NFD_MAX_STRLEN] = {0}; - const char *p_filterList = filterList; - char *p_typebuf = typebuf; - char filterName[NFD_MAX_STRLEN] = {0}; - int i; - - if ( !filterList || strlen(filterList) == 0 ) - return; - - while ( 1 ) - { - - if ( NFDi_IsFilterSegmentChar(*p_filterList) ) - { - char typebufWildcard[NFD_MAX_STRLEN]; - /* add another type to the filter */ - assert( strlen(typebuf) > 0 ); - assert( strlen(typebuf) < NFD_MAX_STRLEN-1 ); - - snprintf( typebufWildcard, NFD_MAX_STRLEN, "*.%s", typebuf ); - - AddTypeToFilterName( typebuf, filterName, NFD_MAX_STRLEN ); - - p_typebuf = typebuf; - memset( typebuf, 0, sizeof(char) * NFD_MAX_STRLEN ); - } - - if ( *p_filterList == ';' || *p_filterList == '\0' ) - { - /* end of filter -- add it to the dialog */ - - for(i = 0; commandArgs[i] != NULL && i < commandArgsLen; i++); - - commandArgs[i] = strdup(filterName); - - filterName[0] = '\0'; - - if ( *p_filterList == '\0' ) - break; - } - - if ( !NFDi_IsFilterSegmentChar( *p_filterList ) ) - { - *p_typebuf = *p_filterList; - p_typebuf++; - } - - p_filterList++; - } - - /* always append a wildcard option to the end*/ - - for(i = 0; commandArgs[i] != NULL && i < commandArgsLen; i++); - - commandArgs[i] = strdup("--file-filter=*.*"); -} - -static nfdresult_t ZenityCommon(char** command, int commandLen, const char* defaultPath, const char* filterList, char** stdOut) -{ - if(defaultPath != NULL) - { - char* prefix = "--filename="; - int len = strlen(prefix) + strlen(defaultPath) + 1; - - char* tmp = (char*) calloc(len, 1); - strcat(tmp, prefix); - strcat(tmp, defaultPath); - - int i; - for(i = 0; command[i] != NULL && i < commandLen; i++); - - command[i] = tmp; - } - - AddFiltersToCommandArgs(command, commandLen, filterList); - - int byteCount = 0; - int exitCode = 0; - int processInvokeError = runCommandArray(stdOut, &byteCount, &exitCode, 0, command); - - for(int i = 0; command[i] != NULL && i < commandLen; i++) - free(command[i]); - - nfdresult_t result = NFD_OKAY; - - if(processInvokeError == COMMAND_NOT_FOUND) - { - NFDi_SetError(NO_ZENITY_MSG); - result = NFD_ERROR; - } - else - { - if(exitCode == 1) - result = NFD_CANCEL; - } - - return result; -} - - -static nfdresult_t AllocPathSet(char* zenityList, nfdpathset_t *pathSet ) -{ - assert(zenityList); - assert(pathSet); - - size_t len = strlen(zenityList) + 1; - pathSet->buf = NFDi_Malloc(len); - - int numEntries = 1; - - for(size_t i = 0; i < len; i++) - { - char ch = zenityList[i]; - - if(ch == '|') - { - numEntries++; - ch = '\0'; - } - - pathSet->buf[i] = ch; - } - - pathSet->count = numEntries; - assert( pathSet->count > 0 ); - - pathSet->indices = NFDi_Malloc( sizeof(size_t)*pathSet->count ); - - int entry = 0; - pathSet->indices[0] = 0; - for(size_t i = 0; i < len; i++) - { - char ch = zenityList[i]; - - if(ch == '|') - { - entry++; - pathSet->indices[entry] = i + 1; - } - } - - return NFD_OKAY; -} - -/* public */ - -nfdresult_t NFD_OpenDialog( const char *filterList, - const nfdchar_t *defaultPath, - nfdchar_t **outPath ) -{ - int commandLen = 100; - char* command[commandLen]; - memset(command, 0, commandLen * sizeof(char*)); - - command[0] = strdup("zenity"); - command[1] = strdup("--file-selection"); - command[2] = strdup("--title=Open File"); - - char* stdOut = NULL; - nfdresult_t result = ZenityCommon(command, commandLen, defaultPath, filterList, &stdOut); - - if(stdOut != NULL) - { - size_t len = strlen(stdOut); - *outPath = NFDi_Malloc(len); - memcpy(*outPath, stdOut, len); - (*outPath)[len-1] = '\0'; // trim out the final \n with a null terminator - free(stdOut); - } - else - { - *outPath = NULL; - } - - return result; -} - - -nfdresult_t NFD_OpenDialogMultiple( const nfdchar_t *filterList, - const nfdchar_t *defaultPath, - nfdpathset_t *outPaths ) -{ - int commandLen = 100; - char* command[commandLen]; - memset(command, 0, commandLen * sizeof(char*)); - - command[0] = strdup("zenity"); - command[1] = strdup("--file-selection"); - command[2] = strdup("--title=Open Files"); - command[3] = strdup("--multiple"); - - char* stdOut = NULL; - nfdresult_t result = ZenityCommon(command, commandLen, defaultPath, filterList, &stdOut); - - if(stdOut != NULL) - { - size_t len = strlen(stdOut); - stdOut[len-1] = '\0'; // remove trailing newline - - if ( AllocPathSet( stdOut, outPaths ) == NFD_ERROR ) - result = NFD_ERROR; - - free(stdOut); - } - else - { - result = NFD_ERROR; - } - - return result; -} - -nfdresult_t NFD_SaveDialog( const nfdchar_t *filterList, - const nfdchar_t *defaultPath, - nfdchar_t **outPath ) -{ - int commandLen = 100; - char* command[commandLen]; - memset(command, 0, commandLen * sizeof(char*)); - - command[0] = strdup("zenity"); - command[1] = strdup("--file-selection"); - command[2] = strdup("--title=Save File"); - command[3] = strdup("--save"); - - char* stdOut = NULL; - nfdresult_t result = ZenityCommon(command, commandLen, defaultPath, filterList, &stdOut); - - if(stdOut != NULL) - { - size_t len = strlen(stdOut); - *outPath = NFDi_Malloc(len); - memcpy(*outPath, stdOut, len); - (*outPath)[len-1] = '\0'; // trim out the final \n with a null terminator - free(stdOut); - } - else - { - *outPath = NULL; - } - - return result; -} - -nfdresult_t NFD_PickFolder(const nfdchar_t *defaultPath, - nfdchar_t **outPath) -{ - int commandLen = 100; - char* command[commandLen]; - memset(command, 0, commandLen * sizeof(char*)); - - command[0] = strdup("zenity"); - command[1] = strdup("--file-selection"); - command[2] = strdup("--directory"); - command[3] = strdup("--title=Select folder"); - - char* stdOut = NULL; - nfdresult_t result = ZenityCommon(command, commandLen, defaultPath, "", &stdOut); - - if(stdOut != NULL) - { - size_t len = strlen(stdOut); - *outPath = NFDi_Malloc(len); - memcpy(*outPath, stdOut, len); - (*outPath)[len-1] = '\0'; // trim out the final \n with a null terminator - free(stdOut); - } - else - { - *outPath = NULL; - } - - return result; -} diff --git a/ext/nativefiledialog/src/simple_exec.h b/ext/nativefiledialog/src/simple_exec.h deleted file mode 100644 index 6308dfe0..00000000 --- a/ext/nativefiledialog/src/simple_exec.h +++ /dev/null @@ -1,218 +0,0 @@ -// copied from: https://github.com/wheybags/simple_exec/blob/5a74c507c4ce1b2bb166177ead4cca7cfa23cb35/simple_exec.h - -// simple_exec.h, single header library to run external programs + retrieve their status code and output (unix only for now) -// -// do this: -// #define SIMPLE_EXEC_IMPLEMENTATION -// before you include this file in *one* C or C++ file to create the implementation. -// i.e. it should look like this: -// #define SIMPLE_EXEC_IMPLEMENTATION -// #include "simple_exec.h" - -#ifndef SIMPLE_EXEC_H -#define SIMPLE_EXEC_H - -int runCommand(char** stdOut, int* stdOutByteCount, int* returnCode, int includeStdErr, char* command, ...); -int runCommandArray(char** stdOut, int* stdOutByteCount, int* returnCode, int includeStdErr, char* const* allArgs); - -#endif // SIMPLE_EXEC_H - -#ifdef SIMPLE_EXEC_IMPLEMENTATION - -#include -#include -#include -#include -#include -#include -#include -#include - -#define release_assert(exp) { if (!(exp)) { abort(); } } - -enum PIPE_FILE_DESCRIPTORS -{ - READ_FD = 0, - WRITE_FD = 1 -}; - -enum RUN_COMMAND_ERROR -{ - COMMAND_RAN_OK = 0, - COMMAND_NOT_FOUND = 1 -}; - -int runCommandArray(char** stdOut, int* stdOutByteCount, int* returnCode, int includeStdErr, char* const* allArgs) -{ - // adapted from: https://stackoverflow.com/a/479103 - - int bufferSize = 256; - char buffer[bufferSize + 1]; - - int dataReadFromChildDefaultSize = bufferSize * 5; - int dataReadFromChildSize = dataReadFromChildDefaultSize; - int dataReadFromChildUsed = 0; - char* dataReadFromChild = (char*)malloc(dataReadFromChildSize); - - - int parentToChild[2]; - release_assert(pipe(parentToChild) == 0); - - int childToParent[2]; - release_assert(pipe(childToParent) == 0); - - int errPipe[2]; - release_assert(pipe(errPipe) == 0); - - pid_t pid; - switch( pid = fork() ) - { - case -1: - { - release_assert(0 && "Fork failed"); - break; - } - - case 0: // child - { - release_assert(dup2(parentToChild[READ_FD ], STDIN_FILENO ) != -1); - release_assert(dup2(childToParent[WRITE_FD], STDOUT_FILENO) != -1); - - if(includeStdErr) - { - release_assert(dup2(childToParent[WRITE_FD], STDERR_FILENO) != -1); - } - else - { - int devNull = open("/dev/null", O_WRONLY); - release_assert(dup2(devNull, STDERR_FILENO) != -1); - } - - // unused - release_assert(close(parentToChild[WRITE_FD]) == 0); - release_assert(close(childToParent[READ_FD ]) == 0); - release_assert(close(errPipe[READ_FD]) == 0); - - const char* command = allArgs[0]; - execvp(command, allArgs); - - char err = 1; - ssize_t result = write(errPipe[WRITE_FD], &err, 1); - release_assert(result != -1); - - close(errPipe[WRITE_FD]); - close(parentToChild[READ_FD]); - close(childToParent[WRITE_FD]); - - exit(0); - } - - - default: // parent - { - // unused - release_assert(close(parentToChild[READ_FD]) == 0); - release_assert(close(childToParent[WRITE_FD]) == 0); - release_assert(close(errPipe[WRITE_FD]) == 0); - - while(1) - { - ssize_t bytesRead = 0; - switch(bytesRead = read(childToParent[READ_FD], buffer, bufferSize)) - { - case 0: // End-of-File, or non-blocking read. - { - int status = 0; - release_assert(waitpid(pid, &status, 0) == pid); - - // done with these now - release_assert(close(parentToChild[WRITE_FD]) == 0); - release_assert(close(childToParent[READ_FD]) == 0); - - char errChar = 0; - ssize_t result = read(errPipe[READ_FD], &errChar, 1); - release_assert(result != -1); - close(errPipe[READ_FD]); - - if(errChar) - { - free(dataReadFromChild); - return COMMAND_NOT_FOUND; - } - - // free any un-needed memory with realloc + add a null terminator for convenience - dataReadFromChild = (char*)realloc(dataReadFromChild, dataReadFromChildUsed + 1); - dataReadFromChild[dataReadFromChildUsed] = '\0'; - - if(stdOut != NULL) - *stdOut = dataReadFromChild; - else - free(dataReadFromChild); - - if(stdOutByteCount != NULL) - *stdOutByteCount = dataReadFromChildUsed; - if(returnCode != NULL) - *returnCode = WEXITSTATUS(status); - - return COMMAND_RAN_OK; - } - case -1: - { - release_assert(0 && "read() failed"); - break; - } - - default: - { - if(dataReadFromChildUsed + bytesRead + 1 >= dataReadFromChildSize) - { - dataReadFromChildSize += dataReadFromChildDefaultSize; - dataReadFromChild = (char*)realloc(dataReadFromChild, dataReadFromChildSize); - } - - memcpy(dataReadFromChild + dataReadFromChildUsed, buffer, bytesRead); - dataReadFromChildUsed += bytesRead; - break; - } - } - } - } - } -} - -int runCommand(char** stdOut, int* stdOutByteCount, int* returnCode, int includeStdErr, char* command, ...) -{ - va_list vl; - va_start(vl, command); - - char* currArg = NULL; - - int allArgsInitialSize = 16; - int allArgsSize = allArgsInitialSize; - char** allArgs = (char**)malloc(sizeof(char*) * allArgsSize); - allArgs[0] = command; - - int i = 1; - do - { - currArg = va_arg(vl, char*); - allArgs[i] = currArg; - - i++; - - if(i >= allArgsSize) - { - allArgsSize += allArgsInitialSize; - allArgs = (char**)realloc(allArgs, sizeof(char*) * allArgsSize); - } - - } while(currArg != NULL); - - va_end(vl); - - int retval = runCommandArray(stdOut, stdOutByteCount, returnCode, includeStdErr, allArgs); - free(allArgs); - return retval; -} - -#endif //SIMPLE_EXEC_IMPLEMENTATION diff --git a/src/app/application.cpp b/src/app/application.cpp index 136e3518..2e2565f0 100644 --- a/src/app/application.cpp +++ b/src/app/application.cpp @@ -11,18 +11,16 @@ #include #include -#include +#include #if MD_PLATFORM_WINDOWS -#define GLFW_EXPOSE_NATIVE_WIN32 -#include +#include #endif -#include #include #include -#include +#include #include // Compressed fonts @@ -60,40 +58,53 @@ static void APIENTRY gl_callback(GLenum source, GLenum type, GLuint id, GLenum s } bool initialize(Context* ctx, size_t width, size_t height, str_t title) { - if (!glfwInit()) { - // TODO Throw critical error - MD_LOG_ERROR("Error while initializing glfw."); + if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_GAMEPAD)) { + MD_LOG_ERROR("Error while initializing SDL: %s", SDL_GetError()); return false; } - glfwSetErrorCallback(error_callback); if (width == 0 && height == 0) { - int count; - GLFWmonitor** monitors = glfwGetMonitors(&count); - if (count > 0) { - int pos_x, pos_y, dim_x, dim_y; - glfwGetMonitorWorkarea(monitors[0], &pos_x, &pos_y, &dim_x, &dim_y); - width = (size_t)(dim_x * 0.9); - height = (size_t)(dim_y * 0.8); + int display_count = 0; + SDL_DisplayID* displays = SDL_GetDisplays(&display_count); + if (display_count > 0 && displays) { + SDL_Rect usable_bounds; + if (SDL_GetDisplayUsableBounds(displays[0], &usable_bounds)) { + width = (size_t)(usable_bounds.w * 0.9); + height = (size_t)(usable_bounds.h * 0.8); + } + SDL_free(displays); } } #if MD_PLATFORM_OSX - glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); - glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1); - glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); - glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); + SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4); + SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1); + SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); + SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_FORWARD_COMPATIBLE_FLAG); #endif + + SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); + SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24); + SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8); + // Zero terminated str_t ztitle = str_copy(title, md_get_temp_allocator()); - GLFWwindow* window = glfwCreateWindow((int)width, (int)height, ztitle.ptr, NULL, NULL); + SDL_Window* window = SDL_CreateWindow(ztitle.ptr, (int)width, (int)height, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_HIGH_PIXEL_DENSITY); if (!window) { - MD_LOG_ERROR("Could not create glfw window."); + MD_LOG_ERROR("Could not create SDL window: %s", SDL_GetError()); return false; } - glfwMakeContextCurrent(window); - glfwSwapInterval(1); + SDL_GLContext gl_context = SDL_GL_CreateContext(window); + if (!gl_context) { + MD_LOG_ERROR("Could not create OpenGL context: %s", SDL_GetError()); + SDL_DestroyWindow(window); + return false; + } + + SDL_GL_MakeCurrent(window, gl_context); + SDL_GL_SetSwapInterval(1); + if (gl3wInit() != GL3W_OK) { MD_LOG_ERROR("Could not load gl functions."); return false; @@ -106,7 +117,10 @@ bool initialize(Context* ctx, size_t width, size_t height, str_t title) { glDebugMessageControl(GL_DONT_CARE, GL_DONT_CARE, GL_DONT_CARE, 0, NULL, true); } - glfwGetVersion(&data.internal_ctx.gl_info.version.major, &data.internal_ctx.gl_info.version.minor, &data.internal_ctx.gl_info.version.revision); + int version = SDL_GetVersion(); + data.internal_ctx.gl_info.version.major = SDL_VERSIONNUM_MAJOR(version); + data.internal_ctx.gl_info.version.minor = SDL_VERSIONNUM_MINOR(version); + data.internal_ctx.gl_info.version.revision = SDL_VERSIONNUM_MICRO(version); IMGUI_CHECKVERSION(); ImGui::CreateContext(); @@ -159,10 +173,9 @@ bool initialize(Context* ctx, size_t width, size_t height, str_t title) { ImGui::GetIO().Fonts->AddFontFromMemoryCompressedTTF((void*)fa_solid_compressed_data, fa_solid_compressed_size, size * scl, &config, ranges_icons); } - io.Fonts->Build(); io.FontDefault = io.Fonts->Fonts[4]; // Set default to 18px - if (!ImGui_ImplGlfw_InitForOpenGL(window, false) || + if (!ImGui_ImplSDL3_InitForOpenGL(window, gl_context) || !ImGui_ImplOpenGL3_Init("#version 150")) { MD_LOG_ERROR("Failed to initialize ImGui OpenGL"); @@ -176,43 +189,21 @@ bool initialize(Context* ctx, size_t width, size_t height, str_t title) { data.internal_ctx.window.vsync = true; int w, h; - glfwGetFramebufferSize(window, &w, &h); + SDL_GetWindowSizeInPixels(window, &w, &h); data.internal_ctx.framebuffer.width = w; data.internal_ctx.framebuffer.height = h; - glfwSetMouseButtonCallback(window, ImGui_ImplGlfw_MouseButtonCallback); - glfwSetScrollCallback(window, ImGui_ImplGlfw_ScrollCallback); - glfwSetKeyCallback(window, ImGui_ImplGlfw_KeyCallback); - glfwSetCharCallback(window, ImGui_ImplGlfw_CharCallback); - - glfwSetWindowUserPointer(window, &data.internal_ctx); - - GLFWdropfun drop_cb = [](GLFWwindow* window, int num_files, const char** paths) { - Context* ctx = (Context*)glfwGetWindowUserPointer(window); - ASSERT(ctx); - - for (int i = 0; i < num_files; ++i) { - MD_LOG_DEBUG("User dropped file: '%s'", paths[i]); - } - - str_t* str_paths = (str_t*)md_temp_push(num_files * sizeof(str_t)); - for (int i = 0; i < num_files; ++i) { - str_paths[i] = {paths[i], strlen(paths[i])}; - } - - if (ctx->file_drop.callback) { - ctx->file_drop.callback((size_t)num_files, str_paths, ctx->file_drop.user_data); - } - }; - glfwSetDropCallback(window, drop_cb); + SDL_SetPointerProperty(SDL_GetWindowProperties(window), "context", &data.internal_ctx); #if MD_PLATFORM_WINDOWS - HWND hwnd = glfwGetWin32Window(window); - HINSTANCE hinst = GetModuleHandle(NULL); - - HICON hIcon = LoadIcon(hinst, "VIAMD_ICON"); - SendMessage(hwnd, WM_SETICON, ICON_SMALL, (LPARAM)hIcon); - SendMessage(hwnd, WM_SETICON, ICON_BIG, (LPARAM)hIcon); + SDL_PropertiesID props = SDL_GetWindowProperties(window); + HWND hwnd = (HWND)SDL_GetPointerProperty(props, SDL_PROP_WINDOW_WIN32_HWND_POINTER, NULL); + if (hwnd) { + HINSTANCE hinst = GetModuleHandle(NULL); + HICON hIcon = LoadIcon(hinst, "VIAMD_ICON"); + SendMessage(hwnd, WM_SETICON, ICON_SMALL, (LPARAM)hIcon); + SendMessage(hwnd, WM_SETICON, ICON_BIG, (LPARAM)hIcon); + } #endif MEMCPY(ctx, &data.internal_ctx, sizeof(Context)); @@ -221,40 +212,69 @@ bool initialize(Context* ctx, size_t width, size_t height, str_t title) { } void shutdown(Context* ctx) { - glfwDestroyWindow((GLFWwindow*)data.internal_ctx.window.ptr); + SDL_Window* window = (SDL_Window*)data.internal_ctx.window.ptr; + SDL_GLContext gl_context = SDL_GL_GetCurrentContext(); + ImGui_ImplOpenGL3_Shutdown(); - ImGui_ImplGlfw_Shutdown(); + ImGui_ImplSDL3_Shutdown(); ImPlot::DestroyContext(); ImGui::DestroyContext(); - glfwTerminate(); + + if (gl_context) { + SDL_GL_DestroyContext(gl_context); + } + if (window) { + SDL_DestroyWindow(window); + } + SDL_Quit(); MEMSET(ctx, 0, sizeof(Context)); } void update(Context* ctx) { - glfwPollEvents(); + SDL_Window* window = (SDL_Window*)data.internal_ctx.window.ptr; + + // Process SDL events + SDL_Event event; + while (SDL_PollEvent(&event)) { + ImGui_ImplSDL3_ProcessEvent(&event); + + if (event.type == SDL_EVENT_QUIT) { + data.internal_ctx.window.should_close = true; + } + + if (event.type == SDL_EVENT_DROP_FILE) { + Context* ctx = (Context*)SDL_GetPointerProperty(SDL_GetWindowProperties(window), "context", NULL); + if (ctx && ctx->file_drop.callback && event.drop.data) { + MD_LOG_DEBUG("User dropped file: '%s'", event.drop.data); + str_t path = {event.drop.data, strlen(event.drop.data)}; + ctx->file_drop.callback(1, &path, ctx->file_drop.user_data); + } + } + } ImGui_ImplOpenGL3_NewFrame(); - ImGui_ImplGlfw_NewFrame(); + ImGui_ImplSDL3_NewFrame(); ImGui::NewFrame(); if (ctx->window.width != data.internal_ctx.window.width || ctx->window.height != data.internal_ctx.window.height) { - glfwSetWindowSize((GLFWwindow*)ctx->window.ptr, ctx->window.width, ctx->window.height); + SDL_SetWindowSize(window, ctx->window.width, ctx->window.height); data.internal_ctx.window.width = ctx->window.width; data.internal_ctx.window.height = ctx->window.height; } + int w, h; - glfwGetFramebufferSize((GLFWwindow*)data.internal_ctx.window.ptr, &w, &h); + SDL_GetWindowSizeInPixels(window, &w, &h); data.internal_ctx.framebuffer.width = w; data.internal_ctx.framebuffer.height = h; - glfwGetWindowSize((GLFWwindow*)data.internal_ctx.window.ptr, &w, &h); + SDL_GetWindowSize(window, &w, &h); data.internal_ctx.window.width = w; data.internal_ctx.window.height = h; if (ctx->window.vsync != data.internal_ctx.window.vsync) { data.internal_ctx.window.vsync = ctx->window.vsync; - glfwSwapInterval((int)ctx->window.vsync); + SDL_GL_SetSwapInterval((int)ctx->window.vsync); } if (ctx->file_drop.callback != data.internal_ctx.file_drop.callback) { @@ -265,21 +285,21 @@ void update(Context* ctx) { data.internal_ctx.file_drop.user_data = ctx->file_drop.user_data; } - data.internal_ctx.window.should_close = (bool)glfwWindowShouldClose((GLFWwindow*)data.internal_ctx.window.ptr); - - double t = glfwGetTime(); - data.internal_ctx.timing.delta_s = (t - data.internal_ctx.timing.total_s); - data.internal_ctx.timing.total_s = t; + uint64_t t = SDL_GetTicks(); + double t_s = t / 1000.0; + data.internal_ctx.timing.delta_s = (t_s - data.internal_ctx.timing.total_s); + data.internal_ctx.timing.total_s = t_s; MEMCPY(ctx, &data.internal_ctx, sizeof(Context)); } void render_imgui(Context* ctx) { (void)ctx; - GLFWwindow* window = (GLFWwindow*)data.internal_ctx.window.ptr; + SDL_Window* window = (SDL_Window*)data.internal_ctx.window.ptr; + SDL_GLContext gl_context = SDL_GL_GetCurrentContext(); ImGui::Render(); - glfwMakeContextCurrent(window); + SDL_GL_MakeCurrent(window, gl_context); ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); // Update and Render additional Platform Windows @@ -288,36 +308,109 @@ void render_imgui(Context* ctx) { ImGui::RenderPlatformWindowsDefault(); } - glfwMakeContextCurrent(window); + SDL_GL_MakeCurrent(window, gl_context); } -void swap_buffers(Context* ctx) { glfwSwapBuffers((GLFWwindow*)ctx->window.ptr); } - -bool file_dialog(char* str_buf, size_t str_cap, FileDialogFlag flags, str_t filter) { - nfdchar_t* out_path = NULL; - defer { if (out_path) free(out_path); }; - - nfdresult_t result = NFD_ERROR; +void swap_buffers(Context* ctx) { + SDL_GL_SwapWindow((SDL_Window*)ctx->window.ptr); +} - const char* default_path = 0; +bool file_dialog(char* str_buf, size_t str_cap, FileDialogFlag flags, str_t filter) { + struct DialogState { + const char* result_path; + bool completed; + }; + + // Use local state instead of static to avoid race conditions + DialogState dialog_state = {NULL, false}; + + SDL_DialogFileFilter* sdl_filters = NULL; + int filter_count = 0; + + // Parse filter string (e.g., "jpg,png,bmp") + if (filter.ptr && filter.len > 0) { + // Count comma-separated items + filter_count = 1; + for (size_t i = 0; i < filter.len; i++) { + if (filter.ptr[i] == ',') filter_count++; + } + + sdl_filters = (SDL_DialogFileFilter*)md_temp_push(filter_count * sizeof(SDL_DialogFileFilter)); + + // Parse extensions + const char* start = filter.ptr; + int idx = 0; + for (size_t i = 0; i <= filter.len; i++) { + if (i == filter.len || filter.ptr[i] == ',') { + size_t ext_len = (filter.ptr + i) - start; + // Validate ext_len and allocate sufficient space + if (ext_len > 0 && ext_len < 256) { // reasonable max ext length + char* ext = (char*)md_temp_push(ext_len + 4); // "*.ext\0" (need 4: "*."+len+"\0") + snprintf(ext, ext_len + 4, "*.%.*s", (int)ext_len, start); + + sdl_filters[idx].name = ext; + sdl_filters[idx].pattern = ext; + idx++; + } + start = filter.ptr + i + 1; + } + } + // Update filter_count in case some were skipped + filter_count = idx; + } - // Zero terminated variant - str_t zfilt = str_copy(filter, md_get_temp_allocator()); + // Use a static function instead of lambda for C compatibility + static auto dialog_callback_impl = [](void* userdata, const char* const* filelist, int filter_idx) { + (void)filter_idx; + DialogState* state = (DialogState*)userdata; + if (filelist && filelist[0]) { + state->result_path = filelist[0]; + } + state->completed = true; + }; if (flags & FileDialogFlag_Open) { - result = NFD_OpenDialog(zfilt.ptr, default_path, &out_path); + SDL_ShowOpenFileDialog( + dialog_callback_impl, + &dialog_state, + NULL, // parent window + sdl_filters, + filter_count, + NULL, // default location + false // allow_many + ); } else if (flags & FileDialogFlag_Save) { - result = NFD_SaveDialog(zfilt.ptr, default_path, &out_path); + SDL_ShowSaveFileDialog( + dialog_callback_impl, + &dialog_state, + NULL, // parent window + sdl_filters, + filter_count, + NULL // default location + ); + } else { + return false; } - - if (result == NFD_OKAY) { - int len = snprintf(str_buf, str_cap, "%s", out_path); + + // Wait for dialog to complete (blocking event loop) + // Note: SDL3 dialog API is async, so we need to pump events + while (!dialog_state.completed) { + SDL_Event event; + if (SDL_WaitEventTimeout(&event, 10)) { + if (event.type == SDL_EVENT_QUIT) { + return false; + } + } + } + + if (dialog_state.result_path && dialog_state.result_path[0] != '\0') { + int len = snprintf(str_buf, str_cap, "%s", dialog_state.result_path); + if (flags & FileDialogFlag_Save) { - // If the user is saving through the dialogue and there is no extension - // In such case we append the first extension found in the filter (if supplied) + // If the user is saving and there is no extension, append the first filter extension str_t ext; - str_t path = str_from_cstr(out_path); - if (!extract_ext(&ext, path) && filter) { + str_t path = str_from_cstr(dialog_state.result_path); + if (!extract_ext(&ext, path) && filter.ptr && filter.len > 0) { // get ext from supplied filter (first match) ext = filter; str_find_char(&ext.len, ext, ','); @@ -325,17 +418,15 @@ bool file_dialog(char* str_buf, size_t str_cap, FileDialogFlag flags, str_t filt } } - if (0 < len && len < str_cap) { + if (0 < len && (size_t)len < str_cap) { replace_char(str_buf, len, '\\', '/'); return true; } - - MD_LOG_ERROR("snprintf failed"); + + MD_LOG_ERROR("snprintf failed or buffer too small"); return false; - } else if (result == NFD_ERROR) { - MD_LOG_ERROR("%s\n", NFD_GetError()); } - /* fallthrough for NFD_CANCEL */ + return false; } diff --git a/src/app/imgui_impl_glfw.cpp b/src/app/imgui_impl_glfw.cpp deleted file mode 100644 index 1df78a54..00000000 --- a/src/app/imgui_impl_glfw.cpp +++ /dev/null @@ -1,1365 +0,0 @@ -// dear imgui: Platform Backend for GLFW -// This needs to be used along with a Renderer (e.g. OpenGL3, Vulkan, WebGPU..) -// (Info: GLFW is a cross-platform general purpose library for handling windows, inputs, OpenGL/Vulkan graphics context creation, etc.) -// (Requires: GLFW 3.1+. Prefer GLFW 3.3+ or GLFW 3.4+ for full feature support.) - -// Implemented features: -// [X] Platform: Clipboard support. -// [X] Platform: Mouse support. Can discriminate Mouse/TouchScreen/Pen (Windows only). -// [X] Platform: Keyboard support. Since 1.87 we are using the io.AddKeyEvent() function. Pass ImGuiKey values to all key functions e.g. ImGui::IsKeyPressed(ImGuiKey_Space). [Legacy GLFW_KEY_* values will also be supported unless IMGUI_DISABLE_OBSOLETE_KEYIO is set] -// [X] Platform: Gamepad support. Enable with 'io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad'. -// [X] Platform: Mouse cursor shape and visibility. Disable with 'io.ConfigFlags |= ImGuiConfigFlags_NoMouseCursorChange' (note: the resizing cursors requires GLFW 3.4+). -// [X] Platform: Multi-viewport support (multiple windows). Enable with 'io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable'. - -// Issues: -// [ ] Platform: Multi-viewport support: ParentViewportID not honored, and so io.ConfigViewportsNoDefaultParent has no effect (minor). - -// You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this. -// Prefer including the entire imgui/ repository into your project (either as a copy or as a submodule), and only build the backends you need. -// Learn about Dear ImGui: -// - FAQ https://dearimgui.com/faq -// - Getting Started https://dearimgui.com/getting-started -// - Documentation https://dearimgui.com/docs (same as your local docs/ folder). -// - Introduction, links and more at the top of imgui.cpp - -// CHANGELOG -// (minor and older changes stripped away, please see git history for details) -// 2024-XX-XX: Platform: Added support for multiple windows via the ImGuiPlatformIO interface. -// 2023-12-19: Emscripten: Added ImGui_ImplGlfw_InstallEmscriptenCanvasResizeCallback() to register canvas selector and auto-resize GLFW window. -// 2023-10-05: Inputs: Added support for extra ImGuiKey values: F13 to F24 function keys. -// 2023-07-18: Inputs: Revert ignoring mouse data on GLFW_CURSOR_DISABLED as it can be used differently. User may set ImGuiConfigFLags_NoMouse if desired. (#5625, #6609) -// 2023-06-12: Accept glfwGetTime() not returning a monotonically increasing value. This seems to happens on some Windows setup when peripherals disconnect, and is likely to also happen on browser + Emscripten. (#6491) -// 2023-04-04: Inputs: Added support for io.AddMouseSourceEvent() to discriminate ImGuiMouseSource_Mouse/ImGuiMouseSource_TouchScreen/ImGuiMouseSource_Pen on Windows ONLY, using a custom WndProc hook. (#2702) -// 2023-03-16: Inputs: Fixed key modifiers handling on secondary viewports (docking branch). Broken on 2023/01/04. (#6248, #6034) -// 2023-03-14: Emscripten: Avoid using glfwGetError() and glfwGetGamepadState() which are not correctly implemented in Emscripten emulation. (#6240) -// 2023-02-03: Emscripten: Registering custom low-level mouse wheel handler to get more accurate scrolling impulses on Emscripten. (#4019, #6096) -// 2023-01-18: Handle unsupported glfwGetVideoMode() call on e.g. Emscripten. -// 2023-01-04: Inputs: Fixed mods state on Linux when using Alt-GR text input (e.g. German keyboard layout), could lead to broken text input. Revert a 2022/01/17 change were we resumed using mods provided by GLFW, turns out they were faulty. -// 2022-11-22: Perform a dummy glfwGetError() read to cancel missing names with glfwGetKeyName(). (#5908) -// 2022-10-18: Perform a dummy glfwGetError() read to cancel missing mouse cursors errors. Using GLFW_VERSION_COMBINED directly. (#5785) -// 2022-10-11: Using 'nullptr' instead of 'NULL' as per our switch to C++11. -// 2022-09-26: Inputs: Renamed ImGuiKey_ModXXX introduced in 1.87 to ImGuiMod_XXX (old names still supported). -// 2022-09-01: Inputs: Honor GLFW_CURSOR_DISABLED by not setting mouse position *EDIT* Reverted 2023-07-18. -// 2022-04-30: Inputs: Fixed ImGui_ImplGlfw_TranslateUntranslatedKey() for lower case letters on OSX. -// 2022-03-23: Inputs: Fixed a regression in 1.87 which resulted in keyboard modifiers events being reported incorrectly on Linux/X11. -// 2022-02-07: Added ImGui_ImplGlfw_InstallCallbacks()/ImGui_ImplGlfw_RestoreCallbacks() helpers to facilitate user installing callbacks after initializing backend. -// 2022-01-26: Inputs: replaced short-lived io.AddKeyModsEvent() (added two weeks ago) with io.AddKeyEvent() using ImGuiKey_ModXXX flags. Sorry for the confusion. -// 2021-01-20: Inputs: calling new io.AddKeyAnalogEvent() for gamepad support, instead of writing directly to io.NavInputs[]. -// 2022-01-17: Inputs: calling new io.AddMousePosEvent(), io.AddMouseButtonEvent(), io.AddMouseWheelEvent() API (1.87+). -// 2022-01-17: Inputs: always update key mods next and before key event (not in NewFrame) to fix input queue with very low framerates. -// 2022-01-12: *BREAKING CHANGE*: Now using glfwSetCursorPosCallback(). If you called ImGui_ImplGlfw_InitXXX() with install_callbacks = false, you MUST install glfwSetCursorPosCallback() and forward it to the backend via ImGui_ImplGlfw_CursorPosCallback(). -// 2022-01-10: Inputs: calling new io.AddKeyEvent(), io.AddKeyModsEvent() + io.SetKeyEventNativeData() API (1.87+). Support for full ImGuiKey range. -// 2022-01-05: Inputs: Converting GLFW untranslated keycodes back to translated keycodes (in the ImGui_ImplGlfw_KeyCallback() function) in order to match the behavior of every other backend, and facilitate the use of GLFW with lettered-shortcuts API. -// 2021-08-17: *BREAKING CHANGE*: Now using glfwSetWindowFocusCallback() to calling io.AddFocusEvent(). If you called ImGui_ImplGlfw_InitXXX() with install_callbacks = false, you MUST install glfwSetWindowFocusCallback() and forward it to the backend via ImGui_ImplGlfw_WindowFocusCallback(). -// 2021-07-29: *BREAKING CHANGE*: Now using glfwSetCursorEnterCallback(). MousePos is correctly reported when the host platform window is hovered but not focused. If you called ImGui_ImplGlfw_InitXXX() with install_callbacks = false, you MUST install glfwSetWindowFocusCallback() callback and forward it to the backend via ImGui_ImplGlfw_CursorEnterCallback(). -// 2021-06-29: Reorganized backend to pull data from a single structure to facilitate usage with multiple-contexts (all g_XXXX access changed to bd->XXXX). -// 2020-01-17: Inputs: Disable error callback while assigning mouse cursors because some X11 setup don't have them and it generates errors. -// 2019-12-05: Inputs: Added support for new mouse cursors added in GLFW 3.4+ (resizing cursors, not allowed cursor). -// 2019-10-18: Misc: Previously installed user callbacks are now restored on shutdown. -// 2019-07-21: Inputs: Added mapping for ImGuiKey_KeyPadEnter. -// 2019-05-11: Inputs: Don't filter value from character callback before calling AddInputCharacter(). -// 2019-03-12: Misc: Preserve DisplayFramebufferScale when main window is minimized. -// 2018-11-30: Misc: Setting up io.BackendPlatformName so it can be displayed in the About Window. -// 2018-11-07: Inputs: When installing our GLFW callbacks, we save user's previously installed ones - if any - and chain call them. -// 2018-08-01: Inputs: Workaround for Emscripten which doesn't seem to handle focus related calls. -// 2018-06-29: Inputs: Added support for the ImGuiMouseCursor_Hand cursor. -// 2018-06-08: Misc: Extracted imgui_impl_glfw.cpp/.h away from the old combined GLFW+OpenGL/Vulkan examples. -// 2018-03-20: Misc: Setup io.BackendFlags ImGuiBackendFlags_HasMouseCursors flag + honor ImGuiConfigFlags_NoMouseCursorChange flag. -// 2018-02-20: Inputs: Added support for mouse cursors (ImGui::GetMouseCursor() value, passed to glfwSetCursor()). -// 2018-02-06: Misc: Removed call to ImGui::Shutdown() which is not available from 1.60 WIP, user needs to call CreateContext/DestroyContext themselves. -// 2018-02-06: Inputs: Added mapping for ImGuiKey_Space. -// 2018-01-25: Inputs: Added gamepad support if ImGuiConfigFlags_NavEnableGamepad is set. -// 2018-01-25: Inputs: Honoring the io.WantSetMousePos by repositioning the mouse (when using navigation and ImGuiConfigFlags_NavMoveMouse is set). -// 2018-01-20: Inputs: Added Horizontal Mouse Wheel support. -// 2018-01-18: Inputs: Added mapping for ImGuiKey_Insert. -// 2017-08-25: Inputs: MousePos set to -FLT_MAX,-FLT_MAX when mouse is unavailable/missing (instead of -1,-1). -// 2016-10-15: Misc: Added a void* user_data parameter to Clipboard function handlers. - -#include "imgui.h" -#ifndef IMGUI_DISABLE -#include "imgui_impl_glfw.h" - -// Clang warnings with -Weverything -#if defined(__clang__) -#pragma clang diagnostic push -#pragma clang diagnostic ignored "-Wold-style-cast" // warning: use of old-style cast -#pragma clang diagnostic ignored "-Wsign-conversion" // warning: implicit conversion changes signedness -#endif - -// GLFW -#include - -#ifdef _WIN32 -#undef APIENTRY -#define GLFW_EXPOSE_NATIVE_WIN32 -#include // for glfwGetWin32Window() -#endif -#ifdef __APPLE__ -#define GLFW_EXPOSE_NATIVE_COCOA -#include // for glfwGetCocoaWindow() -#endif - -#ifdef __EMSCRIPTEN__ -#include -#include -#endif - -// We gather version tests as define in order to easily see which features are version-dependent. -#define GLFW_VERSION_COMBINED (GLFW_VERSION_MAJOR * 1000 + GLFW_VERSION_MINOR * 100 + GLFW_VERSION_REVISION) -#define GLFW_HAS_WINDOW_TOPMOST (GLFW_VERSION_COMBINED >= 3200) // 3.2+ GLFW_FLOATING -#define GLFW_HAS_WINDOW_HOVERED (GLFW_VERSION_COMBINED >= 3300) // 3.3+ GLFW_HOVERED -#define GLFW_HAS_WINDOW_ALPHA (GLFW_VERSION_COMBINED >= 3300) // 3.3+ glfwSetWindowOpacity -#define GLFW_HAS_PER_MONITOR_DPI (GLFW_VERSION_COMBINED >= 3300) // 3.3+ glfwGetMonitorContentScale -#if defined(__EMSCRIPTEN__) || defined(__SWITCH__) // no Vulkan support in GLFW for Emscripten or homebrew Nintendo Switch -#define GLFW_HAS_VULKAN (0) -#else -#define GLFW_HAS_VULKAN (GLFW_VERSION_COMBINED >= 3200) // 3.2+ glfwCreateWindowSurface -#endif -#define GLFW_HAS_FOCUS_WINDOW (GLFW_VERSION_COMBINED >= 3200) // 3.2+ glfwFocusWindow -#define GLFW_HAS_FOCUS_ON_SHOW (GLFW_VERSION_COMBINED >= 3300) // 3.3+ GLFW_FOCUS_ON_SHOW -#define GLFW_HAS_MONITOR_WORK_AREA (GLFW_VERSION_COMBINED >= 3300) // 3.3+ glfwGetMonitorWorkarea -#define GLFW_HAS_OSX_WINDOW_POS_FIX (GLFW_VERSION_COMBINED >= 3301) // 3.3.1+ Fixed: Resizing window repositions it on MacOS #1553 -#ifdef GLFW_RESIZE_NESW_CURSOR // Let's be nice to people who pulled GLFW between 2019-04-16 (3.4 define) and 2019-11-29 (cursors defines) // FIXME: Remove when GLFW 3.4 is released? -#define GLFW_HAS_NEW_CURSORS (GLFW_VERSION_COMBINED >= 3400) // 3.4+ GLFW_RESIZE_ALL_CURSOR, GLFW_RESIZE_NESW_CURSOR, GLFW_RESIZE_NWSE_CURSOR, GLFW_NOT_ALLOWED_CURSOR -#else -#define GLFW_HAS_NEW_CURSORS (0) -#endif -#ifdef GLFW_MOUSE_PASSTHROUGH // Let's be nice to people who pulled GLFW between 2019-04-16 (3.4 define) and 2020-07-17 (passthrough) -#define GLFW_HAS_MOUSE_PASSTHROUGH (GLFW_VERSION_COMBINED >= 3400) // 3.4+ GLFW_MOUSE_PASSTHROUGH -#else -#define GLFW_HAS_MOUSE_PASSTHROUGH (0) -#endif -#define GLFW_HAS_GAMEPAD_API (GLFW_VERSION_COMBINED >= 3300) // 3.3+ glfwGetGamepadState() new api -#define GLFW_HAS_GETKEYNAME (GLFW_VERSION_COMBINED >= 3200) // 3.2+ glfwGetKeyName() -#define GLFW_HAS_GETERROR (GLFW_VERSION_COMBINED >= 3300) // 3.3+ glfwGetError() - -// GLFW data -enum GlfwClientApi -{ - GlfwClientApi_Unknown, - GlfwClientApi_OpenGL, - GlfwClientApi_Vulkan, -}; - -struct ImGui_ImplGlfw_Data -{ - GLFWwindow* Window; - GlfwClientApi ClientApi; - double Time; - GLFWwindow* MouseWindow; - GLFWcursor* MouseCursors[ImGuiMouseCursor_COUNT]; - ImVec2 LastValidMousePos; - GLFWwindow* KeyOwnerWindows[GLFW_KEY_LAST]; - bool InstalledCallbacks; - bool CallbacksChainForAllWindows; - bool WantUpdateMonitors; -#ifdef __EMSCRIPTEN__ - const char* CanvasSelector; -#endif - - // Chain GLFW callbacks: our callbacks will call the user's previously installed callbacks, if any. - GLFWwindowfocusfun PrevUserCallbackWindowFocus; - GLFWcursorposfun PrevUserCallbackCursorPos; - GLFWcursorenterfun PrevUserCallbackCursorEnter; - GLFWmousebuttonfun PrevUserCallbackMousebutton; - GLFWscrollfun PrevUserCallbackScroll; - GLFWkeyfun PrevUserCallbackKey; - GLFWcharfun PrevUserCallbackChar; - GLFWmonitorfun PrevUserCallbackMonitor; -#ifdef _WIN32 - WNDPROC PrevWndProc; -#endif - - ImGui_ImplGlfw_Data() { memset((void*)this, 0, sizeof(*this)); } -}; - -// Backend data stored in io.BackendPlatformUserData to allow support for multiple Dear ImGui contexts -// It is STRONGLY preferred that you use docking branch with multi-viewports (== single Dear ImGui context + multiple windows) instead of multiple Dear ImGui contexts. -// FIXME: multi-context support is not well tested and probably dysfunctional in this backend. -// - Because glfwPollEvents() process all windows and some events may be called outside of it, you will need to register your own callbacks -// (passing install_callbacks=false in ImGui_ImplGlfw_InitXXX functions), set the current dear imgui context and then call our callbacks. -// - Otherwise we may need to store a GLFWWindow* -> ImGuiContext* map and handle this in the backend, adding a little bit of extra complexity to it. -// FIXME: some shared resources (mouse cursor shape, gamepad) are mishandled when using multi-context. -static ImGui_ImplGlfw_Data* ImGui_ImplGlfw_GetBackendData() -{ - return ImGui::GetCurrentContext() ? (ImGui_ImplGlfw_Data*)ImGui::GetIO().BackendPlatformUserData : nullptr; -} - -// Forward Declarations -static void ImGui_ImplGlfw_UpdateMonitors(); -static void ImGui_ImplGlfw_InitPlatformInterface(); -static void ImGui_ImplGlfw_ShutdownPlatformInterface(); - -// Functions -static const char* ImGui_ImplGlfw_GetClipboardText(void* user_data) -{ - return glfwGetClipboardString((GLFWwindow*)user_data); -} - -static void ImGui_ImplGlfw_SetClipboardText(void* user_data, const char* text) -{ - glfwSetClipboardString((GLFWwindow*)user_data, text); -} - -static ImGuiKey ImGui_ImplGlfw_KeyToImGuiKey(int key) -{ - switch (key) - { - case GLFW_KEY_TAB: return ImGuiKey_Tab; - case GLFW_KEY_LEFT: return ImGuiKey_LeftArrow; - case GLFW_KEY_RIGHT: return ImGuiKey_RightArrow; - case GLFW_KEY_UP: return ImGuiKey_UpArrow; - case GLFW_KEY_DOWN: return ImGuiKey_DownArrow; - case GLFW_KEY_PAGE_UP: return ImGuiKey_PageUp; - case GLFW_KEY_PAGE_DOWN: return ImGuiKey_PageDown; - case GLFW_KEY_HOME: return ImGuiKey_Home; - case GLFW_KEY_END: return ImGuiKey_End; - case GLFW_KEY_INSERT: return ImGuiKey_Insert; - case GLFW_KEY_DELETE: return ImGuiKey_Delete; - case GLFW_KEY_BACKSPACE: return ImGuiKey_Backspace; - case GLFW_KEY_SPACE: return ImGuiKey_Space; - case GLFW_KEY_ENTER: return ImGuiKey_Enter; - case GLFW_KEY_ESCAPE: return ImGuiKey_Escape; - case GLFW_KEY_APOSTROPHE: return ImGuiKey_Apostrophe; - case GLFW_KEY_COMMA: return ImGuiKey_Comma; - case GLFW_KEY_MINUS: return ImGuiKey_Minus; - case GLFW_KEY_PERIOD: return ImGuiKey_Period; - case GLFW_KEY_SLASH: return ImGuiKey_Slash; - case GLFW_KEY_SEMICOLON: return ImGuiKey_Semicolon; - case GLFW_KEY_EQUAL: return ImGuiKey_Equal; - case GLFW_KEY_LEFT_BRACKET: return ImGuiKey_LeftBracket; - case GLFW_KEY_BACKSLASH: return ImGuiKey_Backslash; - case GLFW_KEY_RIGHT_BRACKET: return ImGuiKey_RightBracket; - case GLFW_KEY_GRAVE_ACCENT: return ImGuiKey_GraveAccent; - case GLFW_KEY_CAPS_LOCK: return ImGuiKey_CapsLock; - case GLFW_KEY_SCROLL_LOCK: return ImGuiKey_ScrollLock; - case GLFW_KEY_NUM_LOCK: return ImGuiKey_NumLock; - case GLFW_KEY_PRINT_SCREEN: return ImGuiKey_PrintScreen; - case GLFW_KEY_PAUSE: return ImGuiKey_Pause; - case GLFW_KEY_KP_0: return ImGuiKey_Keypad0; - case GLFW_KEY_KP_1: return ImGuiKey_Keypad1; - case GLFW_KEY_KP_2: return ImGuiKey_Keypad2; - case GLFW_KEY_KP_3: return ImGuiKey_Keypad3; - case GLFW_KEY_KP_4: return ImGuiKey_Keypad4; - case GLFW_KEY_KP_5: return ImGuiKey_Keypad5; - case GLFW_KEY_KP_6: return ImGuiKey_Keypad6; - case GLFW_KEY_KP_7: return ImGuiKey_Keypad7; - case GLFW_KEY_KP_8: return ImGuiKey_Keypad8; - case GLFW_KEY_KP_9: return ImGuiKey_Keypad9; - case GLFW_KEY_KP_DECIMAL: return ImGuiKey_KeypadDecimal; - case GLFW_KEY_KP_DIVIDE: return ImGuiKey_KeypadDivide; - case GLFW_KEY_KP_MULTIPLY: return ImGuiKey_KeypadMultiply; - case GLFW_KEY_KP_SUBTRACT: return ImGuiKey_KeypadSubtract; - case GLFW_KEY_KP_ADD: return ImGuiKey_KeypadAdd; - case GLFW_KEY_KP_ENTER: return ImGuiKey_KeypadEnter; - case GLFW_KEY_KP_EQUAL: return ImGuiKey_KeypadEqual; - case GLFW_KEY_LEFT_SHIFT: return ImGuiKey_LeftShift; - case GLFW_KEY_LEFT_CONTROL: return ImGuiKey_LeftCtrl; - case GLFW_KEY_LEFT_ALT: return ImGuiKey_LeftAlt; - case GLFW_KEY_LEFT_SUPER: return ImGuiKey_LeftSuper; - case GLFW_KEY_RIGHT_SHIFT: return ImGuiKey_RightShift; - case GLFW_KEY_RIGHT_CONTROL: return ImGuiKey_RightCtrl; - case GLFW_KEY_RIGHT_ALT: return ImGuiKey_RightAlt; - case GLFW_KEY_RIGHT_SUPER: return ImGuiKey_RightSuper; - case GLFW_KEY_MENU: return ImGuiKey_Menu; - case GLFW_KEY_0: return ImGuiKey_0; - case GLFW_KEY_1: return ImGuiKey_1; - case GLFW_KEY_2: return ImGuiKey_2; - case GLFW_KEY_3: return ImGuiKey_3; - case GLFW_KEY_4: return ImGuiKey_4; - case GLFW_KEY_5: return ImGuiKey_5; - case GLFW_KEY_6: return ImGuiKey_6; - case GLFW_KEY_7: return ImGuiKey_7; - case GLFW_KEY_8: return ImGuiKey_8; - case GLFW_KEY_9: return ImGuiKey_9; - case GLFW_KEY_A: return ImGuiKey_A; - case GLFW_KEY_B: return ImGuiKey_B; - case GLFW_KEY_C: return ImGuiKey_C; - case GLFW_KEY_D: return ImGuiKey_D; - case GLFW_KEY_E: return ImGuiKey_E; - case GLFW_KEY_F: return ImGuiKey_F; - case GLFW_KEY_G: return ImGuiKey_G; - case GLFW_KEY_H: return ImGuiKey_H; - case GLFW_KEY_I: return ImGuiKey_I; - case GLFW_KEY_J: return ImGuiKey_J; - case GLFW_KEY_K: return ImGuiKey_K; - case GLFW_KEY_L: return ImGuiKey_L; - case GLFW_KEY_M: return ImGuiKey_M; - case GLFW_KEY_N: return ImGuiKey_N; - case GLFW_KEY_O: return ImGuiKey_O; - case GLFW_KEY_P: return ImGuiKey_P; - case GLFW_KEY_Q: return ImGuiKey_Q; - case GLFW_KEY_R: return ImGuiKey_R; - case GLFW_KEY_S: return ImGuiKey_S; - case GLFW_KEY_T: return ImGuiKey_T; - case GLFW_KEY_U: return ImGuiKey_U; - case GLFW_KEY_V: return ImGuiKey_V; - case GLFW_KEY_W: return ImGuiKey_W; - case GLFW_KEY_X: return ImGuiKey_X; - case GLFW_KEY_Y: return ImGuiKey_Y; - case GLFW_KEY_Z: return ImGuiKey_Z; - case GLFW_KEY_F1: return ImGuiKey_F1; - case GLFW_KEY_F2: return ImGuiKey_F2; - case GLFW_KEY_F3: return ImGuiKey_F3; - case GLFW_KEY_F4: return ImGuiKey_F4; - case GLFW_KEY_F5: return ImGuiKey_F5; - case GLFW_KEY_F6: return ImGuiKey_F6; - case GLFW_KEY_F7: return ImGuiKey_F7; - case GLFW_KEY_F8: return ImGuiKey_F8; - case GLFW_KEY_F9: return ImGuiKey_F9; - case GLFW_KEY_F10: return ImGuiKey_F10; - case GLFW_KEY_F11: return ImGuiKey_F11; - case GLFW_KEY_F12: return ImGuiKey_F12; - case GLFW_KEY_F13: return ImGuiKey_F13; - case GLFW_KEY_F14: return ImGuiKey_F14; - case GLFW_KEY_F15: return ImGuiKey_F15; - case GLFW_KEY_F16: return ImGuiKey_F16; - case GLFW_KEY_F17: return ImGuiKey_F17; - case GLFW_KEY_F18: return ImGuiKey_F18; - case GLFW_KEY_F19: return ImGuiKey_F19; - case GLFW_KEY_F20: return ImGuiKey_F20; - case GLFW_KEY_F21: return ImGuiKey_F21; - case GLFW_KEY_F22: return ImGuiKey_F22; - case GLFW_KEY_F23: return ImGuiKey_F23; - case GLFW_KEY_F24: return ImGuiKey_F24; - default: return ImGuiKey_None; - } -} - -// X11 does not include current pressed/released modifier key in 'mods' flags submitted by GLFW -// See https://github.com/ocornut/imgui/issues/6034 and https://github.com/glfw/glfw/issues/1630 -static void ImGui_ImplGlfw_UpdateKeyModifiers(GLFWwindow* window) -{ - ImGuiIO& io = ImGui::GetIO(); - io.AddKeyEvent(ImGuiMod_Ctrl, (glfwGetKey(window, GLFW_KEY_LEFT_CONTROL) == GLFW_PRESS) || (glfwGetKey(window, GLFW_KEY_RIGHT_CONTROL) == GLFW_PRESS)); - io.AddKeyEvent(ImGuiMod_Shift, (glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS) || (glfwGetKey(window, GLFW_KEY_RIGHT_SHIFT) == GLFW_PRESS)); - io.AddKeyEvent(ImGuiMod_Alt, (glfwGetKey(window, GLFW_KEY_LEFT_ALT) == GLFW_PRESS) || (glfwGetKey(window, GLFW_KEY_RIGHT_ALT) == GLFW_PRESS)); - io.AddKeyEvent(ImGuiMod_Super, (glfwGetKey(window, GLFW_KEY_LEFT_SUPER) == GLFW_PRESS) || (glfwGetKey(window, GLFW_KEY_RIGHT_SUPER) == GLFW_PRESS)); -} - -static bool ImGui_ImplGlfw_ShouldChainCallback(GLFWwindow* window) -{ - ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData(); - return bd->CallbacksChainForAllWindows ? true : (window == bd->Window); -} - -void ImGui_ImplGlfw_MouseButtonCallback(GLFWwindow* window, int button, int action, int mods) -{ - ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData(); - if (bd->PrevUserCallbackMousebutton != nullptr && ImGui_ImplGlfw_ShouldChainCallback(window)) - bd->PrevUserCallbackMousebutton(window, button, action, mods); - - ImGui_ImplGlfw_UpdateKeyModifiers(window); - - ImGuiIO& io = ImGui::GetIO(); - if (button >= 0 && button < ImGuiMouseButton_COUNT) - io.AddMouseButtonEvent(button, action == GLFW_PRESS); -} - -void ImGui_ImplGlfw_ScrollCallback(GLFWwindow* window, double xoffset, double yoffset) -{ - ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData(); - if (bd->PrevUserCallbackScroll != nullptr && ImGui_ImplGlfw_ShouldChainCallback(window)) - bd->PrevUserCallbackScroll(window, xoffset, yoffset); - -#ifdef __EMSCRIPTEN__ - // Ignore GLFW events: will be processed in ImGui_ImplEmscripten_WheelCallback(). - return; -#endif - - ImGuiIO& io = ImGui::GetIO(); - io.AddMouseWheelEvent((float)xoffset, (float)yoffset); -} - -static int ImGui_ImplGlfw_TranslateUntranslatedKey(int key, int scancode) -{ -#if GLFW_HAS_GETKEYNAME && !defined(__EMSCRIPTEN__) - // GLFW 3.1+ attempts to "untranslate" keys, which goes the opposite of what every other framework does, making using lettered shortcuts difficult. - // (It had reasons to do so: namely GLFW is/was more likely to be used for WASD-type game controls rather than lettered shortcuts, but IHMO the 3.1 change could have been done differently) - // See https://github.com/glfw/glfw/issues/1502 for details. - // Adding a workaround to undo this (so our keys are translated->untranslated->translated, likely a lossy process). - // This won't cover edge cases but this is at least going to cover common cases. - if (key >= GLFW_KEY_KP_0 && key <= GLFW_KEY_KP_EQUAL) - return key; - GLFWerrorfun prev_error_callback = glfwSetErrorCallback(nullptr); - const char* key_name = glfwGetKeyName(key, scancode); - glfwSetErrorCallback(prev_error_callback); -#if GLFW_HAS_GETERROR && !defined(__EMSCRIPTEN__) // Eat errors (see #5908) - (void)glfwGetError(nullptr); -#endif - if (key_name && key_name[0] != 0 && key_name[1] == 0) - { - const char char_names[] = "`-=[]\\,;\'./"; - const int char_keys[] = { GLFW_KEY_GRAVE_ACCENT, GLFW_KEY_MINUS, GLFW_KEY_EQUAL, GLFW_KEY_LEFT_BRACKET, GLFW_KEY_RIGHT_BRACKET, GLFW_KEY_BACKSLASH, GLFW_KEY_COMMA, GLFW_KEY_SEMICOLON, GLFW_KEY_APOSTROPHE, GLFW_KEY_PERIOD, GLFW_KEY_SLASH, 0 }; - IM_ASSERT(IM_ARRAYSIZE(char_names) == IM_ARRAYSIZE(char_keys)); - if (key_name[0] >= '0' && key_name[0] <= '9') { key = GLFW_KEY_0 + (key_name[0] - '0'); } - else if (key_name[0] >= 'A' && key_name[0] <= 'Z') { key = GLFW_KEY_A + (key_name[0] - 'A'); } - else if (key_name[0] >= 'a' && key_name[0] <= 'z') { key = GLFW_KEY_A + (key_name[0] - 'a'); } - else if (const char* p = strchr(char_names, key_name[0])) { key = char_keys[p - char_names]; } - } - // if (action == GLFW_PRESS) printf("key %d scancode %d name '%s'\n", key, scancode, key_name); -#else - IM_UNUSED(scancode); -#endif - return key; -} - -void ImGui_ImplGlfw_KeyCallback(GLFWwindow* window, int keycode, int scancode, int action, int mods) -{ - ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData(); - if (bd->PrevUserCallbackKey != nullptr && ImGui_ImplGlfw_ShouldChainCallback(window)) - bd->PrevUserCallbackKey(window, keycode, scancode, action, mods); - - if (action != GLFW_PRESS && action != GLFW_RELEASE) - return; - - ImGui_ImplGlfw_UpdateKeyModifiers(window); - - if (keycode >= 0 && keycode < IM_ARRAYSIZE(bd->KeyOwnerWindows)) - bd->KeyOwnerWindows[keycode] = (action == GLFW_PRESS) ? window : nullptr; - - keycode = ImGui_ImplGlfw_TranslateUntranslatedKey(keycode, scancode); - - ImGuiIO& io = ImGui::GetIO(); - ImGuiKey imgui_key = ImGui_ImplGlfw_KeyToImGuiKey(keycode); - io.AddKeyEvent(imgui_key, (action == GLFW_PRESS)); - io.SetKeyEventNativeData(imgui_key, keycode, scancode); // To support legacy indexing (<1.87 user code) -} - -void ImGui_ImplGlfw_WindowFocusCallback(GLFWwindow* window, int focused) -{ - ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData(); - if (bd->PrevUserCallbackWindowFocus != nullptr && ImGui_ImplGlfw_ShouldChainCallback(window)) - bd->PrevUserCallbackWindowFocus(window, focused); - - ImGuiIO& io = ImGui::GetIO(); - io.AddFocusEvent(focused != 0); -} - -void ImGui_ImplGlfw_CursorPosCallback(GLFWwindow* window, double x, double y) -{ - ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData(); - if (bd->PrevUserCallbackCursorPos != nullptr && ImGui_ImplGlfw_ShouldChainCallback(window)) - bd->PrevUserCallbackCursorPos(window, x, y); - - ImGuiIO& io = ImGui::GetIO(); - if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable) - { - int window_x, window_y; - glfwGetWindowPos(window, &window_x, &window_y); - x += window_x; - y += window_y; - } - io.AddMousePosEvent((float)x, (float)y); - bd->LastValidMousePos = ImVec2((float)x, (float)y); -} - -// Workaround: X11 seems to send spurious Leave/Enter events which would make us lose our position, -// so we back it up and restore on Leave/Enter (see https://github.com/ocornut/imgui/issues/4984) -void ImGui_ImplGlfw_CursorEnterCallback(GLFWwindow* window, int entered) -{ - ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData(); - if (bd->PrevUserCallbackCursorEnter != nullptr && ImGui_ImplGlfw_ShouldChainCallback(window)) - bd->PrevUserCallbackCursorEnter(window, entered); - - ImGuiIO& io = ImGui::GetIO(); - if (entered) - { - bd->MouseWindow = window; - io.AddMousePosEvent(bd->LastValidMousePos.x, bd->LastValidMousePos.y); - } - else if (!entered && bd->MouseWindow == window) - { - bd->LastValidMousePos = io.MousePos; - bd->MouseWindow = nullptr; - io.AddMousePosEvent(-FLT_MAX, -FLT_MAX); - } -} - -void ImGui_ImplGlfw_CharCallback(GLFWwindow* window, unsigned int c) -{ - ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData(); - if (bd->PrevUserCallbackChar != nullptr && ImGui_ImplGlfw_ShouldChainCallback(window)) - bd->PrevUserCallbackChar(window, c); - - ImGuiIO& io = ImGui::GetIO(); - io.AddInputCharacter(c); -} - -void ImGui_ImplGlfw_MonitorCallback(GLFWmonitor*, int) -{ - ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData(); - bd->WantUpdateMonitors = true; -} - -#ifdef __EMSCRIPTEN__ -static EM_BOOL ImGui_ImplEmscripten_WheelCallback(int, const EmscriptenWheelEvent* ev, void*) -{ - // Mimic Emscripten_HandleWheel() in SDL. - // Corresponding equivalent in GLFW JS emulation layer has incorrect quantizing preventing small values. See #6096 - float multiplier = 0.0f; - if (ev->deltaMode == DOM_DELTA_PIXEL) { multiplier = 1.0f / 100.0f; } // 100 pixels make up a step. - else if (ev->deltaMode == DOM_DELTA_LINE) { multiplier = 1.0f / 3.0f; } // 3 lines make up a step. - else if (ev->deltaMode == DOM_DELTA_PAGE) { multiplier = 80.0f; } // A page makes up 80 steps. - float wheel_x = ev->deltaX * -multiplier; - float wheel_y = ev->deltaY * -multiplier; - ImGuiIO& io = ImGui::GetIO(); - io.AddMouseWheelEvent(wheel_x, wheel_y); - //IMGUI_DEBUG_LOG("[Emsc] mode %d dx: %.2f, dy: %.2f, dz: %.2f --> feed %.2f %.2f\n", (int)ev->deltaMode, ev->deltaX, ev->deltaY, ev->deltaZ, wheel_x, wheel_y); - return EM_TRUE; -} -#endif - -#ifdef _WIN32 -static LRESULT CALLBACK ImGui_ImplGlfw_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam); -#endif - -void ImGui_ImplGlfw_InstallCallbacks(GLFWwindow* window) -{ - ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData(); - IM_ASSERT(bd->InstalledCallbacks == false && "Callbacks already installed!"); - IM_ASSERT(bd->Window == window); - - bd->PrevUserCallbackWindowFocus = glfwSetWindowFocusCallback(window, ImGui_ImplGlfw_WindowFocusCallback); - bd->PrevUserCallbackCursorEnter = glfwSetCursorEnterCallback(window, ImGui_ImplGlfw_CursorEnterCallback); - bd->PrevUserCallbackCursorPos = glfwSetCursorPosCallback(window, ImGui_ImplGlfw_CursorPosCallback); - bd->PrevUserCallbackMousebutton = glfwSetMouseButtonCallback(window, ImGui_ImplGlfw_MouseButtonCallback); - bd->PrevUserCallbackScroll = glfwSetScrollCallback(window, ImGui_ImplGlfw_ScrollCallback); - bd->PrevUserCallbackKey = glfwSetKeyCallback(window, ImGui_ImplGlfw_KeyCallback); - bd->PrevUserCallbackChar = glfwSetCharCallback(window, ImGui_ImplGlfw_CharCallback); - bd->PrevUserCallbackMonitor = glfwSetMonitorCallback(ImGui_ImplGlfw_MonitorCallback); - bd->InstalledCallbacks = true; -} - -void ImGui_ImplGlfw_RestoreCallbacks(GLFWwindow* window) -{ - ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData(); - IM_ASSERT(bd->InstalledCallbacks == true && "Callbacks not installed!"); - IM_ASSERT(bd->Window == window); - - glfwSetWindowFocusCallback(window, bd->PrevUserCallbackWindowFocus); - glfwSetCursorEnterCallback(window, bd->PrevUserCallbackCursorEnter); - glfwSetCursorPosCallback(window, bd->PrevUserCallbackCursorPos); - glfwSetMouseButtonCallback(window, bd->PrevUserCallbackMousebutton); - glfwSetScrollCallback(window, bd->PrevUserCallbackScroll); - glfwSetKeyCallback(window, bd->PrevUserCallbackKey); - glfwSetCharCallback(window, bd->PrevUserCallbackChar); - glfwSetMonitorCallback(bd->PrevUserCallbackMonitor); - bd->InstalledCallbacks = false; - bd->PrevUserCallbackWindowFocus = nullptr; - bd->PrevUserCallbackCursorEnter = nullptr; - bd->PrevUserCallbackCursorPos = nullptr; - bd->PrevUserCallbackMousebutton = nullptr; - bd->PrevUserCallbackScroll = nullptr; - bd->PrevUserCallbackKey = nullptr; - bd->PrevUserCallbackChar = nullptr; - bd->PrevUserCallbackMonitor = nullptr; -} - -// Set to 'true' to enable chaining installed callbacks for all windows (including secondary viewports created by backends or by user. -// This is 'false' by default meaning we only chain callbacks for the main viewport. -// We cannot set this to 'true' by default because user callbacks code may be not testing the 'window' parameter of their callback. -// If you set this to 'true' your user callback code will need to make sure you are testing the 'window' parameter. -void ImGui_ImplGlfw_SetCallbacksChainForAllWindows(bool chain_for_all_windows) -{ - ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData(); - bd->CallbacksChainForAllWindows = chain_for_all_windows; -} - -static bool ImGui_ImplGlfw_Init(GLFWwindow* window, bool install_callbacks, GlfwClientApi client_api) -{ - ImGuiIO& io = ImGui::GetIO(); - IM_ASSERT(io.BackendPlatformUserData == nullptr && "Already initialized a platform backend!"); - //printf("GLFW_VERSION: %d.%d.%d (%d)", GLFW_VERSION_MAJOR, GLFW_VERSION_MINOR, GLFW_VERSION_REVISION, GLFW_VERSION_COMBINED); - - // Setup backend capabilities flags - ImGui_ImplGlfw_Data* bd = IM_NEW(ImGui_ImplGlfw_Data)(); - io.BackendPlatformUserData = (void*)bd; - io.BackendPlatformName = "imgui_impl_glfw"; - io.BackendFlags |= ImGuiBackendFlags_HasMouseCursors; // We can honor GetMouseCursor() values (optional) - io.BackendFlags |= ImGuiBackendFlags_HasSetMousePos; // We can honor io.WantSetMousePos requests (optional, rarely used) -#ifndef __EMSCRIPTEN__ - io.BackendFlags |= ImGuiBackendFlags_PlatformHasViewports; // We can create multi-viewports on the Platform side (optional) -#endif -#if GLFW_HAS_MOUSE_PASSTHROUGH || GLFW_HAS_WINDOW_HOVERED - io.BackendFlags |= ImGuiBackendFlags_HasMouseHoveredViewport; // We can call io.AddMouseViewportEvent() with correct data (optional) -#endif - - bd->Window = window; - bd->Time = 0.0; - bd->WantUpdateMonitors = true; - - io.SetClipboardTextFn = ImGui_ImplGlfw_SetClipboardText; - io.GetClipboardTextFn = ImGui_ImplGlfw_GetClipboardText; - io.ClipboardUserData = bd->Window; - - // Create mouse cursors - // (By design, on X11 cursors are user configurable and some cursors may be missing. When a cursor doesn't exist, - // GLFW will emit an error which will often be printed by the app, so we temporarily disable error reporting. - // Missing cursors will return nullptr and our _UpdateMouseCursor() function will use the Arrow cursor instead.) - GLFWerrorfun prev_error_callback = glfwSetErrorCallback(nullptr); - bd->MouseCursors[ImGuiMouseCursor_Arrow] = glfwCreateStandardCursor(GLFW_ARROW_CURSOR); - bd->MouseCursors[ImGuiMouseCursor_TextInput] = glfwCreateStandardCursor(GLFW_IBEAM_CURSOR); - bd->MouseCursors[ImGuiMouseCursor_ResizeNS] = glfwCreateStandardCursor(GLFW_VRESIZE_CURSOR); - bd->MouseCursors[ImGuiMouseCursor_ResizeEW] = glfwCreateStandardCursor(GLFW_HRESIZE_CURSOR); - bd->MouseCursors[ImGuiMouseCursor_Hand] = glfwCreateStandardCursor(GLFW_HAND_CURSOR); -#if GLFW_HAS_NEW_CURSORS - bd->MouseCursors[ImGuiMouseCursor_ResizeAll] = glfwCreateStandardCursor(GLFW_RESIZE_ALL_CURSOR); - bd->MouseCursors[ImGuiMouseCursor_ResizeNESW] = glfwCreateStandardCursor(GLFW_RESIZE_NESW_CURSOR); - bd->MouseCursors[ImGuiMouseCursor_ResizeNWSE] = glfwCreateStandardCursor(GLFW_RESIZE_NWSE_CURSOR); - bd->MouseCursors[ImGuiMouseCursor_NotAllowed] = glfwCreateStandardCursor(GLFW_NOT_ALLOWED_CURSOR); -#else - bd->MouseCursors[ImGuiMouseCursor_ResizeAll] = glfwCreateStandardCursor(GLFW_ARROW_CURSOR); - bd->MouseCursors[ImGuiMouseCursor_ResizeNESW] = glfwCreateStandardCursor(GLFW_ARROW_CURSOR); - bd->MouseCursors[ImGuiMouseCursor_ResizeNWSE] = glfwCreateStandardCursor(GLFW_ARROW_CURSOR); - bd->MouseCursors[ImGuiMouseCursor_NotAllowed] = glfwCreateStandardCursor(GLFW_ARROW_CURSOR); -#endif - glfwSetErrorCallback(prev_error_callback); -#if GLFW_HAS_GETERROR && !defined(__EMSCRIPTEN__) // Eat errors (see #5908) - (void)glfwGetError(nullptr); -#endif - - // Chain GLFW callbacks: our callbacks will call the user's previously installed callbacks, if any. - if (install_callbacks) - ImGui_ImplGlfw_InstallCallbacks(window); - // Register Emscripten Wheel callback to workaround issue in Emscripten GLFW Emulation (#6096) - // We intentionally do not check 'if (install_callbacks)' here, as some users may set it to false and call GLFW callback themselves. - // FIXME: May break chaining in case user registered their own Emscripten callback? -#ifdef __EMSCRIPTEN__ - emscripten_set_wheel_callback(EMSCRIPTEN_EVENT_TARGET_DOCUMENT, nullptr, false, ImGui_ImplEmscripten_WheelCallback); -#endif - - // Update monitors the first time (note: monitor callback are broken in GLFW 3.2 and earlier, see github.com/glfw/glfw/issues/784) - ImGui_ImplGlfw_UpdateMonitors(); - glfwSetMonitorCallback(ImGui_ImplGlfw_MonitorCallback); - - // Set platform dependent data in viewport - ImGuiViewport* main_viewport = ImGui::GetMainViewport(); - main_viewport->PlatformHandle = (void*)bd->Window; -#ifdef _WIN32 - main_viewport->PlatformHandleRaw = glfwGetWin32Window(bd->Window); -#elif defined(__APPLE__) - main_viewport->PlatformHandleRaw = (void*)glfwGetCocoaWindow(bd->Window); -#else - IM_UNUSED(main_viewport); -#endif - if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable) - ImGui_ImplGlfw_InitPlatformInterface(); - - // Windows: register a WndProc hook so we can intercept some messages. -#ifdef _WIN32 - bd->PrevWndProc = (WNDPROC)::GetWindowLongPtrW((HWND)main_viewport->PlatformHandleRaw, GWLP_WNDPROC); - IM_ASSERT(bd->PrevWndProc != nullptr); - ::SetWindowLongPtrW((HWND)main_viewport->PlatformHandleRaw, GWLP_WNDPROC, (LONG_PTR)ImGui_ImplGlfw_WndProc); -#endif - - bd->ClientApi = client_api; - return true; -} - -bool ImGui_ImplGlfw_InitForOpenGL(GLFWwindow* window, bool install_callbacks) -{ - return ImGui_ImplGlfw_Init(window, install_callbacks, GlfwClientApi_OpenGL); -} - -bool ImGui_ImplGlfw_InitForVulkan(GLFWwindow* window, bool install_callbacks) -{ - return ImGui_ImplGlfw_Init(window, install_callbacks, GlfwClientApi_Vulkan); -} - -bool ImGui_ImplGlfw_InitForOther(GLFWwindow* window, bool install_callbacks) -{ - return ImGui_ImplGlfw_Init(window, install_callbacks, GlfwClientApi_Unknown); -} - -void ImGui_ImplGlfw_Shutdown() -{ - ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData(); - IM_ASSERT(bd != nullptr && "No platform backend to shutdown, or already shutdown?"); - ImGuiIO& io = ImGui::GetIO(); - - ImGui_ImplGlfw_ShutdownPlatformInterface(); - - if (bd->InstalledCallbacks) - ImGui_ImplGlfw_RestoreCallbacks(bd->Window); -#ifdef __EMSCRIPTEN__ - emscripten_set_wheel_callback(EMSCRIPTEN_EVENT_TARGET_DOCUMENT, nullptr, false, nullptr); -#endif - - for (ImGuiMouseCursor cursor_n = 0; cursor_n < ImGuiMouseCursor_COUNT; cursor_n++) - glfwDestroyCursor(bd->MouseCursors[cursor_n]); - - // Windows: restore our WndProc hook -#ifdef _WIN32 - ImGuiViewport* main_viewport = ImGui::GetMainViewport(); - ::SetWindowLongPtrW((HWND)main_viewport->PlatformHandleRaw, GWLP_WNDPROC, (LONG_PTR)bd->PrevWndProc); - bd->PrevWndProc = nullptr; -#endif - - io.BackendPlatformName = nullptr; - io.BackendPlatformUserData = nullptr; - io.BackendFlags &= ~(ImGuiBackendFlags_HasMouseCursors | ImGuiBackendFlags_HasSetMousePos | ImGuiBackendFlags_HasGamepad | ImGuiBackendFlags_PlatformHasViewports | ImGuiBackendFlags_HasMouseHoveredViewport); - IM_DELETE(bd); -} - -static void ImGui_ImplGlfw_UpdateMouseData() -{ - ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData(); - ImGuiIO& io = ImGui::GetIO(); - ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO(); - - ImGuiID mouse_viewport_id = 0; - const ImVec2 mouse_pos_prev = io.MousePos; - for (int n = 0; n < platform_io.Viewports.Size; n++) - { - ImGuiViewport* viewport = platform_io.Viewports[n]; - GLFWwindow* window = (GLFWwindow*)viewport->PlatformHandle; - -#ifdef __EMSCRIPTEN__ - const bool is_window_focused = true; -#else - const bool is_window_focused = glfwGetWindowAttrib(window, GLFW_FOCUSED) != 0; -#endif - if (is_window_focused) - { - // (Optional) Set OS mouse position from Dear ImGui if requested (rarely used, only when ImGuiConfigFlags_NavEnableSetMousePos is enabled by user) - // When multi-viewports are enabled, all Dear ImGui positions are same as OS positions. - if (io.WantSetMousePos) - glfwSetCursorPos(window, (double)(mouse_pos_prev.x - viewport->Pos.x), (double)(mouse_pos_prev.y - viewport->Pos.y)); - - // (Optional) Fallback to provide mouse position when focused (ImGui_ImplGlfw_CursorPosCallback already provides this when hovered or captured) - if (bd->MouseWindow == nullptr) - { - double mouse_x, mouse_y; - glfwGetCursorPos(window, &mouse_x, &mouse_y); - if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable) - { - // Single viewport mode: mouse position in client window coordinates (io.MousePos is (0,0) when the mouse is on the upper-left corner of the app window) - // Multi-viewport mode: mouse position in OS absolute coordinates (io.MousePos is (0,0) when the mouse is on the upper-left of the primary monitor) - int window_x, window_y; - glfwGetWindowPos(window, &window_x, &window_y); - mouse_x += window_x; - mouse_y += window_y; - } - bd->LastValidMousePos = ImVec2((float)mouse_x, (float)mouse_y); - io.AddMousePosEvent((float)mouse_x, (float)mouse_y); - } - } - - // (Optional) When using multiple viewports: call io.AddMouseViewportEvent() with the viewport the OS mouse cursor is hovering. - // If ImGuiBackendFlags_HasMouseHoveredViewport is not set by the backend, Dear imGui will ignore this field and infer the information using its flawed heuristic. - // - [X] GLFW >= 3.3 backend ON WINDOWS ONLY does correctly ignore viewports with the _NoInputs flag (since we implement hit via our WndProc hook) - // On other platforms we rely on the library fallbacking to its own search when reporting a viewport with _NoInputs flag. - // - [!] GLFW <= 3.2 backend CANNOT correctly ignore viewports with the _NoInputs flag, and CANNOT reported Hovered Viewport because of mouse capture. - // Some backend are not able to handle that correctly. If a backend report an hovered viewport that has the _NoInputs flag (e.g. when dragging a window - // for docking, the viewport has the _NoInputs flag in order to allow us to find the viewport under), then Dear ImGui is forced to ignore the value reported - // by the backend, and use its flawed heuristic to guess the viewport behind. - // - [X] GLFW backend correctly reports this regardless of another viewport behind focused and dragged from (we need this to find a useful drag and drop target). - // FIXME: This is currently only correct on Win32. See what we do below with the WM_NCHITTEST, missing an equivalent for other systems. - // See https://github.com/glfw/glfw/issues/1236 if you want to help in making this a GLFW feature. -#if GLFW_HAS_MOUSE_PASSTHROUGH - const bool window_no_input = (viewport->Flags & ImGuiViewportFlags_NoInputs) != 0; - glfwSetWindowAttrib(window, GLFW_MOUSE_PASSTHROUGH, window_no_input); -#endif -#if GLFW_HAS_MOUSE_PASSTHROUGH || GLFW_HAS_WINDOW_HOVERED - if (glfwGetWindowAttrib(window, GLFW_HOVERED)) - mouse_viewport_id = viewport->ID; -#else - // We cannot use bd->MouseWindow maintained from CursorEnter/Leave callbacks, because it is locked to the window capturing mouse. -#endif - } - - if (io.BackendFlags & ImGuiBackendFlags_HasMouseHoveredViewport) - io.AddMouseViewportEvent(mouse_viewport_id); -} - -static void ImGui_ImplGlfw_UpdateMouseCursor() -{ - ImGuiIO& io = ImGui::GetIO(); - ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData(); - if ((io.ConfigFlags & ImGuiConfigFlags_NoMouseCursorChange) || glfwGetInputMode(bd->Window, GLFW_CURSOR) == GLFW_CURSOR_DISABLED) - return; - - ImGuiMouseCursor imgui_cursor = ImGui::GetMouseCursor(); - ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO(); - for (int n = 0; n < platform_io.Viewports.Size; n++) - { - GLFWwindow* window = (GLFWwindow*)platform_io.Viewports[n]->PlatformHandle; - if (imgui_cursor == ImGuiMouseCursor_None || io.MouseDrawCursor) - { - // Hide OS mouse cursor if imgui is drawing it or if it wants no cursor - glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN); - } - else - { - // Show OS mouse cursor - // FIXME-PLATFORM: Unfocused windows seems to fail changing the mouse cursor with GLFW 3.2, but 3.3 works here. - glfwSetCursor(window, bd->MouseCursors[imgui_cursor] ? bd->MouseCursors[imgui_cursor] : bd->MouseCursors[ImGuiMouseCursor_Arrow]); - glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL); - } - } -} - -// Update gamepad inputs -static inline float Saturate(float v) { return v < 0.0f ? 0.0f : v > 1.0f ? 1.0f : v; } -static void ImGui_ImplGlfw_UpdateGamepads() -{ - ImGuiIO& io = ImGui::GetIO(); - if ((io.ConfigFlags & ImGuiConfigFlags_NavEnableGamepad) == 0) // FIXME: Technically feeding gamepad shouldn't depend on this now that they are regular inputs. - return; - - io.BackendFlags &= ~ImGuiBackendFlags_HasGamepad; -#if GLFW_HAS_GAMEPAD_API && !defined(__EMSCRIPTEN__) - GLFWgamepadstate gamepad; - if (!glfwGetGamepadState(GLFW_JOYSTICK_1, &gamepad)) - return; - #define MAP_BUTTON(KEY_NO, BUTTON_NO, _UNUSED) do { io.AddKeyEvent(KEY_NO, gamepad.buttons[BUTTON_NO] != 0); } while (0) - #define MAP_ANALOG(KEY_NO, AXIS_NO, _UNUSED, V0, V1) do { float v = gamepad.axes[AXIS_NO]; v = (v - V0) / (V1 - V0); io.AddKeyAnalogEvent(KEY_NO, v > 0.10f, Saturate(v)); } while (0) -#else - int axes_count = 0, buttons_count = 0; - const float* axes = glfwGetJoystickAxes(GLFW_JOYSTICK_1, &axes_count); - const unsigned char* buttons = glfwGetJoystickButtons(GLFW_JOYSTICK_1, &buttons_count); - if (axes_count == 0 || buttons_count == 0) - return; - #define MAP_BUTTON(KEY_NO, _UNUSED, BUTTON_NO) do { io.AddKeyEvent(KEY_NO, (buttons_count > BUTTON_NO && buttons[BUTTON_NO] == GLFW_PRESS)); } while (0) - #define MAP_ANALOG(KEY_NO, _UNUSED, AXIS_NO, V0, V1) do { float v = (axes_count > AXIS_NO) ? axes[AXIS_NO] : V0; v = (v - V0) / (V1 - V0); io.AddKeyAnalogEvent(KEY_NO, v > 0.10f, Saturate(v)); } while (0) -#endif - io.BackendFlags |= ImGuiBackendFlags_HasGamepad; - MAP_BUTTON(ImGuiKey_GamepadStart, GLFW_GAMEPAD_BUTTON_START, 7); - MAP_BUTTON(ImGuiKey_GamepadBack, GLFW_GAMEPAD_BUTTON_BACK, 6); - MAP_BUTTON(ImGuiKey_GamepadFaceLeft, GLFW_GAMEPAD_BUTTON_X, 2); // Xbox X, PS Square - MAP_BUTTON(ImGuiKey_GamepadFaceRight, GLFW_GAMEPAD_BUTTON_B, 1); // Xbox B, PS Circle - MAP_BUTTON(ImGuiKey_GamepadFaceUp, GLFW_GAMEPAD_BUTTON_Y, 3); // Xbox Y, PS Triangle - MAP_BUTTON(ImGuiKey_GamepadFaceDown, GLFW_GAMEPAD_BUTTON_A, 0); // Xbox A, PS Cross - MAP_BUTTON(ImGuiKey_GamepadDpadLeft, GLFW_GAMEPAD_BUTTON_DPAD_LEFT, 13); - MAP_BUTTON(ImGuiKey_GamepadDpadRight, GLFW_GAMEPAD_BUTTON_DPAD_RIGHT, 11); - MAP_BUTTON(ImGuiKey_GamepadDpadUp, GLFW_GAMEPAD_BUTTON_DPAD_UP, 10); - MAP_BUTTON(ImGuiKey_GamepadDpadDown, GLFW_GAMEPAD_BUTTON_DPAD_DOWN, 12); - MAP_BUTTON(ImGuiKey_GamepadL1, GLFW_GAMEPAD_BUTTON_LEFT_BUMPER, 4); - MAP_BUTTON(ImGuiKey_GamepadR1, GLFW_GAMEPAD_BUTTON_RIGHT_BUMPER, 5); - MAP_ANALOG(ImGuiKey_GamepadL2, GLFW_GAMEPAD_AXIS_LEFT_TRIGGER, 4, -0.75f, +1.0f); - MAP_ANALOG(ImGuiKey_GamepadR2, GLFW_GAMEPAD_AXIS_RIGHT_TRIGGER, 5, -0.75f, +1.0f); - MAP_BUTTON(ImGuiKey_GamepadL3, GLFW_GAMEPAD_BUTTON_LEFT_THUMB, 8); - MAP_BUTTON(ImGuiKey_GamepadR3, GLFW_GAMEPAD_BUTTON_RIGHT_THUMB, 9); - MAP_ANALOG(ImGuiKey_GamepadLStickLeft, GLFW_GAMEPAD_AXIS_LEFT_X, 0, -0.25f, -1.0f); - MAP_ANALOG(ImGuiKey_GamepadLStickRight, GLFW_GAMEPAD_AXIS_LEFT_X, 0, +0.25f, +1.0f); - MAP_ANALOG(ImGuiKey_GamepadLStickUp, GLFW_GAMEPAD_AXIS_LEFT_Y, 1, -0.25f, -1.0f); - MAP_ANALOG(ImGuiKey_GamepadLStickDown, GLFW_GAMEPAD_AXIS_LEFT_Y, 1, +0.25f, +1.0f); - MAP_ANALOG(ImGuiKey_GamepadRStickLeft, GLFW_GAMEPAD_AXIS_RIGHT_X, 2, -0.25f, -1.0f); - MAP_ANALOG(ImGuiKey_GamepadRStickRight, GLFW_GAMEPAD_AXIS_RIGHT_X, 2, +0.25f, +1.0f); - MAP_ANALOG(ImGuiKey_GamepadRStickUp, GLFW_GAMEPAD_AXIS_RIGHT_Y, 3, -0.25f, -1.0f); - MAP_ANALOG(ImGuiKey_GamepadRStickDown, GLFW_GAMEPAD_AXIS_RIGHT_Y, 3, +0.25f, +1.0f); - #undef MAP_BUTTON - #undef MAP_ANALOG -} - -static void ImGui_ImplGlfw_UpdateMonitors() -{ - ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData(); - ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO(); - bd->WantUpdateMonitors = false; - - int monitors_count = 0; - GLFWmonitor** glfw_monitors = glfwGetMonitors(&monitors_count); - if (monitors_count == 0) // Preserve existing monitor list if there are none. Happens on macOS sleeping (#5683) - return; - - platform_io.Monitors.resize(0); - for (int n = 0; n < monitors_count; n++) - { - ImGuiPlatformMonitor monitor; - int x, y; - glfwGetMonitorPos(glfw_monitors[n], &x, &y); - const GLFWvidmode* vid_mode = glfwGetVideoMode(glfw_monitors[n]); - if (vid_mode == nullptr) - continue; // Failed to get Video mode (e.g. Emscripten does not support this function) - monitor.MainPos = monitor.WorkPos = ImVec2((float)x, (float)y); - monitor.MainSize = monitor.WorkSize = ImVec2((float)vid_mode->width, (float)vid_mode->height); -#if GLFW_HAS_MONITOR_WORK_AREA - int w, h; - glfwGetMonitorWorkarea(glfw_monitors[n], &x, &y, &w, &h); - if (w > 0 && h > 0) // Workaround a small GLFW issue reporting zero on monitor changes: https://github.com/glfw/glfw/pull/1761 - { - monitor.WorkPos = ImVec2((float)x, (float)y); - monitor.WorkSize = ImVec2((float)w, (float)h); - } -#endif -#if GLFW_HAS_PER_MONITOR_DPI - // Warning: the validity of monitor DPI information on Windows depends on the application DPI awareness settings, which generally needs to be set in the manifest or at runtime. - float x_scale, y_scale; - glfwGetMonitorContentScale(glfw_monitors[n], &x_scale, &y_scale); - monitor.DpiScale = x_scale; -#endif - monitor.PlatformHandle = (void*)glfw_monitors[n]; // [...] GLFW doc states: "guaranteed to be valid only until the monitor configuration changes" - platform_io.Monitors.push_back(monitor); - } -} - -void ImGui_ImplGlfw_NewFrame() -{ - ImGuiIO& io = ImGui::GetIO(); - ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData(); - IM_ASSERT(bd != nullptr && "Did you call ImGui_ImplGlfw_InitForXXX()?"); - - // Setup display size (every frame to accommodate for window resizing) - int w, h; - int display_w, display_h; - glfwGetWindowSize(bd->Window, &w, &h); - glfwGetFramebufferSize(bd->Window, &display_w, &display_h); - io.DisplaySize = ImVec2((float)w, (float)h); - if (w > 0 && h > 0) - io.DisplayFramebufferScale = ImVec2((float)display_w / (float)w, (float)display_h / (float)h); - if (bd->WantUpdateMonitors) - ImGui_ImplGlfw_UpdateMonitors(); - - // Setup time step - // (Accept glfwGetTime() not returning a monotonically increasing value. Seems to happens on disconnecting peripherals and probably on VMs and Emscripten, see #6491, #6189, #6114, #3644) - double current_time = glfwGetTime(); - if (current_time <= bd->Time) - current_time = bd->Time + 0.00001f; - io.DeltaTime = bd->Time > 0.0 ? (float)(current_time - bd->Time) : (float)(1.0f / 60.0f); - bd->Time = current_time; - - ImGui_ImplGlfw_UpdateMouseData(); - ImGui_ImplGlfw_UpdateMouseCursor(); - - // Update game controllers (if enabled and available) - ImGui_ImplGlfw_UpdateGamepads(); -} - -#ifdef __EMSCRIPTEN__ -static EM_BOOL ImGui_ImplGlfw_OnCanvasSizeChange(int event_type, const EmscriptenUiEvent* event, void* user_data) -{ - ImGui_ImplGlfw_Data* bd = (ImGui_ImplGlfw_Data*)user_data; - double canvas_width, canvas_height; - emscripten_get_element_css_size(bd->CanvasSelector, &canvas_width, &canvas_height); - glfwSetWindowSize(bd->Window, (int)canvas_width, (int)canvas_height); - return true; -} - -static EM_BOOL ImGui_ImplEmscripten_FullscreenChangeCallback(int event_type, const EmscriptenFullscreenChangeEvent* event, void* user_data) -{ - ImGui_ImplGlfw_Data* bd = (ImGui_ImplGlfw_Data*)user_data; - double canvas_width, canvas_height; - emscripten_get_element_css_size(bd->CanvasSelector, &canvas_width, &canvas_height); - glfwSetWindowSize(bd->Window, (int)canvas_width, (int)canvas_height); - return true; -} - -// 'canvas_selector' is a CSS selector. The event listener is applied to the first element that matches the query. -// STRING MUST PERSIST FOR THE APPLICATION DURATION. PLEASE USE A STRING LITERAL OR ENSURE POINTER WILL STAY VALID. -void ImGui_ImplGlfw_InstallEmscriptenCanvasResizeCallback(const char* canvas_selector) -{ - IM_ASSERT(canvas_selector != nullptr); - ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData(); - IM_ASSERT(bd != nullptr && "Did you call ImGui_ImplGlfw_InitForXXX()?"); - - bd->CanvasSelector = canvas_selector; - emscripten_set_resize_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, bd, false, ImGui_ImplGlfw_OnCanvasSizeChange); - emscripten_set_fullscreenchange_callback(EMSCRIPTEN_EVENT_TARGET_DOCUMENT, bd, false, ImGui_ImplEmscripten_FullscreenChangeCallback); - - // Change the size of the GLFW window according to the size of the canvas - ImGui_ImplGlfw_OnCanvasSizeChange(EMSCRIPTEN_EVENT_RESIZE, {}, bd); -} -#endif - - -//-------------------------------------------------------------------------------------------------------- -// MULTI-VIEWPORT / PLATFORM INTERFACE SUPPORT -// This is an _advanced_ and _optional_ feature, allowing the backend to create and handle multiple viewports simultaneously. -// If you are new to dear imgui or creating a new binding for dear imgui, it is recommended that you completely ignore this section first.. -//-------------------------------------------------------------------------------------------------------- - -// Helper structure we store in the void* RendererUserData field of each ImGuiViewport to easily retrieve our backend data. -struct ImGui_ImplGlfw_ViewportData -{ - GLFWwindow* Window; - bool WindowOwned; - int IgnoreWindowPosEventFrame; - int IgnoreWindowSizeEventFrame; -#ifdef _WIN32 - WNDPROC PrevWndProc; -#endif - - ImGui_ImplGlfw_ViewportData() { memset(this, 0, sizeof(*this)); IgnoreWindowSizeEventFrame = IgnoreWindowPosEventFrame = -1; } - ~ImGui_ImplGlfw_ViewportData() { IM_ASSERT(Window == nullptr); } -}; - -static void ImGui_ImplGlfw_WindowCloseCallback(GLFWwindow* window) -{ - if (ImGuiViewport* viewport = ImGui::FindViewportByPlatformHandle(window)) - viewport->PlatformRequestClose = true; -} - -// GLFW may dispatch window pos/size events after calling glfwSetWindowPos()/glfwSetWindowSize(). -// However: depending on the platform the callback may be invoked at different time: -// - on Windows it appears to be called within the glfwSetWindowPos()/glfwSetWindowSize() call -// - on Linux it is queued and invoked during glfwPollEvents() -// Because the event doesn't always fire on glfwSetWindowXXX() we use a frame counter tag to only -// ignore recent glfwSetWindowXXX() calls. -static void ImGui_ImplGlfw_WindowPosCallback(GLFWwindow* window, int, int) -{ - if (ImGuiViewport* viewport = ImGui::FindViewportByPlatformHandle(window)) - { - if (ImGui_ImplGlfw_ViewportData* vd = (ImGui_ImplGlfw_ViewportData*)viewport->PlatformUserData) - { - bool ignore_event = (ImGui::GetFrameCount() <= vd->IgnoreWindowPosEventFrame + 1); - //data->IgnoreWindowPosEventFrame = -1; - if (ignore_event) - return; - } - viewport->PlatformRequestMove = true; - } -} - -static void ImGui_ImplGlfw_WindowSizeCallback(GLFWwindow* window, int, int) -{ - if (ImGuiViewport* viewport = ImGui::FindViewportByPlatformHandle(window)) - { - if (ImGui_ImplGlfw_ViewportData* vd = (ImGui_ImplGlfw_ViewportData*)viewport->PlatformUserData) - { - bool ignore_event = (ImGui::GetFrameCount() <= vd->IgnoreWindowSizeEventFrame + 1); - //data->IgnoreWindowSizeEventFrame = -1; - if (ignore_event) - return; - } - viewport->PlatformRequestResize = true; - } -} - -static void ImGui_ImplGlfw_CreateWindow(ImGuiViewport* viewport) -{ - ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData(); - ImGui_ImplGlfw_ViewportData* vd = IM_NEW(ImGui_ImplGlfw_ViewportData)(); - viewport->PlatformUserData = vd; - - // GLFW 3.2 unfortunately always set focus on glfwCreateWindow() if GLFW_VISIBLE is set, regardless of GLFW_FOCUSED - // With GLFW 3.3, the hint GLFW_FOCUS_ON_SHOW fixes this problem - glfwWindowHint(GLFW_VISIBLE, false); - glfwWindowHint(GLFW_FOCUSED, false); -#if GLFW_HAS_FOCUS_ON_SHOW - glfwWindowHint(GLFW_FOCUS_ON_SHOW, false); - #endif - glfwWindowHint(GLFW_DECORATED, (viewport->Flags & ImGuiViewportFlags_NoDecoration) ? false : true); -#if GLFW_HAS_WINDOW_TOPMOST - glfwWindowHint(GLFW_FLOATING, (viewport->Flags & ImGuiViewportFlags_TopMost) ? true : false); -#endif - GLFWwindow* share_window = (bd->ClientApi == GlfwClientApi_OpenGL) ? bd->Window : nullptr; - vd->Window = glfwCreateWindow((int)viewport->Size.x, (int)viewport->Size.y, "No Title Yet", nullptr, share_window); - vd->WindowOwned = true; - viewport->PlatformHandle = (void*)vd->Window; -#ifdef _WIN32 - viewport->PlatformHandleRaw = glfwGetWin32Window(vd->Window); -#elif defined(__APPLE__) - viewport->PlatformHandleRaw = (void*)glfwGetCocoaWindow(vd->Window); -#endif - glfwSetWindowPos(vd->Window, (int)viewport->Pos.x, (int)viewport->Pos.y); - - // Install GLFW callbacks for secondary viewports - glfwSetWindowFocusCallback(vd->Window, ImGui_ImplGlfw_WindowFocusCallback); - glfwSetCursorEnterCallback(vd->Window, ImGui_ImplGlfw_CursorEnterCallback); - glfwSetCursorPosCallback(vd->Window, ImGui_ImplGlfw_CursorPosCallback); - glfwSetMouseButtonCallback(vd->Window, ImGui_ImplGlfw_MouseButtonCallback); - glfwSetScrollCallback(vd->Window, ImGui_ImplGlfw_ScrollCallback); - glfwSetKeyCallback(vd->Window, ImGui_ImplGlfw_KeyCallback); - glfwSetCharCallback(vd->Window, ImGui_ImplGlfw_CharCallback); - glfwSetWindowCloseCallback(vd->Window, ImGui_ImplGlfw_WindowCloseCallback); - glfwSetWindowPosCallback(vd->Window, ImGui_ImplGlfw_WindowPosCallback); - glfwSetWindowSizeCallback(vd->Window, ImGui_ImplGlfw_WindowSizeCallback); - if (bd->ClientApi == GlfwClientApi_OpenGL) - { - glfwMakeContextCurrent(vd->Window); - glfwSwapInterval(0); - } -} - -static void ImGui_ImplGlfw_DestroyWindow(ImGuiViewport* viewport) -{ - ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData(); - if (ImGui_ImplGlfw_ViewportData* vd = (ImGui_ImplGlfw_ViewportData*)viewport->PlatformUserData) - { - if (vd->WindowOwned) - { -#if !GLFW_HAS_MOUSE_PASSTHROUGH && GLFW_HAS_WINDOW_HOVERED && defined(_WIN32) - HWND hwnd = (HWND)viewport->PlatformHandleRaw; - ::RemovePropA(hwnd, "IMGUI_VIEWPORT"); -#endif - - // Release any keys that were pressed in the window being destroyed and are still held down, - // because we will not receive any release events after window is destroyed. - for (int i = 0; i < IM_ARRAYSIZE(bd->KeyOwnerWindows); i++) - if (bd->KeyOwnerWindows[i] == vd->Window) - ImGui_ImplGlfw_KeyCallback(vd->Window, i, 0, GLFW_RELEASE, 0); // Later params are only used for main viewport, on which this function is never called. - - glfwDestroyWindow(vd->Window); - } - vd->Window = nullptr; - IM_DELETE(vd); - } - viewport->PlatformUserData = viewport->PlatformHandle = nullptr; -} - -static void ImGui_ImplGlfw_ShowWindow(ImGuiViewport* viewport) -{ - ImGui_ImplGlfw_ViewportData* vd = (ImGui_ImplGlfw_ViewportData*)viewport->PlatformUserData; - -#if defined(_WIN32) - // GLFW hack: Hide icon from task bar - HWND hwnd = (HWND)viewport->PlatformHandleRaw; - if (viewport->Flags & ImGuiViewportFlags_NoTaskBarIcon) - { - LONG ex_style = ::GetWindowLong(hwnd, GWL_EXSTYLE); - ex_style &= ~WS_EX_APPWINDOW; - ex_style |= WS_EX_TOOLWINDOW; - ::SetWindowLong(hwnd, GWL_EXSTYLE, ex_style); - } - - // GLFW hack: install hook for WM_NCHITTEST message handler -#if !GLFW_HAS_MOUSE_PASSTHROUGH && GLFW_HAS_WINDOW_HOVERED && defined(_WIN32) - ::SetPropA(hwnd, "IMGUI_VIEWPORT", viewport); - vd->PrevWndProc = (WNDPROC)::GetWindowLongPtrW(hwnd, GWLP_WNDPROC); - ::SetWindowLongPtrW(hwnd, GWLP_WNDPROC, (LONG_PTR)ImGui_ImplGlfw_WndProc); -#endif - -#if !GLFW_HAS_FOCUS_ON_SHOW - // GLFW hack: GLFW 3.2 has a bug where glfwShowWindow() also activates/focus the window. - // The fix was pushed to GLFW repository on 2018/01/09 and should be included in GLFW 3.3 via a GLFW_FOCUS_ON_SHOW window attribute. - // See https://github.com/glfw/glfw/issues/1189 - // FIXME-VIEWPORT: Implement same work-around for Linux/OSX in the meanwhile. - if (viewport->Flags & ImGuiViewportFlags_NoFocusOnAppearing) - { - ::ShowWindow(hwnd, SW_SHOWNA); - return; - } -#endif -#endif - - glfwShowWindow(vd->Window); -} - -static ImVec2 ImGui_ImplGlfw_GetWindowPos(ImGuiViewport* viewport) -{ - ImGui_ImplGlfw_ViewportData* vd = (ImGui_ImplGlfw_ViewportData*)viewport->PlatformUserData; - int x = 0, y = 0; - glfwGetWindowPos(vd->Window, &x, &y); - return ImVec2((float)x, (float)y); -} - -static void ImGui_ImplGlfw_SetWindowPos(ImGuiViewport* viewport, ImVec2 pos) -{ - ImGui_ImplGlfw_ViewportData* vd = (ImGui_ImplGlfw_ViewportData*)viewport->PlatformUserData; - vd->IgnoreWindowPosEventFrame = ImGui::GetFrameCount(); - glfwSetWindowPos(vd->Window, (int)pos.x, (int)pos.y); -} - -static ImVec2 ImGui_ImplGlfw_GetWindowSize(ImGuiViewport* viewport) -{ - ImGui_ImplGlfw_ViewportData* vd = (ImGui_ImplGlfw_ViewportData*)viewport->PlatformUserData; - int w = 0, h = 0; - glfwGetWindowSize(vd->Window, &w, &h); - return ImVec2((float)w, (float)h); -} - -static void ImGui_ImplGlfw_SetWindowSize(ImGuiViewport* viewport, ImVec2 size) -{ - ImGui_ImplGlfw_ViewportData* vd = (ImGui_ImplGlfw_ViewportData*)viewport->PlatformUserData; -#if __APPLE__ && !GLFW_HAS_OSX_WINDOW_POS_FIX - // Native OS windows are positioned from the bottom-left corner on macOS, whereas on other platforms they are - // positioned from the upper-left corner. GLFW makes an effort to convert macOS style coordinates, however it - // doesn't handle it when changing size. We are manually moving the window in order for changes of size to be based - // on the upper-left corner. - int x, y, width, height; - glfwGetWindowPos(vd->Window, &x, &y); - glfwGetWindowSize(vd->Window, &width, &height); - glfwSetWindowPos(vd->Window, x, y - height + size.y); -#endif - vd->IgnoreWindowSizeEventFrame = ImGui::GetFrameCount(); - glfwSetWindowSize(vd->Window, (int)size.x, (int)size.y); -} - -static void ImGui_ImplGlfw_SetWindowTitle(ImGuiViewport* viewport, const char* title) -{ - ImGui_ImplGlfw_ViewportData* vd = (ImGui_ImplGlfw_ViewportData*)viewport->PlatformUserData; - glfwSetWindowTitle(vd->Window, title); -} - -static void ImGui_ImplGlfw_SetWindowFocus(ImGuiViewport* viewport) -{ -#if GLFW_HAS_FOCUS_WINDOW - ImGui_ImplGlfw_ViewportData* vd = (ImGui_ImplGlfw_ViewportData*)viewport->PlatformUserData; - glfwFocusWindow(vd->Window); -#else - // FIXME: What are the effect of not having this function? At the moment imgui doesn't actually call SetWindowFocus - we set that up ahead, will answer that question later. - (void)viewport; -#endif -} - -static bool ImGui_ImplGlfw_GetWindowFocus(ImGuiViewport* viewport) -{ - ImGui_ImplGlfw_ViewportData* vd = (ImGui_ImplGlfw_ViewportData*)viewport->PlatformUserData; - return glfwGetWindowAttrib(vd->Window, GLFW_FOCUSED) != 0; -} - -static bool ImGui_ImplGlfw_GetWindowMinimized(ImGuiViewport* viewport) -{ - ImGui_ImplGlfw_ViewportData* vd = (ImGui_ImplGlfw_ViewportData*)viewport->PlatformUserData; - return glfwGetWindowAttrib(vd->Window, GLFW_ICONIFIED) != 0; -} - -#if GLFW_HAS_WINDOW_ALPHA -static void ImGui_ImplGlfw_SetWindowAlpha(ImGuiViewport* viewport, float alpha) -{ - ImGui_ImplGlfw_ViewportData* vd = (ImGui_ImplGlfw_ViewportData*)viewport->PlatformUserData; - glfwSetWindowOpacity(vd->Window, alpha); -} -#endif - -static void ImGui_ImplGlfw_RenderWindow(ImGuiViewport* viewport, void*) -{ - ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData(); - ImGui_ImplGlfw_ViewportData* vd = (ImGui_ImplGlfw_ViewportData*)viewport->PlatformUserData; - if (bd->ClientApi == GlfwClientApi_OpenGL) - glfwMakeContextCurrent(vd->Window); -} - -static void ImGui_ImplGlfw_SwapBuffers(ImGuiViewport* viewport, void*) -{ - ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData(); - ImGui_ImplGlfw_ViewportData* vd = (ImGui_ImplGlfw_ViewportData*)viewport->PlatformUserData; - if (bd->ClientApi == GlfwClientApi_OpenGL) - { - glfwMakeContextCurrent(vd->Window); - glfwSwapBuffers(vd->Window); - } -} - -//-------------------------------------------------------------------------------------------------------- -// Vulkan support (the Vulkan renderer needs to call a platform-side support function to create the surface) -//-------------------------------------------------------------------------------------------------------- - -// Avoid including so we can build without it -#if GLFW_HAS_VULKAN -#ifndef VULKAN_H_ -#define VK_DEFINE_HANDLE(object) typedef struct object##_T* object; -#if defined(__LP64__) || defined(_WIN64) || defined(__x86_64__) || defined(_M_X64) || defined(__ia64) || defined (_M_IA64) || defined(__aarch64__) || defined(__powerpc64__) -#define VK_DEFINE_NON_DISPATCHABLE_HANDLE(object) typedef struct object##_T *object; -#else -#define VK_DEFINE_NON_DISPATCHABLE_HANDLE(object) typedef uint64_t object; -#endif -VK_DEFINE_HANDLE(VkInstance) -VK_DEFINE_NON_DISPATCHABLE_HANDLE(VkSurfaceKHR) -struct VkAllocationCallbacks; -enum VkResult { VK_RESULT_MAX_ENUM = 0x7FFFFFFF }; -#endif // VULKAN_H_ -extern "C" { extern GLFWAPI VkResult glfwCreateWindowSurface(VkInstance instance, GLFWwindow* window, const VkAllocationCallbacks* allocator, VkSurfaceKHR* surface); } -static int ImGui_ImplGlfw_CreateVkSurface(ImGuiViewport* viewport, ImU64 vk_instance, const void* vk_allocator, ImU64* out_vk_surface) -{ - ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData(); - ImGui_ImplGlfw_ViewportData* vd = (ImGui_ImplGlfw_ViewportData*)viewport->PlatformUserData; - IM_UNUSED(bd); - IM_ASSERT(bd->ClientApi == GlfwClientApi_Vulkan); - VkResult err = glfwCreateWindowSurface((VkInstance)vk_instance, vd->Window, (const VkAllocationCallbacks*)vk_allocator, (VkSurfaceKHR*)out_vk_surface); - return (int)err; -} -#endif // GLFW_HAS_VULKAN - -static void ImGui_ImplGlfw_InitPlatformInterface() -{ - // Register platform interface (will be coupled with a renderer interface) - ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData(); - ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO(); - platform_io.Platform_CreateWindow = ImGui_ImplGlfw_CreateWindow; - platform_io.Platform_DestroyWindow = ImGui_ImplGlfw_DestroyWindow; - platform_io.Platform_ShowWindow = ImGui_ImplGlfw_ShowWindow; - platform_io.Platform_SetWindowPos = ImGui_ImplGlfw_SetWindowPos; - platform_io.Platform_GetWindowPos = ImGui_ImplGlfw_GetWindowPos; - platform_io.Platform_SetWindowSize = ImGui_ImplGlfw_SetWindowSize; - platform_io.Platform_GetWindowSize = ImGui_ImplGlfw_GetWindowSize; - platform_io.Platform_SetWindowFocus = ImGui_ImplGlfw_SetWindowFocus; - platform_io.Platform_GetWindowFocus = ImGui_ImplGlfw_GetWindowFocus; - platform_io.Platform_GetWindowMinimized = ImGui_ImplGlfw_GetWindowMinimized; - platform_io.Platform_SetWindowTitle = ImGui_ImplGlfw_SetWindowTitle; - platform_io.Platform_RenderWindow = ImGui_ImplGlfw_RenderWindow; - platform_io.Platform_SwapBuffers = ImGui_ImplGlfw_SwapBuffers; -#if GLFW_HAS_WINDOW_ALPHA - platform_io.Platform_SetWindowAlpha = ImGui_ImplGlfw_SetWindowAlpha; -#endif -#if GLFW_HAS_VULKAN - platform_io.Platform_CreateVkSurface = ImGui_ImplGlfw_CreateVkSurface; -#endif - - // Register main window handle (which is owned by the main application, not by us) - // This is mostly for simplicity and consistency, so that our code (e.g. mouse handling etc.) can use same logic for main and secondary viewports. - ImGuiViewport* main_viewport = ImGui::GetMainViewport(); - ImGui_ImplGlfw_ViewportData* vd = IM_NEW(ImGui_ImplGlfw_ViewportData)(); - vd->Window = bd->Window; - vd->WindowOwned = false; - main_viewport->PlatformUserData = vd; - main_viewport->PlatformHandle = (void*)bd->Window; -} - -static void ImGui_ImplGlfw_ShutdownPlatformInterface() -{ - ImGui::DestroyPlatformWindows(); -} - -//----------------------------------------------------------------------------- - -// WndProc hook (declared here because we will need access to ImGui_ImplGlfw_ViewportData) -#ifdef _WIN32 -static ImGuiMouseSource GetMouseSourceFromMessageExtraInfo() -{ - LPARAM extra_info = ::GetMessageExtraInfo(); - if ((extra_info & 0xFFFFFF80) == 0xFF515700) - return ImGuiMouseSource_Pen; - if ((extra_info & 0xFFFFFF80) == 0xFF515780) - return ImGuiMouseSource_TouchScreen; - return ImGuiMouseSource_Mouse; -} -static LRESULT CALLBACK ImGui_ImplGlfw_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) -{ - ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData(); - WNDPROC prev_wndproc = bd->PrevWndProc; - ImGuiViewport* viewport = (ImGuiViewport*)::GetPropA(hWnd, "IMGUI_VIEWPORT"); - if (viewport != NULL) - if (ImGui_ImplGlfw_ViewportData* vd = (ImGui_ImplGlfw_ViewportData*)viewport->PlatformUserData) - prev_wndproc = vd->PrevWndProc; - - switch (msg) - { - // GLFW doesn't allow to distinguish Mouse vs TouchScreen vs Pen. - // Add support for Win32 (based on imgui_impl_win32), because we rely on _TouchScreen info to trickle inputs differently. - case WM_MOUSEMOVE: case WM_NCMOUSEMOVE: - case WM_LBUTTONDOWN: case WM_LBUTTONDBLCLK: case WM_LBUTTONUP: - case WM_RBUTTONDOWN: case WM_RBUTTONDBLCLK: case WM_RBUTTONUP: - case WM_MBUTTONDOWN: case WM_MBUTTONDBLCLK: case WM_MBUTTONUP: - case WM_XBUTTONDOWN: case WM_XBUTTONDBLCLK: case WM_XBUTTONUP: - ImGui::GetIO().AddMouseSourceEvent(GetMouseSourceFromMessageExtraInfo()); - break; - - // We have submitted https://github.com/glfw/glfw/pull/1568 to allow GLFW to support "transparent inputs". - // In the meanwhile we implement custom per-platform workarounds here (FIXME-VIEWPORT: Implement same work-around for Linux/OSX!) -#if !GLFW_HAS_MOUSE_PASSTHROUGH && GLFW_HAS_WINDOW_HOVERED - case WM_NCHITTEST: - { - // Let mouse pass-through the window. This will allow the backend to call io.AddMouseViewportEvent() properly (which is OPTIONAL). - // The ImGuiViewportFlags_NoInputs flag is set while dragging a viewport, as want to detect the window behind the one we are dragging. - // If you cannot easily access those viewport flags from your windowing/event code: you may manually synchronize its state e.g. in - // your main loop after calling UpdatePlatformWindows(). Iterate all viewports/platform windows and pass the flag to your windowing system. - if (viewport && (viewport->Flags & ImGuiViewportFlags_NoInputs)) - return HTTRANSPARENT; - break; - } -#endif - } - return ::CallWindowProcW(prev_wndproc, hWnd, msg, wParam, lParam); -} -#endif // #ifdef _WIN32 - -//----------------------------------------------------------------------------- - -#if defined(__clang__) -#pragma clang diagnostic pop -#endif - -#endif // #ifndef IMGUI_DISABLE diff --git a/src/app/imgui_impl_glfw.h b/src/app/imgui_impl_glfw.h deleted file mode 100644 index 14e6ee6a..00000000 --- a/src/app/imgui_impl_glfw.h +++ /dev/null @@ -1,63 +0,0 @@ -// dear imgui: Platform Backend for GLFW -// This needs to be used along with a Renderer (e.g. OpenGL3, Vulkan, WebGPU..) -// (Info: GLFW is a cross-platform general purpose library for handling windows, inputs, OpenGL/Vulkan graphics context creation, etc.) -// (Requires: GLFW 3.1+. Prefer GLFW 3.3+ for full feature support.) - -// Implemented features: -// [X] Platform: Clipboard support. -// [X] Platform: Mouse support. Can discriminate Mouse/TouchScreen/Pen (Windows only). -// [X] Platform: Keyboard support. Since 1.87 we are using the io.AddKeyEvent() function. Pass ImGuiKey values to all key functions e.g. ImGui::IsKeyPressed(ImGuiKey_Space). [Legacy GLFW_KEY_* values will also be supported unless IMGUI_DISABLE_OBSOLETE_KEYIO is set] -// [X] Platform: Gamepad support. Enable with 'io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad'. -// [x] Platform: Mouse cursor shape and visibility. Disable with 'io.ConfigFlags |= ImGuiConfigFlags_NoMouseCursorChange' (note: the resizing cursors requires GLFW 3.4+). -// [X] Platform: Multi-viewport support (multiple windows). Enable with 'io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable'. - -// Issues: -// [ ] Platform: Multi-viewport support: ParentViewportID not honored, and so io.ConfigViewportsNoDefaultParent has no effect (minor). - -// You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this. -// Prefer including the entire imgui/ repository into your project (either as a copy or as a submodule), and only build the backends you need. -// Learn about Dear ImGui: -// - FAQ https://dearimgui.com/faq -// - Getting Started https://dearimgui.com/getting-started -// - Documentation https://dearimgui.com/docs (same as your local docs/ folder). -// - Introduction, links and more at the top of imgui.cpp - -#pragma once -#include "imgui.h" // IMGUI_IMPL_API -#ifndef IMGUI_DISABLE - -struct GLFWwindow; -struct GLFWmonitor; - -IMGUI_IMPL_API bool ImGui_ImplGlfw_InitForOpenGL(GLFWwindow* window, bool install_callbacks); -IMGUI_IMPL_API bool ImGui_ImplGlfw_InitForVulkan(GLFWwindow* window, bool install_callbacks); -IMGUI_IMPL_API bool ImGui_ImplGlfw_InitForOther(GLFWwindow* window, bool install_callbacks); -IMGUI_IMPL_API void ImGui_ImplGlfw_Shutdown(); -IMGUI_IMPL_API void ImGui_ImplGlfw_NewFrame(); - -// Emscripten related initialization phase methods -#ifdef __EMSCRIPTEN__ -IMGUI_IMPL_API void ImGui_ImplGlfw_InstallEmscriptenCanvasResizeCallback(const char* canvas_selector); -#endif - -// GLFW callbacks install -// - When calling Init with 'install_callbacks=true': ImGui_ImplGlfw_InstallCallbacks() is called. GLFW callbacks will be installed for you. They will chain-call user's previously installed callbacks, if any. -// - When calling Init with 'install_callbacks=false': GLFW callbacks won't be installed. You will need to call individual function yourself from your own GLFW callbacks. -IMGUI_IMPL_API void ImGui_ImplGlfw_InstallCallbacks(GLFWwindow* window); -IMGUI_IMPL_API void ImGui_ImplGlfw_RestoreCallbacks(GLFWwindow* window); - -// GFLW callbacks options: -// - Set 'chain_for_all_windows=true' to enable chaining callbacks for all windows (including secondary viewports created by backends or by user) -IMGUI_IMPL_API void ImGui_ImplGlfw_SetCallbacksChainForAllWindows(bool chain_for_all_windows); - -// GLFW callbacks (individual callbacks to call yourself if you didn't install callbacks) -IMGUI_IMPL_API void ImGui_ImplGlfw_WindowFocusCallback(GLFWwindow* window, int focused); // Since 1.84 -IMGUI_IMPL_API void ImGui_ImplGlfw_CursorEnterCallback(GLFWwindow* window, int entered); // Since 1.84 -IMGUI_IMPL_API void ImGui_ImplGlfw_CursorPosCallback(GLFWwindow* window, double x, double y); // Since 1.87 -IMGUI_IMPL_API void ImGui_ImplGlfw_MouseButtonCallback(GLFWwindow* window, int button, int action, int mods); -IMGUI_IMPL_API void ImGui_ImplGlfw_ScrollCallback(GLFWwindow* window, double xoffset, double yoffset); -IMGUI_IMPL_API void ImGui_ImplGlfw_KeyCallback(GLFWwindow* window, int key, int scancode, int action, int mods); -IMGUI_IMPL_API void ImGui_ImplGlfw_CharCallback(GLFWwindow* window, unsigned int c); -IMGUI_IMPL_API void ImGui_ImplGlfw_MonitorCallback(GLFWmonitor* monitor, int event); - -#endif // #ifndef IMGUI_DISABLE diff --git a/src/app/imgui_impl_opengl3.cpp b/src/app/imgui_impl_opengl3.cpp index 4eea37f0..4ef9fb3d 100644 --- a/src/app/imgui_impl_opengl3.cpp +++ b/src/app/imgui_impl_opengl3.cpp @@ -4,8 +4,9 @@ // This needs to be used along with a Platform Backend (e.g. GLFW, SDL, Win32, custom..) // Implemented features: -// [X] Renderer: User texture binding. Use 'GLuint' OpenGL texture identifier as void*/ImTextureID. Read the FAQ about ImTextureID! -// [X] Renderer: Large meshes support (64k+ vertices) with 16-bit indices (Desktop OpenGL only). +// [X] Renderer: User texture binding. Use 'GLuint' OpenGL texture as texture identifier. Read the FAQ about ImTextureID/ImTextureRef! +// [x] Renderer: Large meshes support (64k+ vertices) even with 16-bit indices (ImGuiBackendFlags_RendererHasVtxOffset) [Desktop OpenGL only!] +// [X] Renderer: Texture updates support for dynamic font atlas (ImGuiBackendFlags_RendererHasTextures). // [X] Renderer: Multi-viewport support (multiple windows). Enable with 'io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable'. // About WebGL/ES: @@ -23,7 +24,17 @@ // CHANGELOG // (minor and older changes stripped away, please see git history for details) -// 2024-XX-XX: Platform: Added support for multiple windows via the ImGuiPlatformIO interface. +// 2025-XX-XX: Platform: Added support for multiple windows via the ImGuiPlatformIO interface. +// 2025-09-18: Call platform_io.ClearRendererHandlers() on shutdown. +// 2025-07-22: OpenGL: Add and call embedded loader shutdown during ImGui_ImplOpenGL3_Shutdown() to facilitate multiple init/shutdown cycles in same process. (#8792) +// 2025-07-15: OpenGL: Set GL_UNPACK_ALIGNMENT to 1 before updating textures (#8802) + restore non-WebGL/ES update path that doesn't require a CPU-side copy. +// 2025-06-11: OpenGL: Added support for ImGuiBackendFlags_RendererHasTextures, for dynamic font atlas. Removed ImGui_ImplOpenGL3_CreateFontsTexture() and ImGui_ImplOpenGL3_DestroyFontsTexture(). +// 2025-06-04: OpenGL: Made GLES 3.20 contexts not access GL_CONTEXT_PROFILE_MASK nor GL_PRIMITIVE_RESTART. (#8664) +// 2025-02-18: OpenGL: Lazily reinitialize embedded GL loader for when calling backend from e.g. other DLL boundaries. (#8406) +// 2024-10-07: OpenGL: Changed default texture sampler to Clamp instead of Repeat/Wrap. +// 2024-06-28: OpenGL: ImGui_ImplOpenGL3_NewFrame() recreates font texture if it has been destroyed by ImGui_ImplOpenGL3_DestroyFontsTexture(). (#7748) +// 2024-05-07: OpenGL: Update loader for Linux to support EGL/GLVND. (#7562) +// 2024-04-16: OpenGL: Detect ES3 contexts on desktop based on version string, to e.g. avoid calling glPolygonMode() on them. (#7447) // 2024-01-09: OpenGL: Update GL3W based imgui_impl_opengl3_loader.h to load "libGL.so" and variants, fixing regression on distros missing a symlink. // 2023-11-08: OpenGL: Update GL3W based imgui_impl_opengl3_loader.h to load "libGL.so" instead of "libGL.so.1", accommodating for NetBSD systems having only "libGL.so.3" available. (#6983) // 2023-10-05: OpenGL: Rename symbols in our internal loader so that LTO compilation with another copy of gl3w is possible. (#6875, #6668, #4445) @@ -51,7 +62,7 @@ // 2021-01-03: OpenGL: Backup, setup and restore GL_STENCIL_TEST state. // 2020-10-23: OpenGL: Backup, setup and restore GL_PRIMITIVE_RESTART state. // 2020-10-15: OpenGL: Use glGetString(GL_VERSION) instead of glGetIntegerv(GL_MAJOR_VERSION, ...) when the later returns zero (e.g. Desktop GL 2.x) -// 2020-09-17: OpenGL: Fix to avoid compiling/calling glBindSampler() on ES or pre 3.3 context which have the defines set by a loader. +// 2020-09-17: OpenGL: Fix to avoid compiling/calling glBindSampler() on ES or pre-3.3 context which have the defines set by a loader. // 2020-07-10: OpenGL: Added support for glad2 OpenGL loader. // 2020-05-08: OpenGL: Made default GLSL version 150 (instead of 130) on OSX. // 2020-04-21: OpenGL: Fixed handling of glClipControl(GL_UPPER_LEFT) by inverting projection matrix. @@ -120,12 +131,10 @@ #include #endif -#define IMGUI_IMPL_OPENGL_LOADER_CUSTOM -#include - // Clang/GCC warnings with -Weverything #if defined(__clang__) #pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wunknown-warning-option" // warning: ignore unknown flags #pragma clang diagnostic ignored "-Wold-style-cast" // warning: use of old-style cast #pragma clang diagnostic ignored "-Wsign-conversion" // warning: implicit conversion changes signedness #pragma clang diagnostic ignored "-Wunused-macros" // warning: macro is not used @@ -137,6 +146,7 @@ #pragma GCC diagnostic ignored "-Wpragmas" // warning: unknown option after '#pragma GCC diagnostic' kind #pragma GCC diagnostic ignored "-Wunknown-warning-option" // warning: unknown warning group 'xxx' #pragma GCC diagnostic ignored "-Wcast-function-type" // warning: cast between incompatible function types (for loader) +#pragma GCC diagnostic ignored "-Wstrict-overflow" // warning: assuming signed overflow does not occur when simplifying division / ..when changing X +- C1 cmp C2 to X cmp C2 -+ C1 #endif // GL includes @@ -164,9 +174,11 @@ // In the rest of your app/engine, you can use another loader of your choice (gl3w, glew, glad, glbinding, glext, glLoadGen, etc.). // If you happen to be developing a new feature for this backend (imgui_impl_opengl3.cpp): // - You may need to regenerate imgui_impl_opengl3_loader.h to add new symbols. See https://github.com/dearimgui/gl3w_stripped +// Typically you would run: python3 ./gl3w_gen.py --output ../imgui/backends/imgui_impl_opengl3_loader.h --ref ../imgui/backends/imgui_impl_opengl3.cpp ./extra_symbols.txt // - You can temporarily use an unstripped version. See https://github.com/dearimgui/gl3w_stripped/releases // Changes to this backend using new APIs should be accompanied by a regenerated stripped loader version. #define IMGL3W_IMPL +#define IMGUI_IMPL_OPENGL_LOADER_IMGL3W #include "imgui_impl_opengl3_loader.h" #endif @@ -182,9 +194,10 @@ #endif // Desktop GL 2.0+ has extension and glPolygonMode() which GL ES and WebGL don't have.. +// A desktop ES context can technically compile fine with our loader, so we also perform a runtime checks #if !defined(IMGUI_IMPL_OPENGL_ES2) && !defined(IMGUI_IMPL_OPENGL_ES3) #define IMGUI_IMPL_OPENGL_HAS_EXTENSIONS // has glGetIntegerv(GL_NUM_EXTENSIONS) -#define IMGUI_IMPL_OPENGL_HAS_POLYGON_MODE // has glPolygonMode() +#define IMGUI_IMPL_OPENGL_MAY_HAVE_POLYGON_MODE // may have glPolygonMode() #endif // Desktop GL 2.1+ and GL ES 3.0+ have glBindBuffer() with GL_PIXEL_UNPACK_BUFFER target. @@ -225,7 +238,7 @@ struct ImGui_ImplOpenGL3_Data bool GlProfileIsES3; bool GlProfileIsCompat; GLint GlProfileMask; - GLuint FontTexture; + GLint MaxTextureSize; GLuint ShaderHandle; GLint AttribLocationTex; // Uniforms location GLint AttribLocationProjMtx; @@ -235,8 +248,11 @@ struct ImGui_ImplOpenGL3_Data unsigned int VboHandle, ElementsHandle; GLsizeiptr VertexBufferSize; GLsizeiptr IndexBufferSize; + bool HasPolygonMode; + bool HasBindSampler; bool HasClipOrigin; bool UseBufferSubData; + ImVector TempBuffer; ImGui_ImplOpenGL3_Data() { memset((void*)this, 0, sizeof(*this)); } }; @@ -249,8 +265,8 @@ static ImGui_ImplOpenGL3_Data* ImGui_ImplOpenGL3_GetBackendData() } // Forward Declarations -static void ImGui_ImplOpenGL3_InitPlatformInterface(); -static void ImGui_ImplOpenGL3_ShutdownPlatformInterface(); +static void ImGui_ImplOpenGL3_InitMultiViewportSupport(); +static void ImGui_ImplOpenGL3_ShutdownMultiViewportSupport(); // OpenGL vertex attribute state (for ES 1.0 and ES 2.0 only) #ifndef IMGUI_IMPL_OPENGL_USE_VERTEX_ARRAY @@ -276,20 +292,31 @@ struct ImGui_ImplOpenGL3_VtxAttribState }; #endif -// Functions -bool ImGui_ImplOpenGL3_Init(const char* glsl_version) +// Not static to allow third-party code to use that if they want to (but undocumented) +bool ImGui_ImplOpenGL3_InitLoader(); +bool ImGui_ImplOpenGL3_InitLoader() { - ImGuiIO& io = ImGui::GetIO(); - IM_ASSERT(io.BackendRendererUserData == nullptr && "Already initialized a renderer backend!"); - // Initialize our loader -#if !defined(IMGUI_IMPL_OPENGL_ES2) && !defined(IMGUI_IMPL_OPENGL_ES3) && !defined(IMGUI_IMPL_OPENGL_LOADER_CUSTOM) - if (imgl3wInit() != 0) +#ifdef IMGUI_IMPL_OPENGL_LOADER_IMGL3W + if (glGetIntegerv == nullptr && imgl3wInit() != 0) { fprintf(stderr, "Failed to initialize OpenGL loader!\n"); return false; } #endif + return true; +} + +// Functions +bool ImGui_ImplOpenGL3_Init(const char* glsl_version) +{ + ImGuiIO& io = ImGui::GetIO(); + IMGUI_CHECKVERSION(); + IM_ASSERT(io.BackendRendererUserData == nullptr && "Already initialized a renderer backend!"); + + // Initialize loader + if (!ImGui_ImplOpenGL3_InitLoader()) + return false; // Setup backend capabilities flags ImGui_ImplOpenGL3_Data* bd = IM_NEW(ImGui_ImplOpenGL3_Data)(); @@ -297,10 +324,12 @@ bool ImGui_ImplOpenGL3_Init(const char* glsl_version) io.BackendRendererName = "imgui_impl_opengl3"; // Query for GL version (e.g. 320 for GL 3.2) + const char* gl_version_str = (const char*)glGetString(GL_VERSION); #if defined(IMGUI_IMPL_OPENGL_ES2) // GLES 2 bd->GlVersion = 200; bd->GlProfileIsES2 = true; + IM_UNUSED(gl_version_str); #else // Desktop or GLES 3 GLint major = 0; @@ -308,20 +337,21 @@ bool ImGui_ImplOpenGL3_Init(const char* glsl_version) glGetIntegerv(GL_MAJOR_VERSION, &major); glGetIntegerv(GL_MINOR_VERSION, &minor); if (major == 0 && minor == 0) - { - // Query GL_VERSION in desktop GL 2.x, the string will start with "." - const char* gl_version = (const char*)glGetString(GL_VERSION); - sscanf(gl_version, "%d.%d", &major, &minor); - } + sscanf(gl_version_str, "%d.%d", &major, &minor); // Query GL_VERSION in desktop GL 2.x, the string will start with "." bd->GlVersion = (GLuint)(major * 100 + minor * 10); -#if defined(GL_CONTEXT_PROFILE_MASK) - if (bd->GlVersion >= 320) - glGetIntegerv(GL_CONTEXT_PROFILE_MASK, &bd->GlProfileMask); - bd->GlProfileIsCompat = (bd->GlProfileMask & GL_CONTEXT_COMPATIBILITY_PROFILE_BIT) != 0; -#endif + glGetIntegerv(GL_MAX_TEXTURE_SIZE, &bd->MaxTextureSize); #if defined(IMGUI_IMPL_OPENGL_ES3) bd->GlProfileIsES3 = true; +#else + if (strncmp(gl_version_str, "OpenGL ES 3", 11) == 0) + bd->GlProfileIsES3 = true; +#endif + +#if defined(GL_CONTEXT_PROFILE_MASK) + if (!bd->GlProfileIsES3 && bd->GlVersion >= 320) + glGetIntegerv(GL_CONTEXT_PROFILE_MASK, &bd->GlProfileMask); + bd->GlProfileIsCompat = (bd->GlProfileMask & GL_CONTEXT_COMPATIBILITY_PROFILE_BIT) != 0; #endif bd->UseBufferSubData = false; @@ -336,14 +366,18 @@ bool ImGui_ImplOpenGL3_Init(const char* glsl_version) #endif #ifdef IMGUI_IMPL_OPENGL_DEBUG - printf("GlVersion = %d\nGlProfileIsCompat = %d\nGlProfileMask = 0x%X\nGlProfileIsES2 = %d, GlProfileIsES3 = %d\nGL_VENDOR = '%s'\nGL_RENDERER = '%s'\n", bd->GlVersion, bd->GlProfileIsCompat, bd->GlProfileMask, bd->GlProfileIsES2, bd->GlProfileIsES3, (const char*)glGetString(GL_VENDOR), (const char*)glGetString(GL_RENDERER)); // [DEBUG] + printf("GlVersion = %d, \"%s\"\nGlProfileIsCompat = %d\nGlProfileMask = 0x%X\nGlProfileIsES2/IsEs3 = %d/%d\nGL_VENDOR = '%s'\nGL_RENDERER = '%s'\n", bd->GlVersion, gl_version_str, bd->GlProfileIsCompat, bd->GlProfileMask, bd->GlProfileIsES2, bd->GlProfileIsES3, (const char*)glGetString(GL_VENDOR), (const char*)glGetString(GL_RENDERER)); // [DEBUG] #endif #ifdef IMGUI_IMPL_OPENGL_MAY_HAVE_VTX_OFFSET if (bd->GlVersion >= 320) io.BackendFlags |= ImGuiBackendFlags_RendererHasVtxOffset; // We can honor the ImDrawCmd::VtxOffset field, allowing for large meshes. #endif - io.BackendFlags |= ImGuiBackendFlags_RendererHasViewports; // We can create multi-viewports on the Renderer side (optional) + io.BackendFlags |= ImGuiBackendFlags_RendererHasTextures; // We can honor ImGuiPlatformIO::Textures[] requests during render. + io.BackendFlags |= ImGuiBackendFlags_RendererHasViewports; // We can create multi-viewports on the Renderer side (optional) + + ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO(); + platform_io.Renderer_TextureMaxWidth = platform_io.Renderer_TextureMaxHeight = (int)bd->MaxTextureSize; // Store GLSL version string so we can refer to it later in case we recreate shaders. // Note: GLSL version is NOT the same as GL version. Leave this to nullptr if unsure. @@ -369,6 +403,12 @@ bool ImGui_ImplOpenGL3_Init(const char* glsl_version) glGetIntegerv(GL_TEXTURE_BINDING_2D, ¤t_texture); // Detect extensions we support +#ifdef IMGUI_IMPL_OPENGL_MAY_HAVE_POLYGON_MODE + bd->HasPolygonMode = (!bd->GlProfileIsES2 && !bd->GlProfileIsES3); +#endif +#ifdef IMGUI_IMPL_OPENGL_MAY_HAVE_BIND_SAMPLER + bd->HasBindSampler = (bd->GlVersion >= 330 || bd->GlProfileIsES3); +#endif bd->HasClipOrigin = (bd->GlVersion >= 450); #ifdef IMGUI_IMPL_OPENGL_HAS_EXTENSIONS GLint num_extensions = 0; @@ -381,8 +421,7 @@ bool ImGui_ImplOpenGL3_Init(const char* glsl_version) } #endif - if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable) - ImGui_ImplOpenGL3_InitPlatformInterface(); + ImGui_ImplOpenGL3_InitMultiViewportSupport(); return true; } @@ -392,22 +431,32 @@ void ImGui_ImplOpenGL3_Shutdown() ImGui_ImplOpenGL3_Data* bd = ImGui_ImplOpenGL3_GetBackendData(); IM_ASSERT(bd != nullptr && "No renderer backend to shutdown, or already shutdown?"); ImGuiIO& io = ImGui::GetIO(); + ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO(); - ImGui_ImplOpenGL3_ShutdownPlatformInterface(); + ImGui_ImplOpenGL3_ShutdownMultiViewportSupport(); ImGui_ImplOpenGL3_DestroyDeviceObjects(); + io.BackendRendererName = nullptr; io.BackendRendererUserData = nullptr; - io.BackendFlags &= ~(ImGuiBackendFlags_RendererHasVtxOffset | ImGuiBackendFlags_RendererHasViewports); + io.BackendFlags &= ~(ImGuiBackendFlags_RendererHasVtxOffset | ImGuiBackendFlags_RendererHasTextures | ImGuiBackendFlags_RendererHasViewports); + platform_io.ClearRendererHandlers(); IM_DELETE(bd); + +#ifdef IMGUI_IMPL_OPENGL_LOADER_IMGL3W + imgl3wShutdown(); +#endif } void ImGui_ImplOpenGL3_NewFrame() { ImGui_ImplOpenGL3_Data* bd = ImGui_ImplOpenGL3_GetBackendData(); - IM_ASSERT(bd != nullptr && "Did you call ImGui_ImplOpenGL3_Init()?"); + IM_ASSERT(bd != nullptr && "Context or backend not initialized! Did you call ImGui_ImplOpenGL3_Init()?"); + + ImGui_ImplOpenGL3_InitLoader(); // Lazily init loader if not already done for e.g. DLL boundaries. if (!bd->ShaderHandle) - ImGui_ImplOpenGL3_CreateDeviceObjects(); + if (!ImGui_ImplOpenGL3_CreateDeviceObjects()) + IM_ASSERT(0 && "ImGui_ImplOpenGL3_CreateDeviceObjects() failed!"); } static void ImGui_ImplOpenGL3_SetupRenderState(ImDrawData* draw_data, int fb_width, int fb_height, GLuint vertex_array_object) @@ -423,11 +472,12 @@ static void ImGui_ImplOpenGL3_SetupRenderState(ImDrawData* draw_data, int fb_wid glDisable(GL_STENCIL_TEST); glEnable(GL_SCISSOR_TEST); #ifdef IMGUI_IMPL_OPENGL_MAY_HAVE_PRIMITIVE_RESTART - if (bd->GlVersion >= 310) + if (!bd->GlProfileIsES3 && bd->GlVersion >= 310) glDisable(GL_PRIMITIVE_RESTART); #endif -#ifdef IMGUI_IMPL_OPENGL_HAS_POLYGON_MODE - glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); +#ifdef IMGUI_IMPL_OPENGL_MAY_HAVE_POLYGON_MODE + if (bd->HasPolygonMode) + glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); #endif // Support for GL 4.5 rarely used glClipControl(GL_UPPER_LEFT) @@ -463,7 +513,7 @@ static void ImGui_ImplOpenGL3_SetupRenderState(ImDrawData* draw_data, int fb_wid glUniformMatrix4fv(bd->AttribLocationProjMtx, 1, GL_FALSE, &ortho_projection[0][0]); #ifdef IMGUI_IMPL_OPENGL_MAY_HAVE_BIND_SAMPLER - if (bd->GlVersion >= 330 || bd->GlProfileIsES3) + if (bd->HasBindSampler) glBindSampler(0, 0); // We use combined texture/sampler state. Applications using GL 3.3 and GL ES 3.0 may set that otherwise. #endif @@ -494,15 +544,24 @@ void ImGui_ImplOpenGL3_RenderDrawData(ImDrawData* draw_data) if (fb_width <= 0 || fb_height <= 0) return; + ImGui_ImplOpenGL3_InitLoader(); // Lazily init loader if not already done for e.g. DLL boundaries. + ImGui_ImplOpenGL3_Data* bd = ImGui_ImplOpenGL3_GetBackendData(); + // Catch up with texture updates. Most of the times, the list will have 1 element with an OK status, aka nothing to do. + // (This almost always points to ImGui::GetPlatformIO().Textures[] but is part of ImDrawData to allow overriding or disabling texture updates). + if (draw_data->Textures != nullptr) + for (ImTextureData* tex : *draw_data->Textures) + if (tex->Status != ImTextureStatus_OK) + ImGui_ImplOpenGL3_UpdateTexture(tex); + // Backup GL state GLenum last_active_texture; glGetIntegerv(GL_ACTIVE_TEXTURE, (GLint*)&last_active_texture); glActiveTexture(GL_TEXTURE0); GLuint last_program; glGetIntegerv(GL_CURRENT_PROGRAM, (GLint*)&last_program); GLuint last_texture; glGetIntegerv(GL_TEXTURE_BINDING_2D, (GLint*)&last_texture); #ifdef IMGUI_IMPL_OPENGL_MAY_HAVE_BIND_SAMPLER - GLuint last_sampler; if (bd->GlVersion >= 330 || bd->GlProfileIsES3) { glGetIntegerv(GL_SAMPLER_BINDING, (GLint*)&last_sampler); } else { last_sampler = 0; } + GLuint last_sampler; if (bd->HasBindSampler) { glGetIntegerv(GL_SAMPLER_BINDING, (GLint*)&last_sampler); } else { last_sampler = 0; } #endif GLuint last_array_buffer; glGetIntegerv(GL_ARRAY_BUFFER_BINDING, (GLint*)&last_array_buffer); #ifndef IMGUI_IMPL_OPENGL_USE_VERTEX_ARRAY @@ -515,8 +574,8 @@ void ImGui_ImplOpenGL3_RenderDrawData(ImDrawData* draw_data) #ifdef IMGUI_IMPL_OPENGL_USE_VERTEX_ARRAY GLuint last_vertex_array_object; glGetIntegerv(GL_VERTEX_ARRAY_BINDING, (GLint*)&last_vertex_array_object); #endif -#ifdef IMGUI_IMPL_OPENGL_HAS_POLYGON_MODE - GLint last_polygon_mode[2]; glGetIntegerv(GL_POLYGON_MODE, last_polygon_mode); +#ifdef IMGUI_IMPL_OPENGL_MAY_HAVE_POLYGON_MODE + GLint last_polygon_mode[2]; if (bd->HasPolygonMode) { glGetIntegerv(GL_POLYGON_MODE, last_polygon_mode); } #endif GLint last_viewport[4]; glGetIntegerv(GL_VIEWPORT, last_viewport); GLint last_scissor_box[4]; glGetIntegerv(GL_SCISSOR_BOX, last_scissor_box); @@ -532,7 +591,7 @@ void ImGui_ImplOpenGL3_RenderDrawData(ImDrawData* draw_data) GLboolean last_enable_stencil_test = glIsEnabled(GL_STENCIL_TEST); GLboolean last_enable_scissor_test = glIsEnabled(GL_SCISSOR_TEST); #ifdef IMGUI_IMPL_OPENGL_MAY_HAVE_PRIMITIVE_RESTART - GLboolean last_enable_primitive_restart = (bd->GlVersion >= 310) ? glIsEnabled(GL_PRIMITIVE_RESTART) : GL_FALSE; + GLboolean last_enable_primitive_restart = (!bd->GlProfileIsES3 && bd->GlVersion >= 310) ? glIsEnabled(GL_PRIMITIVE_RESTART) : GL_FALSE; #endif // Setup desired GL state @@ -549,10 +608,8 @@ void ImGui_ImplOpenGL3_RenderDrawData(ImDrawData* draw_data) ImVec2 clip_scale = draw_data->FramebufferScale; // (1,1) unless using retina display which are often (2,2) // Render command lists - for (int n = 0; n < draw_data->CmdListsCount; n++) + for (const ImDrawList* draw_list : draw_data->CmdLists) { - const ImDrawList* cmd_list = draw_data->CmdLists[n]; - // Upload vertex/index buffers // - OpenGL drivers are in a very sorry state nowadays.... // During 2021 we attempted to switch from glBufferData() to orphaning+glBufferSubData() following reports @@ -561,8 +618,8 @@ void ImGui_ImplOpenGL3_RenderDrawData(ImDrawData* draw_data) // - We are now back to using exclusively glBufferData(). So bd->UseBufferSubData IS ALWAYS FALSE in this code. // We are keeping the old code path for a while in case people finding new issues may want to test the bd->UseBufferSubData path. // - See https://github.com/ocornut/imgui/issues/4468 and please report any corruption issues. - const GLsizeiptr vtx_buffer_size = (GLsizeiptr)cmd_list->VtxBuffer.Size * (int)sizeof(ImDrawVert); - const GLsizeiptr idx_buffer_size = (GLsizeiptr)cmd_list->IdxBuffer.Size * (int)sizeof(ImDrawIdx); + const GLsizeiptr vtx_buffer_size = (GLsizeiptr)draw_list->VtxBuffer.Size * (int)sizeof(ImDrawVert); + const GLsizeiptr idx_buffer_size = (GLsizeiptr)draw_list->IdxBuffer.Size * (int)sizeof(ImDrawIdx); if (bd->UseBufferSubData) { if (bd->VertexBufferSize < vtx_buffer_size) @@ -575,18 +632,18 @@ void ImGui_ImplOpenGL3_RenderDrawData(ImDrawData* draw_data) bd->IndexBufferSize = idx_buffer_size; GL_CALL(glBufferData(GL_ELEMENT_ARRAY_BUFFER, bd->IndexBufferSize, nullptr, GL_STREAM_DRAW)); } - GL_CALL(glBufferSubData(GL_ARRAY_BUFFER, 0, vtx_buffer_size, (const GLvoid*)cmd_list->VtxBuffer.Data)); - GL_CALL(glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, idx_buffer_size, (const GLvoid*)cmd_list->IdxBuffer.Data)); + GL_CALL(glBufferSubData(GL_ARRAY_BUFFER, 0, vtx_buffer_size, (const GLvoid*)draw_list->VtxBuffer.Data)); + GL_CALL(glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, idx_buffer_size, (const GLvoid*)draw_list->IdxBuffer.Data)); } else { - GL_CALL(glBufferData(GL_ARRAY_BUFFER, vtx_buffer_size, (const GLvoid*)cmd_list->VtxBuffer.Data, GL_STREAM_DRAW)); - GL_CALL(glBufferData(GL_ELEMENT_ARRAY_BUFFER, idx_buffer_size, (const GLvoid*)cmd_list->IdxBuffer.Data, GL_STREAM_DRAW)); + GL_CALL(glBufferData(GL_ARRAY_BUFFER, vtx_buffer_size, (const GLvoid*)draw_list->VtxBuffer.Data, GL_STREAM_DRAW)); + GL_CALL(glBufferData(GL_ELEMENT_ARRAY_BUFFER, idx_buffer_size, (const GLvoid*)draw_list->IdxBuffer.Data, GL_STREAM_DRAW)); } - for (int cmd_i = 0; cmd_i < cmd_list->CmdBuffer.Size; cmd_i++) + for (int cmd_i = 0; cmd_i < draw_list->CmdBuffer.Size; cmd_i++) { - const ImDrawCmd* pcmd = &cmd_list->CmdBuffer[cmd_i]; + const ImDrawCmd* pcmd = &draw_list->CmdBuffer[cmd_i]; if (pcmd->UserCallback != nullptr) { // User callback, registered via ImDrawList::AddCallback() @@ -594,7 +651,7 @@ void ImGui_ImplOpenGL3_RenderDrawData(ImDrawData* draw_data) if (pcmd->UserCallback == ImDrawCallback_ResetRenderState) ImGui_ImplOpenGL3_SetupRenderState(draw_data, fb_width, fb_height, vertex_array_object); else - pcmd->UserCallback(cmd_list, pcmd); + pcmd->UserCallback(draw_list, pcmd); } else { @@ -629,7 +686,7 @@ void ImGui_ImplOpenGL3_RenderDrawData(ImDrawData* draw_data) if (last_program == 0 || glIsProgram(last_program)) glUseProgram(last_program); glBindTexture(GL_TEXTURE_2D, last_texture); #ifdef IMGUI_IMPL_OPENGL_MAY_HAVE_BIND_SAMPLER - if (bd->GlVersion >= 330 || bd->GlProfileIsES3) + if (bd->HasBindSampler) glBindSampler(0, last_sampler); #endif glActiveTexture(last_active_texture); @@ -651,69 +708,103 @@ void ImGui_ImplOpenGL3_RenderDrawData(ImDrawData* draw_data) if (last_enable_stencil_test) glEnable(GL_STENCIL_TEST); else glDisable(GL_STENCIL_TEST); if (last_enable_scissor_test) glEnable(GL_SCISSOR_TEST); else glDisable(GL_SCISSOR_TEST); #ifdef IMGUI_IMPL_OPENGL_MAY_HAVE_PRIMITIVE_RESTART - if (bd->GlVersion >= 310) { if (last_enable_primitive_restart) glEnable(GL_PRIMITIVE_RESTART); else glDisable(GL_PRIMITIVE_RESTART); } + if (!bd->GlProfileIsES3 && bd->GlVersion >= 310) { if (last_enable_primitive_restart) glEnable(GL_PRIMITIVE_RESTART); else glDisable(GL_PRIMITIVE_RESTART); } #endif -#ifdef IMGUI_IMPL_OPENGL_HAS_POLYGON_MODE +#ifdef IMGUI_IMPL_OPENGL_MAY_HAVE_POLYGON_MODE // Desktop OpenGL 3.0 and OpenGL 3.1 had separate polygon draw modes for front-facing and back-facing faces of polygons - if (bd->GlVersion <= 310 || bd->GlProfileIsCompat) - { - glPolygonMode(GL_FRONT, (GLenum)last_polygon_mode[0]); - glPolygonMode(GL_BACK, (GLenum)last_polygon_mode[1]); - } - else - { - glPolygonMode(GL_FRONT_AND_BACK, (GLenum)last_polygon_mode[0]); - } -#endif // IMGUI_IMPL_OPENGL_HAS_POLYGON_MODE + if (bd->HasPolygonMode) { if (bd->GlVersion <= 310 || bd->GlProfileIsCompat) { glPolygonMode(GL_FRONT, (GLenum)last_polygon_mode[0]); glPolygonMode(GL_BACK, (GLenum)last_polygon_mode[1]); } else { glPolygonMode(GL_FRONT_AND_BACK, (GLenum)last_polygon_mode[0]); } } +#endif // IMGUI_IMPL_OPENGL_MAY_HAVE_POLYGON_MODE glViewport(last_viewport[0], last_viewport[1], (GLsizei)last_viewport[2], (GLsizei)last_viewport[3]); glScissor(last_scissor_box[0], last_scissor_box[1], (GLsizei)last_scissor_box[2], (GLsizei)last_scissor_box[3]); (void)bd; // Not all compilation paths use this } -bool ImGui_ImplOpenGL3_CreateFontsTexture() +static void ImGui_ImplOpenGL3_DestroyTexture(ImTextureData* tex) { - ImGuiIO& io = ImGui::GetIO(); - ImGui_ImplOpenGL3_Data* bd = ImGui_ImplOpenGL3_GetBackendData(); - - // Build texture atlas - unsigned char* pixels; - int width, height; - io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height); // Load as RGBA 32-bit (75% of the memory is wasted, but default font is so small) because it is more likely to be compatible with user's existing shaders. If your ImTextureId represent a higher-level concept than just a GL texture id, consider calling GetTexDataAsAlpha8() instead to save on GPU memory. - - // Upload texture to graphics system - // (Bilinear sampling is required by default. Set 'io.Fonts->Flags |= ImFontAtlasFlags_NoBakedLines' or 'style.AntiAliasedLinesUseTex = false' to allow point/nearest sampling) - GLint last_texture; - GL_CALL(glGetIntegerv(GL_TEXTURE_BINDING_2D, &last_texture)); - GL_CALL(glGenTextures(1, &bd->FontTexture)); - GL_CALL(glBindTexture(GL_TEXTURE_2D, bd->FontTexture)); - GL_CALL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)); - GL_CALL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)); -#ifdef GL_UNPACK_ROW_LENGTH // Not on WebGL/ES - GL_CALL(glPixelStorei(GL_UNPACK_ROW_LENGTH, 0)); -#endif - GL_CALL(glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels)); + GLuint gl_tex_id = (GLuint)(intptr_t)tex->TexID; + glDeleteTextures(1, &gl_tex_id); - // Store our identifier - io.Fonts->SetTexID((ImTextureID)(intptr_t)bd->FontTexture); - - // Restore state - GL_CALL(glBindTexture(GL_TEXTURE_2D, last_texture)); - - return true; + // Clear identifiers and mark as destroyed (in order to allow e.g. calling InvalidateDeviceObjects while running) + tex->SetTexID(ImTextureID_Invalid); + tex->SetStatus(ImTextureStatus_Destroyed); } -void ImGui_ImplOpenGL3_DestroyFontsTexture() +void ImGui_ImplOpenGL3_UpdateTexture(ImTextureData* tex) { - ImGuiIO& io = ImGui::GetIO(); - ImGui_ImplOpenGL3_Data* bd = ImGui_ImplOpenGL3_GetBackendData(); - if (bd->FontTexture) + // FIXME: Consider backing up and restoring + if (tex->Status == ImTextureStatus_WantCreate || tex->Status == ImTextureStatus_WantUpdates) + { +#ifdef GL_UNPACK_ROW_LENGTH // Not on WebGL/ES + GL_CALL(glPixelStorei(GL_UNPACK_ROW_LENGTH, 0)); +#endif +#ifdef GL_UNPACK_ALIGNMENT + GL_CALL(glPixelStorei(GL_UNPACK_ALIGNMENT, 1)); +#endif + } + + if (tex->Status == ImTextureStatus_WantCreate) + { + // Create and upload new texture to graphics system + //IMGUI_DEBUG_LOG("UpdateTexture #%03d: WantCreate %dx%d\n", tex->UniqueID, tex->Width, tex->Height); + IM_ASSERT(tex->TexID == 0 && tex->BackendUserData == nullptr); + IM_ASSERT(tex->Format == ImTextureFormat_RGBA32); + const void* pixels = tex->GetPixels(); + GLuint gl_texture_id = 0; + + // Upload texture to graphics system + // (Bilinear sampling is required by default. Set 'io.Fonts->Flags |= ImFontAtlasFlags_NoBakedLines' or 'style.AntiAliasedLinesUseTex = false' to allow point/nearest sampling) + GLint last_texture; + GL_CALL(glGetIntegerv(GL_TEXTURE_BINDING_2D, &last_texture)); + GL_CALL(glGenTextures(1, &gl_texture_id)); + GL_CALL(glBindTexture(GL_TEXTURE_2D, gl_texture_id)); + GL_CALL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)); + GL_CALL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)); + GL_CALL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)); + GL_CALL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)); + GL_CALL(glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex->Width, tex->Height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels)); + + // Store identifiers + tex->SetTexID((ImTextureID)(intptr_t)gl_texture_id); + tex->SetStatus(ImTextureStatus_OK); + + // Restore state + GL_CALL(glBindTexture(GL_TEXTURE_2D, last_texture)); + } + else if (tex->Status == ImTextureStatus_WantUpdates) { - glDeleteTextures(1, &bd->FontTexture); - io.Fonts->SetTexID(0); - bd->FontTexture = 0; + // Update selected blocks. We only ever write to textures regions which have never been used before! + // This backend choose to use tex->Updates[] but you can use tex->UpdateRect to upload a single region. + GLint last_texture; + GL_CALL(glGetIntegerv(GL_TEXTURE_BINDING_2D, &last_texture)); + + GLuint gl_tex_id = (GLuint)(intptr_t)tex->TexID; + GL_CALL(glBindTexture(GL_TEXTURE_2D, gl_tex_id)); +#if GL_UNPACK_ROW_LENGTH // Not on WebGL/ES + GL_CALL(glPixelStorei(GL_UNPACK_ROW_LENGTH, tex->Width)); + for (ImTextureRect& r : tex->Updates) + GL_CALL(glTexSubImage2D(GL_TEXTURE_2D, 0, r.x, r.y, r.w, r.h, GL_RGBA, GL_UNSIGNED_BYTE, tex->GetPixelsAt(r.x, r.y))); + GL_CALL(glPixelStorei(GL_UNPACK_ROW_LENGTH, 0)); +#else + // GL ES doesn't have GL_UNPACK_ROW_LENGTH, so we need to (A) copy to a contiguous buffer or (B) upload line by line. + ImGui_ImplOpenGL3_Data* bd = ImGui_ImplOpenGL3_GetBackendData(); + for (ImTextureRect& r : tex->Updates) + { + const int src_pitch = r.w * tex->BytesPerPixel; + bd->TempBuffer.resize(r.h * src_pitch); + char* out_p = bd->TempBuffer.Data; + for (int y = 0; y < r.h; y++, out_p += src_pitch) + memcpy(out_p, tex->GetPixelsAt(r.x, r.y + y), src_pitch); + IM_ASSERT(out_p == bd->TempBuffer.end()); + GL_CALL(glTexSubImage2D(GL_TEXTURE_2D, 0, r.x, r.y, r.w, r.h, GL_RGBA, GL_UNSIGNED_BYTE, bd->TempBuffer.Data)); + } +#endif + tex->SetStatus(ImTextureStatus_OK); + GL_CALL(glBindTexture(GL_TEXTURE_2D, last_texture)); // Restore state } + else if (tex->Status == ImTextureStatus_WantDestroy && tex->UnusedFrames > 0) + ImGui_ImplOpenGL3_DestroyTexture(tex); } // If you get an error please report on github. You may try different GL context version or GLSL version. See GL<>GLSL version table at the top of this file. @@ -763,7 +854,7 @@ bool ImGui_ImplOpenGL3_CreateDeviceObjects() glGetIntegerv(GL_TEXTURE_BINDING_2D, &last_texture); glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &last_array_buffer); #ifdef IMGUI_IMPL_OPENGL_MAY_HAVE_BIND_BUFFER_PIXEL_UNPACK - GLint last_pixel_unpack_buffer; + GLint last_pixel_unpack_buffer = 0; if (bd->GlVersion >= 210) { glGetIntegerv(GL_PIXEL_UNPACK_BUFFER_BINDING, &last_pixel_unpack_buffer); glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0); } #endif #ifdef IMGUI_IMPL_OPENGL_USE_VERTEX_ARRAY @@ -901,23 +992,28 @@ bool ImGui_ImplOpenGL3_CreateDeviceObjects() // Create shaders const GLchar* vertex_shader_with_version[2] = { bd->GlslVersionString, vertex_shader }; - GLuint vert_handle = glCreateShader(GL_VERTEX_SHADER); + GLuint vert_handle; + GL_CALL(vert_handle = glCreateShader(GL_VERTEX_SHADER)); glShaderSource(vert_handle, 2, vertex_shader_with_version, nullptr); glCompileShader(vert_handle); - CheckShader(vert_handle, "vertex shader"); + if (!CheckShader(vert_handle, "vertex shader")) + return false; const GLchar* fragment_shader_with_version[2] = { bd->GlslVersionString, fragment_shader }; - GLuint frag_handle = glCreateShader(GL_FRAGMENT_SHADER); + GLuint frag_handle; + GL_CALL(frag_handle = glCreateShader(GL_FRAGMENT_SHADER)); glShaderSource(frag_handle, 2, fragment_shader_with_version, nullptr); glCompileShader(frag_handle); - CheckShader(frag_handle, "fragment shader"); + if (!CheckShader(frag_handle, "fragment shader")) + return false; // Link bd->ShaderHandle = glCreateProgram(); glAttachShader(bd->ShaderHandle, vert_handle); glAttachShader(bd->ShaderHandle, frag_handle); glLinkProgram(bd->ShaderHandle); - CheckProgram(bd->ShaderHandle, "shader program"); + if (!CheckProgram(bd->ShaderHandle, "shader program")) + return false; glDetachShader(bd->ShaderHandle, vert_handle); glDetachShader(bd->ShaderHandle, frag_handle); @@ -934,8 +1030,6 @@ bool ImGui_ImplOpenGL3_CreateDeviceObjects() glGenBuffers(1, &bd->VboHandle); glGenBuffers(1, &bd->ElementsHandle); - ImGui_ImplOpenGL3_CreateFontsTexture(); - // Restore modified GL state glBindTexture(GL_TEXTURE_2D, last_texture); glBindBuffer(GL_ARRAY_BUFFER, last_array_buffer); @@ -955,7 +1049,11 @@ void ImGui_ImplOpenGL3_DestroyDeviceObjects() if (bd->VboHandle) { glDeleteBuffers(1, &bd->VboHandle); bd->VboHandle = 0; } if (bd->ElementsHandle) { glDeleteBuffers(1, &bd->ElementsHandle); bd->ElementsHandle = 0; } if (bd->ShaderHandle) { glDeleteProgram(bd->ShaderHandle); bd->ShaderHandle = 0; } - ImGui_ImplOpenGL3_DestroyFontsTexture(); + + // Destroy all textures + for (ImTextureData* tex : ImGui::GetPlatformIO().Textures) + if (tex->RefCount == 1) + ImGui_ImplOpenGL3_DestroyTexture(tex); } //-------------------------------------------------------------------------------------------------------- @@ -975,13 +1073,13 @@ static void ImGui_ImplOpenGL3_RenderWindow(ImGuiViewport* viewport, void*) ImGui_ImplOpenGL3_RenderDrawData(viewport->DrawData); } -static void ImGui_ImplOpenGL3_InitPlatformInterface() +static void ImGui_ImplOpenGL3_InitMultiViewportSupport() { ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO(); platform_io.Renderer_RenderWindow = ImGui_ImplOpenGL3_RenderWindow; } -static void ImGui_ImplOpenGL3_ShutdownPlatformInterface() +static void ImGui_ImplOpenGL3_ShutdownMultiViewportSupport() { ImGui::DestroyPlatformWindows(); } diff --git a/src/app/imgui_impl_opengl3.h b/src/app/imgui_impl_opengl3.h index ab779e07..9495d4e9 100644 --- a/src/app/imgui_impl_opengl3.h +++ b/src/app/imgui_impl_opengl3.h @@ -4,8 +4,9 @@ // This needs to be used along with a Platform Backend (e.g. GLFW, SDL, Win32, custom..) // Implemented features: -// [X] Renderer: User texture binding. Use 'GLuint' OpenGL texture identifier as void*/ImTextureID. Read the FAQ about ImTextureID! -// [X] Renderer: Large meshes support (64k+ vertices) with 16-bit indices (Desktop OpenGL only). +// [X] Renderer: User texture binding. Use 'GLuint' OpenGL texture as texture identifier. Read the FAQ about ImTextureID/ImTextureRef! +// [x] Renderer: Large meshes support (64k+ vertices) even with 16-bit indices (ImGuiBackendFlags_RendererHasVtxOffset) [Desktop OpenGL only!] +// [X] Renderer: Texture updates support for dynamic font atlas (ImGuiBackendFlags_RendererHasTextures). // [X] Renderer: Multi-viewport support (multiple windows). Enable with 'io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable'. // About WebGL/ES: @@ -30,21 +31,22 @@ #include "imgui.h" // IMGUI_IMPL_API #ifndef IMGUI_DISABLE -// Backend API +// Follow "Getting Started" link and check examples/ folder to learn about using backends! IMGUI_IMPL_API bool ImGui_ImplOpenGL3_Init(const char* glsl_version = nullptr); IMGUI_IMPL_API void ImGui_ImplOpenGL3_Shutdown(); IMGUI_IMPL_API void ImGui_ImplOpenGL3_NewFrame(); IMGUI_IMPL_API void ImGui_ImplOpenGL3_RenderDrawData(ImDrawData* draw_data); // (Optional) Called by Init/NewFrame/Shutdown -IMGUI_IMPL_API bool ImGui_ImplOpenGL3_CreateFontsTexture(); -IMGUI_IMPL_API void ImGui_ImplOpenGL3_DestroyFontsTexture(); IMGUI_IMPL_API bool ImGui_ImplOpenGL3_CreateDeviceObjects(); IMGUI_IMPL_API void ImGui_ImplOpenGL3_DestroyDeviceObjects(); -// Specific OpenGL ES versions -//#define IMGUI_IMPL_OPENGL_ES2 // Auto-detected on Emscripten -//#define IMGUI_IMPL_OPENGL_ES3 // Auto-detected on iOS/Android +// (Advanced) Use e.g. if you need to precisely control the timing of texture updates (e.g. for staged rendering), by setting ImDrawData::Textures = NULL to handle this manually. +IMGUI_IMPL_API void ImGui_ImplOpenGL3_UpdateTexture(ImTextureData* tex); + +// Configuration flags to add in your imconfig file: +//#define IMGUI_IMPL_OPENGL_ES2 // Enable ES 2 (Auto-detected on Emscripten) +//#define IMGUI_IMPL_OPENGL_ES3 // Enable ES 3 (Auto-detected on iOS/Android) // You can explicitly select GLES2 or GLES3 API by using one of the '#define IMGUI_IMPL_OPENGL_LOADER_XXX' in imconfig.h or compiler command-line. #if !defined(IMGUI_IMPL_OPENGL_ES2) \ diff --git a/src/app/imgui_impl_opengl3_loader.h b/src/app/imgui_impl_opengl3_loader.h index 85c58c4e..2c80cc59 100644 --- a/src/app/imgui_impl_opengl3_loader.h +++ b/src/app/imgui_impl_opengl3_loader.h @@ -10,7 +10,7 @@ // THE REST OF YOUR APP SHOULD USE A DIFFERENT GL LOADER: ANY GL LOADER OF YOUR CHOICE. // // IF YOU GET BUILD ERRORS IN THIS FILE (commonly macro redefinitions or function redefinitions): -// IT LIKELY MEANS THAT YOU ARE BUILDING 'imgui_impl_opengl3.cpp' OR INCUDING 'imgui_impl_opengl3_loader.h' +// IT LIKELY MEANS THAT YOU ARE BUILDING 'imgui_impl_opengl3.cpp' OR INCLUDING 'imgui_impl_opengl3_loader.h' // IN THE SAME COMPILATION UNIT AS ONE OF YOUR FILE WHICH IS USING A THIRD-PARTY OPENGL LOADER. // (e.g. COULD HAPPEN IF YOU ARE DOING A UNITY/JUMBO BUILD, OR INCLUDING .CPP FILES FROM OTHERS) // YOU SHOULD NOT BUILD BOTH IN THE SAME COMPILATION UNIT. @@ -18,7 +18,7 @@ // WILL NOT BE USING OUR LOADER, AND INSTEAD EXPECT ANOTHER/YOUR LOADER TO BE AVAILABLE IN THE COMPILATION UNIT. // // Regenerate with: -// python gl3w_gen.py --output ../imgui/backends/imgui_impl_opengl3_loader.h --ref ../imgui/backends/imgui_impl_opengl3.cpp ./extra_symbols.txt +// python3 gl3w_gen.py --output ../imgui/backends/imgui_impl_opengl3_loader.h --ref ../imgui/backends/imgui_impl_opengl3.cpp ./extra_symbols.txt // // More info: // https://github.com/dearimgui/gl3w_stripped @@ -118,7 +118,7 @@ extern "C" { ** included as . ** ** glcorearb.h includes only APIs in the latest OpenGL core profile -** implementation together with APIs in newer ARB extensions which +** implementation together with APIs in newer ARB extensions which ** can be supported by the core profile. It does not, and never will ** include functionality removed from the core profile, such as ** fixed-function vertex and fragment processing. @@ -166,7 +166,9 @@ typedef khronos_uint8_t GLubyte; #define GL_SCISSOR_BOX 0x0C10 #define GL_SCISSOR_TEST 0x0C11 #define GL_UNPACK_ROW_LENGTH 0x0CF2 +#define GL_UNPACK_ALIGNMENT 0x0CF5 #define GL_PACK_ALIGNMENT 0x0D05 +#define GL_MAX_TEXTURE_SIZE 0x0D33 #define GL_TEXTURE_2D 0x0DE1 #define GL_UNSIGNED_BYTE 0x1401 #define GL_UNSIGNED_SHORT 0x1403 @@ -178,9 +180,13 @@ typedef khronos_uint8_t GLubyte; #define GL_RENDERER 0x1F01 #define GL_VERSION 0x1F02 #define GL_EXTENSIONS 0x1F03 +#define GL_NEAREST 0x2600 #define GL_LINEAR 0x2601 #define GL_TEXTURE_MAG_FILTER 0x2800 #define GL_TEXTURE_MIN_FILTER 0x2801 +#define GL_TEXTURE_WRAP_S 0x2802 +#define GL_TEXTURE_WRAP_T 0x2803 +#define GL_REPEAT 0x2901 typedef void (APIENTRYP PFNGLPOLYGONMODEPROC) (GLenum face, GLenum mode); typedef void (APIENTRYP PFNGLSCISSORPROC) (GLint x, GLint y, GLsizei width, GLsizei height); typedef void (APIENTRYP PFNGLTEXPARAMETERIPROC) (GLenum target, GLenum pname, GLint param); @@ -221,16 +227,21 @@ typedef khronos_float_t GLclampf; typedef double GLclampd; #define GL_TEXTURE_BINDING_2D 0x8069 typedef void (APIENTRYP PFNGLDRAWELEMENTSPROC) (GLenum mode, GLsizei count, GLenum type, const void *indices); +typedef void (APIENTRYP PFNGLTEXSUBIMAGE2DPROC) (GLenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, const void *pixels); typedef void (APIENTRYP PFNGLBINDTEXTUREPROC) (GLenum target, GLuint texture); typedef void (APIENTRYP PFNGLDELETETEXTURESPROC) (GLsizei n, const GLuint *textures); typedef void (APIENTRYP PFNGLGENTEXTURESPROC) (GLsizei n, GLuint *textures); #ifdef GL_GLEXT_PROTOTYPES GLAPI void APIENTRY glDrawElements (GLenum mode, GLsizei count, GLenum type, const void *indices); +GLAPI void APIENTRY glTexSubImage2D (GLenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, const void *pixels); GLAPI void APIENTRY glBindTexture (GLenum target, GLuint texture); GLAPI void APIENTRY glDeleteTextures (GLsizei n, const GLuint *textures); GLAPI void APIENTRY glGenTextures (GLsizei n, GLuint *textures); #endif #endif /* GL_VERSION_1_1 */ +#ifndef GL_VERSION_1_2 +#define GL_CLAMP_TO_EDGE 0x812F +#endif /* GL_VERSION_1_2 */ #ifndef GL_VERSION_1_3 #define GL_TEXTURE0 0x84C0 #define GL_ACTIVE_TEXTURE 0x84E0 @@ -346,6 +357,10 @@ GLAPI void APIENTRY glUniformMatrix4fv (GLint location, GLsizei count, GLboolean GLAPI void APIENTRY glVertexAttribPointer (GLuint index, GLint size, GLenum type, GLboolean normalized, GLsizei stride, const void *pointer); #endif #endif /* GL_VERSION_2_0 */ +#ifndef GL_VERSION_2_1 +#define GL_PIXEL_UNPACK_BUFFER 0x88EC +#define GL_PIXEL_UNPACK_BUFFER_BINDING 0x88EF +#endif /* GL_VERSION_2_1 */ #ifndef GL_VERSION_3_0 typedef khronos_uint16_t GLhalf; #define GL_MAJOR_VERSION 0x821B @@ -386,9 +401,15 @@ GLAPI void APIENTRY glDrawElementsBaseVertex (GLenum mode, GLsizei count, GLenum #ifndef GL_VERSION_3_3 #define GL_VERSION_3_3 1 #define GL_SAMPLER_BINDING 0x8919 +typedef void (APIENTRYP PFNGLGENSAMPLERSPROC) (GLsizei count, GLuint *samplers); +typedef void (APIENTRYP PFNGLDELETESAMPLERSPROC) (GLsizei count, const GLuint *samplers); typedef void (APIENTRYP PFNGLBINDSAMPLERPROC) (GLuint unit, GLuint sampler); +typedef void (APIENTRYP PFNGLSAMPLERPARAMETERIPROC) (GLuint sampler, GLenum pname, GLint param); #ifdef GL_GLEXT_PROTOTYPES +GLAPI void APIENTRY glGenSamplers (GLsizei count, GLuint *samplers); +GLAPI void APIENTRY glDeleteSamplers (GLsizei count, const GLuint *samplers); GLAPI void APIENTRY glBindSampler (GLuint unit, GLuint sampler); +GLAPI void APIENTRY glSamplerParameteri (GLuint sampler, GLenum pname, GLint param); #endif #endif /* GL_VERSION_3_3 */ #ifndef GL_VERSION_4_1 @@ -463,12 +484,13 @@ typedef GL3WglProc (*GL3WGetProcAddressProc)(const char *proc); /* gl3w api */ GL3W_API int imgl3wInit(void); GL3W_API int imgl3wInit2(GL3WGetProcAddressProc proc); +GL3W_API void imgl3wShutdown(void); GL3W_API int imgl3wIsSupported(int major, int minor); GL3W_API GL3WglProc imgl3wGetProcAddress(const char *proc); /* gl3w internal state */ union ImGL3WProcs { - GL3WglProc ptr[59]; + GL3WglProc ptr[63]; struct { PFNGLACTIVETEXTUREPROC ActiveTexture; PFNGLATTACHSHADERPROC AttachShader; @@ -488,6 +510,7 @@ union ImGL3WProcs { PFNGLCREATESHADERPROC CreateShader; PFNGLDELETEBUFFERSPROC DeleteBuffers; PFNGLDELETEPROGRAMPROC DeleteProgram; + PFNGLDELETESAMPLERSPROC DeleteSamplers; PFNGLDELETESHADERPROC DeleteShader; PFNGLDELETETEXTURESPROC DeleteTextures; PFNGLDELETEVERTEXARRAYSPROC DeleteVertexArrays; @@ -500,6 +523,7 @@ union ImGL3WProcs { PFNGLENABLEVERTEXATTRIBARRAYPROC EnableVertexAttribArray; PFNGLFLUSHPROC Flush; PFNGLGENBUFFERSPROC GenBuffers; + PFNGLGENSAMPLERSPROC GenSamplers; PFNGLGENTEXTURESPROC GenTextures; PFNGLGENVERTEXARRAYSPROC GenVertexArrays; PFNGLGETATTRIBLOCATIONPROC GetAttribLocation; @@ -520,10 +544,12 @@ union ImGL3WProcs { PFNGLPIXELSTOREIPROC PixelStorei; PFNGLPOLYGONMODEPROC PolygonMode; PFNGLREADPIXELSPROC ReadPixels; + PFNGLSAMPLERPARAMETERIPROC SamplerParameteri; PFNGLSCISSORPROC Scissor; PFNGLSHADERSOURCEPROC ShaderSource; PFNGLTEXIMAGE2DPROC TexImage2D; PFNGLTEXPARAMETERIPROC TexParameteri; + PFNGLTEXSUBIMAGE2DPROC TexSubImage2D; PFNGLUNIFORM1IPROC Uniform1i; PFNGLUNIFORMMATRIX4FVPROC UniformMatrix4fv; PFNGLUSEPROGRAMPROC UseProgram; @@ -553,6 +579,7 @@ GL3W_API extern union ImGL3WProcs imgl3wProcs; #define glCreateShader imgl3wProcs.gl.CreateShader #define glDeleteBuffers imgl3wProcs.gl.DeleteBuffers #define glDeleteProgram imgl3wProcs.gl.DeleteProgram +#define glDeleteSamplers imgl3wProcs.gl.DeleteSamplers #define glDeleteShader imgl3wProcs.gl.DeleteShader #define glDeleteTextures imgl3wProcs.gl.DeleteTextures #define glDeleteVertexArrays imgl3wProcs.gl.DeleteVertexArrays @@ -565,6 +592,7 @@ GL3W_API extern union ImGL3WProcs imgl3wProcs; #define glEnableVertexAttribArray imgl3wProcs.gl.EnableVertexAttribArray #define glFlush imgl3wProcs.gl.Flush #define glGenBuffers imgl3wProcs.gl.GenBuffers +#define glGenSamplers imgl3wProcs.gl.GenSamplers #define glGenTextures imgl3wProcs.gl.GenTextures #define glGenVertexArrays imgl3wProcs.gl.GenVertexArrays #define glGetAttribLocation imgl3wProcs.gl.GetAttribLocation @@ -585,10 +613,12 @@ GL3W_API extern union ImGL3WProcs imgl3wProcs; #define glPixelStorei imgl3wProcs.gl.PixelStorei #define glPolygonMode imgl3wProcs.gl.PolygonMode #define glReadPixels imgl3wProcs.gl.ReadPixels +#define glSamplerParameteri imgl3wProcs.gl.SamplerParameteri #define glScissor imgl3wProcs.gl.Scissor #define glShaderSource imgl3wProcs.gl.ShaderSource #define glTexImage2D imgl3wProcs.gl.TexImage2D #define glTexParameteri imgl3wProcs.gl.TexParameteri +#define glTexSubImage2D imgl3wProcs.gl.TexSubImage2D #define glUniform1i imgl3wProcs.gl.Uniform1i #define glUniformMatrix4fv imgl3wProcs.gl.UniformMatrix4fv #define glUseProgram imgl3wProcs.gl.UseProgram @@ -616,7 +646,7 @@ extern "C" { #endif #include -static HMODULE libgl; +static HMODULE libgl = NULL; typedef PROC(__stdcall* GL3WglGetProcAddr)(LPCSTR); static GL3WglGetProcAddr wgl_get_proc_address; @@ -629,7 +659,7 @@ static int open_libgl(void) return GL3W_OK; } -static void close_libgl(void) { FreeLibrary(libgl); } +static void close_libgl(void) { FreeLibrary(libgl); libgl = NULL; } static GL3WglProc get_proc(const char *proc) { GL3WglProc res; @@ -641,7 +671,7 @@ static GL3WglProc get_proc(const char *proc) #elif defined(__APPLE__) #include -static void *libgl; +static void *libgl = NULL; static int open_libgl(void) { libgl = dlopen("/System/Library/Frameworks/OpenGL.framework/OpenGL", RTLD_LAZY | RTLD_LOCAL); @@ -650,7 +680,7 @@ static int open_libgl(void) return GL3W_OK; } -static void close_libgl(void) { dlclose(libgl); } +static void close_libgl(void) { dlclose(libgl); libgl = NULL; } static GL3WglProc get_proc(const char *proc) { @@ -661,31 +691,127 @@ static GL3WglProc get_proc(const char *proc) #else #include -static void *libgl; -static GL3WglProc (*glx_get_proc_address)(const GLubyte *); +static void* libgl; // OpenGL library +static void* libglx; // GLX library +static void* libegl; // EGL library +static GL3WGetProcAddressProc gl_get_proc_address; -static int open_libgl(void) +static void close_libgl(void) +{ + if (libgl) { + dlclose(libgl); + libgl = NULL; + } + if (libegl) { + dlclose(libegl); + libegl = NULL; + } + if (libglx) { + dlclose(libglx); + libglx = NULL; + } +} + +static int is_library_loaded(const char* name, void** lib) { +#if defined(__HAIKU__) + *lib = NULL; // no support for RTLD_NOLOAD on Haiku. +#else + *lib = dlopen(name, RTLD_LAZY | RTLD_LOCAL | RTLD_NOLOAD); +#endif + return *lib != NULL; +} + +static int open_libs(void) +{ + // On Linux we have two APIs to get process addresses: EGL and GLX. + // EGL is supported under both X11 and Wayland, whereas GLX is X11-specific. + + libgl = NULL; + libegl = NULL; + libglx = NULL; + + // First check what's already loaded, the windowing library might have + // already loaded either EGL or GLX and we want to use the same one. + + if (is_library_loaded("libEGL.so.1", &libegl) || + is_library_loaded("libGLX.so.0", &libglx)) { + libgl = dlopen("libOpenGL.so.0", RTLD_LAZY | RTLD_LOCAL); + if (libgl) + return GL3W_OK; + else + close_libgl(); + } + + if (is_library_loaded("libGL.so", &libgl)) + return GL3W_OK; + if (is_library_loaded("libGL.so.1", &libgl)) + return GL3W_OK; + if (is_library_loaded("libGL.so.3", &libgl)) + return GL3W_OK; + + // Neither is already loaded, so we have to load one. Try EGL first + // because it is supported under both X11 and Wayland. + + // Load OpenGL + EGL + libgl = dlopen("libOpenGL.so.0", RTLD_LAZY | RTLD_LOCAL); + libegl = dlopen("libEGL.so.1", RTLD_LAZY | RTLD_LOCAL); + if (libgl && libegl) + return GL3W_OK; + else + close_libgl(); + + // Fall back to legacy libGL, which includes GLX // While most systems use libGL.so.1, NetBSD seems to use that libGL.so.3. See https://github.com/ocornut/imgui/issues/6983 libgl = dlopen("libGL.so", RTLD_LAZY | RTLD_LOCAL); if (!libgl) libgl = dlopen("libGL.so.1", RTLD_LAZY | RTLD_LOCAL); if (!libgl) libgl = dlopen("libGL.so.3", RTLD_LAZY | RTLD_LOCAL); - if (!libgl) + + if (libgl) + return GL3W_OK; + + return GL3W_ERROR_LIBRARY_OPEN; +} + +static int open_libgl(void) +{ + int res = open_libs(); + if (res) + return res; + + if (libegl) + *(void**)(&gl_get_proc_address) = dlsym(libegl, "eglGetProcAddress"); + else if (libglx) + *(void**)(&gl_get_proc_address) = dlsym(libglx, "glXGetProcAddressARB"); + else + *(void**)(&gl_get_proc_address) = dlsym(libgl, "glXGetProcAddressARB"); + + if (!gl_get_proc_address) { + close_libgl(); return GL3W_ERROR_LIBRARY_OPEN; - *(void **)(&glx_get_proc_address) = dlsym(libgl, "glXGetProcAddressARB"); + } + return GL3W_OK; } -static void close_libgl(void) { dlclose(libgl); } - -static GL3WglProc get_proc(const char *proc) +static GL3WglProc get_proc(const char* proc) { - GL3WglProc res; - res = glx_get_proc_address((const GLubyte *)proc); + GL3WglProc res = NULL; + + // Before EGL version 1.5, eglGetProcAddress doesn't support querying core + // functions and may return a dummy function if we try, so try to load the + // function from the GL library directly first. + if (libegl) + *(void**)(&res) = dlsym(libgl, proc); + if (!res) - *(void **)(&res) = dlsym(libgl, proc); + res = gl_get_proc_address(proc); + + if (!libegl && !res) + *(void**)(&res) = dlsym(libgl, proc); + return res; } #endif @@ -726,6 +852,11 @@ int imgl3wInit2(GL3WGetProcAddressProc proc) return parse_version(); } +void imgl3wShutdown(void) +{ + close_libgl(); +} + int imgl3wIsSupported(int major, int minor) { if (major < 2) @@ -756,6 +887,7 @@ static const char *proc_names[] = { "glCreateShader", "glDeleteBuffers", "glDeleteProgram", + "glDeleteSamplers", "glDeleteShader", "glDeleteTextures", "glDeleteVertexArrays", @@ -768,6 +900,7 @@ static const char *proc_names[] = { "glEnableVertexAttribArray", "glFlush", "glGenBuffers", + "glGenSamplers", "glGenTextures", "glGenVertexArrays", "glGetAttribLocation", @@ -788,10 +921,12 @@ static const char *proc_names[] = { "glPixelStorei", "glPolygonMode", "glReadPixels", + "glSamplerParameteri", "glScissor", "glShaderSource", "glTexImage2D", "glTexParameteri", + "glTexSubImage2D", "glUniform1i", "glUniformMatrix4fv", "glUseProgram", diff --git a/src/app/imgui_impl_sdl3.cpp b/src/app/imgui_impl_sdl3.cpp new file mode 100644 index 00000000..3f60ce4a --- /dev/null +++ b/src/app/imgui_impl_sdl3.cpp @@ -0,0 +1,1289 @@ +// dear imgui: Platform Backend for SDL3 +// This needs to be used along with a Renderer (e.g. SDL_GPU, DirectX11, OpenGL3, Vulkan..) +// (Info: SDL3 is a cross-platform general purpose library for handling windows, inputs, graphics context creation, etc.) + +// Implemented features: +// [X] Platform: Clipboard support. +// [X] Platform: Mouse support. Can discriminate Mouse/TouchScreen. +// [X] Platform: Keyboard support. Since 1.87 we are using the io.AddKeyEvent() function. Pass ImGuiKey values to all key functions e.g. ImGui::IsKeyPressed(ImGuiKey_Space). [Legacy SDL_SCANCODE_* values are obsolete since 1.87 and not supported since 1.91.5] +// [X] Platform: Gamepad support. +// [X] Platform: Mouse cursor shape and visibility (ImGuiBackendFlags_HasMouseCursors). Disable with 'io.ConfigFlags |= ImGuiConfigFlags_NoMouseCursorChange'. +// [x] Platform: Multi-viewport support (multiple windows). Enable with 'io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable' -> the OS animation effect when window gets created/destroyed is problematic. SDL2 backend doesn't have issue. +// Missing features or Issues: +// [ ] Platform: Multi-viewport: Minimized windows seems to break mouse wheel events (at least under Windows). +// [x] Platform: IME support. Position somehow broken in SDL3 + app needs to call 'SDL_SetHint(SDL_HINT_IME_SHOW_UI, "1");' before SDL_CreateWindow()!. + +// You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this. +// Prefer including the entire imgui/ repository into your project (either as a copy or as a submodule), and only build the backends you need. +// Learn about Dear ImGui: +// - FAQ https://dearimgui.com/faq +// - Getting Started https://dearimgui.com/getting-started +// - Documentation https://dearimgui.com/docs (same as your local docs/ folder). +// - Introduction, links and more at the top of imgui.cpp + +// CHANGELOG +// (minor and older changes stripped away, please see git history for details) +// 2025-XX-XX: Platform: Added support for multiple windows via the ImGuiPlatformIO interface. +// 2025-11-05: Fixed an issue with missing characters events when an already active text field changes viewports. (#9054) +// 2025-10-22: Fixed Platform_OpenInShellFn() return value (unused in core). +// 2025-09-24: Skip using the SDL_GetGlobalMouseState() state when one of our window is hovered, as the SDL_EVENT_MOUSE_MOTION data is reliable. Fix macOS notch mouse coordinates issue in fullscreen mode + better perf on X11. (#7919, #7786) +// 2025-09-18: Call platform_io.ClearPlatformHandlers() on shutdown. +// 2025-09-15: Use SDL_GetWindowDisplayScale() on Mac to output DisplayFrameBufferScale. The function is more reliable during resolution changes e.g. going fullscreen. (#8703, #4414) +// 2025-06-27: IME: avoid calling SDL_StartTextInput() again if already active. (#8727) +// 2025-05-15: [Docking] Add Platform_GetWindowFramebufferScale() handler, to allow varying Retina display density on multiple monitors. +// 2025-05-06: [Docking] macOS: fixed secondary viewports not appearing on other monitors before of parenting. +// 2025-04-09: [Docking] Revert update monitors and work areas information every frame. Only do it on Windows. (#8415, #8558) +// 2025-04-22: IME: honor ImGuiPlatformImeData->WantTextInput as an alternative way to call SDL_StartTextInput(), without IME being necessarily visible. +// 2025-04-09: Don't attempt to call SDL_CaptureMouse() on drivers where we don't call SDL_GetGlobalMouseState(). (#8561) +// 2025-03-30: Update for SDL3 api changes: Revert SDL_GetClipboardText() memory ownership change. (#8530, #7801) +// 2025-03-21: Fill gamepad inputs and set ImGuiBackendFlags_HasGamepad regardless of ImGuiConfigFlags_NavEnableGamepad being set. +// 2025-03-10: When dealing with OEM keys, use scancodes instead of translated keycodes to choose ImGuiKey values. (#7136, #7201, #7206, #7306, #7670, #7672, #8468) +// 2025-02-26: Only start SDL_CaptureMouse() when mouse is being dragged, to mitigate issues with e.g.Linux debuggers not claiming capture back. (#6410, #3650) +// 2025-02-25: [Docking] Revert to use SDL_GetDisplayBounds() for WorkPos/WorkRect if SDL_GetDisplayUsableBounds() failed. +// 2025-02-24: Avoid calling SDL_GetGlobalMouseState() when mouse is in relative mode. +// 2025-02-21: [Docking] Update monitors and work areas information every frame, as the later may change regardless of monitor changes. (#8415) +// 2025-02-18: Added ImGuiMouseCursor_Wait and ImGuiMouseCursor_Progress mouse cursor support. +// 2025-02-10: Using SDL_OpenURL() in platform_io.Platform_OpenInShellFn handler. +// 2025-01-20: Made ImGui_ImplSDL3_SetGamepadMode(ImGui_ImplSDL3_GamepadMode_Manual) accept an empty array. +// 2024-10-24: Emscripten: SDL_EVENT_MOUSE_WHEEL event doesn't require dividing by 100.0f on Emscripten. +// 2024-09-11: (Docking) Added support for viewport->ParentViewportId field to support parenting at OS level. (#7973) +// 2024-09-03: Update for SDL3 api changes: SDL_GetGamepads() memory ownership revert. (#7918, #7898, #7807) +// 2024-08-22: moved some OS/backend related function pointers from ImGuiIO to ImGuiPlatformIO: +// - io.GetClipboardTextFn -> platform_io.Platform_GetClipboardTextFn +// - io.SetClipboardTextFn -> platform_io.Platform_SetClipboardTextFn +// - io.PlatformSetImeDataFn -> platform_io.Platform_SetImeDataFn +// 2024-08-19: Storing SDL_WindowID inside ImGuiViewport::PlatformHandle instead of SDL_Window*. +// 2024-08-19: ImGui_ImplSDL3_ProcessEvent() now ignores events intended for other SDL windows. (#7853) +// 2024-07-22: Update for SDL3 api changes: SDL_GetGamepads() memory ownership change. (#7807) +// 2024-07-18: Update for SDL3 api changes: SDL_GetClipboardText() memory ownership change. (#7801) +// 2024-07-15: Update for SDL3 api changes: SDL_GetProperty() change to SDL_GetPointerProperty(). (#7794) +// 2024-07-02: Update for SDL3 api changes: SDLK_x renames and SDLK_KP_x removals (#7761, #7762). +// 2024-07-01: Update for SDL3 api changes: SDL_SetTextInputRect() changed to SDL_SetTextInputArea(). +// 2024-06-26: Update for SDL3 api changes: SDL_StartTextInput()/SDL_StopTextInput()/SDL_SetTextInputRect() functions signatures. +// 2024-06-24: Update for SDL3 api changes: SDL_EVENT_KEY_DOWN/SDL_EVENT_KEY_UP contents. +// 2024-06-03; Update for SDL3 api changes: SDL_SYSTEM_CURSOR_ renames. +// 2024-05-15: Update for SDL3 api changes: SDLK_ renames. +// 2024-04-15: Inputs: Re-enable calling SDL_StartTextInput()/SDL_StopTextInput() as SDL3 no longer enables it by default and should play nicer with IME. +// 2024-02-13: Inputs: Fixed gamepad support. Handle gamepad disconnection. Added ImGui_ImplSDL3_SetGamepadMode(). +// 2023-11-13: Updated for recent SDL3 API changes. +// 2023-10-05: Inputs: Added support for extra ImGuiKey values: F13 to F24 function keys, app back/forward keys. +// 2023-05-04: Fixed build on Emscripten/iOS/Android. (#6391) +// 2023-04-06: Inputs: Avoid calling SDL_StartTextInput()/SDL_StopTextInput() as they don't only pertain to IME. It's unclear exactly what their relation is to IME. (#6306) +// 2023-04-04: Inputs: Added support for io.AddMouseSourceEvent() to discriminate ImGuiMouseSource_Mouse/ImGuiMouseSource_TouchScreen. (#2702) +// 2023-02-23: Accept SDL_GetPerformanceCounter() not returning a monotonically increasing value. (#6189, #6114, #3644) +// 2023-02-07: Forked "imgui_impl_sdl2" into "imgui_impl_sdl3". Removed version checks for old feature. Refer to imgui_impl_sdl2.cpp for older changelog. + +#include "imgui.h" +#ifndef IMGUI_DISABLE +#include "imgui_impl_sdl3.h" + +// Clang warnings with -Weverything +#if defined(__clang__) +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wold-style-cast" // warning: use of old-style cast +#pragma clang diagnostic ignored "-Wimplicit-int-float-conversion" // warning: implicit conversion from 'xxx' to 'float' may lose precision +#endif + +// SDL +#include +#include // for snprintf() +#if defined(__APPLE__) +#include +#endif +#ifdef _WIN32 +#ifndef WIN32_LEAN_AND_MEAN +#define WIN32_LEAN_AND_MEAN +#endif +#include +#endif + +#if !defined(__EMSCRIPTEN__) && !defined(__ANDROID__) && !(defined(__APPLE__) && TARGET_OS_IOS) && !defined(__amigaos4__) +#define SDL_HAS_CAPTURE_AND_GLOBAL_MOUSE 1 +#else +#define SDL_HAS_CAPTURE_AND_GLOBAL_MOUSE 0 +#endif + +// FIXME-LEGACY: remove when SDL 3.1.3 preview is released. +#ifndef SDLK_APOSTROPHE +#define SDLK_APOSTROPHE SDLK_QUOTE +#endif +#ifndef SDLK_GRAVE +#define SDLK_GRAVE SDLK_BACKQUOTE +#endif + +// SDL Data +struct ImGui_ImplSDL3_Data +{ + SDL_Window* Window; + SDL_WindowID WindowID; + SDL_Renderer* Renderer; + Uint64 Time; + char* ClipboardTextData; + char BackendPlatformName[48]; + bool UseVulkan; + bool WantUpdateMonitors; + + // IME handling + SDL_Window* ImeWindow; + ImGuiPlatformImeData ImeData; + bool ImeDirty; + + // Mouse handling + Uint32 MouseWindowID; + int MouseButtonsDown; + SDL_Cursor* MouseCursors[ImGuiMouseCursor_COUNT]; + SDL_Cursor* MouseLastCursor; + int MousePendingLeaveFrame; + bool MouseCanUseGlobalState; + bool MouseCanUseCapture; + bool MouseCanReportHoveredViewport; // This is hard to use/unreliable on SDL so we'll set ImGuiBackendFlags_HasMouseHoveredViewport dynamically based on state. + + // Gamepad handling + ImVector Gamepads; + ImGui_ImplSDL3_GamepadMode GamepadMode; + bool WantUpdateGamepadsList; + + ImGui_ImplSDL3_Data() { memset((void*)this, 0, sizeof(*this)); } +}; + +// Backend data stored in io.BackendPlatformUserData to allow support for multiple Dear ImGui contexts +// It is STRONGLY preferred that you use docking branch with multi-viewports (== single Dear ImGui context + multiple windows) instead of multiple Dear ImGui contexts. +// FIXME: multi-context support is not well tested and probably dysfunctional in this backend. +// FIXME: some shared resources (mouse cursor shape, gamepad) are mishandled when using multi-context. +static ImGui_ImplSDL3_Data* ImGui_ImplSDL3_GetBackendData() +{ + return ImGui::GetCurrentContext() ? (ImGui_ImplSDL3_Data*)ImGui::GetIO().BackendPlatformUserData : nullptr; +} + +// Forward Declarations +static void ImGui_ImplSDL3_UpdateIme(); +static void ImGui_ImplSDL3_UpdateMonitors(); +static void ImGui_ImplSDL3_InitMultiViewportSupport(SDL_Window* window, void* sdl_gl_context); +static void ImGui_ImplSDL3_ShutdownMultiViewportSupport(); + +// Functions +static const char* ImGui_ImplSDL3_GetClipboardText(ImGuiContext*) +{ + ImGui_ImplSDL3_Data* bd = ImGui_ImplSDL3_GetBackendData(); + if (bd->ClipboardTextData) + SDL_free(bd->ClipboardTextData); + bd->ClipboardTextData = SDL_GetClipboardText(); + return bd->ClipboardTextData; +} + +static void ImGui_ImplSDL3_SetClipboardText(ImGuiContext*, const char* text) +{ + SDL_SetClipboardText(text); +} + +static ImGuiViewport* ImGui_ImplSDL3_GetViewportForWindowID(SDL_WindowID window_id) +{ + return ImGui::FindViewportByPlatformHandle((void*)(intptr_t)window_id); +} + +static void ImGui_ImplSDL3_PlatformSetImeData(ImGuiContext*, ImGuiViewport*, ImGuiPlatformImeData* data) +{ + ImGui_ImplSDL3_Data* bd = ImGui_ImplSDL3_GetBackendData(); + bd->ImeData = *data; + bd->ImeDirty = true; + ImGui_ImplSDL3_UpdateIme(); +} + +// We discard viewport passed via ImGuiPlatformImeData and always call SDL_StartTextInput() on SDL_GetKeyboardFocus(). +static void ImGui_ImplSDL3_UpdateIme() +{ + ImGui_ImplSDL3_Data* bd = ImGui_ImplSDL3_GetBackendData(); + ImGuiPlatformImeData* data = &bd->ImeData; + SDL_Window* window = SDL_GetKeyboardFocus(); + + // Stop previous input + if ((!(data->WantVisible || data->WantTextInput) || bd->ImeWindow != window) && bd->ImeWindow != nullptr) + { + SDL_StopTextInput(bd->ImeWindow); + bd->ImeWindow = nullptr; + } + if ((!bd->ImeDirty && bd->ImeWindow == window) || (window == NULL)) + return; + + // Start/update current input + bd->ImeDirty = false; + if (data->WantVisible) + { + ImVec2 viewport_pos; + if (ImGuiViewport* viewport = ImGui_ImplSDL3_GetViewportForWindowID(SDL_GetWindowID(window))) + viewport_pos = viewport->Pos; + SDL_Rect r; + r.x = (int)(data->InputPos.x - viewport_pos.x); + r.y = (int)(data->InputPos.y - viewport_pos.y + data->InputLineHeight); + r.w = 1; + r.h = (int)data->InputLineHeight; + SDL_SetTextInputArea(window, &r, 0); + bd->ImeWindow = window; + } + if (!SDL_TextInputActive(window) && (data->WantVisible || data->WantTextInput)) + SDL_StartTextInput(window); +} + +// Not static to allow third-party code to use that if they want to (but undocumented) +ImGuiKey ImGui_ImplSDL3_KeyEventToImGuiKey(SDL_Keycode keycode, SDL_Scancode scancode); +ImGuiKey ImGui_ImplSDL3_KeyEventToImGuiKey(SDL_Keycode keycode, SDL_Scancode scancode) +{ + // Keypad doesn't have individual key values in SDL3 + switch (scancode) + { + case SDL_SCANCODE_KP_0: return ImGuiKey_Keypad0; + case SDL_SCANCODE_KP_1: return ImGuiKey_Keypad1; + case SDL_SCANCODE_KP_2: return ImGuiKey_Keypad2; + case SDL_SCANCODE_KP_3: return ImGuiKey_Keypad3; + case SDL_SCANCODE_KP_4: return ImGuiKey_Keypad4; + case SDL_SCANCODE_KP_5: return ImGuiKey_Keypad5; + case SDL_SCANCODE_KP_6: return ImGuiKey_Keypad6; + case SDL_SCANCODE_KP_7: return ImGuiKey_Keypad7; + case SDL_SCANCODE_KP_8: return ImGuiKey_Keypad8; + case SDL_SCANCODE_KP_9: return ImGuiKey_Keypad9; + case SDL_SCANCODE_KP_PERIOD: return ImGuiKey_KeypadDecimal; + case SDL_SCANCODE_KP_DIVIDE: return ImGuiKey_KeypadDivide; + case SDL_SCANCODE_KP_MULTIPLY: return ImGuiKey_KeypadMultiply; + case SDL_SCANCODE_KP_MINUS: return ImGuiKey_KeypadSubtract; + case SDL_SCANCODE_KP_PLUS: return ImGuiKey_KeypadAdd; + case SDL_SCANCODE_KP_ENTER: return ImGuiKey_KeypadEnter; + case SDL_SCANCODE_KP_EQUALS: return ImGuiKey_KeypadEqual; + default: break; + } + switch (keycode) + { + case SDLK_TAB: return ImGuiKey_Tab; + case SDLK_LEFT: return ImGuiKey_LeftArrow; + case SDLK_RIGHT: return ImGuiKey_RightArrow; + case SDLK_UP: return ImGuiKey_UpArrow; + case SDLK_DOWN: return ImGuiKey_DownArrow; + case SDLK_PAGEUP: return ImGuiKey_PageUp; + case SDLK_PAGEDOWN: return ImGuiKey_PageDown; + case SDLK_HOME: return ImGuiKey_Home; + case SDLK_END: return ImGuiKey_End; + case SDLK_INSERT: return ImGuiKey_Insert; + case SDLK_DELETE: return ImGuiKey_Delete; + case SDLK_BACKSPACE: return ImGuiKey_Backspace; + case SDLK_SPACE: return ImGuiKey_Space; + case SDLK_RETURN: return ImGuiKey_Enter; + case SDLK_ESCAPE: return ImGuiKey_Escape; + //case SDLK_APOSTROPHE: return ImGuiKey_Apostrophe; + case SDLK_COMMA: return ImGuiKey_Comma; + //case SDLK_MINUS: return ImGuiKey_Minus; + case SDLK_PERIOD: return ImGuiKey_Period; + //case SDLK_SLASH: return ImGuiKey_Slash; + case SDLK_SEMICOLON: return ImGuiKey_Semicolon; + //case SDLK_EQUALS: return ImGuiKey_Equal; + //case SDLK_LEFTBRACKET: return ImGuiKey_LeftBracket; + //case SDLK_BACKSLASH: return ImGuiKey_Backslash; + //case SDLK_RIGHTBRACKET: return ImGuiKey_RightBracket; + //case SDLK_GRAVE: return ImGuiKey_GraveAccent; + case SDLK_CAPSLOCK: return ImGuiKey_CapsLock; + case SDLK_SCROLLLOCK: return ImGuiKey_ScrollLock; + case SDLK_NUMLOCKCLEAR: return ImGuiKey_NumLock; + case SDLK_PRINTSCREEN: return ImGuiKey_PrintScreen; + case SDLK_PAUSE: return ImGuiKey_Pause; + case SDLK_LCTRL: return ImGuiKey_LeftCtrl; + case SDLK_LSHIFT: return ImGuiKey_LeftShift; + case SDLK_LALT: return ImGuiKey_LeftAlt; + case SDLK_LGUI: return ImGuiKey_LeftSuper; + case SDLK_RCTRL: return ImGuiKey_RightCtrl; + case SDLK_RSHIFT: return ImGuiKey_RightShift; + case SDLK_RALT: return ImGuiKey_RightAlt; + case SDLK_RGUI: return ImGuiKey_RightSuper; + case SDLK_APPLICATION: return ImGuiKey_Menu; + case SDLK_0: return ImGuiKey_0; + case SDLK_1: return ImGuiKey_1; + case SDLK_2: return ImGuiKey_2; + case SDLK_3: return ImGuiKey_3; + case SDLK_4: return ImGuiKey_4; + case SDLK_5: return ImGuiKey_5; + case SDLK_6: return ImGuiKey_6; + case SDLK_7: return ImGuiKey_7; + case SDLK_8: return ImGuiKey_8; + case SDLK_9: return ImGuiKey_9; + case SDLK_A: return ImGuiKey_A; + case SDLK_B: return ImGuiKey_B; + case SDLK_C: return ImGuiKey_C; + case SDLK_D: return ImGuiKey_D; + case SDLK_E: return ImGuiKey_E; + case SDLK_F: return ImGuiKey_F; + case SDLK_G: return ImGuiKey_G; + case SDLK_H: return ImGuiKey_H; + case SDLK_I: return ImGuiKey_I; + case SDLK_J: return ImGuiKey_J; + case SDLK_K: return ImGuiKey_K; + case SDLK_L: return ImGuiKey_L; + case SDLK_M: return ImGuiKey_M; + case SDLK_N: return ImGuiKey_N; + case SDLK_O: return ImGuiKey_O; + case SDLK_P: return ImGuiKey_P; + case SDLK_Q: return ImGuiKey_Q; + case SDLK_R: return ImGuiKey_R; + case SDLK_S: return ImGuiKey_S; + case SDLK_T: return ImGuiKey_T; + case SDLK_U: return ImGuiKey_U; + case SDLK_V: return ImGuiKey_V; + case SDLK_W: return ImGuiKey_W; + case SDLK_X: return ImGuiKey_X; + case SDLK_Y: return ImGuiKey_Y; + case SDLK_Z: return ImGuiKey_Z; + case SDLK_F1: return ImGuiKey_F1; + case SDLK_F2: return ImGuiKey_F2; + case SDLK_F3: return ImGuiKey_F3; + case SDLK_F4: return ImGuiKey_F4; + case SDLK_F5: return ImGuiKey_F5; + case SDLK_F6: return ImGuiKey_F6; + case SDLK_F7: return ImGuiKey_F7; + case SDLK_F8: return ImGuiKey_F8; + case SDLK_F9: return ImGuiKey_F9; + case SDLK_F10: return ImGuiKey_F10; + case SDLK_F11: return ImGuiKey_F11; + case SDLK_F12: return ImGuiKey_F12; + case SDLK_F13: return ImGuiKey_F13; + case SDLK_F14: return ImGuiKey_F14; + case SDLK_F15: return ImGuiKey_F15; + case SDLK_F16: return ImGuiKey_F16; + case SDLK_F17: return ImGuiKey_F17; + case SDLK_F18: return ImGuiKey_F18; + case SDLK_F19: return ImGuiKey_F19; + case SDLK_F20: return ImGuiKey_F20; + case SDLK_F21: return ImGuiKey_F21; + case SDLK_F22: return ImGuiKey_F22; + case SDLK_F23: return ImGuiKey_F23; + case SDLK_F24: return ImGuiKey_F24; + case SDLK_AC_BACK: return ImGuiKey_AppBack; + case SDLK_AC_FORWARD: return ImGuiKey_AppForward; + default: break; + } + + // Fallback to scancode + switch (scancode) + { + case SDL_SCANCODE_GRAVE: return ImGuiKey_GraveAccent; + case SDL_SCANCODE_MINUS: return ImGuiKey_Minus; + case SDL_SCANCODE_EQUALS: return ImGuiKey_Equal; + case SDL_SCANCODE_LEFTBRACKET: return ImGuiKey_LeftBracket; + case SDL_SCANCODE_RIGHTBRACKET: return ImGuiKey_RightBracket; + case SDL_SCANCODE_NONUSBACKSLASH: return ImGuiKey_Oem102; + case SDL_SCANCODE_BACKSLASH: return ImGuiKey_Backslash; + case SDL_SCANCODE_SEMICOLON: return ImGuiKey_Semicolon; + case SDL_SCANCODE_APOSTROPHE: return ImGuiKey_Apostrophe; + case SDL_SCANCODE_COMMA: return ImGuiKey_Comma; + case SDL_SCANCODE_PERIOD: return ImGuiKey_Period; + case SDL_SCANCODE_SLASH: return ImGuiKey_Slash; + default: break; + } + return ImGuiKey_None; +} + +static void ImGui_ImplSDL3_UpdateKeyModifiers(SDL_Keymod sdl_key_mods) +{ + ImGuiIO& io = ImGui::GetIO(); + io.AddKeyEvent(ImGuiMod_Ctrl, (sdl_key_mods & SDL_KMOD_CTRL) != 0); + io.AddKeyEvent(ImGuiMod_Shift, (sdl_key_mods & SDL_KMOD_SHIFT) != 0); + io.AddKeyEvent(ImGuiMod_Alt, (sdl_key_mods & SDL_KMOD_ALT) != 0); + io.AddKeyEvent(ImGuiMod_Super, (sdl_key_mods & SDL_KMOD_GUI) != 0); +} + +// You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs. +// - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application, or clear/overwrite your copy of the mouse data. +// - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application, or clear/overwrite your copy of the keyboard data. +// Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags. +bool ImGui_ImplSDL3_ProcessEvent(const SDL_Event* event) +{ + ImGui_ImplSDL3_Data* bd = ImGui_ImplSDL3_GetBackendData(); + IM_ASSERT(bd != nullptr && "Context or backend not initialized! Did you call ImGui_ImplSDL3_Init()?"); + ImGuiIO& io = ImGui::GetIO(); + + switch (event->type) + { + case SDL_EVENT_MOUSE_MOTION: + { + if (ImGui_ImplSDL3_GetViewportForWindowID(event->motion.windowID) == nullptr) + return false; + ImVec2 mouse_pos((float)event->motion.x, (float)event->motion.y); + if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable) + { + int window_x, window_y; + SDL_GetWindowPosition(SDL_GetWindowFromID(event->motion.windowID), &window_x, &window_y); + mouse_pos.x += window_x; + mouse_pos.y += window_y; + } + io.AddMouseSourceEvent(event->motion.which == SDL_TOUCH_MOUSEID ? ImGuiMouseSource_TouchScreen : ImGuiMouseSource_Mouse); + io.AddMousePosEvent(mouse_pos.x, mouse_pos.y); + return true; + } + case SDL_EVENT_MOUSE_WHEEL: + { + if (ImGui_ImplSDL3_GetViewportForWindowID(event->wheel.windowID) == nullptr) + return false; + //IMGUI_DEBUG_LOG("wheel %.2f %.2f, precise %.2f %.2f\n", (float)event->wheel.x, (float)event->wheel.y, event->wheel.preciseX, event->wheel.preciseY); + float wheel_x = -event->wheel.x; + float wheel_y = event->wheel.y; + io.AddMouseSourceEvent(event->wheel.which == SDL_TOUCH_MOUSEID ? ImGuiMouseSource_TouchScreen : ImGuiMouseSource_Mouse); + io.AddMouseWheelEvent(wheel_x, wheel_y); + return true; + } + case SDL_EVENT_MOUSE_BUTTON_DOWN: + case SDL_EVENT_MOUSE_BUTTON_UP: + { + if (ImGui_ImplSDL3_GetViewportForWindowID(event->button.windowID) == nullptr) + return false; + int mouse_button = -1; + if (event->button.button == SDL_BUTTON_LEFT) { mouse_button = 0; } + if (event->button.button == SDL_BUTTON_RIGHT) { mouse_button = 1; } + if (event->button.button == SDL_BUTTON_MIDDLE) { mouse_button = 2; } + if (event->button.button == SDL_BUTTON_X1) { mouse_button = 3; } + if (event->button.button == SDL_BUTTON_X2) { mouse_button = 4; } + if (mouse_button == -1) + break; + io.AddMouseSourceEvent(event->button.which == SDL_TOUCH_MOUSEID ? ImGuiMouseSource_TouchScreen : ImGuiMouseSource_Mouse); + io.AddMouseButtonEvent(mouse_button, (event->type == SDL_EVENT_MOUSE_BUTTON_DOWN)); + bd->MouseButtonsDown = (event->type == SDL_EVENT_MOUSE_BUTTON_DOWN) ? (bd->MouseButtonsDown | (1 << mouse_button)) : (bd->MouseButtonsDown & ~(1 << mouse_button)); + return true; + } + case SDL_EVENT_TEXT_INPUT: + { + if (ImGui_ImplSDL3_GetViewportForWindowID(event->text.windowID) == nullptr) + return false; + io.AddInputCharactersUTF8(event->text.text); + return true; + } + case SDL_EVENT_KEY_DOWN: + case SDL_EVENT_KEY_UP: + { + ImGuiViewport* viewport = ImGui_ImplSDL3_GetViewportForWindowID(event->key.windowID); + if (viewport == nullptr) + return false; + //IMGUI_DEBUG_LOG("SDL_EVENT_KEY_%s : key=0x%08X ('%s'), scancode=%d ('%s'), mod=%X, windowID=%d, viewport=%08X\n", + // (event->type == SDL_EVENT_KEY_DOWN) ? "DOWN" : "UP ", event->key.key, SDL_GetKeyName(event->key.key), event->key.scancode, SDL_GetScancodeName(event->key.scancode), event->key.mod, event->key.windowID, viewport ? viewport->ID : 0); + ImGui_ImplSDL3_UpdateKeyModifiers((SDL_Keymod)event->key.mod); + ImGuiKey key = ImGui_ImplSDL3_KeyEventToImGuiKey(event->key.key, event->key.scancode); + io.AddKeyEvent(key, (event->type == SDL_EVENT_KEY_DOWN)); + io.SetKeyEventNativeData(key, (int)event->key.key, (int)event->key.scancode, (int)event->key.scancode); // To support legacy indexing (<1.87 user code). Legacy backend uses SDLK_*** as indices to IsKeyXXX() functions. + return true; + } + case SDL_EVENT_DISPLAY_ORIENTATION: + case SDL_EVENT_DISPLAY_ADDED: + case SDL_EVENT_DISPLAY_REMOVED: + case SDL_EVENT_DISPLAY_MOVED: + case SDL_EVENT_DISPLAY_CONTENT_SCALE_CHANGED: + { + bd->WantUpdateMonitors = true; + return true; + } + case SDL_EVENT_WINDOW_MOUSE_ENTER: + { + if (ImGui_ImplSDL3_GetViewportForWindowID(event->window.windowID) == nullptr) + return false; + bd->MouseWindowID = event->window.windowID; + bd->MousePendingLeaveFrame = 0; + return true; + } + // - In some cases, when detaching a window from main viewport SDL may send SDL_WINDOWEVENT_ENTER one frame too late, + // causing SDL_WINDOWEVENT_LEAVE on previous frame to interrupt drag operation by clear mouse position. This is why + // we delay process the SDL_WINDOWEVENT_LEAVE events by one frame. See issue #5012 for details. + // FIXME: Unconfirmed whether this is still needed with SDL3. + case SDL_EVENT_WINDOW_MOUSE_LEAVE: + { + if (ImGui_ImplSDL3_GetViewportForWindowID(event->window.windowID) == nullptr) + return false; + bd->MousePendingLeaveFrame = ImGui::GetFrameCount() + 1; + return true; + } + case SDL_EVENT_WINDOW_FOCUS_GAINED: + case SDL_EVENT_WINDOW_FOCUS_LOST: + { + ImGuiViewport* viewport = ImGui_ImplSDL3_GetViewportForWindowID(event->window.windowID); + if (viewport == nullptr) + return false; + //IMGUI_DEBUG_LOG("%s: windowId %d, viewport: %08X\n", (event->type == SDL_EVENT_WINDOW_FOCUS_GAINED) ? "SDL_EVENT_WINDOW_FOCUS_GAINED" : "SDL_WINDOWEVENT_FOCUS_LOST", event->window.windowID, viewport ? viewport->ID : 0); + io.AddFocusEvent(event->type == SDL_EVENT_WINDOW_FOCUS_GAINED); + return true; + } + case SDL_EVENT_WINDOW_CLOSE_REQUESTED: + case SDL_EVENT_WINDOW_MOVED: + case SDL_EVENT_WINDOW_RESIZED: + { + ImGuiViewport* viewport = ImGui_ImplSDL3_GetViewportForWindowID(event->window.windowID); + if (viewport == NULL) + return false; + if (event->type == SDL_EVENT_WINDOW_CLOSE_REQUESTED) + viewport->PlatformRequestClose = true; + if (event->type == SDL_EVENT_WINDOW_MOVED) + viewport->PlatformRequestMove = true; + if (event->type == SDL_EVENT_WINDOW_RESIZED) + viewport->PlatformRequestResize = true; + return true; + } + case SDL_EVENT_GAMEPAD_ADDED: + case SDL_EVENT_GAMEPAD_REMOVED: + { + bd->WantUpdateGamepadsList = true; + return true; + } + default: + break; + } + return false; +} + +static void ImGui_ImplSDL3_SetupPlatformHandles(ImGuiViewport* viewport, SDL_Window* window) +{ + viewport->PlatformHandle = (void*)(intptr_t)SDL_GetWindowID(window); + viewport->PlatformHandleRaw = nullptr; +#if defined(_WIN32) && !defined(__WINRT__) + viewport->PlatformHandleRaw = (HWND)SDL_GetPointerProperty(SDL_GetWindowProperties(window), SDL_PROP_WINDOW_WIN32_HWND_POINTER, nullptr); +#elif defined(__APPLE__) + viewport->PlatformHandleRaw = SDL_GetPointerProperty(SDL_GetWindowProperties(window), SDL_PROP_WINDOW_COCOA_WINDOW_POINTER, nullptr); +#endif +} + +static bool ImGui_ImplSDL3_Init(SDL_Window* window, SDL_Renderer* renderer, void* sdl_gl_context) +{ + ImGuiIO& io = ImGui::GetIO(); + IMGUI_CHECKVERSION(); + IM_ASSERT(io.BackendPlatformUserData == nullptr && "Already initialized a platform backend!"); + IM_UNUSED(sdl_gl_context); // Unused in this branch + //SDL_SetHint(SDL_HINT_EVENT_LOGGING, "2"); + + const int ver_linked = SDL_GetVersion(); + + // Setup backend capabilities flags + ImGui_ImplSDL3_Data* bd = IM_NEW(ImGui_ImplSDL3_Data)(); + snprintf(bd->BackendPlatformName, sizeof(bd->BackendPlatformName), "imgui_impl_sdl3 (%d.%d.%d; %d.%d.%d)", + SDL_MAJOR_VERSION, SDL_MINOR_VERSION, SDL_MICRO_VERSION, SDL_VERSIONNUM_MAJOR(ver_linked), SDL_VERSIONNUM_MINOR(ver_linked), SDL_VERSIONNUM_MICRO(ver_linked)); + io.BackendPlatformUserData = (void*)bd; + io.BackendPlatformName = bd->BackendPlatformName; + io.BackendFlags |= ImGuiBackendFlags_HasMouseCursors; // We can honor GetMouseCursor() values (optional) + io.BackendFlags |= ImGuiBackendFlags_HasSetMousePos; // We can honor io.WantSetMousePos requests (optional, rarely used) + // (ImGuiBackendFlags_PlatformHasViewports and ImGuiBackendFlags_HasParentViewport may be set just below) + // (ImGuiBackendFlags_HasMouseHoveredViewport is set dynamically in our _NewFrame function) + + bd->Window = window; + bd->WindowID = SDL_GetWindowID(window); + bd->Renderer = renderer; + + // SDL on Linux/OSX doesn't report events for unfocused windows (see https://github.com/ocornut/imgui/issues/4960) + // We will use 'MouseCanReportHoveredViewport' to set 'ImGuiBackendFlags_HasMouseHoveredViewport' dynamically each frame. +#ifndef __APPLE__ + bd->MouseCanReportHoveredViewport = bd->MouseCanUseGlobalState; +#else + bd->MouseCanReportHoveredViewport = false; +#endif + + // Check and store if we are on a SDL backend that supports SDL_GetGlobalMouseState() and SDL_CaptureMouse() + // ("wayland" and "rpi" don't support it, but we chose to use a white-list instead of a black-list) + bd->MouseCanUseGlobalState = false; + bd->MouseCanUseCapture = false; +#if SDL_HAS_CAPTURE_AND_GLOBAL_MOUSE + const char* sdl_backend = SDL_GetCurrentVideoDriver(); + const char* capture_and_global_state_whitelist[] = { "windows", "cocoa", "x11", "DIVE", "VMAN" }; + for (const char* item : capture_and_global_state_whitelist) + if (strncmp(sdl_backend, item, strlen(item)) == 0) + bd->MouseCanUseGlobalState = bd->MouseCanUseCapture = true; +#endif + if (bd->MouseCanUseGlobalState) + { + io.BackendFlags |= ImGuiBackendFlags_PlatformHasViewports; // We can create multi-viewports on the Platform side (optional) + io.BackendFlags |= ImGuiBackendFlags_HasParentViewport; // We can honor viewport->ParentViewportId by applying the corresponding parent/child relationship at platform level (optional) + } + + ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO(); + platform_io.Platform_SetClipboardTextFn = ImGui_ImplSDL3_SetClipboardText; + platform_io.Platform_GetClipboardTextFn = ImGui_ImplSDL3_GetClipboardText; + platform_io.Platform_SetImeDataFn = ImGui_ImplSDL3_PlatformSetImeData; + platform_io.Platform_OpenInShellFn = [](ImGuiContext*, const char* url) { return SDL_OpenURL(url); }; + + // Update monitor a first time during init + ImGui_ImplSDL3_UpdateMonitors(); + + // Gamepad handling + bd->GamepadMode = ImGui_ImplSDL3_GamepadMode_AutoFirst; + bd->WantUpdateGamepadsList = true; + + // Load mouse cursors + bd->MouseCursors[ImGuiMouseCursor_Arrow] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_DEFAULT); + bd->MouseCursors[ImGuiMouseCursor_TextInput] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_TEXT); + bd->MouseCursors[ImGuiMouseCursor_ResizeAll] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_MOVE); + bd->MouseCursors[ImGuiMouseCursor_ResizeNS] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_NS_RESIZE); + bd->MouseCursors[ImGuiMouseCursor_ResizeEW] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_EW_RESIZE); + bd->MouseCursors[ImGuiMouseCursor_ResizeNESW] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_NESW_RESIZE); + bd->MouseCursors[ImGuiMouseCursor_ResizeNWSE] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_NWSE_RESIZE); + bd->MouseCursors[ImGuiMouseCursor_Hand] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_POINTER); + bd->MouseCursors[ImGuiMouseCursor_Wait] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_WAIT); + bd->MouseCursors[ImGuiMouseCursor_Progress] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_PROGRESS); + bd->MouseCursors[ImGuiMouseCursor_NotAllowed] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_NOT_ALLOWED); + + // Set platform dependent data in viewport + // Our mouse update function expect PlatformHandle to be filled for the main viewport + ImGuiViewport* main_viewport = ImGui::GetMainViewport(); + ImGui_ImplSDL3_SetupPlatformHandles(main_viewport, window); + + // From 2.0.5: Set SDL hint to receive mouse click events on window focus, otherwise SDL doesn't emit the event. + // Without this, when clicking to gain focus, our widgets wouldn't activate even though they showed as hovered. + // (This is unfortunately a global SDL setting, so enabling it might have a side-effect on your application. + // It is unlikely to make a difference, but if your app absolutely needs to ignore the initial on-focus click: + // you can ignore SDL_EVENT_MOUSE_BUTTON_DOWN events coming right after a SDL_EVENT_WINDOW_FOCUS_GAINED) + SDL_SetHint(SDL_HINT_MOUSE_FOCUS_CLICKTHROUGH, "1"); + + // From 2.0.22: Disable auto-capture, this is preventing drag and drop across multiple windows (see #5710) + SDL_SetHint(SDL_HINT_MOUSE_AUTO_CAPTURE, "0"); + + // SDL 3.x : see https://github.com/libsdl-org/SDL/issues/6659 + SDL_SetHint("SDL_BORDERLESS_WINDOWED_STYLE", "0"); + + // We need SDL_CaptureMouse(), SDL_GetGlobalMouseState() from SDL 2.0.4+ to support multiple viewports. + // We left the call to ImGui_ImplSDL3_InitPlatformInterface() outside of #ifdef to avoid unused-function warnings. + if (io.BackendFlags & ImGuiBackendFlags_PlatformHasViewports) + ImGui_ImplSDL3_InitMultiViewportSupport(window, sdl_gl_context); + + return true; +} + +// Should technically be a SDL_GLContext but due to typedef it is sane to keep it void* in public interface. +bool ImGui_ImplSDL3_InitForOpenGL(SDL_Window* window, void* sdl_gl_context) +{ + return ImGui_ImplSDL3_Init(window, nullptr, sdl_gl_context); +} + +bool ImGui_ImplSDL3_InitForVulkan(SDL_Window* window) +{ + if (!ImGui_ImplSDL3_Init(window, nullptr, nullptr)) + return false; + ImGui_ImplSDL3_Data* bd = ImGui_ImplSDL3_GetBackendData(); + bd->UseVulkan = true; + return true; +} + +bool ImGui_ImplSDL3_InitForD3D(SDL_Window* window) +{ +#if !defined(_WIN32) + IM_ASSERT(0 && "Unsupported"); +#endif + return ImGui_ImplSDL3_Init(window, nullptr, nullptr); +} + +bool ImGui_ImplSDL3_InitForMetal(SDL_Window* window) +{ + return ImGui_ImplSDL3_Init(window, nullptr, nullptr); +} + +bool ImGui_ImplSDL3_InitForSDLRenderer(SDL_Window* window, SDL_Renderer* renderer) +{ + return ImGui_ImplSDL3_Init(window, renderer, nullptr); +} + +bool ImGui_ImplSDL3_InitForSDLGPU(SDL_Window* window) +{ + return ImGui_ImplSDL3_Init(window, nullptr, nullptr); +} + +bool ImGui_ImplSDL3_InitForOther(SDL_Window* window) +{ + return ImGui_ImplSDL3_Init(window, nullptr, nullptr); +} + +static void ImGui_ImplSDL3_CloseGamepads(); + +void ImGui_ImplSDL3_Shutdown() +{ + ImGui_ImplSDL3_Data* bd = ImGui_ImplSDL3_GetBackendData(); + IM_ASSERT(bd != nullptr && "No platform backend to shutdown, or already shutdown?"); + ImGuiIO& io = ImGui::GetIO(); + ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO(); + + ImGui_ImplSDL3_ShutdownMultiViewportSupport(); + if (bd->ClipboardTextData) + SDL_free(bd->ClipboardTextData); + for (ImGuiMouseCursor cursor_n = 0; cursor_n < ImGuiMouseCursor_COUNT; cursor_n++) + SDL_DestroyCursor(bd->MouseCursors[cursor_n]); + ImGui_ImplSDL3_CloseGamepads(); + + io.BackendPlatformName = nullptr; + io.BackendPlatformUserData = nullptr; + io.BackendFlags &= ~(ImGuiBackendFlags_HasMouseCursors | ImGuiBackendFlags_HasSetMousePos | ImGuiBackendFlags_HasGamepad | ImGuiBackendFlags_PlatformHasViewports | ImGuiBackendFlags_HasMouseHoveredViewport | ImGuiBackendFlags_HasParentViewport); + platform_io.ClearPlatformHandlers(); + IM_DELETE(bd); +} + +// This code is incredibly messy because some of the functions we need for full viewport support are not available in SDL < 2.0.4. +static void ImGui_ImplSDL3_UpdateMouseData() +{ + ImGui_ImplSDL3_Data* bd = ImGui_ImplSDL3_GetBackendData(); + ImGuiIO& io = ImGui::GetIO(); + + // We forward mouse input when hovered or captured (via SDL_EVENT_MOUSE_MOTION) or when focused (below) +#if SDL_HAS_CAPTURE_AND_GLOBAL_MOUSE + // - SDL_CaptureMouse() let the OS know e.g. that our drags can extend outside of parent boundaries (we want updated position) and shouldn't trigger other operations outside. + // - Debuggers under Linux tends to leave captured mouse on break, which may be very inconvenient, so to mitigate the issue we wait until mouse has moved to begin capture. + if (bd->MouseCanUseCapture) + { + bool want_capture = false; + for (int button_n = 0; button_n < ImGuiMouseButton_COUNT && !want_capture; button_n++) + if (ImGui::IsMouseDragging(button_n, 1.0f)) + want_capture = true; + SDL_CaptureMouse(want_capture); + } + + SDL_Window* focused_window = SDL_GetKeyboardFocus(); + const bool is_app_focused = (focused_window && (bd->Window == focused_window || ImGui_ImplSDL3_GetViewportForWindowID(SDL_GetWindowID(focused_window)) != NULL)); +#else + SDL_Window* focused_window = bd->Window; + const bool is_app_focused = (SDL_GetWindowFlags(bd->Window) & SDL_WINDOW_INPUT_FOCUS) != 0; // SDL 2.0.3 and non-windowed systems: single-viewport only +#endif + if (is_app_focused) + { + // (Optional) Set OS mouse position from Dear ImGui if requested (rarely used, only when io.ConfigNavMoveSetMousePos is enabled by user) + if (io.WantSetMousePos) + { +#if SDL_HAS_CAPTURE_AND_GLOBAL_MOUSE + if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable) + SDL_WarpMouseGlobal(io.MousePos.x, io.MousePos.y); + else +#endif + SDL_WarpMouseInWindow(bd->Window, io.MousePos.x, io.MousePos.y); + } + + // (Optional) Fallback to provide unclamped mouse position when focused but not hovered (SDL_EVENT_MOUSE_MOTION already provides this when hovered or captured) + // Note that SDL_GetGlobalMouseState() is in theory slow on X11, but this only runs on rather specific cases. If a problem we may provide a way to opt-out this feature. + SDL_Window* hovered_window = SDL_GetMouseFocus(); + const bool is_relative_mouse_mode = SDL_GetWindowRelativeMouseMode(bd->Window); + if (hovered_window == NULL && bd->MouseCanUseGlobalState && bd->MouseButtonsDown == 0 && !is_relative_mouse_mode) + { + // Single-viewport mode: mouse position in client window coordinates (io.MousePos is (0,0) when the mouse is on the upper-left corner of the app window) + // Multi-viewport mode: mouse position in OS absolute coordinates (io.MousePos is (0,0) when the mouse is on the upper-left of the primary monitor) + float mouse_x, mouse_y; + int window_x, window_y; + SDL_GetGlobalMouseState(&mouse_x, &mouse_y); + if (!(io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)) + { + SDL_GetWindowPosition(focused_window, &window_x, &window_y); + mouse_x -= window_x; + mouse_y -= window_y; + } + io.AddMousePosEvent(mouse_x, mouse_y); + } + } + + // (Optional) When using multiple viewports: call io.AddMouseViewportEvent() with the viewport the OS mouse cursor is hovering. + // If ImGuiBackendFlags_HasMouseHoveredViewport is not set by the backend, Dear imGui will ignore this field and infer the information using its flawed heuristic. + // - [!] SDL backend does NOT correctly ignore viewports with the _NoInputs flag. + // Some backend are not able to handle that correctly. If a backend report an hovered viewport that has the _NoInputs flag (e.g. when dragging a window + // for docking, the viewport has the _NoInputs flag in order to allow us to find the viewport under), then Dear ImGui is forced to ignore the value reported + // by the backend, and use its flawed heuristic to guess the viewport behind. + // - [X] SDL backend correctly reports this regardless of another viewport behind focused and dragged from (we need this to find a useful drag and drop target). + if (io.BackendFlags & ImGuiBackendFlags_HasMouseHoveredViewport) + { + ImGuiID mouse_viewport_id = 0; + if (ImGuiViewport* mouse_viewport = ImGui_ImplSDL3_GetViewportForWindowID(bd->MouseWindowID)) + mouse_viewport_id = mouse_viewport->ID; + io.AddMouseViewportEvent(mouse_viewport_id); + } +} + +static void ImGui_ImplSDL3_UpdateMouseCursor() +{ + ImGuiIO& io = ImGui::GetIO(); + if (io.ConfigFlags & ImGuiConfigFlags_NoMouseCursorChange) + return; + ImGui_ImplSDL3_Data* bd = ImGui_ImplSDL3_GetBackendData(); + + ImGuiMouseCursor imgui_cursor = ImGui::GetMouseCursor(); + if (io.MouseDrawCursor || imgui_cursor == ImGuiMouseCursor_None) + { + // Hide OS mouse cursor if imgui is drawing it or if it wants no cursor + SDL_HideCursor(); + } + else + { + // Show OS mouse cursor + SDL_Cursor* expected_cursor = bd->MouseCursors[imgui_cursor] ? bd->MouseCursors[imgui_cursor] : bd->MouseCursors[ImGuiMouseCursor_Arrow]; + if (bd->MouseLastCursor != expected_cursor) + { + SDL_SetCursor(expected_cursor); // SDL function doesn't have an early out (see #6113) + bd->MouseLastCursor = expected_cursor; + } + SDL_ShowCursor(); + } +} + +static void ImGui_ImplSDL3_CloseGamepads() +{ + ImGui_ImplSDL3_Data* bd = ImGui_ImplSDL3_GetBackendData(); + if (bd->GamepadMode != ImGui_ImplSDL3_GamepadMode_Manual) + for (SDL_Gamepad* gamepad : bd->Gamepads) + SDL_CloseGamepad(gamepad); + bd->Gamepads.resize(0); +} + +void ImGui_ImplSDL3_SetGamepadMode(ImGui_ImplSDL3_GamepadMode mode, SDL_Gamepad** manual_gamepads_array, int manual_gamepads_count) +{ + ImGui_ImplSDL3_Data* bd = ImGui_ImplSDL3_GetBackendData(); + ImGui_ImplSDL3_CloseGamepads(); + if (mode == ImGui_ImplSDL3_GamepadMode_Manual) + { + IM_ASSERT(manual_gamepads_array != nullptr || manual_gamepads_count <= 0); + for (int n = 0; n < manual_gamepads_count; n++) + bd->Gamepads.push_back(manual_gamepads_array[n]); + } + else + { + IM_ASSERT(manual_gamepads_array == nullptr && manual_gamepads_count <= 0); + bd->WantUpdateGamepadsList = true; + } + bd->GamepadMode = mode; +} + +static void ImGui_ImplSDL3_UpdateGamepadButton(ImGui_ImplSDL3_Data* bd, ImGuiIO& io, ImGuiKey key, SDL_GamepadButton button_no) +{ + bool merged_value = false; + for (SDL_Gamepad* gamepad : bd->Gamepads) + merged_value |= SDL_GetGamepadButton(gamepad, button_no) != 0; + io.AddKeyEvent(key, merged_value); +} + +static inline float Saturate(float v) { return v < 0.0f ? 0.0f : v > 1.0f ? 1.0f : v; } +static void ImGui_ImplSDL3_UpdateGamepadAnalog(ImGui_ImplSDL3_Data* bd, ImGuiIO& io, ImGuiKey key, SDL_GamepadAxis axis_no, float v0, float v1) +{ + float merged_value = 0.0f; + for (SDL_Gamepad* gamepad : bd->Gamepads) + { + float vn = Saturate((float)(SDL_GetGamepadAxis(gamepad, axis_no) - v0) / (float)(v1 - v0)); + if (merged_value < vn) + merged_value = vn; + } + io.AddKeyAnalogEvent(key, merged_value > 0.1f, merged_value); +} + +static void ImGui_ImplSDL3_UpdateGamepads() +{ + ImGuiIO& io = ImGui::GetIO(); + ImGui_ImplSDL3_Data* bd = ImGui_ImplSDL3_GetBackendData(); + + // Update list of gamepads to use + if (bd->WantUpdateGamepadsList && bd->GamepadMode != ImGui_ImplSDL3_GamepadMode_Manual) + { + ImGui_ImplSDL3_CloseGamepads(); + int sdl_gamepads_count = 0; + SDL_JoystickID* sdl_gamepads = SDL_GetGamepads(&sdl_gamepads_count); + for (int n = 0; n < sdl_gamepads_count; n++) + if (SDL_Gamepad* gamepad = SDL_OpenGamepad(sdl_gamepads[n])) + { + bd->Gamepads.push_back(gamepad); + if (bd->GamepadMode == ImGui_ImplSDL3_GamepadMode_AutoFirst) + break; + } + bd->WantUpdateGamepadsList = false; + SDL_free(sdl_gamepads); + } + + io.BackendFlags &= ~ImGuiBackendFlags_HasGamepad; + if (bd->Gamepads.Size == 0) + return; + io.BackendFlags |= ImGuiBackendFlags_HasGamepad; + + // Update gamepad inputs + const int thumb_dead_zone = 8000; // SDL_gamepad.h suggests using this value. + ImGui_ImplSDL3_UpdateGamepadButton(bd, io, ImGuiKey_GamepadStart, SDL_GAMEPAD_BUTTON_START); + ImGui_ImplSDL3_UpdateGamepadButton(bd, io, ImGuiKey_GamepadBack, SDL_GAMEPAD_BUTTON_BACK); + ImGui_ImplSDL3_UpdateGamepadButton(bd, io, ImGuiKey_GamepadFaceLeft, SDL_GAMEPAD_BUTTON_WEST); // Xbox X, PS Square + ImGui_ImplSDL3_UpdateGamepadButton(bd, io, ImGuiKey_GamepadFaceRight, SDL_GAMEPAD_BUTTON_EAST); // Xbox B, PS Circle + ImGui_ImplSDL3_UpdateGamepadButton(bd, io, ImGuiKey_GamepadFaceUp, SDL_GAMEPAD_BUTTON_NORTH); // Xbox Y, PS Triangle + ImGui_ImplSDL3_UpdateGamepadButton(bd, io, ImGuiKey_GamepadFaceDown, SDL_GAMEPAD_BUTTON_SOUTH); // Xbox A, PS Cross + ImGui_ImplSDL3_UpdateGamepadButton(bd, io, ImGuiKey_GamepadDpadLeft, SDL_GAMEPAD_BUTTON_DPAD_LEFT); + ImGui_ImplSDL3_UpdateGamepadButton(bd, io, ImGuiKey_GamepadDpadRight, SDL_GAMEPAD_BUTTON_DPAD_RIGHT); + ImGui_ImplSDL3_UpdateGamepadButton(bd, io, ImGuiKey_GamepadDpadUp, SDL_GAMEPAD_BUTTON_DPAD_UP); + ImGui_ImplSDL3_UpdateGamepadButton(bd, io, ImGuiKey_GamepadDpadDown, SDL_GAMEPAD_BUTTON_DPAD_DOWN); + ImGui_ImplSDL3_UpdateGamepadButton(bd, io, ImGuiKey_GamepadL1, SDL_GAMEPAD_BUTTON_LEFT_SHOULDER); + ImGui_ImplSDL3_UpdateGamepadButton(bd, io, ImGuiKey_GamepadR1, SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER); + ImGui_ImplSDL3_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadL2, SDL_GAMEPAD_AXIS_LEFT_TRIGGER, 0.0f, 32767); + ImGui_ImplSDL3_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadR2, SDL_GAMEPAD_AXIS_RIGHT_TRIGGER, 0.0f, 32767); + ImGui_ImplSDL3_UpdateGamepadButton(bd, io, ImGuiKey_GamepadL3, SDL_GAMEPAD_BUTTON_LEFT_STICK); + ImGui_ImplSDL3_UpdateGamepadButton(bd, io, ImGuiKey_GamepadR3, SDL_GAMEPAD_BUTTON_RIGHT_STICK); + ImGui_ImplSDL3_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadLStickLeft, SDL_GAMEPAD_AXIS_LEFTX, -thumb_dead_zone, -32768); + ImGui_ImplSDL3_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadLStickRight, SDL_GAMEPAD_AXIS_LEFTX, +thumb_dead_zone, +32767); + ImGui_ImplSDL3_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadLStickUp, SDL_GAMEPAD_AXIS_LEFTY, -thumb_dead_zone, -32768); + ImGui_ImplSDL3_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadLStickDown, SDL_GAMEPAD_AXIS_LEFTY, +thumb_dead_zone, +32767); + ImGui_ImplSDL3_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadRStickLeft, SDL_GAMEPAD_AXIS_RIGHTX, -thumb_dead_zone, -32768); + ImGui_ImplSDL3_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadRStickRight, SDL_GAMEPAD_AXIS_RIGHTX, +thumb_dead_zone, +32767); + ImGui_ImplSDL3_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadRStickUp, SDL_GAMEPAD_AXIS_RIGHTY, -thumb_dead_zone, -32768); + ImGui_ImplSDL3_UpdateGamepadAnalog(bd, io, ImGuiKey_GamepadRStickDown, SDL_GAMEPAD_AXIS_RIGHTY, +thumb_dead_zone, +32767); +} + +static void ImGui_ImplSDL3_UpdateMonitors() +{ + ImGui_ImplSDL3_Data* bd = ImGui_ImplSDL3_GetBackendData(); + ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO(); + platform_io.Monitors.resize(0); + bd->WantUpdateMonitors = false; + + int display_count; + SDL_DisplayID* displays = SDL_GetDisplays(&display_count); + for (int n = 0; n < display_count; n++) + { + // Warning: the validity of monitor DPI information on Windows depends on the application DPI awareness settings, which generally needs to be set in the manifest or at runtime. + SDL_DisplayID display_id = displays[n]; + ImGuiPlatformMonitor monitor; + SDL_Rect r; + SDL_GetDisplayBounds(display_id, &r); + monitor.MainPos = monitor.WorkPos = ImVec2((float)r.x, (float)r.y); + monitor.MainSize = monitor.WorkSize = ImVec2((float)r.w, (float)r.h); + if (SDL_GetDisplayUsableBounds(display_id, &r) && r.w > 0 && r.h > 0) + { + monitor.WorkPos = ImVec2((float)r.x, (float)r.y); + monitor.WorkSize = ImVec2((float)r.w, (float)r.h); + } + monitor.DpiScale = SDL_GetDisplayContentScale(display_id); // See https://wiki.libsdl.org/SDL3/README-highdpi for details. + monitor.PlatformHandle = (void*)(intptr_t)n; + if (monitor.DpiScale <= 0.0f) + continue; // Some accessibility applications are declaring virtual monitors with a DPI of 0, see #7902. + platform_io.Monitors.push_back(monitor); + } + SDL_free(displays); +} + +static void ImGui_ImplSDL3_GetWindowSizeAndFramebufferScale(SDL_Window* window, ImVec2* out_size, ImVec2* out_framebuffer_scale) +{ + int w, h; + SDL_GetWindowSize(window, &w, &h); + if (SDL_GetWindowFlags(window) & SDL_WINDOW_MINIMIZED) + w = h = 0; + +#if defined(__APPLE__) + float fb_scale_x = SDL_GetWindowDisplayScale(window); // Seems more reliable during resolution change (#8703) + float fb_scale_y = fb_scale_x; +#else + int display_w, display_h; + SDL_GetWindowSizeInPixels(window, &display_w, &display_h); + float fb_scale_x = (w > 0) ? (float)display_w / (float)w : 1.0f; + float fb_scale_y = (h > 0) ? (float)display_h / (float)h : 1.0f; +#endif + + if (out_size != nullptr) + *out_size = ImVec2((float)w, (float)h); + if (out_framebuffer_scale != nullptr) + *out_framebuffer_scale = ImVec2(fb_scale_x, fb_scale_y); +} + +void ImGui_ImplSDL3_NewFrame() +{ + ImGui_ImplSDL3_Data* bd = ImGui_ImplSDL3_GetBackendData(); + IM_ASSERT(bd != nullptr && "Context or backend not initialized! Did you call ImGui_ImplSDL3_Init()?"); + ImGuiIO& io = ImGui::GetIO(); + + // Setup main viewport size (every frame to accommodate for window resizing) + ImGui_ImplSDL3_GetWindowSizeAndFramebufferScale(bd->Window, &io.DisplaySize, &io.DisplayFramebufferScale); + + // Update monitors +#ifdef WIN32 + bd->WantUpdateMonitors = true; // Keep polling under Windows to handle changes of work area when resizing task-bar (#8415) +#endif + if (bd->WantUpdateMonitors) + ImGui_ImplSDL3_UpdateMonitors(); + + // Setup time step (we could also use SDL_GetTicksNS() available since SDL3) + // (Accept SDL_GetPerformanceCounter() not returning a monotonically increasing value. Happens in VMs and Emscripten, see #6189, #6114, #3644) + static Uint64 frequency = SDL_GetPerformanceFrequency(); + Uint64 current_time = SDL_GetPerformanceCounter(); + if (current_time <= bd->Time) + current_time = bd->Time + 1; + io.DeltaTime = bd->Time > 0 ? (float)((double)(current_time - bd->Time) / frequency) : (float)(1.0f / 60.0f); + bd->Time = current_time; + + if (bd->MousePendingLeaveFrame && bd->MousePendingLeaveFrame >= ImGui::GetFrameCount() && bd->MouseButtonsDown == 0) + { + bd->MouseWindowID = 0; + bd->MousePendingLeaveFrame = 0; + io.AddMousePosEvent(-FLT_MAX, -FLT_MAX); + } + + // Our io.AddMouseViewportEvent() calls will only be valid when not capturing. + // Technically speaking testing for 'bd->MouseButtonsDown == 0' would be more rigorous, but testing for payload reduces noise and potential side-effects. + if (bd->MouseCanReportHoveredViewport && ImGui::GetDragDropPayload() == nullptr) + io.BackendFlags |= ImGuiBackendFlags_HasMouseHoveredViewport; + else + io.BackendFlags &= ~ImGuiBackendFlags_HasMouseHoveredViewport; + + ImGui_ImplSDL3_UpdateMouseData(); + ImGui_ImplSDL3_UpdateMouseCursor(); + ImGui_ImplSDL3_UpdateIme(); + + // Update game controllers (if enabled and available) + ImGui_ImplSDL3_UpdateGamepads(); +} + +//-------------------------------------------------------------------------------------------------------- +// MULTI-VIEWPORT / PLATFORM INTERFACE SUPPORT +// This is an _advanced_ and _optional_ feature, allowing the backend to create and handle multiple viewports simultaneously. +// If you are new to dear imgui or creating a new binding for dear imgui, it is recommended that you completely ignore this section first.. +//-------------------------------------------------------------------------------------------------------- + +// Helper structure we store in the void* PlatformUserData field of each ImGuiViewport to easily retrieve our backend data. +struct ImGui_ImplSDL3_ViewportData +{ + SDL_Window* Window; + SDL_Window* ParentWindow; + Uint32 WindowID; // Stored in ImGuiViewport::PlatformHandle. Use SDL_GetWindowFromID() to get SDL_Window* from Uint32 WindowID. + bool WindowOwned; + SDL_GLContext GLContext; + + ImGui_ImplSDL3_ViewportData() { Window = ParentWindow = nullptr; WindowID = 0; WindowOwned = false; GLContext = nullptr; } + ~ImGui_ImplSDL3_ViewportData() { IM_ASSERT(Window == nullptr && GLContext == nullptr); } +}; + +static SDL_Window* ImGui_ImplSDL3_GetSDLWindowFromViewport(ImGuiViewport* viewport) +{ + if (viewport != nullptr) + { + SDL_WindowID window_id = (SDL_WindowID)(intptr_t)viewport->PlatformHandle; + return SDL_GetWindowFromID(window_id); + } + return nullptr; +} + +static void ImGui_ImplSDL3_CreateWindow(ImGuiViewport* viewport) +{ + ImGui_ImplSDL3_Data* bd = ImGui_ImplSDL3_GetBackendData(); + ImGui_ImplSDL3_ViewportData* vd = IM_NEW(ImGui_ImplSDL3_ViewportData)(); + viewport->PlatformUserData = vd; + + vd->ParentWindow = ImGui_ImplSDL3_GetSDLWindowFromViewport(viewport->ParentViewport); + + ImGuiViewport* main_viewport = ImGui::GetMainViewport(); + ImGui_ImplSDL3_ViewportData* main_viewport_data = (ImGui_ImplSDL3_ViewportData*)main_viewport->PlatformUserData; + + // Share GL resources with main context + bool use_opengl = (main_viewport_data->GLContext != nullptr); + SDL_GLContext backup_context = nullptr; + if (use_opengl) + { + backup_context = SDL_GL_GetCurrentContext(); + SDL_GL_SetAttribute(SDL_GL_SHARE_WITH_CURRENT_CONTEXT, 1); + SDL_GL_MakeCurrent(main_viewport_data->Window, main_viewport_data->GLContext); + } + + SDL_WindowFlags sdl_flags = 0; + sdl_flags |= SDL_WINDOW_HIDDEN; + sdl_flags |= use_opengl ? SDL_WINDOW_OPENGL : (bd->UseVulkan ? SDL_WINDOW_VULKAN : 0); + sdl_flags |= SDL_GetWindowFlags(bd->Window) & SDL_WINDOW_HIGH_PIXEL_DENSITY; + sdl_flags |= (viewport->Flags & ImGuiViewportFlags_NoDecoration) ? SDL_WINDOW_BORDERLESS : 0; + sdl_flags |= (viewport->Flags & ImGuiViewportFlags_NoDecoration) ? 0 : SDL_WINDOW_RESIZABLE; + sdl_flags |= (viewport->Flags & ImGuiViewportFlags_NoTaskBarIcon) ? SDL_WINDOW_UTILITY : 0; + sdl_flags |= (viewport->Flags & ImGuiViewportFlags_TopMost) ? SDL_WINDOW_ALWAYS_ON_TOP : 0; + vd->Window = SDL_CreateWindow("No Title Yet", (int)viewport->Size.x, (int)viewport->Size.y, sdl_flags); +#ifndef __APPLE__ // On Mac, SDL3 Parenting appears to prevent viewport from appearing in another monitor + SDL_SetWindowParent(vd->Window, vd->ParentWindow); +#endif + SDL_SetWindowPosition(vd->Window, (int)viewport->Pos.x, (int)viewport->Pos.y); + vd->WindowOwned = true; + if (use_opengl) + { + vd->GLContext = SDL_GL_CreateContext(vd->Window); + SDL_GL_SetSwapInterval(0); + } + if (use_opengl && backup_context) + SDL_GL_MakeCurrent(vd->Window, backup_context); + + ImGui_ImplSDL3_SetupPlatformHandles(viewport, vd->Window); +} + +static void ImGui_ImplSDL3_DestroyWindow(ImGuiViewport* viewport) +{ + if (ImGui_ImplSDL3_ViewportData* vd = (ImGui_ImplSDL3_ViewportData*)viewport->PlatformUserData) + { + if (vd->GLContext && vd->WindowOwned) + SDL_GL_DestroyContext(vd->GLContext); + if (vd->Window && vd->WindowOwned) + SDL_DestroyWindow(vd->Window); + vd->GLContext = nullptr; + vd->Window = nullptr; + IM_DELETE(vd); + } + viewport->PlatformUserData = viewport->PlatformHandle = nullptr; +} + +static void ImGui_ImplSDL3_ShowWindow(ImGuiViewport* viewport) +{ + ImGui_ImplSDL3_ViewportData* vd = (ImGui_ImplSDL3_ViewportData*)viewport->PlatformUserData; +#if defined(_WIN32) && !(defined(WINAPI_FAMILY) && ((defined(WINAPI_FAMILY_APP) && WINAPI_FAMILY == WINAPI_FAMILY_APP) || (defined(WINAPI_FAMILY_GAMES) && WINAPI_FAMILY == WINAPI_FAMILY_GAMES))) + HWND hwnd = (HWND)viewport->PlatformHandleRaw; + + // SDL hack: Show icon in task bar (#7989) + // Note: SDL_WINDOW_UTILITY can be used to control task bar visibility, but on Windows, it does not affect child windows. + if (!(viewport->Flags & ImGuiViewportFlags_NoTaskBarIcon)) + { + LONG ex_style = ::GetWindowLong(hwnd, GWL_EXSTYLE); + ex_style |= WS_EX_APPWINDOW; + ex_style &= ~WS_EX_TOOLWINDOW; + ::ShowWindow(hwnd, SW_HIDE); + ::SetWindowLong(hwnd, GWL_EXSTYLE, ex_style); + } +#endif + +#ifdef __APPLE__ + SDL_SetHint(SDL_HINT_WINDOW_ACTIVATE_WHEN_SHOWN, "1"); // Otherwise new window appear under +#else + SDL_SetHint(SDL_HINT_WINDOW_ACTIVATE_WHEN_SHOWN, (viewport->Flags & ImGuiViewportFlags_NoFocusOnAppearing) ? "0" : "1"); +#endif + SDL_ShowWindow(vd->Window); +} + +static void ImGui_ImplSDL3_UpdateWindow(ImGuiViewport* viewport) +{ + ImGui_ImplSDL3_ViewportData* vd = (ImGui_ImplSDL3_ViewportData*)viewport->PlatformUserData; + IM_UNUSED(vd); + +#ifndef __APPLE__ // On Mac, SDL3 Parenting appears to prevent viewport from appearing in another monitor + // Update SDL3 parent if it changed _after_ creation. + // This is for advanced apps that are manipulating ParentViewportID manually. + SDL_Window* new_parent = ImGui_ImplSDL3_GetSDLWindowFromViewport(viewport->ParentViewport); + if (new_parent != vd->ParentWindow) + { + vd->ParentWindow = new_parent; + SDL_SetWindowParent(vd->Window, vd->ParentWindow); + } +#endif +} + +static ImVec2 ImGui_ImplSDL3_GetWindowPos(ImGuiViewport* viewport) +{ + ImGui_ImplSDL3_ViewportData* vd = (ImGui_ImplSDL3_ViewportData*)viewport->PlatformUserData; + int x = 0, y = 0; + SDL_GetWindowPosition(vd->Window, &x, &y); + return ImVec2((float)x, (float)y); +} + +static void ImGui_ImplSDL3_SetWindowPos(ImGuiViewport* viewport, ImVec2 pos) +{ + ImGui_ImplSDL3_ViewportData* vd = (ImGui_ImplSDL3_ViewportData*)viewport->PlatformUserData; + SDL_SetWindowPosition(vd->Window, (int)pos.x, (int)pos.y); +} + +static ImVec2 ImGui_ImplSDL3_GetWindowSize(ImGuiViewport* viewport) +{ + ImGui_ImplSDL3_ViewportData* vd = (ImGui_ImplSDL3_ViewportData*)viewport->PlatformUserData; + int w = 0, h = 0; + SDL_GetWindowSize(vd->Window, &w, &h); + return ImVec2((float)w, (float)h); +} + +static void ImGui_ImplSDL3_SetWindowSize(ImGuiViewport* viewport, ImVec2 size) +{ + ImGui_ImplSDL3_ViewportData* vd = (ImGui_ImplSDL3_ViewportData*)viewport->PlatformUserData; + SDL_SetWindowSize(vd->Window, (int)size.x, (int)size.y); +} + +static ImVec2 ImGui_ImplSDL3_GetWindowFramebufferScale(ImGuiViewport* viewport) +{ + ImGui_ImplSDL3_ViewportData* vd = (ImGui_ImplSDL3_ViewportData*)viewport->PlatformUserData; + ImVec2 framebuffer_scale; + ImGui_ImplSDL3_GetWindowSizeAndFramebufferScale(vd->Window, nullptr, &framebuffer_scale); + return framebuffer_scale; +} + +static void ImGui_ImplSDL3_SetWindowTitle(ImGuiViewport* viewport, const char* title) +{ + ImGui_ImplSDL3_ViewportData* vd = (ImGui_ImplSDL3_ViewportData*)viewport->PlatformUserData; + SDL_SetWindowTitle(vd->Window, title); +} + +static void ImGui_ImplSDL3_SetWindowAlpha(ImGuiViewport* viewport, float alpha) +{ + ImGui_ImplSDL3_ViewportData* vd = (ImGui_ImplSDL3_ViewportData*)viewport->PlatformUserData; + SDL_SetWindowOpacity(vd->Window, alpha); +} + +static void ImGui_ImplSDL3_SetWindowFocus(ImGuiViewport* viewport) +{ + ImGui_ImplSDL3_ViewportData* vd = (ImGui_ImplSDL3_ViewportData*)viewport->PlatformUserData; + SDL_RaiseWindow(vd->Window); +} + +static bool ImGui_ImplSDL3_GetWindowFocus(ImGuiViewport* viewport) +{ + ImGui_ImplSDL3_ViewportData* vd = (ImGui_ImplSDL3_ViewportData*)viewport->PlatformUserData; + return (SDL_GetWindowFlags(vd->Window) & SDL_WINDOW_INPUT_FOCUS) != 0; +} + +static bool ImGui_ImplSDL3_GetWindowMinimized(ImGuiViewport* viewport) +{ + ImGui_ImplSDL3_ViewportData* vd = (ImGui_ImplSDL3_ViewportData*)viewport->PlatformUserData; + return (SDL_GetWindowFlags(vd->Window) & SDL_WINDOW_MINIMIZED) != 0; +} + +static void ImGui_ImplSDL3_RenderWindow(ImGuiViewport* viewport, void*) +{ + ImGui_ImplSDL3_ViewportData* vd = (ImGui_ImplSDL3_ViewportData*)viewport->PlatformUserData; + if (vd->GLContext) + SDL_GL_MakeCurrent(vd->Window, vd->GLContext); +} + +static void ImGui_ImplSDL3_SwapBuffers(ImGuiViewport* viewport, void*) +{ + ImGui_ImplSDL3_ViewportData* vd = (ImGui_ImplSDL3_ViewportData*)viewport->PlatformUserData; + if (vd->GLContext) + { + SDL_GL_MakeCurrent(vd->Window, vd->GLContext); + SDL_GL_SwapWindow(vd->Window); + } +} + +// Vulkan support (the Vulkan renderer needs to call a platform-side support function to create the surface) +// SDL is graceful enough to _not_ need so we can safely include this. +#include +static int ImGui_ImplSDL3_CreateVkSurface(ImGuiViewport* viewport, ImU64 vk_instance, const void* vk_allocator, ImU64* out_vk_surface) +{ + ImGui_ImplSDL3_ViewportData* vd = (ImGui_ImplSDL3_ViewportData*)viewport->PlatformUserData; + (void)vk_allocator; + bool ret = SDL_Vulkan_CreateSurface(vd->Window, (VkInstance)vk_instance, (const VkAllocationCallbacks*)vk_allocator, (VkSurfaceKHR*)out_vk_surface); + return ret ? 0 : 1; // ret ? VK_SUCCESS : VK_NOT_READY +} + +static void ImGui_ImplSDL3_InitMultiViewportSupport(SDL_Window* window, void* sdl_gl_context) +{ + // Register platform interface (will be coupled with a renderer interface) + ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO(); + platform_io.Platform_CreateWindow = ImGui_ImplSDL3_CreateWindow; + platform_io.Platform_DestroyWindow = ImGui_ImplSDL3_DestroyWindow; + platform_io.Platform_ShowWindow = ImGui_ImplSDL3_ShowWindow; + platform_io.Platform_UpdateWindow = ImGui_ImplSDL3_UpdateWindow; + platform_io.Platform_SetWindowPos = ImGui_ImplSDL3_SetWindowPos; + platform_io.Platform_GetWindowPos = ImGui_ImplSDL3_GetWindowPos; + platform_io.Platform_SetWindowSize = ImGui_ImplSDL3_SetWindowSize; + platform_io.Platform_GetWindowSize = ImGui_ImplSDL3_GetWindowSize; + platform_io.Platform_GetWindowFramebufferScale = ImGui_ImplSDL3_GetWindowFramebufferScale; + platform_io.Platform_SetWindowFocus = ImGui_ImplSDL3_SetWindowFocus; + platform_io.Platform_GetWindowFocus = ImGui_ImplSDL3_GetWindowFocus; + platform_io.Platform_GetWindowMinimized = ImGui_ImplSDL3_GetWindowMinimized; + platform_io.Platform_SetWindowTitle = ImGui_ImplSDL3_SetWindowTitle; + platform_io.Platform_RenderWindow = ImGui_ImplSDL3_RenderWindow; + platform_io.Platform_SwapBuffers = ImGui_ImplSDL3_SwapBuffers; + platform_io.Platform_SetWindowAlpha = ImGui_ImplSDL3_SetWindowAlpha; + platform_io.Platform_CreateVkSurface = ImGui_ImplSDL3_CreateVkSurface; + + // Register main window handle (which is owned by the main application, not by us) + // This is mostly for simplicity and consistency, so that our code (e.g. mouse handling etc.) can use same logic for main and secondary viewports. + ImGuiViewport* main_viewport = ImGui::GetMainViewport(); + ImGui_ImplSDL3_ViewportData* vd = IM_NEW(ImGui_ImplSDL3_ViewportData)(); + vd->Window = window; + vd->WindowID = SDL_GetWindowID(window); + vd->WindowOwned = false; + vd->GLContext = (SDL_GLContext)sdl_gl_context; + main_viewport->PlatformUserData = vd; + main_viewport->PlatformHandle = (void*)(intptr_t)vd->WindowID; +} + +static void ImGui_ImplSDL3_ShutdownMultiViewportSupport() +{ + ImGui::DestroyPlatformWindows(); +} + +//----------------------------------------------------------------------------- + +#if defined(__clang__) +#pragma clang diagnostic pop +#endif + +#endif // #ifndef IMGUI_DISABLE diff --git a/src/app/imgui_impl_sdl3.h b/src/app/imgui_impl_sdl3.h new file mode 100644 index 00000000..31f43aa7 --- /dev/null +++ b/src/app/imgui_impl_sdl3.h @@ -0,0 +1,50 @@ +// dear imgui: Platform Backend for SDL3 +// This needs to be used along with a Renderer (e.g. SDL_GPU, DirectX11, OpenGL3, Vulkan..) +// (Info: SDL3 is a cross-platform general purpose library for handling windows, inputs, graphics context creation, etc.) + +// Implemented features: +// [X] Platform: Clipboard support. +// [X] Platform: Mouse support. Can discriminate Mouse/TouchScreen. +// [X] Platform: Keyboard support. Since 1.87 we are using the io.AddKeyEvent() function. Pass ImGuiKey values to all key functions e.g. ImGui::IsKeyPressed(ImGuiKey_Space). [Legacy SDL_SCANCODE_* values are obsolete since 1.87 and not supported since 1.91.5] +// [X] Platform: Gamepad support. +// [X] Platform: Mouse cursor shape and visibility (ImGuiBackendFlags_HasMouseCursors). Disable with 'io.ConfigFlags |= ImGuiConfigFlags_NoMouseCursorChange'. +// [x] Platform: Multi-viewport support (multiple windows). Enable with 'io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable' -> the OS animation effect when window gets created/destroyed is problematic. SDL2 backend doesn't have issue. +// Missing features or Issues: +// [ ] Platform: Multi-viewport: Minimized windows seems to break mouse wheel events (at least under Windows). +// [x] Platform: IME support. Position somehow broken in SDL3 + app needs to call 'SDL_SetHint(SDL_HINT_IME_SHOW_UI, "1");' before SDL_CreateWindow()!. + +// You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this. +// Prefer including the entire imgui/ repository into your project (either as a copy or as a submodule), and only build the backends you need. +// Learn about Dear ImGui: +// - FAQ https://dearimgui.com/faq +// - Getting Started https://dearimgui.com/getting-started +// - Documentation https://dearimgui.com/docs (same as your local docs/ folder). +// - Introduction, links and more at the top of imgui.cpp + +#pragma once +#include "imgui.h" // IMGUI_IMPL_API +#ifndef IMGUI_DISABLE + +struct SDL_Window; +struct SDL_Renderer; +struct SDL_Gamepad; +typedef union SDL_Event SDL_Event; + +// Follow "Getting Started" link and check examples/ folder to learn about using backends! +IMGUI_IMPL_API bool ImGui_ImplSDL3_InitForOpenGL(SDL_Window* window, void* sdl_gl_context); +IMGUI_IMPL_API bool ImGui_ImplSDL3_InitForVulkan(SDL_Window* window); +IMGUI_IMPL_API bool ImGui_ImplSDL3_InitForD3D(SDL_Window* window); +IMGUI_IMPL_API bool ImGui_ImplSDL3_InitForMetal(SDL_Window* window); +IMGUI_IMPL_API bool ImGui_ImplSDL3_InitForSDLRenderer(SDL_Window* window, SDL_Renderer* renderer); +IMGUI_IMPL_API bool ImGui_ImplSDL3_InitForSDLGPU(SDL_Window* window); +IMGUI_IMPL_API bool ImGui_ImplSDL3_InitForOther(SDL_Window* window); +IMGUI_IMPL_API void ImGui_ImplSDL3_Shutdown(); +IMGUI_IMPL_API void ImGui_ImplSDL3_NewFrame(); +IMGUI_IMPL_API bool ImGui_ImplSDL3_ProcessEvent(const SDL_Event* event); + +// Gamepad selection automatically starts in AutoFirst mode, picking first available SDL_Gamepad. You may override this. +// When using manual mode, caller is responsible for opening/closing gamepad. +enum ImGui_ImplSDL3_GamepadMode { ImGui_ImplSDL3_GamepadMode_AutoFirst, ImGui_ImplSDL3_GamepadMode_AutoAll, ImGui_ImplSDL3_GamepadMode_Manual }; +IMGUI_IMPL_API void ImGui_ImplSDL3_SetGamepadMode(ImGui_ImplSDL3_GamepadMode mode, SDL_Gamepad** manual_gamepads_array = nullptr, int manual_gamepads_count = -1); + +#endif // #ifndef IMGUI_DISABLE diff --git a/src/components/ramachandran/ramachandran.cpp b/src/components/ramachandran/ramachandran.cpp index f9832a49..316d93c7 100644 --- a/src/components/ramachandran/ramachandran.cpp +++ b/src/components/ramachandran/ramachandran.cpp @@ -25,6 +25,7 @@ #include "gfx/gl.h" #include "gfx/gl_utils.h" +#include "gfx/gpu.h" #include "image.h" #include "task_system.h" @@ -390,43 +391,56 @@ static void blur_density_gaussian(vec4_t* data, int dim, float sigma) { static void rama_rep_init(rama_rep_t* rep) { ASSERT(rep); - glGenTextures(1, &rep->den_tex); - glGenTextures(4, rep->map_tex); - glGenTextures(4, rep->iso_tex); - - glBindTexture(GL_TEXTURE_2D, rep->den_tex); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); - glTexStorage2D(GL_TEXTURE_2D, 1, GL_RGBA32F, density_tex_dim, density_tex_dim); - - + // Create density texture using GPU API + gpu::TextureDesc den_desc = { + .width = (int)density_tex_dim, + .height = (int)density_tex_dim, + .internal_format = GL_RGBA32F, + .format = GL_RGBA, + .type = GL_FLOAT, + .min_filter = GL_LINEAR, + .mag_filter = GL_LINEAR, + .wrap_s = GL_REPEAT, + .wrap_t = GL_REPEAT, + }; + gpu::Texture den_tex = gpu::create_texture_2d(den_desc); + rep->den_tex = den_tex.id; + + // Create map textures + gpu::TextureDesc map_desc = { + .width = (int)tex_dim, + .height = (int)tex_dim, + .internal_format = GL_RGBA8, + .format = GL_RGBA, + .type = GL_UNSIGNED_BYTE, + .min_filter = GL_LINEAR, + .mag_filter = GL_LINEAR, + .wrap_s = GL_REPEAT, + .wrap_t = GL_REPEAT, + }; for (int i = 0; i < 4; ++i) { - glBindTexture(GL_TEXTURE_2D, rep->map_tex[i]); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); - glTexStorage2D(GL_TEXTURE_2D, 1, GL_RGBA8, tex_dim, tex_dim); + gpu::Texture map_tex = gpu::create_texture_2d(map_desc); + rep->map_tex[i] = map_tex.id; } + // Create iso textures for (int i = 0; i < 4; ++i) { - glBindTexture(GL_TEXTURE_2D, rep->iso_tex[i]); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); - glTexStorage2D(GL_TEXTURE_2D, 1, GL_RGBA8, tex_dim, tex_dim); + gpu::Texture iso_tex = gpu::create_texture_2d(map_desc); + rep->iso_tex[i] = iso_tex.id; } - - glBindTexture(GL_TEXTURE_2D, 0); } static void rama_rep_free(rama_rep_t* rep) { - glDeleteTextures(1, &rep->den_tex); - glDeleteTextures(4, rep->map_tex); - glDeleteTextures(4, rep->iso_tex); + gpu::Texture den_tex = {rep->den_tex}; + gpu::destroy_texture(den_tex); + + for (int i = 0; i < 4; ++i) { + gpu::Texture map_tex = {rep->map_tex[i]}; + gpu::destroy_texture(map_tex); + + gpu::Texture iso_tex = {rep->iso_tex[i]}; + gpu::destroy_texture(iso_tex); + } } struct Ramachandran : viamd::EventHandler { @@ -595,11 +609,13 @@ struct Ramachandran : viamd::EventHandler { } if (!fbo) { - glGenFramebuffers(1, &fbo); + gpu::Framebuffer fb = gpu::create_framebuffer(tex_dim, tex_dim); + fbo = fb.fbo; } if (!vao) { - glGenVertexArrays(1, &vao); + gpu::VertexArray va = gpu::create_vertex_array(); + vao = va.vao; } rama_rep_init(&rama_data.ref); diff --git a/src/gfx/gpu.cpp b/src/gfx/gpu.cpp new file mode 100644 index 00000000..94da99ef --- /dev/null +++ b/src/gfx/gpu.cpp @@ -0,0 +1,369 @@ +// GPU - Low-level GPU API implementation + +#include "gpu.h" +#include "gl_utils.h" +#include + +namespace gpu { + +// Texture management +Texture create_texture_2d(const TextureDesc& desc) { + Texture tex; + tex.width = desc.width; + tex.height = desc.height; + tex.depth = 1; + + glGenTextures(1, &tex.id); + glBindTexture(GL_TEXTURE_2D, tex.id); + + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, desc.min_filter); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, desc.mag_filter); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, desc.wrap_s); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, desc.wrap_t); + + // Use glTexStorage2D for immutable storage if no data provided, otherwise use glTexImage2D + if (desc.data == nullptr) { + glTexStorage2D(GL_TEXTURE_2D, 1, desc.internal_format, desc.width, desc.height); + } else { + glTexImage2D(GL_TEXTURE_2D, 0, desc.internal_format, desc.width, desc.height, 0, + desc.format, desc.type, desc.data); + } + + glBindTexture(GL_TEXTURE_2D, 0); + + return tex; +} + +Texture create_texture_3d(const TextureDesc& desc) { + Texture tex; + tex.width = desc.width; + tex.height = desc.height; + tex.depth = desc.depth; + + glGenTextures(1, &tex.id); + glBindTexture(GL_TEXTURE_3D, tex.id); + + glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, desc.min_filter); + glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, desc.mag_filter); + glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, desc.wrap_s); + glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, desc.wrap_t); + glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, desc.wrap_r); + + glTexImage3D(GL_TEXTURE_3D, 0, desc.internal_format, desc.width, desc.height, desc.depth, 0, + desc.format, desc.type, desc.data); + + glBindTexture(GL_TEXTURE_3D, 0); + + return tex; +} + +void update_texture_2d(Texture& tex, const void* data, int width, int height) { + if (tex.id == 0) return; + + glBindTexture(GL_TEXTURE_2D, tex.id); + glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, data); + glBindTexture(GL_TEXTURE_2D, 0); + + tex.width = width; + tex.height = height; +} + +void update_texture_3d(Texture& tex, const void* data, int width, int height, int depth) { + if (tex.id == 0) return; + + glBindTexture(GL_TEXTURE_3D, tex.id); + glTexSubImage3D(GL_TEXTURE_3D, 0, 0, 0, 0, width, height, depth, GL_RGBA, GL_UNSIGNED_BYTE, data); + glBindTexture(GL_TEXTURE_3D, 0); + + tex.width = width; + tex.height = height; + tex.depth = depth; +} + +void destroy_texture(Texture& tex) { + if (tex.id != 0) { + glDeleteTextures(1, &tex.id); + tex.id = 0; + tex.width = 0; + tex.height = 0; + tex.depth = 0; + } +} + +void bind_texture(const Texture& tex, uint32_t slot) { + glActiveTexture(GL_TEXTURE0 + slot); + if (tex.depth > 1) { + glBindTexture(GL_TEXTURE_3D, tex.id); + } else { + glBindTexture(GL_TEXTURE_2D, tex.id); + } +} + +void unbind_texture(uint32_t slot) { + glActiveTexture(GL_TEXTURE0 + slot); + glBindTexture(GL_TEXTURE_2D, 0); + glBindTexture(GL_TEXTURE_3D, 0); +} + +// Shader management +Shader create_shader(const ShaderSource& source) { + Shader shader; + shader.program = glCreateProgram(); + + // Compile vertex shader + if (source.vertex) { + uint32_t vs = glCreateShader(GL_VERTEX_SHADER); + glShaderSource(vs, 1, &source.vertex, nullptr); + glCompileShader(vs); + + // Check compilation + int success; + glGetShaderiv(vs, GL_COMPILE_STATUS, &success); + if (!success) { + char log[512]; + glGetShaderInfoLog(vs, 512, nullptr, log); + MD_LOG_ERROR("Vertex shader compilation failed: %s", log); + } + glAttachShader(shader.program, vs); + glDeleteShader(vs); + } + + // Compile fragment shader + if (source.fragment) { + uint32_t fs = glCreateShader(GL_FRAGMENT_SHADER); + glShaderSource(fs, 1, &source.fragment, nullptr); + glCompileShader(fs); + + // Check compilation + int success; + glGetShaderiv(fs, GL_COMPILE_STATUS, &success); + if (!success) { + char log[512]; + glGetShaderInfoLog(fs, 512, nullptr, log); + MD_LOG_ERROR("Fragment shader compilation failed: %s", log); + } + glAttachShader(shader.program, fs); + glDeleteShader(fs); + } + + // Compile geometry shader + if (source.geometry) { + uint32_t gs = glCreateShader(GL_GEOMETRY_SHADER); + glShaderSource(gs, 1, &source.geometry, nullptr); + glCompileShader(gs); + + // Check compilation + int success; + glGetShaderiv(gs, GL_COMPILE_STATUS, &success); + if (!success) { + char log[512]; + glGetShaderInfoLog(gs, 512, nullptr, log); + MD_LOG_ERROR("Geometry shader compilation failed: %s", log); + } + glAttachShader(shader.program, gs); + glDeleteShader(gs); + } + + // Link program + glLinkProgram(shader.program); + + // Check linking + int success; + glGetProgramiv(shader.program, GL_LINK_STATUS, &success); + if (!success) { + char log[512]; + glGetProgramInfoLog(shader.program, 512, nullptr, log); + MD_LOG_ERROR("Shader program linking failed: %s", log); + } + + return shader; +} + +void destroy_shader(Shader& shader) { + if (shader.program != 0) { + glDeleteProgram(shader.program); + shader.program = 0; + } +} + +void use_shader(const Shader& shader) { + glUseProgram(shader.program); +} + +void set_uniform(const Shader& shader, const char* name, int value) { + int loc = glGetUniformLocation(shader.program, name); + if (loc != -1) { + glUniform1i(loc, value); + } +} + +void set_uniform(const Shader& shader, const char* name, float value) { + int loc = glGetUniformLocation(shader.program, name); + if (loc != -1) { + glUniform1f(loc, value); + } +} + +void set_uniform(const Shader& shader, const char* name, const vec2_t& value) { + int loc = glGetUniformLocation(shader.program, name); + if (loc != -1) { + glUniform2f(loc, value.x, value.y); + } +} + +void set_uniform(const Shader& shader, const char* name, const vec3_t& value) { + int loc = glGetUniformLocation(shader.program, name); + if (loc != -1) { + glUniform3f(loc, value.x, value.y, value.z); + } +} + +void set_uniform(const Shader& shader, const char* name, const vec4_t& value) { + int loc = glGetUniformLocation(shader.program, name); + if (loc != -1) { + glUniform4f(loc, value.x, value.y, value.z, value.w); + } +} + +void set_uniform(const Shader& shader, const char* name, const mat4_t& value) { + int loc = glGetUniformLocation(shader.program, name); + if (loc != -1) { + glUniformMatrix4fv(loc, 1, GL_FALSE, &value.elem[0][0]); + } +} + +// Framebuffer management +Framebuffer create_framebuffer(int width, int height) { + Framebuffer fb; + fb.width = width; + fb.height = height; + + glGenFramebuffers(1, &fb.fbo); + + return fb; +} + +void destroy_framebuffer(Framebuffer& fb) { + if (fb.fbo != 0) { + glDeleteFramebuffers(1, &fb.fbo); + fb.fbo = 0; + fb.width = 0; + fb.height = 0; + } +} + +void bind_framebuffer(const Framebuffer& fb) { + glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fb.fbo); +} + +void unbind_framebuffer() { + glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0); +} + +void attach_texture_to_framebuffer(const Framebuffer& fb, const Texture& tex, GLenum attachment) { + glBindFramebuffer(GL_FRAMEBUFFER, fb.fbo); + if (tex.depth > 1) { + glFramebufferTexture(GL_FRAMEBUFFER, attachment, tex.id, 0); + } else { + glFramebufferTexture2D(GL_FRAMEBUFFER, attachment, GL_TEXTURE_2D, tex.id, 0); + } + glBindFramebuffer(GL_FRAMEBUFFER, 0); +} + +void set_draw_buffers(const GLenum* attachments, int count) { + glDrawBuffers(count, attachments); +} + +// Vertex Array Object management +VertexArray create_vertex_array() { + VertexArray vao; + glGenVertexArrays(1, &vao.vao); + return vao; +} + +void destroy_vertex_array(VertexArray& vao) { + if (vao.vao != 0) { + glDeleteVertexArrays(1, &vao.vao); + vao.vao = 0; + } +} + +void bind_vertex_array(const VertexArray& vao) { + glBindVertexArray(vao.vao); +} + +void unbind_vertex_array() { + glBindVertexArray(0); +} + +// Rendering state +void set_viewport(int x, int y, int width, int height) { + glViewport(x, y, width, height); +} + +void set_scissor(int x, int y, int width, int height) { + glScissor(x, y, width, height); +} + +void enable_blend(GLenum src, GLenum dst) { + glEnable(GL_BLEND); + glBlendFunc(src, dst); +} + +void disable_blend() { + glDisable(GL_BLEND); +} + +void enable_depth_test(GLenum func) { + glEnable(GL_DEPTH_TEST); + glDepthFunc(func); +} + +void disable_depth_test() { + glDisable(GL_DEPTH_TEST); +} + +void enable_cull_face(GLenum mode) { + glEnable(GL_CULL_FACE); + glCullFace(mode); +} + +void disable_cull_face() { + glDisable(GL_CULL_FACE); +} + +void enable_scissor_test() { + glEnable(GL_SCISSOR_TEST); +} + +void disable_scissor_test() { + glDisable(GL_SCISSOR_TEST); +} + +void set_depth_mask(bool enable) { + glDepthMask(enable ? GL_TRUE : GL_FALSE); +} + +void set_color_mask(bool r, bool g, bool b, bool a) { + glColorMask(r ? GL_TRUE : GL_FALSE, g ? GL_TRUE : GL_FALSE, + b ? GL_TRUE : GL_FALSE, a ? GL_TRUE : GL_FALSE); +} + +void clear_color(float r, float g, float b, float a) { + glClearColor(r, g, b, a); +} + +void clear(GLbitfield mask) { + glClear(mask); +} + +// Drawing +void draw_arrays(GLenum mode, int first, int count) { + glDrawArrays(mode, first, count); +} + +void draw_elements(GLenum mode, int count, GLenum type, const void* indices) { + glDrawElements(mode, count, type, indices); +} + +} // namespace gpu diff --git a/src/gfx/gpu.h b/src/gfx/gpu.h new file mode 100644 index 00000000..2590e907 --- /dev/null +++ b/src/gfx/gpu.h @@ -0,0 +1,108 @@ +// GPU - Low-level GPU API abstraction +// Handles textures, shaders, framebuffers, and other GPU resources + +#pragma once + +#include "gl.h" +#include +#include + +namespace gpu { + +// Texture management +struct Texture { + uint32_t id = 0; + int width = 0; + int height = 0; + int depth = 0; // For 3D textures +}; + +struct TextureDesc { + int width = 0; + int height = 0; + int depth = 1; // 1 for 2D, >1 for 3D + GLenum internal_format = GL_RGBA8; + GLenum format = GL_RGBA; + GLenum type = GL_UNSIGNED_BYTE; + GLenum min_filter = GL_LINEAR; + GLenum mag_filter = GL_LINEAR; + GLenum wrap_s = GL_CLAMP_TO_EDGE; + GLenum wrap_t = GL_CLAMP_TO_EDGE; + GLenum wrap_r = GL_CLAMP_TO_EDGE; + const void* data = nullptr; +}; + +Texture create_texture_2d(const TextureDesc& desc); +Texture create_texture_3d(const TextureDesc& desc); +void update_texture_2d(Texture& tex, const void* data, int width, int height); +void update_texture_3d(Texture& tex, const void* data, int width, int height, int depth); +void destroy_texture(Texture& tex); +void bind_texture(const Texture& tex, uint32_t slot = 0); +void unbind_texture(uint32_t slot = 0); + +// Shader management +struct Shader { + uint32_t program = 0; +}; + +struct ShaderSource { + const char* vertex = nullptr; + const char* fragment = nullptr; + const char* geometry = nullptr; +}; + +Shader create_shader(const ShaderSource& source); +void destroy_shader(Shader& shader); +void use_shader(const Shader& shader); +void set_uniform(const Shader& shader, const char* name, int value); +void set_uniform(const Shader& shader, const char* name, float value); +void set_uniform(const Shader& shader, const char* name, const vec2_t& value); +void set_uniform(const Shader& shader, const char* name, const vec3_t& value); +void set_uniform(const Shader& shader, const char* name, const vec4_t& value); +void set_uniform(const Shader& shader, const char* name, const mat4_t& value); + +// Framebuffer management +struct Framebuffer { + uint32_t fbo = 0; + uint32_t width = 0; + uint32_t height = 0; +}; + +Framebuffer create_framebuffer(int width, int height); +void destroy_framebuffer(Framebuffer& fb); +void bind_framebuffer(const Framebuffer& fb); +void unbind_framebuffer(); +void attach_texture_to_framebuffer(const Framebuffer& fb, const Texture& tex, GLenum attachment); +void set_draw_buffers(const GLenum* attachments, int count); + +// Vertex Array Object management +struct VertexArray { + uint32_t vao = 0; +}; + +VertexArray create_vertex_array(); +void destroy_vertex_array(VertexArray& vao); +void bind_vertex_array(const VertexArray& vao); +void unbind_vertex_array(); + +// Rendering state +void set_viewport(int x, int y, int width, int height); +void set_scissor(int x, int y, int width, int height); +void enable_blend(GLenum src = GL_SRC_ALPHA, GLenum dst = GL_ONE_MINUS_SRC_ALPHA); +void disable_blend(); +void enable_depth_test(GLenum func = GL_LESS); +void disable_depth_test(); +void enable_cull_face(GLenum mode = GL_BACK); +void disable_cull_face(); +void enable_scissor_test(); +void disable_scissor_test(); +void set_depth_mask(bool enable); +void set_color_mask(bool r, bool g, bool b, bool a); +void clear_color(float r, float g, float b, float a); +void clear(GLbitfield mask); + +// Drawing +void draw_arrays(GLenum mode, int first, int count); +void draw_elements(GLenum mode, int count, GLenum type, const void* indices); + +} // namespace gpu diff --git a/src/gfx/renderer.cpp b/src/gfx/renderer.cpp new file mode 100644 index 00000000..ca738bda --- /dev/null +++ b/src/gfx/renderer.cpp @@ -0,0 +1,437 @@ +// Renderer implementation +// This file contains all OpenGL rendering code for VIAMD molecular visualization + +#include "renderer.h" +#include "immediate_draw_utils.h" +#include "postprocessing_utils.h" +#include "volumerender_utils.h" +#include "gl_utils.h" + +#include +#include +#include + +#include + +#include +#include + +// External allocators +extern md_allocator_i* frame_alloc; +extern md_allocator_i* persistent_alloc; + +namespace renderer { + +// Helper macros +#define PUSH_GPU_SECTION(lbl) { if (glPushDebugGroup) glPushDebugGroup(GL_DEBUG_SOURCE_APPLICATION, GL_KHR_debug, -1, lbl); } +#define POP_GPU_SECTION() { if (glPopDebugGroup) glPopDebugGroup(); } + +#define HIGHLIGHT_PULSE_TIME_SCALE 5.0 +#define HIGHLIGHT_PULSE_ALPHA_SCALE 0.1 + +void initialize() { + ::immediate::initialize(); +} + +void shutdown() { + ::immediate::shutdown(); +} + +void set_viewport(int x, int y, int width, int height) { + glViewport(x, y, width, height); + glScissor(x, y, width, height); +} + +void clear_framebuffer(const vec4_t& color, float depth, int stencil) { + glClearColor(color.x, color.y, color.z, color.w); + glClearDepth(depth); + glClearStencil(stencil); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); +} + +void bind_default_framebuffer() { + glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0); + glDrawBuffer(GL_BACK); +} + +void clear_gbuffer(GBuffer& gbuffer) { + ::clear_gbuffer(&gbuffer); +} + +void begin_frame(ApplicationState* state) { + (void)state; + // Frame initialization if needed +} + +void end_frame(ApplicationState* state) { + (void)state; + // Frame cleanup if needed +} + +void draw_representations_opaque(ApplicationState* data) { + ASSERT(data); + if (data->mold.sys.atom.count == 0) return; + + const size_t num_representations = md_array_size(data->representation.reps); + if (num_representations == 0) return; + + md_array(md_gl_draw_op_t) draw_ops = 0; + for (size_t i = 0; i < num_representations; ++i) { + const Representation& rep = data->representation.reps[i]; + + if (rep.type > RepresentationType::Cartoon) continue; + + if (rep.enabled && rep.type_is_valid) { + md_gl_draw_op_t op = { + .type = (md_gl_rep_type_t)rep.type, + .args = {}, + .rep = data->representation.reps[i].md_rep, + .model_matrix = NULL, + }; + MEMCPY(&op.args, &rep.scale, sizeof(op.args)); + md_array_push(draw_ops, op, frame_alloc); + } + } + + md_gl_draw_args_t args = { + .shaders = data->mold.gl_shaders, + .draw_operations = { + .count = (uint32_t)md_array_size(draw_ops), + .ops = draw_ops, + }, + .view_transform = { + .view_matrix = &data->view.param.matrix.curr.view.elem[0][0], + .proj_matrix = &data->view.param.matrix.curr.proj.elem[0][0], + .prev_view_matrix = &data->view.param.matrix.prev.view.elem[0][0], + .prev_proj_matrix = &data->view.param.matrix.prev.proj.elem[0][0], + }, + }; + + md_gl_draw(&args); +} + +void draw_representations_transparent(ApplicationState* state) { + ASSERT(state); + if (state->mold.sys.atom.count == 0) return; + + const size_t num_representations = md_array_size(state->representation.reps); + if (num_representations == 0) return; + + for (size_t i = 0; i < num_representations; ++i) { + const Representation& rep = state->representation.reps[i]; + if (!rep.enabled) continue; + if (rep.type != RepresentationType::ElectronicStructure) continue; + + const IsoDesc* iso = nullptr; + + switch (rep.electronic_structure.type) { + case ElectronicStructureType::MolecularOrbital: + case ElectronicStructureType::NaturalTransitionOrbitalParticle: + case ElectronicStructureType::NaturalTransitionOrbitalHole: + iso = &rep.electronic_structure.iso_psi; + break; + case ElectronicStructureType::MolecularOrbitalDensity: + case ElectronicStructureType::NaturalTransitionOrbitalDensityParticle: + case ElectronicStructureType::NaturalTransitionOrbitalDensityHole: + case ElectronicStructureType::AttachmentDensity: + case ElectronicStructureType::DetachmentDensity: + case ElectronicStructureType::ElectronDensity: + iso = &rep.electronic_structure.iso_den; + break; + default: + ASSERT(false); + } + + volume::RenderDesc desc = { + .render_target = { + .depth = state->gbuffer.tex.depth, + .color = state->gbuffer.tex.transparency, + .width = state->gbuffer.width, + .height = state->gbuffer.height, + }, + .texture = { + .volume = rep.electronic_structure.vol.tex_id, + .transfer_function = rep.electronic_structure.dvr.tf_tex, + }, + .matrix = { + .model = rep.electronic_structure.vol.texture_to_world, + .view = state->view.param.matrix.curr.view, + .proj = state->view.param.matrix.curr.proj, + .inv_proj = state->view.param.matrix.inv.proj, + }, + .clip_volume = { + .min = {0,0,0}, + .max = {1,1,1}, + }, + .temporal = { + .enabled = state->visuals.temporal_aa.enabled, + }, + .iso = { + .enabled = iso->enabled, + .count = iso->count, + .values = iso->values, + .colors = iso->colors, + }, + .dvr = { + .enabled = rep.electronic_structure.dvr.enabled, + .min_tf_value = -1.0f, + .max_tf_value = 1.0f, + }, + .shading = { + .env_radiance = state->visuals.background.color * state->visuals.background.intensity * 0.25f, + .roughness = 0.3f, + .dir_radiance = {10,10,10}, + .ior = 1.5f, + }, + .voxel_spacing = rep.electronic_structure.vol.voxel_size, + }; + + volume::render_volume(desc); + } +} + +void draw_representations_opaque_lean_and_mean(ApplicationState* data, uint32_t mask) { + md_gl_draw_op_t* draw_ops = 0; + for (size_t i = 0; i < md_array_size(data->representation.reps); ++i) { + const Representation& rep = data->representation.reps[i]; + + if (rep.type > RepresentationType::Cartoon) continue; + + if (rep.enabled && rep.type_is_valid) { + md_gl_draw_op_t op = { + .type = (md_gl_rep_type_t)rep.type, + .args = {}, + .rep = data->representation.reps[i].md_rep, + .model_matrix = NULL, + }; + MEMCPY(&op.args, &rep.scale, sizeof(op.args)); + md_array_push(draw_ops, op, frame_alloc); + } + } + + md_gl_draw_args_t args = { + .shaders = data->mold.gl_shaders_lean_and_mean, + .draw_operations = { + .count = md_array_size(draw_ops), + .ops = draw_ops, + }, + .view_transform = { + .view_matrix = &data->view.param.matrix.curr.view.elem[0][0], + .proj_matrix = &data->view.param.matrix.curr.proj.elem[0][0], + }, + .atom_mask = mask, + }; + + md_gl_draw(&args); +} + +void fill_gbuffer(ApplicationState* data) { + const GLenum draw_buffers[] = { + GL_COLOR_ATTACHMENT_COLOR, + GL_COLOR_ATTACHMENT_NORMAL, + GL_COLOR_ATTACHMENT_VELOCITY, + GL_COLOR_ATTACHMENT_PICKING, + GL_COLOR_ATTACHMENT_TRANSPARENCY + }; + + glEnable(GL_CULL_FACE); + glCullFace(GL_BACK); + + glEnable(GL_DEPTH_TEST); + glDepthFunc(GL_LESS); + + // Enable all draw buffers + glBindFramebuffer(GL_DRAW_FRAMEBUFFER, data->gbuffer.fbo); + glDrawBuffers((int)ARRAY_SIZE(draw_buffers), draw_buffers); + + PUSH_GPU_SECTION("G-Buffer fill") + + // Draw simulation box if enabled + if (data->simulation_box.enabled && data->mold.sys.unitcell.flags != 0) { + PUSH_GPU_SECTION("Draw Simulation Box") + const mat4_t basis_model_mat = md_unitcell_basis_mat4(&data->mold.sys.unitcell); + ::immediate::set_model_view_matrix(data->view.param.matrix.curr.view); + ::immediate::set_proj_matrix(data->view.param.matrix.curr.proj); + ::immediate::draw_box_wireframe({0,0,0}, {1,1,1}, basis_model_mat, convert_color(data->simulation_box.color)); + ::immediate::render(); + POP_GPU_SECTION() + } + + // Draw velocity of static objects + PUSH_GPU_SECTION("Blit Static Velocity") + glDrawBuffer(GL_COLOR_ATTACHMENT_VELOCITY); + glDepthMask(0); + postprocessing::blit_static_velocity(data->gbuffer.tex.depth, data->view.param); + glDepthMask(1); + POP_GPU_SECTION() + + glDepthMask(1); + glColorMask(1, 1, 1, 1); + + // Draw representations + PUSH_GPU_SECTION("Draw Opaque") + glDrawBuffers((int)ARRAY_SIZE(draw_buffers), draw_buffers); + draw_representations_opaque(data); + viamd::event_system_broadcast_event(viamd::EventType_ViamdRenderOpaque, viamd::EventPayloadType_ApplicationState, data); + POP_GPU_SECTION() + + glDrawBuffer(GL_COLOR_ATTACHMENT_TRANSPARENCY); + + // Selection and highlight overlays + PUSH_GPU_SECTION("Selection") + const bool atom_selection_empty = md_bitfield_popcount(&data->selection.selection_mask) == 0; + const bool atom_highlight_empty = md_bitfield_popcount(&data->selection.highlight_mask) == 0; + + glDepthMask(0); + + if (!atom_selection_empty) { + glColorMask(0, 0, 0, 0); + + glEnable(GL_DEPTH_TEST); + glDepthFunc(GL_EQUAL); + + glEnable(GL_STENCIL_TEST); + glStencilMask(0xFF); + + glClearStencil(1); + glClear(GL_STENCIL_BUFFER_BIT); + + glStencilFunc(GL_GREATER, 0x02, 0xFF); + glStencilOp(GL_KEEP, GL_ZERO, GL_REPLACE); + draw_representations_opaque_lean_and_mean(data, AtomBit_Selected | AtomBit_Visible); + + glDisable(GL_DEPTH_TEST); + + glStencilMask(0x0); + glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP); + glColorMask(1, 1, 1, 1); + + glStencilFunc(GL_EQUAL, 2, 0xFF); + postprocessing::blit_color(data->selection.color.selection.visible); + + glStencilFunc(GL_EQUAL, 0, 0xFF); + postprocessing::blit_color(data->selection.color.selection.hidden); + } + + if (!atom_highlight_empty) { + glColorMask(0, 0, 0, 0); + + glEnable(GL_DEPTH_TEST); + glDepthFunc(GL_EQUAL); + + glEnable(GL_STENCIL_TEST); + glStencilMask(0xFF); + + glClearStencil(1); + glClear(GL_STENCIL_BUFFER_BIT); + + glStencilFunc(GL_GREATER, 0x02, 0xFF); + glStencilOp(GL_KEEP, GL_ZERO, GL_REPLACE); + draw_representations_opaque_lean_and_mean(data, AtomBit_Highlighted | AtomBit_Visible); + + glDisable(GL_DEPTH_TEST); + + glStencilMask(0x0); + glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP); + glColorMask(1, 1, 1, 1); + + glStencilFunc(GL_EQUAL, 2, 0xFF); + vec4_t col_vis = data->selection.color.highlight.visible; + col_vis.w += sin(ImGui::GetTime() * HIGHLIGHT_PULSE_TIME_SCALE) * HIGHLIGHT_PULSE_ALPHA_SCALE; + postprocessing::blit_color(col_vis); + + glStencilFunc(GL_EQUAL, 0, 0xFF); + postprocessing::blit_color(data->selection.color.highlight.hidden); + } + + glDisable(GL_STENCIL_TEST); + + if (!atom_selection_empty) { + PUSH_GPU_SECTION("Desaturate") + const float saturation = data->selection.color.saturation; + glDrawBuffer(GL_COLOR_ATTACHMENT_COLOR); + postprocessing::scale_hsv(data->gbuffer.tex.color, vec3_t{1, saturation, 1}); + POP_GPU_SECTION() + } + + glDepthFunc(GL_LESS); + glDepthMask(1); + glColorMask(1, 1, 1, 1); + POP_GPU_SECTION() + + // Draw transparent representations + PUSH_GPU_SECTION("Draw Transparent") + draw_representations_transparent(data); + viamd::event_system_broadcast_event(viamd::EventType_ViamdRenderTransparent, viamd::EventPayloadType_ApplicationState, data); + POP_GPU_SECTION() + + glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0); + glDisable(GL_DEPTH_TEST); + POP_GPU_SECTION() +} + +void apply_postprocessing(ApplicationState* state) { + PUSH_GPU_SECTION("Postprocessing") + postprocessing::Descriptor desc; + + desc.background.color = state->visuals.background.color * state->visuals.background.intensity; + + desc.ambient_occlusion.enabled = state->visuals.ssao.enabled; + desc.ambient_occlusion.intensity = state->visuals.ssao.intensity; + desc.ambient_occlusion.radius = state->visuals.ssao.radius; + desc.ambient_occlusion.bias = state->visuals.ssao.bias; + + desc.tonemapping.enabled = state->visuals.tonemapping.enabled; + desc.tonemapping.mode = state->visuals.tonemapping.tonemapper; + desc.tonemapping.exposure = state->visuals.tonemapping.exposure; + desc.tonemapping.gamma = state->visuals.tonemapping.gamma; + + desc.depth_of_field.enabled = state->visuals.dof.enabled; + desc.depth_of_field.focus_depth = state->visuals.dof.focus_depth; + desc.depth_of_field.focus_scale = state->visuals.dof.focus_scale; + + desc.fxaa.enabled = state->visuals.fxaa.enabled; + + constexpr float MOTION_BLUR_REFERENCE_DT = 1.0f / 60.0f; + const float dt_compensation = MOTION_BLUR_REFERENCE_DT / (float)state->app.timing.delta_s; + const float motion_scale = state->visuals.temporal_aa.motion_blur.motion_scale * dt_compensation; + desc.temporal_aa.enabled = state->visuals.temporal_aa.enabled; + desc.temporal_aa.feedback_min = state->visuals.temporal_aa.feedback_min; + desc.temporal_aa.feedback_max = state->visuals.temporal_aa.feedback_max; + desc.temporal_aa.motion_blur.enabled = state->visuals.temporal_aa.motion_blur.enabled; + desc.temporal_aa.motion_blur.motion_scale = motion_scale; + + desc.sharpen.enabled = state->visuals.temporal_aa.enabled && state->visuals.sharpen.enabled; + desc.sharpen.weight = state->visuals.sharpen.weight; + + desc.input_textures.depth = state->gbuffer.tex.depth; + desc.input_textures.color = state->gbuffer.tex.color; + desc.input_textures.normal = state->gbuffer.tex.normal; + desc.input_textures.velocity = state->gbuffer.tex.velocity; + desc.input_textures.transparency = state->gbuffer.tex.transparency; + + postprocessing::shade_and_postprocess(desc, state->view.param); + POP_GPU_SECTION() +} + +void render_scene(ApplicationState* state) { + clear_gbuffer(state->gbuffer); + fill_gbuffer(state); +} + +namespace immediate { + void begin(const mat4_t& view_matrix, const mat4_t& proj_matrix) { + ::immediate::set_model_view_matrix(view_matrix); + ::immediate::set_proj_matrix(proj_matrix); + } + + void end() { + ::immediate::render(); + } + + void flush() { + ::immediate::render(); + } +} + +} // namespace renderer diff --git a/src/gfx/renderer.h b/src/gfx/renderer.h new file mode 100644 index 00000000..1d65fe95 --- /dev/null +++ b/src/gfx/renderer.h @@ -0,0 +1,63 @@ +// Renderer - Abstracts all rendering operations for VIAMD +// This module encapsulates OpenGL rendering calls and provides a clean interface +// for rendering molecular representations, immediate graphics, and post-processing + +#pragma once + +#include +#include + +#include "gl.h" +#include "view_param.h" +#include "postprocessing_utils.h" + +// Forward declarations +struct ApplicationState; +struct Representation; + +namespace renderer { + +// Initialization and cleanup +void initialize(); +void shutdown(); + +// Main rendering entry points +void begin_frame(ApplicationState* state); +void end_frame(ApplicationState* state); + +// Rendering passes +void render_scene(ApplicationState* state); +void render_molecular_representations(ApplicationState* state); +void render_volume_representations(ApplicationState* state); +void render_immediate_graphics(ApplicationState* state); +void render_ui_overlay(ApplicationState* state); + +// Viewport and framebuffer management +void set_viewport(int x, int y, int width, int height); +void clear_framebuffer(const vec4_t& color, float depth = 1.0f, int stencil = 0); +void bind_default_framebuffer(); + +// GBuffer operations +void clear_gbuffer(GBuffer& gbuffer); +void fill_gbuffer(ApplicationState* state); + +// Representation rendering +void draw_representations_opaque(ApplicationState* state); +void draw_representations_transparent(ApplicationState* state); +void draw_representations_opaque_lean_and_mean(ApplicationState* state, uint32_t mask); + +// Post-processing +void apply_postprocessing(ApplicationState* state); + +// Immediate mode rendering +namespace immediate { + void begin(const mat4_t& view_matrix, const mat4_t& proj_matrix); + void end(); + void flush(); + + void draw_simulation_box(ApplicationState* state); + void draw_selection_overlay(ApplicationState* state); + void draw_highlight_overlay(ApplicationState* state); +} + +} // namespace renderer diff --git a/src/main.cpp b/src/main.cpp index cc5d943a..054eeb71 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -49,6 +49,7 @@ #include #include +#include #include #include #include @@ -93,8 +94,8 @@ #define LOG_SUCCESS(...) ImGui::InsertNotification(ImGuiToast(ImGuiToastType_Success, 6000, __VA_ARGS__)) // Global data for application -static md_allocator_i* frame_alloc = 0; // Linear allocator for scratch data which only is valid for the frame and then is reset -static md_allocator_i* persistent_alloc = 0; +md_allocator_i* frame_alloc = 0; // Linear allocator for scratch data which only is valid for the frame and then is reset +md_allocator_i* persistent_alloc = 0; static TextEditor editor {}; static bool use_gfx = false; @@ -581,12 +582,6 @@ static void handle_camera_interaction(ApplicationState* data); //static void update_density_volume_texture(ApplicationState* data); static void handle_picking(ApplicationState* data); -static void fill_gbuffer(ApplicationState* data); -static void apply_postprocessing(const ApplicationState& data); - -static void draw_representations_opaque(ApplicationState*); -static void draw_representations_opaque_lean_and_mean(ApplicationState*, uint32_t mask = 0xFFFFFFFFU); -static void draw_representations_transparent(ApplicationState*); static void draw_load_dataset_window(ApplicationState* data); static void draw_main_menu(ApplicationState* data); @@ -1433,8 +1428,8 @@ int main(int argc, char** argv) { update_display_properties(&data); handle_picking(&data); - clear_gbuffer(&data.gbuffer); - fill_gbuffer(&data); + renderer::clear_gbuffer(data.gbuffer); + renderer::fill_gbuffer(&data); glDisable(GL_DEPTH_TEST); @@ -1451,7 +1446,7 @@ int main(int argc, char** argv) { glClear(GL_COLOR_BUFFER_BIT); } - apply_postprocessing(data); + renderer::apply_postprocessing(&data); if (do_screenshot && data.screenshot.hide_gui) { data.screenshot.sample_count += 1; @@ -9119,248 +9114,6 @@ static void handle_camera_interaction(ApplicationState* data) { } } -static void fill_gbuffer(ApplicationState* data) { - const GLenum draw_buffers[] = {GL_COLOR_ATTACHMENT_COLOR, GL_COLOR_ATTACHMENT_NORMAL, GL_COLOR_ATTACHMENT_VELOCITY, - GL_COLOR_ATTACHMENT_PICKING, GL_COLOR_ATTACHMENT_TRANSPARENCY }; - - glEnable(GL_CULL_FACE); - glCullFace(GL_BACK); - - glEnable(GL_DEPTH_TEST); - glDepthFunc(GL_LESS); - - // Enable all draw buffers - glBindFramebuffer(GL_DRAW_FRAMEBUFFER, data->gbuffer.fbo); - glDrawBuffers((int)ARRAY_SIZE(draw_buffers), draw_buffers); - - PUSH_GPU_SECTION("G-Buffer fill") - - // Immediate mode graphics - const mat4_t model_view_mat = data->view.param.matrix.curr.view; - - if (data->simulation_box.enabled && data->mold.sys.unitcell.flags != 0) { - PUSH_GPU_SECTION("Draw Simulation Box") - const mat4_t basis_model_mat = md_unitcell_basis_mat4(&data->mold.sys.unitcell); - immediate::set_model_view_matrix(model_view_mat); - immediate::set_proj_matrix(data->view.param.matrix.curr.proj); - immediate::draw_box_wireframe({0,0,0}, {1,1,1}, basis_model_mat, convert_color(data->simulation_box.color)); - immediate::render(); - POP_GPU_SECTION() - } - -#if 0 - // RENDER DEBUG INFORMATION (WITH DEPTH) - PUSH_GPU_SECTION("Debug Draw") { - glDrawBuffer(GL_COLOR_ATTACHMENT_TRANSPARENCY); - immediate::set_model_view_matrix(data->view.param.matrix.curr.view); - immediate::set_proj_matrix(data->view.param.matrix.curr.proj); - immediate::flush(); - } - POP_GPU_SECTION() - - PUSH_GPU_SECTION("Debug Draw Overlay") { - glDrawBuffer(GL_COLOR_ATTACHMENT_TRANSPARENCY); // Post_Tonemap buffer - glDisable(GL_DEPTH_TEST); - glDepthMask(0); - - // immediate::set_model_view_matrix(data->view.param.matrix.current.view); - // immediate::set_proj_matrix(data->view.param.matrix.current.proj); - // immediate::flush(); - - glEnable(GL_DEPTH_TEST); - glDepthMask(1); - } - POP_GPU_SECTION() -#endif - - if (!use_gfx) { - // DRAW VELOCITY OF STATIC OBJECTS - PUSH_GPU_SECTION("Blit Static Velocity") - glDrawBuffer(GL_COLOR_ATTACHMENT_VELOCITY); - glDepthMask(0); - postprocessing::blit_static_velocity(data->gbuffer.tex.depth, data->view.param); - glDepthMask(1); - POP_GPU_SECTION() - } - glDepthMask(1); - glColorMask(1, 1, 1, 1); - - // DRAW REPRESENTATIONS - PUSH_GPU_SECTION("Draw Opaque") - glDrawBuffers((int)ARRAY_SIZE(draw_buffers), draw_buffers); - draw_representations_opaque(data); - viamd::event_system_broadcast_event(viamd::EventType_ViamdRenderOpaque, viamd::EventPayloadType_ApplicationState, data); - POP_GPU_SECTION() - - glDrawBuffer(GL_COLOR_ATTACHMENT_TRANSPARENCY); - - if (!use_gfx) { - PUSH_GPU_SECTION("Selection") - const bool atom_selection_empty = md_bitfield_popcount(&data->selection.selection_mask) == 0; - const bool atom_highlight_empty = md_bitfield_popcount(&data->selection.highlight_mask) == 0; - - glDepthMask(0); - - // @NOTE(Robin): This is a b*tch to get right, What we want is to separate in a single pass, the visible selected from the - // non visible selected. In order to achieve this, we start with a cleared stencil of value 1 then either set it to zero selected and not visible - // and to two if it is selected and visible. But the visible atoms should always be able to write over a non visible 0, but not the other way around. - // Hence the GL_GREATER stencil test against the reference value of 2. - - if (!atom_selection_empty) { - glColorMask(0, 0, 0, 0); - - glEnable(GL_DEPTH_TEST); - glDepthFunc(GL_EQUAL); - - glEnable(GL_STENCIL_TEST); - glStencilMask(0xFF); - - glClearStencil(1); - glClear(GL_STENCIL_BUFFER_BIT); - - glStencilFunc(GL_GREATER, 0x02, 0xFF); - glStencilOp(GL_KEEP, GL_ZERO, GL_REPLACE); - draw_representations_opaque_lean_and_mean(data, AtomBit_Selected | AtomBit_Visible); - - glDisable(GL_DEPTH_TEST); - - glStencilMask(0x0); - glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP); - glColorMask(1, 1, 1, 1); - - glStencilFunc(GL_EQUAL, 2, 0xFF); - postprocessing::blit_color(data->selection.color.selection.visible); - - glStencilFunc(GL_EQUAL, 0, 0xFF); - postprocessing::blit_color(data->selection.color.selection.hidden); - } - - if (!atom_highlight_empty) { - glColorMask(0, 0, 0, 0); - - glEnable(GL_DEPTH_TEST); - glDepthFunc(GL_EQUAL); - - glEnable(GL_STENCIL_TEST); - glStencilMask(0xFF); - - glClearStencil(1); - glClear(GL_STENCIL_BUFFER_BIT); - - glStencilFunc(GL_GREATER, 0x02, 0xFF); - glStencilOp(GL_KEEP, GL_ZERO, GL_REPLACE); - draw_representations_opaque_lean_and_mean(data, AtomBit_Highlighted | AtomBit_Visible); - - glDisable(GL_DEPTH_TEST); - - glStencilMask(0x0); - glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP); - glColorMask(1, 1, 1, 1); - - glStencilFunc(GL_EQUAL, 2, 0xFF); - vec4_t col_vis = data->selection.color.highlight.visible; - col_vis.w += sin(ImGui::GetTime() * HIGHLIGHT_PULSE_TIME_SCALE) * HIGHLIGHT_PULSE_ALPHA_SCALE; - //col_vis = hcla_to_rgba(col_vis); - postprocessing::blit_color(col_vis); - - glStencilFunc(GL_EQUAL, 0, 0xFF); - postprocessing::blit_color(data->selection.color.highlight.hidden); - } - - glDisable(GL_STENCIL_TEST); - - if (!atom_selection_empty) { - PUSH_GPU_SECTION("Desaturate") { - const float saturation = data->selection.color.saturation; - glDrawBuffer(GL_COLOR_ATTACHMENT_COLOR); - postprocessing::scale_hsv(data->gbuffer.tex.color, vec3_t{1, saturation, 1}); - } POP_GPU_SECTION() - } - - glDepthFunc(GL_LESS); - glDepthMask(0); - glColorMask(1,1,1,1); - POP_GPU_SECTION() - } - - glDisable(GL_STENCIL_TEST); - glEnable(GL_DEPTH_TEST); - glDepthFunc(GL_LESS); - glColorMask(1, 1, 1, 1); - glDrawBuffer(GL_COLOR_ATTACHMENT_TRANSPARENCY); - - PUSH_GPU_SECTION("Draw Transparent") - draw_representations_transparent(data); - viamd::event_system_broadcast_event(viamd::EventType_ViamdRenderTransparent, viamd::EventPayloadType_ApplicationState, data); - POP_GPU_SECTION() - - PUSH_GPU_SECTION("Draw Visualization Geometry") - glDisable(GL_DEPTH_TEST); - glDisable(GL_CULL_FACE); - - immediate::set_model_view_matrix(model_view_mat); - immediate::set_proj_matrix(data->view.param.matrix.curr.proj); - - const md_script_vis_t& vis = data->script.vis; - - const uint32_t point_color = convert_color(data->script.point_color); - const uint32_t line_color = convert_color(data->script.line_color); - const uint32_t triangle_color = convert_color(data->script.triangle_color); - - if (vis.points) { - immediate::draw_points_v((immediate::Vertex*)vis.points, md_array_size(vis.points), data->script.point_color); - } - - if (vis.triangles) { - immediate::draw_triangles_v((immediate::Vertex*)vis.triangles, md_array_size(vis.triangles), data->script.triangle_color); - } - - if (vis.lines) { - immediate::draw_lines_v((immediate::Vertex*)vis.lines, md_array_size(vis.lines), data->script.line_color); - } - - md_array(mat4_t) model_matrices = 0; - defer { md_array_free(model_matrices, frame_alloc); }; - - if (vis.sdf.matrices) { - if (md_array_size(vis.sdf.matrices) > 0) { - model_matrices = md_array_create(mat4_t, md_array_size(vis.sdf.matrices), frame_alloc); - for (size_t i = 0; i < md_array_size(vis.sdf.matrices); ++i) { - model_matrices[i] = mat4_inverse(vis.sdf.matrices[i]); - } - } - - const vec4_t col_x = {1, 0, 0, 0.7f}; - const vec4_t col_y = {0, 1, 0, 0.7f}; - const vec4_t col_z = {0, 0, 1, 0.7f}; - const float ext = vis.sdf.extent * 0.25f; - for (size_t i = 0; i < md_array_size(model_matrices); ++i) { - immediate::draw_basis(model_matrices[i], ext, col_x, col_y, col_z); - } - } - - immediate::render(); - - // This operation is split in two as this portion requires DEPTH TEST - if (model_matrices) { - immediate::set_model_view_matrix(model_view_mat); - immediate::set_proj_matrix(data->view.param.matrix.curr.proj); - - glEnable(GL_DEPTH_TEST); - - const vec3_t box_ext = vec3_set1(vis.sdf.extent); - for (size_t i = 0; i < md_array_size(model_matrices); ++i) { - immediate::draw_box_wireframe(-box_ext, box_ext, model_matrices[i], data->density_volume.bounding_box_color); - } - - immediate::render(); - } - - glEnable(GL_CULL_FACE); - POP_GPU_SECTION() - - POP_GPU_SECTION() // G-buffer -} static void handle_picking(ApplicationState* data) { ImGuiContext* ctx = ImGui::GetCurrentContext(); @@ -9408,268 +9161,6 @@ static void handle_picking(ApplicationState* data) { } } -static void apply_postprocessing(const ApplicationState& data) { - PUSH_GPU_SECTION("Postprocessing") - postprocessing::Descriptor desc; - - desc.background.color = data.visuals.background.color * data.visuals.background.intensity; - - desc.ambient_occlusion.enabled = data.visuals.ssao.enabled; - desc.ambient_occlusion.intensity = data.visuals.ssao.intensity; - desc.ambient_occlusion.radius = data.visuals.ssao.radius; - desc.ambient_occlusion.bias = data.visuals.ssao.bias; - - desc.tonemapping.enabled = data.visuals.tonemapping.enabled; - desc.tonemapping.mode = data.visuals.tonemapping.tonemapper; - desc.tonemapping.exposure = data.visuals.tonemapping.exposure; - desc.tonemapping.gamma = data.visuals.tonemapping.gamma; - - desc.depth_of_field.enabled = data.visuals.dof.enabled; - desc.depth_of_field.focus_depth = data.visuals.dof.focus_depth; - desc.depth_of_field.focus_scale = data.visuals.dof.focus_scale; - - desc.fxaa.enabled = data.visuals.fxaa.enabled; - - constexpr float MOTION_BLUR_REFERENCE_DT = 1.0f / 60.0f; - const float dt_compensation = MOTION_BLUR_REFERENCE_DT / (float)data.app.timing.delta_s; - const float motion_scale = data.visuals.temporal_aa.motion_blur.motion_scale * dt_compensation; - desc.temporal_aa.enabled = data.visuals.temporal_aa.enabled; - desc.temporal_aa.feedback_min = data.visuals.temporal_aa.feedback_min; - desc.temporal_aa.feedback_max = data.visuals.temporal_aa.feedback_max; - desc.temporal_aa.motion_blur.enabled = data.visuals.temporal_aa.motion_blur.enabled; - desc.temporal_aa.motion_blur.motion_scale = motion_scale; - - desc.sharpen.enabled = data.visuals.temporal_aa.enabled && data.visuals.sharpen.enabled; - desc.sharpen.weight = data.visuals.sharpen.weight; - - desc.input_textures.depth = data.gbuffer.tex.depth; - desc.input_textures.color = data.gbuffer.tex.color; - desc.input_textures.normal = data.gbuffer.tex.normal; - desc.input_textures.velocity = data.gbuffer.tex.velocity; - desc.input_textures.transparency = data.gbuffer.tex.transparency; - - postprocessing::shade_and_postprocess(desc, data.view.param); - POP_GPU_SECTION() -} - -static void draw_representations_opaque(ApplicationState* data) { - ASSERT(data); - - if (data->mold.sys.atom.count == 0) { - return; - } - -#if EXPERIMENTAL_GFX_API - if (use_gfx) { - const uint32_t instance_count = 10000; - static mat4_t* transforms = 0; - - if (transforms == 0) { - auto rnd = []() -> float { - return (float)rand() / RAND_MAX; - }; - for (uint32_t i = 0; i < instance_count; ++i) { - vec3_t axis = {rnd(), rnd(), rnd()}; - quat_t ori = quat_angle_axis(rnd() * TWO_PI, vec3_normalize(axis)); - mat4_t R = mat4_from_quat(ori); - mat4_t T = mat4_translate(rnd() * 4000, rnd() * 4000, rnd() * 4000); - mat4_t M = T * R; - md_urange_t range = {0, (int32_t)data->mold.sys.atom.count}; - md_array_push(transforms, M, persistent_alloc); - } - } - - md_gfx_draw_op_t* draw_ops = 0; - for (int64_t i = 0; i < md_array_size(data->representation.reps); ++i) { - if (data->representation.reps[i].enabled) { - md_gfx_draw_op_t op; - op.structure = data->mold.gfx_structure; - op.representation = data->representation.reps[i].gfx_rep; - op.model_mat = NULL; - md_array_push(draw_ops, op, frame_alloc); - - for (uint32_t j = 0; j < instance_count; ++j) { - md_gfx_draw_op_t op; - op.structure = data->mold.gfx_structure; - op.representation = data->representation.reps[i].gfx_rep; - op.model_mat = &transforms[j]; - md_array_push(draw_ops, op, frame_alloc); - } - - } - } - - md_gfx_draw((uint32_t)md_array_size(draw_ops), draw_ops, &data->view.param.matrix.curr.proj, &data->view.param.matrix.curr.view, &data->view.param.matrix.inv.proj, &data->view.param.matrix.inv.view); - } else { -#endif - const size_t num_representations = md_array_size(data->representation.reps); - if (num_representations == 0) return; - - md_array(md_gl_draw_op_t) draw_ops = 0; - for (size_t i = 0; i < num_representations; ++i) { - const Representation& rep = data->representation.reps[i]; - if (rep.type > RepresentationType::Cartoon) continue; - if (rep.enabled && rep.type_is_valid) { - md_gl_draw_op_t op = { - .type = (md_gl_rep_type_t)rep.type, - .args = {}, - .rep = data->representation.reps[i].md_rep, - .model_matrix = NULL, - }; - MEMCPY(&op.args, &rep.scale, sizeof(op.args)); - md_array_push(draw_ops, op, frame_alloc); - } - } - - md_gl_draw_args_t args = { - .shaders = data->mold.gl_shaders, - .draw_operations = { - .count = (uint32_t)md_array_size(draw_ops), - .ops = draw_ops, - }, - .view_transform = { - .view_matrix = &data->view.param.matrix.curr.view.elem[0][0], - .proj_matrix = &data->view.param.matrix.curr.proj.elem[0][0], - // These two are for temporal anti-aliasing reprojection (optional) - .prev_view_matrix = &data->view.param.matrix.prev.view.elem[0][0], - .prev_proj_matrix = &data->view.param.matrix.prev.proj.elem[0][0], - }, - }; - - md_gl_draw(&args); -#if EXPERIMENTAL_GFX_API - } -#endif -} - -static void draw_representations_transparent(ApplicationState* state) { - ASSERT(state); - if (state->mold.sys.atom.count == 0) return; - - const size_t num_representations = md_array_size(state->representation.reps); - if (num_representations == 0) return; - - for (size_t i = 0; i < num_representations; ++i) { - const Representation& rep = state->representation.reps[i]; - if (!rep.enabled) continue; - if (rep.type != RepresentationType::ElectronicStructure) continue; - - const IsoDesc* iso = nullptr; - - switch (rep.electronic_structure.type) { - case ElectronicStructureType::MolecularOrbital: - case ElectronicStructureType::NaturalTransitionOrbitalParticle: - case ElectronicStructureType::NaturalTransitionOrbitalHole: - iso = &rep.electronic_structure.iso_psi; - break; - case ElectronicStructureType::MolecularOrbitalDensity: - case ElectronicStructureType::NaturalTransitionOrbitalDensityParticle: - case ElectronicStructureType::NaturalTransitionOrbitalDensityHole: - case ElectronicStructureType::AttachmentDensity: - case ElectronicStructureType::DetachmentDensity: - case ElectronicStructureType::ElectronDensity: - iso = &rep.electronic_structure.iso_den; - break; - default: - ASSERT(false); - } - -#if VIAMD_RECOMPUTE_ORBITAL_PER_FRAME - update_representation(state, &state->representation.reps[i]); -#endif - - volume::RenderDesc desc = { - .render_target = { - .depth = state->gbuffer.tex.depth, - .color = state->gbuffer.tex.transparency, - .width = state->gbuffer.width, - .height = state->gbuffer.height, - }, - .texture = { - .volume = rep.electronic_structure.vol.tex_id, - .transfer_function = rep.electronic_structure.dvr.tf_tex, - }, - .matrix = { - .model = rep.electronic_structure.vol.texture_to_world, - .view = state->view.param.matrix.curr.view, - .proj = state->view.param.matrix.curr.proj, - .inv_proj = state->view.param.matrix.inv.proj, - }, - .clip_volume = { - .min = {0,0,0}, - .max = {1,1,1}, - }, - .temporal = { - .enabled = state->visuals.temporal_aa.enabled, - }, - .iso = { - .enabled = iso->enabled, - .count = iso->count, - .values = iso->values, - .colors = iso->colors, - }, - .dvr = { - .enabled = rep.electronic_structure.dvr.enabled, - .min_tf_value = -1.0f, - .max_tf_value = 1.0f, - }, - .shading = { - .env_radiance = state->visuals.background.color * state->visuals.background.intensity * 0.25f, - .roughness = 0.3f, - .dir_radiance = {10,10,10}, - .ior = 1.5f, - }, - .voxel_spacing = rep.electronic_structure.vol.voxel_size, - }; - - volume::render_volume(desc); - -#if DEBUG - immediate::set_model_view_matrix(state->view.param.matrix.curr.view); - immediate::set_proj_matrix(state->view.param.matrix.curr.proj); - immediate::draw_box_wireframe({0,0,0}, {1,1,1}, rep.electronic_structure.vol.texture_to_world, immediate::COLOR_BLACK); - immediate::render(); -#endif - } -} - -static void draw_representations_opaque_lean_and_mean(ApplicationState* data, uint32_t mask) { - md_gl_draw_op_t* draw_ops = 0; - for (size_t i = 0; i < md_array_size(data->representation.reps); ++i) { - const Representation& rep = data->representation.reps[i]; - - if (rep.type > RepresentationType::Cartoon) continue; - - if (rep.enabled && rep.type_is_valid) { - md_gl_draw_op_t op = { - .type = (md_gl_rep_type_t)rep.type, - .args = {}, - .rep = data->representation.reps[i].md_rep, - .model_matrix = NULL, - }; - MEMCPY(&op.args, &rep.scale, sizeof(op.args)); - md_array_push(draw_ops, op, frame_alloc); - } - } - - md_gl_draw_args_t args = { - .shaders = data->mold.gl_shaders_lean_and_mean, - .draw_operations = { - .count = md_array_size(draw_ops), - .ops = draw_ops, - }, - .view_transform = { - .view_matrix = &data->view.param.matrix.curr.view.elem[0][0], - .proj_matrix = &data->view.param.matrix.curr.proj.elem[0][0], - // These two are for temporal anti-aliasing reprojection - //.prev_model_view_matrix = &data->view.param.matrix.previous.view[0][0], - //.prev_projection_matrix = &data->view.param.matrix.previous.proj[0][0], - }, - .atom_mask = mask, - }; - - md_gl_draw(&args); -}