changeset 1157:dd757eb5ca86

fix: minimizing window on windows breaks everything, also: do not try to acquire next frame for inifinity
author sam@sambook-windows.localdomain
date Sat, 15 Jun 2024 18:17:31 +0700
parents c43d4e62b89b
children f32359ffd882
files semicongine/engine.nim semicongine/events.nim semicongine/input.nim semicongine/platform/windows/window.nim semicongine/renderer.nim
diffstat 5 files changed, 27 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/semicongine/engine.nim	Sat Jun 15 17:16:06 2024 +0700
+++ b/semicongine/engine.nim	Sat Jun 15 18:17:31 2024 +0700
@@ -154,15 +154,17 @@
   engine.renderer.get.Destroy(scene)
 
 proc RenderScene*(engine: var Engine, scene: var Scene) =
+  if WindowIsMinimized():
+    return
   assert engine.renderer.isSome, "Renderer has not yet been initialized, call 'engine.InitRenderer' first"
   assert engine.renderer.get.HasScene(scene), &"Scene '{scene.name}' has not been loaded yet"
   let t0 = getMonoTime()
 
-  engine.renderer.get.StartNewFrame()
-  scene.SetShaderGlobal(ASPECT_RATIO_ATTRIBUTE, engine.GetAspectRatio)
-  engine.renderer.get.UpdateMeshData(scene)
-  engine.renderer.get.UpdateUniformData(scene)
-  engine.renderer.get.Render(scene)
+  if engine.renderer.get.StartNewFrame():
+    scene.SetShaderGlobal(ASPECT_RATIO_ATTRIBUTE, engine.GetAspectRatio)
+    engine.renderer.get.UpdateMeshData(scene)
+    engine.renderer.get.UpdateUniformData(scene)
+    engine.renderer.get.Render(scene)
 
   if engine.showFps:
     let nanoSecs = getMonoTime().ticks - t0.ticks
--- a/semicongine/events.nim	Sat Jun 15 17:16:06 2024 +0700
+++ b/semicongine/events.nim	Sat Jun 15 18:17:31 2024 +0700
@@ -1,7 +1,7 @@
 type
   EventType* = enum
     Quit
-    ResizedWindow
+    ResizedWindow, MinimizedWindow, RestoredWindow
     KeyPressed, KeyReleased
     MousePressed, MouseReleased, MouseMoved,
     MouseWheel
--- a/semicongine/input.nim	Sat Jun 15 17:16:06 2024 +0700
+++ b/semicongine/input.nim	Sat Jun 15 18:17:31 2024 +0700
@@ -21,6 +21,7 @@
     mouseMove: Vec2f
     mouseWheel: float32
     windowWasResized: bool = true
+    windowIsMinimized: bool = false
 
 # warning, shit is not thread safe
 var input: Input
@@ -60,6 +61,11 @@
         input.mousePosition = newPos
       of MouseWheel:
         input.mouseWheel = event.amount
+      of MinimizedWindow:
+        input.windowIsMinimized = true
+      of RestoredWindow:
+        input.windowIsMinimized = false
+
   return not killed
 
 proc KeyIsDown*(key: Key): bool = key in input.keyIsDown
@@ -80,6 +86,7 @@
 proc MouseMove*(): auto = input.mouseMove
 proc MouseWheel*(): auto = input.mouseWheel
 proc WindowWasResized*(): auto = input.windowWasResized
+proc WindowIsMinimized*(): auto = input.windowIsMinimized
 
 # actions as a slight abstraction over raw input
 
--- a/semicongine/platform/windows/window.nim	Sat Jun 15 17:16:06 2024 +0700
+++ b/semicongine/platform/windows/window.nim	Sat Jun 15 18:17:31 2024 +0700
@@ -66,6 +66,13 @@
     currentEvents.add(Event(eventType: events.MouseMoved, x: GET_X_LPARAM(lParam), y: GET_Y_LPARAM(lParam)))
   of WM_MOUSEWHEEL:
     currentEvents.add(Event(eventType: events.MouseWheel, amount: float32(GET_WHEEL_DELTA_WPARAM(wParam)) / WHEEL_DELTA))
+  of WM_SIZING:
+    currentEvents.add(Event(eventType: ResizedWindow))
+  of WM_SIZE:
+    if wParam == SIZE_MINIMIZED:
+      currentEvents.add(Event(eventType: MinimizedWindow))
+    elif wParam == SIZE_RESTORED:
+      currentEvents.add(Event(eventType: RestoredWindow))
   of WM_SETCURSOR:
     if LOWORD(lParam) == HTCLIENT:
       SetCursor(currentCursor)
--- a/semicongine/renderer.nim	Sat Jun 15 17:16:06 2024 +0700
+++ b/semicongine/renderer.nim	Sat Jun 15 18:17:31 2024 +0700
@@ -369,9 +369,8 @@
         offset += uniform.Size
   scene.ClearDirtyShaderGlobals()
 
-proc StartNewFrame*(renderer: var Renderer) =
-  # TODO: chance for an infinity-loop?
-  while not renderer.swapchain.AcquireNextFrame():
+proc StartNewFrame*(renderer: var Renderer): bool =
+  if not renderer.swapchain.AcquireNextFrame():
     checkVkResult renderer.device.vk.vkDeviceWaitIdle()
     let res = renderer.swapchain.Recreate()
     if res.isSome:
@@ -379,7 +378,10 @@
       renderer.swapchain = res.get()
       checkVkResult renderer.device.vk.vkDeviceWaitIdle()
       oldSwapchain.Destroy()
+    else:
+      return false
   renderer.nextFrameReady = true
+  return true
 
 proc Render*(renderer: var Renderer, scene: Scene) =
   assert scene in renderer.scenedata