comparison examples/E04_input.nim @ 203:84fd522fdf3f

did: update examples to use new API for scene + scene globals
author Sam <sam@basx.dev>
date Mon, 08 May 2023 21:21:49 +0700
parents 25d97fa0ad5c
children f3912838cd69
comparison
equal deleted inserted replaced
202:9bb3fdbecc52 203:84fd522fdf3f
1 import std/enumerate 1 import std/enumerate
2 import std/strutils
3 import std/typetraits 2 import std/typetraits
4 import std/times
5 import std/math 3 import std/math
6 4
7 import semicongine 5 import semicongine
8 6
9 const 7 const
56 CtrlL, SuperL, AltL, Space, AltR, SuperR, CtrlR, Left, Down, Right 54 CtrlL, SuperL, AltL, Space, AltR, SuperR, CtrlR, Left, Down, Right
57 ] 55 ]
58 56
59 # build keyboard and cursor meshes 57 # build keyboard and cursor meshes
60 var 58 var
61 scene: Entity 59 scene: Scene
62 keyvertexpos: seq[Vec3f] 60 keyvertexpos: seq[Vec3f]
63 keyvertexcolor: seq[Vec3f] 61 keyvertexcolor: seq[Vec3f]
64 keymeshindices: seq[array[3, uint16]] 62 keymeshindices: seq[array[3, uint16]]
65 rowpos = newVec2f(0, 0) 63 rowpos = newVec2f(0, 0)
66 i = 0'u16 64 i = 0'u16
134 ], 132 ],
135 indices= @[[0'u16, 1'u16, 2'u16], [2'u16, 3'u16, 0'u16]], 133 indices= @[[0'u16, 1'u16, 2'u16], [2'u16, 3'u16, 0'u16]],
136 ) 134 )
137 135
138 # define mesh objects 136 # define mesh objects
139 scene = newEntity("scene") 137 scene = newScene("scene", newEntity("scene"))
140 scene.add newEntity("background", backgroundmesh) 138 scene.root.add newEntity("background", backgroundmesh)
141 let keyboard = newEntity("keyboard", keyboardmesh) 139 let keyboard = newEntity("keyboard", keyboardmesh)
142 keyboard.transform = translate3d( 140 keyboard.transform = translate3d(
143 -float32(rowWidth) / 2'f32, 141 -float32(rowWidth) / 2'f32,
144 -float32(tupleLen(keyRows) * (keyDimension + keyGap) - keyGap) / 2'f32, 142 -float32(tupleLen(keyRows) * (keyDimension + keyGap) - keyGap) / 2'f32,
145 0'f32 143 0'f32
146 ) 144 )
147 scene.add newEntity("keyboard-center", keyboard) 145 scene.root.add newEntity("keyboard-center", keyboard)
148 scene.add newEntity("cursor", cursormesh) 146 scene.root.add newEntity("cursor", cursormesh)
149 147
150 # shaders 148 # shaders
151 const 149 const
152 vertexInput = @[ 150 vertexInput = @[
153 attr[Vec3f]("position"), 151 attr[Vec3f]("position"),
173 ) 171 )
174 172
175 # set up rendering 173 # set up rendering
176 myengine.setRenderer(myengine.gpuDevice.simpleForwardRenderPass(vertexCode, fragmentCode, clearColor=newVec4f(0, 0, 0.5))) 174 myengine.setRenderer(myengine.gpuDevice.simpleForwardRenderPass(vertexCode, fragmentCode, clearColor=newVec4f(0, 0, 0.5)))
177 myengine.addScene(scene, vertexInput, transformAttribute="transform") 175 myengine.addScene(scene, vertexInput, transformAttribute="transform")
178 var projection = initShaderGlobal("projection", Unit4f32) 176 scene.addShaderGlobal("projection", Unit4f32)
179 scene.add projection
180 177
181 # mainloop 178 # mainloop
182 while myengine.updateInputs() == Running: 179 while myengine.updateInputs() == Running:
183 if myengine.windowWasResized(): 180 if myengine.windowWasResized():
184 setValue[Mat4](projection.value, ortho[float32]( 181 setShaderGlobal(scene, "projection",
185 0'f32, float32(myengine.getWindow().size[0]), 182 ortho[float32](
186 0'f32, float32(myengine.getWindow().size[1]), 183 0'f32, float32(myengine.getWindow().size[0]),
187 0'f32, 1'f32, 184 0'f32, float32(myengine.getWindow().size[1]),
188 )) 185 0'f32, 1'f32,
186 )
187 )
189 let 188 let
190 winsize = myengine.getWindow().size 189 winsize = myengine.getWindow().size
191 center = translate3d(float32(winsize[0]) / 2'f32, float32(winsize[1]) / 2'f32, 0.1'f32) 190 center = translate3d(float32(winsize[0]) / 2'f32, float32(winsize[1]) / 2'f32, 0.1'f32)
192 scene.firstWithName("keyboard-center").transform = center 191 scene.root.firstWithName("keyboard-center").transform = center
193 scene.firstWithName("background").transform = scale3d(float32(winsize[0]), float32(winsize[1]), 1'f32) 192 scene.root.firstWithName("background").transform = scale3d(float32(winsize[0]), float32(winsize[1]), 1'f32)
194 193
195 let mousePos = translate3d(myengine.mousePosition().x + 20, myengine.mousePosition().y + 20, 0'f32) 194 let mousePos = translate3d(myengine.mousePosition().x + 20, myengine.mousePosition().y + 20, 0'f32)
196 scene.firstWithName("cursor").transform = mousePos 195 scene.root.firstWithName("cursor").transform = mousePos
197 196
198 var mesh = Mesh(scene.firstWithName("keyboard").components[0]) 197 var mesh = Mesh(scene.root.firstWithName("keyboard").components[0])
199 for (index, key) in enumerate(keyIndices): 198 for (index, key) in enumerate(keyIndices):
200 if myengine.keyWasPressed(key): 199 if myengine.keyWasPressed(key):
201 let baseIndex = uint32(index * 4) 200 let baseIndex = uint32(index * 4)
202 mesh.updateMeshData("color", baseIndex + 0, activeColor) 201 mesh.updateMeshData("color", baseIndex + 0, activeColor)
203 mesh.updateMeshData("color", baseIndex + 1, activeColor) 202 mesh.updateMeshData("color", baseIndex + 1, activeColor)