Mercurial > games > semicongine
comparison tests/test_text.nim @ 1283:0369fa1ffbd9
did: undo part of stupid API renaming a few weeks back ;(
| author | sam <sam@basx.dev> |
|---|---|
| date | Mon, 29 Jul 2024 16:50:50 +0700 |
| parents | 3308b88e53a6 |
| children | 5de466f5f087 |
comparison
equal
deleted
inserted
replaced
| 1282:3308b88e53a6 | 1283:0369fa1ffbd9 |
|---|---|
| 8 import std/random | 8 import std/random |
| 9 | 9 |
| 10 import ../semicongine | 10 import ../semicongine |
| 11 | 11 |
| 12 proc test_01_static_label(time: float32) = | 12 proc test_01_static_label(time: float32) = |
| 13 var renderdata = InitRenderData() | 13 var renderdata = initRenderData() |
| 14 | 14 |
| 15 var pipeline = CreatePipeline[DefaultFontShader](renderPass = vulkan.swapchain.renderPass) | 15 var pipeline = createPipeline[DefaultFontShader](renderPass = vulkan.swapchain.renderPass) |
| 16 | 16 |
| 17 var font = loadFont("Overhaul.ttf", lineHeightPixels = 160) | 17 var font = loadFont("Overhaul.ttf", lineHeightPixels = 160) |
| 18 var label1 = InitTextbox( | 18 var label1 = initTextbox( |
| 19 renderdata, | 19 renderdata, |
| 20 pipeline.descriptorSetLayouts[0], | 20 pipeline.descriptorSetLayouts[0], |
| 21 font, | 21 font, |
| 22 "Hello semicongine!", | 22 "Hello semicongine!", |
| 23 color = vec4(1, 1, 1, 1), | 23 color = vec4(1, 1, 1, 1), |
| 24 scale = 0.0005, | 24 scale = 0.0005, |
| 25 ) | 25 ) |
| 26 | 26 |
| 27 var start = getMonoTime() | 27 var start = getMonoTime() |
| 28 while ((getMonoTime() - start).inMilliseconds().int / 1000) < time: | 28 while ((getMonoTime() - start).inMilliseconds().int / 1000) < time: |
| 29 label1.Refresh() | 29 label1.refresh() |
| 30 WithNextFrame(framebuffer, commandbuffer): | 30 withNextFrame(framebuffer, commandbuffer): |
| 31 WithRenderPass(vulkan.swapchain.renderPass, framebuffer, commandbuffer, vulkan.swapchain.width, vulkan.swapchain.height, vec4(0, 0, 0, 0)): | 31 withRenderPass(vulkan.swapchain.renderPass, framebuffer, commandbuffer, vulkan.swapchain.width, vulkan.swapchain.height, vec4(0, 0, 0, 0)): |
| 32 WithPipeline(commandbuffer, pipeline): | 32 withPipeline(commandbuffer, pipeline): |
| 33 Render(label1, commandbuffer, pipeline) | 33 render(label1, commandbuffer, pipeline) |
| 34 | 34 |
| 35 # cleanup | 35 # cleanup |
| 36 checkVkResult vkDeviceWaitIdle(vulkan.device) | 36 checkVkResult vkDeviceWaitIdle(vulkan.device) |
| 37 DestroyPipeline(pipeline) | 37 destroyPipeline(pipeline) |
| 38 DestroyRenderData(renderdata) | 38 destroyRenderData(renderdata) |
| 39 | 39 |
| 40 proc test_02_multiple_animated(time: float32) = | 40 proc test_02_multiple_animated(time: float32) = |
| 41 var renderdata = InitRenderData() | 41 var renderdata = initRenderData() |
| 42 | 42 |
| 43 var pipeline = CreatePipeline[DefaultFontShader](renderPass = vulkan.swapchain.renderPass) | 43 var pipeline = createPipeline[DefaultFontShader](renderPass = vulkan.swapchain.renderPass) |
| 44 | 44 |
| 45 var font1 = loadFont("Overhaul.ttf", lineHeightPixels = 40) | 45 var font1 = loadFont("Overhaul.ttf", lineHeightPixels = 40) |
| 46 var font2 = loadFont("Overhaul.ttf", lineHeightPixels = 160) | 46 var font2 = loadFont("Overhaul.ttf", lineHeightPixels = 160) |
| 47 var font3 = loadFont("DejaVuSans.ttf", lineHeightPixels = 160) | 47 var font3 = loadFont("DejaVuSans.ttf", lineHeightPixels = 160) |
| 48 var labels = [ | 48 var labels = [ |
| 49 InitTextbox( | 49 initTextbox( |
| 50 renderdata, | 50 renderdata, |
| 51 pipeline.descriptorSetLayouts[0], | 51 pipeline.descriptorSetLayouts[0], |
| 52 font1, | 52 font1, |
| 53 " 0", | 53 " 0", |
| 54 color = vec4(0, 1, 1, 1), | 54 color = vec4(0, 1, 1, 1), |
| 55 scale = 0.004, | 55 scale = 0.004, |
| 56 position = vec3(-0.3, 0.5) | 56 position = vec3(-0.3, 0.5) |
| 57 ), | 57 ), |
| 58 InitTextbox( | 58 initTextbox( |
| 59 renderdata, | 59 renderdata, |
| 60 pipeline.descriptorSetLayouts[0], | 60 pipeline.descriptorSetLayouts[0], |
| 61 font2, | 61 font2, |
| 62 " 1", | 62 " 1", |
| 63 color = vec4(1, 0, 1, 1), | 63 color = vec4(1, 0, 1, 1), |
| 64 scale = 0.001, | 64 scale = 0.001, |
| 65 position = vec3(0, 0) | 65 position = vec3(0, 0) |
| 66 ), | 66 ), |
| 67 InitTextbox( | 67 initTextbox( |
| 68 renderdata, | 68 renderdata, |
| 69 pipeline.descriptorSetLayouts[0], | 69 pipeline.descriptorSetLayouts[0], |
| 70 font3, | 70 font3, |
| 71 " 2", | 71 " 2", |
| 72 color = vec4(1, 1, 0, 1), | 72 color = vec4(1, 1, 0, 1), |
| 78 var start = getMonoTime() | 78 var start = getMonoTime() |
| 79 var p = 0 | 79 var p = 0 |
| 80 while ((getMonoTime() - start).inMilliseconds().int / 1000) < time: | 80 while ((getMonoTime() - start).inMilliseconds().int / 1000) < time: |
| 81 let progress = ((getMonoTime() - start).inMilliseconds().int / 1000) / time | 81 let progress = ((getMonoTime() - start).inMilliseconds().int / 1000) / time |
| 82 for i in 0 ..< labels.len: | 82 for i in 0 ..< labels.len: |
| 83 var c = labels[i].Color | 83 var c = labels[i].color |
| 84 c[i] = progress | 84 c[i] = progress |
| 85 labels[i].Color = c | 85 labels[i].color = c |
| 86 labels[i].Scale = labels[i].Scale * (1.0 + (i + 1).float * 0.001) | 86 labels[i].scale = labels[i].scale * (1.0 + (i + 1).float * 0.001) |
| 87 labels[i].Position = labels[i].Position + vec3(0.001 * (i.float - 1'f)) | 87 labels[i].position = labels[i].position + vec3(0.001 * (i.float - 1'f)) |
| 88 labels[i].text = $(p + i) | 88 labels[i].text = $(p + i) |
| 89 labels[i].Refresh() | 89 labels[i].refresh() |
| 90 inc p | 90 inc p |
| 91 WithNextFrame(framebuffer, commandbuffer): | 91 withNextFrame(framebuffer, commandbuffer): |
| 92 WithRenderPass(vulkan.swapchain.renderPass, framebuffer, commandbuffer, vulkan.swapchain.width, vulkan.swapchain.height, vec4(0, 0, 0, 0)): | 92 withRenderPass(vulkan.swapchain.renderPass, framebuffer, commandbuffer, vulkan.swapchain.width, vulkan.swapchain.height, vec4(0, 0, 0, 0)): |
| 93 WithPipeline(commandbuffer, pipeline): | 93 withPipeline(commandbuffer, pipeline): |
| 94 for label in labels: | 94 for label in labels: |
| 95 Render(label, commandbuffer, pipeline) | 95 render(label, commandbuffer, pipeline) |
| 96 | 96 |
| 97 # cleanup | 97 # cleanup |
| 98 checkVkResult vkDeviceWaitIdle(vulkan.device) | 98 checkVkResult vkDeviceWaitIdle(vulkan.device) |
| 99 DestroyPipeline(pipeline) | 99 destroyPipeline(pipeline) |
| 100 DestroyRenderData(renderdata) | 100 destroyRenderData(renderdata) |
| 101 | 101 |
| 102 proc test_03_layouting(time: float32) = | 102 proc test_03_layouting(time: float32) = |
| 103 var renderdata = InitRenderData() | 103 var renderdata = initRenderData() |
| 104 | 104 |
| 105 var pipeline = CreatePipeline[DefaultFontShader](renderPass = vulkan.swapchain.renderPass) | 105 var pipeline = createPipeline[DefaultFontShader](renderPass = vulkan.swapchain.renderPass) |
| 106 | 106 |
| 107 var font = loadFont("DejaVuSans.ttf", lineHeightPixels = 40) | 107 var font = loadFont("DejaVuSans.ttf", lineHeightPixels = 40) |
| 108 var labels: seq[Textbox] | 108 var labels: seq[Textbox] |
| 109 | 109 |
| 110 for horizontal in HorizontalAlignment: | 110 for horizontal in HorizontalAlignment: |
| 111 labels.add InitTextbox( | 111 labels.add initTextbox( |
| 112 renderdata, | 112 renderdata, |
| 113 pipeline.descriptorSetLayouts[0], | 113 pipeline.descriptorSetLayouts[0], |
| 114 font, | 114 font, |
| 115 $horizontal & " aligned", | 115 $horizontal & " aligned", |
| 116 color = vec4(1, 1, 1, 1), | 116 color = vec4(1, 1, 1, 1), |
| 117 scale = 0.001, | 117 scale = 0.001, |
| 118 position = vec3(0, 0.9 - (horizontal.float * 0.15)), | 118 position = vec3(0, 0.9 - (horizontal.float * 0.15)), |
| 119 horizontalAlignment = horizontal, | 119 horizontalAlignment = horizontal, |
| 120 ) | 120 ) |
| 121 for vertical in VerticalAlignment: | 121 for vertical in VerticalAlignment: |
| 122 labels.add InitTextbox( | 122 labels.add initTextbox( |
| 123 renderdata, | 123 renderdata, |
| 124 pipeline.descriptorSetLayouts[0], | 124 pipeline.descriptorSetLayouts[0], |
| 125 font, | 125 font, |
| 126 $vertical & " aligned", | 126 $vertical & " aligned", |
| 127 color = vec4(1, 1, 1, 1), | 127 color = vec4(1, 1, 1, 1), |
| 128 scale = 0.001, | 128 scale = 0.001, |
| 129 position = vec3(-0.35 + (vertical.float * 0.35), 0.3), | 129 position = vec3(-0.35 + (vertical.float * 0.35), 0.3), |
| 130 verticalAlignment = vertical, | 130 verticalAlignment = vertical, |
| 131 ) | 131 ) |
| 132 labels.add InitTextbox( | 132 labels.add initTextbox( |
| 133 renderdata, | 133 renderdata, |
| 134 pipeline.descriptorSetLayouts[0], | 134 pipeline.descriptorSetLayouts[0], |
| 135 font, | 135 font, |
| 136 """Paragraph | 136 """Paragraph |
| 137 This is a somewhat longer paragraph with a few newlines and a maximum width of 0.2. | 137 This is a somewhat longer paragraph with a few newlines and a maximum width of 0.2. |
| 147 | 147 |
| 148 | 148 |
| 149 var start = getMonoTime() | 149 var start = getMonoTime() |
| 150 while ((getMonoTime() - start).inMilliseconds().int / 1000) < time: | 150 while ((getMonoTime() - start).inMilliseconds().int / 1000) < time: |
| 151 let progress = ((getMonoTime() - start).inMilliseconds().int / 1000) / time | 151 let progress = ((getMonoTime() - start).inMilliseconds().int / 1000) / time |
| 152 WithNextFrame(framebuffer, commandbuffer): | 152 withNextFrame(framebuffer, commandbuffer): |
| 153 WithRenderPass(vulkan.swapchain.renderPass, framebuffer, commandbuffer, vulkan.swapchain.width, vulkan.swapchain.height, vec4(0, 0, 0, 0)): | 153 withRenderPass(vulkan.swapchain.renderPass, framebuffer, commandbuffer, vulkan.swapchain.width, vulkan.swapchain.height, vec4(0, 0, 0, 0)): |
| 154 WithPipeline(commandbuffer, pipeline): | 154 withPipeline(commandbuffer, pipeline): |
| 155 for label in labels: | 155 for label in labels: |
| 156 Render(label, commandbuffer, pipeline) | 156 render(label, commandbuffer, pipeline) |
| 157 | 157 |
| 158 # cleanup | 158 # cleanup |
| 159 checkVkResult vkDeviceWaitIdle(vulkan.device) | 159 checkVkResult vkDeviceWaitIdle(vulkan.device) |
| 160 DestroyPipeline(pipeline) | 160 destroyPipeline(pipeline) |
| 161 DestroyRenderData(renderdata) | 161 destroyRenderData(renderdata) |
| 162 | 162 |
| 163 proc test_04_lots_of_texts(time: float32) = | 163 proc test_04_lots_of_texts(time: float32) = |
| 164 var renderdata = InitRenderData() | 164 var renderdata = initRenderData() |
| 165 | 165 |
| 166 var pipeline = CreatePipeline[DefaultFontShader](renderPass = vulkan.swapchain.renderPass) | 166 var pipeline = createPipeline[DefaultFontShader](renderPass = vulkan.swapchain.renderPass) |
| 167 | 167 |
| 168 var font = loadFont("DejaVuSans.ttf", lineHeightPixels = 160) | 168 var font = loadFont("DejaVuSans.ttf", lineHeightPixels = 160) |
| 169 var labels: seq[Textbox] | 169 var labels: seq[Textbox] |
| 170 for i in 0 ..< 100: | 170 for i in 0 ..< 100: |
| 171 labels.add InitTextbox( | 171 labels.add initTextbox( |
| 172 renderdata, | 172 renderdata, |
| 173 pipeline.descriptorSetLayouts[0], | 173 pipeline.descriptorSetLayouts[0], |
| 174 font, | 174 font, |
| 175 $i, | 175 $i, |
| 176 color = vec4(rand(0.5 .. 1.0), rand(0.5 .. 1.0), rand(0.5 .. 1.0), rand(0.5 .. 1.0)), | 176 color = vec4(rand(0.5 .. 1.0), rand(0.5 .. 1.0), rand(0.5 .. 1.0), rand(0.5 .. 1.0)), |
| 177 scale = rand(0.0002 .. 0.002), | 177 scale = rand(0.0002 .. 0.002), |
| 178 position = vec3(rand(-0.5 .. 0.5), rand(-0.5 .. 0.5), rand(-0.1 .. 0.1)) | 178 position = vec3(rand(-0.5 .. 0.5), rand(-0.5 .. 0.5), rand(-0.1 .. 0.1)) |
| 179 ) | 179 ) |
| 180 labels = labels.sortedByIt(-it.Position.z) | 180 labels = labels.sortedByIt(-it.position.z) |
| 181 | 181 |
| 182 var start = getMonoTime() | 182 var start = getMonoTime() |
| 183 while ((getMonoTime() - start).inMilliseconds().int / 1000) < time: | 183 while ((getMonoTime() - start).inMilliseconds().int / 1000) < time: |
| 184 for l in labels.mitems: | 184 for l in labels.mitems: |
| 185 l.Refresh() | 185 l.refresh() |
| 186 WithNextFrame(framebuffer, commandbuffer): | 186 withNextFrame(framebuffer, commandbuffer): |
| 187 WithRenderPass(vulkan.swapchain.renderPass, framebuffer, commandbuffer, vulkan.swapchain.width, vulkan.swapchain.height, vec4(0, 0, 0, 0)): | 187 withRenderPass(vulkan.swapchain.renderPass, framebuffer, commandbuffer, vulkan.swapchain.width, vulkan.swapchain.height, vec4(0, 0, 0, 0)): |
| 188 WithPipeline(commandbuffer, pipeline): | 188 withPipeline(commandbuffer, pipeline): |
| 189 for l in labels: | 189 for l in labels: |
| 190 Render(l, commandbuffer, pipeline) | 190 render(l, commandbuffer, pipeline) |
| 191 | 191 |
| 192 # cleanup | 192 # cleanup |
| 193 checkVkResult vkDeviceWaitIdle(vulkan.device) | 193 checkVkResult vkDeviceWaitIdle(vulkan.device) |
| 194 DestroyPipeline(pipeline) | 194 destroyPipeline(pipeline) |
| 195 DestroyRenderData(renderdata) | 195 destroyRenderData(renderdata) |
| 196 | 196 |
| 197 when isMainModule: | 197 when isMainModule: |
| 198 var time = 1'f32 | 198 var time = 1'f32 |
| 199 InitVulkan() | 199 initVulkan() |
| 200 | 200 |
| 201 for depthBuffer in [true, false]: | 201 for depthBuffer in [true, false]: |
| 202 var renderpass = CreateDirectPresentationRenderPass(depthBuffer = depthBuffer) | 202 var renderpass = createDirectPresentationRenderPass(depthBuffer = depthBuffer) |
| 203 SetupSwapchain(renderpass = renderpass) | 203 setupSwapchain(renderpass = renderpass) |
| 204 | 204 |
| 205 # tests a simple triangle with minimalistic shader and vertex format | 205 # tests a simple triangle with minimalistic shader and vertex format |
| 206 test_01_static_label(time) | 206 test_01_static_label(time) |
| 207 test_02_multiple_animated(time) | 207 test_02_multiple_animated(time) |
| 208 test_03_layouting(time) | 208 test_03_layouting(time) |
| 209 test_04_lots_of_texts(time) | 209 test_04_lots_of_texts(time) |
| 210 | 210 |
| 211 checkVkResult vkDeviceWaitIdle(vulkan.device) | 211 checkVkResult vkDeviceWaitIdle(vulkan.device) |
| 212 vkDestroyRenderPass(vulkan.device, renderpass.vk, nil) | 212 vkDestroyRenderPass(vulkan.device, renderpass.vk, nil) |
| 213 ClearSwapchain() | 213 clearSwapchain() |
| 214 | 214 |
| 215 DestroyVulkan() | 215 destroyVulkan() |
