December 03, 2020, 08:13:42 PM

Author Topic: [bmx] Media file 'Nibbler' [bmax] by Beaker [ 1+ years ago ]  (Read 537 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
Title : Media file 'Nibbler' [bmax]
Author : Beaker
Posted : 1+ years ago

Description : 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).

Code :
Code: BlitzMax
  1. SuperStrict
  2.  
  3. Type Nibble
  4.         Field FileStream        :TStream
  5.         Field Bank              :TBank
  6.         Field BankStream        :TBankStream
  7.         Field chunkSize         :Int
  8.         Field ready             :Int
  9.         Field estimatedFileSize :Int
  10.        
  11.         Function Create:Nibble(file$,chunkSize:Int=20,estimatedFileSize:Int=4000)
  12.                 Local nib:Nibble = New Nibble
  13.                 nib.FileStream = OpenStream(file, True,False)
  14.                 If nib.FileStream = Null Then Return Null
  15.                 nib.chunksize = chunkSize
  16.                 nib.estimatedFileSize = estimatedFileSize
  17.                 Return nib
  18.         End Function
  19.        
  20.         Method New()
  21.                 Bank = CreateBank()
  22.                 BankStream = CreateBankStream(Bank)
  23.         End Method     
  24.        
  25.         Method Delete()
  26.                 CloseStream(BankStream)
  27.                 CloseStream(FileStream)
  28.                 Bank=Null
  29.         End Method
  30.        
  31.         Method result:TBankStream()
  32.                 SeekStream(BankStream, 0)
  33.                 Return BankStream
  34.         End Method
  35.                
  36.         Method Nibble:Float()
  37.                 If Eof(FileStream) Return 1.0
  38.  
  39.                 Local nibbleSize:Int = StreamSize(FileStream)*(Float(chunkSize)/100.0)
  40.                 If StreamPos(FileStream) = -1 Then nibbleSize = chunkSize
  41.                 For Local f:Int = 0 To nibbleSize
  42.                         If Eof(FileStream)
  43.                                 ready = True
  44.                                 Exit
  45.                         EndIf
  46.                         WriteByte BankStream,ReadByte(FileStream)
  47.                 Next
  48.                 If StreamPos(FileStream) = -1 Then
  49.                         Return Float(BankSize(Bank) Mod estimatedFileSize) / Float(estimatedFileSize)
  50.                 EndIf
  51.                 Return Float(StreamPos(FileStream))/Float(StreamSize(FileStream))
  52.         End Method
  53.  
  54.         Method Progress:Float()
  55.                 Return Float(StreamPos(FileStream))/Float(StreamSize(FileStream))
  56.         End Method
  57.  
  58.         Method Status:Int()
  59.                 Return ready
  60.         End Method
  61. End Type
  62.  
  63.  
  64. Graphics 800,600,0
  65.  
  66. 'Local mynibble:Nibble = Nibble.Create("zombie.jpg",5)
  67. Local mynibble:Nibble = Nibble.Create("http::www.blitzbasic.com/img/brllogo-thin.png",50,4000)
  68.  
  69.  
  70. Local progress:Float = 0
  71. While mynibble.ready = False
  72.         Cls
  73.         DrawRect 50,50,progress*200.0,20
  74.         DrawText Int(progress*100.0),5,5
  75.         progress = mynibble.Nibble()   
  76.         Flip
  77. Wend
  78.  
  79. Global myImage:TImage = LoadImage(myNibble.result())
  80.  
  81. myNibble = Null
  82.  
  83. While Not KeyDown(KEY_ESCAPE)
  84.         DrawImage myImage, MouseX(), MouseY()
  85.         Flip
  86.         Cls
  87. Wend
  88. End


Comments :


amonite(Posted 1+ years ago)

 thanks for sharing :)


jtfrench(Posted 1+ years ago)

 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!


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal