Drawing to front buffer, tip

BlitzMax Forums/BlitzMax Programming/Drawing to front buffer, tip

ImaginaryHuman(Posted 2006) [#1]
Here's just a little tip for anyone interested.... You don't necessarily have to draw stuff to the backbuffer. You can draw it to the front buffer and it will be immediately visible without having to perform a Flip. This seems not to work using Max2D, though - DrawRect still will draw to the backbuffer.

When you do the glFlush() it makes sure the gfx card performs all the instructions you've given it, rather than keeping them in a queue. The nice thing is you can draw stuff to the backbuffer, it will be kept there - `preserved` if you will, and then you can draw more stuff on top of it, AND be able to see what is being drawn. This means you don't have to grab the backbuffer and redraw it to restore/keep the contents. So if you wanted to maintain a backdrop and have objects moving over it, you could draw the backdrop once, then just draw the objects and just`restore` the areas that are uncovered as the objects move (using some other technique ;-)

Handy for things like paint programs. You could also set up your own display rather than using Graphics(), so that you only have single-buffering to begin with. The only thing to remember is that without using the backbuffer, you may not always avoid flicker. And if you DON'T want the user to see what is being drawn until all of it is done, you'd need to revert to a backbuffer also.

Strict
Graphics 640,480,0

glDrawBuffer(GL_FRONT)

Repeat
	Local x:Int=MouseX()
	Local y:Int=MouseY()
	glbegin(GL_QUADS)
		glvertex2i x,y
		glvertex2i x+50,y
		glvertex2i x+50,y+50
		glvertex2i x,y+50
	glend()
	glflush()
Until KeyHit(KEY_ESCAPE)
End