diff examples/E04_input.nim @ 145:a4e6e76128e6

add: upgrade all simple examples to new engine version
author Sam <sam@basx.dev>
date Wed, 26 Apr 2023 02:15:43 +0700
parents 8ce634aa6ea6
children 253dd797e719
line wrap: on
line diff
--- a/examples/E04_input.nim	Tue Apr 25 18:32:23 2023 +0700
+++ b/examples/E04_input.nim	Wed Apr 26 02:15:43 2023 +0700
@@ -56,6 +56,7 @@
     CtrlL, SuperL, AltL, Space, AltR, SuperR, CtrlR, Left, Down, Right
   ]
 
+# build keyboard and cursor meshes
 var
   scene: Entity
   keyvertexpos: seq[Vec3f]
@@ -91,7 +92,6 @@
   firstRow = false
 
 
-
 when isMainModule:
   var myengine = initEngine("Input")
 
@@ -106,41 +106,40 @@
   var positions = arrow
   for i in 0 ..< positions.len:
     positions[i] = cursorscale * newVec3f(positions[i].x, positions[i].y)
-  # cursor
-  var cursormesh = newMesh(
-    positions=positions,
-    colors=arrow_colors,
-    instanceCount=1,
-  )
 
-  # keyboard
-  var keyboardmesh = newMesh(
-    positions=keyvertexpos,
-    colors=keyvertexcolor,
-    indices=keymeshindices
-  )
-
-  # background
-  var backgroundmesh = newMesh(
-    positions= @[
-      newVec3f(0'f32, 0'f32),
-      newVec3f(1'f32, 0'f32),
-      newVec3f(1'f32, 1'f32),
-      newVec3f(0'f32, 1'f32),
-    ],
-    colors= @[
-      backgroundColor,
-      backgroundColor,
-      backgroundColor,
-      backgroundColor,
-    ],
-    indices= @[[0'u16, 1'u16, 2'u16], [2'u16, 3'u16, 0'u16]],
-  )
+  # define mesh objects
+  var
+    cursormesh = newMesh(
+      positions=positions,
+      colors=arrow_colors,
+      instanceCount=1,
+    )
+    keyboardmesh = newMesh(
+      positions=keyvertexpos,
+      colors=keyvertexcolor,
+      indices=keymeshindices
+    )
+    backgroundmesh = newMesh(
+      positions= @[
+        newVec3f(0'f32, 0'f32),
+        newVec3f(1'f32, 0'f32),
+        newVec3f(1'f32, 1'f32),
+        newVec3f(0'f32, 1'f32),
+      ],
+      colors= @[
+        backgroundColor,
+        backgroundColor,
+        backgroundColor,
+        backgroundColor,
+      ],
+      indices= @[[0'u16, 1'u16, 2'u16], [2'u16, 3'u16, 0'u16]],
+    )
 
   backgroundmesh.setInstanceData("transform", @[Unit4f32])
   keyboardmesh.setInstanceData("transform", @[Unit4f32])
   cursormesh.setInstanceData("transform", @[Unit4f32])
 
+  # define mesh objects
   scene = newEntity("scene")
   scene.add newEntity("background", backgroundmesh)
   let keyboard = newEntity("keyboard", keyboardmesh)
@@ -152,6 +151,7 @@
   scene.add newEntity("keyboard-center", keyboard)
   scene.add newEntity("cursor", cursormesh)
 
+  # shaders
   const
     vertexInput = @[
       attr[Vec3f]("position", memoryLocation=VRAM),
@@ -175,24 +175,29 @@
       outputs=fragOutput,
       main="color = vec4(outcolor, 1);"
     )
+
+  # set up rendering
   myengine.setRenderer(myengine.gpuDevice.simpleForwardRenderPass(vertexCode, fragmentCode, clearColor=newVec4f(0, 0, 0.5)))
   myengine.addScene(scene, vertexInput, transformAttribute="transform")
   var projection = initShaderGlobal("projection", Unit4f32)
   scene.add projection
-  while myengine.running and not myengine.keyWasPressed(Escape):
-    myengine.updateInputs()
-    setValue[Mat4](projection.value, ortho[float32](
-      0'f32, float32(myengine.getWindow().size[0]),
-      0'f32, float32(myengine.getWindow().size[1]),
-      0'f32, 1'f32,
-    ))
-    let
-      mousePos = translate3d(myengine.mousePosition().x + 20, myengine.mousePosition().y + 20, 0'f32)
-      winsize = myengine.getWindow().size
-      center = translate3d(float32(winsize[0]) / 2'f32, float32(winsize[1]) / 2'f32, 0.1'f32)
+
+  # mainloop
+  while myengine.updateInputs() == Running:
+    if myengine.windowWasResized():
+      setValue[Mat4](projection.value, ortho[float32](
+        0'f32, float32(myengine.getWindow().size[0]),
+        0'f32, float32(myengine.getWindow().size[1]),
+        0'f32, 1'f32,
+      ))
+      let
+        winsize = myengine.getWindow().size
+        center = translate3d(float32(winsize[0]) / 2'f32, float32(winsize[1]) / 2'f32, 0.1'f32)
+      scene.firstWithName("keyboard-center").transform = center
+      scene.firstWithName("background").transform = scale3d(float32(winsize[0]), float32(winsize[1]), 1'f32)
+
+    let mousePos = translate3d(myengine.mousePosition().x + 20, myengine.mousePosition().y + 20, 0'f32)
     scene.firstWithName("cursor").transform = mousePos
-    scene.firstWithName("keyboard-center").transform = center
-    scene.firstWithName("background").transform = scale3d(float32(winsize[0]), float32(winsize[1]), 1'f32)
 
     var mesh = Mesh(scene.firstWithName("keyboard").components[0])
     for (index, key) in enumerate(keyIndices):