Page 1 of 1

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

Posted: Tue Jan 03, 2006 2:02 pm
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.

Posted: Tue Jan 03, 2006 2:23 pm
by Fred
Did you try with debugger off ?

Posted: Tue Jan 03, 2006 2:40 pm
by Hydrate
Fred wrote:Did you try with debugger off ?
Yes, the game still runs very jumpy and its almost impossible to use...

Posted: Tue Jan 03, 2006 3:39 pm
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.

Posted: Tue Jan 03, 2006 3:57 pm
by remi_meier
I think at least you have to add a WaitThread() before CreateThread().
AFAIK this could cause problems!

Posted: Tue Jan 03, 2006 6:58 pm
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  

Posted: Tue Jan 03, 2006 8:24 pm
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.

Posted: Tue Jan 03, 2006 10:05 pm
by blueznl