[bb] render in thread by Bobysait [ 1+ years ago ]

Started by BlitzBot, June 29, 2017, 00:28:41

Previous topic - Next topic

BlitzBot

Title : render in thread
Author : Bobysait
Posted : 1+ years ago

Description : Allow to render an image in a thread using the FastPointer dll.

Main Loop access to the thread variables using a collection of type


Code :
Code (blitzbasic) Select
Graphics3D 800,600,0,2
SetBuffer BackBuffer()

; Register Pointer and Thread
thread.TThread = New TThread
hnd = Handle(thread)

ptr = FunctionPointer()
Goto skip
TextureThreaded(0)
.skip
th = CreateThread (Ptr,hnd)
threadThread = th
threadPtr = Ptr

Img=CreateImage(800,600)

starttime=MilliSecs()

Repeat

; delay / No delay
If KeyHit(57)
DelayThread=1-DelayThread
Print thread hread
PauseThread(thread hread)
Repeat
MyVars.ThreadVars=threadVars
If MyVars<>Null MyVarsDelayed=DelayThread:Exit
Forever
ResumeThread(thread hread)
EndIf

; pause the thread to access thread variables
PauseThread(thread hread)
; get the variable container
MyVars.ThreadVars=threadVars
; Check MyVars exists !
If MyVars<>Null
; thread state
cur=MyVarsPercent
; end of thread loop : Grab the image
If threadReady=True
threadloop = MyVarsloop
threadRate = MyVarsFps
threadDelay = MyVarsDelayed
CopyRect 0,0,800,600,0,0,ImageBuffer(MyVarsImg),ImageBuffer(Img)
EndIf
ThreadReady=False
EndIf
ResumeThread(thread hread)

Cls
time=MilliSecs()-starttime
fpscount=fpscount+1
If time>fpstime
fpstime=time+1000
fps=fpscount
fpscount=0
EndIf
DrawImage Img,0,0
loop=loop+1
Color 0,0,0
Rect 0,0,200,100,1
Color 200,200,200
Text 10,10,"Main loop/fps : "+loop+" ["+fps+"]"
Text 10,25,"thread loops  : "+threadloop
Text 10,40,"Thread Fps    : "+threadrate
Text 10,55,"Thread Delayed: "+threadDelay
Color 0,0,0
Rect 08,78,104,24,1
Color 100,100,100
Rect 08,78,104,24,0
Color 255-cur*2.5,cur*2.5,0
Rect 08,78,cur,20,1
Flip
Until KeyHit(1)
End







Type TThread
; pointers
Field Ptr%
Field Thread%
; thread state ( Ready=true +> Thread in end of loop , waiting for Ready=false )
Field Ready%
; specific variables for thread
Field Vars.ThreadVars
End Type

Type ThreadVars
Field Thread.TThread
    Field Loop%,time%,Starttime%,fps%,Delayed%

; specific to the function
Field Img%,ImgReady%,Percent%
End Type



Function TextureThreaded(Handle_Thread%)

; create instance for variables storing
MyVars.ThreadVars = New ThreadVars
; setup
MyvarsDelayed = True
; link vars to thread
thread.TThread = Object.TThread(Handle_Thread)
MyvarsThread = Thread
; Link thread to vars
MyvarsThreadVars = Myvars

MyvarsStarttime = MilliSecs()

MyVarsImg = CreateImage(800,600)

Repeat
ThreadReady = False

; thread Fps
MyVars ime=MilliSecs()-MyVarsStarttime
LoopCount=LoopCount+1

If MyVars ime>fpsTime
fpsTime=MyVars ime+1000
MyVarsfps=LoopCount
LoopCount=0
EndIf

; Thread Count loop
MyVarsLoop=MyVarsLoop+1

; thread delay
If MyVarsDelayed Delay DelayForThread

LockBuffer(ImageBuffer(MyVarsImg))
For j = 0 To 599
MyVarsPercent=j/6
For i = 0 To 799
col=Rand(50,200)
col=col Shl(16)+col Shl(8)+col
WritePixelFast i,j,col,ImageBuffer(MyVarsImg)
Next
Next
UnlockBuffer(ImageBuffer(MyVarsImg))

; thread pause +> Image ready for capture.
ThreadReady = True
Repeat
Delay 1
Until ThreadReady=False
Forever
End Function


Comments : none...