Mercurial > games > semicongine
diff tests/test_font.nim @ 272:bfcb41211c5b
add: final font-rendering, API changes fixed
author | Sam <sam@basx.dev> |
---|---|
date | Tue, 30 May 2023 16:59:01 +0700 |
parents | ad4f2b45410b |
children | b145a05c2459 |
line wrap: on
line diff
--- a/tests/test_font.nim Tue May 30 16:58:35 2023 +0700 +++ b/tests/test_font.nim Tue May 30 16:59:01 2023 +0700 @@ -1,51 +1,67 @@ +import std/unicode +import std/tables + import semicongine proc main() = - var scene = newScene("main", root=newEntity("rect", rect())) - var font = loadFont("DejaVuSans.ttf", "myfont") - var img = loadImage("flag.png") - - var textbox = newTextbox(10, 10, font, "Joni") - scene.root.add textbox - var sampler = DefaultSampler() sampler.magnification = VK_FILTER_NEAREST sampler.minification = VK_FILTER_NEAREST - scene.addTextures("my_texture", @[ - Texture(image: img, sampler: sampler) - ]) - scene.addShaderGlobalArray("test2", @[1'f32, 2'f32]) + var font = loadFont("DejaVuSans.ttf", color=newVec4f(1, 0.5, 0.5, 1), resolution=20) + + var scene = newScene("main", root=newEntity("rect")) - var engine = initEngine("Test fonts") + var flag = rect() + flag.setInstanceData("material", @[0'u8]) + # scene.root.add flag + scene.addMaterial Material(name: "material", textures: {"textures": Texture(image: loadImage("flag.png"), sampler: sampler)}.toTable) + + var textbox = newTextbox(32, font, "".toRunes) + scene.addMaterial Material(name: "fontMaterial", textures: {"textures": font.fontAtlas}.toTable) + textbox.mesh.setInstanceData("material", @[1'u8]) + textbox.transform = scale3d(0.1, 0.1) + scene.root.add textbox const vertexInput = @[ + attr[Mat4]("transform", memoryPerformanceHint=PreferFastRead, perInstance=true), attr[Vec3f]("position", memoryPerformanceHint=PreferFastRead), attr[Vec2f]("uv", memoryPerformanceHint=PreferFastRead), + attr[uint8]("material", memoryPerformanceHint=PreferFastRead, perInstance=true), ] - vertexOutput = @[attr[Vec2f]("uvout")] - uniforms = @[attr[float32]("test2", arrayCount=2)] - samplers = @[attr[Sampler2DType]("my_texture", arrayCount=1)] + intermediate = @[attr[Vec2f]("uvout"), attr[uint8]("materialId", noInterpolation=true)] + samplers = @[attr[Sampler2DType]("textures", arrayCount=2)] + uniforms = @[attr[Mat4]("perspective")] fragOutput = @[attr[Vec4f]("color")] - vertexCode = compileGlslShader( - stage=VK_SHADER_STAGE_VERTEX_BIT, + (vertexCode, fragmentCode) = compileVertexFragmentShaderSet( inputs=vertexInput, - uniforms=uniforms, + intermediate=intermediate, + outputs=fragOutput, samplers=samplers, - outputs=vertexOutput, - main="""gl_Position = vec4(position, 1.0); uvout = uv;""" + uniforms=uniforms, + vertexCode="""gl_Position = vec4(position, 1.0) * (transform * Uniforms.perspective); uvout = uv; materialId = material;""", + fragmentCode="""color = texture(textures[materialId], uvout);""", ) - fragmentCode = compileGlslShader( - stage=VK_SHADER_STAGE_FRAGMENT_BIT, - inputs=vertexOutput, - uniforms=uniforms, - samplers=samplers, - outputs=fragOutput, - main="""color = texture(my_texture[0], uvout);""" - ) + + var engine = initEngine("Test fonts") engine.setRenderer(engine.gpuDevice.simpleForwardRenderPass(vertexCode, fragmentCode)) engine.addScene(scene, vertexInput, samplers) + scene.addShaderGlobal("perspective", Unit4F32) + while engine.updateInputs() == Running and not engine.keyIsDown(Escape): + if engine.windowWasResized(): + var winSize = engine.getWindow().size + scene.setShaderGlobal("perspective", orthoWindowAspect(winSize[1] / winSize[0])) + for c in [Key.A, Key.B, Key.C, Key.D, Key.E, Key.F, Key.G, Key.H, Key.I, Key.J, Key.K, Key.L, Key.M, Key.N, Key.O, Key.P, Key.Q, Key.R, Key.S, Key.T, Key.U, Key.V, Key.W, Key.X, Key.Y, Key.Z]: + if engine.keyWasPressed(c): + if engine.keyIsDown(ShiftL) or engine.keyIsDown(ShiftR): + textbox.text = textbox.text & ($c).toRunes + else: + textbox.text = textbox.text & ($c).toRunes[0].toLower() + if engine.keyWasPressed(Space): + textbox.text = textbox.text & " ".toRunes[0] + if engine.keyWasPressed(Backspace) and textbox.text.len > 0: + textbox.text = textbox.text[0 ..< ^1] engine.renderScene(scene) engine.destroy()