Mercurial > games > semicongine
comparison examples/E04_input.nim @ 1140:5934c5615f13
did: update all examples to work with latest refactoring
author | sam <sam@basx.dev> |
---|---|
date | Sat, 08 Jun 2024 15:16:17 +0700 |
parents | 114f395b9144 |
children |
comparison
equal
deleted
inserted
replaced
1139:114f395b9144 | 1140:5934c5615f13 |
---|---|
90 rowpos[1] += keyDimension + keyGap * (if firstRow: 2'f32 else: 1'f32) | 90 rowpos[1] += keyDimension + keyGap * (if firstRow: 2'f32 else: 1'f32) |
91 firstRow = false | 91 firstRow = false |
92 | 92 |
93 | 93 |
94 when isMainModule: | 94 when isMainModule: |
95 var myengine = initEngine("Input") | 95 var myengine = InitEngine("Input") |
96 | 96 |
97 # transform the cursor a bit to make it look nice | 97 # transform the cursor a bit to make it look nice |
98 let cursorscale = ( | 98 let cursorscale = ( |
99 scale2d(20'f32, 20'f32) * | 99 Scale2d(20'f32, 20'f32) * |
100 translate2d(1'f32, 1'f32) * | 100 Translate2d(1'f32, 1'f32) * |
101 rotate2d(-float32(PI) / 4'f32) * | 101 Rotate2d(-float32(PI) / 4'f32) * |
102 scale2d(0.5'f32, 1'f32) * | 102 Scale2d(0.5'f32, 1'f32) * |
103 rotate2d(float32(PI) / 4'f32) | 103 Rotate2d(float32(PI) / 4'f32) |
104 ) | 104 ) |
105 var positions = arrow | 105 var positions = arrow |
106 for i in 0 ..< positions.len: | 106 for i in 0 ..< positions.len: |
107 positions[i] = cursorscale * NewVec3f(positions[i].x, positions[i].y) | 107 positions[i] = cursorscale * NewVec3f(positions[i].x, positions[i].y) |
108 | 108 |
110 var | 110 var |
111 matDef = MaterialType(name: "default", vertexAttributes: { | 111 matDef = MaterialType(name: "default", vertexAttributes: { |
112 "position": Vec3F32, | 112 "position": Vec3F32, |
113 "color": Vec4F32, | 113 "color": Vec4F32, |
114 }.toTable) | 114 }.toTable) |
115 cursormesh = newMesh( | 115 cursormesh = NewMesh( |
116 positions = positions, | 116 positions = positions, |
117 colors = arrow_colors, | 117 colors = arrow_colors, |
118 material = matDef.InitMaterialData(), | 118 material = matDef.InitMaterialData(), |
119 ) | 119 ) |
120 keyboardmesh = newMesh( | 120 keyboardmesh = NewMesh( |
121 positions = keyvertexpos, | 121 positions = keyvertexpos, |
122 colors = keyvertexcolor, | 122 colors = keyvertexcolor, |
123 indices = keymeshindices, | 123 indices = keymeshindices, |
124 material = matDef.InitMaterialData(), | 124 material = matDef.InitMaterialData(), |
125 ) | 125 ) |
126 backgroundmesh = newMesh( | 126 backgroundmesh = NewMesh( |
127 positions = @[ | 127 positions = @[ |
128 NewVec3f(0'f32, 0'f32), | 128 NewVec3f(0'f32, 0'f32), |
129 NewVec3f(1'f32, 0'f32), | 129 NewVec3f(1'f32, 0'f32), |
130 NewVec3f(1'f32, 1'f32), | 130 NewVec3f(1'f32, 1'f32), |
131 NewVec3f(0'f32, 1'f32), | 131 NewVec3f(0'f32, 1'f32), |
139 indices = @[[0'u16, 1'u16, 2'u16], [2'u16, 3'u16, 0'u16]], | 139 indices = @[[0'u16, 1'u16, 2'u16], [2'u16, 3'u16, 0'u16]], |
140 material = matDef.InitMaterialData(), | 140 material = matDef.InitMaterialData(), |
141 ) | 141 ) |
142 | 142 |
143 # define mesh objects | 143 # define mesh objects |
144 var keyboard_center = translate( | 144 var keyboard_center = Translate( |
145 -float32(rowWidth) / 2'f32, | 145 -float32(rowWidth) / 2'f32, |
146 -float32(tupleLen(keyRows) * (keyDimension + keyGap) - keyGap) / 2'f32, | 146 -float32(tupleLen(keyRows) * (keyDimension + keyGap) - keyGap) / 2'f32, |
147 0'f32 | 147 0'f32 |
148 ) | 148 ) |
149 scene = Scene(name: "scene", meshes: @[backgroundmesh, keyboardmesh, cursormesh]) | 149 scene = Scene(name: "scene", meshes: @[backgroundmesh, keyboardmesh, cursormesh]) |
150 | 150 |
151 # shaders | 151 # shaders |
152 const | 152 const |
153 shaderConfiguration = createShaderConfiguration( | 153 shaderConfiguration = CreateShaderConfiguration( |
154 name = "default shader", | 154 name = "default shader", |
155 inputs = [ | 155 inputs = [ |
156 attr[Vec3f]("position"), | 156 Attr[Vec3f]("position"), |
157 attr[Vec4f]("color", memoryPerformanceHint = PreferFastWrite), | 157 Attr[Vec4f]("color", memoryPerformanceHint = PreferFastWrite), |
158 attr[Mat4]("transform", memoryPerformanceHint = PreferFastWrite, perInstance = true), | 158 Attr[Mat4]("transform", memoryPerformanceHint = PreferFastWrite, perInstance = true), |
159 ], | 159 ], |
160 intermediates = [attr[Vec4f]("outcolor")], | 160 intermediates = [Attr[Vec4f]("outcolor")], |
161 uniforms = [attr[Mat4]("projection")], | 161 uniforms = [Attr[Mat4]("projection")], |
162 outputs = [attr[Vec4f]("color")], | 162 outputs = [Attr[Vec4f]("color")], |
163 vertexCode = """outcolor = color; gl_Position = vec4(position, 1) * (transform * Uniforms.projection);""", | 163 vertexCode = """outcolor = color; gl_Position = vec4(position, 1) * (transform * Uniforms.projection);""", |
164 fragmentCode = "color = outcolor;", | 164 fragmentCode = "color = outcolor;", |
165 ) | 165 ) |
166 | 166 |
167 # set up rendering | 167 # set up rendering |
168 myengine.initRenderer({matDef: shaderConfiguration}) | 168 myengine.InitRenderer({matDef: shaderConfiguration}) |
169 scene.addShaderGlobal("projection", Unit4f32) | 169 scene.AddShaderGlobal("projection", Unit4f32) |
170 myengine.loadScene(scene) | 170 myengine.LoadScene(scene) |
171 myengine.HideSystemCursor() | 171 myengine.HideSystemCursor() |
172 | 172 |
173 # mainloop | 173 # mainloop |
174 while myengine.UpdateInputs(): | 174 while myengine.UpdateInputs(): |
175 if WindowWasResized(): | 175 if WindowWasResized(): |
176 scene.setShaderGlobal("projection", | 176 scene.SetShaderGlobal("projection", |
177 ortho( | 177 Ortho( |
178 0, float32(myengine.GetWindow().size[0]), | 178 0, float32(myengine.GetWindow().Size[0]), |
179 0, float32(myengine.GetWindow().size[1]), | 179 0, float32(myengine.GetWindow().Size[1]), |
180 0, 1, | 180 0, 1, |
181 ) | 181 ) |
182 ) | 182 ) |
183 let | 183 let |
184 winsize = myengine.GetWindow().size | 184 winsize = myengine.GetWindow().Size |
185 center = translate(float32(winsize[0]) / 2'f32, float32(winsize[1]) / 2'f32, 0.1'f32) | 185 center = Translate(float32(winsize[0]) / 2'f32, float32(winsize[1]) / 2'f32, 0.1'f32) |
186 keyboardmesh.transform = keyboard_center * center | 186 keyboardmesh.transform = keyboard_center * center |
187 backgroundmesh.transform = scale(float32(winsize[0]), float32(winsize[1]), 1'f32) | 187 backgroundmesh.transform = Scale(float32(winsize[0]), float32(winsize[1]), 1'f32) |
188 | 188 |
189 let mousePos = translate(MousePosition().x + 20, MousePosition().y + 20, 0'f32) | 189 let mousePos = Translate(MousePosition().x + 20, MousePosition().y + 20, 0'f32) |
190 cursormesh.transform = mousePos | 190 cursormesh.transform = mousePos |
191 | 191 |
192 for (index, key) in enumerate(keyIndices): | 192 for (index, key) in enumerate(keyIndices): |
193 if KeyWasPressed(key): | 193 if KeyWasPressed(key): |
194 let baseIndex = index * 4 | 194 let baseIndex = index * 4 |
201 keyboardmesh["color", baseIndex + 0] = baseColor | 201 keyboardmesh["color", baseIndex + 0] = baseColor |
202 keyboardmesh["color", baseIndex + 1] = baseColor | 202 keyboardmesh["color", baseIndex + 1] = baseColor |
203 keyboardmesh["color", baseIndex + 2] = baseColor | 203 keyboardmesh["color", baseIndex + 2] = baseColor |
204 keyboardmesh["color", baseIndex + 3] = baseColor | 204 keyboardmesh["color", baseIndex + 3] = baseColor |
205 | 205 |
206 myengine.renderScene(scene) | 206 myengine.RenderScene(scene) |
207 | 207 |
208 myengine.destroy() | 208 myengine.Destroy() |