changeset 238:ef8cea2545f8

did: make material working halfway, add some more mouse functionality
author Sam <sam@basx.dev>
date Sat, 20 May 2023 23:16:48 +0700
parents 2fca78b0c0d6
children f05497ef8fd2
files src/semicongine/engine.nim src/semicongine/events.nim src/semicongine/platform/linux/window.nim src/semicongine/renderer.nim src/semicongine/resources.nim
diffstat 5 files changed, 39 insertions(+), 19 deletions(-) [+]
line wrap: on
line diff
--- a/src/semicongine/engine.nim	Sat May 20 18:17:21 2023 +0700
+++ b/src/semicongine/engine.nim	Sat May 20 23:16:48 2023 +0700
@@ -30,8 +30,10 @@
     mouseWasPressed: set[MouseButton]
     mouseWasReleased: set[MouseButton]
     mousePosition: Vec2f
+    mouseMove: Vec2f
     eventsProcessed: uint64
     windowWasResized: bool
+    mouseWheel: float32
   Engine* = object
     state*: EngineState
     device: Device
@@ -116,10 +118,9 @@
 proc renderScene*(engine: var Engine, scene: var Scene) =
   assert engine.state == Running
   assert engine.renderer.isSome
-  if engine.state == Running:
-    engine.renderer.get.updateMeshData(scene)
-    engine.renderer.get.updateUniformData(scene)
-    engine.renderer.get.render(scene)
+  engine.renderer.get.updateMeshData(scene)
+  engine.renderer.get.updateUniformData(scene)
+  engine.renderer.get.render(scene)
 
 proc updateInputs*(engine: var Engine): EngineState =
   assert engine.state in [Starting, Running]
@@ -128,7 +129,13 @@
   engine.input.keyWasReleased = {}
   engine.input.mouseWasPressed = {}
   engine.input.mouseWasReleased = {}
-  engine.input.windowWasResized = engine.state == Starting
+  engine.input.mouseWheel = 0
+  engine.input.mouseMove = newVec2f()
+  if engine.state == Starting:
+    engine.input.windowWasResized = true
+    var mpos = engine.window.getMousePosition()
+    if mpos.isSome:
+      engine.input.mousePosition = mpos.get
 
   var killed = false
   for event in engine.window.pendingEvents():
@@ -153,7 +160,11 @@
         engine.input.mouseWasReleased.incl event.button
         engine.input.mouseIsDown.excl event.button
       of MouseMoved:
-        engine.input.mousePosition = newVec2(float32(event.x), float32(event.y))
+        let newPos = newVec2(float32(event.x), float32(event.y))
+        engine.input.mouseMove = newPos - engine.input.mousePosition
+        engine.input.mousePosition = newPos
+      of MouseWheel:
+        engine.input.mouseWheel = event.amount
   if engine.state == Starting:
     engine.state = Running
   if killed:
@@ -172,6 +183,8 @@
 func mouseWasPressed*(engine: Engine, button: MouseButton): auto = button in engine.input.mouseWasPressed
 func mouseWasReleased*(engine: Engine, button: MouseButton): auto = button in engine.input.mouseWasReleased
 func mousePosition*(engine: Engine): auto = engine.input.mousePosition
+func mouseMove*(engine: Engine): auto = engine.input.mouseMove
+func mouseWheel*(engine: Engine): auto = engine.input.mouseWheel
 func eventsProcessed*(engine: Engine): auto = engine.input.eventsProcessed
 func framesRendered*(engine: Engine): uint64 = (if engine.renderer.isSome: engine.renderer.get.framesRendered else: 0)
 func gpuDevice*(engine: Engine): Device = engine.device
--- a/src/semicongine/events.nim	Sat May 20 18:17:21 2023 +0700
+++ b/src/semicongine/events.nim	Sat May 20 23:16:48 2023 +0700
@@ -4,6 +4,7 @@
     ResizedWindow
     KeyPressed, KeyReleased
     MousePressed, MouseReleased, MouseMoved,
+    MouseWheel
   Key* {.size: sizeof(cint), pure.} = enum
     UNKNOWN
     Escape, F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12
@@ -28,5 +29,7 @@
       button*: MouseButton
     of MouseMoved:
       x*, y*: int
+    of MouseWheel:
+      amount*: float32
     else:
       discard
--- a/src/semicongine/platform/linux/window.nim	Sat May 20 18:17:21 2023 +0700
+++ b/src/semicongine/platform/linux/window.nim	Sat May 20 23:16:48 2023 +0700
@@ -5,8 +5,8 @@
 import
   x11/xlib,
   x11/xutil,
-  x11/keysym
-import x11/x
+  x11/keysym,
+  x11/x
 
 import ../../core
 import ../../events
@@ -29,7 +29,6 @@
     raise newException(Exception, "Xlib error: " & astToStr(call) &
         " returned " & $value)
 
-
 proc XErrorLogger(display: PDisplay, event: PXErrorEvent): cint {.cdecl.} =
   echo &"Xlib: {event[]}"
 
@@ -157,12 +156,15 @@
           key: KeyTypeMap.getOrDefault(xkey, Key.UNKNOWN))
     of ButtonPress:
       let button = int(cast[PXButtonEvent](addr(event)).button)
-      result.add Event(eventType: MousePressed,
-          button: MouseButtonTypeMap.getOrDefault(button, MouseButton.UNKNOWN))
+      if button == Button4:
+        result.add Event(eventType: MouseWheel, amount: 1'f32)
+      elif button == Button5:
+        result.add Event(eventType: MouseWheel, amount: -1'f32)
+      else:
+        result.add Event(eventType: MousePressed, button: MouseButtonTypeMap.getOrDefault(button, MouseButton.UNKNOWN))
     of ButtonRelease:
       let button = int(cast[PXButtonEvent](addr(event)).button)
-      result.add Event(eventType: MouseReleased,
-          button: MouseButtonTypeMap.getOrDefault(button, MouseButton.UNKNOWN))
+      result.add Event(eventType: MouseReleased, button: MouseButtonTypeMap.getOrDefault(button, MouseButton.UNKNOWN))
     of MotionNotify:
       let motion = cast[PXMotionEvent](addr(event))
       result.add Event(eventType: MouseMoved, x: motion.x, y: motion.y)
--- a/src/semicongine/renderer.nim	Sat May 20 18:17:21 2023 +0700
+++ b/src/semicongine/renderer.nim	Sat May 20 23:16:48 2023 +0700
@@ -180,6 +180,7 @@
         let interpolation = images[1]
         for image in images[0]:
           data.textures[name].add renderer.device.createTexture(image.width, image.height, 4, addr image.imagedata[0][0], interpolation)
+      # TODO: this only bind shit to the last scene, find per-scene solution
       pipeline.setupDescriptors(data.uniformBuffers, data.textures, inFlightFrames=renderer.swapchain.inFlightFrames)
       for frame_i in 0 ..< renderer.swapchain.inFlightFrames:
         pipeline.descriptorSets[frame_i].writeDescriptorSet()
@@ -217,10 +218,9 @@
 proc updateUniformData*(renderer: var Renderer, scene: var Scene) =
   assert scene in renderer.scenedata
 
-  var data = renderer.scenedata[scene]
-  if data.uniformBuffers.len == 0:
+  if renderer.scenedata[scene].uniformBuffers.len == 0:
     return
-  assert data.uniformBuffers[renderer.swapchain.currentInFlight].vk.valid
+  assert renderer.scenedata[scene].uniformBuffers[renderer.swapchain.currentInFlight].vk.valid
 
   for i in 0 ..< renderer.renderPass.subpasses.len:
     for pipeline in renderer.renderPass.subpasses[i].pipelines.mitems:
@@ -228,7 +228,7 @@
       for uniform in pipeline.uniforms:
         assert uniform.thetype == scene.shaderGlobals[uniform.name].thetype
         let (pdata, size) = scene.shaderGlobals[uniform.name].getRawData()
-        data.uniformBuffers[renderer.swapchain.currentInFlight].setData(pdata, size, offset)
+        renderer.scenedata[scene].uniformBuffers[renderer.swapchain.currentInFlight].setData(pdata, size, offset)
         offset += size
 
 proc render*(renderer: var Renderer, scene: var Scene) =
--- a/src/semicongine/resources.nim	Sat May 20 18:17:21 2023 +0700
+++ b/src/semicongine/resources.nim	Sat May 20 23:16:48 2023 +0700
@@ -121,8 +121,10 @@
 proc loadMesh*(path: string): Entity =
   loadResource_intern(path).readglTF()[0].root
 
-proc loadScene*(path: string): Scene =
-  loadResource_intern(path).readglTF()[0]
+proc loadScene*(path: string, name=""): Scene =
+  result = loadResource_intern(path).readglTF()[0]
+  if name != "":
+    result.name = name
 
 proc loadScenes*(path: string): seq[Scene] =
   loadResource_intern(path).readglTF()