AUI Framework  master
Cross-platform module-based framework for developing C++20 desktop applications
OpenGLRenderingContext.h
1/*
2 * AUI Framework - Declarative UI toolkit for modern C++20
3 * Copyright (C) 2020-2024 Alex2772 and Contributors
4 *
5 * SPDX-License-Identifier: MPL-2.0
6 *
7 * This Source Code Form is subject to the terms of the Mozilla Public
8 * License, v. 2.0. If a copy of the MPL was not distributed with this
9 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 */
11
12//
13// Created by Alex2772 on 12/7/2021.
14//
15
16#pragma once
17
18
19#include <AUI/Platform/CommonRenderingContext.h>
20#include "ARenderingContextOptions.h"
21#include "AUI/GL/OpenGLRenderer.h"
22
24public:
25 OpenGLRenderingContext(const ARenderingContextOptions::OpenGL& config) : mConfig(config) {}
26
27 void init(const Init& init) override;
28 ~OpenGLRenderingContext() override;
29
30 AImage makeScreenshot() override;
31
32 void destroyNativeWindow(AWindowBase& window) override;
33 void beginPaint(AWindowBase& window) override;
34 void endPaint(AWindowBase& window) override;
35 void beginResize(AWindowBase& window) override;
36 void endResize(AWindowBase& window) override;
37
38 [[nodiscard]]
39 uint32_t getDefaultFb() const noexcept;
40
41 void bindViewport();
42
43 [[nodiscard]]
44 glm::uvec2 viewportSize() const noexcept {
45 return mViewportSize;
46 }
47
48 [[nodiscard]]
49 uint32_t getSupersamplingRatio() const noexcept;
50
51 [[nodiscard]]
52 AOptional<gl::Framebuffer*> framebuffer() noexcept {
53 if (auto fb = std::get_if<gl::Framebuffer>(&mFramebuffer)) {
54 return fb;
55 }
56 return std::nullopt;
57 }
58
59 IRenderer& renderer() override {
60 return *mRenderer;
61 }
62
63 static gl::Framebuffer newOffscreenRenderingFramebuffer(glm::uvec2 initialSize);
64
65private:
67 struct NotTried{}; struct Failed{}; std::variant<NotTried, Failed, gl::Framebuffer> mFramebuffer;
68 _<OpenGLRenderer> mRenderer;
69 glm::uvec2 mViewportSize;
70
71 static _<OpenGLRenderer> ourRenderer() {
72 static _weak<OpenGLRenderer> g;
73 if (auto v = g.lock()) {
74 return v;
75 }
76 auto temp = _new<OpenGLRenderer>();
77 g = temp;
78 return temp;
79 }
80
81 void tryEnableFramebuffer(glm::uvec2 windowSize);
82 void beginFramebuffer(glm::uvec2 windowSize);
83 void endFramebuffer();
84
85#if AUI_PLATFORM_WIN
86 static HGLRC ourHrc;
87 static void makeCurrent(HDC hdc) noexcept;
88#elif AUI_PLATFORM_LINUX
89 static GLXContext ourContext;
90#elif AUI_PLATFORM_MACOS
91 static void* ourContext;
92#endif
93
94};
Utility wrapper implementing the stack-allocated (fast) optional idiom.
Definition: AOptional.h:32
Definition: AWindowBase.h:33
Definition: CommonRenderingContext.h:28
Base class for rendering.
Definition: IRenderer.h:149
Definition: OpenGLRenderingContext.h:23
An std::weak_ptr with AUI extensions.
Definition: SharedPtrTypes.h:177
Definition: Framebuffer.h:26
Definition: ARenderingContextOptions.h:57
Definition: IRenderingContext.h:38
An std::weak_ptr with AUI extensions.
Definition: SharedPtrTypes.h:51