Mercurial > games > semicongine
annotate examples/E04_input.nim @ 604:fdd80220b5ff
did: update example 04
author | Sam <sam@basx.dev> |
---|---|
date | Tue, 25 Apr 2023 18:24:18 +0700 |
parents | 21c276c0a968 |
children | f41c1b78cf5b |
rev | line source |
---|---|
527 | 1 import std/enumerate |
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
2 import std/strutils |
525 | 3 import std/typetraits |
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
4 import std/times |
525 | 5 import std/math |
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
6 |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
7 import semicongine |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
8 |
525 | 9 const |
10 arrow = @[ | |
604 | 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), | |
525 | 17 ] |
527 | 18 # keyboard layout, specifying rows with key widths, negative numbers are empty spaces |
525 | 19 keyrows = ( |
604 | 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], | |
527 | 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], | |
525 | 26 ) |
527 | 27 keyDimension = 50'f32 |
525 | 28 keyGap = 10'f32 |
604 | 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) | |
530 | 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 ] | |
527 | 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 ] | |
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
58 |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
59 var |
604 | 60 scene: Entity |
61 keyvertexpos: seq[Vec3f] | |
62 keyvertexcolor: seq[Vec3f] | |
527 | 63 keymeshindices: seq[array[3, uint16]] |
604 | 64 rowpos = newVec2f(0, 0) |
525 | 65 i = 0'u16 |
527 | 66 firstRow = true |
67 rowWidth = 0'f32 | |
525 | 68 for row in keyrows.fields: |
69 for key in row: | |
527 | 70 let keySpace = float32(keyDimension * key) |
71 if key > 0: | |
72 if keyIndices[i div 4] == Enter: | |
604 | 73 keyvertexpos.add newVec3f(rowpos[0], rowpos[1] - keyDimension - keyGap) |
74 keyvertexpos.add newVec3f(rowpos[0] + keySpace, rowpos[1] - keyDimension - keyGap) | |
527 | 75 else: |
604 | 76 keyvertexpos.add newVec3f(rowpos[0], rowpos[1]) |
77 keyvertexpos.add newVec3f(rowpos[0] + keySpace, rowpos[1]) | |
78 keyvertexpos.add newVec3f(rowpos[0] + keySpace, rowpos[1] + keyDimension) | |
79 keyvertexpos.add newVec3f(rowpos[0], rowpos[1] + keyDimension) | |
527 | 80 keyvertexcolor.add [baseColor, baseColor, baseColor, baseColor] |
81 keymeshindices.add [i, i + 1, i + 2] | |
82 keymeshindices.add [i + 2, i + 3, i] | |
83 rowpos[0] += keySpace + keyGap | |
84 i += 4 | |
85 else: | |
86 rowpos[0] += -keySpace + keyGap | |
87 if firstRow: | |
88 rowWidth = rowpos[0] | |
525 | 89 rowpos[0] = 0 |
527 | 90 rowpos[1] += keyDimension + keyGap * (if firstRow: 2'f32 else: 1'f32) |
91 firstRow = false | |
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
92 |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
93 |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
94 |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
95 when isMainModule: |
604 | 96 var myengine = initEngine("Input") |
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
97 |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
98 # transform the cursor a bit to make it look nice |
525 | 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 ) | |
604 | 106 var positions = arrow |
107 for i in 0 ..< positions.len: | |
108 positions[i] = cursorscale * newVec3f(positions[i].x, positions[i].y) | |
109 # cursor | |
110 var cursormesh = newMesh( | |
111 positions=positions, | |
112 colors=arrow_colors, | |
113 instanceCount=1, | |
114 ) | |
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
115 |
527 | 116 # keyboard |
604 | 117 var keyboardmesh = newMesh( |
118 positions=keyvertexpos, | |
119 colors=keyvertexcolor, | |
120 indices=keymeshindices | |
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
121 ) |
527 | 122 |
123 # background | |
604 | 124 var backgroundmesh = newMesh( |
125 positions= @[ | |
126 newVec3f(0'f32, 0'f32), | |
127 newVec3f(1'f32, 0'f32), | |
128 newVec3f(1'f32, 1'f32), | |
129 newVec3f(0'f32, 1'f32), | |
130 ], | |
131 colors= @[ | |
528 | 132 backgroundColor, |
133 backgroundColor, | |
134 backgroundColor, | |
135 backgroundColor, | |
604 | 136 ], |
137 indices= @[[0'u16, 1'u16, 2'u16], [2'u16, 3'u16, 0'u16]], | |
527 | 138 ) |
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
139 |
604 | 140 backgroundmesh.setInstanceData("transform", @[Unit4f32]) |
141 keyboardmesh.setInstanceData("transform", @[Unit4f32]) | |
142 cursormesh.setInstanceData("transform", @[Unit4f32]) | |
143 | |
144 scene = newEntity("scene") | |
145 scene.add newEntity("background", backgroundmesh) | |
146 let keyboard = newEntity("keyboard", keyboardmesh) | |
147 keyboard.transform = translate3d( | |
148 -float32(rowWidth) / 2'f32, | |
149 -float32(tupleLen(keyRows) * (keyDimension + keyGap) - keyGap) / 2'f32, | |
150 0'f32 | |
151 ) | |
152 scene.add newEntity("keyboard-center", keyboard) | |
153 scene.add newEntity("cursor", cursormesh) | |
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
154 |
604 | 155 const |
156 vertexInput = @[ | |
157 attr[Vec3f]("position", memoryLocation=VRAM), | |
158 attr[Vec3f]("color", memoryLocation=VRAMVisible), | |
159 attr[Mat4]("transform", memoryLocation=VRAMVisible, perInstance=true), | |
160 ] | |
161 vertexOutput = @[attr[Vec3f]("outcolor")] | |
162 uniforms = @[attr[Mat4]("projection")] | |
163 fragOutput = @[attr[Vec4f]("color")] | |
164 vertexCode = compileGlslShader( | |
165 stage=VK_SHADER_STAGE_VERTEX_BIT, | |
166 inputs=vertexInput, | |
167 uniforms=uniforms, | |
168 outputs=vertexOutput, | |
169 main="""outcolor = color; gl_Position = vec4(position, 1) * (transform * Uniforms.projection);""" | |
170 ) | |
171 fragmentCode = compileGlslShader( | |
172 stage=VK_SHADER_STAGE_FRAGMENT_BIT, | |
173 inputs=vertexOutput, | |
174 uniforms=uniforms, | |
175 outputs=fragOutput, | |
176 main="color = vec4(outcolor, 1);" | |
177 ) | |
178 myengine.setRenderer(myengine.gpuDevice.simpleForwardRenderPass(vertexCode, fragmentCode, clearColor=newVec4f(0, 0, 0.5))) | |
179 myengine.addScene(scene, vertexInput, transformAttribute="transform") | |
180 var projection = initShaderGlobal("projection", Unit4f32) | |
181 scene.add projection | |
182 while myengine.running and not myengine.keyWasPressed(Escape): | |
183 myengine.updateInputs() | |
184 setValue[Mat4](projection.value, ortho[float32]( | |
185 0'f32, float32(myengine.getWindow().size[0]), | |
186 0'f32, float32(myengine.getWindow().size[1]), | |
187 0'f32, 1'f32, | |
188 )) | |
189 let | |
190 mousePos = translate3d(myengine.mousePosition().x + 20, myengine.mousePosition().y + 20, 0'f32) | |
191 winsize = myengine.getWindow().size | |
192 center = translate3d(float32(winsize[0]) / 2'f32, float32(winsize[1]) / 2'f32, 0.1'f32) | |
193 scene.firstWithName("cursor").transform = mousePos | |
194 scene.firstWithName("keyboard-center").transform = center | |
195 scene.firstWithName("background").transform = scale3d(float32(winsize[0]), float32(winsize[1]), 1'f32) | |
196 | |
197 var mesh = Mesh(scene.firstWithName("keyboard").components[0]) | |
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() |