Files
cuber/cbt/opengl/context.cpp
T
portersky 7a81b30d32 feat: enable Windows dark mode for GLFW window
- Add DwmSetWindowAttribute call for immersive dark mode
- Include glfw3native.h after glfw3.h on Windows
2026-05-05 22:52:24 +02:00

114 lines
2.6 KiB
C++

#include <string_view>
#ifdef _WIN32
#include <dwmapi.h>
#include <windows.h>
#endif
#define GLFW_INCLUDE_NONE
#include "GLFW/glfw3.h"
#ifdef _WIN32
#define GLFW_EXPOSE_NATIVE_WIN32
#include "GLFW/glfw3native.h"
#endif
#include "fmt/std.h"
#include "glad/glad.h"
#include "cbt/opengl/context.hpp"
namespace cbt::opengl {
auto context::init() -> bool {
if (!glfwInit()) {
fmt::print("Failed to initialize GLFW\n");
return false;
}
return true;
}
auto context::terminate() -> void {
glfwTerminate();
}
auto context::setup_gl() -> bool {
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
fmt::print("Failed to initialize GLAD\n");
return false;
}
print_info();
return true;
}
auto context::print_info() -> void {
fmt::print("OpenGL Info:\n");
fmt::print(" vendor | {}\n", std::string_view(reinterpret_cast<char const*>(glGetString(GL_VENDOR))));
fmt::print(" renderer| {}\n", std::string_view(reinterpret_cast<char const*>(glGetString(GL_RENDERER))));
fmt::print(" version | {}\n", std::string_view(reinterpret_cast<char const*>(glGetString(GL_VERSION))));
fmt::print(" glsl | {}\n", std::string_view(reinterpret_cast<char const*>(glGetString(GL_SHADING_LANGUAGE_VERSION))));
fmt::print("\n");
}
context::context(std::string title, int width, int height) {
if (!init()) {
return;
}
m_initialized = true;
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
m_window = glfwCreateWindow(width, height, title.c_str(), nullptr, nullptr);
if (!m_window) {
fmt::print("Failed to create window\n");
terminate();
return;
}
glfwMakeContextCurrent(m_window);
#ifdef _WIN32
auto hwnd = glfwGetWin32Window(static_cast<GLFWwindow*>(m_window));
BOOL use_dark_mode = TRUE;
DwmSetWindowAttribute(hwnd, DWMWA_USE_IMMERSIVE_DARK_MODE, &use_dark_mode, sizeof(use_dark_mode));
#endif
if (!setup_gl()) {
terminate();
return;
}
}
context::~context() {
if (m_window) {
glfwDestroyWindow(m_window);
}
if (m_initialized) {
terminate();
}
}
auto context::should_close() const -> bool {
return m_window && glfwWindowShouldClose(m_window);
}
auto context::valid() const -> bool {
return m_window != nullptr;
}
auto context::swap_buffers() -> void {
glfwSwapBuffers(m_window);
}
auto context::poll_events() -> void {
glfwPollEvents();
}
auto context::raw() const -> GLFWwindow* {
return m_window;
}
}