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