From: Stephan Soller <stephan.soller@helionweb.de>
Date: Thu, 9 Feb 2017 21:40:10 +0000 (+0100)
Subject: Framebuffer size has to be specified manually now because size queries only work... 
X-Git-Url: https://git.owens.tech/112-editable-focus.html/112-editable-focus.html/git?a=commitdiff_plain;h=e5598a2e5fd56661c80f06a315f498010c49ad7d;p=forks%2Fsingle-header-file-c-libs.git

Framebuffer size has to be specified manually now because size queries only work for framebuffers backed by textures.
---

diff --git a/slim_gl.c b/slim_gl.c
index 3bf1748..a41c78f 100755
--- a/slim_gl.c
+++ b/slim_gl.c
@@ -409,6 +409,9 @@ void texture_update(GLuint texture, const void* data) {
 //
 
 GLuint framebuffer_new(GLuint color_buffer_texture) {
+	GLint prev_draw_fb = 0;
+	glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &prev_draw_fb);
+	
 	GLuint framebuffer = 0;
 	glGenFramebuffers(1, &framebuffer);
 	
@@ -417,9 +420,10 @@ GLuint framebuffer_new(GLuint color_buffer_texture) {
 	
 	if ( glCheckFramebufferStatus(GL_DRAW_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE ) {
 		glDeleteFramebuffers(1, &framebuffer);
-		return 0;
+		framebuffer = 0;
 	}
 	
+	glBindFramebuffer(GL_DRAW_FRAMEBUFFER, prev_draw_fb);
 	return framebuffer;
 }
 
@@ -444,35 +448,14 @@ void framebuffer_blit(GLuint read_framebuffer, GLint rx, GLint ry, GLint rw, GLi
 	glBlitFramebuffer(rx, ry, rx+rw, ry+rh, dx, dy, dx+dw, dy+dh, GL_COLOR_BUFFER_BIT, GL_LINEAR);
 }
 
-void framebuffer_bind(GLuint framebuffer) {
+void framebuffer_bind(GLuint framebuffer, GLsizei width, GLsizei height) {
 	// Check framebuffer binding, and bind the target framebuffer if necessary
 	GLint bound_framebuffer = 0;
 	glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &bound_framebuffer);
 	if ((GLuint)bound_framebuffer != framebuffer) {
 		glBindFramebuffer(GL_DRAW_FRAMEBUFFER, framebuffer);
-		gl_error("Failed to bind framebuffer %u. glBindFramebuffer()", framebuffer);
-		
-		GLint type = 0;
-		glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE, &type);
-		if (type == GL_TEXTURE) {
-			GLint color_buffer = 0, old_texture_binding = 0, width = 0, height = 0;
-			glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME, &color_buffer);
-			
-			GLenum texture_type = GL_TEXTURE_2D;
-			glGetIntegerv(GL_TEXTURE_BINDING_2D, &old_texture_binding);
-			glBindTexture(texture_type, color_buffer);
-			if ( glGetError() == GL_INVALID_OPERATION ) {
-				glGetIntegerv(GL_TEXTURE_BINDING_RECTANGLE, &old_texture_binding);
-				texture_type = GL_TEXTURE_RECTANGLE;
-				glBindTexture(texture_type, color_buffer);
-			}
-			
-			glGetTexLevelParameteriv(texture_type, 0, GL_TEXTURE_WIDTH, &width);
-			glGetTexLevelParameteriv(texture_type, 0, GL_TEXTURE_HEIGHT, &height);
-			glBindTexture(texture_type, old_texture_binding);
-			
+		if ( !gl_error("Failed to bind framebuffer %u. glBindFramebuffer()", framebuffer) && width != 0 && height != 0 )
 			glViewport(0, 0, width, height);
-		}
 	}
 }
 
@@ -593,6 +576,9 @@ int render(GLenum primitive, GLuint program, const char* bindings, ...) {
 	uint32_t indices_to_render = 0;
 	//GLuint use_framebuffer = 0;
 	
+	// Make sure no previous error code messes up our state
+	glGetError();
+	
 	GLuint vertex_array_object = 0;
 	glGetIntegerv(GL_VERTEX_ARRAY_BINDING, (GLint*)&vertex_array_object);
 	if (vertex_array_object == 0) {
diff --git a/slim_gl.h b/slim_gl.h
index 463991b..5e37ac5 100755
--- a/slim_gl.h
+++ b/slim_gl.h
@@ -23,7 +23,7 @@ void   texture_update(GLuint texture, const void* data);
 GLuint framebuffer_new(GLuint color_buffer_texture);
 void   framebuffer_destroy(GLuint framebuffer);
 void   framebuffer_blit(GLuint read_framebuffer, GLint rx, GLint ry, GLint rw, GLint rh, GLuint draw_framebuffer, GLint dx, GLint dy, GLint dw, GLint dh);
-void   framebuffer_bind(GLuint framebuffer);
+void   framebuffer_bind(GLuint framebuffer, GLsizei width, GLsizei height);
 
 int    render(GLenum primitive, GLuint program, const char* bindings, ...);