comparison tests/test_text.nim @ 1292:5de466f5f087

add: color utils and textbox refactoring
author sam <sam@basx.dev>
date Sun, 04 Aug 2024 00:58:00 +0700
parents 0369fa1ffbd9
children 385dbd68a947
comparison
equal deleted inserted replaced
1291:a6a80b78e811 1292:5de466f5f087
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.layout(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 )
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(commandbuffer, pipeline, label1)
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)
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.layout(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.layout(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.layout(0),
70 font3, 70 font3,
71 " 2", 71 " 2",
72 color = vec4(1, 1, 0, 1), 72 color = vec4(1, 1, 0, 1),
73 scale = 0.001, 73 scale = 0.001,
74 position = vec3(0.3, -0.5) 74 position = vec3(0.3, -0.5)
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(commandbuffer, pipeline, label)
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)
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.layout(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.layout(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.layout(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.
138 138
139 It should display with some space above and have a pleasing appearance overall! :)""", 139 It should display with some space above and have a pleasing appearance overall! :)""",
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(commandbuffer, pipeline, label)
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)
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.layout(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))
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(commandbuffer, pipeline, l)
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)