Page 1 of 1

Is possible wait for vertical blanking on windows?

Posted: Sun Mar 15, 2020 12:18 pm
by Caronte3D
I'm an old school coder (Amiga rulez! 8) ) so... my question is:
Exists a vertical blank variable accessible to the user while in normal Windows (windowed)?

Re: Is possible wait for vertical blanking on windows?

Posted: Sun Mar 15, 2020 12:55 pm
by Mijikai
Caronte3D wrote:I'm an old school coder (Amiga rulez! 8) ) so... my question is:
Exists a vertical blank variable accessible to the user while in normal Windows (windowed)?
U can use OpenWindowScreen() with either #PB_Screen_WaitSynchronization or #PB_Screen_SmartSynchronization.
(Opengl (if used directly) also has functions to enable vSync -> ex. wglSwapIntervalEXT() on Windows.)
Anyhow you will need additional timing on top of that since monitors may have different refresh rates.

Re: Is possible wait for vertical blanking on windows?

Posted: Sun Mar 15, 2020 1:48 pm
by Caronte3D
Thanks, Mijikai, but I hope (workaround?) something to use with "OpenWindow" can be done :?

Re: Is possible wait for vertical blanking on windows?

Posted: Sun Mar 15, 2020 2:18 pm
by Mijikai
The other option would be a timer to render at fixed intervals.
Tearing is not really a problem, its not uncommon to see games that have options to disable vSync.

Edit: Just had an idea (old ddraw!) and it worked - have fun :)

Code:

Code: Select all

;TINY VSYNC (DDRAW)
;AUTHOR: MIJIKAI

EnableExplicit

Procedure.i WaitForVerticalBlank();wait for vsync! / returns 1 on sucess 0 on failure / note: busy wait!
  Static *ddraw.IDirectDraw
  If Not *ddraw
    If DirectDrawCreate_(#Null,@*ddraw,#Null) = #Null
      If *ddraw\WaitForVerticalBlank($4,#Null) = #Null
        ProcedureReturn 1  
      Else
        ProcedureReturn 0
      EndIf
    Else
      ProcedureReturn 0
    EndIf
  Else
    If *ddraw\WaitForVerticalBlank($4,#Null) = #Null
      ProcedureReturn 1  
    Else
      ProcedureReturn 0
    EndIf
  EndIf
EndProcedure

Procedure.i Demo(Title.s = #Null$,Width.i = 600,Height.i = 400)
  Protected wnd.i
  Protected wnd_flags.i
  Protected wnd_event.i
  Protected wnd_exit.i
  Protected sync.i
  Protected clock.i
  wnd_flags = #PB_Window_SystemMenu|#PB_Window_ScreenCentered|#PB_Window_MinimizeGadget|#PB_Window_MaximizeGadget|#PB_Window_SizeGadget
  wnd = OpenWindow(#PB_Any,#Null,#Null,Width,Height,Title,wnd_flags) 
  If wnd
    WindowBounds(wnd,Width,Height,#PB_Ignore,#PB_Ignore)
    Repeat 
      Repeat
        wnd_event = WindowEvent()
        If wnd_event = #PB_Event_CloseWindow
          wnd_exit = #True
        EndIf
      Until wnd_event = #Null
      ;-------------------------------
      clock = ElapsedMilliseconds()
      sync = WaitForVerticalBlank()
      Debug ElapsedMilliseconds() - clock
      Debug sync
      ;-------------------------------
    Until wnd_exit
    CloseWindow(wnd)
  EndIf
  ProcedureReturn #Null
EndProcedure

Demo("VSYNC")

End
U might want to add some timers and some sort of delay/event to release some cpu time ;)

Re: Is possible wait for vertical blanking on windows?

Posted: Mon Mar 16, 2020 10:56 pm
by djes
Welcome ! I commit this code some years ago : viewtopic.php?f=12&t=36852

Re: Is possible wait for vertical blanking on windows?

Posted: Tue Mar 17, 2020 12:32 pm
by Caronte3D
Thanks both for the code examples, I will take a look and try to insert something like that on my own code :wink: