Can VFTLib be compiled into a BlitzMax module?

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

JoshK(Posted 2006) [#1]
VTFLib loads Half-Life 2 .vtf textures. Can this be compiled into a BlitzMax module? I have the dll working, but am getting some random crashes, probably because I declared a function incorrectly.

Binary:
http://nemesis.thewavelength.net/files/files/vtflib123-bin.zip
Source code:
http://nemesis.thewavelength.net/files/files/vtflib123.zip


Dreamora(Posted 2006) [#2]
Yes, its definitely possible to compile that to a BM module from what I've seen in the sources of the vtflib folder.
It would just be a little externing of classes and enums if you haven't already done that for using the DLL

But beside that it should work quite good.

Interesting question would be why the DLL is not working or if it is more of an interfacing problem there (like creating Byte Ptr versions of data you send without freeing them after sending or after finishing which creates leaks or the like)


DStastny(Posted 2006) [#3]
Not real familar with the library but a question, if compiled into BMAX module wouldnt that then make any program that compiled it in need to be open source due to the LPGL?

Seems like if left in DLL then the source to application using the library is not restricted.

The DLL should work. You want someone to review your implmentation I would take a look. Dont have time to impelment the interface myself. From my quick glance it seems the DLL is using "C" calling convention so are your extern "C" or extern "WIN32" that might be cause of your problem.

Doug Stastny


JoshK(Posted 2006) [#4]
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



Dreamora(Posted 2006) [#5]
I'm not 100% sure but quite sure:

Reason for the crash is that it returns null all the time. (assume that you compile in strict. Not doing so is a bad idea)

You need to declare it before "if vlBindImage(vtfimage0)"

on the other hand: Whats vtfimage0? The creatimage points to vtfimage ...

I definitely advice to use strict to prevent such hard to spot errors. thankfully BM supports it so you aren't in the same hard situation as with BlitzPlus anymore :-)


JoshK(Posted 2006) [#6]
It was the vtfimage0 thing. Thanks.

Sometimes it is good to have another set of eyes. I go through so much code that my brain stops working.


JoshK(Posted 2006) [#7]
Actually, I take that back. I accidentally changed the name of the bumpmap to one that doesn't exist. The VTF loader still crashes.


DStastny(Posted 2006) [#8]
Try removing the "Win32" I dont see the _stdcall convention on the API in the header file.

Doug Stastny


JoshK(Posted 2006) [#9]
Bingo.

Thanks!


DStastny(Posted 2006) [#10]
Welecome.

Doug Stastny


Dreamora(Posted 2006) [#11]
But still: Strong advice to program only in Strict.
No name typing error problems, strong scoping, usable by others (I think no one works in BM without strict for quite long) as I assume that you are not planning to use it forever on your own only :-)

But beside that: good that your problem was solved.