TMSafeLoadImage

BlitzMax Forums/BlitzMax Programming/TMSafeLoadImage

_Skully(Posted 2010) [#1]
I have this function


Function TMSafeLoadImage:TImage(Path:String,flag:Int=-1)
	If FileType(Path)
		Local image:TImage=LoadImage(path,flag)
		If image
			Return image
		Else
			Notify "SafeLoadImage Error:  File "+Path+" exists but does not load"
		EndIf
	Else
		Notify "SafeLoadImage Error:  File "+Path+" does not exist"
		End
	End If
End Function


It will work fine with a normal file reference but fails with Incbin::
Does this occur for you as well... anyone know why it would?


plash(Posted 2010) [#2]
FileType only handles files on the file system.

You'll have to do something else for incbinned files.


Tommo(Posted 2010) [#3]
Function TMSafeLoadImage:TImage(Path:String,flag:Int=-1)
        local stream:TStream=ReadStream(path)
	If stream
		Local image:TImage=LoadImage(stream,flag)
 		stream.close()
 		If image
			Return image
		Else
			Notify "SafeLoadImage Error:  File "+Path+" exists but does not load"
		EndIf
	Else
		Notify "SafeLoadImage Error:  File "+Path+" does not exist"
		End
	End If
End Function


Try this.


_Skully(Posted 2010) [#4]
Oh talk about tunnel vision.. night time!

Function TMSafeLoadImage:TImage(Path:String,flag:Int=-1)
	If Path.Contains("Incbin::") Return LoadImage(Path,flag)
	If FileType(Path)
		Local image:TImage=LoadImage(path,flag)
		If image
			Return image
		Else
			Notify "SafeLoadImage Error:  File "+Path+" exists but does not load"
		EndIf
	Else
		Notify "SafeLoadImage Error:  File "+Path+" does not exist"
		End
	End If
End Function


Thanks!


Brucey(Posted 2010) [#5]
Shouldn't you still check "If Image" ?

And Tommo's idea of using TStream I would expand to have TMSafeLoadImage() accept Object as a param, which could then convert to TStream/String internally.
But if the function is a private thing, then I guess it doesn't matter?


ziggy(Posted 2010) [#6]
If you get the Path as an object instead of as a string it should work.


_Skully(Posted 2010) [#7]
Shouldn't you still check "If Image" ?

Quite right... I've been running on about 3-4 hours sleep the last few days... got a good 6 in last night so I'm feeling a little more aware ;)


And Tommo's idea of using TStream I would expand to have TMSafeLoadImage() accept Object as a param, which could then convert to TStream/String internally.


indeed....


But if the function is a private thing, then I guess it doesn't matter?


No its one of the documented TilMmax functions. I found it handy when I used a similar approach in Santas Slay. I guess the zip extracted flat and the game notified of the problem... it was when we were trying to get it running on vista.. which I don't think ever worked (earlier B3D)... I've lost the source (one month comp.. HD Controller crash.. both mirrors killed).

If you get the Path as an object instead of as a string it should work.

I like that idea


Brucey(Posted 2010) [#8]
I like that idea

Of course you do.. I said it first ;-)