# glfwSwapBuffers() and window resize events

**URL:** <https://discourse.glfw.org/t/glfwswapbuffers-and-window-resize-events/232>\
**Category:** support\
**Created:** [May 1, 2012, 3:49pm UTC](https://discourse.glfw.org/t/glfwswapbuffers-and-window-resize-events/232 "2012-05-01T15:49:08Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex035/uploads/glfw/original/1X/b8cdefe6b61b8d5aee6c43b2c56e9cf5eabecfe4.svg) [@system](https://discourse.glfw.org/u/system)\
**Post date:** [May 1, 2012, 3:49pm UTC](https://discourse.glfw.org/t/glfwswapbuffers-and-window-resize-events/232/1 "2012-05-01T15:49:08Z")

</div>

**[geago](https://sourceforge.net/u/geago/)** wrote on [Tuesday, May 01, 2012](https://sourceforge.net/p/glfw/discussion/247562/thread/ff83bd28/#0e27):

Hello…I was hoping someone might be able to comment on my code below so I can  
better understand why it works the way it works.

I’ve simplified and changed the code as much as possible to replicate a  
behavior I have noticed in my other programs.

In the code below I have basically 4 sets of 3 lines of code that are:  
glClear; glClearColor and glfwSwapBuffers.

I’ve commented them out below with // 1, // 2, // 3, and // 4

If I have all the //1 code uncommented and run the program, and drag to a new  
window size I get the new window size background completely filled with old  
color black ( not the new color ).

If I uncomment all the // 2 code then if I do the same I will get the new  
sized window filled with the new color.

So at this point my question is why would it take 2 iterations of the same  
code to get this result.

But if that is the case where in my 2 iterations of glClear, glClearColor and  
glfwSwapBuffers is that information ( new window size ) being passed on to  
opengl that I am getting the results as expected ie. the full window sized  
filled with the new color?

What makes it even more confusing is the commented code I’m talking about  
doesn’t behave the same within my callback function window\_size\_change.

If recomment the // 1 and // 2 code and then uncomment the // 3 code what  
happens is if I resize the window the original window size area remains black  
( original color ) while the new window resized area is transparent. Each  
resize afterwards fills the old windows with the new color with every new  
window resized area remaining transparent.

If I uncomment // 4 code I get the same results with the exception that first  
window resize incorporates the new color and as before new window areas are  
transparent.

So I guess my question is why do I need to call code that is in // 1 and // 2  
to get my result ( I would have thought just glClear, glclearColor and  
glfwSwapBuffers would have sufficed)?

And secondly, why does repeating the same code within my function  
"Window\_size\_change" yield different results?

Any comments on the matter is much appreciated, thank you.

```
#include <windows.h>
#include <stdbool.h>
#include <stdio.h>
#include <GL/glfw.h>
// ******Functions Declared*********************
bool setup_GL(int, int);
void GLFWCALL Window_size_change( int, int);
// ********Variables to use***********************
int running, new_window_width, new_window_height;
bool testswap;
// **********Program starts************************
int main( void )
{
new_window_width = 200;
new_window_height = 200;
glfwInit();
glfwOpenWindow( new_window_width, new_window_height, 0,0,0,0,0,0, GLFW_WINDOW ); 
setup_GL(new_window_width, new_window_height);
running = GL_TRUE;
testswap = FALSE;
while( running )
	{
	glfwWaitEvents();
	
	glfwSetWindowSizeCallback( Window_size_change );
	if(testswap)
		{
	// 1	glClear(GL_COLOR_BUFFER_BIT);
	// 1	glClearColor(1.0f, 0.0f, 0.0f, 0.0f);			
	// 1	glfwSwapBuffers();
	// 2	glClear(GL_COLOR_BUFFER_BIT);
	// 2	glClearColor(1.0f, 0.0f, 0.0f, 0.0f);
	// 2 glfwSwapBuffers();
		testswap = FALSE;
		}
	
	if( glfwGetWindowParam( GLFW_OPENED) == FALSE)                   
		{
		running = FALSE;
		}
	}
	
glfwTerminate();
exit( EXIT_SUCCESS );
}
// *******************Functions Defined********************************
bool setup_GL(int width, int height)
{
   glClear(GL_COLOR_BUFFER_BIT);
   glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
   glClearDepth(1.0);
   glDepthFunc(GL_LESS);
   glEnable(GL_DEPTH_TEST);
   glShadeModel(GL_SMOOTH);
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
   glOrtho( -(width/2), width/2, -(height/2), height/2, -1, 1 );
   glMatrixMode(GL_MODELVIEW);
   glLoadIdentity();
   glfwSwapBuffers();
if( glGetError() != GL_NO_ERROR )
	{
	return false;
	} 
return true;
}	
// ******************************************************************
void GLFWCALL Window_size_change(int width, int height )
{
if( new_window_width != width || new_window_height != height )
		{
		new_window_width = width;
		new_window_height = height;
	// 3	glClear(GL_COLOR_BUFFER_BIT);
	// 3	glClearColor(1.0f, 0.0f, 0.0f, 0.0f);			
	// 3	glfwSwapBuffers();
	// 4	glClear(GL_COLOR_BUFFER_BIT);
	// 4	glClearColor(1.0f, 0.0f, 0.0f, 0.0f);
  	// 4	glfwSwapBuffers();
		testswap = TRUE;
		}
}
```
