Mercurial > games > semicongine
changeset 893:a0826956dc5c
did: small refactoring
author | Sam <sam@basx.dev> |
---|---|
date | Sat, 10 Feb 2024 15:54:25 +0700 |
parents | f2d7529b49ca |
children | 2aa26c23cc60 |
files | semicongine/text.nim tests/test_font.nim |
diffstat | 2 files changed, 28 insertions(+), 27 deletions(-) [+] |
line wrap: on
line diff
--- a/semicongine/text.nim Fri Feb 09 20:02:55 2024 +0700 +++ b/semicongine/text.nim Sat Feb 10 15:54:25 2024 +0700 @@ -1,6 +1,5 @@ import std/tables import std/algorithm -# import std/sequtils import std/unicode import std/strformat @@ -10,31 +9,12 @@ import ./vulkan/shader const - SHADER_ATTRIB_PREFIX = "semicon_text_" - MAX_TEXT_MATERIALS = 10 -var instanceCounter = 0 - -type - Text* = object - maxLen*: int - font*: Font - maxWidth: float32 = 0 - # properties: - text: seq[Rune] - position: Vec2f - horizontalAlignment: HorizontalAlignment = Center - verticalAlignment: VerticalAlignment = Center - scale: float32 - aspect_ratio: float32 - # management/internal: - dirty: bool # is true if any of the attributes changed - processedText: seq[Rune] # used to store processed (word-wrapper) text to preserve original - lastRenderedText: seq[Rune] # stores the last rendered text, to prevent unnecessary updates - mesh: Mesh - -const NEWLINE = Rune('\n') SPACE = Rune(' ') + + # font shader + MAX_TEXT_MATERIALS = 10 + SHADER_ATTRIB_PREFIX = "semicon_text_" POSITION_ATTRIB = SHADER_ATTRIB_PREFIX & "position" UV_ATTRIB = SHADER_ATTRIB_PREFIX & "uv" TEXT_MATERIAL_TYPE* = MaterialType( @@ -64,6 +44,26 @@ fragmentCode = &"""color = vec4(Uniforms.color[materialIndexOut].rgb, Uniforms.color[materialIndexOut].a * texture(fontAtlas[materialIndexOut], uvFrag).r);""" ) +var instanceCounter = 0 + +type + Text* = object + maxLen*: int + font*: Font + maxWidth: float32 = 0 + # properties: + text: seq[Rune] + position: Vec2f + horizontalAlignment: HorizontalAlignment = Center + verticalAlignment: VerticalAlignment = Center + scale: float32 + aspect_ratio: float32 + # management/internal: + dirty: bool # is true if any of the attributes changed + processedText: seq[Rune] # used to store processed (word-wrapper) text to preserve original + lastRenderedText: seq[Rune] # stores the last rendered text, to prevent unnecessary updates + mesh: Mesh + func `$`*(text: Text): string = "\"" & $text.text[0 ..< min(text.text.len, 16)] & "\"" @@ -273,9 +273,9 @@ [uint16(offset + 2), uint16(offset + 3), uint16(offset + 0)], ] - result = Text(maxLen: maxLen, text: text, font: font, dirty: true, scale: scale, position: position, aspect_ratio: 1, horizontalAlignment: horizontalAlignment, verticalAlignment: verticalAlignment, maxWidth: maxWidth) + result = Text(maxLen: maxLen, font: font, dirty: true, scale: scale, position: position, aspect_ratio: 1, horizontalAlignment: horizontalAlignment, verticalAlignment: verticalAlignment, maxWidth: maxWidth) + `text=`(result, text) result.mesh = newMesh(positions = positions, indices = indices, uvs = uvs, name = &"text-{instanceCounter}") - inc instanceCounter result.mesh[].renameAttribute("position", POSITION_ATTRIB) result.mesh[].renameAttribute("uv", UV_ATTRIB) result.mesh.material = initMaterialData( @@ -283,6 +283,7 @@ name = font.name & " text", attributes = {"fontAtlas": initDataList(@[font.fontAtlas]), "color": initDataList(@[color])}, ) + inc instanceCounter result.refresh()
--- a/tests/test_font.nim Fri Feb 09 20:02:55 2024 +0700 +++ b/tests/test_font.nim Sat Feb 10 15:54:25 2024 +0700 @@ -22,7 +22,7 @@ Vertical alignment: F4: Top F5: Center - F6: Bottom""", scale = 0.0002, position = newVec2f(0, 0), horizontalAlignment = Left, verticalAlignment = Top) + F6: Bottom""", scale = 0.0002, position = newVec2f(-0.9, -0.9), horizontalAlignment = Left, verticalAlignment = Top) scene.add main_text scene.add help_text engine.loadScene(scene)