annotate tests/test_font.nim @ 316:b145a05c2459

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