/
crhlkj
/
learning-c
Обзор
Документация
Войти
/
crhlkj
/
learning-c
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
main.cpp
104 строки
3 KB
Никита Осипов
create main.cpp
19 дек 2025, 20:15
19 дек 2025, 20:15
84f1c41
Код
Авторство
О чём код?
#include <GL/glew.h> #include <GLFW/glfw3.h> #include <expected> #include <format> #include <print> #include <span> #include <ranges> class Window { private: GLFWwindow* handle_{nullptr}; int width_{800}, height_{600}; bool vsync_{true}; static void framebuffer_size_callback(GLFWwindow* window, int w, int h) { glViewport(0, 0, w, h); } public: struct Error { std::string message; }; using Result = std::expected<Window, Error>; static Result create(int w = 800, int h = 600, const char* title = "OpenGL Window") { if (!glfwInit()) { return std::unexpected{Error{"GLFW init failed"}}; } glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 6); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); glfwWindowHint(GLFW_DOUBLEBUFFER, GLFW_TRUE); glfwWindowHint(GLFW_SAMPLES, 4); // MSAA для сглаживания [web:3] GLFWwindow* win = glfwCreateWindow(w, h, title, nullptr, nullptr); if (!win) { glfwTerminate(); return std::unexpected{Error{"Window creation failed"}}; } glfwMakeContextCurrent(win); glfwSwapInterval(vsync_ ? 1 : 0); // VSync опционально glfwSetFramebufferSizeCallback(win, framebuffer_size_callback); if (glewInit() != GLEW_OK) { glfwDestroyWindow(win); glfwTerminate(); return std::unexpected{Error{"GLEW init failed"}}; } glEnable(GL_DEPTH_TEST); glEnable(GL_MULTISAMPLE); // MSAA [web:3] glViewport(0, 0, w, h); return Window{win, w, h}; } Window(GLFWwindow* h, int w, int h) : handle_(h), width_(w), height_(h) {} ~Window() { if (handle_) { glfwDestroyWindow(handle_); glfwTerminate(); } } Window(const Window&) = delete; Window& operator=(const Window&) = delete; Window(Window&& other) noexcept : handle_(other.handle_), width_(other.width_), height_(other.height_) { other.handle_ = nullptr; } Window& operator=(Window&& other) noexcept { if (this != &other) { if (handle_) glfwDestroyWindow(handle_); handle_ = other.handle_; width_ = other.width_; height_ = other.height_; other.handle_ = nullptr; } return *this; } bool should_close() const { return glfwWindowShouldClose(handle_); } void swap_buffers() { glfwSwapBuffers(handle_); } void poll_events() { glfwPollEvents(); } bool get_key(int key) const { return glfwGetKey(handle_, key) == GLFW_PRESS; } std::pair<double, double> get_cursor_pos() const { double x, y; glfwGetCursorPos(handle_, &x, &y); return {x, y}; } int get_width() const { return width_; } int get_height() const { return height_; } void set_vsync(bool enable) { vsync_ = enable; glfwSwapInterval(enable ? 1 : 0); } };