Mercurial > games > semicongine
annotate tests/test_font.nim @ 873:1ed09c1bc79c
add: packed textures for font-atlas
author | Sam <sam@basx.dev> |
---|---|
date | Sat, 20 Jan 2024 21:28:17 +0700 |
parents | 1ee397815b0b |
children | 8fb4387b2ea1 |
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 |
726 | 3 import semicongine |
4 | |
5 proc main() = | |
832
388c4b35a6e3
fix: tests, test_materials and test_mesh still needs to be done
Sam <sam@basx.dev>
parents:
805
diff
changeset
|
6 # setup engine |
388c4b35a6e3
fix: tests, test_materials and test_mesh still needs to be done
Sam <sam@basx.dev>
parents:
805
diff
changeset
|
7 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
|
8 engine.initRenderer([]) |
388c4b35a6e3
fix: tests, test_materials and test_mesh still needs to be done
Sam <sam@basx.dev>
parents:
805
diff
changeset
|
9 |
388c4b35a6e3
fix: tests, test_materials and test_mesh still needs to be done
Sam <sam@basx.dev>
parents:
805
diff
changeset
|
10 # build scene |
388c4b35a6e3
fix: tests, test_materials and test_mesh still needs to be done
Sam <sam@basx.dev>
parents:
805
diff
changeset
|
11 var scene = Scene(name: "main") |
872
1ee397815b0b
did: image & font refactoring, add texture-atlas-packing
Sam <sam@basx.dev>
parents:
832
diff
changeset
|
12 # var font = loadFont("DejaVuSans.ttf", lineHeightPixels=90'f32, charset="abcdefghijklmnopqrstuvwxyz ".toRunes) |
873 | 13 var font = loadFont("DejaVuSans.ttf", lineHeightPixels=180'f32) |
872
1ee397815b0b
did: image & font refactoring, add texture-atlas-packing
Sam <sam@basx.dev>
parents:
832
diff
changeset
|
14 var textbox = initText(32, font, "", color=newVec4f(1, 0, 0, 1)) |
873 | 15 let fontscale = 0.001 |
792
d65b62812d34
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:
791
diff
changeset
|
16 scene.add textbox |
872
1ee397815b0b
did: image & font refactoring, add texture-atlas-packing
Sam <sam@basx.dev>
parents:
832
diff
changeset
|
17 textbox.mesh.transform = scale(fontscale, fontscale) |
832
388c4b35a6e3
fix: tests, test_materials and test_mesh still needs to be done
Sam <sam@basx.dev>
parents:
805
diff
changeset
|
18 engine.loadScene(scene) |
733
07c755e65a4a
add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents:
726
diff
changeset
|
19 |
726 | 20 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
|
21 if engine.windowWasResized(): |
07c755e65a4a
add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents:
726
diff
changeset
|
22 var winSize = engine.getWindow().size |
872
1ee397815b0b
did: image & font refactoring, add texture-atlas-packing
Sam <sam@basx.dev>
parents:
832
diff
changeset
|
23 textbox.mesh.transform = scale(fontscale * (winSize[1] / winSize[0]), fontscale) |
733
07c755e65a4a
add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents:
726
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]: |
07c755e65a4a
add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents:
726
diff
changeset
|
25 if engine.keyWasPressed(c): |
07c755e65a4a
add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents:
726
diff
changeset
|
26 if engine.keyIsDown(ShiftL) or engine.keyIsDown(ShiftR): |
07c755e65a4a
add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents:
726
diff
changeset
|
27 textbox.text = textbox.text & ($c).toRunes |
07c755e65a4a
add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents:
726
diff
changeset
|
28 else: |
07c755e65a4a
add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents:
726
diff
changeset
|
29 textbox.text = textbox.text & ($c).toRunes[0].toLower() |
07c755e65a4a
add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents:
726
diff
changeset
|
30 if engine.keyWasPressed(Space): |
07c755e65a4a
add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents:
726
diff
changeset
|
31 textbox.text = textbox.text & " ".toRunes[0] |
07c755e65a4a
add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents:
726
diff
changeset
|
32 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
|
33 textbox.text = textbox.text[0 ..< ^1] |
726 | 34 engine.renderScene(scene) |
35 engine.destroy() | |
36 | |
37 | |
38 when isMainModule: | |
39 main() |