changeset 726:443791a202c8

add: font-test
author Sam <sam@basx.dev>
date Sat, 27 May 2023 13:45:03 +0700
parents 5c08c45b51b9
children 360664dfc17d
files tests/test_font.nim
diffstat 1 files changed, 54 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/test_font.nim	Sat May 27 13:45:03 2023 +0700
@@ -0,0 +1,54 @@
+import semicongine
+
+proc main() =
+  var scene = newScene("main", root=newEntity("rect", rect()))
+  var font = loadFont("DejaVuSans.ttf", "myfont")
+  var img = loadImage("flag.png")
+
+  var textbox = newTextbox(10, 10, font, "Joni")
+  scene.root.add textbox
+
+  var sampler = DefaultSampler()
+  sampler.magnification = VK_FILTER_NEAREST
+  sampler.minification = VK_FILTER_NEAREST
+  scene.addTextures("my_texture", @[
+    Texture(image: img, sampler: sampler)
+  ])
+  scene.addShaderGlobalArray("test2", @[1'f32, 2'f32])
+
+  var engine = initEngine("Test fonts")
+
+  const
+    vertexInput = @[
+      attr[Vec3f]("position", memoryPerformanceHint=PreferFastRead),
+      attr[Vec2f]("uv", memoryPerformanceHint=PreferFastRead),
+    ]
+    vertexOutput = @[attr[Vec2f]("uvout")]
+    uniforms = @[attr[float32]("test2", arrayCount=2)]
+    samplers = @[attr[Sampler2DType]("my_texture", arrayCount=1)]
+    fragOutput = @[attr[Vec4f]("color")]
+    vertexCode = compileGlslShader(
+      stage=VK_SHADER_STAGE_VERTEX_BIT,
+      inputs=vertexInput,
+      uniforms=uniforms,
+      samplers=samplers,
+      outputs=vertexOutput,
+      main="""gl_Position = vec4(position, 1.0); uvout = uv;"""
+    )
+    fragmentCode = compileGlslShader(
+      stage=VK_SHADER_STAGE_FRAGMENT_BIT,
+      inputs=vertexOutput,
+      uniforms=uniforms,
+      samplers=samplers,
+      outputs=fragOutput,
+      main="""color = texture(my_texture[0], uvout);"""
+    )
+  engine.setRenderer(engine.gpuDevice.simpleForwardRenderPass(vertexCode, fragmentCode))
+  engine.addScene(scene, vertexInput, samplers)
+  while engine.updateInputs() == Running and not engine.keyIsDown(Escape):
+    engine.renderScene(scene)
+  engine.destroy()
+
+
+when isMainModule:
+  main()