OpenGL vsync on/off (Windows and Linux)

Post your FreeBASIC tips and tricks here. Please don’t post your code without including an explanation.
D.J.Peters
Posts: 7852
Joined: May 28, 2005 3:28

OpenGL vsync on/off (Windows and Linux)

Postby D.J.Peters » Jun 14, 2014 23:52

The wiki example is windows only but I got it for Linux also.
glXQueryExtensionsString, glXGetCurrentDisplay() and glXGetCurrentDrawable() is the key for Linux.

I get ~7000 fps on Linux and Windows.

Joshy

Code: Select all

#include once "fbgfx.bi"
#include once "GL/gl.bi"
#include once "GL/glu.bi"

#ifdef __FB_WIN32__
const as string SWAP_EXT  = "WGL_EXT_swap_control"
const as string SWAP_PROC = "wglSwapIntervalEXT"
type SwapIntervalEXT as function stdcall (interval as long) as ubyte
#else
const as string SWAP_EXT  = "GLX_EXT_swap_control"
const as string SWAP_PROC = "glXSwapIntervalEXT"
type SwapIntervalEXT as sub cdecl(Display as any ptr, drawable as long, interval as long)
type Display as any
type GLXDrawable as integer

extern "C" lib "GL"
declare function glXGetCurrentDisplay() as Display ptr
declare function glXGetCurrentDrawable() as GLXDrawable
declare function glXQueryExtensionsString(dpy as Display ptr, nScreen as long) as const zstring ptr
end extern

#endif

type GL_GFX
  declare destructor
  declare constructor (iWidth       as integer=640, _
                       iHeight      as integer=480, _
                       iBits        as integer= 32, _
                       bFullscreen  as integer=  0, _
                       iDepthBits   as integer= 24, _
                       iStencilBits as integer=  8, _
                       iAccumBits   as integer=  8)
 
  declare sub SwapInterval(bOnOff as integer)
  private:
  declare sub m_InitExtensions
  as uinteger m_iDesktopWidth
  as uinteger m_iDesktopHeight
  as uinteger m_iDesktopBits
  as string   m_sExtensions
  as SwapIntervalEXT m_SwapIntervalEXT
  #ifndef __FB_WIN32__
  as any ptr m_Display
  as integer m_Drawable
  #endif
end type
destructor GL_GFX
 ' nothing atm
end destructor
constructor GL_GFX(iWidth       as integer, _
                   iHeight      as integer, _
                   iPixelBits   as integer, _
                   bFullscreen  as integer, _
                   iDepthBits   as integer, _
                   iStencilBits as integer, _
                   iAccumBits   as integer)
  ScreenInfo m_iDesktopWidth,m_iDesktopHeight,m_iDesktopBits
  if bFullscreen=0 then
    iPixelBits=m_iDesktopBits
    if iWidth >m_iDesktopWidth  then iWidth =m_iDesktopWidth
    if iHeight>m_iDesktopHeight then iHeight=m_iDesktopHeight
  else
    if iPixelBits<24 then
      iPixelBits=16
    elseif iPixelBits>24 then
      iPixelBits=32
    else
    iPixelBits=24
    end if
  end if
  if iWidth<640 or iHeight<480 then
    iWidth=640:iHeight=480
  end if
  if iDepthBits then
    if iDepthBits<24 then
      iDepthBits=16
    elseif iDepthBits>24 then
      iDepthBits=32
    else
      iDepthBits=24
    end if
    ScreenControl FB.SET_GL_DEPTH_BITS,iDepthBits
  end if
  if iStencilBits then ScreenControl FB.SET_GL_STENCIL_BITS,8
  if iAccumBits   then ScreenControl FB.SET_GL_ACCUM_BITS,8
  ScreenRes  iWidth,iHeight,iPixelBits,,FB.GFX_OPENGL
  #ifndef __FB_WIN32__
    m_Display  = glXGetCurrentDisplay()
    m_Drawable = glXGetCurrentDrawable()
  #endif
  m_InitExtensions
end constructor

sub GL_GFX.m_InitExtensions
  if ScreenPtr()=0 then return
  #ifdef __FB_WIN32__
  ScreenControl FB.GET_GL_EXTENSIONS,m_sExtensions
  #else
  m_sExtensions = *glXQueryExtensionsString(m_Display,0)
  #endif
  if instr(m_sExtensions,SWAP_EXT) then
    m_SwapIntervalExt = ScreenGLProc(SWAP_PROC)
  end if
end sub

sub GL_GFX.SwapInterval(bOnOff as integer)
  if m_SwapIntervalEXT=0 then return
  dim as integer iValue=iif(bOnOff,1,0)
  #ifdef __FB_WIN32__
  m_SwapIntervalEXT(iValue)
  #else
  if m_Display andalso m_Drawable then
    m_SwapIntervalEXT(m_Display,m_Drawable,iValue)
  end if
  #endif
end sub

dim as GL_GFX glGfx



dim as double fps=60,t,t2,t1=timer()
dim as integer frames
dim as single r

glGfx.SwapInterval 0 ' off
'glGfx.SwapInterval 1 ' on

while inkey=""
  glClearColor r,0,0,1
  glClear(GL_COLOR_BUFFER_BIT)
  frames+=1
  flip
 
  if frames mod 100=0 then
    t2=timer()
    fps=100/(t2-t1)
    t1=t2
  end if
  r=(1+sin(t))*0.5:t+=1/fps
  if frames mod int(fps)=0 then  WindowTitle "fps: " & int(fps) 
wend

Return to “Tips and Tricks”

Who is online

Users browsing this forum: No registered users and 1 guest