annotate tests/test_text.nim @ 1237:97813ac43cfb

add: multi-text with all properties animated
author sam <sam@basx.dev>
date Sun, 21 Jul 2024 00:03:48 +0700
parents 176383220123
children 03634915bbdb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1236
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
1 import std/os
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
2 import std/sequtils
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
3 import std/monotimes
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
4 import std/times
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
5 import std/options
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
6 import std/random
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
7
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
8 import ../semiconginev2
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
9
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
10 proc test_01_static_label(time: float32, swapchain: var Swapchain) =
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
11 var renderdata = InitRenderData()
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
12
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
13 var pipeline = CreatePipeline[DefaultFontShader](renderPass = swapchain.renderPass)
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
14
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
15 var font = LoadFont("Overhaul.ttf", lineHeightPixels = 160)
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
16 var label1 = InitTextbox(
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
17 renderdata,
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
18 pipeline.descriptorSetLayouts[0],
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
19 font,
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
20 "Hello semicongine!",
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
21 color = NewVec4f(1, 1, 1, 1),
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
22 scale = 0.0005,
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
23 )
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
24
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
25 var start = getMonoTime()
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
26 while ((getMonoTime() - start).inMilliseconds().int / 1000) < time:
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
27 label1.Refresh(swapchain.GetAspectRatio())
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
28 WithNextFrame(swapchain, framebuffer, commandbuffer):
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
29 WithRenderPass(swapchain.renderPass, framebuffer, commandbuffer, swapchain.width, swapchain.height, NewVec4f(0, 0, 0, 0)):
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
30 WithPipeline(commandbuffer, pipeline):
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
31 Render(label1, commandbuffer, pipeline, swapchain.currentFiF)
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
32
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
33 # cleanup
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
34 checkVkResult vkDeviceWaitIdle(vulkan.device)
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
35 DestroyPipeline(pipeline)
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
36 DestroyRenderData(renderdata)
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
37
1237
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
38 proc test_02_multiple_animated(time: float32, swapchain: var Swapchain) =
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
39 var renderdata = InitRenderData()
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
40
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
41 var pipeline = CreatePipeline[DefaultFontShader](renderPass = swapchain.renderPass)
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
42
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
43 var font1 = LoadFont("Overhaul.ttf", lineHeightPixels = 40)
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
44 var font2 = LoadFont("Overhaul.ttf", lineHeightPixels = 160)
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
45 var font3 = LoadFont("DejaVuSans.ttf", lineHeightPixels = 160)
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
46 var labels = [
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
47 InitTextbox(
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
48 renderdata,
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
49 pipeline.descriptorSetLayouts[0],
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
50 font1,
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
51 "0",
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
52 color = NewVec4f(0, 1, 1, 1),
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
53 scale = 0.004,
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
54 position = NewVec3f(-0.3, 0.5)
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
55 ),
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
56 InitTextbox(
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
57 renderdata,
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
58 pipeline.descriptorSetLayouts[0],
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
59 font2,
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
60 "1",
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
61 color = NewVec4f(1, 0, 1, 1),
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
62 scale = 0.001,
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
63 position = NewVec3f(0, 0)
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
64 ),
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
65 InitTextbox(
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
66 renderdata,
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
67 pipeline.descriptorSetLayouts[0],
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
68 font3,
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
69 "2",
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
70 color = NewVec4f(1, 1, 0, 1),
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
71 scale = 0.001,
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
72 position = NewVec3f(0.3, -0.5)
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
73 )
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
74 ]
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
75
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
76 var start = getMonoTime()
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
77 while ((getMonoTime() - start).inMilliseconds().int / 1000) < time:
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
78 let progress = ((getMonoTime() - start).inMilliseconds().int / 1000) / time
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
79 for i in 0 ..< labels.len:
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
80 var c = labels[i].Color
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
81 c[i] = progress
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
82 labels[i].Color = c
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
83 labels[i].Scale = labels[i].Scale * 1.002
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
84 labels[i].Position = labels[i].Position + NewVec3f(0.001 * (i.float - 1'f))
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
85 labels[i].Refresh(swapchain.GetAspectRatio())
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
86 WithNextFrame(swapchain, framebuffer, commandbuffer):
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
87 WithRenderPass(swapchain.renderPass, framebuffer, commandbuffer, swapchain.width, swapchain.height, NewVec4f(0, 0, 0, 0)):
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
88 WithPipeline(commandbuffer, pipeline):
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
89 for label in labels:
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
90 Render(label, commandbuffer, pipeline, swapchain.currentFiF)
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
91
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
92 # cleanup
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
93 checkVkResult vkDeviceWaitIdle(vulkan.device)
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
94 DestroyPipeline(pipeline)
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
95 DestroyRenderData(renderdata)
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
96
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
97 proc test_03_layouting(time: float32, swapchain: var Swapchain) =
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
98 discard # TODO
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
99 proc test_04_lots_of_texts(time: float32, swapchain: var Swapchain) =
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
100 discard # TODO
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
101
1236
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
102 when isMainModule:
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
103 var time = 10'f32
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
104 InitVulkan()
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
105
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
106 var renderpass = CreateDirectPresentationRenderPass(depthBuffer = true)
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
107 var swapchain = InitSwapchain(renderpass = renderpass).get()
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
108
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
109 # tests a simple triangle with minimalistic shader and vertex format
1237
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
110 # test_01_static_label(time, swapchain)
97813ac43cfb add: multi-text with all properties animated
sam <sam@basx.dev>
parents: 1236
diff changeset
111 test_02_multiple_animated(time, swapchain)
1236
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
112
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
113 checkVkResult vkDeviceWaitIdle(vulkan.device)
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
114 vkDestroyRenderPass(vulkan.device, renderpass.vk, nil)
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
115 DestroySwapchain(swapchain)
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
116
176383220123 add: first font-rendering test
sam <sam@basx.dev>
parents:
diff changeset
117 DestroyVulkan()