AUI Framework  develop
Cross-platform base for C++ UI apps
Loading...
Searching...
No Matches
OpenGLRenderingContext.h
    1/*
    2 * AUI Framework - Declarative UI toolkit for modern C++20
    3 * Copyright (C) 2020-2025 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
   23class OpenGLRenderingContext: public CommonRenderingContext {
   24public:
   25    OpenGLRenderingContext(const ARenderingContextOptions::OpenGL& config) : mConfig(config) {}
   26
   27    ~OpenGLRenderingContext() override;
   28
   29    AImage makeScreenshot() override;
   30
   31#if !AUI_PLATFORM_LINUX
   32    // to be implemented by IPlatformAbstraction
   33    void init(const Init& init) override;
   34    void destroyNativeWindow(AWindowBase& window) override;
   35#endif
   36
   37    void beginPaint(AWindowBase& window) override;
   38    void endPaint(AWindowBase& window) override;
   39    void beginResize(AWindowBase& window) override;
   40    void endResize(AWindowBase& window) override;
   41
   42    [[nodiscard]]
   43    uint32_t getDefaultFb() const noexcept;
   44
   45    void bindViewport();
   46
   47    [[nodiscard]]
   48    glm::uvec2 viewportSize() const noexcept {
   49        return mViewportSize;
   50    }
   51
   52    [[nodiscard]]
   53    uint32_t getSupersamplingRatio() const noexcept;
   54
   55    [[nodiscard]]
   56    AOptional<gl::Framebuffer*> framebuffer() noexcept {
   57        if (auto fb = std::get_if<gl::Framebuffer>(&mFramebuffer)) {
   58            return fb;
   59        }
   60        return std::nullopt;
   61    }
   62
   63    IRenderer& renderer() override {
   64        return *mRenderer;
   65    }
   66
   67    static gl::Framebuffer newOffscreenRenderingFramebuffer(glm::uvec2 initialSize);
   68
   69protected:
   70    _<OpenGLRenderer> mRenderer;
   71    glm::uvec2 mViewportSize;
   72    struct NotTried{}; struct Failed{}; std::variant<NotTried, Failed, gl::Framebuffer> mFramebuffer;
   73    static _<OpenGLRenderer> ourRenderer() {
   74        static _weak<OpenGLRenderer> g;
   75        if (auto v = g.lock()) {
   76            return v;
   77        }
   78        auto temp = _new<OpenGLRenderer>();
   79        g = temp;
   80        return temp;
   81    }
   82
   83    virtual void endFramebuffer();
   84
   85private:
   86    ARenderingContextOptions::OpenGL mConfig;
   87
   88    void tryEnableFramebuffer(glm::uvec2 windowSize);
   89    void beginFramebuffer(glm::uvec2 windowSize);
   90
   91#if AUI_PLATFORM_WIN
   92    static HGLRC ourHrc;
   93    static void makeCurrent(HDC hdc) noexcept;
   94#endif
   95#if AUI_PLATFORM_MACOS
   96    static void* ourContext;
   97#endif
   98
   99};
Owning image representation.
Definition AImage.h:25
Utility wrapper implementing the stack-allocated (fast) optional idiom.
Definition AOptional.h:33
Definition AWindowBase.h:33
Definition CommonRenderingContext.h:19
Base class for rendering.
Definition IRenderer.h:149
An std::weak_ptr with AUI extensions.
Definition SharedPtrTypes.h:179
Definition Framebuffer.h:26
Definition ARenderingContextOptions.h:55
Definition IRenderingContext.h:38
Definition OpenGLRenderingContext.h:72
Definition OpenGLRenderingContext.h:72
An std::weak_ptr with AUI extensions.
Definition SharedPtrTypes.h:52