Mercurial > games > semicongine
annotate tests/test_font.nim @ 877:773af36148bd
did: changes
| author | Sam <sam@basx.dev> | 
|---|---|
| date | Sat, 27 Jan 2024 21:08:31 +0700 | 
| parents | 164b41a2d5a6 | 
| children | b032768df631 | 
| rev | line source | 
|---|---|
| 733 
07c755e65a4a
add: final font-rendering, API changes fixed
 Sam <sam@basx.dev> parents: 
726diff
changeset | 1 import std/unicode | 
| 
07c755e65a4a
add: final font-rendering, API changes fixed
 Sam <sam@basx.dev> parents: 
726diff
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: 
805diff
changeset | 6 # setup engine | 
| 
388c4b35a6e3
fix: tests, test_materials and test_mesh still needs to be done
 Sam <sam@basx.dev> parents: 
805diff
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: 
805diff
changeset | 8 engine.initRenderer([]) | 
| 
388c4b35a6e3
fix: tests, test_materials and test_mesh still needs to be done
 Sam <sam@basx.dev> parents: 
805diff
changeset | 9 | 
| 
388c4b35a6e3
fix: tests, test_materials and test_mesh still needs to be done
 Sam <sam@basx.dev> parents: 
805diff
changeset | 10 # build scene | 
| 
388c4b35a6e3
fix: tests, test_materials and test_mesh still needs to be done
 Sam <sam@basx.dev> parents: 
805diff
changeset | 11 var scene = Scene(name: "main") | 
| 872 
1ee397815b0b
did: image & font refactoring, add texture-atlas-packing
 Sam <sam@basx.dev> parents: 
832diff
changeset | 12 # var font = loadFont("DejaVuSans.ttf", lineHeightPixels=90'f32, charset="abcdefghijklmnopqrstuvwxyz ".toRunes) | 
| 877 | 13 var font = loadFont("DejaVuSans.ttf", lineHeightPixels = 250'f32) | 
| 14 var textbox = initText(32, font, "_", color = newVec4f(1, 0, 0, 1)) | |
| 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: 
791diff
changeset | 16 scene.add textbox | 
| 872 
1ee397815b0b
did: image & font refactoring, add texture-atlas-packing
 Sam <sam@basx.dev> parents: 
832diff
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: 
805diff
changeset | 18 engine.loadScene(scene) | 
| 733 
07c755e65a4a
add: final font-rendering, API changes fixed
 Sam <sam@basx.dev> parents: 
726diff
changeset | 19 | 
| 877 | 20 let cursor = Rune('_') | 
| 726 | 21 while engine.updateInputs() == Running and not engine.keyIsDown(Escape): | 
| 733 
07c755e65a4a
add: final font-rendering, API changes fixed
 Sam <sam@basx.dev> parents: 
726diff
changeset | 22 if engine.windowWasResized(): | 
| 
07c755e65a4a
add: final font-rendering, API changes fixed
 Sam <sam@basx.dev> parents: 
726diff
changeset | 23 var winSize = engine.getWindow().size | 
| 872 
1ee397815b0b
did: image & font refactoring, add texture-atlas-packing
 Sam <sam@basx.dev> parents: 
832diff
changeset | 24 textbox.mesh.transform = scale(fontscale * (winSize[1] / winSize[0]), fontscale) | 
| 877 | 25 for c in [Key.A, Key.B, Key.C, Key.D, Key.E, Key.F, Key.G, Key.H, Key.I, | 
| 26 Key.J, Key.K, Key.L, Key.M, Key.N, Key.O, Key.P, Key.Q, Key.R, Key.S, | |
| 27 Key.T, Key.U, Key.V, Key.W, Key.X, Key.Y, Key.Z]: | |
| 733 
07c755e65a4a
add: final font-rendering, API changes fixed
 Sam <sam@basx.dev> parents: 
726diff
changeset | 28 if engine.keyWasPressed(c): | 
| 
07c755e65a4a
add: final font-rendering, API changes fixed
 Sam <sam@basx.dev> parents: 
726diff
changeset | 29 if engine.keyIsDown(ShiftL) or engine.keyIsDown(ShiftR): | 
| 877 | 30 textbox.text = textbox.text[0 ..< ^1] & ($c).toRunes & cursor | 
| 733 
07c755e65a4a
add: final font-rendering, API changes fixed
 Sam <sam@basx.dev> parents: 
726diff
changeset | 31 else: | 
| 877 | 32 textbox.text = textbox.text[0 ..< ^1] & ($c).toRunes[0].toLower() & cursor | 
| 876 
164b41a2d5a6
add: font/text improvments, support for newline rendering
 Sam <sam@basx.dev> parents: 
874diff
changeset | 33 if engine.keyWasPressed(Enter): | 
| 877 | 34 textbox.text = textbox.text[0 ..< ^1] & Rune('\n') & cursor | 
| 733 
07c755e65a4a
add: final font-rendering, API changes fixed
 Sam <sam@basx.dev> parents: 
726diff
changeset | 35 if engine.keyWasPressed(Space): | 
| 877 | 36 textbox.text = textbox.text[0 ..< ^1] & Rune(' ') & cursor | 
| 37 if engine.keyWasPressed(Backspace) and textbox.text.len > 1: | |
| 38 textbox.text = textbox.text[0 ..< ^2] & cursor | |
| 726 | 39 engine.renderScene(scene) | 
| 40 engine.destroy() | |
| 41 | |
| 42 | |
| 43 when isMainModule: | |
| 44 main() | 
