annotate tests/test_font.nim @ 415:25db1fa56cb7

add: font/text improvments, support for newline rendering
author Sam <sam@basx.dev>
date Sat, 27 Jan 2024 00:31:11 +0700
parents 8fb4387b2ea1
children 73cca428e27a
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
265
ad4f2b45410b add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
3 import semicongine
ad4f2b45410b add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
4
ad4f2b45410b add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
5 proc main() =
371
f054b8bacab8 fix: tests, test_materials and test_mesh still needs to be done
Sam <sam@basx.dev>
parents: 344
diff changeset
6 # setup engine
f054b8bacab8 fix: tests, test_materials and test_mesh still needs to be done
Sam <sam@basx.dev>
parents: 344
diff changeset
7 var engine = initEngine("Test fonts")
f054b8bacab8 fix: tests, test_materials and test_mesh still needs to be done
Sam <sam@basx.dev>
parents: 344
diff changeset
8 engine.initRenderer([])
f054b8bacab8 fix: tests, test_materials and test_mesh still needs to be done
Sam <sam@basx.dev>
parents: 344
diff changeset
9
f054b8bacab8 fix: tests, test_materials and test_mesh still needs to be done
Sam <sam@basx.dev>
parents: 344
diff changeset
10 # build scene
f054b8bacab8 fix: tests, test_materials and test_mesh still needs to be done
Sam <sam@basx.dev>
parents: 344
diff changeset
11 var scene = Scene(name: "main")
411
e50c57423567 did: image & font refactoring, add texture-atlas-packing
Sam <sam@basx.dev>
parents: 371
diff changeset
12 # var font = loadFont("DejaVuSans.ttf", lineHeightPixels=90'f32, charset="abcdefghijklmnopqrstuvwxyz ".toRunes)
412
262e771e2573 add: packed textures for font-atlas
Sam <sam@basx.dev>
parents: 411
diff changeset
13 var font = loadFont("DejaVuSans.ttf", lineHeightPixels=180'f32)
411
e50c57423567 did: image & font refactoring, add texture-atlas-packing
Sam <sam@basx.dev>
parents: 371
diff changeset
14 var textbox = initText(32, font, "", color=newVec4f(1, 0, 0, 1))
413
8fb4387b2ea1 add: packed textures for font-atlas
Sam <sam@basx.dev>
parents: 412
diff changeset
15 let fontscale = 0.002
331
05fb85ba97dd did: undid using meshes as values, ref is much better, fix a few things, fix a few huge performance issues
Sam <sam@basx.dev>
parents: 330
diff changeset
16 scene.add textbox
411
e50c57423567 did: image & font refactoring, add texture-atlas-packing
Sam <sam@basx.dev>
parents: 371
diff changeset
17 textbox.mesh.transform = scale(fontscale, fontscale)
371
f054b8bacab8 fix: tests, test_materials and test_mesh still needs to be done
Sam <sam@basx.dev>
parents: 344
diff changeset
18 engine.loadScene(scene)
272
bfcb41211c5b add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents: 265
diff changeset
19
265
ad4f2b45410b add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
20 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
21 if engine.windowWasResized():
bfcb41211c5b add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents: 265
diff changeset
22 var winSize = engine.getWindow().size
411
e50c57423567 did: image & font refactoring, add texture-atlas-packing
Sam <sam@basx.dev>
parents: 371
diff changeset
23 textbox.mesh.transform = scale(fontscale * (winSize[1] / winSize[0]), fontscale)
272
bfcb41211c5b add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents: 265
diff changeset
24 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
25 if engine.keyWasPressed(c):
bfcb41211c5b add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents: 265
diff changeset
26 if engine.keyIsDown(ShiftL) or engine.keyIsDown(ShiftR):
bfcb41211c5b add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents: 265
diff changeset
27 textbox.text = textbox.text & ($c).toRunes
bfcb41211c5b add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents: 265
diff changeset
28 else:
bfcb41211c5b add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents: 265
diff changeset
29 textbox.text = textbox.text & ($c).toRunes[0].toLower()
415
25db1fa56cb7 add: font/text improvments, support for newline rendering
Sam <sam@basx.dev>
parents: 413
diff changeset
30 if engine.keyWasPressed(Enter):
25db1fa56cb7 add: font/text improvments, support for newline rendering
Sam <sam@basx.dev>
parents: 413
diff changeset
31 textbox.text = textbox.text & Rune('\n')
272
bfcb41211c5b add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents: 265
diff changeset
32 if engine.keyWasPressed(Space):
415
25db1fa56cb7 add: font/text improvments, support for newline rendering
Sam <sam@basx.dev>
parents: 413
diff changeset
33 textbox.text = textbox.text & Rune(' ')
272
bfcb41211c5b add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents: 265
diff changeset
34 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
35 textbox.text = textbox.text[0 ..< ^1]
265
ad4f2b45410b add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
36 engine.renderScene(scene)
ad4f2b45410b add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
37 engine.destroy()
ad4f2b45410b add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
38
ad4f2b45410b add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
39
ad4f2b45410b add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
40 when isMainModule:
ad4f2b45410b add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
41 main()