Mercurial > games > semicongine
annotate tests/test_font.nim @ 904:a253a691e0d6
fix: font-api, allow consistent use of mesh-transform
author | Sam <sam@basx.dev> |
---|---|
date | Sat, 24 Feb 2024 14:31:15 +0700 |
parents | fa54a8a1e3e4 |
children | 6406766a222d |
rev | line source |
---|---|
890 | 1 import std/times |
733
07c755e65a4a
add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents:
726
diff
changeset
|
2 import std/unicode |
07c755e65a4a
add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents:
726
diff
changeset
|
3 |
726 | 4 import semicongine |
5 | |
879
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
6 |
726 | 7 proc main() = |
832
388c4b35a6e3
fix: tests, test_materials and test_mesh still needs to be done
Sam <sam@basx.dev>
parents:
805
diff
changeset
|
8 # setup engine |
388c4b35a6e3
fix: tests, test_materials and test_mesh still needs to be done
Sam <sam@basx.dev>
parents:
805
diff
changeset
|
9 var engine = initEngine("Test fonts") |
388c4b35a6e3
fix: tests, test_materials and test_mesh still needs to be done
Sam <sam@basx.dev>
parents:
805
diff
changeset
|
10 engine.initRenderer([]) |
388c4b35a6e3
fix: tests, test_materials and test_mesh still needs to be done
Sam <sam@basx.dev>
parents:
805
diff
changeset
|
11 |
388c4b35a6e3
fix: tests, test_materials and test_mesh still needs to be done
Sam <sam@basx.dev>
parents:
805
diff
changeset
|
12 # build scene |
388c4b35a6e3
fix: tests, test_materials and test_mesh still needs to be done
Sam <sam@basx.dev>
parents:
805
diff
changeset
|
13 var scene = Scene(name: "main") |
879
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
14 var font = loadFont("DejaVuSans.ttf", lineHeightPixels = 210'f32) |
897
fa54a8a1e3e4
fix: text-alignment, a few smaller fixes
Sam <sam@basx.dev>
parents:
893
diff
changeset
|
15 var origin = initPanel(size = newVec2f(0.01, 0.01)) |
904
a253a691e0d6
fix: font-api, allow consistent use of mesh-transform
Sam <sam@basx.dev>
parents:
897
diff
changeset
|
16 var main_text = font.initText("".toRunes, maxLen = 255, color = newVec4f(1, 0.15, 0.15, 1), maxWidth = 1.0, transform = scale(0.0005, 0.0005)) |
879
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
17 var help_text = font.initText("""Controls |
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
18 |
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
19 Horizontal alignment: |
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
20 F1: Left |
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
21 F2: Center |
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
22 F3: Right |
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
23 Vertical alignment: |
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
24 F4: Top |
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
25 F5: Center |
904
a253a691e0d6
fix: font-api, allow consistent use of mesh-transform
Sam <sam@basx.dev>
parents:
897
diff
changeset
|
26 F6: Bottom""".toRunes, horizontalAlignment = Left, verticalAlignment = Top, transform = translate(-0.9, -0.9) * scale(0.0002, 0.0002)) |
897
fa54a8a1e3e4
fix: text-alignment, a few smaller fixes
Sam <sam@basx.dev>
parents:
893
diff
changeset
|
27 scene.add origin |
879
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
28 scene.add main_text |
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
29 scene.add help_text |
832
388c4b35a6e3
fix: tests, test_materials and test_mesh still needs to be done
Sam <sam@basx.dev>
parents:
805
diff
changeset
|
30 engine.loadScene(scene) |
888
cbbb9abe256e
add: nicer key-stroke test, cleanup test-resource files
Sam <sam@basx.dev>
parents:
887
diff
changeset
|
31 mixer[].loadSound("key", "key.ogg") |
890 | 32 mixer[].setLevel(0.5) |
733
07c755e65a4a
add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents:
726
diff
changeset
|
33 |
726 | 34 while engine.updateInputs() == Running and not engine.keyIsDown(Escape): |
890 | 35 var t = cpuTime() |
36 main_text.color = newVec4f(sin(t) * 0.5 + 0.5, 0.15, 0.15, 1) | |
733
07c755e65a4a
add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents:
726
diff
changeset
|
37 if engine.windowWasResized(): |
07c755e65a4a
add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents:
726
diff
changeset
|
38 var winSize = engine.getWindow().size |
879
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
39 |
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
40 # add character |
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
41 if main_text.text.len < main_text.maxLen - 1: |
878 | 42 for c in [Key.A, Key.B, Key.C, Key.D, Key.E, Key.F, Key.G, Key.H, Key.I, |
43 Key.J, Key.K, Key.L, Key.M, Key.N, Key.O, Key.P, Key.Q, Key.R, Key.S, | |
44 Key.T, Key.U, Key.V, Key.W, Key.X, Key.Y, Key.Z]: | |
45 if engine.keyWasPressed(c): | |
888
cbbb9abe256e
add: nicer key-stroke test, cleanup test-resource files
Sam <sam@basx.dev>
parents:
887
diff
changeset
|
46 discard mixer[].play("key") |
878 | 47 if engine.keyIsDown(ShiftL) or engine.keyIsDown(ShiftR): |
879
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
48 main_text.text = main_text.text & ($c).toRunes |
878 | 49 else: |
879
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
50 main_text.text = main_text.text & ($c).toRunes[0].toLower() |
878 | 51 if engine.keyWasPressed(Enter): |
888
cbbb9abe256e
add: nicer key-stroke test, cleanup test-resource files
Sam <sam@basx.dev>
parents:
887
diff
changeset
|
52 discard mixer[].play("key") |
879
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
53 main_text.text = main_text.text & Rune('\n') |
878 | 54 if engine.keyWasPressed(Space): |
888
cbbb9abe256e
add: nicer key-stroke test, cleanup test-resource files
Sam <sam@basx.dev>
parents:
887
diff
changeset
|
55 discard mixer[].play("key") |
879
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
56 main_text.text = main_text.text & Rune(' ') |
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
57 |
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
58 # remove character |
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
59 if engine.keyWasPressed(Backspace) and main_text.text.len > 0: |
888
cbbb9abe256e
add: nicer key-stroke test, cleanup test-resource files
Sam <sam@basx.dev>
parents:
887
diff
changeset
|
60 discard mixer[].play("key") |
879
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
61 main_text.text = main_text.text[0 ..< ^1] |
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
62 |
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
63 # alignemtn with F-keys |
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
64 if engine.keyWasPressed(F1): main_text.horizontalAlignment = Left |
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
65 elif engine.keyWasPressed(F2): main_text.horizontalAlignment = Center |
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
66 elif engine.keyWasPressed(F3): main_text.horizontalAlignment = Right |
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
67 elif engine.keyWasPressed(F4): main_text.verticalAlignment = Top |
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
68 elif engine.keyWasPressed(F5): main_text.verticalAlignment = Center |
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
69 elif engine.keyWasPressed(F6): main_text.verticalAlignment = Bottom |
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
70 |
897
fa54a8a1e3e4
fix: text-alignment, a few smaller fixes
Sam <sam@basx.dev>
parents:
893
diff
changeset
|
71 origin.refresh() |
879
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
72 main_text.text = main_text.text & Rune('_') |
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
73 main_text.refresh() |
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
74 main_text.text = main_text.text[0 ..< ^1] |
7ecae083b670
add: correct version of text-alignment, and a few improvments
Sam <sam@basx.dev>
parents:
878
diff
changeset
|
75 help_text.refresh() |
726 | 76 engine.renderScene(scene) |
77 engine.destroy() | |
78 | |
79 | |
80 when isMainModule: | |
81 main() |