#include "glfw_gl_backend.hpp" #include #include #include #include class GLFWGLRenderableResource final : public mln::gl::RenderableResource { public: explicit GLFWGLRenderableResource(GLFWGLBackend& backend_) : backend(backend_) {} void bind() override { MLN_TRACE_FUNC(); backend.setFramebufferBinding(0); backend.setViewport(0, 0, backend.getFramebufferSize()); } void swap() override { MLN_TRACE_FUNC(); backend.swap(); } private: GLFWGLBackend& backend; }; GLFWGLBackend::GLFWGLBackend(GLFWwindow* window_, const bool capFrameRate) : mln::gl::RendererBackend(mln::gfx::ContextMode::Unique), mln::gfx::Renderable( [window_] { int fbWidth; int fbHeight; glfwGetFramebufferSize(window_, &fbWidth, &fbHeight); return mln::Size{static_cast(fbWidth), static_cast(fbHeight)}; }(), std::make_unique(*this)), window(window_) { MLN_TRACE_FUNC(); glfwMakeContextCurrent(window); if (!capFrameRate) { // Disables vsync on platforms that support it. glfwSwapInterval(0); } else { glfwSwapInterval(1); } } GLFWGLBackend::~GLFWGLBackend() = default; void GLFWGLBackend::activate() { MLN_TRACE_FUNC(); glfwMakeContextCurrent(window); } void GLFWGLBackend::deactivate() { MLN_TRACE_FUNC(); glfwMakeContextCurrent(nullptr); } mln::gl::ProcAddress GLFWGLBackend::getExtensionFunctionPointer(const char* name) { return glfwGetProcAddress(name); } void GLFWGLBackend::updateAssumedState() { MLN_TRACE_FUNC(); assumeFramebufferBinding(0); setViewport(0, 0, getSize()); } mln::Size GLFWGLBackend::getFramebufferSize() const { return getSize(); } void GLFWGLBackend::setFramebufferSize(const mln::Size newSize) { setRenderableSize(newSize); } void GLFWGLBackend::swap() { MLN_TRACE_FUNC(); glfwSwapBuffers(window); } namespace mln { namespace gfx { template <> std::unique_ptr Backend::Create(GLFWwindow* window, bool capFrameRate) { MLN_TRACE_FUNC(); return std::make_unique(window, capFrameRate); } } // namespace gfx } // namespace mln