annotate tests/test_font.nim @ 731:c12b11a5f112

did: overhaul some of the mesh-data uploading and transformation handling, added: text/font rendering
author Sam <sam@basx.dev>
date Tue, 30 May 2023 16:58:14 +0700
parents 443791a202c8
children bfcb41211c5b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
726
443791a202c8 add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
1 import semicongine
443791a202c8 add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
2
443791a202c8 add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
3 proc main() =
443791a202c8 add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
4 var scene = newScene("main", root=newEntity("rect", rect()))
443791a202c8 add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
5 var font = loadFont("DejaVuSans.ttf", "myfont")
443791a202c8 add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
6 var img = loadImage("flag.png")
443791a202c8 add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
7
443791a202c8 add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
8 var textbox = newTextbox(10, 10, font, "Joni")
443791a202c8 add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
9 scene.root.add textbox
443791a202c8 add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
10
443791a202c8 add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
11 var sampler = DefaultSampler()
443791a202c8 add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
12 sampler.magnification = VK_FILTER_NEAREST
443791a202c8 add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
13 sampler.minification = VK_FILTER_NEAREST
443791a202c8 add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
14 scene.addTextures("my_texture", @[
443791a202c8 add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
15 Texture(image: img, sampler: sampler)
443791a202c8 add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
16 ])
443791a202c8 add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
17 scene.addShaderGlobalArray("test2", @[1'f32, 2'f32])
443791a202c8 add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
18
443791a202c8 add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
19 var engine = initEngine("Test fonts")
443791a202c8 add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
20
443791a202c8 add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
21 const
443791a202c8 add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
22 vertexInput = @[
443791a202c8 add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
23 attr[Vec3f]("position", memoryPerformanceHint=PreferFastRead),
443791a202c8 add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
24 attr[Vec2f]("uv", memoryPerformanceHint=PreferFastRead),
443791a202c8 add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
25 ]
443791a202c8 add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
26 vertexOutput = @[attr[Vec2f]("uvout")]
443791a202c8 add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
27 uniforms = @[attr[float32]("test2", arrayCount=2)]
443791a202c8 add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
28 samplers = @[attr[Sampler2DType]("my_texture", arrayCount=1)]
443791a202c8 add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
29 fragOutput = @[attr[Vec4f]("color")]
443791a202c8 add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
30 vertexCode = compileGlslShader(
443791a202c8 add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
31 stage=VK_SHADER_STAGE_VERTEX_BIT,
443791a202c8 add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
32 inputs=vertexInput,
443791a202c8 add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
33 uniforms=uniforms,
443791a202c8 add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
34 samplers=samplers,
443791a202c8 add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
35 outputs=vertexOutput,
443791a202c8 add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
36 main="""gl_Position = vec4(position, 1.0); uvout = uv;"""
443791a202c8 add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
37 )
443791a202c8 add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
38 fragmentCode = compileGlslShader(
443791a202c8 add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
39 stage=VK_SHADER_STAGE_FRAGMENT_BIT,
443791a202c8 add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
40 inputs=vertexOutput,
443791a202c8 add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
41 uniforms=uniforms,
443791a202c8 add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
42 samplers=samplers,
443791a202c8 add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
43 outputs=fragOutput,
443791a202c8 add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
44 main="""color = texture(my_texture[0], uvout);"""
443791a202c8 add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
45 )
443791a202c8 add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
46 engine.setRenderer(engine.gpuDevice.simpleForwardRenderPass(vertexCode, fragmentCode))
443791a202c8 add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
47 engine.addScene(scene, vertexInput, samplers)
443791a202c8 add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
48 while engine.updateInputs() == Running and not engine.keyIsDown(Escape):
443791a202c8 add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
49 engine.renderScene(scene)
443791a202c8 add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
50 engine.destroy()
443791a202c8 add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
51
443791a202c8 add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
52
443791a202c8 add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
53 when isMainModule:
443791a202c8 add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
54 main()