annotate tests/test_font.nim @ 336:887ddc8d45fd

did: update examples to work with improved scenegraph/material api, notice removed complexity!
author Sam <sam@basx.dev>
date Tue, 05 Sep 2023 00:28:35 +0700
parents 05fb85ba97dd
children 6cc16b62e497
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() =
272
bfcb41211c5b add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents: 265
diff changeset
7 var font = loadFont("DejaVuSans.ttf", color=newVec4f(1, 0.5, 0.5, 1), resolution=20)
330
04531bec3583 did: remove some stuff from the heap, maybe nicer?
Sam <sam@basx.dev>
parents: 316
diff changeset
8
04531bec3583 did: remove some stuff from the heap, maybe nicer?
Sam <sam@basx.dev>
parents: 316
diff changeset
9 var textbox = initTextbox(32, font, "".toRunes)
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
10 var scene = Scene(name: "main")
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
11 scene.add textbox
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
12 textbox.mesh.transform = scale(0.01, 0.01)
272
bfcb41211c5b add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents: 265
diff changeset
13 var engine = initEngine("Test fonts")
330
04531bec3583 did: remove some stuff from the heap, maybe nicer?
Sam <sam@basx.dev>
parents: 316
diff changeset
14 engine.initRenderer()
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
15 scene.addShaderGlobal("perspective", Unit4F32)
330
04531bec3583 did: remove some stuff from the heap, maybe nicer?
Sam <sam@basx.dev>
parents: 316
diff changeset
16 engine.addScene(scene)
272
bfcb41211c5b add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents: 265
diff changeset
17
265
ad4f2b45410b add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
18 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
19 if engine.windowWasResized():
bfcb41211c5b add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents: 265
diff changeset
20 var winSize = engine.getWindow().size
bfcb41211c5b add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents: 265
diff changeset
21 scene.setShaderGlobal("perspective", orthoWindowAspect(winSize[1] / winSize[0]))
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
22 textbox.mesh.transform = scale(0.01 * (winSize[1] / winSize[0]), 0.01)
272
bfcb41211c5b add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents: 265
diff changeset
23 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
24 if engine.keyWasPressed(c):
bfcb41211c5b add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents: 265
diff changeset
25 if engine.keyIsDown(ShiftL) or engine.keyIsDown(ShiftR):
bfcb41211c5b add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents: 265
diff changeset
26 textbox.text = textbox.text & ($c).toRunes
bfcb41211c5b add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents: 265
diff changeset
27 else:
bfcb41211c5b add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents: 265
diff changeset
28 textbox.text = textbox.text & ($c).toRunes[0].toLower()
bfcb41211c5b add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents: 265
diff changeset
29 if engine.keyWasPressed(Space):
bfcb41211c5b add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents: 265
diff changeset
30 textbox.text = textbox.text & " ".toRunes[0]
bfcb41211c5b add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents: 265
diff changeset
31 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
32 textbox.text = textbox.text[0 ..< ^1]
265
ad4f2b45410b add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
33 engine.renderScene(scene)
ad4f2b45410b add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
34 engine.destroy()
ad4f2b45410b add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
35
ad4f2b45410b add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
36
ad4f2b45410b add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
37 when isMainModule:
ad4f2b45410b add: font-test
Sam <sam@basx.dev>
parents:
diff changeset
38 main()