annotate tests/test_font.nim @ 733:07c755e65a4a

add: final font-rendering, API changes fixed
author Sam <sam@basx.dev>
date Tue, 30 May 2023 16:59:01 +0700
parents 443791a202c8
children b145a05c2459
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
733
07c755e65a4a add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents: 726
diff changeset
1 import std/unicode
07c755e65a4a add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents: 726
diff changeset
2 import std/tables
07c755e65a4a add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents: 726
diff changeset
3
726
443791a202c8 add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
4 import semicongine
443791a202c8 add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
5
443791a202c8 add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
6 proc main() =
443791a202c8 add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
7 var sampler = DefaultSampler()
443791a202c8 add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
8 sampler.magnification = VK_FILTER_NEAREST
443791a202c8 add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
9 sampler.minification = VK_FILTER_NEAREST
733
07c755e65a4a add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents: 726
diff changeset
10 var font = loadFont("DejaVuSans.ttf", color=newVec4f(1, 0.5, 0.5, 1), resolution=20)
07c755e65a4a add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents: 726
diff changeset
11
07c755e65a4a add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents: 726
diff changeset
12 var scene = newScene("main", root=newEntity("rect"))
726
443791a202c8 add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
13
733
07c755e65a4a add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents: 726
diff changeset
14 var flag = rect()
07c755e65a4a add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents: 726
diff changeset
15 flag.setInstanceData("material", @[0'u8])
07c755e65a4a add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents: 726
diff changeset
16 # scene.root.add flag
07c755e65a4a add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents: 726
diff changeset
17 scene.addMaterial Material(name: "material", textures: {"textures": Texture(image: loadImage("flag.png"), sampler: sampler)}.toTable)
07c755e65a4a add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents: 726
diff changeset
18
07c755e65a4a add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents: 726
diff changeset
19 var textbox = newTextbox(32, font, "".toRunes)
07c755e65a4a add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents: 726
diff changeset
20 scene.addMaterial Material(name: "fontMaterial", textures: {"textures": font.fontAtlas}.toTable)
07c755e65a4a add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents: 726
diff changeset
21 textbox.mesh.setInstanceData("material", @[1'u8])
07c755e65a4a add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents: 726
diff changeset
22 textbox.transform = scale3d(0.1, 0.1)
07c755e65a4a add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents: 726
diff changeset
23 scene.root.add textbox
726
443791a202c8 add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
24
443791a202c8 add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
25 const
443791a202c8 add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
26 vertexInput = @[
733
07c755e65a4a add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents: 726
diff changeset
27 attr[Mat4]("transform", memoryPerformanceHint=PreferFastRead, perInstance=true),
726
443791a202c8 add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
28 attr[Vec3f]("position", memoryPerformanceHint=PreferFastRead),
443791a202c8 add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
29 attr[Vec2f]("uv", memoryPerformanceHint=PreferFastRead),
733
07c755e65a4a add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents: 726
diff changeset
30 attr[uint8]("material", memoryPerformanceHint=PreferFastRead, perInstance=true),
726
443791a202c8 add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
31 ]
733
07c755e65a4a add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents: 726
diff changeset
32 intermediate = @[attr[Vec2f]("uvout"), attr[uint8]("materialId", noInterpolation=true)]
07c755e65a4a add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents: 726
diff changeset
33 samplers = @[attr[Sampler2DType]("textures", arrayCount=2)]
07c755e65a4a add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents: 726
diff changeset
34 uniforms = @[attr[Mat4]("perspective")]
726
443791a202c8 add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
35 fragOutput = @[attr[Vec4f]("color")]
733
07c755e65a4a add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents: 726
diff changeset
36 (vertexCode, fragmentCode) = compileVertexFragmentShaderSet(
726
443791a202c8 add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
37 inputs=vertexInput,
733
07c755e65a4a add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents: 726
diff changeset
38 intermediate=intermediate,
07c755e65a4a add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents: 726
diff changeset
39 outputs=fragOutput,
726
443791a202c8 add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
40 samplers=samplers,
733
07c755e65a4a add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents: 726
diff changeset
41 uniforms=uniforms,
07c755e65a4a add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents: 726
diff changeset
42 vertexCode="""gl_Position = vec4(position, 1.0) * (transform * Uniforms.perspective); uvout = uv; materialId = material;""",
07c755e65a4a add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents: 726
diff changeset
43 fragmentCode="""color = texture(textures[materialId], uvout);""",
726
443791a202c8 add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
44 )
733
07c755e65a4a add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents: 726
diff changeset
45
07c755e65a4a add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents: 726
diff changeset
46 var engine = initEngine("Test fonts")
726
443791a202c8 add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
47 engine.setRenderer(engine.gpuDevice.simpleForwardRenderPass(vertexCode, fragmentCode))
443791a202c8 add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
48 engine.addScene(scene, vertexInput, samplers)
733
07c755e65a4a add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents: 726
diff changeset
49 scene.addShaderGlobal("perspective", Unit4F32)
07c755e65a4a add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents: 726
diff changeset
50
726
443791a202c8 add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
51 while engine.updateInputs() == Running and not engine.keyIsDown(Escape):
733
07c755e65a4a add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents: 726
diff changeset
52 if engine.windowWasResized():
07c755e65a4a add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents: 726
diff changeset
53 var winSize = engine.getWindow().size
07c755e65a4a add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents: 726
diff changeset
54 scene.setShaderGlobal("perspective", orthoWindowAspect(winSize[1] / winSize[0]))
07c755e65a4a add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents: 726
diff changeset
55 for c in [Key.A, Key.B, Key.C, Key.D, Key.E, Key.F, Key.G, Key.H, Key.I, Key.J, Key.K, Key.L, Key.M, Key.N, Key.O, Key.P, Key.Q, Key.R, Key.S, Key.T, Key.U, Key.V, Key.W, Key.X, Key.Y, Key.Z]:
07c755e65a4a add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents: 726
diff changeset
56 if engine.keyWasPressed(c):
07c755e65a4a add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents: 726
diff changeset
57 if engine.keyIsDown(ShiftL) or engine.keyIsDown(ShiftR):
07c755e65a4a add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents: 726
diff changeset
58 textbox.text = textbox.text & ($c).toRunes
07c755e65a4a add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents: 726
diff changeset
59 else:
07c755e65a4a add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents: 726
diff changeset
60 textbox.text = textbox.text & ($c).toRunes[0].toLower()
07c755e65a4a add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents: 726
diff changeset
61 if engine.keyWasPressed(Space):
07c755e65a4a add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents: 726
diff changeset
62 textbox.text = textbox.text & " ".toRunes[0]
07c755e65a4a add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents: 726
diff changeset
63 if engine.keyWasPressed(Backspace) and textbox.text.len > 0:
07c755e65a4a add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents: 726
diff changeset
64 textbox.text = textbox.text[0 ..< ^1]
726
443791a202c8 add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
65 engine.renderScene(scene)
443791a202c8 add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
66 engine.destroy()
443791a202c8 add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
67
443791a202c8 add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
68
443791a202c8 add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
69 when isMainModule:
443791a202c8 add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
70 main()