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