# HG changeset patch # User sam # Date 1722707880 -25200 # Node ID 5de466f5f08718403617957dec2b37ff3dca8fe3 # Parent a6a80b78e811ead155a5ce37fbd7c924ef246bdb add: color utils and textbox refactoring diff -r a6a80b78e811 -r 5de466f5f087 semicongine/core/vector.nim --- a/semicongine/core/vector.nim Sat Aug 03 20:45:22 2024 +0700 +++ b/semicongine/core/vector.nim Sun Aug 04 00:58:00 2024 +0700 @@ -66,6 +66,29 @@ func vec4i*[T: SomeInteger](x: T): Vec4i = vec4i(x, 0, 0, 0) func vec4i*(): Vec4i = vec4i(0, 0, 0, 0) +# shortcuts color +func toVec*(value: string): Vec4f = + assert value != "" + var hex = value + if hex[0] == '#': + hex = hex[1 .. ^1] + # when 3 or 6 -> set alpha to 1.0 + assert hex.len == 3 or hex.len == 6 or hex.len == 4 or hex.len == 8 + if hex.len == 3: + hex = hex & "f" + if hex.len == 4: + hex = hex[0] & hex[0] & hex[1] & hex[1] & hex[2] & hex[2] & hex[3] & hex[3] + if hex.len == 6: + hex = hex & "ff" + assert hex.len == 8 + let + r = parseHexInt(hex[0 .. 1]).float32 / 255'f32 + g = parseHexInt(hex[2 .. 3]).float32 / 255'f32 + b = parseHexInt(hex[4 .. 5]).float32 / 255'f32 + a = parseHexInt(hex[6 .. 7]).float32 / 255'f32 + return vec4(r, g, b, a) + + const X* = vec3(1, 0, 0) Y* = vec3(0, 1, 0) diff -r a6a80b78e811 -r 5de466f5f087 semicongine/text/textbox.nim --- a/semicongine/text/textbox.nim Sat Aug 03 20:45:22 2024 +0700 +++ b/semicongine/text/textbox.nim Sun Aug 04 00:58:00 2024 +0700 @@ -173,7 +173,7 @@ textbox.refreshGeometry() textbox.dirtyGeometry = false -proc render*(textbox: Textbox, commandbuffer: VkCommandBuffer, pipeline: Pipeline) = +proc render*(commandbuffer: VkCommandBuffer, pipeline: Pipeline, textbox: Textbox) = withBind(commandbuffer, (textbox.shaderdata, ), pipeline): render(commandbuffer = commandbuffer, pipeline = pipeline, mesh = textbox) @@ -234,5 +234,5 @@ initDescriptorSet(renderdata, descriptorSetLayout, result.shaderdata) result.refresh() - updateAllGPUBuffers(result, flush = true, allFrames = true) + updateAllGPUBuffers(result, flush = true) updateAllGPUBuffers(result.shaderdata.data, flush = true) diff -r a6a80b78e811 -r 5de466f5f087 tests/test_text.nim --- a/tests/test_text.nim Sat Aug 03 20:45:22 2024 +0700 +++ b/tests/test_text.nim Sun Aug 04 00:58:00 2024 +0700 @@ -17,7 +17,7 @@ var font = loadFont("Overhaul.ttf", lineHeightPixels = 160) var label1 = initTextbox( renderdata, - pipeline.descriptorSetLayouts[0], + pipeline.layout(0), font, "Hello semicongine!", color = vec4(1, 1, 1, 1), @@ -30,7 +30,7 @@ withNextFrame(framebuffer, commandbuffer): withRenderPass(vulkan.swapchain.renderPass, framebuffer, commandbuffer, vulkan.swapchain.width, vulkan.swapchain.height, vec4(0, 0, 0, 0)): withPipeline(commandbuffer, pipeline): - render(label1, commandbuffer, pipeline) + render(commandbuffer, pipeline, label1) # cleanup checkVkResult vkDeviceWaitIdle(vulkan.device) @@ -48,7 +48,7 @@ var labels = [ initTextbox( renderdata, - pipeline.descriptorSetLayouts[0], + pipeline.layout(0), font1, " 0", color = vec4(0, 1, 1, 1), @@ -57,7 +57,7 @@ ), initTextbox( renderdata, - pipeline.descriptorSetLayouts[0], + pipeline.layout(0), font2, " 1", color = vec4(1, 0, 1, 1), @@ -66,7 +66,7 @@ ), initTextbox( renderdata, - pipeline.descriptorSetLayouts[0], + pipeline.layout(0), font3, " 2", color = vec4(1, 1, 0, 1), @@ -92,7 +92,7 @@ withRenderPass(vulkan.swapchain.renderPass, framebuffer, commandbuffer, vulkan.swapchain.width, vulkan.swapchain.height, vec4(0, 0, 0, 0)): withPipeline(commandbuffer, pipeline): for label in labels: - render(label, commandbuffer, pipeline) + render(commandbuffer, pipeline, label) # cleanup checkVkResult vkDeviceWaitIdle(vulkan.device) @@ -110,7 +110,7 @@ for horizontal in HorizontalAlignment: labels.add initTextbox( renderdata, - pipeline.descriptorSetLayouts[0], + pipeline.layout(0), font, $horizontal & " aligned", color = vec4(1, 1, 1, 1), @@ -121,7 +121,7 @@ for vertical in VerticalAlignment: labels.add initTextbox( renderdata, - pipeline.descriptorSetLayouts[0], + pipeline.layout(0), font, $vertical & " aligned", color = vec4(1, 1, 1, 1), @@ -131,7 +131,7 @@ ) labels.add initTextbox( renderdata, - pipeline.descriptorSetLayouts[0], + pipeline.layout(0), font, """Paragraph This is a somewhat longer paragraph with a few newlines and a maximum width of 0.2. @@ -153,7 +153,7 @@ withRenderPass(vulkan.swapchain.renderPass, framebuffer, commandbuffer, vulkan.swapchain.width, vulkan.swapchain.height, vec4(0, 0, 0, 0)): withPipeline(commandbuffer, pipeline): for label in labels: - render(label, commandbuffer, pipeline) + render(commandbuffer, pipeline, label) # cleanup checkVkResult vkDeviceWaitIdle(vulkan.device) @@ -170,7 +170,7 @@ for i in 0 ..< 100: labels.add initTextbox( renderdata, - pipeline.descriptorSetLayouts[0], + pipeline.layout(0), font, $i, color = vec4(rand(0.5 .. 1.0), rand(0.5 .. 1.0), rand(0.5 .. 1.0), rand(0.5 .. 1.0)), @@ -187,7 +187,7 @@ withRenderPass(vulkan.swapchain.renderPass, framebuffer, commandbuffer, vulkan.swapchain.width, vulkan.swapchain.height, vec4(0, 0, 0, 0)): withPipeline(commandbuffer, pipeline): for l in labels: - render(l, commandbuffer, pipeline) + render(commandbuffer, pipeline, l) # cleanup checkVkResult vkDeviceWaitIdle(vulkan.device)