Skip to content

Commit

Permalink
Update to GLFW 3.4 and Dear ImGui 1.91.0
Browse files Browse the repository at this point in the history
  • Loading branch information
pierre-dejoue committed Aug 25, 2024
1 parent fdfc2db commit 3eaf9bc
Show file tree
Hide file tree
Showing 7 changed files with 152 additions and 32 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/packaging.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:
name: Install Dependencies
run: |
sudo apt-get update
sudo apt-get install -y xorg-dev libgl1-mesa-dev
sudo apt-get install -y libwayland-dev libxkbcommon-dev xorg-dev libgl1-mesa-dev
- name: Configure CMake
run: cmake -B ${{ github.workspace }}/build -DCMAKE_BUILD_TYPE=${{ matrix.build_type }} ${{ env.CMAKE_CONFIG_PROJECT }} ${{ env.CMAKE_CONFIG_EXTRA }}
Expand Down
2 changes: 1 addition & 1 deletion cmake/third_party/glfw.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ include(FetchContent)
FetchContent_Declare(
glfw
GIT_REPOSITORY https://github.com/glfw/glfw.git
GIT_TAG 3.3.8
GIT_TAG 3.4
)

set(GLFW_BUILD_DOCS OFF CACHE BOOL "" FORCE)
Expand Down
2 changes: 1 addition & 1 deletion cmake/third_party/imgui.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ include(FetchContent)
FetchContent_Declare(
imgui
GIT_REPOSITORY https://github.com/ocornut/imgui.git
GIT_TAG v1.89.8
GIT_TAG v1.91.0
)
FetchContent_Populate(imgui)
46 changes: 31 additions & 15 deletions src/gui/imgui/src/imgui_config.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//-----------------------------------------------------------------------------
// COMPILE-TIME OPTIONS FOR DEAR IMGUI
// DEAR IMGUI COMPILE-TIME OPTIONS
// Runtime options (clipboard callbacks, enabling various features, etc.) can generally be set via the ImGuiIO structure.
// You can use ImGui::SetAllocatorFunctions() before calling ImGui::CreateContext() to rewire memory allocation functions.
//-----------------------------------------------------------------------------
Expand All @@ -9,7 +9,7 @@
// You need to make sure that configuration settings are defined consistently _everywhere_ Dear ImGui is used, which include the imgui*.cpp
// files but also _any_ of your code that uses Dear ImGui. This is because some compile-time options have an affect on data structures.
// Defining those options in imconfig.h will ensure every compilation unit gets to see the same data structure layouts.
// Call IMGUI_CHECKVERSION() from your .cpp files to verify that the data structures your files are using are matching the ones imgui.cpp is using.
// Call IMGUI_CHECKVERSION() from your .cpp file to verify that the data structures your files are using are matching the ones imgui.cpp is using.
//-----------------------------------------------------------------------------

#pragma once
Expand All @@ -21,50 +21,58 @@

//---- Define attributes of all API symbols declarations, e.g. for DLL under Windows
// Using Dear ImGui via a shared library is not recommended, because of function call overhead and because we don't guarantee backward nor forward ABI compatibility.
// DLL users: heaps and globals are not shared across DLL boundaries! You will need to call SetCurrentContext() + SetAllocatorFunctions()
// for each static/DLL boundary you are calling from. Read "Context and Memory Allocators" section of imgui.cpp for more details.
//#define IMGUI_API __declspec( dllexport )
//#define IMGUI_API __declspec( dllimport )
// - Windows DLL users: heaps and globals are not shared across DLL boundaries! You will need to call SetCurrentContext() + SetAllocatorFunctions()
// for each static/DLL boundary you are calling from. Read "Context and Memory Allocators" section of imgui.cpp for more details.
//#define IMGUI_API __declspec(dllexport) // MSVC Windows: DLL export
//#define IMGUI_API __declspec(dllimport) // MSVC Windows: DLL import
//#define IMGUI_API __attribute__((visibility("default"))) // GCC/Clang: override visibility when set is hidden

//---- Don't define obsolete functions/enums/behaviors. Consider enabling from time to time after updating to avoid using soon-to-be obsolete function/names.
//---- Don't define obsolete functions/enums/behaviors. Consider enabling from time to time after updating to clean your code of obsolete function/names.
//#define IMGUI_DISABLE_OBSOLETE_FUNCTIONS
//#define IMGUI_DISABLE_OBSOLETE_KEYIO // 1.87: disable legacy io.KeyMap[]+io.KeysDown[] in favor io.AddKeyEvent(). This will be folded into IMGUI_DISABLE_OBSOLETE_FUNCTIONS in a few versions.
//#define IMGUI_DISABLE_OBSOLETE_KEYIO // 1.87+ disable legacy io.KeyMap[]+io.KeysDown[] in favor io.AddKeyEvent(). This is automatically done by IMGUI_DISABLE_OBSOLETE_FUNCTIONS.

//---- Disable all of Dear ImGui or don't implement standard windows/tools.
// It is very strongly recommended to NOT disable the demo windows and debug tool during development. They are extremely useful in day to day work. Please read comments in imgui_demo.cpp.
//#define IMGUI_DISABLE // Disable everything: all headers and source files will be empty.
//#define IMGUI_DISABLE_DEMO_WINDOWS // Disable demo windows: ShowDemoWindow()/ShowStyleEditor() will be empty.
//#define IMGUI_DISABLE_DEBUG_TOOLS // Disable metrics/debugger and other debug tools: ShowMetricsWindow(), ShowDebugLogWindow() and ShowStackToolWindow() will be empty (this was called IMGUI_DISABLE_METRICS_WINDOW before 1.88).
//#define IMGUI_DISABLE_DEBUG_TOOLS // Disable metrics/debugger and other debug tools: ShowMetricsWindow(), ShowDebugLogWindow() and ShowIDStackToolWindow() will be empty.

//---- Don't implement some functions to reduce linkage requirements.
//#define IMGUI_DISABLE_WIN32_DEFAULT_CLIPBOARD_FUNCTIONS // [Win32] Don't implement default clipboard handler. Won't use and link with OpenClipboard/GetClipboardData/CloseClipboard etc. (user32.lib/.a, kernel32.lib/.a)
//#define IMGUI_ENABLE_WIN32_DEFAULT_IME_FUNCTIONS // [Win32] [Default with Visual Studio] Implement default IME handler (require imm32.lib/.a, auto-link for Visual Studio, -limm32 on command-line for MinGW)
//#define IMGUI_DISABLE_WIN32_DEFAULT_IME_FUNCTIONS // [Win32] [Default with non-Visual Studio compilers] Don't implement default IME handler (won't require imm32.lib/.a)
//#define IMGUI_DISABLE_WIN32_FUNCTIONS // [Win32] Won't use and link with any Win32 function (clipboard, ime).
//#define IMGUI_DISABLE_WIN32_FUNCTIONS // [Win32] Won't use and link with any Win32 function (clipboard, IME).
//#define IMGUI_ENABLE_OSX_DEFAULT_CLIPBOARD_FUNCTIONS // [OSX] Implement default OSX clipboard handler (need to link with '-framework ApplicationServices', this is why this is not the default).
//#define IMGUI_DISABLE_DEFAULT_SHELL_FUNCTIONS // Don't implement default io.PlatformOpenInShellFn() handler (Win32: ShellExecute(), require shell32.lib/.a, Mac/Linux: use system("")).
//#define IMGUI_DISABLE_DEFAULT_FORMAT_FUNCTIONS // Don't implement ImFormatString/ImFormatStringV so you can implement them yourself (e.g. if you don't want to link with vsnprintf)
//#define IMGUI_DISABLE_DEFAULT_MATH_FUNCTIONS // Don't implement ImFabs/ImSqrt/ImPow/ImFmod/ImCos/ImSin/ImAcos/ImAtan2 so you can implement them yourself.
//#define IMGUI_DISABLE_FILE_FUNCTIONS // Don't implement ImFileOpen/ImFileClose/ImFileRead/ImFileWrite and ImFileHandle at all (replace them with dummies)
//#define IMGUI_DISABLE_DEFAULT_FILE_FUNCTIONS // Don't implement ImFileOpen/ImFileClose/ImFileRead/ImFileWrite and ImFileHandle so you can implement them yourself if you don't want to link with fopen/fclose/fread/fwrite. This will also disable the LogToTTY() function.
//#define IMGUI_DISABLE_DEFAULT_ALLOCATORS // Don't implement default allocators calling malloc()/free() to avoid linking with them. You will need to call ImGui::SetAllocatorFunctions().
//#define IMGUI_DISABLE_SSE // Disable use of SSE intrinsics even if available

//---- Enable Test Engine / Automation features.
//#define IMGUI_ENABLE_TEST_ENGINE // Enable imgui_test_engine hooks. Generally set automatically by include "imgui_te_config.h", see Test Engine for details.

//---- Include imgui_user.h at the end of imgui.h as a convenience
// May be convenient for some users to only explicitly include vanilla imgui.h and have extra stuff included.
//#define IMGUI_INCLUDE_IMGUI_USER_H
//#define IMGUI_USER_H_FILENAME "my_folder/my_imgui_user.h"

//---- Pack colors to BGRA8 instead of RGBA8 (to avoid converting from one to another)
//#define IMGUI_USE_BGRA_PACKED_COLOR

//---- Use 32-bit for ImWchar (default is 16-bit) to support unicode planes 1-16. (e.g. point beyond 0xFFFF like emoticons, dingbats, symbols, shapes, ancient languages, etc...)
//---- Use 32-bit for ImWchar (default is 16-bit) to support Unicode planes 1-16. (e.g. point beyond 0xFFFF like emoticons, dingbats, symbols, shapes, ancient languages, etc...)
//#define IMGUI_USE_WCHAR32

//---- Avoid multiple STB libraries implementations, or redefine path/filenames to prioritize another version
// By default the embedded implementations are declared static and not available outside of Dear ImGui sources files.
//#define IMGUI_STB_TRUETYPE_FILENAME "my_folder/stb_truetype.h"
//#define IMGUI_STB_RECT_PACK_FILENAME "my_folder/stb_rect_pack.h"
//#define IMGUI_STB_SPRINTF_FILENAME "my_folder/stb_sprintf.h" // only used if enabled
//#define IMGUI_STB_SPRINTF_FILENAME "my_folder/stb_sprintf.h" // only used if IMGUI_USE_STB_SPRINTF is defined.
//#define IMGUI_DISABLE_STB_TRUETYPE_IMPLEMENTATION
//#define IMGUI_DISABLE_STB_RECT_PACK_IMPLEMENTATION
//#define IMGUI_DISABLE_STB_SPRINTF_IMPLEMENTATION // only disabled if IMGUI_USE_STB_SPRINTF is defined.

//---- Use stb_sprintf.h for a faster implementation of vsnprintf instead of the one from libc (unless IMGUI_DISABLE_DEFAULT_FORMAT_FUNCTIONS is defined)
// Compatibility checks of arguments and formats done by clang and GCC will be disabled in order to support the extra formats provided by stb_sprintf.h.
Expand All @@ -75,6 +83,12 @@
// On Windows you may use vcpkg with 'vcpkg install freetype --triplet=x64-windows' + 'vcpkg integrate install'.
//#define IMGUI_ENABLE_FREETYPE

//---- Use FreeType+lunasvg library to render OpenType SVG fonts (SVGinOT)
// Requires lunasvg headers to be available in the include path + program to be linked with the lunasvg library (not provided).
// Only works in combination with IMGUI_ENABLE_FREETYPE.
// (implementation is based on Freetype's rsvg-port.c which is licensed under CeCILL-C Free Software License Agreement)
//#define IMGUI_ENABLE_FREETYPE_LUNASVG

//---- Use stb_truetype to build and rasterize the font atlas (default)
// The only purpose of this define is if you want force compilation of the stb_truetype backend ALONG with the FreeType backend.
//#define IMGUI_ENABLE_STB_TRUETYPE
Expand All @@ -90,6 +104,8 @@
constexpr ImVec4(const MyVec4& f) : x(f.x), y(f.y), z(f.z), w(f.w) {} \
operator MyVec4() const { return MyVec4(x,y,z,w); }
*/
//---- ...Or use Dear ImGui's own very basic math operators.
//#define IMGUI_DEFINE_MATH_OPERATORS

//---- Use 32-bit vertex indices (default is 16-bit) is one way to allow large meshes with more than 64K vertices.
// Your renderer backend will need to support it (most example renderer backends support both 16/32-bit indices).
Expand All @@ -103,18 +119,18 @@
//typedef void (*MyImDrawCallback)(const ImDrawList* draw_list, const ImDrawCmd* cmd, void* my_renderer_user_data);
//#define ImDrawCallback MyImDrawCallback

//---- Debug Tools: Macro to break in Debugger
//---- Debug Tools: Macro to break in Debugger (we provide a default implementation of this in the codebase)
// (use 'Metrics->Tools->Item Picker' to pick widgets with the mouse and break into them for easy debugging.)
//#define IM_DEBUG_BREAK IM_ASSERT(0)
//#define IM_DEBUG_BREAK __debugbreak()

//---- Debug Tools: Enable slower asserts
//#define IMGUI_DEBUG_PARANOID

//---- Tip: You can add extra functions within the ImGui:: namespace, here or in your own headers files.
//---- Tip: You can add extra functions within the ImGui:: namespace from anywhere (e.g. your own sources/header files)
/*
namespace ImGui
{
void MyFunction(const char* name, const MyMatrix44& v);
void MyFunction(const char* name, MyMatrix44* mtx);
}
*/
72 changes: 70 additions & 2 deletions src/gui/src/glfw_context.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
// Copyright (c) 2023 Pierre DEJOUE
// This code is distributed under the terms of the MIT License
#include "glfw_context.h"


#include <GLFW/glfw3.h>

#include <cassert>

namespace {

Expand All @@ -24,7 +26,7 @@ void glfw_error_callback(int error, const char* description)

} // namespace

GLFWWindowContext::GLFWWindowContext(int width, int height, const std::string_view& title, const stdutils::io::ErrorHandler* err_handler)
GLFWWindowContext::GLFWWindowContext(int width, int height, const GLFWOptions& options, const stdutils::io::ErrorHandler* err_handler)
: m_window_ptr(nullptr)
, m_glfw_init(false)
{
Expand All @@ -33,11 +35,16 @@ GLFWWindowContext::GLFWWindowContext(int width, int height, const std::string_vi
return;
call_once = true;

// Window title
if (options.title.empty() && err_handler) { (*err_handler)(stdutils::io::Severity::WARN, "Window title is not specified"); }
const std::string_view title = options.title.empty() ? "untitled" : options.title;

if (err_handler)
{
s_glfw_err_handler = *err_handler;
glfwSetErrorCallback(glfw_error_callback);
}

if (!glfwInit())
{
if (err_handler) { (*err_handler)(stdutils::io::Severity::FATAL, "GLFW failed to initialize"); }
Expand All @@ -51,6 +58,9 @@ GLFWWindowContext::GLFWWindowContext(int width, int height, const std::string_vi
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
if constexpr (TARGET_OPENGL_FORWARD_COMPATIBILITY)
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
if (options.maximize_window)
glfwWindowHint(GLFW_MAXIMIZED, GL_TRUE);
assert(title.data());
m_window_ptr = glfwCreateWindow(width, height, title.data(), nullptr, nullptr);
if (m_window_ptr == nullptr)
{
Expand All @@ -70,6 +80,64 @@ GLFWWindowContext::~GLFWWindowContext()
glfwTerminate();
}

GLFWWindowContext::WindowStatus GLFWWindowContext::window_status() const
{
WindowStatus result;
assert(m_window_ptr);
result.is_minimized = glfwGetWindowAttrib(m_window_ptr, GLFW_ICONIFIED);
result.is_maximized = glfwGetWindowAttrib(m_window_ptr, GLFW_MAXIMIZED);
return result;
}

std::pair<int, int> GLFWWindowContext::framebuffer_size() const
{
std::pair<int, int> sz(0, 0);
assert(m_window_ptr);
glfwGetFramebufferSize(m_window_ptr, &sz.first, &sz.second);
return sz;
}

std::pair<int, int> GLFWWindowContext::window_size() const
{
std::pair<int, int> sz(0, 0);
assert(m_window_ptr);
glfwGetWindowSize(m_window_ptr, &sz.first, &sz.second);
return sz;
}

float GLFWWindowContext::get_framebuffer_scale() const
{
float scale{1.f};
assert(m_window_ptr);

std::pair<int, int> fb_sz(0, 0);
glfwGetFramebufferSize(m_window_ptr, &fb_sz.first, &fb_sz.second);
std::pair<int, int> window_sz(0, 0);
glfwGetWindowSize(m_window_ptr, &window_sz.first, &window_sz.second);

if (window_sz.first == 0 || window_sz.second == 0)
return scale;

const std::pair<float, float> fb_scale(
static_cast<float>(fb_sz.first) / static_cast<float>(window_sz.first),
static_cast<float>(fb_sz.second) / static_cast<float>(window_sz.second)
);

// If the scales are different on the X and Y axis, log an error and arbitrarily use scale.x
if (fb_scale.first != fb_scale.second && s_glfw_err_handler)
{
std::stringstream out;
out << "Different coordinate scales on the X and Y axis";
out << "; Framebuffer: " << fb_sz.first << "x" << fb_sz.second;
out << "; Window: " << window_sz.first << "x" << window_sz.second;
s_glfw_err_handler(stdutils::io::Severity::ERR, out.str());
}
scale = fb_scale.first;

assert(scale > 0.f);
return scale;
}

const char* glsl_version()
{
return TARGET_GLSL_VERSION_STR;
Expand Down
24 changes: 23 additions & 1 deletion src/gui/src/glfw_context.h
Original file line number Diff line number Diff line change
@@ -1,16 +1,38 @@
// Copyright (c) 2023 Pierre DEJOUE
// This code is distributed under the terms of the MIT License
#pragma once

#include <stdutils/io.h>

#include <string_view>
#include <utility>

struct GLFWOptions
{
std::string_view title{};
bool maximize_window{false};
};

// Wrapper class for GLFW initialization and window
struct GLFWwindow;
class GLFWWindowContext
{
public:
GLFWWindowContext(int width, int height, const std::string_view& title, const stdutils::io::ErrorHandler* err_handler = nullptr);
struct WindowStatus
{
bool is_minimized{false};
bool is_maximized{false};
};

GLFWWindowContext(int width, int height, const GLFWOptions& options, const stdutils::io::ErrorHandler* err_handler = nullptr);
~GLFWWindowContext();
GLFWwindow* window() { return m_window_ptr; }
WindowStatus window_status() const;
std::pair<int, int> framebuffer_size() const;
std::pair<int, int> window_size() const;

// Ratio between the framebuffer coordinates and the screen coordinates. Supposedly the same on the X and Y axis
float get_framebuffer_scale() const;

private:
GLFWwindow* m_window_ptr;
Expand Down
36 changes: 25 additions & 11 deletions src/gui/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,21 @@ void err_callback(stdutils::io::SeverityCode sev, std::string_view msg)
std::cerr << stdutils::io::str_severity_code(sev) << ": " << msg << std::endl;
}

std::string project_title()
namespace details {

std::string s_project_title()
{
std::stringstream title;
title << "Picross Solver " << picross::get_version_string();
return title.str();
}

} // namespace details

std::string_view project_title()
{
std::stringstream title;
title << "Picross Solver " << picross::get_version_string();
return title.str();
static std::string project_title = details::s_project_title();
return project_title;
}

// Application windows
Expand Down Expand Up @@ -126,12 +136,18 @@ void main_menu_bar(AppWindows& windows, bool& application_should_close, bool& gu
int main(int argc, char *argv[])
{
UNUSED(argc); UNUSED(argv);
stdutils::io::ErrorHandler err_handler(err_callback);

// Setup main window
constexpr int WINDOW_WIDTH = 1280;
constexpr int WINDOW_HEIGHT = 720;
stdutils::io::ErrorHandler err_handler(err_callback);
GLFWWindowContext glfw_context(WINDOW_WIDTH, WINDOW_HEIGHT, project_title().data(), &err_handler);
GLFWWindowContext glfw_context = [&err_handler]() {
constexpr int WINDOW_WIDTH = 1280;
constexpr int WINDOW_HEIGHT = 720;
GLFWOptions options;
options.title = project_title();
options.maximize_window = true;
GLFWWindowContext context(WINDOW_WIDTH, WINDOW_HEIGHT, options, &err_handler);
return context;
}();
if (glfw_context.window() == nullptr)
return EXIT_FAILURE;

Expand Down Expand Up @@ -202,8 +218,7 @@ int main(int argc, char *argv[])
if (!windows.settings) { windows.settings = std::make_unique<SettingsWindow>(settings); }

// Rendering
int display_w, display_h;
glfwGetFramebufferSize(glfw_context.window(), &display_w, &display_h);
const auto [display_w, display_h] = glfw_context.framebuffer_size();
glViewport(0, 0, display_w, display_h);
const auto clear_color = get_background_color(gui_dark_mode);
glClearColor(clear_color[0], clear_color[1], clear_color[2], clear_color[3]);
Expand All @@ -214,4 +229,3 @@ int main(int argc, char *argv[])

return EXIT_SUCCESS;
}

0 comments on commit 3eaf9bc

Please sign in to comment.