Can VFTLib be compiled into a BlitzMax module?

BlitzMax Forums/BlitzMax Programming/Can VFTLib be compiled into a BlitzMax module?

Sure, please take a look.

I tried replacing some of the Int values with Byte, but it still crashes. It loads the pixmap okay, but I get an unexplainable crash when the LoadVTF() function returns. Untrackable errors usually mean there is a problem with memory or the calling stack, in my experience:

Global VTF_HLIB

Global vlInitialize:Int() "win32"
Global vlCreateImage:Int(imageformat:Byte Ptr) "win32"
Global vlBindImage:Int(vtfimage:Int) "win32"
Global vlImageLoad:Int(file$z,headeronly:Byte) "win32"
Global vlImageGetWidth:Int() "win32"
Global vlImageGetHeight:Int() "win32"
Global vlImageGetFormat:Int() "win32"
Global vlImageGetData:Int(Frame:Int,Face:Int,MipmapLevel:Int) "win32"
Global vlImageConvertToRGBA8888:Int(Source:Int,Dest:Byte Ptr,Width:Int,Height:Int,SourceFormat:Int) "win32"
Global vlDeleteImage(vtfimage:Int) "win32"
Global vlShutdown() "win32"

Function VTFInit()
	If VTF_HLIB Return VTF_HLIB
	ProgramLog "Initializing VTF library..."
	VTF_HLIB=LoadLibraryA(AppDir$+"\VTFLib.dll")
	If VTF_HLIB
		vlInitialize=getprocaddress(VTF_HLIB,"vlInitialize")
		vlCreateImage=getprocaddress(VTF_HLIB,"vlCreateImage")
		vlBindImage=getprocaddress(VTF_HLIB,"vlBindImage")
		vlImageLoad=getprocaddress(VTF_HLIB,"vlImageLoad")
		vlImageGetWidth=getprocaddress(VTF_HLIB,"vlImageGetWidth")
		vlImageGetHeight=getprocaddress(VTF_HLIB,"vlImageGetHeight")
		vlImageGetFormat=getprocaddress(VTF_HLIB,"vlImageGetFormat")
		vlImageGetData=getprocaddress(VTF_HLIB,"vlImageGetData")
		vlImageConvertToRGBA8888=getprocaddress(VTF_HLIB,"vlImageConvertToRGBA8888")
		vlDeleteImage=getprocaddress(VTF_HLIB,"vlDeleteImage")
		vlShutdown=getprocaddress(VTF_HLIB,"vlShutdown")
		If vlInitialize()
			Return VTF_HLIB
		Else
			freelibrary VTF_HLIB
			VTF_HLIB=0
			ProgramLog "Failed to initialize VTF library",1
		EndIf
	Else
		ProgramLog "Failed to initialize VTF library",1
	EndIf
EndFunction

Function LoadVTF:TPixmap(file$)
	If Not VTFInit() Return
	Local vtfimage
	If vlCreateImage(Varptr(vtfimage))
		If vlBindImage(vtfimage0)
			If vlImageLoad(file$,0)
				width=vlImageGetWidth()
				height=vlImageGetHeight()
				format=vlImageGetFormat()
				imagedata=vlImageGetData(0,0,0)
				pixmap:TPixmap=CreatePixmap(width,height,PF_RGBA8888,4)
				vlImageConvertToRGBA8888(imagedata,pixmap.pixelptr(0,0),width,height,format)
			EndIf
		EndIf
		vlDeleteImage(vtfimage)
		Return pixmap
	EndIf
EndFunction

Function VTFEnd()
	If VTF_HLIB
		vlShutdown()
		FreeLibrary VTF_HLIB
		VTF_HLIB=0
	EndIf
EndFunction