Mercurial > games > semicongine
comparison examples/E04_input.nim @ 145:a4e6e76128e6
add: upgrade all simple examples to new engine version
author | Sam <sam@basx.dev> |
---|---|
date | Wed, 26 Apr 2023 02:15:43 +0700 |
parents | 8ce634aa6ea6 |
children | 253dd797e719 |
comparison
equal
deleted
inserted
replaced
144:01881f9296fa | 145:a4e6e76128e6 |
---|---|
54 LetterRow3Extra3, ShiftR, Up, | 54 LetterRow3Extra3, ShiftR, Up, |
55 | 55 |
56 CtrlL, SuperL, AltL, Space, AltR, SuperR, CtrlR, Left, Down, Right | 56 CtrlL, SuperL, AltL, Space, AltR, SuperR, CtrlR, Left, Down, Right |
57 ] | 57 ] |
58 | 58 |
59 # build keyboard and cursor meshes | |
59 var | 60 var |
60 scene: Entity | 61 scene: Entity |
61 keyvertexpos: seq[Vec3f] | 62 keyvertexpos: seq[Vec3f] |
62 keyvertexcolor: seq[Vec3f] | 63 keyvertexcolor: seq[Vec3f] |
63 keymeshindices: seq[array[3, uint16]] | 64 keymeshindices: seq[array[3, uint16]] |
89 rowpos[0] = 0 | 90 rowpos[0] = 0 |
90 rowpos[1] += keyDimension + keyGap * (if firstRow: 2'f32 else: 1'f32) | 91 rowpos[1] += keyDimension + keyGap * (if firstRow: 2'f32 else: 1'f32) |
91 firstRow = false | 92 firstRow = false |
92 | 93 |
93 | 94 |
94 | |
95 when isMainModule: | 95 when isMainModule: |
96 var myengine = initEngine("Input") | 96 var myengine = initEngine("Input") |
97 | 97 |
98 # transform the cursor a bit to make it look nice | 98 # transform the cursor a bit to make it look nice |
99 let cursorscale = ( | 99 let cursorscale = ( |
104 rotate2d(float32(PI) / 4'f32) | 104 rotate2d(float32(PI) / 4'f32) |
105 ) | 105 ) |
106 var positions = arrow | 106 var positions = arrow |
107 for i in 0 ..< positions.len: | 107 for i in 0 ..< positions.len: |
108 positions[i] = cursorscale * newVec3f(positions[i].x, positions[i].y) | 108 positions[i] = cursorscale * newVec3f(positions[i].x, positions[i].y) |
109 # cursor | 109 |
110 var cursormesh = newMesh( | 110 # define mesh objects |
111 positions=positions, | 111 var |
112 colors=arrow_colors, | 112 cursormesh = newMesh( |
113 instanceCount=1, | 113 positions=positions, |
114 ) | 114 colors=arrow_colors, |
115 | 115 instanceCount=1, |
116 # keyboard | 116 ) |
117 var keyboardmesh = newMesh( | 117 keyboardmesh = newMesh( |
118 positions=keyvertexpos, | 118 positions=keyvertexpos, |
119 colors=keyvertexcolor, | 119 colors=keyvertexcolor, |
120 indices=keymeshindices | 120 indices=keymeshindices |
121 ) | 121 ) |
122 | 122 backgroundmesh = newMesh( |
123 # background | 123 positions= @[ |
124 var backgroundmesh = newMesh( | 124 newVec3f(0'f32, 0'f32), |
125 positions= @[ | 125 newVec3f(1'f32, 0'f32), |
126 newVec3f(0'f32, 0'f32), | 126 newVec3f(1'f32, 1'f32), |
127 newVec3f(1'f32, 0'f32), | 127 newVec3f(0'f32, 1'f32), |
128 newVec3f(1'f32, 1'f32), | 128 ], |
129 newVec3f(0'f32, 1'f32), | 129 colors= @[ |
130 ], | 130 backgroundColor, |
131 colors= @[ | 131 backgroundColor, |
132 backgroundColor, | 132 backgroundColor, |
133 backgroundColor, | 133 backgroundColor, |
134 backgroundColor, | 134 ], |
135 backgroundColor, | 135 indices= @[[0'u16, 1'u16, 2'u16], [2'u16, 3'u16, 0'u16]], |
136 ], | 136 ) |
137 indices= @[[0'u16, 1'u16, 2'u16], [2'u16, 3'u16, 0'u16]], | |
138 ) | |
139 | 137 |
140 backgroundmesh.setInstanceData("transform", @[Unit4f32]) | 138 backgroundmesh.setInstanceData("transform", @[Unit4f32]) |
141 keyboardmesh.setInstanceData("transform", @[Unit4f32]) | 139 keyboardmesh.setInstanceData("transform", @[Unit4f32]) |
142 cursormesh.setInstanceData("transform", @[Unit4f32]) | 140 cursormesh.setInstanceData("transform", @[Unit4f32]) |
143 | 141 |
142 # define mesh objects | |
144 scene = newEntity("scene") | 143 scene = newEntity("scene") |
145 scene.add newEntity("background", backgroundmesh) | 144 scene.add newEntity("background", backgroundmesh) |
146 let keyboard = newEntity("keyboard", keyboardmesh) | 145 let keyboard = newEntity("keyboard", keyboardmesh) |
147 keyboard.transform = translate3d( | 146 keyboard.transform = translate3d( |
148 -float32(rowWidth) / 2'f32, | 147 -float32(rowWidth) / 2'f32, |
150 0'f32 | 149 0'f32 |
151 ) | 150 ) |
152 scene.add newEntity("keyboard-center", keyboard) | 151 scene.add newEntity("keyboard-center", keyboard) |
153 scene.add newEntity("cursor", cursormesh) | 152 scene.add newEntity("cursor", cursormesh) |
154 | 153 |
154 # shaders | |
155 const | 155 const |
156 vertexInput = @[ | 156 vertexInput = @[ |
157 attr[Vec3f]("position", memoryLocation=VRAM), | 157 attr[Vec3f]("position", memoryLocation=VRAM), |
158 attr[Vec3f]("color", memoryLocation=VRAMVisible), | 158 attr[Vec3f]("color", memoryLocation=VRAMVisible), |
159 attr[Mat4]("transform", memoryLocation=VRAMVisible, perInstance=true), | 159 attr[Mat4]("transform", memoryLocation=VRAMVisible, perInstance=true), |
173 inputs=vertexOutput, | 173 inputs=vertexOutput, |
174 uniforms=uniforms, | 174 uniforms=uniforms, |
175 outputs=fragOutput, | 175 outputs=fragOutput, |
176 main="color = vec4(outcolor, 1);" | 176 main="color = vec4(outcolor, 1);" |
177 ) | 177 ) |
178 | |
179 # set up rendering | |
178 myengine.setRenderer(myengine.gpuDevice.simpleForwardRenderPass(vertexCode, fragmentCode, clearColor=newVec4f(0, 0, 0.5))) | 180 myengine.setRenderer(myengine.gpuDevice.simpleForwardRenderPass(vertexCode, fragmentCode, clearColor=newVec4f(0, 0, 0.5))) |
179 myengine.addScene(scene, vertexInput, transformAttribute="transform") | 181 myengine.addScene(scene, vertexInput, transformAttribute="transform") |
180 var projection = initShaderGlobal("projection", Unit4f32) | 182 var projection = initShaderGlobal("projection", Unit4f32) |
181 scene.add projection | 183 scene.add projection |
182 while myengine.running and not myengine.keyWasPressed(Escape): | 184 |
183 myengine.updateInputs() | 185 # mainloop |
184 setValue[Mat4](projection.value, ortho[float32]( | 186 while myengine.updateInputs() == Running: |
185 0'f32, float32(myengine.getWindow().size[0]), | 187 if myengine.windowWasResized(): |
186 0'f32, float32(myengine.getWindow().size[1]), | 188 setValue[Mat4](projection.value, ortho[float32]( |
187 0'f32, 1'f32, | 189 0'f32, float32(myengine.getWindow().size[0]), |
188 )) | 190 0'f32, float32(myengine.getWindow().size[1]), |
189 let | 191 0'f32, 1'f32, |
190 mousePos = translate3d(myengine.mousePosition().x + 20, myengine.mousePosition().y + 20, 0'f32) | 192 )) |
191 winsize = myengine.getWindow().size | 193 let |
192 center = translate3d(float32(winsize[0]) / 2'f32, float32(winsize[1]) / 2'f32, 0.1'f32) | 194 winsize = myengine.getWindow().size |
195 center = translate3d(float32(winsize[0]) / 2'f32, float32(winsize[1]) / 2'f32, 0.1'f32) | |
196 scene.firstWithName("keyboard-center").transform = center | |
197 scene.firstWithName("background").transform = scale3d(float32(winsize[0]), float32(winsize[1]), 1'f32) | |
198 | |
199 let mousePos = translate3d(myengine.mousePosition().x + 20, myengine.mousePosition().y + 20, 0'f32) | |
193 scene.firstWithName("cursor").transform = mousePos | 200 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 | 201 |
197 var mesh = Mesh(scene.firstWithName("keyboard").components[0]) | 202 var mesh = Mesh(scene.firstWithName("keyboard").components[0]) |
198 for (index, key) in enumerate(keyIndices): | 203 for (index, key) in enumerate(keyIndices): |
199 if myengine.keyWasPressed(key): | 204 if myengine.keyWasPressed(key): |
200 let baseIndex = uint32(index * 4) | 205 let baseIndex = uint32(index * 4) |