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

New to FreeBASIC? Post your questions here.
Imortis
Moderator
Posts: 1923
Joined: Jun 02, 2005 15:10
Location: USA
Contact:

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

Post by Imortis »

PavelUT wrote:
Imortis wrote:At the top of your code add:

Code: Select all

#include once "fbgfx.bi"
Using FB
After your screenset command add this:

Code: Select all

Screencontrol SET_GL_2D_MODE, OGL_2D_MANUAL_SYNC
You could also use:

Code: Select all

Screencontrol SET_GL_2D_MODE, OGL_2D_AUTO_SYNC
Which does not require the Flip command to be in place.
#include once "fbgfx.bi"
Using FB
Screencontrol SET_GL_2D_MODE, OGL_2D_MANUAL_SYNC
Screencontrol SET_GL_2D_MODE, OGL_2D_AUTO_SY
Unfortunately, the gadgets did not help.
Flip switches buffers, which should theoretically cut should reduce distortion
Only use 1 of the Screencontrol statements.

Also, in OpenGL 2D mode, Flip is required for anything to be displayed to the screen. Without it, you will see nothing, unless you use the OGL_2D_AUTO_SYNC version.

Also, either way it is not a bad idea to use "Screenlock" before all commands that draw and "ScreenUnlock" after. Then call the Flip.
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

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

Post by dodicat »

Hello PavelUT
Here is an example of a sprite/texture in opengl.
There are many on this forum who are opengl experts, and opengl can now have a completely new format.

Code: Select all


#include "GL/gl.bi"
#include "GL/glext.bi"
#include "fbgfx.bi"
Screenres 800,600,32
Dim As long refreshrate
Dim As String driver
Screeninfo ,,,,,refreshrate

Screenres 800, 600, 32,,2
Screeninfo ,,,,,,driver
Width 800\8,600\16 'full fonts on image


Declare Function settimer       Alias "timeBeginPeriod"(As Ulong=1) As Long
Declare Function freetimer      Alias "timeEndPeriod"  (As Ulong=1) As Long


Sub setupgl
    Dim As Integer xres,yres
    Screeninfo xres,yres
    glDisable (GL_DEPTH_TEST)
    glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA)
    glEnable (GL_BLEND)
    glEnable (GL_LINE_SMOOTH)
    glOrtho 0, xres, yres,0,-1, 1
    glClearColor 0,0,200/255,1
End Sub

setupgl

Sub drawstring(xpos As Long,ypos As Long,text As String ,col As Ulong,size As Single,xres As Long,yres As Long) Export
    glMatrixMode GL_PROJECTION 'save projection
    glPushMatrix
    glMatrixMode GL_MODELVIEW
    glPushMatrix
    
    glMatrixMode GL_PROJECTION 'make ortho
    glLoadIdentity
    glOrtho 0, xres, yres, 0,-1, 1
    glMatrixMode GL_MODELVIEW
    glLoadIdentity
    #define Red(c) ((c) Shr 16 And 255)
    #define Green(c) ((c) Shr  8 And 255)
    #define Blue(c) ((c) And 255)
    #define Alph(c) ((c) Shr 24)
    glColor4ub Red(col),Green(col),Blue(col),alph(col)
    glend
    glpointsize(1.1*size)
    glBegin (GL_POINTS)
    Type D2
        As Single x,y
    End Type
    Static As d2 cpt(),XY()
    Static As Long runflag
    If runflag=0 Then   
        Redim  XY(128,127)
        Redim cpt(1 To 64*2)
        Screen 8
        Width 640\8,200\16
        Dim As Ulong Pointer img
        Dim count As Long
        For ch As Long=1 To 127
            img=Imagecreate(640,200)
            Draw String img,(1,1),Chr(ch)
            For x As Long=1 To 8 
                For y As Long=1 To 16
                    If Point(x,y,img)<>0 Then
                        count=count+1
                        XY(count,ch)=Type<D2>(x,y)
                    End If
                Next y
            Next x
            count=0
            Imagedestroy img
        Next ch
        runflag=1
    End If
    If size=0 Then Exit Sub
    Dim As D2 np,t
    #macro Scale(p1,p2,d)
    np.x=d*(p2.x-p1.x)+p1.x
    np.y=d*(p2.y-p1.y)+p1.y
    #endmacro
    
    Dim As D2 c=Type<D2>(xpos,ypos)
    Dim As Long dx=xpos,dy=ypos
    For z6 As Long=1 To Len(text)
        Var asci=text[z6-1]
        For _x1 As Long=1 To 64*2
            t=Type<D2>(XY(_x1,asci).x+dx,XY(_x1,asci).y+dy)         
            Scale(c,t,size)
            cpt(_x1)=np
            
            If XY(_x1,asci).x<>0 Then
                If Abs(size)>0 Then
                    glVertex3f (cpt(_x1).x,(cpt(_x1).y),0)
                End If
            End If
        Next _x1
        dx=dx+8
    Next z6
    glend
    glMatrixMode GL_PROJECTION 'restore 
    glPopMatrix
    glMatrixMode GL_MODELVIEW
    glPopMatrix
End Sub

Sub inittext Constructor
    drawstring(0,0,"",0,0,0,0)
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

Sub settexture( texture As gluint, image As Any Ptr)
    glGenTextures(1, @texture)
    glBindTexture( GL_TEXTURE_2D, texture )
    glTexImage2d( GL_TEXTURE_2D, 0, GL_RGBA, Cast(fb.image Ptr, image)->Width, Cast(fb.image Ptr, image)->height, 0, GL_BGRA, GL_UNSIGNED_BYTE, image+Sizeof(fb.image) )
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST )
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST )
    glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL)
End Sub


Sub drawquad(x As Integer)
    glBegin(GL_QUADS)
    glTexCoord2f( 1,0 )
    glVertex2f( x, 80)            'Top right of the quad
    glTexCoord2f( 0,0 )
    glVertex2f(x-63, 80)          'Top left of the quad
    glTexCoord2f( 0,1 )
    glVertex2f(x-63, 127+80)      ' Bottom left of the quad
    glTexCoord2f( 1,1 )
    glVertex2f( x, 127+80)        ' Bottom right of the quad
    glend
End Sub


Dim As Any Ptr im=Imagecreate(128/2,128)
'draw stuff to image
For x As Long=0 To 128\2
    For y As Long=0 To 128
        Pset im,(x,y),Rgb(x*4,x*4 xor y*4,y*4)
    Next y
Next x
Circle im,(32,64),20,Rgb(200,0,0),,,,f
Draw String im,(10,100),"Hello",Rgb(255,255,255)

Dim As gluint tex1
settexture(tex1,im)

Dim As Integer x = 0
Dim As Long fps

Do
    glClear(GL_COLOR_BUFFER_BIT)
    
    glEnable( GL_TEXTURE_2D )
    drawquad(x)
    gldisable( GL_TEXTURE_2D )
    
    x += 1: If (x > 639) Then x = 0
    drawstring(20,20,"framerate = " &fps,Rgb(0,200,0),1,800,600)
    drawstring(20,40,"refreshrate = " &refreshrate,Rgb(0,200,0),1,800,600)
    drawstring(20,60,"driver = " +driver,Rgb(0,200,0),1,800,600)
    Flip
    settimer
    Sleep regulate(refreshrate*3,fps)
    freetimer
Loop While Inkey = ""
Imagedestroy im

End


 
caseih
Posts: 2157
Joined: Feb 26, 2007 5:32

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

Post by caseih »

Not sure immediate-mode OpenGL code will work on the Raspberry Pi, though. I think the Pi only supports OpenGL ES.

Anyway I think @PavelUT might have the most success with an existing library for this purpose, like SDL. It's designed for animations, sprites, etc.
bfuller
Posts: 362
Joined: Jun 02, 2007 12:35
Location: Sydney, Australia

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

Post by bfuller »

I had to delete this line to get Dodicat's code to run on my W10 64bit PC.
Screeninfo ,,,,,refreshrate
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

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

Post by dodicat »

Hi bfuller
I am on 1.08 now, you might need integer for refreshrate otherwise.
Or you could just
refreshrate=60
How's the boat?
bfuller
Posts: 362
Joined: Jun 02, 2007 12:35
Location: Sydney, Australia

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

Post by bfuller »

How's the boat?
Currently on the hard stand with masts down for standing and running rig replacement ready for re-launch mid July.
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

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

Post by D.J.Peters »

bfuller wrote:I had to delete this line to get Dodicat's code to run on my W10 64bit PC.
Screeninfo ,,,,,refreshrate

Code: Select all

dim as integer unused
Screeninfo unused,unused,unused,unused,unused,refreshrate
PavelUT
Posts: 78
Joined: Jun 14, 2021 14:42

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

Post by PavelUT »

caseih wrote:Not sure immediate-mode OpenGL code will work on the Raspberry Pi, though. I think the Pi only supports OpenGL ES.

Anyway I think @PavelUT might have the most success with an existing library for this purpose, like SDL. It's designed for animations, sprites, etc.
I'm not against. Tell me how
PavelUT
Posts: 78
Joined: Jun 14, 2021 14:42

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

Post by PavelUT »

dodicat wrote:Hello PavelUT
Here is an example of a sprite/texture in opengl.
There are many on this forum who are opengl experts, and opengl can now have a completely new format.

Code: Select all


#include "GL/gl.bi"
#include "GL/glext.bi"
#include "fbgfx.bi"
Screenres 800,600,32
Dim As long refreshrate
Dim As String driver
Screeninfo ,,,,,refreshrate

Screenres 800, 600, 32,,2
Screeninfo ,,,,,,driver
Width 800\8,600\16 'full fonts on image


Declare Function settimer       Alias "timeBeginPeriod"(As Ulong=1) As Long
Declare Function freetimer      Alias "timeEndPeriod"  (As Ulong=1) As Long


Sub setupgl
    Dim As Integer xres,yres
    Screeninfo xres,yres
    glDisable (GL_DEPTH_TEST)
    glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA)
    glEnable (GL_BLEND)
    glEnable (GL_LINE_SMOOTH)
    glOrtho 0, xres, yres,0,-1, 1
    glClearColor 0,0,200/255,1
End Sub

setupgl

Sub drawstring(xpos As Long,ypos As Long,text As String ,col As Ulong,size As Single,xres As Long,yres As Long) Export
    glMatrixMode GL_PROJECTION 'save projection
    glPushMatrix
    glMatrixMode GL_MODELVIEW
    glPushMatrix
    
    glMatrixMode GL_PROJECTION 'make ortho
    glLoadIdentity
    glOrtho 0, xres, yres, 0,-1, 1
    glMatrixMode GL_MODELVIEW
    glLoadIdentity
    #define Red(c) ((c) Shr 16 And 255)
    #define Green(c) ((c) Shr  8 And 255)
    #define Blue(c) ((c) And 255)
    #define Alph(c) ((c) Shr 24)
    glColor4ub Red(col),Green(col),Blue(col),alph(col)
    glend
    glpointsize(1.1*size)
    glBegin (GL_POINTS)
    Type D2
        As Single x,y
    End Type
    Static As d2 cpt(),XY()
    Static As Long runflag
    If runflag=0 Then   
        Redim  XY(128,127)
        Redim cpt(1 To 64*2)
        Screen 8
        Width 640\8,200\16
        Dim As Ulong Pointer img
        Dim count As Long
        For ch As Long=1 To 127
            img=Imagecreate(640,200)
            Draw String img,(1,1),Chr(ch)
            For x As Long=1 To 8 
                For y As Long=1 To 16
                    If Point(x,y,img)<>0 Then
                        count=count+1
                        XY(count,ch)=Type<D2>(x,y)
                    End If
                Next y
            Next x
            count=0
            Imagedestroy img
        Next ch
        runflag=1
    End If
    If size=0 Then Exit Sub
    Dim As D2 np,t
    #macro Scale(p1,p2,d)
    np.x=d*(p2.x-p1.x)+p1.x
    np.y=d*(p2.y-p1.y)+p1.y
    #endmacro
    
    Dim As D2 c=Type<D2>(xpos,ypos)
    Dim As Long dx=xpos,dy=ypos
    For z6 As Long=1 To Len(text)
        Var asci=text[z6-1]
        For _x1 As Long=1 To 64*2
            t=Type<D2>(XY(_x1,asci).x+dx,XY(_x1,asci).y+dy)         
            Scale(c,t,size)
            cpt(_x1)=np
            
            If XY(_x1,asci).x<>0 Then
                If Abs(size)>0 Then
                    glVertex3f (cpt(_x1).x,(cpt(_x1).y),0)
                End If
            End If
        Next _x1
        dx=dx+8
    Next z6
    glend
    glMatrixMode GL_PROJECTION 'restore 
    glPopMatrix
    glMatrixMode GL_MODELVIEW
    glPopMatrix
End Sub

Sub inittext Constructor
    drawstring(0,0,"",0,0,0,0)
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

Sub settexture( texture As gluint, image As Any Ptr)
    glGenTextures(1, @texture)
    glBindTexture( GL_TEXTURE_2D, texture )
    glTexImage2d( GL_TEXTURE_2D, 0, GL_RGBA, Cast(fb.image Ptr, image)->Width, Cast(fb.image Ptr, image)->height, 0, GL_BGRA, GL_UNSIGNED_BYTE, image+Sizeof(fb.image) )
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST )
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST )
    glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL)
End Sub


Sub drawquad(x As Integer)
    glBegin(GL_QUADS)
    glTexCoord2f( 1,0 )
    glVertex2f( x, 80)            'Top right of the quad
    glTexCoord2f( 0,0 )
    glVertex2f(x-63, 80)          'Top left of the quad
    glTexCoord2f( 0,1 )
    glVertex2f(x-63, 127+80)      ' Bottom left of the quad
    glTexCoord2f( 1,1 )
    glVertex2f( x, 127+80)        ' Bottom right of the quad
    glend
End Sub


Dim As Any Ptr im=Imagecreate(128/2,128)
'draw stuff to image
For x As Long=0 To 128\2
    For y As Long=0 To 128
        Pset im,(x,y),Rgb(x*4,x*4 xor y*4,y*4)
    Next y
Next x
Circle im,(32,64),20,Rgb(200,0,0),,,,f
Draw String im,(10,100),"Hello",Rgb(255,255,255)

Dim As gluint tex1
settexture(tex1,im)

Dim As Integer x = 0
Dim As Long fps

Do
    glClear(GL_COLOR_BUFFER_BIT)
    
    glEnable( GL_TEXTURE_2D )
    drawquad(x)
    gldisable( GL_TEXTURE_2D )
    
    x += 1: If (x > 639) Then x = 0
    drawstring(20,20,"framerate = " &fps,Rgb(0,200,0),1,800,600)
    drawstring(20,40,"refreshrate = " &refreshrate,Rgb(0,200,0),1,800,600)
    drawstring(20,60,"driver = " +driver,Rgb(0,200,0),1,800,600)
    Flip
    settimer
    Sleep regulate(refreshrate*3,fps)
    freetimer
Loop While Inkey = ""
Imagedestroy im

End


 
I have FreeBasic 1.072. When trying to compile, writes:
ld: Time1.o: in function `main ':
Time1.c :(. Text + 0x1138): undefined reference to `timeBeginPeriod '
ld: Time1.c :(. text + 0x116c): undefined reference to `timeEndPeriod '
and stops with an error

I have Linux
PavelUT
Posts: 78
Joined: Jun 14, 2021 14:42

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

Post by PavelUT »

Imortis wrote:
PavelUT wrote:
Imortis wrote:At the top of your code add:

Code: Select all

#include once "fbgfx.bi"
Using FB
After your screenset command add this:

Code: Select all

Screencontrol SET_GL_2D_MODE, OGL_2D_MANUAL_SYNC
You could also use:

Code: Select all

Screencontrol SET_GL_2D_MODE, OGL_2D_AUTO_SYNC
Which does not require the Flip command to be in place.
#include once "fbgfx.bi"
Using FB
Screencontrol SET_GL_2D_MODE, OGL_2D_MANUAL_SYNC
Screencontrol SET_GL_2D_MODE, OGL_2D_AUTO_SY
Unfortunately, the gadgets did not help.
Flip switches buffers, which should theoretically cut should reduce distortion
Only use 1 of the Screencontrol statements.

Also, in OpenGL 2D mode, Flip is required for anything to be displayed to the screen. Without it, you will see nothing, unless you use the OGL_2D_AUTO_SYNC version.

Also, either way it is not a bad idea to use "Screenlock" before all commands that draw and "ScreenUnlock" after. Then call the Flip.
I tried it in different ways. The result is still the same
PavelUT
Posts: 78
Joined: Jun 14, 2021 14:42

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

Post by PavelUT »

It turns out interestingly when moving vertically (yspr2 = yspr2 + 3: If (yspr2> 500) Then yspr2 = 0)
No distortion
speedfixer
Posts: 606
Joined: Nov 28, 2012 1:27
Location: CA, USA moving to WA, USA
Contact:

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

Post by speedfixer »

CLS is kind of expensive.

try this:

Code: Select all

ScreenRes 800, 600, 32,2
Screenset  1,0
Dim As Integer x = 0
    Color RGB(255, 128, 0), RGB(0, 0, 200)
    Cls
Do
    Line (x, 80)-Step(63, 127), RGB(0, 0, 200), BF
    x += 3: If (x > 639) Then x = 0
     ScreenSync
    sleep 5, 1
    Line (x, 80)-Step(63, 127), RGB(255, 255, 0), BF
    flip

Loop While Inkey = ""

sleep
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

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

Post by dodicat »

PavelUT wrote:
dodicat wrote:Hello PavelUT
Here is an example of a sprite/texture in opengl.
There are many on this forum who are opengl experts, and opengl can now have a completely new format.

Code: Select all


#include "GL/gl.bi"
#include "GL/glext.bi"
#include "fbgfx.bi"
Screenres 800,600,32
Dim As long refreshrate
Dim As String driver
Screeninfo ,,,,,refreshrate

Screenres 800, 600, 32,,2
Screeninfo ,,,,,,driver
Width 800\8,600\16 'full fonts on image


'Declare Function settimer       Alias "timeBeginPeriod"(As Ulong=1) As Long
'Declare Function freetimer      Alias "timeEndPeriod"  (As Ulong=1) As Long


Sub setupgl
    Dim As Integer xres,yres
    Screeninfo xres,yres
    glDisable (GL_DEPTH_TEST)
    glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA)
    glEnable (GL_BLEND)
    glEnable (GL_LINE_SMOOTH)
    glOrtho 0, xres, yres,0,-1, 1
    glClearColor 0,0,200/255,1
End Sub

setupgl

Sub drawstring(xpos As Long,ypos As Long,text As String ,col As Ulong,size As Single,xres As Long,yres As Long) Export
    glMatrixMode GL_PROJECTION 'save projection
    glPushMatrix
    glMatrixMode GL_MODELVIEW
    glPushMatrix
    
    glMatrixMode GL_PROJECTION 'make ortho
    glLoadIdentity
    glOrtho 0, xres, yres, 0,-1, 1
    glMatrixMode GL_MODELVIEW
    glLoadIdentity
    #define Red(c) ((c) Shr 16 And 255)
    #define Green(c) ((c) Shr  8 And 255)
    #define Blue(c) ((c) And 255)
    #define Alph(c) ((c) Shr 24)
    glColor4ub Red(col),Green(col),Blue(col),alph(col)
    glend
    glpointsize(1.1*size)
    glBegin (GL_POINTS)
    Type D2
        As Single x,y
    End Type
    Static As d2 cpt(),XY()
    Static As Long runflag
    If runflag=0 Then   
        Redim  XY(128,127)
        Redim cpt(1 To 64*2)
        Screen 8
        Width 640\8,200\16
        Dim As Ulong Pointer img
        Dim count As Long
        For ch As Long=1 To 127
            img=Imagecreate(640,200)
            Draw String img,(1,1),Chr(ch)
            For x As Long=1 To 8 
                For y As Long=1 To 16
                    If Point(x,y,img)<>0 Then
                        count=count+1
                        XY(count,ch)=Type<D2>(x,y)
                    End If
                Next y
            Next x
            count=0
            Imagedestroy img
        Next ch
        runflag=1
    End If
    If size=0 Then Exit Sub
    Dim As D2 np,t
    #macro Scale(p1,p2,d)
    np.x=d*(p2.x-p1.x)+p1.x
    np.y=d*(p2.y-p1.y)+p1.y
    #endmacro
    
    Dim As D2 c=Type<D2>(xpos,ypos)
    Dim As Long dx=xpos,dy=ypos
    For z6 As Long=1 To Len(text)
        Var asci=text[z6-1]
        For _x1 As Long=1 To 64*2
            t=Type<D2>(XY(_x1,asci).x+dx,XY(_x1,asci).y+dy)         
            Scale(c,t,size)
            cpt(_x1)=np
            
            If XY(_x1,asci).x<>0 Then
                If Abs(size)>0 Then
                    glVertex3f (cpt(_x1).x,(cpt(_x1).y),0)
                End If
            End If
        Next _x1
        dx=dx+8
    Next z6
    glend
    glMatrixMode GL_PROJECTION 'restore 
    glPopMatrix
    glMatrixMode GL_MODELVIEW
    glPopMatrix
End Sub

Sub inittext Constructor
    drawstring(0,0,"",0,0,0,0)
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

Sub settexture( texture As gluint, image As Any Ptr)
    glGenTextures(1, @texture)
    glBindTexture( GL_TEXTURE_2D, texture )
    glTexImage2d( GL_TEXTURE_2D, 0, GL_RGBA, Cast(fb.image Ptr, image)->Width, Cast(fb.image Ptr, image)->height, 0, GL_BGRA, GL_UNSIGNED_BYTE, image+Sizeof(fb.image) )
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST )
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST )
    glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL)
End Sub


Sub drawquad(x As Integer)
    glBegin(GL_QUADS)
    glTexCoord2f( 1,0 )
    glVertex2f( x, 80)            'Top right of the quad
    glTexCoord2f( 0,0 )
    glVertex2f(x-63, 80)          'Top left of the quad
    glTexCoord2f( 0,1 )
    glVertex2f(x-63, 127+80)      ' Bottom left of the quad
    glTexCoord2f( 1,1 )
    glVertex2f( x, 127+80)        ' Bottom right of the quad
    glend
End Sub


Dim As Any Ptr im=Imagecreate(128/2,128)
'draw stuff to image
For x As Long=0 To 128\2
    For y As Long=0 To 128
        Pset im,(x,y),Rgb(x*4,x*4 xor y*4,y*4)
    Next y
Next x
Circle im,(32,64),20,Rgb(200,0,0),,,,f
Draw String im,(10,100),"Hello",Rgb(255,255,255)

Dim As gluint tex1
settexture(tex1,im)

Dim As Integer x = 0
Dim As Long fps

Do
    glClear(GL_COLOR_BUFFER_BIT)
    
    glEnable( GL_TEXTURE_2D )
    drawquad(x)
    gldisable( GL_TEXTURE_2D )
    
    x += 1: If (x > 639) Then x = 0
    drawstring(20,20,"framerate = " &fps,Rgb(0,200,0),1,800,600)
    drawstring(20,40,"refreshrate = " &refreshrate,Rgb(0,200,0),1,800,600)
    drawstring(20,60,"driver = " +driver,Rgb(0,200,0),1,800,600)
    Flip
   ' settimer
    Sleep regulate(refreshrate*3,fps)
  '  freetimer
Loop While Inkey = ""
Imagedestroy im

End


 
I have FreeBasic 1.072. When trying to compile, writes:
ld: Time1.o: in function `main ':
Time1.c :(. Text + 0x1138): undefined reference to `timeBeginPeriod '
ld: Time1.c :(. text + 0x116c): undefined reference to `timeEndPeriod '
and stops with an error

I have Linux
. . .
I have commented out the windows stuff (ABOVE in this post), please try again.
PavelUT
Posts: 78
Joined: Jun 14, 2021 14:42

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

Post by PavelUT »

speedfixer wrote:CLS is kind of expensive.

try this:

Code: Select all

ScreenRes 800, 600, 32,2
Screenset  1,0
Dim As Integer x = 0
    Color RGB(255, 128, 0), RGB(0, 0, 200)
    Cls
Do
    Line (x, 80)-Step(63, 127), RGB(0, 0, 200), BF
    x += 3: If (x > 639) Then x = 0
     ScreenSync
    sleep 5, 1
    Line (x, 80)-Step(63, 127), RGB(255, 255, 0), BF
    flip

Loop While Inkey = ""

sleep
In fact (on Rasberry 4B) 640x480 is updated in 1/1000 of a second (about the same time as filling the screen with a picture) In my opinion, quite quickly. Then below I gave a program using sprites. (For simplicity, a circle and a rectangle.) If you wish, take a look.
PavelUT
Posts: 78
Joined: Jun 14, 2021 14:42

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

Post by PavelUT »

dodicat wrote:
PavelUT wrote:
dodicat wrote:Hello PavelUT
Here is an example of a sprite/texture in opengl.
There are many on this forum who are opengl experts, and opengl can now have a completely new format.

Code: Select all


#include "GL/gl.bi"
#include "GL/glext.bi"
#include "fbgfx.bi"
Screenres 800,600,32
Dim As long refreshrate
Dim As String driver
Screeninfo ,,,,,refreshrate

Screenres 800, 600, 32,,2
Screeninfo ,,,,,,driver
Width 800\8,600\16 'full fonts on image


'Declare Function settimer       Alias "timeBeginPeriod"(As Ulong=1) As Long
'Declare Function freetimer      Alias "timeEndPeriod"  (As Ulong=1) As Long


Sub setupgl
    Dim As Integer xres,yres
    Screeninfo xres,yres
    glDisable (GL_DEPTH_TEST)
    glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA)
    glEnable (GL_BLEND)
    glEnable (GL_LINE_SMOOTH)
    glOrtho 0, xres, yres,0,-1, 1
    glClearColor 0,0,200/255,1
End Sub

setupgl

Sub drawstring(xpos As Long,ypos As Long,text As String ,col As Ulong,size As Single,xres As Long,yres As Long) Export
    glMatrixMode GL_PROJECTION 'save projection
    glPushMatrix
    glMatrixMode GL_MODELVIEW
    glPushMatrix
    
    glMatrixMode GL_PROJECTION 'make ortho
    glLoadIdentity
    glOrtho 0, xres, yres, 0,-1, 1
    glMatrixMode GL_MODELVIEW
    glLoadIdentity
    #define Red(c) ((c) Shr 16 And 255)
    #define Green(c) ((c) Shr  8 And 255)
    #define Blue(c) ((c) And 255)
    #define Alph(c) ((c) Shr 24)
    glColor4ub Red(col),Green(col),Blue(col),alph(col)
    glend
    glpointsize(1.1*size)
    glBegin (GL_POINTS)
    Type D2
        As Single x,y
    End Type
    Static As d2 cpt(),XY()
    Static As Long runflag
    If runflag=0 Then   
        Redim  XY(128,127)
        Redim cpt(1 To 64*2)
        Screen 8
        Width 640\8,200\16
        Dim As Ulong Pointer img
        Dim count As Long
        For ch As Long=1 To 127
            img=Imagecreate(640,200)
            Draw String img,(1,1),Chr(ch)
            For x As Long=1 To 8 
                For y As Long=1 To 16
                    If Point(x,y,img)<>0 Then
                        count=count+1
                        XY(count,ch)=Type<D2>(x,y)
                    End If
                Next y
            Next x
            count=0
            Imagedestroy img
        Next ch
        runflag=1
    End If
    If size=0 Then Exit Sub
    Dim As D2 np,t
    #macro Scale(p1,p2,d)
    np.x=d*(p2.x-p1.x)+p1.x
    np.y=d*(p2.y-p1.y)+p1.y
    #endmacro
    
    Dim As D2 c=Type<D2>(xpos,ypos)
    Dim As Long dx=xpos,dy=ypos
    For z6 As Long=1 To Len(text)
        Var asci=text[z6-1]
        For _x1 As Long=1 To 64*2
            t=Type<D2>(XY(_x1,asci).x+dx,XY(_x1,asci).y+dy)         
            Scale(c,t,size)
            cpt(_x1)=np
            
            If XY(_x1,asci).x<>0 Then
                If Abs(size)>0 Then
                    glVertex3f (cpt(_x1).x,(cpt(_x1).y),0)
                End If
            End If
        Next _x1
        dx=dx+8
    Next z6
    glend
    glMatrixMode GL_PROJECTION 'restore 
    glPopMatrix
    glMatrixMode GL_MODELVIEW
    glPopMatrix
End Sub

Sub inittext Constructor
    drawstring(0,0,"",0,0,0,0)
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

Sub settexture( texture As gluint, image As Any Ptr)
    glGenTextures(1, @texture)
    glBindTexture( GL_TEXTURE_2D, texture )
    glTexImage2d( GL_TEXTURE_2D, 0, GL_RGBA, Cast(fb.image Ptr, image)->Width, Cast(fb.image Ptr, image)->height, 0, GL_BGRA, GL_UNSIGNED_BYTE, image+Sizeof(fb.image) )
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST )
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST )
    glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL)
End Sub


Sub drawquad(x As Integer)
    glBegin(GL_QUADS)
    glTexCoord2f( 1,0 )
    glVertex2f( x, 80)            'Top right of the quad
    glTexCoord2f( 0,0 )
    glVertex2f(x-63, 80)          'Top left of the quad
    glTexCoord2f( 0,1 )
    glVertex2f(x-63, 127+80)      ' Bottom left of the quad
    glTexCoord2f( 1,1 )
    glVertex2f( x, 127+80)        ' Bottom right of the quad
    glend
End Sub


Dim As Any Ptr im=Imagecreate(128/2,128)
'draw stuff to image
For x As Long=0 To 128\2
    For y As Long=0 To 128
        Pset im,(x,y),Rgb(x*4,x*4 xor y*4,y*4)
    Next y
Next x
Circle im,(32,64),20,Rgb(200,0,0),,,,f
Draw String im,(10,100),"Hello",Rgb(255,255,255)

Dim As gluint tex1
settexture(tex1,im)

Dim As Integer x = 0
Dim As Long fps

Do
    glClear(GL_COLOR_BUFFER_BIT)
    
    glEnable( GL_TEXTURE_2D )
    drawquad(x)
    gldisable( GL_TEXTURE_2D )
    
    x += 1: If (x > 639) Then x = 0
    drawstring(20,20,"framerate = " &fps,Rgb(0,200,0),1,800,600)
    drawstring(20,40,"refreshrate = " &refreshrate,Rgb(0,200,0),1,800,600)
    drawstring(20,60,"driver = " +driver,Rgb(0,200,0),1,800,600)
    Flip
   ' settimer
    Sleep regulate(refreshrate*3,fps)
  '  freetimer
Loop While Inkey = ""
Imagedestroy im

End


 
I have FreeBasic 1.072. When trying to compile, writes:
ld: Time1.o: in function `main ':
Time1.c :(. Text + 0x1138): undefined reference to `timeBeginPeriod '
ld: Time1.c :(. text + 0x116c): undefined reference to `timeEndPeriod '
and stops with an error

I have Linux
. . .
I have commented out the windows stuff (ABOVE in this post), please try again.
Thank you. Now, even at a speed of +3, there are really no distortions. Now I would like to understand this code without a bottle of whiskey ...
Post Reply