sdl12-compat: SDL_GL_GetAttribute: bind FBO 0 before querying SDL2.

From 8f3b53bed30fa2e77734130514277ec2fa29bf2a Mon Sep 17 00:00:00 2001
From: "Ryan C. Gordon" <[EMAIL REDACTED]>
Date: Tue, 7 Dec 2021 18:17:59 -0500
Subject: [PATCH] SDL_GL_GetAttribute: bind FBO 0 before querying SDL2.

This appears to be an SDL2 bug; we are working around it.

Fixes #150.
---
 src/SDL12_compat.c | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)

diff --git a/src/SDL12_compat.c b/src/SDL12_compat.c
index a7b9e29..adbe2d1 100644
--- a/src/SDL12_compat.c
+++ b/src/SDL12_compat.c
@@ -6471,6 +6471,8 @@ SDL_GL_SetAttribute(SDL12_GLattr attr, int value)
 DECLSPEC int SDLCALL
 SDL_GL_GetAttribute(SDL12_GLattr attr, int* value)
 {
+    int retval;
+
     if (attr >= SDL12_GL_MAX_ATTRIBUTE) {
         return SDL20_SetError("Unknown GL attribute");
     }
@@ -6487,7 +6489,19 @@ SDL_GL_GetAttribute(SDL12_GLattr attr, int* value)
         *value = (OpenGLLogicalScalingSamples) ? 1 : 0;
         return 0;
     }        
-    return SDL20_GL_GetAttribute((SDL_GLattr) attr, value);
+
+    /* SDL2 has a bug where FBO 0 must be bound or SDL20_GL_GetAttribute gives incorrect information.
+       See https://github.com/libsdl-org/sdl12-compat/issues/150 */
+    if (OpenGLCurrentDrawFBO == 0) {
+        retval = SDL20_GL_GetAttribute((SDL_GLattr) attr, value);
+    } else {
+        SDL_assert(OpenGLFuncs.glBindFramebuffer != NULL);
+        OpenGLFuncs.glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
+        retval = SDL20_GL_GetAttribute((SDL_GLattr) attr, value);
+        OpenGLFuncs.glBindFramebuffer(GL_DRAW_FRAMEBUFFER, OpenGLCurrentDrawFBO);
+    }
+
+    return retval;
 }