changeset 626:bf2f4a9cd962

add: option to show/hide cursor
author Sam <sam@basx.dev>
date Sat, 29 Apr 2023 21:38:52 +0700
parents c48ceb622b27
children 34e536f7001f
files examples/E10_pong.nim semicongine.nimble src/semicongine/engine.nim src/semicongine/platform/linux/window.nim src/semicongine/platform/windows/window.nim
diffstat 5 files changed, 25 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/examples/E10_pong.nim	Sat Apr 29 05:40:11 2023 -0700
+++ b/examples/E10_pong.nim	Sat Apr 29 21:38:52 2023 +0700
@@ -66,7 +66,15 @@
     height = float32(winsize[1]) / float32(winsize[0])
     width = 1'f
     currentTime = cpuTime()
+    showSystemCursor = true
   while myengine.updateInputs() == Running and not myengine.keyWasPressed(Escape):
+    if myengine.keyWasPressed(C):
+      if showSystemCursor:
+        myengine.hideSystemCursor()
+      else:
+        myengine.showSystemCursor()
+      showSystemCursor = not showSystemCursor
+
     let dt: float32 = cpuTime() - currentTime
     currentTime = cpuTime()
     if myengine.windowWasResized():
--- a/semicongine.nimble	Sat Apr 29 05:40:11 2023 -0700
+++ b/semicongine.nimble	Sat Apr 29 21:38:52 2023 +0700
@@ -11,3 +11,5 @@
 
 requires "nim >= 1.6.10"
 requires "winim"
+requires "xlib"
+requires "alsa"
--- a/src/semicongine/engine.nim	Sat Apr 29 05:40:11 2023 -0700
+++ b/src/semicongine/engine.nim	Sat Apr 29 21:38:52 2023 +0700
@@ -152,6 +152,7 @@
     engine.resizeHandler(engine)
   return engine.state
 
+# wrappers for internal things
 func keyIsDown*(engine: Engine, key: Key): auto = key in engine.input.keyIsDown
 func keyWasPressed*(engine: Engine, key: Key): auto = key in engine.input.keyWasPressed
 func keyWasReleased*(engine: Engine, key: Key): auto = key in engine.input.keyWasReleased
@@ -164,3 +165,5 @@
 func gpuDevice*(engine: Engine): Device = engine.device
 func getWindow*(engine: Engine): auto = engine.window
 func windowWasResized*(engine: Engine): auto = engine.input.windowWasResized
+func showSystemCursor*(engine: Engine) = engine.window.showSystemCursor()
+func hideSystemCursor*(engine: Engine) = engine.window.hideSystemCursor()
--- a/src/semicongine/platform/linux/window.nim	Sat Apr 29 05:40:11 2023 -0700
+++ b/src/semicongine/platform/linux/window.nim	Sat Apr 29 21:38:52 2023 +0700
@@ -54,13 +54,15 @@
   var data = "\0".cstring
   var pixmap = XCreateBitmapFromData(display, window, data, 1, 1)
   var color: XColor
-  var empty_cursor = XCreatePixmapCursor(display, pixmap, pixmap, addr(color),
-      addr(color), 0, 0)
+  var empty_cursor = XCreatePixmapCursor(display, pixmap, pixmap, addr(color), addr(color), 0, 0)
   checkXlibResult XFreePixmap(display, pixmap)
-  checkXlibResult XDefineCursor(display, window, empty_cursor)
+  return NativeWindow(display: display, window: window, emptyCursor: empty_cursor)
 
-  return NativeWindow(display: display, window: window,
-      emptyCursor: empty_cursor)
+proc hideSystemCursor*(window: NativeWindow) =
+  checkXlibResult XDefineCursor(window.display, window.window, window.emptyCursor)
+
+proc showSystemCursor*(window: NativeWindow) =
+  checkXlibResult XUndefineCursor(window.display, window.window)
 
 proc destroy*(window: NativeWindow) =
   checkXlibResult window.display.XFreeCursor(window.emptyCursor)
--- a/src/semicongine/platform/windows/window.nim	Sat Apr 29 05:40:11 2023 -0700
+++ b/src/semicongine/platform/windows/window.nim	Sat Apr 29 21:38:52 2023 +0700
@@ -86,8 +86,13 @@
     )
 
   discard ShowWindow(result.hwnd, SW_SHOW)
+
+proc hideSystemCursor*(window: NativeWindow) =
   discard ShowCursor(false)
 
+proc showSystemCursor*(window: NativeWindow) =
+  discard ShowCursor(true)
+
 proc destroy*(window: NativeWindow) =
   discard