comparison examples/E04_input.nim @ 1132:7f2c477ae1f4

merge
author sam <sam@basx.dev>
date Thu, 23 May 2024 02:26:37 +0700
parents d6c27f0ed3e4
children 71315636ba82
comparison
equal deleted inserted replaced
1131:99869a2b8a53 1132:7f2c477ae1f4
1 import std/enumerate 1 import std/enumerate
2 import std/tables 2 import std/tables
3 import std/typetraits 3 import std/typetraits
4 import std/math 4 import std/math
5 5
6 import ../src/semicongine 6 import ../semicongine
7 7
8 const 8 const
9 arrow = @[ 9 arrow = @[
10 newVec3f(-1, -1), 10 newVec3f(-1, -1),
11 newVec3f(1, -1), 11 newVec3f(1, -1),
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
109 # define mesh objects 109 # define mesh objects
110 var 110 var
111 material = Material(name: "default") 111 matDef = MaterialType(name: "default", vertexAttributes: {
112 "position": Vec3F32,
113 "color": Vec4F32,
114 }.toTable)
112 cursormesh = newMesh( 115 cursormesh = newMesh(
113 positions=positions, 116 positions = positions,
114 colors=arrow_colors, 117 colors = arrow_colors,
115 material=material, 118 material = matDef.initMaterialData(),
116 ) 119 )
117 keyboardmesh = newMesh( 120 keyboardmesh = newMesh(
118 positions=keyvertexpos, 121 positions = keyvertexpos,
119 colors=keyvertexcolor, 122 colors = keyvertexcolor,
120 indices=keymeshindices, 123 indices = keymeshindices,
121 material=material, 124 material = matDef.initMaterialData(),
122 ) 125 )
123 backgroundmesh = newMesh( 126 backgroundmesh = newMesh(
124 positions= @[ 127 positions = @[
125 newVec3f(0'f32, 0'f32), 128 newVec3f(0'f32, 0'f32),
126 newVec3f(1'f32, 0'f32), 129 newVec3f(1'f32, 0'f32),
127 newVec3f(1'f32, 1'f32), 130 newVec3f(1'f32, 1'f32),
128 newVec3f(0'f32, 1'f32), 131 newVec3f(0'f32, 1'f32),
129 ], 132 ],
130 colors= @[ 133 colors = @[
131 backgroundColor, 134 backgroundColor,
132 backgroundColor, 135 backgroundColor,
133 backgroundColor, 136 backgroundColor,
134 backgroundColor, 137 backgroundColor,
135 ], 138 ],
136 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]],
137 material=material, 140 material = matDef.initMaterialData(),
138 ) 141 )
139 142
140 # define mesh objects 143 # define mesh objects
141 var keyboard_center = translate( 144 var keyboard_center = translate(
142 -float32(rowWidth) / 2'f32, 145 -float32(rowWidth) / 2'f32,
146 scene = Scene(name: "scene", meshes: @[backgroundmesh, keyboardmesh, cursormesh]) 149 scene = Scene(name: "scene", meshes: @[backgroundmesh, keyboardmesh, cursormesh])
147 150
148 # shaders 151 # shaders
149 const 152 const
150 shaderConfiguration = createShaderConfiguration( 153 shaderConfiguration = createShaderConfiguration(
151 inputs=[ 154 name = "default shader",
155 inputs = [
152 attr[Vec3f]("position"), 156 attr[Vec3f]("position"),
153 attr[Vec4f]("color", memoryPerformanceHint=PreferFastWrite), 157 attr[Vec4f]("color", memoryPerformanceHint = PreferFastWrite),
154 attr[Mat4]("transform", memoryPerformanceHint=PreferFastWrite, perInstance=true), 158 attr[Mat4]("transform", memoryPerformanceHint = PreferFastWrite, perInstance = true),
155 ], 159 ],
156 intermediates=[attr[Vec4f]("outcolor")], 160 intermediates = [attr[Vec4f]("outcolor")],
157 uniforms=[attr[Mat4]("projection")], 161 uniforms = [attr[Mat4]("projection")],
158 outputs=[attr[Vec4f]("color")], 162 outputs = [attr[Vec4f]("color")],
159 vertexCode="""outcolor = color; gl_Position = vec4(position, 1) * (transform * Uniforms.projection);""", 163 vertexCode = """outcolor = color; gl_Position = vec4(position, 1) * (transform * Uniforms.projection);""",
160 fragmentCode="color = outcolor;", 164 fragmentCode = "color = outcolor;",
161 ) 165 )
162 166
163 # set up rendering 167 # set up rendering
164 myengine.initRenderer({"default": shaderConfiguration}.toTable) 168 myengine.initRenderer({matDef: shaderConfiguration})
165 scene.addShaderGlobal("projection", Unit4f32) 169 scene.addShaderGlobal("projection", Unit4f32)
166 myengine.addScene(scene) 170 myengine.loadScene(scene)
167 myengine.hideSystemCursor() 171 myengine.HideSystemCursor()
168 172
169 # mainloop 173 # mainloop
170 while myengine.updateInputs() == Running: 174 while myengine.UpdateInputs():
171 if myengine.windowWasResized(): 175 if WindowWasResized():
172 scene.setShaderGlobal("projection", 176 scene.setShaderGlobal("projection",
173 ortho( 177 ortho(
174 0, float32(myengine.getWindow().size[0]), 178 0, float32(myengine.GetWindow().size[0]),
175 0, float32(myengine.getWindow().size[1]), 179 0, float32(myengine.GetWindow().size[1]),
176 0, 1, 180 0, 1,
177 ) 181 )
178 ) 182 )
179 let 183 let
180 winsize = myengine.getWindow().size 184 winsize = myengine.GetWindow().size
181 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)
182 keyboardmesh.transform = keyboard_center * center 186 keyboardmesh.transform = keyboard_center * center
183 backgroundmesh.transform = scale(float32(winsize[0]), float32(winsize[1]), 1'f32) 187 backgroundmesh.transform = scale(float32(winsize[0]), float32(winsize[1]), 1'f32)
184 188
185 let mousePos = translate(myengine.mousePosition().x + 20, myengine.mousePosition().y + 20, 0'f32) 189 let mousePos = translate(MousePosition().x + 20, MousePosition().y + 20, 0'f32)
186 cursormesh.transform = mousePos 190 cursormesh.transform = mousePos
187 191
188 for (index, key) in enumerate(keyIndices): 192 for (index, key) in enumerate(keyIndices):
189 if myengine.keyWasPressed(key): 193 if KeyWasPressed(key):
190 let baseIndex = index * 4 194 let baseIndex = index * 4
191 keyboardmesh["color", baseIndex + 0] = activeColor 195 keyboardmesh["color", baseIndex + 0] = activeColor
192 keyboardmesh["color", baseIndex + 1] = activeColor 196 keyboardmesh["color", baseIndex + 1] = activeColor
193 keyboardmesh["color", baseIndex + 2] = activeColor 197 keyboardmesh["color", baseIndex + 2] = activeColor
194 keyboardmesh["color", baseIndex + 3] = activeColor 198 keyboardmesh["color", baseIndex + 3] = activeColor
195 if myengine.keyWasReleased(key): 199 if KeyWasReleased(key):
196 let baseIndex = index * 4 200 let baseIndex = index * 4
197 keyboardmesh["color", baseIndex + 0] = baseColor 201 keyboardmesh["color", baseIndex + 0] = baseColor
198 keyboardmesh["color", baseIndex + 1] = baseColor 202 keyboardmesh["color", baseIndex + 1] = baseColor
199 keyboardmesh["color", baseIndex + 2] = baseColor 203 keyboardmesh["color", baseIndex + 2] = baseColor
200 keyboardmesh["color", baseIndex + 3] = baseColor 204 keyboardmesh["color", baseIndex + 3] = baseColor