Is possible wait for vertical blanking on windows?

Just starting out? Need help? Post your questions and find answers here.
User avatar
Caronte3D
Addict
Addict
Posts: 1027
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

Is possible wait for vertical blanking on windows?

Post 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)?
User avatar
Mijikai
Addict
Addict
Posts: 1360
Joined: Sun Sep 11, 2016 2:17 pm

Re: Is possible wait for vertical blanking on windows?

Post 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.
User avatar
Caronte3D
Addict
Addict
Posts: 1027
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

Re: Is possible wait for vertical blanking on windows?

Post by Caronte3D »

Thanks, Mijikai, but I hope (workaround?) something to use with "OpenWindow" can be done :?
User avatar
Mijikai
Addict
Addict
Posts: 1360
Joined: Sun Sep 11, 2016 2:17 pm

Re: Is possible wait for vertical blanking on windows?

Post 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 ;)
User avatar
djes
Addict
Addict
Posts: 1806
Joined: Sat Feb 19, 2005 2:46 pm
Location: Pas-de-Calais, France

Re: Is possible wait for vertical blanking on windows?

Post by djes »

Welcome ! I commit this code some years ago : viewtopic.php?f=12&t=36852
User avatar
Caronte3D
Addict
Addict
Posts: 1027
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

Re: Is possible wait for vertical blanking on windows?

Post 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:
Post Reply