diff old_tests/test_collision.nim @ 1203:6360c8d17ce0 compiletime-tests

did: preprations to add rendering tests
author sam <sam@basx.dev>
date Mon, 15 Jul 2024 20:06:42 +0700
parents tests/test_collision.nim@114f395b9144
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/old_tests/test_collision.nim	Mon Jul 15 20:06:42 2024 +0700
@@ -0,0 +1,59 @@
+import semicongine
+
+proc main() =
+  var scene = Scene(name: "main")
+
+  scene.Add Rect(color = "f00f")
+  scene.Add Rect()
+  scene.Add Circle(color = "0f0f")
+  scene.meshes[0].material = VERTEX_COLORED_MATERIAL.InitMaterialData()
+  scene.meshes[1].material = VERTEX_COLORED_MATERIAL.InitMaterialData()
+  scene.meshes[2].material = VERTEX_COLORED_MATERIAL.InitMaterialData()
+  scene.meshes[1].transform = Scale(0.8, 0.8)
+  scene.meshes[2].transform = Scale(0.1, 0.1)
+  scene.AddShaderGlobal("perspective", Unit4F32)
+
+  const
+    shaderConfiguration = CreateShaderConfiguration(
+      name = "default shader",
+      inputs = [
+        Attr[Mat4]("transform", memoryPerformanceHint = PreferFastRead, perInstance = true),
+        Attr[Vec3f]("position", memoryPerformanceHint = PreferFastRead),
+        Attr[Vec4f]("color", memoryPerformanceHint = PreferFastRead),
+      ],
+      intermediates = [Attr[Vec4f]("colorout")],
+      uniforms = [Attr[Mat4]("perspective")],
+      outputs = [Attr[Vec4f]("fragcolor")],
+      vertexCode = """gl_Position = vec4(position, 1.0) * (transform * Uniforms.perspective); colorout = color;""",
+      fragmentCode = """fragcolor = colorout;""",
+    )
+
+  var engine = InitEngine("Test collisions")
+
+  engine.InitRenderer({VERTEX_COLORED_MATERIAL: shaderConfiguration})
+  engine.LoadScene(scene)
+
+  while engine.UpdateInputs() and not KeyIsDown(Escape):
+    if WindowWasResized():
+      var winSize = engine.GetWindow().Size
+      scene.SetShaderGlobal("perspective", OrthoWindowAspect(winSize[0] / winSize[1]))
+    if KeyIsDown(A): scene.meshes[0].transform = scene.meshes[0].transform * Translate(-0.001, 0, 0)
+    if KeyIsDown(D): scene.meshes[0].transform = scene.meshes[0].transform * Translate(0.001, 0, 0)
+    if KeyIsDown(W): scene.meshes[0].transform = scene.meshes[0].transform * Translate(0, -0.001, 0)
+    if KeyIsDown(S): scene.meshes[0].transform = scene.meshes[0].transform * Translate(0, 0.001, 0)
+    if KeyIsDown(Q): scene.meshes[0].transform = scene.meshes[0].transform * Rotate(-0.001, Z)
+    if KeyIsDown(Key.E): scene.meshes[0].transform = scene.meshes[0].transform * Rotate(0.001, Z)
+
+    if KeyIsDown(Key.Z): scene.meshes[1].transform = scene.meshes[1].transform * Rotate(-0.001, Z)
+    if KeyIsDown(Key.X): scene.meshes[1].transform = scene.meshes[1].transform * Rotate(0.001, Z)
+    if KeyIsDown(Key.C): scene.meshes[1].transform = scene.meshes[1].transform * Translate(0, -0.001, 0)
+    if KeyIsDown(Key.V): scene.meshes[1].transform = scene.meshes[1].transform * Translate(0, 0.001, 0)
+    let hitbox = Collider(theType: Box, transform: scene.meshes[0].transform * Translate(-0.5, -0.5))
+    let hitsphere = Collider(theType: Sphere, transform: scene.meshes[2].transform, radius: 0.5)
+    echo Intersects(hitbox, hitsphere)
+    engine.RenderScene(scene)
+  engine.Destroy()
+
+
+when isMainModule:
+  main()