Code archives/File Utilities/Media file 'Nibbler' [bmax]
This code has been declared by its author to be Public Domain code.
Download source code
| |||||
| Lets you give the user feedback about a file loading progress (thru progress-bar or other) by nibbling small chunks of the file (image, sound etc). This should work well with both local and remote (HTTP) files. The parameters for local and remote files are slightly different: the Nibble Create method requires a URL/filename, and optionally one extra parameter for a local file, or two for remote file. The second extra parameter for a remote file is so that you can guess/estimate the file size (not essential). | |||||
SuperStrict
Type Nibble
Field FileStream :TStream
Field Bank :TBank
Field BankStream :TBankStream
Field chunkSize :Int
Field ready :Int
Field estimatedFileSize :Int
Function Create:Nibble(file$,chunkSize:Int=20,estimatedFileSize:Int=4000)
Local nib:Nibble = New Nibble
nib.FileStream = OpenStream(file, True,False)
If nib.FileStream = Null Then Return Null
nib.chunksize = chunkSize
nib.estimatedFileSize = estimatedFileSize
Return nib
End Function
Method New()
Bank = CreateBank()
BankStream = CreateBankStream(Bank)
End Method
Method Delete()
CloseStream(BankStream)
CloseStream(FileStream)
Bank=Null
End Method
Method result:TBankStream()
SeekStream(BankStream, 0)
Return BankStream
End Method
Method Nibble:Float()
If Eof(FileStream) Return 1.0
Local nibbleSize:Int = StreamSize(FileStream)*(Float(chunkSize)/100.0)
If StreamPos(FileStream) = -1 Then nibbleSize = chunkSize
For Local f:Int = 0 To nibbleSize
If Eof(FileStream)
ready = True
Exit
EndIf
WriteByte BankStream,ReadByte(FileStream)
Next
If StreamPos(FileStream) = -1 Then
Return Float(BankSize(Bank) Mod estimatedFileSize) / Float(estimatedFileSize)
EndIf
Return Float(StreamPos(FileStream))/Float(StreamSize(FileStream))
End Method
Method Progress:Float()
Return Float(StreamPos(FileStream))/Float(StreamSize(FileStream))
End Method
Method Status:Int()
Return ready
End Method
End Type
Graphics 800,600,0
'Local mynibble:Nibble = Nibble.Create("zombie.jpg",5)
Local mynibble:Nibble = Nibble.Create("http::www.blitzbasic.com/img/brllogo-thin.png",50,4000)
Local progress:Float = 0
While mynibble.ready = False
Cls
DrawRect 50,50,progress*200.0,20
DrawText Int(progress*100.0),5,5
progress = mynibble.Nibble()
Flip
Wend
Global myImage:TImage = LoadImage(myNibble.result())
myNibble = Null
While Not KeyDown(KEY_ESCAPE)
DrawImage myImage, MouseX(), MouseY()
Flip
Cls
Wend
End |
Comments
| ||
| thanks for sharing :) |
| ||
| does anyone know how well this performs with remote streams? That is using the default chunk size of say 20 and assuming the server you're reading from is reasonably responsive, does the call to nibble() halt execution noticeably? I'm considering using this to load an image file from an HTTP server while upwards of 10 animated sprites are moving around the screen. I wanted to check if anyone had experience with this and if this would be a suitable appication of Nibble. Thanks! |
Code Archives Forum