Need Help creating thread from a simple program

Just starting out? Need help? Post your questions and find answers here.
JagV12
New User
New User
Posts: 9
Joined: Sat Jun 22, 2019 11:28 am

Need Help creating thread from a simple program

Post by JagV12 »

Hi there,

Based on this topic on the french forum, itself based on the Help example code for "OpenWindowedScreen()", I wrote this :

Code: Select all

; Program Name : Sprite V4.pb
EnableExplicit
UsePNGImageDecoder()

Enumeration Windows
  #MainWindow
EndEnumeration

Enumeration Sprite
  #BallSprite
EndEnumeration

#BackgroundColor = #Black
#FullyOpaque = 255
#MainWindowWidth  = 220
#MainWindowHeight = 160
#SpriteWidth  = 20
#SpriteHeight = 20
#SpriteSpeed  =  4

Define iBallSprite.i, fHorizSpritePos.f, fVertSpritePos.f, fAlpha.f, fHorizSpeed.f, fVertSpeed.f
InitSprite()

If OpenWindow(#MainWindow, #PB_Ignore, #PB_Ignore, #MainWindowWidth, #MainWindowHeight, "A screen in a window...", #PB_Window_SystemMenu)
  If OpenWindowedScreen(WindowID(#MainWindow), 0, 0, #MainWindowWidth, #MainWindowHeight)
    iBallSprite = CatchSprite(#PB_Any, ?soccer_ball_png_start ,#PB_Sprite_AlphaBlending )
    fHorizSpritePos = #MainWindowWidth / 2
    fVertSpritePos = #MainWindowHeight / 2
    fAlpha = Radian(Random(90))
    Debug "fAlpha = " + StrF(fAlpha)
    fHorizSpeed = #SpriteSpeed * Cos(fAlpha)
    fVertSpeed  = #SpriteSpeed * Sin(fAlpha)
    Repeat
      FlipBuffers()
      ClearScreen(#BackgroundColor )
      DisplayTransparentSprite(iBallSprite, fHorizSpritePos, fVertSpritePos, #FullyOpaque)
      fHorizSpritePos + fHorizSpeed
      If fHorizSpritePos < 0 Or fHorizSpritePos > #MainWindowWidth - #SpriteWidth : fHorizSpeed = -fHorizSpeed : EndIf
      fVertSpritePos + fVertSpeed
      If fVertSpritePos < 0 Or fVertSpritePos > #MainWindowHeight - #SpriteHeight : fVertSpeed = -fVertSpeed : EndIf
      Delay(1)
    Until WindowEvent() = #PB_Event_CloseWindow
  EndIf
EndIf

DataSection
soccer_ball_png_start:
  ; size : 901 bytes
  Data.q $0A1A0A0D474E5089,$524448490D000000,$1000000010000000,$FFF31F0000000608,$5948700900000061
  Data.q $0B0000130B000073,$0000189C9A000113,$00004D5248632000,$000083800000257A,$0000E9800000FFF9
  Data.q $000060EA00003075,$5F926F170000983A,$44490B03000046C5,$285F9374DA785441,$E756AEBFC6007BF3
  Data.q $F9417217339D3A92,$DC172D65726AE593,$B2FC2E475699929C,$9B0B5E142C34CB59,$112D2DF8768A32D8
  Data.q $29C5DA36692D6CD1,$708AC5FE4FE58A14,$53B9CFA96F42ED31,$CFA7A9E2E78B9CDE,$40AAFC4008F3D3C5
  Data.q $ADADAD3F7F7F4020,$ECC6565650D4D4D4,$B9B317ECBBF7ACEC,$84026BDBDBDE19B9,$B0B0B10A1423E7CF
  Data.q $44489110066CD980,$AFAC05BE7A040817,$6883E3E3E2005FAF,$64C93F9FCFC00A34,$C3333330BCBCBC32
  Data.q $A76FE850A13DFDFD,$7B5B5B5802FA19D3,$93939325E5E5C400,$7864C990036F6F6C,$999996F6F6E27A7A
  Data.q $D1996590FAFAFA19,$9FBFFF7F7F50D1D1,$F4CB2CB71D9D9D80,$AFAFA858B161F5F5,$32452A5494F4F4E7
  Data.q $D092490565656499,$183060545454F5EB,$0FE4E4E4FDE92490,$BB76E1018F060C11,$AD5A874E9D092491
  Data.q $EEC4747470891226,$3ABABA83870E2EEE,$BAA1292928AAAAAA,$A7A7A665E5E59ABA,$4C37B7B79FBF7E11
  Data.q $854A9516AD5A1326,$934E9D20ECFF6EDD,$52A545ADADA8A54A,$EFC22A2A292A54A1,$3C78F089D9D9D3F7
  Data.q $C18EDEDED0DCDCDC,$C0F0F0F356AD4060,$DDDCAFAFAF3BFBFB,$461E1E19264C91DD,$E5E5E51656564108
  Data.q $4E105A5A5A5EBD78,$AFAFAE39B9B993A7,$B1B23E3E3CD3A749,$57575733F3F3C1B1,$D9B1C78F121E1E1C
  Data.q $702448931F1F1C6C,$1A3462F6F6F41C38,$C5CAD5AB19B36611,$9F9F9CA7A7A705C5,$7F633B3B3878F1E3
  Data.q $C5EBD78A34689F7F,$EFDF85B5B5B1F3E7,$CD82C58B02626267,$68D1A18D8D8C4366,$68C3870A9FE3C788
  Data.q $6C6C699B9B9A1A34,$6590C58B13D7AF44,$1A172E5C11919199,$9326488AD5AB068D,$3A74E85E161615BF
  Data.q $6AC45C5C58969696,$3171714A5A5A52B5,$CB23676764F9F9F9,$7A01818183870E32,$1C1CCDCDCDC87A7A
  Data.q $C71A34610029FC1C,$13264C366CD81084,$54A1E5E5E4858585,$56810841C9C9C92A,$443434323468C5AB
  Data.q $FC93535341E56565,$CF9F1FE18306CA65,$93A74E2AEAEAD7F7,$850A050A14028282,$B130E1C206868682
  Data.q $75756371F1F18C58,$BDEDDBB267B7CEF5,$F3376EDC3F1D1D1D,$060C11B5B5B4F3F3,$2E5C418304E3C789
  Data.q $BEFFD354D4D4DB57,$70706F2244891011,$ABA66E6E68924910,$376EDC6EEEEE8BAB,$6FFF9AFFBF878787
  Data.q $3285D19FA3885900,$4E45490000000074
  Data.b $44,$AE,$42,$60,$82
soccer_ball_png_end:
EndDataSection
I now want to put the drawing part of the code in a thread so that the ball doesn't stop when I move the window :

Code: Select all

; Program Name : Sprite V5.pb
EnableExplicit
UsePNGImageDecoder()

Enumeration Windows
  #MainWindow
EndEnumeration

; Enumeration Sprite
;   #BallSprite
; EndEnumeration

#BackgroundColor = #Black
#FullyOpaque = 255
#MainWindowWidth  = 220
#MainWindowHeight = 160
#SpriteWidth  = 20
#SpriteHeight = 20
#SpriteSpeed  =  4

InitSprite()
Global bExitThread.b

Procedure DisplayThread(*WindowID)
  Protected iBallSprite.i, fHorizSpritePos.f, fVertSpritePos.f, fAlpha.f, fHorizSpeed.f, fVertSpeed.f
  If OpenWindowedScreen(*WindowID, 0, 0, #MainWindowWidth, #MainWindowHeight)
    iBallSprite = CatchSprite(#PB_Any, ?soccer_ball_png_start ,#PB_Sprite_AlphaBlending )
    fHorizSpritePos = #MainWindowWidth / 2
    fVertSpritePos = #MainWindowHeight / 2
    fAlpha = Radian(Random(90))
    Debug "fAlpha = " + StrF(fAlpha)
    fHorizSpeed = #SpriteSpeed * Cos(fAlpha)
    fVertSpeed  = #SpriteSpeed * Sin(fAlpha)
    Repeat
      FlipBuffers()
      ClearScreen(#BackgroundColor)
      DisplayTransparentSprite(iBallSprite, fHorizSpritePos, fVertSpritePos, #FullyOpaque)
      fHorizSpritePos + fHorizSpeed
      If fHorizSpritePos < 0 Or fHorizSpritePos > #MainWindowWidth - #SpriteWidth : fHorizSpeed = -fHorizSpeed : EndIf
      fVertSpritePos + fVertSpeed
      If fVertSpritePos < 0 Or fVertSpritePos > #MainWindowHeight - #SpriteHeight : fVertSpeed = -fVertSpeed : EndIf
      Delay(1)
    Until bExitThread
    Debug "Exiting Thread"
  EndIf
EndProcedure

Define ThreadID, Event
If OpenWindow(#MainWindow, #PB_Ignore, #PB_Ignore, #MainWindowWidth, #MainWindowHeight, "A screen in a window...", #PB_Window_SystemMenu)
  bExitThread = #False
  ThreadID = CreateThread(@DisplayThread(), WindowID(#MainWindow))
  Repeat
    Event = WaitWindowEvent()
    Debug "Event = " + Event
  Until Event = #PB_Event_CloseWindow
  bExitThread = #True
  Debug "Exiting Event Loop"
  Repeat
    WindowEvent()
    Delay(10)
  Until Not(IsThread(ThreadID))
  CloseWindow(#MainWindow)
EndIf

DataSection
soccer_ball_png_start:
  ; size : 901 bytes
  Data.q $0A1A0A0D474E5089,$524448490D000000,$1000000010000000,$FFF31F0000000608,$5948700900000061
  Data.q $0B0000130B000073,$0000189C9A000113,$00004D5248632000,$000083800000257A,$0000E9800000FFF9
  Data.q $000060EA00003075,$5F926F170000983A,$44490B03000046C5,$285F9374DA785441,$E756AEBFC6007BF3
  Data.q $F9417217339D3A92,$DC172D65726AE593,$B2FC2E475699929C,$9B0B5E142C34CB59,$112D2DF8768A32D8
  Data.q $29C5DA36692D6CD1,$708AC5FE4FE58A14,$53B9CFA96F42ED31,$CFA7A9E2E78B9CDE,$40AAFC4008F3D3C5
  Data.q $ADADAD3F7F7F4020,$ECC6565650D4D4D4,$B9B317ECBBF7ACEC,$84026BDBDBDE19B9,$B0B0B10A1423E7CF
  Data.q $44489110066CD980,$AFAC05BE7A040817,$6883E3E3E2005FAF,$64C93F9FCFC00A34,$C3333330BCBCBC32
  Data.q $A76FE850A13DFDFD,$7B5B5B5802FA19D3,$93939325E5E5C400,$7864C990036F6F6C,$999996F6F6E27A7A
  Data.q $D1996590FAFAFA19,$9FBFFF7F7F50D1D1,$F4CB2CB71D9D9D80,$AFAFA858B161F5F5,$32452A5494F4F4E7
  Data.q $D092490565656499,$183060545454F5EB,$0FE4E4E4FDE92490,$BB76E1018F060C11,$AD5A874E9D092491
  Data.q $EEC4747470891226,$3ABABA83870E2EEE,$BAA1292928AAAAAA,$A7A7A665E5E59ABA,$4C37B7B79FBF7E11
  Data.q $854A9516AD5A1326,$934E9D20ECFF6EDD,$52A545ADADA8A54A,$EFC22A2A292A54A1,$3C78F089D9D9D3F7
  Data.q $C18EDEDED0DCDCDC,$C0F0F0F356AD4060,$DDDCAFAFAF3BFBFB,$461E1E19264C91DD,$E5E5E51656564108
  Data.q $4E105A5A5A5EBD78,$AFAFAE39B9B993A7,$B1B23E3E3CD3A749,$57575733F3F3C1B1,$D9B1C78F121E1E1C
  Data.q $702448931F1F1C6C,$1A3462F6F6F41C38,$C5CAD5AB19B36611,$9F9F9CA7A7A705C5,$7F633B3B3878F1E3
  Data.q $C5EBD78A34689F7F,$EFDF85B5B5B1F3E7,$CD82C58B02626267,$68D1A18D8D8C4366,$68C3870A9FE3C788
  Data.q $6C6C699B9B9A1A34,$6590C58B13D7AF44,$1A172E5C11919199,$9326488AD5AB068D,$3A74E85E161615BF
  Data.q $6AC45C5C58969696,$3171714A5A5A52B5,$CB23676764F9F9F9,$7A01818183870E32,$1C1CCDCDCDC87A7A
  Data.q $C71A34610029FC1C,$13264C366CD81084,$54A1E5E5E4858585,$56810841C9C9C92A,$443434323468C5AB
  Data.q $FC93535341E56565,$CF9F1FE18306CA65,$93A74E2AEAEAD7F7,$850A050A14028282,$B130E1C206868682
  Data.q $75756371F1F18C58,$BDEDDBB267B7CEF5,$F3376EDC3F1D1D1D,$060C11B5B5B4F3F3,$2E5C418304E3C789
  Data.q $BEFFD354D4D4DB57,$70706F2244891011,$ABA66E6E68924910,$376EDC6EEEEE8BAB,$6FFF9AFFBF878787
  Data.q $3285D19FA3885900,$4E45490000000074
  Data.b $44,$AE,$42,$60,$82
soccer_ball_png_end:
EndDataSection
The "CloseWindow" works well but the program freezes as soon as I try to move the window.
Any suggestion ? (comments about my code welcome too...)
Marc56us
Addict
Addict
Posts: 1479
Joined: Sat Feb 08, 2014 3:26 pm

Re: Need Help creating thread from a simple program

Post by Marc56us »

I now want to put the drawing part of the code in a thread so that the ball doesn't stop when I move the window :
The "CloseWindow" works well but the program freezes as soon as I try to move the window.
PureBasic - Sprite

Windows: DirectX 9 is used for screen creation which your programs to use hardware acceleration if available

PureBasic - Thread

Note: Don't use DirectX inside threads (MS Windows limitation)! If you need to display graphics in threads use Images and 2DDrawing instead.

:wink:
JagV12
New User
New User
Posts: 9
Joined: Sat Jun 22, 2019 11:28 am

Re: Need Help creating thread from a simple program

Post by JagV12 »

This means I can't use Sprites in Threads, right ? (Tell me if not)

So here is the new version, but I get flicker. How can I get rid of it ? Are OpenWindowedScreen() and FlipBuffers() relevant in this "Images and 2DDrawing" case ? Or is it possible to Sync my drawing thread ?

Code: Select all

EnableExplicit
UsePNGImageDecoder()

Enumeration Windows
  #MainWindow
EndEnumeration

Enumeration Gadgets
  #MainImage
EndEnumeration

Enumeration Image
  #ScreenImage
  #BallImage
EndEnumeration

#BackgroundColor = #Black
#MainWindowWidth  = 220
#MainWindowHeight = 160
#BallWidth  = 20
#BallHeight = 20
#BallSpeed  =  1

Global bExitThread.b

Procedure DisplayThread(*WindowID)
  Protected fHorizBallPos.f, fVertBallPos.f, fAlpha.f, fHorizSpeed.f, fVertSpeed.f
  
  CatchImage(#BallImage, ?soccer_ball_png_start)
  fHorizBallPos = #MainWindowWidth / 2
  fVertBallPos = #MainWindowHeight / 2
  fAlpha = Radian(Random(90))
  Debug "fAlpha = " + StrF(fAlpha)
  fHorizSpeed = #BallSpeed * Cos(fAlpha)
  fVertSpeed  = #BallSpeed * Sin(fAlpha)
  Repeat
    Delay(3)
    StartDrawing(ImageOutput(#ScreenImage))
    Box(fHorizBallPos, fVertBallPos, #BallWidth, #BallHeight, #BackgroundColor)
    fHorizBallPos + fHorizSpeed
    If fHorizBallPos < 0 Or fHorizBallPos > #MainWindowWidth - #BallWidth : fHorizSpeed = -fHorizSpeed : EndIf
    fVertBallPos + fVertSpeed
    If fVertBallPos < 0 Or fVertBallPos > #MainWindowHeight - #BallHeight : fVertSpeed = -fVertSpeed : EndIf
    DrawingMode(#PB_2DDrawing_AlphaBlend)
    DrawImage(ImageID(#BallImage), fHorizBallPos, fVertBallPos)
    StopDrawing()
    SetGadgetState(#MainImage, ImageID(#ScreenImage))
  Until bExitThread
  Debug "Exiting Thread"
EndProcedure

Define ThreadID, Event
If OpenWindow(#MainWindow, #PB_Ignore, #PB_Ignore, #MainWindowWidth, #MainWindowHeight, "A screen in a window...", #PB_Window_SystemMenu)
  bExitThread = #False
  If CreateImage(#ScreenImage, #MainWindowWidth, #MainWindowHeight, 32)
    ImageGadget(#MainImage, 0, 0, 0, 0, ImageID(#ScreenImage))
    ThreadID = CreateThread(@DisplayThread(), WindowID(#MainWindow))
    Repeat
      Event = WaitWindowEvent()
;       Debug "Event = " + Event
    Until Event = #PB_Event_CloseWindow
    bExitThread = #True
    Debug "Exiting Event Loop"
    Repeat
      WindowEvent()
      Delay(10)
    Until Not(IsThread(ThreadID))
  EndIf
  CloseWindow(#MainWindow)
EndIf

DataSection
soccer_ball_png_start:
  ; size : 901 bytes
  Data.q $0A1A0A0D474E5089,$524448490D000000,$1000000010000000,$FFF31F0000000608,$5948700900000061
  Data.q $0B0000130B000073,$0000189C9A000113,$00004D5248632000,$000083800000257A,$0000E9800000FFF9
  Data.q $000060EA00003075,$5F926F170000983A,$44490B03000046C5,$285F9374DA785441,$E756AEBFC6007BF3
  Data.q $F9417217339D3A92,$DC172D65726AE593,$B2FC2E475699929C,$9B0B5E142C34CB59,$112D2DF8768A32D8
  Data.q $29C5DA36692D6CD1,$708AC5FE4FE58A14,$53B9CFA96F42ED31,$CFA7A9E2E78B9CDE,$40AAFC4008F3D3C5
  Data.q $ADADAD3F7F7F4020,$ECC6565650D4D4D4,$B9B317ECBBF7ACEC,$84026BDBDBDE19B9,$B0B0B10A1423E7CF
  Data.q $44489110066CD980,$AFAC05BE7A040817,$6883E3E3E2005FAF,$64C93F9FCFC00A34,$C3333330BCBCBC32
  Data.q $A76FE850A13DFDFD,$7B5B5B5802FA19D3,$93939325E5E5C400,$7864C990036F6F6C,$999996F6F6E27A7A
  Data.q $D1996590FAFAFA19,$9FBFFF7F7F50D1D1,$F4CB2CB71D9D9D80,$AFAFA858B161F5F5,$32452A5494F4F4E7
  Data.q $D092490565656499,$183060545454F5EB,$0FE4E4E4FDE92490,$BB76E1018F060C11,$AD5A874E9D092491
  Data.q $EEC4747470891226,$3ABABA83870E2EEE,$BAA1292928AAAAAA,$A7A7A665E5E59ABA,$4C37B7B79FBF7E11
  Data.q $854A9516AD5A1326,$934E9D20ECFF6EDD,$52A545ADADA8A54A,$EFC22A2A292A54A1,$3C78F089D9D9D3F7
  Data.q $C18EDEDED0DCDCDC,$C0F0F0F356AD4060,$DDDCAFAFAF3BFBFB,$461E1E19264C91DD,$E5E5E51656564108
  Data.q $4E105A5A5A5EBD78,$AFAFAE39B9B993A7,$B1B23E3E3CD3A749,$57575733F3F3C1B1,$D9B1C78F121E1E1C
  Data.q $702448931F1F1C6C,$1A3462F6F6F41C38,$C5CAD5AB19B36611,$9F9F9CA7A7A705C5,$7F633B3B3878F1E3
  Data.q $C5EBD78A34689F7F,$EFDF85B5B5B1F3E7,$CD82C58B02626267,$68D1A18D8D8C4366,$68C3870A9FE3C788
  Data.q $6C6C699B9B9A1A34,$6590C58B13D7AF44,$1A172E5C11919199,$9326488AD5AB068D,$3A74E85E161615BF
  Data.q $6AC45C5C58969696,$3171714A5A5A52B5,$CB23676764F9F9F9,$7A01818183870E32,$1C1CCDCDCDC87A7A
  Data.q $C71A34610029FC1C,$13264C366CD81084,$54A1E5E5E4858585,$56810841C9C9C92A,$443434323468C5AB
  Data.q $FC93535341E56565,$CF9F1FE18306CA65,$93A74E2AEAEAD7F7,$850A050A14028282,$B130E1C206868682
  Data.q $75756371F1F18C58,$BDEDDBB267B7CEF5,$F3376EDC3F1D1D1D,$060C11B5B5B4F3F3,$2E5C418304E3C789
  Data.q $BEFFD354D4D4DB57,$70706F2244891011,$ABA66E6E68924910,$376EDC6EEEEE8BAB,$6FFF9AFFBF878787
  Data.q $3285D19FA3885900,$4E45490000000074
  Data.b $44,$AE,$42,$60,$82
soccer_ball_png_end:
EndDataSection
By the way, WaitThread(ThreadID) could replace

Code: Select all

    Repeat
      WindowEvent()
      Delay(10)
    Until Not(IsThread(ThreadID))
in the Sprite version but not in the "Images and 2DDrawing" version (=> Crashed). Why :?:
Post Reply