Mercurial > games > semicongine
comparison tests/test_text.nim @ 1239:69489a678141
add: better syncing, better swapchain access, correct font offset, two font-rendering tests
author | sam <sam@basx.dev> |
---|---|
date | Mon, 22 Jul 2024 00:46:10 +0700 |
parents | 03634915bbdb |
children | 42eeb59f3a43 |
comparison
equal
deleted
inserted
replaced
1238:03634915bbdb | 1239:69489a678141 |
---|---|
6 import std/options | 6 import std/options |
7 import std/random | 7 import std/random |
8 | 8 |
9 import ../semiconginev2 | 9 import ../semiconginev2 |
10 | 10 |
11 proc test_01_static_label(time: float32, swapchain: var Swapchain) = | 11 proc test_01_static_label(time: float32) = |
12 var renderdata = InitRenderData() | 12 var renderdata = InitRenderData() |
13 | 13 |
14 var pipeline = CreatePipeline[DefaultFontShader](renderPass = swapchain.renderPass) | 14 var pipeline = CreatePipeline[DefaultFontShader](renderPass = vulkan.swapchain.renderPass) |
15 | 15 |
16 var font = LoadFont("Overhaul.ttf", lineHeightPixels = 160) | 16 var font = LoadFont("Overhaul.ttf", lineHeightPixels = 160) |
17 var label1 = InitTextbox( | 17 var label1 = InitTextbox( |
18 renderdata, | 18 renderdata, |
19 pipeline.descriptorSetLayouts[0], | 19 pipeline.descriptorSetLayouts[0], |
23 scale = 0.0005, | 23 scale = 0.0005, |
24 ) | 24 ) |
25 | 25 |
26 var start = getMonoTime() | 26 var start = getMonoTime() |
27 while ((getMonoTime() - start).inMilliseconds().int / 1000) < time: | 27 while ((getMonoTime() - start).inMilliseconds().int / 1000) < time: |
28 label1.Refresh(swapchain.GetAspectRatio()) | 28 label1.Refresh() |
29 WithNextFrame(swapchain, framebuffer, commandbuffer): | 29 WithNextFrame(framebuffer, commandbuffer): |
30 WithRenderPass(swapchain.renderPass, framebuffer, commandbuffer, swapchain.width, swapchain.height, NewVec4f(0, 0, 0, 0)): | 30 WithRenderPass(vulkan.swapchain.renderPass, framebuffer, commandbuffer, vulkan.swapchain.width, vulkan.swapchain.height, NewVec4f(0, 0, 0, 0)): |
31 WithPipeline(commandbuffer, pipeline): | 31 WithPipeline(commandbuffer, pipeline): |
32 Render(label1, commandbuffer, pipeline, swapchain.currentFiF) | 32 Render(label1, commandbuffer, pipeline) |
33 | 33 |
34 # cleanup | 34 # cleanup |
35 checkVkResult vkDeviceWaitIdle(vulkan.device) | 35 checkVkResult vkDeviceWaitIdle(vulkan.device) |
36 DestroyPipeline(pipeline) | 36 DestroyPipeline(pipeline) |
37 DestroyRenderData(renderdata) | 37 DestroyRenderData(renderdata) |
38 | 38 |
39 proc test_02_multiple_animated(time: float32, swapchain: var Swapchain) = | 39 proc test_02_multiple_animated(time: float32) = |
40 var renderdata = InitRenderData() | 40 var renderdata = InitRenderData() |
41 | 41 |
42 var pipeline = CreatePipeline[DefaultFontShader](renderPass = swapchain.renderPass) | 42 var pipeline = CreatePipeline[DefaultFontShader](renderPass = vulkan.swapchain.renderPass) |
43 | 43 |
44 var font1 = LoadFont("Overhaul.ttf", lineHeightPixels = 40) | 44 var font1 = LoadFont("Overhaul.ttf", lineHeightPixels = 40) |
45 var font2 = LoadFont("Overhaul.ttf", lineHeightPixels = 160) | 45 var font2 = LoadFont("Overhaul.ttf", lineHeightPixels = 160) |
46 var font3 = LoadFont("DejaVuSans.ttf", lineHeightPixels = 160) | 46 var font3 = LoadFont("DejaVuSans.ttf", lineHeightPixels = 160) |
47 var labels = [ | 47 var labels = [ |
83 c[i] = progress | 83 c[i] = progress |
84 labels[i].Color = c | 84 labels[i].Color = c |
85 labels[i].Scale = labels[i].Scale * (1.0 + (i + 1).float * 0.001) | 85 labels[i].Scale = labels[i].Scale * (1.0 + (i + 1).float * 0.001) |
86 labels[i].Position = labels[i].Position + NewVec3f(0.001 * (i.float - 1'f)) | 86 labels[i].Position = labels[i].Position + NewVec3f(0.001 * (i.float - 1'f)) |
87 labels[i].text = $(p + i) | 87 labels[i].text = $(p + i) |
88 labels[i].Refresh(swapchain.GetAspectRatio()) | 88 labels[i].Refresh() |
89 inc p | 89 inc p |
90 WithNextFrame(swapchain, framebuffer, commandbuffer): | 90 WithNextFrame(framebuffer, commandbuffer): |
91 WithRenderPass(swapchain.renderPass, framebuffer, commandbuffer, swapchain.width, swapchain.height, NewVec4f(0, 0, 0, 0)): | 91 WithRenderPass(vulkan.swapchain.renderPass, framebuffer, commandbuffer, vulkan.swapchain.width, vulkan.swapchain.height, NewVec4f(0, 0, 0, 0)): |
92 WithPipeline(commandbuffer, pipeline): | 92 WithPipeline(commandbuffer, pipeline): |
93 for label in labels: | 93 for label in labels: |
94 Render(label, commandbuffer, pipeline, swapchain.currentFiF) | 94 Render(label, commandbuffer, pipeline) |
95 | 95 |
96 # cleanup | 96 # cleanup |
97 checkVkResult vkDeviceWaitIdle(vulkan.device) | 97 checkVkResult vkDeviceWaitIdle(vulkan.device) |
98 DestroyPipeline(pipeline) | 98 DestroyPipeline(pipeline) |
99 DestroyRenderData(renderdata) | 99 DestroyRenderData(renderdata) |
100 | 100 |
101 proc test_03_layouting(time: float32, swapchain: var Swapchain) = | 101 proc test_03_layouting(time: float32) = |
102 discard # TODO | 102 var renderdata = InitRenderData() |
103 proc test_04_lots_of_texts(time: float32, swapchain: var Swapchain) = | 103 |
104 var pipeline = CreatePipeline[DefaultFontShader](renderPass = vulkan.swapchain.renderPass) | |
105 | |
106 var font = LoadFont("DejaVuSans.ttf", lineHeightPixels = 40) | |
107 var labels: seq[Textbox] | |
108 | |
109 for horizontal in HorizontalAlignment: | |
110 labels.add InitTextbox( | |
111 renderdata, | |
112 pipeline.descriptorSetLayouts[0], | |
113 font, | |
114 $horizontal & " aligned", | |
115 color = NewVec4f(1, 1, 1, 1), | |
116 scale = 0.001, | |
117 position = NewVec3f(0, 0.9 - (horizontal.float * 0.15)), | |
118 horizontalAlignment = horizontal, | |
119 ) | |
120 for vertical in VerticalAlignment: | |
121 labels.add InitTextbox( | |
122 renderdata, | |
123 pipeline.descriptorSetLayouts[0], | |
124 font, | |
125 $vertical & " aligned", | |
126 color = NewVec4f(1, 1, 1, 1), | |
127 scale = 0.001, | |
128 position = NewVec3f(-0.35 + (vertical.float * 0.35), 0.3), | |
129 verticalAlignment = vertical, | |
130 ) | |
131 labels.add InitTextbox( | |
132 renderdata, | |
133 pipeline.descriptorSetLayouts[0], | |
134 font, | |
135 """Paragraph | |
136 This is a somewhat longer paragraph with a few newlines and a maximum width of 0.2. | |
137 | |
138 It should display with some space above and have a pleasing appearance overall! :)""", | |
139 maxWidth = 0.6, | |
140 color = NewVec4f(1, 1, 1, 1), | |
141 scale = 0.001, | |
142 position = NewVec3f(-0.9, 0.1), | |
143 verticalAlignment = Top, | |
144 horizontalAlignment = Left, | |
145 ) | |
146 | |
147 | |
148 var start = getMonoTime() | |
149 while ((getMonoTime() - start).inMilliseconds().int / 1000) < time: | |
150 let progress = ((getMonoTime() - start).inMilliseconds().int / 1000) / time | |
151 WithNextFrame(framebuffer, commandbuffer): | |
152 WithRenderPass(vulkan.swapchain.renderPass, framebuffer, commandbuffer, vulkan.swapchain.width, vulkan.swapchain.height, NewVec4f(0, 0, 0, 0)): | |
153 WithPipeline(commandbuffer, pipeline): | |
154 for label in labels: | |
155 Render(label, commandbuffer, pipeline) | |
156 | |
157 # cleanup | |
158 checkVkResult vkDeviceWaitIdle(vulkan.device) | |
159 DestroyPipeline(pipeline) | |
160 DestroyRenderData(renderdata) | |
161 | |
162 proc test_04_lots_of_texts(time: float32) = | |
104 discard # TODO | 163 discard # TODO |
105 | 164 |
106 when isMainModule: | 165 when isMainModule: |
107 var time = 10'f32 | 166 var time = 10'f32 |
108 InitVulkan() | 167 InitVulkan() |
109 | 168 |
110 var renderpass = CreateDirectPresentationRenderPass(depthBuffer = true) | 169 var renderpass = CreateDirectPresentationRenderPass(depthBuffer = true) |
111 var swapchain = InitSwapchain(renderpass = renderpass).get() | 170 SetupSwapchain(renderpass = renderpass) |
112 | 171 |
113 # tests a simple triangle with minimalistic shader and vertex format | 172 # tests a simple triangle with minimalistic shader and vertex format |
114 # test_01_static_label(time, swapchain) | 173 # test_01_static_label(time, swapchain) |
115 test_02_multiple_animated(time, swapchain) | 174 # test_02_multiple_animated(time) |
175 test_03_layouting(time) | |
176 | |
116 | 177 |
117 checkVkResult vkDeviceWaitIdle(vulkan.device) | 178 checkVkResult vkDeviceWaitIdle(vulkan.device) |
118 vkDestroyRenderPass(vulkan.device, renderpass.vk, nil) | 179 vkDestroyRenderPass(vulkan.device, renderpass.vk, nil) |
119 DestroySwapchain(swapchain) | |
120 | 180 |
121 DestroyVulkan() | 181 DestroyVulkan() |