Help with slow code, only in a thread, how to speed up

Advanced game related topics
Hydrate
Enthusiast
Enthusiast
Posts: 436
Joined: Mon May 16, 2005 9:37 pm
Contact:

Help with slow code, only in a thread, how to speed up

Post by Hydrate »

Well, i am creating a small game, but i want it to work in a thread, thing is its very slow, i want it to run fast, im not too sure what im doing wrong, could someone show me what i need to do to make it work at the right speed?

Code: Select all


Dim diff_Kollision.w(3)
Global BallX.w : BallX = 10
Global BallY.w : BallY = 10
Global BallRateY.w : BallRateY = 2  ; Ball-Geschwindigkeit in Y-Richtung
Global BallRateX.w : BallRateX = 3  ; Ball-Geschwindigkeit in X-Richtung
Global BallDirY.w : BallDirY = -1   ; Y-Richtung
Global BallDirX.w : BallDirX = 1    ; X-Richtung
Global BallRadius.w : BallRadius = 10 ; Ball-Radius   ist in Wirklichkeit ein Rechteck ;)
Global BlockXL.w  ; Linke X-Kante des Blocks
Global BlockXR.w  ; Rechte X-Kante des Blocks
Global BlockYT.w  ; Obere Y-Kante des Blocks
Global BlockYB.w  ; Untere Y-Kante des Blocks

Procedure Ball_Block_Collision()
  Shared BlockXL , BlockXR , BlockYT , BlockYB
  Shared BallX , BallY , BallDirX , BallDirY
  If BallX >= BlockXL-BallRadius And BallX <= BlockXR+BallRadius And BallY >= BlockYT-BallRadius And BallY <= BlockYB+BallRadius
    diff_XL=BallX-BlockXL : diff_XR=BlockXR-BallX : diff_YT=BallY-BlockYT : diff_YB=BlockYB-BallY
    diff_Kollision(0)=diff_XL : diff_Kollision(1)=diff_XR : diff_Kollision(2)=diff_YT : diff_Kollision(3)=diff_YB
    SortArray(diff_Kollision(),0)
    If diff_XL = diff_Kollision(0)
      BallDirX = -1
    ElseIf diff_XR = diff_Kollision(0)
      BallDirX = 1
    ElseIf diff_YT = diff_Kollision(0)
      BallDirY = -1
    ElseIf diff_YB = diff_Kollision(0)
      BallDirY = 1
    EndIf
  EndIf
EndProcedure

Procedure GamePlay(Val.l)
BallX = Int(BallX + BallRateX * BallDirX)
BallY = Int(BallY + BallRateY * BallDirY)
If BallY <= 16
  BallDirY = 1
EndIf
If BallY >= 224
  BallDirY = -1
EndIf
If BallX <= 20
  BallDirX = 1
EndIf
If BallX >= 294
  BallDirX = -1
EndIf
If KeyboardPushed(#PB_Key_Down)
BlockYT=BlockYT+Val
BlockYB = BlockYT+56
ElseIf KeyboardPushed(#PB_Key_Up)
BlockYT=BlockYT-Val
BlockYB = BlockYT+56
EndIf
Ball_Block_Collision()
ClearScreen(100,100,100)
DisplaySprite(0,0,0)
DisplayTransparentSprite(1,BallX-BallRadius,BallY-BallRadius)
DisplaySprite(2,BlockXL,BlockYT)
FlipBuffers()
EndProcedure



Val.l=4
If OpenWindow(0,5,5,330,250,#PB_Window_SystemMenu,"Breakout")
InitSprite()
InitKeyboard()
OpenWindowedScreen(WindowID(0),5,5,320,240,0,0,0)
SetFrameRate(100)

UsePNGImageDecoder()
LoadSprite(0,"Background.PNG")
LoadSprite(1,"Ball.PNG")
TransparentSpriteColor(1,255,0,255)
LoadSprite(2,"Bar.PNG")

BlockXL = 272
BlockXR = BlockXL+12
BlockYT = 94
BlockYB = BlockYT+56
Event=WaitWindowEvent()
Repeat
Select Event
Case #PB_Event_CloseWindow
quit=1

EndSelect
ExamineKeyboard()
If KeyboardPushed(1)
quit=1
EndIf

CreateThread(@GamePlay(),4)

Until quit=1
CloseScreen()
EndIf
Thanks if you can help.
Fred
Administrator
Administrator
Posts: 18384
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

Did you try with debugger off ?
Hydrate
Enthusiast
Enthusiast
Posts: 436
Joined: Mon May 16, 2005 9:37 pm
Contact:

Post by Hydrate »

Fred wrote:Did you try with debugger off ?
Yes, the game still runs very jumpy and its almost impossible to use...
coma
Enthusiast
Enthusiast
Posts: 164
Joined: Fri Aug 15, 2003 3:46 am
Location: Canada

Post by coma »

hi hydrate.

I don't understand why you create a thread each time. why not call the procedure in the loop with GamePlay(4) ?
If you really want to use a thread, call it before the main loop, only once, and make a infinite loop in GamePlay().

another thing, Use Event=WindowEvent() instead of Event=WaitWindowEvent(), and put it inside the loop, after the repeat.
remi_meier
Enthusiast
Enthusiast
Posts: 468
Joined: Sat Dec 20, 2003 6:19 pm
Location: Switzerland

Post by remi_meier »

I think at least you have to add a WaitThread() before CreateThread().
AFAIK this could cause problems!
Athlon64 3700+, 1024MB Ram, Radeon X1600
User avatar
utopiomania
Addict
Addict
Posts: 1655
Joined: Tue May 10, 2005 10:00 pm
Location: Norway

Post by utopiomania »

Hi, I tried to rewrite your code a bit, and this ok I guess, especially if you move the ball at steps of 2 instead of one.
Didn't have your sprites though, so I tried with three 64x64 pixel PNG's instead.

Code: Select all

Repeat 
  Event=WindowEvent() 
  Select Event 
    Case #PB_Event_CloseWindow 
      Quit = 1 
    EndSelect 
  ExamineKeyboard() 
  If KeyboardPushed(1) 
    Quit = 1 
  EndIf 
  GamePlay(4) 
Until Quit  
Hydrate
Enthusiast
Enthusiast
Posts: 436
Joined: Mon May 16, 2005 9:37 pm
Contact:

Post by Hydrate »

utopiomania wrote:Hi, I tried to rewrite your code a bit, and this ok I guess, especially if you move the ball at steps of 2 instead of one.
Didn't have your sprites though, so I tried with three 64x64 pixel PNG's instead.

Code: Select all

Repeat 
  Event=WindowEvent() 
  Select Event 
    Case #PB_Event_CloseWindow 
      Quit = 1 
    EndSelect 
  ExamineKeyboard() 
  If KeyboardPushed(1) 
    Quit = 1 
  EndIf 
  GamePlay(4) 
Until Quit  
Thank you for the help, i have got it to run at full speed now, by changing waitwindowevent for windowevent, thanks for the help.
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6172
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB - upgrade incoming...)
( The path to enlightenment and the PureBasic Survival Guide right here... )
Post Reply