refactor: separate window from opengl::context

Extract GLFW window management into a dedicated cbt::window
class (new files in cbt/). The opengl::context now only
handles GLAD setup and context activation (no more window
creation or GLFW init/terminate).

Updated main loop in cuber.cpp, CMakeLists.txt (to build
the new source), and AGENTS.md (docs + source layout).

Addresses the design note in context.cpp about mixing
concerns.
This commit is contained in:
2026-05-05 23:37:19 +02:00
parent 7a81b30d32
commit 22d2bb1c40
7 changed files with 140 additions and 93 deletions
+5 -73
View File
@@ -1,18 +1,8 @@
#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"
@@ -20,18 +10,6 @@
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");
@@ -50,64 +28,18 @@ auto context::print_info() -> void {
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
context::context(window const& win) {
glfwMakeContextCurrent(win.raw());
if (!setup_gl()) {
terminate();
return;
}
m_valid = true;
}
context::~context() {
if (m_window) {
glfwDestroyWindow(m_window);
}
if (m_initialized) {
terminate();
}
}
auto context::should_close() const -> bool {
return m_window && glfwWindowShouldClose(m_window);
}
context::~context() {}
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;
return m_valid;
}
}