Mercurial > games > semicongine
comparison tests/test_text.nim @ 1405:46bac138ad6c
did: minimal work on next test
author | sam <sam@basx.dev> |
---|---|
date | Sat, 21 Dec 2024 00:19:11 +0700 |
parents | 80cfa19d1e2c |
children | aeb15aa9768c |
comparison
equal
deleted
inserted
replaced
1404:80cfa19d1e2c | 1405:46bac138ad6c |
---|---|
9 import std/random | 9 import std/random |
10 import std/unicode | 10 import std/unicode |
11 | 11 |
12 import ../semicongine | 12 import ../semicongine |
13 | 13 |
14 type FontDS = object | |
15 fontAtlas: Image[Gray] | |
16 | |
17 const MAX_CODEPOINTS = 200 | 14 const MAX_CODEPOINTS = 200 |
18 const FONTNAME = "Overhaul.ttf" | 15 const FONTNAME = "Overhaul.ttf" |
19 # const FONTNAME = "DejaVuSans.ttf" | 16 # const FONTNAME = "DejaVuSans.ttf" |
20 | 17 |
21 proc test_01_static_label(time: float32) = | 18 proc test_01_static_label(time: float32) = |
123 # cleanup | 120 # cleanup |
124 checkVkResult vkDeviceWaitIdle(vulkan.device) | 121 checkVkResult vkDeviceWaitIdle(vulkan.device) |
125 destroyPipeline(pipeline) | 122 destroyPipeline(pipeline) |
126 destroyRenderData(renderdata) | 123 destroyRenderData(renderdata) |
127 | 124 |
128 #[ | |
129 proc test_03_layouting(time: float32) = | 125 proc test_03_layouting(time: float32) = |
130 var font = loadFont[MAX_CODEPOINTS]("DejaVuSans.ttf", lineHeightPixels = 40) | 126 var font = loadFont[MAX_CODEPOINTS]("DejaVuSans.ttf", lineHeightPixels = 40) |
131 var renderdata = initRenderData() | 127 var renderdata = initRenderData() |
132 | 128 |
133 var pipeline = createPipeline[GlyphShader[MAX_CODEPOINTS]]( | 129 var pipeline = createPipeline[GlyphShader[MAX_CODEPOINTS]]( |
134 renderPass = vulkan.swapchain.renderPass | 130 renderPass = vulkan.swapchain.renderPass |
135 ) | 131 ) |
136 | 132 |
137 var ds = asDescriptorSetData(FontDS(fontAtlas: font.fontAtlas.copy())) | 133 var ds = asDescriptorSetData(FontDS(fontAtlas: font.fontAtlas.copy())) |
138 uploadImages(renderdata, ds) | 134 |
139 initDescriptorSet(renderdata, pipeline.layout(0), ds) | 135 assignBuffers(renderdata, font.descriptorSet) |
140 | 136 uploadImages(renderdata, font.descriptorSet) |
141 var labels: seq[Textbox] | 137 initDescriptorSet(renderdata, pipeline.layout(0), font.descriptorSet) |
138 | |
139 var glyphs = font.initGlyphs(1000, baseScale = 0.1) | |
140 assignBuffers(renderdata, glyphs) | |
142 | 141 |
143 for horizontal in HorizontalAlignment: | 142 for horizontal in HorizontalAlignment: |
144 labels.add initTextbox( | 143 glyphs.add $horizontal & " aligned" |
145 renderdata, | |
146 pipeline.layout(0), | |
147 font, | |
148 0.001, | |
149 $horizontal & " aligned", | |
150 horizontalAlignment = horizontal, | |
151 ) | |
152 for vertical in VerticalAlignment: | 144 for vertical in VerticalAlignment: |
153 labels.add initTextbox( | 145 glyphs.add $vertical & " aligned" |
154 renderdata, | 146 |
155 pipeline.layout(0), | 147 glyphs.add( |
156 font, | |
157 0.001, | |
158 $vertical & " aligned", | |
159 verticalAlignment = vertical, | |
160 ) | |
161 labels.add initTextbox( | |
162 renderdata, | |
163 pipeline.layout(0), | |
164 font, | |
165 0.001, | |
166 """Paragraph | 148 """Paragraph |
167 This is a somewhat longer paragraph with a few newlines and a maximum width of 0.2. | 149 This is a somewhat longer paragraph with a few newlines and a maximum width of 0.2. |
168 | 150 |
169 It should display with some space above and have a pleasing appearance overall! :)""", | 151 It should display with some space above and have a pleasing appearance overall! :)""", |
170 maxWidth = 0.6, | |
171 verticalAlignment = Top, | 152 verticalAlignment = Top, |
172 horizontalAlignment = Left, | 153 horizontalAlignment = Left, |
173 ) | 154 ) |
174 | 155 |
175 var start = getMonoTime() | 156 var start = getMonoTime() |
176 while ((getMonoTime() - start).inMilliseconds().int / 1000) < time: | 157 while ((getMonoTime() - start).inMilliseconds().int / 1000) < time: |
177 let progress = ((getMonoTime() - start).inMilliseconds().int / 1000) / time | 158 let progress = ((getMonoTime() - start).inMilliseconds().int / 1000) / time |
178 withNextFrame(framebuffer, commandbuffer): | 159 withNextFrame(framebuffer, commandbuffer): |
179 bindDescriptorSet(commandbuffer, ds, 0, pipeline) | 160 bindDescriptorSet(commandbuffer, font.descriptorSet, 0, pipeline) |
180 withRenderPass( | 161 withRenderPass( |
181 vulkan.swapchain.renderPass, | 162 vulkan.swapchain.renderPass, |
182 framebuffer, | 163 framebuffer, |
183 commandbuffer, | 164 commandbuffer, |
184 vulkan.swapchain.width, | 165 vulkan.swapchain.width, |
185 vulkan.swapchain.height, | 166 vulkan.swapchain.height, |
186 vec4(0, 0, 0, 0), | 167 vec4(0, 0, 0, 0), |
187 ): | 168 ): |
188 withPipeline(commandbuffer, pipeline): | 169 withPipeline(commandbuffer, pipeline): |
189 for i in 0 ..< labels.len: | 170 renderGlyphs(commandbuffer, pipeline, glyphs) |
190 render( | |
191 commandbuffer, | |
192 pipeline, | |
193 labels[i], | |
194 vec3(0.5 - i.float32 * 0.1, 0.5 - i.float32 * 0.1), | |
195 vec4(1, 1, 1, 1), | |
196 ) | |
197 | 171 |
198 # cleanup | 172 # cleanup |
199 checkVkResult vkDeviceWaitIdle(vulkan.device) | 173 checkVkResult vkDeviceWaitIdle(vulkan.device) |
200 destroyPipeline(pipeline) | 174 destroyPipeline(pipeline) |
201 destroyRenderData(renderdata) | 175 destroyRenderData(renderdata) |
202 | 176 |
177 #[ | |
203 proc test_04_lots_of_texts(time: float32) = | 178 proc test_04_lots_of_texts(time: float32) = |
204 var font = loadFont[MAX_CODEPOINTS]("DejaVuSans.ttf", lineHeightPixels = 160) | 179 var font = loadFont[MAX_CODEPOINTS]("DejaVuSans.ttf", lineHeightPixels = 160) |
205 var renderdata = initRenderData() | 180 var renderdata = initRenderData() |
206 | 181 |
207 var pipeline = createPipeline[GlyphShader[MAX_CODEPOINTS]]( | 182 var pipeline = createPipeline[GlyphShader[MAX_CODEPOINTS]]( |
258 setupSwapchain(renderpass = renderpass) | 233 setupSwapchain(renderpass = renderpass) |
259 | 234 |
260 # tests a simple triangle with minimalistic shader and vertex format | 235 # tests a simple triangle with minimalistic shader and vertex format |
261 test_01_static_label(time) | 236 test_01_static_label(time) |
262 test_02_multi_counter(time) | 237 test_02_multi_counter(time) |
263 # test_03_layouting(time) | 238 test_03_layouting(time) |
264 # test_04_lots_of_texts(time) | 239 # test_04_lots_of_texts(time) |
265 | 240 |
266 checkVkResult vkDeviceWaitIdle(vulkan.device) | 241 checkVkResult vkDeviceWaitIdle(vulkan.device) |
267 vkDestroyRenderPass(vulkan.device, renderpass.vk, nil) | 242 vkDestroyRenderPass(vulkan.device, renderpass.vk, nil) |
268 clearSwapchain() | 243 clearSwapchain() |