vgl 1.0.0
Virtual Globe Library: API reference
Loading...
Searching...
No Matches
gl.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Copyright (c) 2026 Jeremiah van Oosten
3//
4// C++ wrapper for vgl/gl.h (docs/c-api.md, section 16). Link vgl::gl.
5
6#ifndef VGL_GL_HPP
7#define VGL_GL_HPP
8
9#include <vgl/base.hpp>
10#include <vgl/context.hpp>
11#include <vgl/gl.h>
12
13#include <cstdint>
14#include <span>
15#include <utility>
16
17namespace vgl
18{
19
20// Owns a vgl_gl_backend. Destroy it on the render thread, with its OpenGL context current and
21// before its context.
23{
24public:
25 gl_backend() noexcept = default;
26 explicit gl_backend(vgl_gl_backend* handle) noexcept : handle_(handle) {}
27 gl_backend(const gl_backend&) = delete;
28 gl_backend& operator=(const gl_backend&) = delete;
29 gl_backend(gl_backend&& other) noexcept : handle_(other.release()) {}
30 gl_backend& operator=(gl_backend&& other) noexcept
31 {
32 if (this != &other)
33 {
34 reset(other.release());
35 }
36 return *this;
37 }
39
40 // Creates a backend for owner on the current OpenGL context.
41 [[nodiscard]] static expected<gl_backend>
43 {
44 vgl_gl_backend* handle = nullptr;
45 const auto r = static_cast<result>(vgl_gl_backend_create(owner.get(), &info, &handle));
46 if (failed(r))
47 {
48 return unexpected(r);
49 }
50 return gl_backend(handle);
51 }
52
53 [[nodiscard]] vgl_gl_backend* get() const noexcept { return handle_; }
54 [[nodiscard]] explicit operator bool() const noexcept { return handle_ != nullptr; }
55
56 [[nodiscard]] vgl_gl_backend* release() noexcept { return std::exchange(handle_, nullptr); }
57
58 void reset(vgl_gl_backend* handle = nullptr) noexcept
59 {
60 static_cast<void>(vgl_gl_backend_destroy(std::exchange(handle_, handle)));
61 }
62
63 // Draws the globe into the current framebuffer. info may be null.
64 [[nodiscard]] result draw(const vgl_gl_draw_info* info = nullptr) noexcept
65 {
66 return static_cast<result>(vgl_gl_backend_draw(handle_, info));
67 }
68
69 // Sets the stars it draws with VGL_DRAW_FEATURE_STARS (vgl_gl_backend_stars_set).
70 [[nodiscard]] result stars_set(std::span<const vgl_star> stars) noexcept
71 {
72 return static_cast<result>(vgl_gl_backend_stars_set(
73 handle_, stars.data(), static_cast<std::uint32_t>(stars.size())));
74 }
75
76 // Sets the clouds it draws with VGL_DRAW_FEATURE_CLOUDS (vgl_gl_backend_clouds_set).
77 [[nodiscard]] result
78 clouds_set(const std::uint8_t* coverage, std::uint32_t width, std::uint32_t height) noexcept
79 {
80 return static_cast<result>(vgl_gl_backend_clouds_set(handle_, coverage, width, height));
81 }
82
83 // Recovers from a lost OpenGL context, on the new one (vgl_gl_backend_device_reset).
84 [[nodiscard]] result device_reset() noexcept
85 {
86 return static_cast<result>(vgl_gl_backend_device_reset(handle_));
87 }
88
89private:
90 vgl_gl_backend* handle_ = nullptr;
91};
92
93} // namespace vgl
94
95#endif // VGL_GL_HPP
Definition context.hpp:106
Definition base.hpp:87
Definition gl.hpp:23
gl_backend() noexcept=default
gl_backend & operator=(gl_backend &&other) noexcept
Definition gl.hpp:30
vgl_gl_backend * release() noexcept
Definition gl.hpp:56
gl_backend(gl_backend &&other) noexcept
Definition gl.hpp:29
~gl_backend()
Definition gl.hpp:38
result stars_set(std::span< const vgl_star > stars) noexcept
Definition gl.hpp:70
result draw(const vgl_gl_draw_info *info=nullptr) noexcept
Definition gl.hpp:64
void reset(vgl_gl_backend *handle=nullptr) noexcept
Definition gl.hpp:58
vgl_gl_backend * get() const noexcept
Definition gl.hpp:53
result clouds_set(const std::uint8_t *coverage, std::uint32_t width, std::uint32_t height) noexcept
Definition gl.hpp:78
result device_reset() noexcept
Definition gl.hpp:84
static expected< gl_backend > create(context &owner, const vgl_gl_backend_create_info &info) noexcept
Definition gl.hpp:42
gl_backend(const gl_backend &)=delete
gl_backend & operator=(const gl_backend &)=delete
Definition base.hpp:72
vgl_result vgl_gl_backend_clouds_set(vgl_gl_backend *backend, const uint8_t *coverage, uint32_t width, uint32_t height)
vgl_result vgl_gl_backend_draw(vgl_gl_backend *backend, const vgl_gl_draw_info *info)
struct vgl_gl_backend vgl_gl_backend
Definition gl.h:80
vgl_result vgl_gl_backend_destroy(vgl_gl_backend *backend)
vgl_result vgl_gl_backend_create(vgl_context *context, const vgl_gl_backend_create_info *info, vgl_gl_backend **out_backend)
vgl_result vgl_gl_backend_device_reset(vgl_gl_backend *backend)
vgl_result vgl_gl_backend_stars_set(vgl_gl_backend *backend, const vgl_star *stars, uint32_t star_count)
Definition base.hpp:22
result
Definition base.hpp:25
Definition gl.h:84
Definition gl.h:144