comparison tests/test_collision.nim @ 1021:73b572f82a1f

add: bases for a better input-system
author sam <sam@basx.dev>
date Thu, 09 May 2024 23:02:35 +0700
parents f054b8bacab8
children 74957cbf589b
comparison
equal deleted inserted replaced
1020:355ef428b5f4 1021:73b572f82a1f
1 import semicongine 1 import semicongine
2 2
3 proc main() = 3 proc main() =
4 var scene = Scene(name: "main") 4 var scene = Scene(name: "main")
5 5
6 scene.add rect(color="f00f") 6 scene.add rect(color = "f00f")
7 scene.add rect() 7 scene.add rect()
8 scene.add circle(color="0f0f") 8 scene.add circle(color = "0f0f")
9 scene.meshes[0].material = VERTEX_COLORED_MATERIAL.initMaterialData() 9 scene.meshes[0].material = VERTEX_COLORED_MATERIAL.initMaterialData()
10 scene.meshes[1].material = VERTEX_COLORED_MATERIAL.initMaterialData() 10 scene.meshes[1].material = VERTEX_COLORED_MATERIAL.initMaterialData()
11 scene.meshes[2].material = VERTEX_COLORED_MATERIAL.initMaterialData() 11 scene.meshes[2].material = VERTEX_COLORED_MATERIAL.initMaterialData()
12 scene.meshes[1].transform = scale(0.8, 0.8) 12 scene.meshes[1].transform = scale(0.8, 0.8)
13 scene.meshes[2].transform = scale(0.1, 0.1) 13 scene.meshes[2].transform = scale(0.1, 0.1)
14 scene.addShaderGlobal("perspective", Unit4F32) 14 scene.addShaderGlobal("perspective", Unit4F32)
15 15
16 const 16 const
17 shaderConfiguration = createShaderConfiguration( 17 shaderConfiguration = createShaderConfiguration(
18 inputs=[ 18 name = "default shader",
19 attr[Mat4]("transform", memoryPerformanceHint=PreferFastRead, perInstance=true), 19 inputs = [
20 attr[Vec3f]("position", memoryPerformanceHint=PreferFastRead), 20 attr[Mat4]("transform", memoryPerformanceHint = PreferFastRead, perInstance = true),
21 attr[Vec4f]("color", memoryPerformanceHint=PreferFastRead), 21 attr[Vec3f]("position", memoryPerformanceHint = PreferFastRead),
22 attr[Vec4f]("color", memoryPerformanceHint = PreferFastRead),
22 ], 23 ],
23 intermediates=[attr[Vec4f]("colorout")], 24 intermediates = [attr[Vec4f]("colorout")],
24 uniforms=[attr[Mat4]("perspective")], 25 uniforms = [attr[Mat4]("perspective")],
25 outputs=[attr[Vec4f]("fragcolor")], 26 outputs = [attr[Vec4f]("fragcolor")],
26 vertexCode="""gl_Position = vec4(position, 1.0) * (transform * Uniforms.perspective); colorout = color;""", 27 vertexCode = """gl_Position = vec4(position, 1.0) * (transform * Uniforms.perspective); colorout = color;""",
27 fragmentCode="""fragcolor = colorout;""", 28 fragmentCode = """fragcolor = colorout;""",
28 ) 29 )
29 30
30 var engine = initEngine("Test collisions") 31 var engine = initEngine("Test collisions")
31 32
32 engine.initRenderer({VERTEX_COLORED_MATERIAL: shaderConfiguration}) 33 engine.initRenderer({VERTEX_COLORED_MATERIAL: shaderConfiguration})
33 engine.loadScene(scene) 34 engine.loadScene(scene)
34 35
35 while engine.updateInputs() == Running and not engine.keyIsDown(Escape): 36 while engine.updateInputs() and not keyIsDown(Escape):
36 if engine.windowWasResized(): 37 if windowWasResized():
37 var winSize = engine.getWindow().size 38 var winSize = engine.getWindow().size
38 scene.setShaderGlobal("perspective", orthoWindowAspect(winSize[1] / winSize[0])) 39 scene.setShaderGlobal("perspective", orthoWindowAspect(winSize[0] / winSize[1]))
39 if engine.keyIsDown(A): scene.meshes[0].transform = scene.meshes[0].transform * translate(-0.001, 0, 0) 40 if keyIsDown(A): scene.meshes[0].transform = scene.meshes[0].transform * translate(-0.001, 0, 0)
40 if engine.keyIsDown(D): scene.meshes[0].transform = scene.meshes[0].transform * translate( 0.001, 0, 0) 41 if keyIsDown(D): scene.meshes[0].transform = scene.meshes[0].transform * translate(0.001, 0, 0)
41 if engine.keyIsDown(W): scene.meshes[0].transform = scene.meshes[0].transform * translate( 0, -0.001, 0) 42 if keyIsDown(W): scene.meshes[0].transform = scene.meshes[0].transform * translate(0, -0.001, 0)
42 if engine.keyIsDown(S): scene.meshes[0].transform = scene.meshes[0].transform * translate( 0, 0.001, 0) 43 if keyIsDown(S): scene.meshes[0].transform = scene.meshes[0].transform * translate(0, 0.001, 0)
43 if engine.keyIsDown(Q): scene.meshes[0].transform = scene.meshes[0].transform * rotate(-0.001, Z) 44 if keyIsDown(Q): scene.meshes[0].transform = scene.meshes[0].transform * rotate(-0.001, Z)
44 if engine.keyIsDown(Key.E): scene.meshes[0].transform = scene.meshes[0].transform * rotate( 0.001, Z) 45 if keyIsDown(Key.E): scene.meshes[0].transform = scene.meshes[0].transform * rotate(0.001, Z)
45 46
46 if engine.keyIsDown(Key.Z): scene.meshes[1].transform = scene.meshes[1].transform * rotate(-0.001, Z) 47 if keyIsDown(Key.Z): scene.meshes[1].transform = scene.meshes[1].transform * rotate(-0.001, Z)
47 if engine.keyIsDown(Key.X): scene.meshes[1].transform = scene.meshes[1].transform * rotate( 0.001, Z) 48 if keyIsDown(Key.X): scene.meshes[1].transform = scene.meshes[1].transform * rotate(0.001, Z)
48 if engine.keyIsDown(Key.C): scene.meshes[1].transform = scene.meshes[1].transform * translate(0, -0.001, 0) 49 if keyIsDown(Key.C): scene.meshes[1].transform = scene.meshes[1].transform * translate(0, -0.001, 0)
49 if engine.keyIsDown(Key.V): scene.meshes[1].transform = scene.meshes[1].transform * translate(0, 0.001, 0) 50 if keyIsDown(Key.V): scene.meshes[1].transform = scene.meshes[1].transform * translate(0, 0.001, 0)
50 let hitbox = Collider(theType: Box, transform: scene.meshes[0].transform * translate(-0.5, -0.5)) 51 let hitbox = Collider(theType: Box, transform: scene.meshes[0].transform * translate(-0.5, -0.5))
51 let hitsphere = Collider(theType: Sphere, transform: scene.meshes[2].transform, radius: 0.5) 52 let hitsphere = Collider(theType: Sphere, transform: scene.meshes[2].transform, radius: 0.5)
52 echo intersects(hitbox, hitsphere) 53 echo intersects(hitbox, hitsphere)
53 engine.renderScene(scene) 54 engine.renderScene(scene)
54 engine.destroy() 55 engine.destroy()