Mercurial > games > semicongine
annotate examples/E04_input.nim @ 271:95281f2db400
did: fix API changes broke examples
author | Sam <sam@basx.dev> |
---|---|
date | Tue, 30 May 2023 16:58:35 +0700 |
parents | ad078e26a1c7 |
children | da0bd61abe91 |
rev | line source |
---|---|
66 | 1 import std/enumerate |
64 | 2 import std/typetraits |
3 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
|
4 |
271 | 5 import ../src/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
|
6 |
64 | 7 const |
8 arrow = @[ | |
143 | 9 newVec3f(-1, -1), |
10 newVec3f(1, -1), | |
11 newVec3f(-0.3, -0.3), | |
12 newVec3f(-0.3, -0.3), | |
13 newVec3f(-1, 1), | |
14 newVec3f(-1, -1), | |
64 | 15 ] |
66 | 16 # keyboard layout, specifying rows with key widths, negative numbers are empty spaces |
64 | 17 keyrows = ( |
143 | 18 [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, 1.0, 1.0, -0.1, 1.0, 1.0, 1.0], |
19 [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, 1.0, 1.0, 1.0], | |
20 [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, 1.0, 1.0], | |
66 | 21 [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], |
22 [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], | |
23 [1.5, 1.5, 1.5, 6, 1.5, 1.5, -1.2, 1.5, -0.1, 1.0, 1.0, 1.0], | |
64 | 24 ) |
66 | 25 keyDimension = 50'f32 |
64 | 26 keyGap = 10'f32 |
210 | 27 backgroundColor = newVec4f(0.6705882352941176, 0.6078431372549019, 0.5882352941176471, 1) |
28 baseColor = newVec4f(0.9411764705882353, 0.9058823529411765, 0.8470588235294118, 1) | |
29 activeColor = newVec4f(0.6509803921568628, 0.22745098039215686, 0.3137254901960784, 1) | |
69 | 30 arrow_colors = @[ |
31 baseColor * 0.9'f32, | |
32 baseColor * 0.9'f32, | |
33 baseColor * 0.9'f32, | |
34 baseColor * 0.8'f32, | |
35 baseColor * 0.8'f32, | |
36 baseColor * 0.8'f32, | |
37 ] | |
66 | 38 keyIndices = [ |
39 Escape, F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, PrintScreen, | |
40 ScrollLock, Pause, | |
41 | |
42 NumberRowExtra1, `1`, `2`, `3`, `4`, `5`, `6`, `7`, `8`, `9`, `0`, | |
43 | |
44 NumberRowExtra2, NumberRowExtra3, Backspace, Insert, Home, PageUp, | |
45 Tab, Q, W, Key.E, R, T, Key.Y, U, I, O, P, LetterRow1Extra1, | |
46 LetterRow1Extra2, Delete, End, PageDown, | |
47 | |
48 CapsLock, A, S, D, F, G, H, J, K, L, LetterRow2Extra1, LetterRow2Extra2, | |
49 LetterRow2Extra3, Enter, | |
50 | |
51 ShiftL, Key.Z, Key.X, C, V, B, N, M, LetterRow3Extra1, LetterRow3Extra2, | |
52 LetterRow3Extra3, ShiftR, Up, | |
53 | |
54 CtrlL, SuperL, AltL, Space, AltR, SuperR, CtrlR, Left, Down, Right | |
55 ] | |
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
|
56 |
145
a4e6e76128e6
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
143
diff
changeset
|
57 # build keyboard and cursor meshes |
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
|
58 var |
203
84fd522fdf3f
did: update examples to use new API for scene + scene globals
Sam <sam@basx.dev>
parents:
163
diff
changeset
|
59 scene: Scene |
143 | 60 keyvertexpos: seq[Vec3f] |
210 | 61 keyvertexcolor: seq[Vec4f] |
66 | 62 keymeshindices: seq[array[3, uint16]] |
143 | 63 rowpos = newVec2f(0, 0) |
64 | 64 i = 0'u16 |
66 | 65 firstRow = true |
66 rowWidth = 0'f32 | |
64 | 67 for row in keyrows.fields: |
68 for key in row: | |
66 | 69 let keySpace = float32(keyDimension * key) |
70 if key > 0: | |
71 if keyIndices[i div 4] == Enter: | |
143 | 72 keyvertexpos.add newVec3f(rowpos[0], rowpos[1] - keyDimension - keyGap) |
73 keyvertexpos.add newVec3f(rowpos[0] + keySpace, rowpos[1] - keyDimension - keyGap) | |
66 | 74 else: |
143 | 75 keyvertexpos.add newVec3f(rowpos[0], rowpos[1]) |
76 keyvertexpos.add newVec3f(rowpos[0] + keySpace, rowpos[1]) | |
77 keyvertexpos.add newVec3f(rowpos[0] + keySpace, rowpos[1] + keyDimension) | |
78 keyvertexpos.add newVec3f(rowpos[0], rowpos[1] + keyDimension) | |
66 | 79 keyvertexcolor.add [baseColor, baseColor, baseColor, baseColor] |
80 keymeshindices.add [i, i + 1, i + 2] | |
81 keymeshindices.add [i + 2, i + 3, i] | |
82 rowpos[0] += keySpace + keyGap | |
83 i += 4 | |
84 else: | |
85 rowpos[0] += -keySpace + keyGap | |
86 if firstRow: | |
87 rowWidth = rowpos[0] | |
64 | 88 rowpos[0] = 0 |
66 | 89 rowpos[1] += keyDimension + keyGap * (if firstRow: 2'f32 else: 1'f32) |
90 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
|
91 |
c3c963e7c1a6
did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff
changeset
|
92 |
c3c963e7c1a6
did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff
changeset
|
93 when isMainModule: |
143 | 94 var myengine = initEngine("Input") |
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
|
95 |
c3c963e7c1a6
did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff
changeset
|
96 # transform the cursor a bit to make it look nice |
64 | 97 let cursorscale = ( |
98 scale2d(20'f32, 20'f32) * | |
99 translate2d(1'f32, 1'f32) * | |
100 rotate2d(-float32(PI) / 4'f32) * | |
101 scale2d(0.5'f32, 1'f32) * | |
102 rotate2d(float32(PI) / 4'f32) | |
103 ) | |
143 | 104 var positions = arrow |
105 for i in 0 ..< positions.len: | |
106 positions[i] = cursorscale * newVec3f(positions[i].x, positions[i].y) | |
57 | 107 |
145
a4e6e76128e6
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
143
diff
changeset
|
108 # define mesh objects |
a4e6e76128e6
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
143
diff
changeset
|
109 var |
a4e6e76128e6
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
143
diff
changeset
|
110 cursormesh = newMesh( |
a4e6e76128e6
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
143
diff
changeset
|
111 positions=positions, |
a4e6e76128e6
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
143
diff
changeset
|
112 colors=arrow_colors, |
a4e6e76128e6
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
143
diff
changeset
|
113 instanceCount=1, |
a4e6e76128e6
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
143
diff
changeset
|
114 ) |
a4e6e76128e6
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
143
diff
changeset
|
115 keyboardmesh = newMesh( |
a4e6e76128e6
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
143
diff
changeset
|
116 positions=keyvertexpos, |
a4e6e76128e6
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
143
diff
changeset
|
117 colors=keyvertexcolor, |
a4e6e76128e6
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
143
diff
changeset
|
118 indices=keymeshindices |
a4e6e76128e6
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
143
diff
changeset
|
119 ) |
a4e6e76128e6
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
143
diff
changeset
|
120 backgroundmesh = newMesh( |
a4e6e76128e6
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
143
diff
changeset
|
121 positions= @[ |
a4e6e76128e6
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
143
diff
changeset
|
122 newVec3f(0'f32, 0'f32), |
a4e6e76128e6
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
143
diff
changeset
|
123 newVec3f(1'f32, 0'f32), |
a4e6e76128e6
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
143
diff
changeset
|
124 newVec3f(1'f32, 1'f32), |
a4e6e76128e6
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
143
diff
changeset
|
125 newVec3f(0'f32, 1'f32), |
a4e6e76128e6
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
143
diff
changeset
|
126 ], |
a4e6e76128e6
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
143
diff
changeset
|
127 colors= @[ |
a4e6e76128e6
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
143
diff
changeset
|
128 backgroundColor, |
a4e6e76128e6
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
143
diff
changeset
|
129 backgroundColor, |
a4e6e76128e6
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
143
diff
changeset
|
130 backgroundColor, |
a4e6e76128e6
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
143
diff
changeset
|
131 backgroundColor, |
a4e6e76128e6
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
143
diff
changeset
|
132 ], |
a4e6e76128e6
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
143
diff
changeset
|
133 indices= @[[0'u16, 1'u16, 2'u16], [2'u16, 3'u16, 0'u16]], |
a4e6e76128e6
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
143
diff
changeset
|
134 ) |
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
|
135 |
145
a4e6e76128e6
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
143
diff
changeset
|
136 # define mesh objects |
203
84fd522fdf3f
did: update examples to use new API for scene + scene globals
Sam <sam@basx.dev>
parents:
163
diff
changeset
|
137 scene = newScene("scene", newEntity("scene")) |
84fd522fdf3f
did: update examples to use new API for scene + scene globals
Sam <sam@basx.dev>
parents:
163
diff
changeset
|
138 scene.root.add newEntity("background", backgroundmesh) |
143 | 139 let keyboard = newEntity("keyboard", keyboardmesh) |
140 keyboard.transform = translate3d( | |
141 -float32(rowWidth) / 2'f32, | |
142 -float32(tupleLen(keyRows) * (keyDimension + keyGap) - keyGap) / 2'f32, | |
143 0'f32 | |
144 ) | |
203
84fd522fdf3f
did: update examples to use new API for scene + scene globals
Sam <sam@basx.dev>
parents:
163
diff
changeset
|
145 scene.root.add newEntity("keyboard-center", keyboard) |
84fd522fdf3f
did: update examples to use new API for scene + scene globals
Sam <sam@basx.dev>
parents:
163
diff
changeset
|
146 scene.root.add newEntity("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
|
147 |
145
a4e6e76128e6
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
143
diff
changeset
|
148 # shaders |
143 | 149 const |
150 vertexInput = @[ | |
163 | 151 attr[Vec3f]("position"), |
210 | 152 attr[Vec4f]("color", memoryPerformanceHint=PreferFastWrite), |
163 | 153 attr[Mat4]("transform", memoryPerformanceHint=PreferFastWrite, perInstance=true), |
143 | 154 ] |
210 | 155 vertexOutput = @[attr[Vec4f]("outcolor")] |
143 | 156 uniforms = @[attr[Mat4]("projection")] |
157 fragOutput = @[attr[Vec4f]("color")] | |
158 vertexCode = compileGlslShader( | |
159 stage=VK_SHADER_STAGE_VERTEX_BIT, | |
160 inputs=vertexInput, | |
161 uniforms=uniforms, | |
162 outputs=vertexOutput, | |
163 main="""outcolor = color; gl_Position = vec4(position, 1) * (transform * Uniforms.projection);""" | |
164 ) | |
165 fragmentCode = compileGlslShader( | |
166 stage=VK_SHADER_STAGE_FRAGMENT_BIT, | |
167 inputs=vertexOutput, | |
168 uniforms=uniforms, | |
169 outputs=fragOutput, | |
210 | 170 main="color = outcolor;" |
143 | 171 ) |
145
a4e6e76128e6
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
143
diff
changeset
|
172 |
a4e6e76128e6
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
143
diff
changeset
|
173 # set up rendering |
143 | 174 myengine.setRenderer(myengine.gpuDevice.simpleForwardRenderPass(vertexCode, fragmentCode, clearColor=newVec4f(0, 0, 0.5))) |
253 | 175 myengine.addScene(scene, vertexInput, @[], transformAttribute="transform") |
203
84fd522fdf3f
did: update examples to use new API for scene + scene globals
Sam <sam@basx.dev>
parents:
163
diff
changeset
|
176 scene.addShaderGlobal("projection", Unit4f32) |
145
a4e6e76128e6
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
143
diff
changeset
|
177 |
a4e6e76128e6
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
143
diff
changeset
|
178 # mainloop |
a4e6e76128e6
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
143
diff
changeset
|
179 while myengine.updateInputs() == Running: |
a4e6e76128e6
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
143
diff
changeset
|
180 if myengine.windowWasResized(): |
203
84fd522fdf3f
did: update examples to use new API for scene + scene globals
Sam <sam@basx.dev>
parents:
163
diff
changeset
|
181 setShaderGlobal(scene, "projection", |
247 | 182 ortho( |
183 0, float32(myengine.getWindow().size[0]), | |
184 0, float32(myengine.getWindow().size[1]), | |
185 0, 1, | |
203
84fd522fdf3f
did: update examples to use new API for scene + scene globals
Sam <sam@basx.dev>
parents:
163
diff
changeset
|
186 ) |
84fd522fdf3f
did: update examples to use new API for scene + scene globals
Sam <sam@basx.dev>
parents:
163
diff
changeset
|
187 ) |
145
a4e6e76128e6
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
143
diff
changeset
|
188 let |
a4e6e76128e6
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
143
diff
changeset
|
189 winsize = myengine.getWindow().size |
a4e6e76128e6
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
143
diff
changeset
|
190 center = translate3d(float32(winsize[0]) / 2'f32, float32(winsize[1]) / 2'f32, 0.1'f32) |
203
84fd522fdf3f
did: update examples to use new API for scene + scene globals
Sam <sam@basx.dev>
parents:
163
diff
changeset
|
191 scene.root.firstWithName("keyboard-center").transform = center |
84fd522fdf3f
did: update examples to use new API for scene + scene globals
Sam <sam@basx.dev>
parents:
163
diff
changeset
|
192 scene.root.firstWithName("background").transform = scale3d(float32(winsize[0]), float32(winsize[1]), 1'f32) |
145
a4e6e76128e6
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
143
diff
changeset
|
193 |
a4e6e76128e6
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
143
diff
changeset
|
194 let mousePos = translate3d(myengine.mousePosition().x + 20, myengine.mousePosition().y + 20, 0'f32) |
203
84fd522fdf3f
did: update examples to use new API for scene + scene globals
Sam <sam@basx.dev>
parents:
163
diff
changeset
|
195 scene.root.firstWithName("cursor").transform = mousePos |
143 | 196 |
203
84fd522fdf3f
did: update examples to use new API for scene + scene globals
Sam <sam@basx.dev>
parents:
163
diff
changeset
|
197 var mesh = Mesh(scene.root.firstWithName("keyboard").components[0]) |
143 | 198 for (index, key) in enumerate(keyIndices): |
199 if myengine.keyWasPressed(key): | |
200 let baseIndex = uint32(index * 4) | |
201 mesh.updateMeshData("color", baseIndex + 0, activeColor) | |
202 mesh.updateMeshData("color", baseIndex + 1, activeColor) | |
203 mesh.updateMeshData("color", baseIndex + 2, activeColor) | |
204 mesh.updateMeshData("color", baseIndex + 3, activeColor) | |
205 if myengine.keyWasReleased(key): | |
206 let baseIndex = uint32(index * 4) | |
207 mesh.updateMeshData("color", baseIndex + 0, baseColor) | |
208 mesh.updateMeshData("color", baseIndex + 1, baseColor) | |
209 mesh.updateMeshData("color", baseIndex + 2, baseColor) | |
210 mesh.updateMeshData("color", baseIndex + 3, baseColor) | |
211 | |
212 myengine.renderScene(scene) | |
213 | |
214 myengine.destroy() |