Skip to content

Commit

Permalink
add: vsync
Browse files Browse the repository at this point in the history
  • Loading branch information
levovix0 committed Jul 24, 2022
1 parent 3013a3b commit 2792e06
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
21 changes: 19 additions & 2 deletions src/siwin/window.nim
Original file line number Diff line number Diff line change
Expand Up @@ -593,8 +593,6 @@ when defined(linux):
# 32,
# @[this.xSyncCounter].asString
# )

# todo: enable vsync

# set window VM class (can be used by window managers)
block vmHint:
Expand Down Expand Up @@ -940,6 +938,17 @@ when defined(linux):
proc makeCurrent*(window: OpenglWindow) =
window.xwin.makeCurrent window.ctx

proc `vsync=`*(window: OpenglWindow, v: bool, silent = false) =
if glxSwapIntervalExt != nil:
display.glxSwapIntervalExt(window.xwin, if v: 1 else: 0)
elif glxSwapIntervalMesa != nil:
glxSwapIntervalMesa(if v: 1 else: 0)
elif glxSwapIntervalSgi != nil:
glxSwapIntervalSgi(if v: 1 else: 0)
else:
if not silent:
raise OSError.newException("VSync is not supported")


proc run*(this: Window, makeVisible = true) =
## run main loop of window
Expand Down Expand Up @@ -1494,6 +1503,12 @@ elif defined(windows):
proc makeCurrent*(window: OpenglWindow) =
doassert window.hdc.wglMakeCurrent(window.ctx)

proc `vsync=`*(window: OpenglWindow, v: bool, silent = false) =
if wglSwapIntervalExt == nil:
wglSwapIntervalExt = cast[typeof wglSwapIntervalExt](wglGetProcAddress("wglSwapIntervalEXT"))
if wglSwapIntervalExt == nil or wglSwapIntervalExt(if v: 1 else: 0) == 0:
if not silent:
raise OSError.newException("failed to " & (if v: "enable" else: "disable") & " vsync")

method displayImpl(this: Window) {.base.} =
var ps: PaintStruct
Expand Down Expand Up @@ -1692,6 +1707,7 @@ proc newOpenglWindow*(
fullscreen = false,
frameless = false,
transparent = false,
vsync = true,

class = "", # window class (used in x11), equals to title if not specified
): OpenglWindow =
Expand All @@ -1701,3 +1717,4 @@ proc newOpenglWindow*(
else:
result.initOpenglWindow(size, screen, fullscreen, frameless, transparent)
result.title = title
result.`vsync=`(vsync, silent=true)
7 changes: 6 additions & 1 deletion src/siwin/wrappers/glx.nim
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import macros, unicode, strutils, sequtils
import macros, unicode, strutils, sequtils, dynlib
import x

type
Expand Down Expand Up @@ -44,3 +44,8 @@ proc glxCurrentContext*(): GlxContext {.glx: "getCurrentContext".}
proc glxSwapBuffers*(d: Drawable) =
proc impl(dpy: PDisplay, drawable: Drawable) {.glx: "swapBuffers".}
display.impl(d)

let lib = loadLib dllname
let glxSwapIntervalExt* = cast[proc(d: ptr Display, drawable: Drawable, interval: cint) {.stdcall.}](lib.symAddr("glXSwapIntervalEXT"))
let glxSwapIntervalMesa* = cast[proc(interval: cint) {.stdcall.}](lib.symAddr("glXSwapIntervalMESA"))
let glxSwapIntervalSgi* = cast[proc(interval: cint) {.stdcall.}](lib.symAddr("glXSwapIntervalSGI"))
2 changes: 2 additions & 0 deletions src/siwin/wrappers/winapi.nim
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ type

var hInstance* = GetModuleHandle(nil)

var wglSwapIntervalEXT*: proc(interval: int32): Bool {.stdcall.}

proc trackMouseEvent*(handle: HWnd, e: DWord) =
var ev = TTrackMouseEvent(cbSize: TTrackMouseEvent.sizeof.DWord, dwFlags: e, hwndTrack: handle, dwHoverTime: 0)
TrackMouseEvent(ev.addr)
Expand Down

0 comments on commit 2792e06

Please sign in to comment.