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:
@@ -34,7 +34,7 @@ To add a new dependency:
|
|||||||
|
|
||||||
The project is split into static libraries:
|
The project is split into static libraries:
|
||||||
|
|
||||||
- **`cbt_opengl`** — OpenGL abstraction (context, buffer, texture, vao, shader, descriptor)
|
- **`cbt_opengl`** — OpenGL abstraction and window management (window, context, buffer, texture, vao, shader, descriptor)
|
||||||
- **`cbt_scene`** — Base scene class
|
- **`cbt_scene`** — Base scene class
|
||||||
- **`scenes_cube`** — Cube scene implementation
|
- **`scenes_cube`** — Cube scene implementation
|
||||||
|
|
||||||
@@ -102,8 +102,10 @@ cuber/
|
|||||||
cbt/ # Project namespace (utilities)
|
cbt/ # Project namespace (utilities)
|
||||||
scene.hpp # Base scene class
|
scene.hpp # Base scene class
|
||||||
scene.cpp # Scene implementation
|
scene.cpp # Scene implementation
|
||||||
|
window.hpp # GLFW window RAII wrapper
|
||||||
|
window.cpp # Window implementation
|
||||||
opengl/ # OpenGL abstraction layer
|
opengl/ # OpenGL abstraction layer
|
||||||
context.hpp # OpenGL context RAII wrapper
|
context.hpp # OpenGL context RAII wrapper (GLAD setup)
|
||||||
context.cpp # Context implementation
|
context.cpp # Context implementation
|
||||||
buffer.hpp # Buffer resource (VBO, EBO, UBO, SSBO)
|
buffer.hpp # Buffer resource (VBO, EBO, UBO, SSBO)
|
||||||
buffer.cpp # Buffer implementation
|
buffer.cpp # Buffer implementation
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ find_package(glm REQUIRED)
|
|||||||
|
|
||||||
# OpenGL abstraction library
|
# OpenGL abstraction library
|
||||||
add_library(cbt_opengl STATIC
|
add_library(cbt_opengl STATIC
|
||||||
|
"cbt/window.cpp"
|
||||||
"cbt/opengl/context.cpp"
|
"cbt/opengl/context.cpp"
|
||||||
"cbt/opengl/buffer.cpp"
|
"cbt/opengl/buffer.cpp"
|
||||||
"cbt/opengl/texture.cpp"
|
"cbt/opengl/texture.cpp"
|
||||||
|
|||||||
+5
-73
@@ -1,18 +1,8 @@
|
|||||||
#include <string_view>
|
#include <string_view>
|
||||||
|
|
||||||
#ifdef _WIN32
|
|
||||||
#include <dwmapi.h>
|
|
||||||
#include <windows.h>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#define GLFW_INCLUDE_NONE
|
#define GLFW_INCLUDE_NONE
|
||||||
#include "GLFW/glfw3.h"
|
#include "GLFW/glfw3.h"
|
||||||
|
|
||||||
#ifdef _WIN32
|
|
||||||
#define GLFW_EXPOSE_NATIVE_WIN32
|
|
||||||
#include "GLFW/glfw3native.h"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#include "fmt/std.h"
|
#include "fmt/std.h"
|
||||||
#include "glad/glad.h"
|
#include "glad/glad.h"
|
||||||
|
|
||||||
@@ -20,18 +10,6 @@
|
|||||||
|
|
||||||
namespace cbt::opengl {
|
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 {
|
auto context::setup_gl() -> bool {
|
||||||
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
|
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
|
||||||
fmt::print("Failed to initialize GLAD\n");
|
fmt::print("Failed to initialize GLAD\n");
|
||||||
@@ -50,64 +28,18 @@ auto context::print_info() -> void {
|
|||||||
fmt::print("\n");
|
fmt::print("\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
context::context(std::string title, int width, int height) {
|
context::context(window const& win) {
|
||||||
if (!init()) {
|
glfwMakeContextCurrent(win.raw());
|
||||||
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()) {
|
if (!setup_gl()) {
|
||||||
terminate();
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
m_valid = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
context::~context() {
|
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 {
|
auto context::valid() const -> bool {
|
||||||
return m_window != nullptr;
|
return m_valid;
|
||||||
}
|
|
||||||
|
|
||||||
auto context::swap_buffers() -> void {
|
|
||||||
glfwSwapBuffers(m_window);
|
|
||||||
}
|
|
||||||
|
|
||||||
auto context::poll_events() -> void {
|
|
||||||
glfwPollEvents();
|
|
||||||
}
|
|
||||||
|
|
||||||
auto context::raw() const -> GLFWwindow* {
|
|
||||||
return m_window;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
+3
-13
@@ -1,29 +1,19 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <string>
|
#include "cbt/window.hpp"
|
||||||
|
|
||||||
#define GLFW_INCLUDE_NONE
|
|
||||||
#include "GLFW/glfw3.h"
|
|
||||||
|
|
||||||
namespace cbt::opengl {
|
namespace cbt::opengl {
|
||||||
|
|
||||||
class context {
|
class context {
|
||||||
public:
|
public:
|
||||||
explicit context(std::string title, int width, int height);
|
explicit context(window const& win);
|
||||||
~context();
|
~context();
|
||||||
|
|
||||||
auto should_close() const -> bool;
|
|
||||||
auto valid() const -> bool;
|
auto valid() const -> bool;
|
||||||
auto swap_buffers() -> void;
|
|
||||||
auto poll_events() -> void;
|
|
||||||
auto raw() const -> GLFWwindow*;
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
GLFWwindow* m_window = nullptr;
|
bool m_valid = false;
|
||||||
bool m_initialized = false;
|
|
||||||
|
|
||||||
static auto init() -> bool;
|
|
||||||
static auto terminate() -> void;
|
|
||||||
static auto setup_gl() -> bool;
|
static auto setup_gl() -> bool;
|
||||||
static auto print_info() -> void;
|
static auto print_info() -> void;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -0,0 +1,86 @@
|
|||||||
|
#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 "cbt/window.hpp"
|
||||||
|
|
||||||
|
namespace cbt {
|
||||||
|
|
||||||
|
auto window::init_glfw() -> bool {
|
||||||
|
if (!glfwInit()) {
|
||||||
|
fmt::print("Failed to initialize GLFW\n");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto window::terminate_glfw() -> void {
|
||||||
|
glfwTerminate();
|
||||||
|
}
|
||||||
|
|
||||||
|
window::window(std::string title, int width, int height) {
|
||||||
|
if (!init_glfw()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
m_glfw_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_glfw();
|
||||||
|
m_glfw_initialized = false;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef _WIN32
|
||||||
|
auto hwnd = glfwGetWin32Window(m_window);
|
||||||
|
BOOL use_dark_mode = TRUE;
|
||||||
|
DwmSetWindowAttribute(hwnd, DWMWA_USE_IMMERSIVE_DARK_MODE, &use_dark_mode, sizeof(use_dark_mode));
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
window::~window() {
|
||||||
|
if (m_window) {
|
||||||
|
glfwDestroyWindow(m_window);
|
||||||
|
}
|
||||||
|
if (m_glfw_initialized) {
|
||||||
|
terminate_glfw();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
auto window::should_close() const -> bool {
|
||||||
|
return m_window && glfwWindowShouldClose(m_window);
|
||||||
|
}
|
||||||
|
|
||||||
|
auto window::valid() const -> bool {
|
||||||
|
return m_window != nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto window::swap_buffers() -> void {
|
||||||
|
glfwSwapBuffers(m_window);
|
||||||
|
}
|
||||||
|
|
||||||
|
auto window::poll_events() -> void {
|
||||||
|
glfwPollEvents();
|
||||||
|
}
|
||||||
|
|
||||||
|
auto window::raw() const -> GLFWwindow* {
|
||||||
|
return m_window;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
#define GLFW_INCLUDE_NONE
|
||||||
|
#include "GLFW/glfw3.h"
|
||||||
|
|
||||||
|
namespace cbt {
|
||||||
|
|
||||||
|
class window {
|
||||||
|
public:
|
||||||
|
explicit window(std::string title, int width, int height);
|
||||||
|
~window();
|
||||||
|
|
||||||
|
auto should_close() const -> bool;
|
||||||
|
auto valid() const -> bool;
|
||||||
|
auto swap_buffers() -> void;
|
||||||
|
auto poll_events() -> void;
|
||||||
|
auto raw() const -> GLFWwindow*;
|
||||||
|
|
||||||
|
private:
|
||||||
|
GLFWwindow* m_window = nullptr;
|
||||||
|
bool m_glfw_initialized = false;
|
||||||
|
|
||||||
|
static auto init_glfw() -> bool;
|
||||||
|
static auto terminate_glfw() -> void;
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
@@ -5,11 +5,18 @@
|
|||||||
#include "GLFW/glfw3.h"
|
#include "GLFW/glfw3.h"
|
||||||
#include "asio.hpp"
|
#include "asio.hpp"
|
||||||
|
|
||||||
|
#include "cbt/window.hpp"
|
||||||
#include "cbt/opengl/context.hpp"
|
#include "cbt/opengl/context.hpp"
|
||||||
#include "scenes/cube.hpp"
|
#include "scenes/cube.hpp"
|
||||||
|
|
||||||
auto main(int, char const*[]) -> int {
|
auto main(int, char const*[]) -> int {
|
||||||
auto ctx = cbt::opengl::context("cuber", 1280, 720);
|
auto win = cbt::window("cuber", 1280, 720);
|
||||||
|
|
||||||
|
if (!win.valid()) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto ctx = cbt::opengl::context(win);
|
||||||
|
|
||||||
if (!ctx.valid()) {
|
if (!ctx.valid()) {
|
||||||
return 1;
|
return 1;
|
||||||
@@ -37,10 +44,10 @@ auto main(int, char const*[]) -> int {
|
|||||||
// render loop
|
// render loop
|
||||||
auto prev = std::chrono::steady_clock::now();
|
auto prev = std::chrono::steady_clock::now();
|
||||||
|
|
||||||
while (!ctx.should_close()) {
|
while (!win.should_close()) {
|
||||||
process_signals();
|
process_signals();
|
||||||
|
|
||||||
if (quit || glfwGetKey(ctx.raw(), GLFW_KEY_Q) == GLFW_PRESS) {
|
if (quit || glfwGetKey(win.raw(), GLFW_KEY_Q) == GLFW_PRESS) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -51,8 +58,8 @@ auto main(int, char const*[]) -> int {
|
|||||||
scn.update(dt);
|
scn.update(dt);
|
||||||
scn.render();
|
scn.render();
|
||||||
|
|
||||||
ctx.swap_buffers();
|
win.swap_buffers();
|
||||||
ctx.poll_events();
|
win.poll_events();
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
Reference in New Issue
Block a user