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