feat: add RAII window class with OpenGL 4.1 context
- Add cbt::window class in cbt/ directory with RAII lifecycle - Add setup_opengl and info_opengl for glad init and GL info - Add Q key to quit the application - Update CMakeLists.txt with glfw3 and glad dependencies - Update AGENTS.md with snake_case naming and shell conventions
This commit is contained in:
@@ -0,0 +1,92 @@
|
||||
#define GLFW_INCLUDE_NONE
|
||||
#include "GLFW/glfw3.h"
|
||||
|
||||
#include "window.hpp"
|
||||
|
||||
#include <string_view>
|
||||
|
||||
#include "glad/glad.h"
|
||||
#include "fmt/std.h"
|
||||
|
||||
namespace cbt {
|
||||
|
||||
auto window::init() -> bool {
|
||||
if (!glfwInit()) {
|
||||
fmt::print("Failed to initialize GLFW\n");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
auto window::terminate() -> void {
|
||||
glfwTerminate();
|
||||
}
|
||||
|
||||
auto window::setup_opengl() -> bool {
|
||||
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
|
||||
fmt::print("Failed to initialize GLAD\n");
|
||||
return false;
|
||||
}
|
||||
info_opengl();
|
||||
return true;
|
||||
}
|
||||
|
||||
auto window::info_opengl() -> void {
|
||||
fmt::print("OpenGL Info:\n");
|
||||
fmt::print(" vendor | {}\n", std::string_view(reinterpret_cast<const char*>(glGetString(GL_VENDOR))));
|
||||
fmt::print(" renderer| {}\n", std::string_view(reinterpret_cast<const char*>(glGetString(GL_RENDERER))));
|
||||
fmt::print(" version | {}\n", std::string_view(reinterpret_cast<const char*>(glGetString(GL_VERSION))));
|
||||
fmt::print(" glsl | {}\n", std::string_view(reinterpret_cast<const char*>(glGetString(GL_SHADING_LANGUAGE_VERSION))));
|
||||
}
|
||||
|
||||
window::window(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(static_cast<GLFWwindow*>(m_window));
|
||||
|
||||
if (!setup_opengl()) {
|
||||
terminate();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
window::~window() {
|
||||
if (m_window) {
|
||||
glfwDestroyWindow(static_cast<GLFWwindow*>(m_window));
|
||||
}
|
||||
if (m_initialized) {
|
||||
terminate();
|
||||
}
|
||||
}
|
||||
|
||||
auto window::should_close() const -> bool {
|
||||
return m_window && glfwWindowShouldClose(static_cast<GLFWwindow*>(m_window));
|
||||
}
|
||||
|
||||
auto window::valid() const -> bool {
|
||||
return m_window != nullptr;
|
||||
}
|
||||
|
||||
auto window::swap_buffers() -> void {
|
||||
glfwSwapBuffers(static_cast<GLFWwindow*>(m_window));
|
||||
}
|
||||
|
||||
auto window::poll_events() -> void {
|
||||
glfwPollEvents();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace cbt {
|
||||
|
||||
class window {
|
||||
void* m_window = nullptr;
|
||||
bool m_initialized = false;
|
||||
|
||||
static auto init() -> bool;
|
||||
static auto terminate() -> void;
|
||||
static auto setup_opengl() -> bool;
|
||||
static auto info_opengl() -> void;
|
||||
|
||||
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 -> void* { return m_window; }
|
||||
};
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user