Mercurial > games > semicongine
comparison 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 |
comparison
equal
deleted
inserted
replaced
603:f494e291ff4a | 604:fdd80220b5ff |
---|---|
4 import std/times | 4 import std/times |
5 import std/math | 5 import std/math |
6 | 6 |
7 import semicongine | 7 import semicongine |
8 | 8 |
9 type | |
10 # define type of vertex | |
11 VertexDataA = object | |
12 position: PositionAttribute[Vec3] | |
13 color: ColorAttribute[Vec4] | |
14 transform: ModelTransformAttribute | |
15 Uniforms = object | |
16 projection: ViewProjectionTransform | |
17 | |
18 const | 9 const |
19 arrow = @[ | 10 arrow = @[ |
20 Vec3([-1'f32, -1'f32, 0'f32]), | 11 newVec3f(-1, -1), |
21 Vec3([1'f32, -1'f32, 0'f32]), | 12 newVec3f(1, -1), |
22 Vec3([-0.3'f32, -0.3'f32, 0'f32]), | 13 newVec3f(-0.3, -0.3), |
23 Vec3([-0.3'f32, -0.3'f32, 0'f32]), | 14 newVec3f(-0.3, -0.3), |
24 Vec3([-1'f32, 1'f32, 0'f32]), | 15 newVec3f(-1, 1), |
25 Vec3([-1'f32, -1'f32, 0'f32]), | 16 newVec3f(-1, -1), |
26 ] | 17 ] |
27 # keyboard layout, specifying rows with key widths, negative numbers are empty spaces | 18 # keyboard layout, specifying rows with key widths, negative numbers are empty spaces |
28 keyrows = ( | 19 keyrows = ( |
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, | 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], |
30 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], |
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, | 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], |
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], | 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], |
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], | 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], |
37 [1.5, 1.5, 1.5, 6, 1.5, 1.5, -1.2, 1.5, -0.1, 1.0, 1.0, 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], |
38 ) | 26 ) |
39 keyDimension = 50'f32 | 27 keyDimension = 50'f32 |
40 keyGap = 10'f32 | 28 keyGap = 10'f32 |
41 backgroundColor = Vec4([0.6705882352941176'f32, 0.6078431372549019'f32, | 29 backgroundColor = newVec3f(0.6705882352941176, 0.6078431372549019, 0.5882352941176471) |
42 0.5882352941176471'f32, 0'f32]) | 30 baseColor = newVec3f(0.9411764705882353, 0.9058823529411765, 0.8470588235294118'f32) |
43 baseColor = Vec4([0.9411764705882353'f32, 0.9058823529411765'f32, | 31 activeColor = newVec3f(0.6509803921568628'f32, 0.22745098039215686, 0.3137254901960784'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 = @[ | 32 arrow_colors = @[ |
49 baseColor * 0.9'f32, | 33 baseColor * 0.9'f32, |
50 baseColor * 0.9'f32, | 34 baseColor * 0.9'f32, |
51 baseColor * 0.9'f32, | 35 baseColor * 0.9'f32, |
52 baseColor * 0.8'f32, | 36 baseColor * 0.8'f32, |
71 | 55 |
72 CtrlL, SuperL, AltL, Space, AltR, SuperR, CtrlR, Left, Down, Right | 56 CtrlL, SuperL, AltL, Space, AltR, SuperR, CtrlR, Left, Down, Right |
73 ] | 57 ] |
74 | 58 |
75 var | 59 var |
76 pipeline: RenderPipeline[VertexDataA, Uniforms] | 60 scene: Entity |
77 uniforms: Uniforms | 61 keyvertexpos: seq[Vec3f] |
78 scene: Thing | 62 keyvertexcolor: seq[Vec3f] |
79 keyvertexpos: seq[Vec3] | |
80 keyvertexcolor: seq[Vec4] | |
81 keymeshindices: seq[array[3, uint16]] | 63 keymeshindices: seq[array[3, uint16]] |
82 rowpos = Vec2([0'f32, 0'f32]) | 64 rowpos = newVec2f(0, 0) |
83 i = 0'u16 | 65 i = 0'u16 |
84 firstRow = true | 66 firstRow = true |
85 rowWidth = 0'f32 | 67 rowWidth = 0'f32 |
86 for row in keyrows.fields: | 68 for row in keyrows.fields: |
87 for key in row: | 69 for key in row: |
88 let keySpace = float32(keyDimension * key) | 70 let keySpace = float32(keyDimension * key) |
89 if key > 0: | 71 if key > 0: |
90 if keyIndices[i div 4] == Enter: | 72 if keyIndices[i div 4] == Enter: |
91 keyvertexpos.add Vec3([rowpos[0], rowpos[1] - keyDimension - keyGap, 0'f32]) | 73 keyvertexpos.add newVec3f(rowpos[0], rowpos[1] - keyDimension - keyGap) |
92 keyvertexpos.add Vec3([rowpos[0] + keySpace, rowpos[1] - keyDimension - | 74 keyvertexpos.add newVec3f(rowpos[0] + keySpace, rowpos[1] - keyDimension - keyGap) |
93 keyGap, 0'f32]) | |
94 else: | 75 else: |
95 keyvertexpos.add Vec3([rowpos[0], rowpos[1], 0'f32]) | 76 keyvertexpos.add newVec3f(rowpos[0], rowpos[1]) |
96 keyvertexpos.add Vec3([rowpos[0] + keySpace, rowpos[1], 0'f32]) | 77 keyvertexpos.add newVec3f(rowpos[0] + keySpace, rowpos[1]) |
97 keyvertexpos.add Vec3([rowpos[0] + keySpace, rowpos[1] + keyDimension, 0'f32]) | 78 keyvertexpos.add newVec3f(rowpos[0] + keySpace, rowpos[1] + keyDimension) |
98 keyvertexpos.add Vec3([rowpos[0], rowpos[1] + keyDimension, 0'f32]) | 79 keyvertexpos.add newVec3f(rowpos[0], rowpos[1] + keyDimension) |
99 keyvertexcolor.add [baseColor, baseColor, baseColor, baseColor] | 80 keyvertexcolor.add [baseColor, baseColor, baseColor, baseColor] |
100 keymeshindices.add [i, i + 1, i + 2] | 81 keymeshindices.add [i, i + 1, i + 2] |
101 keymeshindices.add [i + 2, i + 3, i] | 82 keymeshindices.add [i + 2, i + 3, i] |
102 rowpos[0] += keySpace + keyGap | 83 rowpos[0] += keySpace + keyGap |
103 i += 4 | 84 i += 4 |
108 rowpos[0] = 0 | 89 rowpos[0] = 0 |
109 rowpos[1] += keyDimension + keyGap * (if firstRow: 2'f32 else: 1'f32) | 90 rowpos[1] += keyDimension + keyGap * (if firstRow: 2'f32 else: 1'f32) |
110 firstRow = false | 91 firstRow = false |
111 | 92 |
112 | 93 |
113 proc globalUpdate(engine: var Engine, t, dt: float32) = | |
114 uniforms.projection.value = ortho[float32]( | |
115 0'f32, float32(engine.vulkan.frameSize.x), | |
116 0'f32, float32(engine.vulkan.frameSize.y), | |
117 0'f32, 1'f32, | |
118 ) | |
119 engine.vulkan.device.updateUniformData(pipeline, uniforms) | |
120 | |
121 let | |
122 mousePos = translate3d(engine.input.mousePos.x + 20, | |
123 engine.input.mousePos.y + 20, 0'f32) | |
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 | |
129 scene.firstWithName("background").transform = scale3d(float32(winsize[0]), | |
130 float32(winsize[1]), 0'f32) | |
131 var mesh = Mesh[VertexDataA, uint16](scene.firstWithName("keyboard").parts[0]) | |
132 var hadUpdate = false | |
133 for (index, key) in enumerate(keyIndices): | |
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) | |
150 | |
151 | 94 |
152 when isMainModule: | 95 when isMainModule: |
153 var myengine = igniteEngine("Input") | 96 var myengine = initEngine("Input") |
154 | 97 |
155 # cursor | |
156 var cursormesh = new Mesh[VertexDataA, uint16] | |
157 cursormesh.vertexData = VertexDataA( | |
158 position: PositionAttribute[Vec3](data: arrow, useOnDeviceMemory: true), | |
159 color: ColorAttribute[Vec4](data: arrow_colors), | |
160 transform: ModelTransformAttribute(data: @[Unit44]), | |
161 ) | |
162 # transform the cursor a bit to make it look nice | 98 # transform the cursor a bit to make it look nice |
163 let cursorscale = ( | 99 let cursorscale = ( |
164 scale2d(20'f32, 20'f32) * | 100 scale2d(20'f32, 20'f32) * |
165 translate2d(1'f32, 1'f32) * | 101 translate2d(1'f32, 1'f32) * |
166 rotate2d(-float32(PI) / 4'f32) * | 102 rotate2d(-float32(PI) / 4'f32) * |
167 scale2d(0.5'f32, 1'f32) * | 103 scale2d(0.5'f32, 1'f32) * |
168 rotate2d(float32(PI) / 4'f32) | 104 rotate2d(float32(PI) / 4'f32) |
169 ) | 105 ) |
170 for i in 0 ..< cursormesh.vertexData.position.data.len: | 106 var positions = arrow |
171 let pos = Vec3([cursormesh.vertexData.position.data[i][0], | 107 for i in 0 ..< positions.len: |
172 cursormesh.vertexData.position.data[i][1], 0'f32]) | 108 positions[i] = cursorscale * newVec3f(positions[i].x, positions[i].y) |
173 cursormesh.vertexData.position.data[i] = (cursorscale * pos) | 109 # cursor |
110 var cursormesh = newMesh( | |
111 positions=positions, | |
112 colors=arrow_colors, | |
113 instanceCount=1, | |
114 ) | |
174 | 115 |
175 # keyboard | 116 # keyboard |
176 var keyboardmesh = new Mesh[VertexDataA, uint16] | 117 var keyboardmesh = newMesh( |
177 keyboardmesh.indexed = true | 118 positions=keyvertexpos, |
178 keyboardmesh.vertexData = VertexDataA( | 119 colors=keyvertexcolor, |
179 position: PositionAttribute[Vec3](data: keyvertexpos, | 120 indices=keymeshindices |
180 useOnDeviceMemory: true), | 121 ) |
181 color: ColorAttribute[Vec4](data: keyvertexcolor), | |
182 transform: ModelTransformAttribute(data: @[Unit44]), | |
183 ) | |
184 keyboardmesh.indices = keymeshindices | |
185 | 122 |
186 # background | 123 # background |
187 var backgroundmesh = new Mesh[VertexDataA, uint16] | 124 var backgroundmesh = newMesh( |
188 backgroundmesh.indexed = true | 125 positions= @[ |
189 backgroundmesh.indices = @[[0'u16, 1'u16, 2'u16], [2'u16, 3'u16, 0'u16]] | 126 newVec3f(0'f32, 0'f32), |
190 backgroundmesh.vertexData = VertexDataA( | 127 newVec3f(1'f32, 0'f32), |
191 position: PositionAttribute[Vec3](data: @[ | 128 newVec3f(1'f32, 1'f32), |
192 Vec3([0'f32, 0'f32, 0'f32]), | 129 newVec3f(0'f32, 1'f32), |
193 Vec3([1'f32, 0'f32, 0'f32]), | 130 ], |
194 Vec3([1'f32, 1'f32, 0'f32]), | 131 colors= @[ |
195 Vec3([0'f32, 1'f32, 0'f32]), | 132 backgroundColor, |
196 ], useOnDeviceMemory: true), | 133 backgroundColor, |
197 color: ColorAttribute[Vec4](data: @[ | 134 backgroundColor, |
198 backgroundColor, | 135 backgroundColor, |
199 backgroundColor, | 136 ], |
200 backgroundColor, | 137 indices= @[[0'u16, 1'u16, 2'u16], [2'u16, 3'u16, 0'u16]], |
201 backgroundColor, | 138 ) |
202 ]), | 139 |
203 transform: ModelTransformAttribute(data: @[Unit44]), | 140 backgroundmesh.setInstanceData("transform", @[Unit4f32]) |
204 ) | 141 keyboardmesh.setInstanceData("transform", @[Unit4f32]) |
205 | 142 cursormesh.setInstanceData("transform", @[Unit4f32]) |
206 scene = newThing("scene") | 143 |
207 scene.add newThing("background", backgroundmesh) | 144 scene = newEntity("scene") |
208 let keyboard = newThing("keyboard", keyboardmesh) | 145 scene.add newEntity("background", backgroundmesh) |
209 keyboard.transform = translate3d(-float32(rowWidth) / 2'f32, -float32( | 146 let keyboard = newEntity("keyboard", keyboardmesh) |
210 tupleLen(keyRows) * (keyDimension + keyGap) - keyGap) / 2'f32, 0'f32) | 147 keyboard.transform = translate3d( |
211 scene.add newThing("keyboard-center", keyboard) | 148 -float32(rowWidth) / 2'f32, |
212 scene.add newThing("cursor", cursormesh) | 149 -float32(tupleLen(keyRows) * (keyDimension + keyGap) - keyGap) / 2'f32, |
213 | 150 0'f32 |
214 # upload data, prepare shaders, etc | 151 ) |
215 const vertexShader = generateVertexShaderCode[VertexDataA, Uniforms]() | 152 scene.add newEntity("keyboard-center", keyboard) |
216 const fragmentShader = generateFragmentShaderCode[VertexDataA]() | 153 scene.add newEntity("cursor", cursormesh) |
217 pipeline = setupPipeline[VertexDataA, Uniforms, uint16]( | 154 |
218 myengine, | 155 const |
219 scene, | 156 vertexInput = @[ |
220 vertexShader, | 157 attr[Vec3f]("position", memoryLocation=VRAM), |
221 fragmentShader | 158 attr[Vec3f]("color", memoryLocation=VRAMVisible), |
222 ) | 159 attr[Mat4]("transform", memoryLocation=VRAMVisible, perInstance=true), |
223 # show something | 160 ] |
224 myengine.run(pipeline, globalUpdate) | 161 vertexOutput = @[attr[Vec3f]("outcolor")] |
225 pipeline.trash() | 162 uniforms = @[attr[Mat4]("projection")] |
226 myengine.trash() | 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() |