Simple quad program?

BlitzMax Forums/BlitzMax OpenGL Programming/Simple quad program?

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

I don't agree that Max2D is 'ass-backwards' (because it's clearly not) but it does make it difficult for third parties to make modules that extend it or are based on it. Essentially you have to rewrite the entire driver if you want to do something simple but different. This is a problem when:

* The official Max2D and glMax2D modules are updated
* You want to distribute the modified driver (based on the original driver code) with your module - the BlitzMax licence isn't clear whether you're legally allowed to do that.

Including a state lock/state restore function in Max2D would be wonderful for third-party developers. It doesn't solve all the problems, but it solves a lot of them.

Something like this (not thoroughly tested):
Strict
Import pub.opengl
Import brl.glmax2d

Private

Global lock_gl_texture_2d:Int
Global lock_gl_texture_binding_2d:Int
Global lock_blendstate:Int
Global lock_rgba:Byte[4]

Public

Rem
bbdoc: Locks the gl state so you can use OpenGL functions with glMax2D.
You can use native OpenGL functions between LockGL() and UnlockGL().
All Max2D commands <u>must</u> be called outside of those functions. Be nice
with your OpenGL and clean up after yourself before calling UnlockGL().
These functions rely on Max2D working in a specific way - if the Max2D
internals break, these functions will need to be updated. Note that Graphics()
does not initialise the depth buffer.
End Rem
Function LockGL()
	lock_blendstate = GetBlend()
	
	Local r:Int, g:Int, b:Int, a:Int
	a = GetAlpha()*255#
	If(a>255) Then a=255
	If(a<0) Then a=0
	
	GetColor(r, g, b)
	
	lock_rgba[0] = Byte(r)
	lock_rgba[1] = Byte(g)
	lock_rgba[2] = Byte(b)
	lock_rgba[3] = Byte(a)

	glPushMatrix()
	glGetBooleanv(GL_TEXTURE_2D, Varptr lock_gl_texture_2d)
	glGetIntegerv(GL_TEXTURE_BINDING_2D, Varptr lock_gl_texture_binding_2d)
End Function


Rem
bbdoc: Unlock the OpenGL state so Max2D can use it again
End Rem
Function UnlockGL()
	glBindTexture(GL_TEXTURE_2D,lock_gl_texture_binding_2d)

	If(lock_gl_texture_2d)
		glEnable(GL_TEXTURE_2D)
	Else
		glDisable(GL_TEXTURE_2D)
	EndIf

	glPopMatrix()
	
	SetBlend(lock_blendstate)
	glColor4ubv(lock_rgba)
End Function

That only stores some of the texture functions and the colour/alpha settings though - there's many other state variables that you can store, but many of them are very slow.