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