Distortion when moving across the screen sprite (Raspberry 4B))

New to FreeBASIC? Post your questions here.
badidea
Posts: 2586
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: Distortion when moving across the screen sprite (Raspberry 4B))

Post by badidea »

PavelUT wrote:Only running these third party libraries is not very easy.
Getting the actual libraries on linux is relative easy. Via the distro package manager. I have no experience with Raspberry however.
PavelUT wrote:Can you advise any literature? For now, 2D graphics, sounds and keyboard and mouse controls are enough for me.
Not really. Last time I worked with SDL was with version 1.2 and only in combination with C, long time ago. There should be a lot of tutorials on the internet on SDL and C. For freebasic, not so much, but not a big problem if you know a bit of C. Also, SDL comes with sound (if I am not wrong). Something that is still missing in the freebasic installation.
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Distortion when moving across the screen sprite (Raspberry 4B))

Post by dodicat »

I tested cairo, not as smooth as opengl.
The reason I used settimer and freetimer on windows was to make sure a sleep 1 was 1 millisecond.
Normally it is about 15 milliseconds on windows.
Google chrome used to set this one millisecond automatically when online in the freebasic forum, but I note that this is not happening now.
Maybe Linux is actually one millisecond for sleep 1 anyway, I don't know.
In order to get about 120 fps here I must use settimer and freetimer now.
Can you get 120 fps on Linux by moving the slider up?

Code: Select all

 #ifdef __FB_WIN32__
declare function settimer       alias "timeBeginPeriod"(as Ulong=1) as long
declare function freetimer      alias "timeEndPeriod"  (as Ulong=1) as long
#endif

 #define map(a,b,x,c,d) ((d)-(c))*((x)-(a))/((b)-(a))+(c)
 #define inbox (mx>125) and (mx<475)' and (my>300) and (my<350)
 #define incircle(cx,cy,radius,x,y) (cx-x)*(cx-x) +(cy-y)*(cy-y)<= radius*radius
 
 #macro display
    fr=map(125,475,cx,10,120)
    screenlock
    cls
    draw string(20,20),"Actual Framerate     = " &fps
    draw string(20,40),"Requested Framerate  = " &fr
    draw string(20,60),"Sleeping time        = " &sleeptime
    draw string(100,280),"10"
    draw string(480,280),"120"
    draw string(250,280),"<--slider-->"
    angle+=.1
    drawline(600,200,.2*sin(angle)-pi/2,300,4)
    line(100,300)-(500,350),2,bf
    circle(cx,cy),25,5,,,,f
    screenunlock
     #ifdef __FB_WIN32__
    settimer
    #endif
    sleep sleeptime,1
     #ifdef __FB_WIN32__
    freetimer
    #endif
#endmacro

#macro mouse
Dim As Long x=mx,y=my,dx,dy
While mb = 1
    Display
    Getmouse mx,my,,mb
    If inbox Then
        If mx<>x Or my<>y  Then
            dx = mx - x
            dy = my - y
            x = mx
            y = my
            cx=x+dx
            if cx<125 then cx=125
            if cx>475 then cx=475
        End If
    End If
Wend
#endmacro

sub drawline(x as long,y as long,angle as single,length as long,col as ulong)
    var x2=x+length*cos(angle)
    var y2=y-length*sin(angle)
     line(x,y)-(x2,y2)
     circle(x2,y2),10,6,,,,f
end sub

Function Regulate(Byval MyFps As long,Byref fps As long) As long
    Static As Double timervalue,lastsleeptime,t3,frames
    frames+=1
    If (Timer-t3)>=1 Then t3=Timer:fps=frames:frames=0
    Var sleeptime=lastsleeptime+((1/myfps)-Timer+timervalue)*1000
    If sleeptime<1 Then sleeptime=1
    lastsleeptime=sleeptime
    timervalue=Timer
    Return sleeptime
End Function

screen 19
dim as integer mx,my,mb
dim as long cx=125,cy=325,fps,fr,sleeptime
dim as single angle,pi=4*atn(1)
do
    getmouse mx,my,,mb
    
    display
    
    if incircle(cx,cy,25,mx,my) and mb=1 then
        mouse
        end if
   
    sleeptime= regulate(fr,fps)
   
    loop until len(inkey) 
PavelUT
Posts: 78
Joined: Jun 14, 2021 14:42

Re: Distortion when moving across the screen sprite (Raspberry 4B))

Post by PavelUT »

dodicat wrote:I tested cairo, not as smooth as opengl.
The reason I used settimer and freetimer on windows was to make sure a sleep 1 was 1 millisecond.
Normally it is about 15 milliseconds on windows.
Google chrome used to set this one millisecond automatically when online in the freebasic forum, but I note that this is not happening now.
Maybe Linux is actually one millisecond for sleep 1 anyway, I don't know.
In order to get about 120 fps here I must use settimer and freetimer now.
Can you get 120 fps on Linux by moving the slider up?

Code: Select all

 #ifdef __FB_WIN32__
declare function settimer       alias "timeBeginPeriod"(as Ulong=1) as long
declare function freetimer      alias "timeEndPeriod"  (as Ulong=1) as long
#endif

 #define map(a,b,x,c,d) ((d)-(c))*((x)-(a))/((b)-(a))+(c)
 #define inbox (mx>125) and (mx<475)' and (my>300) and (my<350)
 #define incircle(cx,cy,radius,x,y) (cx-x)*(cx-x) +(cy-y)*(cy-y)<= radius*radius
 
 #macro display
    fr=map(125,475,cx,10,120)
    screenlock
    cls
    draw string(20,20),"Actual Framerate     = " &fps
    draw string(20,40),"Requested Framerate  = " &fr
    draw string(20,60),"Sleeping time        = " &sleeptime
    draw string(100,280),"10"
    draw string(480,280),"120"
    draw string(250,280),"<--slider-->"
    angle+=.1
    drawline(600,200,.2*sin(angle)-pi/2,300,4)
    line(100,300)-(500,350),2,bf
    circle(cx,cy),25,5,,,,f
    screenunlock
     #ifdef __FB_WIN32__
    settimer
    #endif
    sleep sleeptime,1
     #ifdef __FB_WIN32__
    freetimer
    #endif
#endmacro

#macro mouse
Dim As Long x=mx,y=my,dx,dy
While mb = 1
    Display
    Getmouse mx,my,,mb
    If inbox Then
        If mx<>x Or my<>y  Then
            dx = mx - x
            dy = my - y
            x = mx
            y = my
            cx=x+dx
            if cx<125 then cx=125
            if cx>475 then cx=475
        End If
    End If
Wend
#endmacro

sub drawline(x as long,y as long,angle as single,length as long,col as ulong)
    var x2=x+length*cos(angle)
    var y2=y-length*sin(angle)
     line(x,y)-(x2,y2)
     circle(x2,y2),10,6,,,,f
end sub

Function Regulate(Byval MyFps As long,Byref fps As long) As long
    Static As Double timervalue,lastsleeptime,t3,frames
    frames+=1
    If (Timer-t3)>=1 Then t3=Timer:fps=frames:frames=0
    Var sleeptime=lastsleeptime+((1/myfps)-Timer+timervalue)*1000
    If sleeptime<1 Then sleeptime=1
    lastsleeptime=sleeptime
    timervalue=Timer
    Return sleeptime
End Function

screen 19
dim as integer mx,my,mb
dim as long cx=125,cy=325,fps,fr,sleeptime
dim as single angle,pi=4*atn(1)
do
    getmouse mx,my,,mb
    
    display
    
    if incircle(cx,cy,25,mx,my) and mb=1 then
        mouse
        end if
   
    sleeptime= regulate(fr,fps)
   
    loop until len(inkey) 
slider 120
real frame 95 (gradually rises to 120)
requested frame 120
sleep time 8
What is the test?
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Distortion when moving across the screen sprite (Raspberry 4B))

Post by dodicat »

The test means that sleep 1 in your Linux distro is one millisecond, so it will be easier to get a smooth run.
On windows you will get a max of about 64 fps no matter how far you drag the slider, (thus for windows I have to add settimer and freetimer) to get the 120 fps.
I assume that even offline you still get the same result.
PavelUT
Posts: 78
Joined: Jun 14, 2021 14:42

Re: Distortion when moving across the screen sprite (Raspberry 4B))

Post by PavelUT »

dodicat wrote:The test means that sleep 1 in your Linux distro is one millisecond, so it will be easier to get a smooth run.
On windows you will get a max of about 64 fps no matter how far you drag the slider, (thus for windows I have to add settimer and freetimer) to get the 120 fps.
I assume that even offline you still get the same result.
Cool. But how does this help me solve the Linux distortion problem? What are your recommendations?
PavelUT
Posts: 78
Joined: Jun 14, 2021 14:42

Re: Distortion when moving across the screen sprite (Raspberry 4B))

Post by PavelUT »

PavelUT wrote:
dodicat wrote:The test means that sleep 1 in your Linux distro is one millisecond, so it will be easier to get a smooth run.
On windows you will get a max of about 64 fps no matter how far you drag the slider, (thus for windows I have to add settimer and freetimer) to get the 120 fps.
I assume that even offline you still get the same result.
Cool. But how does this help me solve the Linux distortion problem? What are your recommendations?
Generally the problem is not in FPS. The image does NOT flicker. It is distorted when moving across the screen more than +1 pixel.
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Distortion when moving across the screen sprite (Raspberry 4B))

Post by dodicat »

I can only suggest you move 1 pixel at a time and speed up the framerate with the regulator.
Also keep your colours contrasted a bit to conceal any distortions, yellow on blue is very bold on.
Draw your sprite in the centre of a larger image, leave a bit of transparency around the edge.

Code: Select all



ScreenRes 800, 600, 32,2
width 800\8,600\16 '' get biggest font
Screenset  1,0

dim as any ptr i=imagecreate(128,256)

Function Regulate(Byval MyFps As long,Byref fps As long) As long
    Static As Double timervalue,lastsleeptime,t3,frames
    frames+=1
    If (Timer-t3)>=1 Then t3=Timer:fps=frames:frames=0
    Var sleeptime=lastsleeptime+((1/myfps)-Timer+timervalue)*1000
    If sleeptime<1 Then sleeptime=1
    lastsleeptime=sleeptime
    timervalue=Timer
    Return sleeptime
End Function


circle i,(64,128),40,rgb(200,0,0),,,,f
draw string i,(45,120),"Hello",0
dim as long fps
Dim As single x = 0
    Color RGB(255, 128, 0), RGB(0, 0, 200)
    Cls
Do
    Cls
    put(x,80),i,trans
    'Line (x, 80)-Step(63, 127), RGB(255, 255, 0), BF
    x += 1: If (x > 639) Then x = 0
     
     Flip
     sleep regulate(120,fps)
Loop While Inkey = ""

sleep
end 
PavelUT
Posts: 78
Joined: Jun 14, 2021 14:42

Re: Distortion when moving across the screen sprite (Raspberry 4B))

Post by PavelUT »

dodicat wrote:I can only suggest you move 1 pixel at a time and speed up the framerate with the regulator.
Also keep your colours contrasted a bit to conceal any distortions, yellow on blue is very bold on.
Draw your sprite in the centre of a larger image, leave a bit of transparency around the edge.

Code: Select all



ScreenRes 800, 600, 32,2
width 800\8,600\16 '' get biggest font
Screenset  1,0

dim as any ptr i=imagecreate(128,256)

Function Regulate(Byval MyFps As long,Byref fps As long) As long
    Static As Double timervalue,lastsleeptime,t3,frames
    frames+=1
    If (Timer-t3)>=1 Then t3=Timer:fps=frames:frames=0
    Var sleeptime=lastsleeptime+((1/myfps)-Timer+timervalue)*1000
    If sleeptime<1 Then sleeptime=1
    lastsleeptime=sleeptime
    timervalue=Timer
    Return sleeptime
End Function


circle i,(64,128),40,rgb(200,0,0),,,,f
draw string i,(45,120),"Hello",0
dim as long fps
Dim As single x = 0
    Color RGB(255, 128, 0), RGB(0, 0, 200)
    Cls
Do
    Cls
    put(x,80),i,trans
    'Line (x, 80)-Step(63, 127), RGB(255, 255, 0), BF
    x += 1: If (x > 639) Then x = 0
     
     Flip
     sleep regulate(120,fps)
Loop While Inkey = ""

sleep
end 
Thanks for your help, but these guidelines won't help. The circle and rectangle program is a lightweight example of working with sprites. You gave me 2 examples with OpenGL. Are you good at it? Maybe mastering OpenGL 2D isn't too hard?
Landeel
Posts: 777
Joined: Jan 25, 2007 10:32
Location: Brazil
Contact:

Re: Distortion when moving across the screen sprite (Raspberry 4B))

Post by Landeel »

I think what you're seeing is tearing. It means the image refresh is not in sync with the monitor retrace.

This is what I use to enable VSYNC: viewtopic.php?t=13715&start=0&postdays= ... engl+vsync

For this to work, you must:
1) Set an OpenGL video/window mode using the 'GFX_OPENGL' flag in SCREEN/SCREENRES;
2) Enable rendering fbgfx primitives in OpenGL using "SCREENCONTROL SET_GL_2D_MODE, OGL_2D_MANUAL_SYNC".

Then, you just call 'FLIP' every frame, and it will sync to your monitor retrace.
PavelUT
Posts: 78
Joined: Jun 14, 2021 14:42

Re: Distortion when moving across the screen sprite (Raspberry 4B))

Post by PavelUT »

Thanks, I'll try
PavelUT
Posts: 78
Joined: Jun 14, 2021 14:42

Re: Distortion when moving across the screen sprite (Raspberry 4B))

Post by PavelUT »

Landeel wrote:I think what you're seeing is tearing. It means the image refresh is not in sync with the monitor retrace.

This is what I use to enable VSYNC: viewtopic.php?t=13715&start=0&postdays= ... engl+vsync

For this to work, you must:
1) Set an OpenGL video/window mode using the 'GFX_OPENGL' flag in SCREEN/SCREENRES;
2) Enable rendering fbgfx primitives in OpenGL using "SCREENCONTROL SET_GL_2D_MODE, OGL_2D_MANUAL_SYNC".

Then, you just call 'FLIP' every frame, and it will sync to your monitor retrace.

The program of your link viewtopic.php?t=13715&start=0&postdays= ... engl+vsync
is easily converted to 32-bit FB1. 07. But on 64-bit 1.08 it gives an error: Passing different pointer types, at parameter 2 of GLGETINTEGERV(). Please correct it, if it's not difficult for you.
Landeel
Posts: 777
Joined: Jan 25, 2007 10:32
Location: Brazil
Contact:

Re: Distortion when moving across the screen sprite (Raspberry 4B))

Post by Landeel »

PavelUT wrote: The program of your link viewtopic.php?t=13715&start=0&postdays= ... engl+vsync
is easily converted to 32-bit FB1. 07. But on 64-bit 1.08 it gives an error: Passing different pointer types, at parameter 2 of GLGETINTEGERV(). Please correct it, if it's not difficult for you.
Just change integers to longs. This is due recent changes in fbc.
Post Reply