Code archives/3D Graphics - Misc/Texture type

This code has been declared by its author to be Public Domain code.

Download source code

Texture type by Oddball2005
A texture type to be used with BlitzGL. It's main advantages are that it shares instances of the same texture but still auto-deletes unreferanced textures when FlushMem() is used.

To load a texture:
VarName:FDtexture=FDtexture.Load(filename$,mipmap%)

To bind a texture:
VarName.Bind()

To access the texture directly:
VarName.texture.ID

Remember: Textures are deleted automatically with FlushMem(). Manually deleting textures with glDeleteTextues will cause errors.
'Code by David Williamson
'01/07/05

Strict
Rem
	Texture type for use with BlitzGL.
	Auto-deletes textures when no longer in use(Remember to use FlushMem).
	
	To load a texture:
		VarName:FDtexture=FDtexture.Load(filename$,mipmap%)
		
	To bind a texture:
		VarName.Bind()
	
	To access the texture directly:
		VarName.texture.ID
	
End Rem

'Texture type
Type FDtexture
	
	'Texture data
	Field texture:fdtexdata
	
	'Bind texture
	Method Bind()
		'Bind texture
		glBindTexture GL_TEXTURE_2D,texture.ID
		
	End Method
	
	'Clean up method
	Method Delete()
		'Remove referance
		texture.refcount:-1
		
		'If texture not referanced anywhere else then remove from texture list
		If texture.refcount<1 Then fd_texlist.Remove(texture)
		
	End Method
	
	'Load texture
	Function Load:FDtexture(lfilename$,flag%=True)
		'Change to full path name
		lfilename=RealPath(lfilename)
		
		Local ltex:FDtexture=New FDtexture
		
		'Check to see if texture is already loaded
		For Local tdata:fdtexdata=EachIn fd_texlist
			
			'If it is already loaded
			If lfilename=tdata.filename
				
				'Referance texture
				ltex.texture=tdata
				
				'increase refcounter
				ltex.texture.refcount:+1
				
				'return loaded texture
				Return ltex
				
			EndIf
			
		Next
		
		'If texture isn't already loaded
		ltex.texture=New fdtexdata
		
		'Store texture filename
		ltex.texture.filename=lfilename
		
		'Load texture
		ltex.texture.ID=bglTexFromPixmap(LoadPixmap(lfilename),flag)
		
		'Return the loaded texture
		Return ltex
		
	End Function

End Type

'List of loaded textures
Global fd_texlist:TList=CreateList()

'Shared texture info
Type fdtexdata
	
	Field ID%
	Field refcount%=1
	Field filename$
	
	Method New()
		'Add to texture list
		fd_texlist.AddLast(Self)
	
	End Method
	
	'Clean up method
	Method Delete()
		'Delete texture
		gldeletetextures 1,Varptr ID
		
	End Method
	
End Type

Comments

Dreamora2005
better replace the last part with:

'Shared texture info
Type fdtexdata
        'List of loaded textures
	Global fd_texlist
	Field ID%
	Field refcount%=1
	Field filename$
	
	Method New()
                if fd_texlist = null fd_texlist = new TList
		'Add to texture list
		fd_texlist.AddLast(Self)
	
	End Method
	
	'Clean up method
	Method Delete()
		'Delete texture
		gldeletetextures 1,Varptr ID
		
	End Method
	
End Type


This way the global isn't interfering with "real" code globals

You then just need to replace fd_texlist.remove () above with fdtexdata.fd_texlist.remove ()


Oddball2005
That's a good idea. In the module this is from I have that part within the private section so that isn't an issue, but I probably should have changed it for the code archive. Cheers.


Code Archives Forum