mouse lag
Posted: Mon Jun 20, 2022 6:57 pm
both "asm" and "c backend" has the lag.
beta 6 and before is ok.
beta 7,8 I do not know.
beta 9,10 has lag.
in line 38; if replace "15" with a lower digit , the lag disappear.
the #speed2 constant is used in line 187.
I felt I should tell.
// Moved from "Bugs - C backend" to "Coding Questions" (Kiffi)
beta 6 and before is ok.
beta 7,8 I do not know.
beta 9,10 has lag.
in line 38; if replace "15" with a lower digit , the lag disappear.
the #speed2 constant is used in line 187.
I felt I should tell.
Code: Select all
Procedure do_beep_sound()
InitSound()
Protected.i a,b
Restore sound_data
#samples=250
Define.i *Header = AllocateMemory(44+#samples*2)
For a=0 To 9 :Read.l b :PokeL(*Header+a*4 ,b):Next a :PokeL(*Header+a*4 ,#samples*2)
For a=0 To #samples-2 :b=(Bool(a%25<13)*2-1)*4000 :PokeW(*Header+44+a*2 ,b):Next a
DataSection
sound_data:
Data.l $46464952 ,$0 ,$45564157 ,$20746D66 ,$10 ,$10001 ,$2B11 ,$5622 ,$100002 ,$61746164
EndDataSection
For a=0 To 3 : CatchSound(a ,*Header): Next a
; SetSoundFrequency(0,7795 ):SoundVolume(0 ,40)
SetSoundFrequency(1,11025):SoundVolume(1 ,10)
SetSoundFrequency(2,22050):SoundVolume(2 ,10)
SetSoundFrequency(3,31183):SoundVolume(3 ,10)
; 2756 ; 3897; 5512; 7795; 11025; 15592; 22050; 31183
EndProcedure
EnableExplicit
do_beep_sound()
PlaySound(3)
InitSprite():InitKeyboard(): InitMouse()
#scr_x = 900 ;* #scalex
#scr_y = 720 ; * #scaley
OpenWindow(0 , 0 , 0 , #scr_x , #scr_y , "B O U N C E" , #PB_Window_SystemMenu | #PB_Window_ScreenCentered )
OpenWindowedScreen(WindowID(0) , 0 , 0 , #scr_x , #scr_y, 0 , 0 , 0 , 0)
#deg_0 = 0.0
#deg_45 = #PI*0.25
#deg_90 = #PI*0.5 ; 1.5707963267948966
#deg_180 = #PI*1.0 ; 3.1415926535897931
#deg_270 = #PI*1.5 ; 4.7123889803846897
#deg_360 = #PI*2.0
#speed=0.0001
#speed2=(1<<15)-1
; SetFrameRate(50)
#ant=2+70
#size=20
RandomSeed(3)
Global.d Dim x(#ant) , Dim y(#ant)
Global.i Dim r(#ant) , Dim col(#ant)
Global.i butt_font = LoadFont(2 , "verdana" , 14,#PB_Font_Bold )
Global.i a , b , ant , time
#ball=1 : #size_ball=20
#mouse=2: #size_mouse=100
#txt=3 : #size_txt=38
r(#ball) =#size_ball :col(#ball) =#Yellow
x(#ball) =#scr_x/2 : y(#ball) =#scr_y/2+#size_txt
r(#mouse)=#size_mouse:col(#mouse)=#Cyan
x(#mouse)=#scr_x/4 : y(#mouse)=#scr_y/4
r(#txt) =#size_txt
#x1=0 : #x2=#scr_x-#size_ball*2
#y1=0 : #y2=#scr_y-#size_ball*2
Procedure resprite(a,txt$)
CreateSprite(a,r(a)*2+1,r(a)*2+1,#PB_Sprite_PixelCollision)
StartDrawing(SpriteOutput(a))
DrawingFont(butt_font)
Circle(r(a),r(a),r(a),col(a))
DrawText(r(a)-TextWidth(txt$)/2,r(a)-11,txt$,0,col(a))
StopDrawing()
EndProcedure
Procedure Randompos()
Global.i check=0
Protected c , txt$
x(#txt)=#size_txt +Random(#scr_x-#size_txt*2) : y(#txt)=#size_txt +Random(#scr_y-#size_txt*2 )
col(#txt) =#Yellow
ant=#ant
For a=1 To ant
If a>3
r(a)=Random(#size+20,#size) : col(a)=$CC7755
x(a)=Random(#scr_x) : y(a)=Random(#scr_y)
EndIf
time =ElapsedMilliseconds() : c=0
For b=3 To a-1
; a = Sqr((x1-x2)^2 + (y1-y2)^2)
If Sqr( (x(a)-x(b)) * (x(a)-x(b) ) + (y(a)-y(b)) * (y(a)-y(b)) ) < r(a)+r(b)+r(1)*2.3
r(a)=Random(#size+10,#size) : col(a)=#Green
x(a)=Random(#scr_x) : y(a)=Random(#scr_y)
b=2: c+1
EndIf
If ElapsedMilliseconds() > time+20 :col(a)=#Red: ant=a-1 :Break 2 : EndIf
; If ElapsedMilliseconds() > time+20 :col(a)=#Red: r(a)=70: ant=a :Break : EndIf
Next b
; PlaySound(1)
txt$=Str(a) ; r(a) , c
resprite(a,txt$)
Next
For a=3 To ant
x(a)-r(a)
y(a)-r(a)
Next a
CopySprite(#txt,0)
EndProcedure
Procedure.d mod2(Number.d)
Number = Mod(Number,2*#PI)
If Number<0 : Number + 2*#PI: Debug "<0" :EndIf
ProcedureReturn Number
EndProcedure
Global.d ang_ball , vx , vy
Procedure newangle(ang_surface.d)
Protected.d diff
ang_ball - #PI
diff = ang_surface-ang_ball
ang_ball = ang_surface + diff
vy=Sin(ang_ball)*#speed
vx=Cos(ang_ball)*#speed
EndProcedure
Randompos()
Define m=2 ,c1,c2,c3,sound,sound2,sound3 , delay
Define.d x1,y1
ang_ball=#PI+Radian(315)
newangle(ang_ball+#PI)
;- loop
Repeat
time = ElapsedMilliseconds()+1000
Repeat
sound=0
For a=m To ant
If SpriteCollision(#ball, x(#ball), y(#ball) ,a, x(a), y(a))
; If SpritePixelCollision(#ball, x(#ball), y(#ball) ,a, x(a), y(a))
; For delay=0 To 30
; x1=x(#ball)+r(#ball)-x(a) -r(a)
; y1=y(#ball)+r(#ball)-y(a) -r(a)
; If Sqr(x1*x1 + y1*y1) < r(#ball)+r(a): EndIf
; Next delay
x1=x(#ball)+r(#ball)-x(a) -r(a)
y1=y(#ball)+r(#ball)-y(a) -r(a)
If Sqr(x1*x1 + y1*y1) < r(#ball)+r(a)
If col(a)<>#White And a>2
sound=3
check+1
col(a)=#White
resprite(a,Str(a))
Else
sound=2
EndIf
Break
EndIf
EndIf
Next a
If sound
If sound2=1
; ang = ATan2(x(#ball)+r(#ball) - x(a)-r(a) , y(#ball)+r(#ball) - y(a)-r(a));+#PI
newangle(ATan2(x(#ball)+r(#ball) - x(a)-r(a) , y(#ball)+r(#ball) - y(a)-r(a)))
sound3=sound
Else
ang_ball=ATan2(x(#ball)+r(#ball) - x(a)-r(a) , y(#ball)+r(#ball) - y(a)-r(a))
vy=Sin(ang_ball)*#speed
vx=Cos(ang_ball)*#speed
sound3=1
EndIf
sound2=0
Else
sound2=1
EndIf
If x(#ball) < #x1 :x(#ball) = #x1 : newangle(#deg_0):sound3=2: EndIf
If y(#ball) < #y1 :y(#ball) = #y1 : newangle(#deg_90):sound3=2: EndIf
If x(#ball) > #x2 :x(#ball) = #x2 : newangle(#deg_180):sound3=2: EndIf
If y(#ball) > #Y2 :y(#ball) = #Y2 : newangle(#deg_270):sound3=2: EndIf
x(#ball) + vx
Y(#ball) + vy
If c1&#speed2=0
If sound3 :PlaySound(sound3): sound3=0:EndIf
ExamineKeyboard()
If KeyboardPushed(#PB_Key_R):Randompos():ClearScreen(0): EndIf
If KeyboardReleased(#PB_Key_M):m=2+(m+1)&1:ReleaseMouse((m+0)&1):EndIf
If KeyboardPushed(#PB_Key_Escape):End: EndIf
If m=2
ExamineMouse()
x(#mouse)=MouseX()-r(#mouse)
y(#mouse)=MouseY()-r(#mouse)
DisplayTransparentSprite(#mouse,x(#mouse),y(#mouse))
EndIf
For a=3 To ant
DisplayTransparentSprite(a,x(a),y(a))
Next a
DisplayTransparentSprite(#ball,x(#ball),y(#ball))
WindowEvent()
FlipBuffers()
ClearScreen(0)
c3+1
EndIf
c2+1
c1+1
Until ElapsedMilliseconds() > time
StartDrawing(SpriteOutput(#txt))
DrawingFont(butt_font)
Circle(r(#txt),r(#txt),r(#txt),col(#txt))
DrawText(20,6,Str(ant),1,col(#txt))
DrawText(6,25,StrF(c2/1000000,3-Bool(c2>9999999)),1,col(#txt))
DrawText(14,45,Str(c3-1),1,col(#txt))
StopDrawing()
c2=0:c3=0
If check And check=ant-2:c1=0:Randompos():ClearScreen(0): EndIf
ForEver
End