News:

Function Finder  Find all the functions within source code files

Main Menu

Threads

Started by ATLUS, September 10, 2014, 02:26:17 PM

Previous topic - Next topic

ATLUS

Hello folks.

Is PlayBasic support multiThreads? For Example: i want to move my circle and my TypeWriter function write.
Like in Java for example:

Thread thread = new Thread(TypeWriter(mytext$));;

Maybe it's not thread matter ?

PlayBASIC Code: [Select]
global xt = 20
x = 100
y = 100
setfps 60
do


TypeWriter("Hello this is my typewriter function")

circle x,y, 10, 0

if RightKey() = 1
x+=1
endif

sync
loop

Function TypeWriter(mytext$)
For t=1 To Len(mytext$)
Text (t*10),xt,Mid$(mytext$,t,1)
wait 100


sync


Next t
xt += 10
EndFunction





kevin

#1
  Threading is not needed here.  Your example shows both 'threads' rendering in a none synchronous fashion, apart from crashing, all the user you would see is a flashing mess.   This is because the TypeWriter thread doesn't know when it's safe to draw to the current surface.  So both threads can and will be rendering over each other.  

PlayBASIC Code: [Select]
   ; Set a cap on the number of syncs that can occur inside a second
setfps 60

; Variable to hold the current millisecond timing point
Global CurrentTime

; user variables for the player
x = 100
y = 100


; message structure
Type tTypeWriterMessage
Status ;
Message$ ; The Message Text
StartTime ; when the message was created
Interval ; ticks between characters
X,Y ; position
Colour ; colour of text
EndType

; DIm a Typed variable
Dim M as tTypeWriterMessage

; init our message structure/variable
M= NewMessage(200,200,"This is a big long message")


do
; clear the screen
cls 25

; Set the ticker at this position so everything is relative to this point in time
CurrentTime=timer()

; draw message
ShowMessage(m)

; draw user controlled character
circle x,y, 10, 0

if RightKey() = 1
x+=1
endif

sync
loop


Function NewMessage(X,Y,Message$,Colour=$ffffff,Speed=100)
M = new tTypeWriterMessage
M.STatus=true
M.Message$=Message$
M.X=X
M.Y=Y
M.INterval =Speed
M.STartTime=Timer()
M.Colour=Colour
EndFunction M as tTypeWriterMessage


Function ShowMessage(M as tTypeWriterMessage )
if M.Status
StartTime=M.StartTime
TimePast =CurrentTime-StartTime
if TimePast=>0
oldink=getink()
Size =Len(M.Message$)
ChrPos=TimePast/M.Interval
ink M.Colour
Text m.x,m.y,Left$(M.Message$,ChrPos)
ink oldink
endif
endif
EndFunction




ATLUS


kevin


The windows API has features to call a function on a separate threads.  So using PlayBASIC2DLL you could potentially set up worker bee threads to push time consuming tasks into the background