a4ef4adfc7
Added width/height tracking to the window class via a GLFW resize callback. The callback stores the current size and forwards it to a user-provided std::function. Added context::set_size() to update the GL viewport when the window is resized. Changed scene::render() to accept (int width, int height) parameters so scenes can compute the correct aspect ratio for their projection matrix instead of hardcoding 1280/720. Fixed parameter shadowing in resize_callback_impl (glfw_window instead of window).
53 lines
1.3 KiB
C++
53 lines
1.3 KiB
C++
#include <string_view>
|
|
|
|
#define GLFW_INCLUDE_NONE
|
|
#include "GLFW/glfw3.h"
|
|
|
|
#include "fmt/std.h"
|
|
#include "glad/glad.h"
|
|
|
|
#include "cbt/opengl/context.hpp"
|
|
|
|
namespace cbt::opengl {
|
|
|
|
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(window const& win) {
|
|
glfwMakeContextCurrent(win.raw());
|
|
if (!setup_gl()) {
|
|
return;
|
|
}
|
|
m_valid = true;
|
|
|
|
// Set initial viewport to match window size
|
|
glViewport(0, 0, win.width(), win.height());
|
|
}
|
|
|
|
context::~context() {}
|
|
|
|
auto context::valid() const -> bool {
|
|
return m_valid;
|
|
}
|
|
|
|
auto context::set_size(int width, int height) -> void {
|
|
glViewport(0, 0, width, height);
|
|
}
|
|
|
|
}
|