comparison old_tests/test_font.nim @ 1203:6360c8d17ce0 compiletime-tests

did: preprations to add rendering tests
author sam <sam@basx.dev>
date Mon, 15 Jul 2024 20:06:42 +0700
parents tests/test_font.nim@114f395b9144
children
comparison
equal deleted inserted replaced
1202:a8864fe6fe6e 1203:6360c8d17ce0
1 import std/times
2 import std/unicode
3
4 import semicongine
5
6
7 proc main() =
8 # setup engine
9 var engine = InitEngine("Test fonts")
10 engine.InitRenderer([])
11
12 # build scene
13 var scene = Scene(name: "main")
14 var font = LoadFont("DejaVuSans.ttf", lineHeightPixels = 210'f32)
15 var origin = InitPanel(transform = Scale(0.01, 0.01))
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))
17 var help_text = font.InitText("""Controls
18
19 Horizontal alignment:
20 F1: Left
21 F2: Center
22 F3: Right
23 Vertical alignment:
24 F4: Top
25 F5: Center
26 F6: Bottom""".toRunes, horizontalAlignment = Left, verticalAlignment = Top, transform = Translate(-0.9, -0.9) * Scale(0.0002, 0.0002))
27 scene.Add origin
28 scene.Add main_text
29 scene.Add help_text
30 engine.LoadScene(scene)
31 mixer[].LoadSound("key", "key.ogg")
32 mixer[].SetLevel(0.5)
33
34 while engine.UpdateInputs() and not KeyIsDown(Escape):
35 var t = cpuTime()
36 main_text.Color = NewVec4f(sin(t) * 0.5 + 0.5, 0.15, 0.15, 1)
37
38 # add character
39 if main_text.text.len < main_text.maxLen - 1:
40 for c in [Key.A, Key.B, Key.C, Key.D, Key.E, Key.F, Key.G, Key.H, Key.I,
41 Key.J, Key.K, Key.L, Key.M, Key.N, Key.O, Key.P, Key.Q, Key.R, Key.S,
42 Key.T, Key.U, Key.V, Key.W, Key.X, Key.Y, Key.Z]:
43 if KeyWasPressed(c):
44 discard mixer[].Play("key")
45 if KeyIsDown(ShiftL) or KeyIsDown(ShiftR):
46 main_text.text = main_text.text & ($c).toRunes
47 else:
48 main_text.text = main_text.text & ($c).toRunes[0].toLower()
49 if KeyWasPressed(Enter):
50 discard mixer[].Play("key")
51 main_text.text = main_text.text & Rune('\n')
52 if KeyWasPressed(Space):
53 discard mixer[].Play("key")
54 main_text.text = main_text.text & Rune(' ')
55
56 # remove character
57 if KeyWasPressed(Backspace) and main_text.text.len > 0:
58 discard mixer[].Play("key")
59 main_text.text = main_text.text[0 ..< ^1]
60
61 # alignemtn with F-keys
62 if KeyWasPressed(F1): main_text.horizontalAlignment = Left
63 elif KeyWasPressed(F2): main_text.horizontalAlignment = Center
64 elif KeyWasPressed(F3): main_text.horizontalAlignment = Right
65 elif KeyWasPressed(F4): main_text.verticalAlignment = Top
66 elif KeyWasPressed(F5): main_text.verticalAlignment = Center
67 elif KeyWasPressed(F6): main_text.verticalAlignment = Bottom
68
69 origin.Refresh()
70 main_text.text = main_text.text & Rune('_')
71 main_text.Refresh()
72 main_text.text = main_text.text[0 ..< ^1]
73 help_text.Refresh()
74 engine.RenderScene(scene)
75 engine.Destroy()
76
77
78 when isMainModule:
79 main()