Mercurial > games > semicongine
annotate examples/E04_input.nim @ 530:cfa230e1f3b9
fix: mouse coord, colors
| author | Sam <sam@basx.dev> |
|---|---|
| date | Sun, 05 Feb 2023 00:46:40 +0700 |
| parents | 3ec1be39e1ad |
| children | 5d457f7bb624 |
| rev | line source |
|---|---|
| 525 | 1 import std/tables |
| 527 | 2 import std/enumerate |
|
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
3 import std/strutils |
| 525 | 4 import std/typetraits |
|
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
5 import std/times |
| 525 | 6 import std/math |
|
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
7 |
|
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
8 import semicongine |
|
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
9 |
|
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
10 type |
|
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
11 # define type of vertex |
|
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
12 VertexDataA = object |
| 525 | 13 position: PositionAttribute[Vec3] |
| 14 color: ColorAttribute[Vec4] | |
|
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
15 transform: ModelTransformAttribute |
|
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
16 Uniforms = object |
|
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
17 projection: Descriptor[Mat44] |
| 525 | 18 |
| 19 const | |
| 20 arrow = @[ | |
| 21 Vec3([-1'f32, -1'f32, 0'f32]), | |
| 22 Vec3([1'f32, -1'f32, 0'f32]), | |
| 23 Vec3([-0.3'f32, -0.3'f32, 0'f32]), | |
| 24 Vec3([-0.3'f32, -0.3'f32, 0'f32]), | |
| 25 Vec3([-1'f32, 1'f32, 0'f32]), | |
| 26 Vec3([-1'f32, -1'f32, 0'f32]), | |
| 27 ] | |
| 527 | 28 # keyboard layout, specifying rows with key widths, negative numbers are empty spaces |
| 525 | 29 keyrows = ( |
| 527 | 30 [1.0, -0.6, 1.0, 1.0, 1.0, 1.0, -0.5, 1.0, 1.0, 1.0, 1.0, -0.5, 1.0, 1.0, |
| 31 1.0, 1.0, -0.1, 1.0, 1.0, 1.0], | |
| 32 [1.2, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.8, -0.1, | |
| 33 1.0, 1.0, 1.0], | |
| 34 [1.8, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, -1.5, 1.0, | |
| 35 1.0, 1.0], | |
| 36 [2.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0], | |
| 37 [2.6, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 2.8, -1.3, 1.0], | |
| 38 [1.5, 1.5, 1.5, 6, 1.5, 1.5, -1.2, 1.5, -0.1, 1.0, 1.0, 1.0], | |
| 525 | 39 ) |
| 527 | 40 keyDimension = 50'f32 |
| 525 | 41 keyGap = 10'f32 |
| 530 | 42 backgroundColor = Vec4([0.6705882352941176'f32, 0.6078431372549019'f32, |
| 43 0.5882352941176471'f32, 0'f32]) | |
| 44 baseColor = Vec4([0.9411764705882353'f32, 0.9058823529411765'f32, | |
| 45 0.8470588235294118'f32, 0'f32]) | |
| 46 activeColor = Vec4([0.6509803921568628'f32, 0.22745098039215686'f32, | |
| 47 0.3137254901960784'f32, 0'f32]) | |
| 48 | |
| 49 arrow_colors = @[ | |
| 50 baseColor * 0.9'f32, | |
| 51 baseColor * 0.9'f32, | |
| 52 baseColor * 0.9'f32, | |
| 53 baseColor * 0.8'f32, | |
| 54 baseColor * 0.8'f32, | |
| 55 baseColor * 0.8'f32, | |
| 56 ] | |
| 527 | 57 keyIndices = [ |
| 58 Escape, F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, PrintScreen, | |
| 59 ScrollLock, Pause, | |
| 60 | |
| 61 NumberRowExtra1, `1`, `2`, `3`, `4`, `5`, `6`, `7`, `8`, `9`, `0`, | |
| 62 | |
| 63 NumberRowExtra2, NumberRowExtra3, Backspace, Insert, Home, PageUp, | |
| 64 Tab, Q, W, Key.E, R, T, Key.Y, U, I, O, P, LetterRow1Extra1, | |
| 65 LetterRow1Extra2, Delete, End, PageDown, | |
| 66 | |
| 67 CapsLock, A, S, D, F, G, H, J, K, L, LetterRow2Extra1, LetterRow2Extra2, | |
| 68 LetterRow2Extra3, Enter, | |
| 69 | |
| 70 ShiftL, Key.Z, Key.X, C, V, B, N, M, LetterRow3Extra1, LetterRow3Extra2, | |
| 71 LetterRow3Extra3, ShiftR, Up, | |
| 72 | |
| 73 CtrlL, SuperL, AltL, Space, AltR, SuperR, CtrlR, Left, Down, Right | |
| 74 ] | |
|
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
75 |
|
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
76 var |
|
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
77 pipeline: RenderPipeline[VertexDataA, Uniforms] |
|
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
78 uniforms: Uniforms |
|
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
79 scene: Thing |
| 525 | 80 keyvertexpos: seq[Vec3] |
| 81 keyvertexcolor: seq[Vec4] | |
| 527 | 82 keymeshindices: seq[array[3, uint16]] |
| 525 | 83 rowpos = Vec2([0'f32, 0'f32]) |
| 84 i = 0'u16 | |
| 527 | 85 firstRow = true |
| 86 rowWidth = 0'f32 | |
| 525 | 87 for row in keyrows.fields: |
| 88 for key in row: | |
| 527 | 89 let keySpace = float32(keyDimension * key) |
| 90 if key > 0: | |
| 91 if keyIndices[i div 4] == Enter: | |
| 92 keyvertexpos.add Vec3([rowpos[0], rowpos[1] - keyDimension - keyGap, 0'f32]) | |
| 93 keyvertexpos.add Vec3([rowpos[0] + keySpace, rowpos[1] - keyDimension - | |
| 94 keyGap, 0'f32]) | |
| 95 else: | |
| 96 keyvertexpos.add Vec3([rowpos[0], rowpos[1], 0'f32]) | |
| 97 keyvertexpos.add Vec3([rowpos[0] + keySpace, rowpos[1], 0'f32]) | |
| 98 keyvertexpos.add Vec3([rowpos[0] + keySpace, rowpos[1] + keyDimension, 0'f32]) | |
| 99 keyvertexpos.add Vec3([rowpos[0], rowpos[1] + keyDimension, 0'f32]) | |
| 100 keyvertexcolor.add [baseColor, baseColor, baseColor, baseColor] | |
| 101 keymeshindices.add [i, i + 1, i + 2] | |
| 102 keymeshindices.add [i + 2, i + 3, i] | |
| 103 rowpos[0] += keySpace + keyGap | |
| 104 i += 4 | |
| 105 else: | |
| 106 rowpos[0] += -keySpace + keyGap | |
| 107 if firstRow: | |
| 108 rowWidth = rowpos[0] | |
| 525 | 109 rowpos[0] = 0 |
| 527 | 110 rowpos[1] += keyDimension + keyGap * (if firstRow: 2'f32 else: 1'f32) |
| 111 firstRow = false | |
|
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
112 |
|
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
113 |
|
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
114 proc globalUpdate(engine: var Engine, dt: float32) = |
|
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
115 uniforms.projection.value = ortho[float32]( |
|
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
116 0'f32, float32(engine.vulkan.frameSize.x), |
|
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
117 0'f32, float32(engine.vulkan.frameSize.y), |
|
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
118 0'f32, 1'f32, |
|
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
119 ) |
|
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
120 engine.vulkan.device.updateUniformData(pipeline, uniforms) |
|
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
121 |
| 525 | 122 let |
| 530 | 123 mousePos = translate3d(engine.input.mousePos.x + 20, |
| 124 engine.input.mousePos.y + 20, 0'f32) | |
| 525 | 125 winsize = engine.window.size |
| 126 center = translate3d(float32(winsize[0]) / 2'f32, float32(winsize[1]) / | |
| 127 2'f32, 0.1'f32) | |
| 128 scene.firstWithName("cursor").transform = mousePos | |
| 129 scene.firstWithName("keyboard-center").transform = center | |
| 527 | 130 scene.firstWithName("background").transform = scale3d(float32(winsize[0]), |
| 131 float32(winsize[1]), 0'f32) | |
| 525 | 132 var mesh = Mesh[VertexDataA, uint16](scene.firstWithName("keyboard").parts[0]) |
| 133 var hadUpdate = false | |
| 527 | 134 for (index, key) in enumerate(keyIndices): |
| 525 | 135 if key in engine.input.keysPressed: |
| 136 let baseIndex = index * 4 | |
| 137 mesh.vertexData.color.data[baseIndex + 0] = activeColor | |
| 138 mesh.vertexData.color.data[baseIndex + 1] = activeColor | |
| 139 mesh.vertexData.color.data[baseIndex + 2] = activeColor | |
| 140 mesh.vertexData.color.data[baseIndex + 3] = activeColor | |
| 141 hadUpdate = true | |
| 142 if key in engine.input.keysReleased: | |
| 143 let baseIndex = index * 4 | |
| 144 mesh.vertexData.color.data[baseIndex + 0] = baseColor | |
| 145 mesh.vertexData.color.data[baseIndex + 1] = baseColor | |
| 146 mesh.vertexData.color.data[baseIndex + 2] = baseColor | |
| 147 mesh.vertexData.color.data[baseIndex + 3] = baseColor | |
| 148 hadUpdate = true | |
| 149 if hadUpdate: | |
| 150 engine.vulkan.device.updateVertexData(mesh.vertexData.color) | |
|
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
151 |
|
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
152 |
|
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
153 when isMainModule: |
|
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
154 var myengine = igniteEngine("Input") |
|
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
155 |
| 527 | 156 # cursor |
| 525 | 157 var cursormesh = new Mesh[VertexDataA, uint16] |
|
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
158 cursormesh.vertexData = VertexDataA( |
| 525 | 159 position: PositionAttribute[Vec3](data: arrow, useOnDeviceMemory: true), |
| 160 color: ColorAttribute[Vec4](data: arrow_colors), | |
|
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
161 transform: ModelTransformAttribute(data: @[Unit44]), |
|
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
162 ) |
|
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
163 # transform the cursor a bit to make it look nice |
| 525 | 164 let cursorscale = ( |
| 165 scale2d(20'f32, 20'f32) * | |
| 166 translate2d(1'f32, 1'f32) * | |
| 167 rotate2d(-float32(PI) / 4'f32) * | |
| 168 scale2d(0.5'f32, 1'f32) * | |
| 169 rotate2d(float32(PI) / 4'f32) | |
| 170 ) | |
|
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
171 for i in 0 ..< cursormesh.vertexData.position.data.len: |
|
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
172 let pos = Vec3([cursormesh.vertexData.position.data[i][0], |
| 525 | 173 cursormesh.vertexData.position.data[i][1], 0'f32]) |
| 174 cursormesh.vertexData.position.data[i] = (cursorscale * pos) | |
|
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
175 |
| 527 | 176 # keyboard |
| 525 | 177 var keyboardmesh = new Mesh[VertexDataA, uint16] |
| 178 keyboardmesh.indexed = true | |
| 179 keyboardmesh.vertexData = VertexDataA( | |
| 180 position: PositionAttribute[Vec3](data: keyvertexpos, | |
| 181 useOnDeviceMemory: true), | |
| 182 color: ColorAttribute[Vec4](data: keyvertexcolor), | |
|
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
183 transform: ModelTransformAttribute(data: @[Unit44]), |
|
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
184 ) |
| 527 | 185 keyboardmesh.indices = keymeshindices |
| 186 | |
| 187 # background | |
| 188 var backgroundmesh = new Mesh[VertexDataA, uint16] | |
| 189 backgroundmesh.indexed = true | |
| 190 backgroundmesh.indices = @[[0'u16, 1'u16, 2'u16], [2'u16, 3'u16, 0'u16]] | |
| 191 backgroundmesh.vertexData = VertexDataA( | |
| 192 position: PositionAttribute[Vec3](data: @[ | |
| 193 Vec3([0'f32, 0'f32, 0'f32]), | |
| 194 Vec3([1'f32, 0'f32, 0'f32]), | |
| 195 Vec3([1'f32, 1'f32, 0'f32]), | |
| 196 Vec3([0'f32, 1'f32, 0'f32]), | |
| 197 ], useOnDeviceMemory: true), | |
| 198 color: ColorAttribute[Vec4](data: @[ | |
| 528 | 199 backgroundColor, |
| 200 backgroundColor, | |
| 201 backgroundColor, | |
| 202 backgroundColor, | |
| 527 | 203 ]), |
| 204 transform: ModelTransformAttribute(data: @[Unit44]), | |
| 205 ) | |
|
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
206 |
|
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
207 scene = newThing("scene") |
| 527 | 208 scene.add newThing("background", backgroundmesh) |
| 525 | 209 let keyboard = newThing("keyboard", keyboardmesh) |
| 210 keyboard.transform = translate3d(-float32(rowWidth) / 2'f32, -float32( | |
| 527 | 211 tupleLen(keyRows) * (keyDimension + keyGap) - keyGap) / 2'f32, 0'f32) |
| 525 | 212 scene.add newThing("keyboard-center", keyboard) |
|
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
213 scene.add newThing("cursor", cursormesh) |
|
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
214 |
|
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
215 # upload data, prepare shaders, etc |
|
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
216 const vertexShader = generateVertexShaderCode[VertexDataA, Uniforms](""" |
| 525 | 217 out_position = uniforms.projection * transform * vec4(position, 1); |
|
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
218 """) |
|
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
219 const fragmentShader = generateFragmentShaderCode[VertexDataA]() |
|
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
220 pipeline = setupPipeline[VertexDataA, Uniforms, uint16]( |
|
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
221 myengine, |
|
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
222 scene, |
|
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
223 vertexShader, |
|
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
224 fragmentShader |
|
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
225 ) |
|
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
226 # show something |
|
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
227 myengine.run(pipeline, globalUpdate) |
|
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
228 pipeline.trash() |
|
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
229 myengine.trash() |
