changeset 1292:5de466f5f087

add: color utils and textbox refactoring
author sam <sam@basx.dev>
date Sun, 04 Aug 2024 00:58:00 +0700
parents a6a80b78e811
children b94b648df33c
files semicongine/core/vector.nim semicongine/text/textbox.nim tests/test_text.nim
diffstat 3 files changed, 37 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- 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)
--- 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)
--- 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)