Ooops
November 28, 2020, 10:42:35 AM

Author Topic: [bb] Threading by Andy [ 1+ years ago ]  (Read 554 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] Threading by Andy [ 1+ years ago ]
« on: June 29, 2017, 12:28:41 AM »
Title : Threading
Author : Andy
Posted : 1+ years ago

Description : Emulate threading so that you can make animated loading screens etc.

Code :
Code: BlitzBasic
  1. Global thread_branch=0
  2. Global thread_count=0
  3. Global thread_percent=0
  4. Global filecount
  5. Global file_in$="image.img"
  6. Global file_out$="image.img"
  7. Global filein
  8. Global fileout
  9. Global x=0
  10. Global y=0
  11.  
  12. ; Create the file you are loading later
  13. savefile(file_out$)
  14.  
  15. Graphics 800,600
  16.  
  17. ; Setup imagebuffer
  18. Global img=CreateImage(640,480)
  19.  
  20. ; Setup mousepointer
  21. Global mouseimg=CreateImage(10,10)
  22. SetBuffer ImageBuffer(mouseimg)
  23. Color 250,250,250
  24.  For q= 0 To 9
  25.  Plot q,q
  26.  Plot 0,q
  27.  Next
  28.  Color 5,5,5
  29.  For q= 1 To 9
  30.  Plot q+1,q
  31.  Plot 1,q
  32.  Next
  33.  
  34. ; setup doublebuffering
  35. SetBuffer BackBuffer()
  36.  
  37. ; Starting values
  38. thread_branch=1
  39.  
  40. ; main loop
  41. While Not KeyHit(1)
  42.  
  43. ; branch to file loading
  44.         If thread_branch=1 Then
  45.          loadfile(file_in$)
  46.     EndIf
  47.  
  48. ; draw on backbuffer
  49. SetBuffer BackBuffer()
  50. Cls
  51. Color 255,255,255
  52. Text 360,230,"Loading..."
  53.  
  54. ; draw the slider
  55.         If thread_branch=1 Then
  56.      thread_percent=((filecount*100)/FileSize(file_in$))
  57.      Color 255,255,255
  58.      Rect 150,250,500,25,0
  59.      Rect 150,250,thread_percent*5,25,1
  60.     EndIf
  61.  
  62. ; Draw the image when loaded
  63.         If thread_branch=2 Then
  64.          DrawBlock img, 250,200
  65.      Text 360,450,"Esc to quit"
  66.     EndIf
  67.  
  68. ; draw mouse pointer
  69. DrawImage mouseimg, MouseX(), MouseY()
  70.    
  71. Flip False
  72. Wend
  73.  
  74. End
  75.  
  76.  
  77.  
  78. ; saving file
  79. Function savefile(file_out$)
  80. fileout = WriteFile(file_out$)
  81. For x=0 To 319
  82.         For y=0 To 199
  83.         WriteByte( fileout, Rnd(0,255))
  84.         WriteByte( fileout, Rnd(0,255))
  85.         WriteByte( fileout, Rnd(0,255))
  86.         Next
  87. Next
  88. CloseFile( fileout )
  89. End Function
  90.  
  91. ; loading file in logical steps
  92. Function loadfile(file_in$)
  93.  
  94. ; step one - open file for reading
  95. If thread_count=0 Then
  96.         filein = ReadFile(file_in$)
  97.         thread_count=1
  98.     filecount=0
  99.     x=0
  100.     y=0
  101. EndIf
  102.  
  103. ; step two - read data and write to imagebuffer
  104. If thread_count =1 Then
  105.         y=y+1
  106.  
  107.         If y=200 Then
  108.                 x=x+1
  109.                 y=0
  110.         EndIf
  111.         Read1 = ReadByte( filein )
  112.         Read2 = ReadByte( filein )
  113.     Read3 = ReadByte( filein )
  114.         filecount=filecount+3
  115.  
  116. ; draw to imagebuffer
  117.     SetBuffer ImageBuffer(img)
  118.         Color read1,read2,read3
  119.         Plot x,y
  120. ; check for end of file
  121.         If Eof(filein) Then
  122.                 thread_count=2
  123.         EndIf
  124. EndIf
  125.  
  126. ; step three - close file
  127. If thread_count=2 Then
  128.         CloseFile( filein )
  129.         thread_branch=2
  130. EndIf
  131. End Function


Comments : none...

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal