Mercurial > games > semicongine
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 |
rev | line source |
---|---|
1236 | 1 import std/os |
2 import std/sequtils | |
3 import std/monotimes | |
4 import std/times | |
5 import std/options | |
6 import std/random | |
7 | |
8 import ../semiconginev2 | |
9 | |
10 proc test_01_static_label(time: float32, swapchain: var Swapchain) = | |
11 var renderdata = InitRenderData() | |
12 | |
13 var pipeline = CreatePipeline[DefaultFontShader](renderPass = swapchain.renderPass) | |
14 | |
15 var font = LoadFont("Overhaul.ttf", lineHeightPixels = 160) | |
16 var label1 = InitTextbox( | |
17 renderdata, | |
18 pipeline.descriptorSetLayouts[0], | |
19 font, | |
20 "Hello semicongine!", | |
21 color = NewVec4f(1, 1, 1, 1), | |
22 scale = 0.0005, | |
23 ) | |
24 | |
25 var start = getMonoTime() | |
26 while ((getMonoTime() - start).inMilliseconds().int / 1000) < time: | |
27 label1.Refresh(swapchain.GetAspectRatio()) | |
28 WithNextFrame(swapchain, framebuffer, commandbuffer): | |
29 WithRenderPass(swapchain.renderPass, framebuffer, commandbuffer, swapchain.width, swapchain.height, NewVec4f(0, 0, 0, 0)): | |
30 WithPipeline(commandbuffer, pipeline): | |
31 Render(label1, commandbuffer, pipeline, swapchain.currentFiF) | |
32 | |
33 # cleanup | |
34 checkVkResult vkDeviceWaitIdle(vulkan.device) | |
35 DestroyPipeline(pipeline) | |
36 DestroyRenderData(renderdata) | |
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 | 102 when isMainModule: |
103 var time = 10'f32 | |
104 InitVulkan() | |
105 | |
106 var renderpass = CreateDirectPresentationRenderPass(depthBuffer = true) | |
107 var swapchain = InitSwapchain(renderpass = renderpass).get() | |
108 | |
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 | 112 |
113 checkVkResult vkDeviceWaitIdle(vulkan.device) | |
114 vkDestroyRenderPass(vulkan.device, renderpass.vk, nil) | |
115 DestroySwapchain(swapchain) | |
116 | |
117 DestroyVulkan() |