comparison 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
comparison
equal deleted inserted replaced
271:95281f2db400 272:bfcb41211c5b
1 import std/unicode
2 import std/tables
3
1 import semicongine 4 import semicongine
2 5
3 proc main() = 6 proc main() =
4 var scene = newScene("main", root=newEntity("rect", rect()))
5 var font = loadFont("DejaVuSans.ttf", "myfont")
6 var img = loadImage("flag.png")
7
8 var textbox = newTextbox(10, 10, font, "Joni")
9 scene.root.add textbox
10
11 var sampler = DefaultSampler() 7 var sampler = DefaultSampler()
12 sampler.magnification = VK_FILTER_NEAREST 8 sampler.magnification = VK_FILTER_NEAREST
13 sampler.minification = VK_FILTER_NEAREST 9 sampler.minification = VK_FILTER_NEAREST
14 scene.addTextures("my_texture", @[ 10 var font = loadFont("DejaVuSans.ttf", color=newVec4f(1, 0.5, 0.5, 1), resolution=20)
15 Texture(image: img, sampler: sampler)
16 ])
17 scene.addShaderGlobalArray("test2", @[1'f32, 2'f32])
18 11
19 var engine = initEngine("Test fonts") 12 var scene = newScene("main", root=newEntity("rect"))
13
14 var flag = rect()
15 flag.setInstanceData("material", @[0'u8])
16 # scene.root.add flag
17 scene.addMaterial Material(name: "material", textures: {"textures": Texture(image: loadImage("flag.png"), sampler: sampler)}.toTable)
18
19 var textbox = newTextbox(32, font, "".toRunes)
20 scene.addMaterial Material(name: "fontMaterial", textures: {"textures": font.fontAtlas}.toTable)
21 textbox.mesh.setInstanceData("material", @[1'u8])
22 textbox.transform = scale3d(0.1, 0.1)
23 scene.root.add textbox
20 24
21 const 25 const
22 vertexInput = @[ 26 vertexInput = @[
27 attr[Mat4]("transform", memoryPerformanceHint=PreferFastRead, perInstance=true),
23 attr[Vec3f]("position", memoryPerformanceHint=PreferFastRead), 28 attr[Vec3f]("position", memoryPerformanceHint=PreferFastRead),
24 attr[Vec2f]("uv", memoryPerformanceHint=PreferFastRead), 29 attr[Vec2f]("uv", memoryPerformanceHint=PreferFastRead),
30 attr[uint8]("material", memoryPerformanceHint=PreferFastRead, perInstance=true),
25 ] 31 ]
26 vertexOutput = @[attr[Vec2f]("uvout")] 32 intermediate = @[attr[Vec2f]("uvout"), attr[uint8]("materialId", noInterpolation=true)]
27 uniforms = @[attr[float32]("test2", arrayCount=2)] 33 samplers = @[attr[Sampler2DType]("textures", arrayCount=2)]
28 samplers = @[attr[Sampler2DType]("my_texture", arrayCount=1)] 34 uniforms = @[attr[Mat4]("perspective")]
29 fragOutput = @[attr[Vec4f]("color")] 35 fragOutput = @[attr[Vec4f]("color")]
30 vertexCode = compileGlslShader( 36 (vertexCode, fragmentCode) = compileVertexFragmentShaderSet(
31 stage=VK_SHADER_STAGE_VERTEX_BIT,
32 inputs=vertexInput, 37 inputs=vertexInput,
38 intermediate=intermediate,
39 outputs=fragOutput,
40 samplers=samplers,
33 uniforms=uniforms, 41 uniforms=uniforms,
34 samplers=samplers, 42 vertexCode="""gl_Position = vec4(position, 1.0) * (transform * Uniforms.perspective); uvout = uv; materialId = material;""",
35 outputs=vertexOutput, 43 fragmentCode="""color = texture(textures[materialId], uvout);""",
36 main="""gl_Position = vec4(position, 1.0); uvout = uv;"""
37 ) 44 )
38 fragmentCode = compileGlslShader( 45
39 stage=VK_SHADER_STAGE_FRAGMENT_BIT, 46 var engine = initEngine("Test fonts")
40 inputs=vertexOutput,
41 uniforms=uniforms,
42 samplers=samplers,
43 outputs=fragOutput,
44 main="""color = texture(my_texture[0], uvout);"""
45 )
46 engine.setRenderer(engine.gpuDevice.simpleForwardRenderPass(vertexCode, fragmentCode)) 47 engine.setRenderer(engine.gpuDevice.simpleForwardRenderPass(vertexCode, fragmentCode))
47 engine.addScene(scene, vertexInput, samplers) 48 engine.addScene(scene, vertexInput, samplers)
49 scene.addShaderGlobal("perspective", Unit4F32)
50
48 while engine.updateInputs() == Running and not engine.keyIsDown(Escape): 51 while engine.updateInputs() == Running and not engine.keyIsDown(Escape):
52 if engine.windowWasResized():
53 var winSize = engine.getWindow().size
54 scene.setShaderGlobal("perspective", orthoWindowAspect(winSize[1] / winSize[0]))
55 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]:
56 if engine.keyWasPressed(c):
57 if engine.keyIsDown(ShiftL) or engine.keyIsDown(ShiftR):
58 textbox.text = textbox.text & ($c).toRunes
59 else:
60 textbox.text = textbox.text & ($c).toRunes[0].toLower()
61 if engine.keyWasPressed(Space):
62 textbox.text = textbox.text & " ".toRunes[0]
63 if engine.keyWasPressed(Backspace) and textbox.text.len > 0:
64 textbox.text = textbox.text[0 ..< ^1]
49 engine.renderScene(scene) 65 engine.renderScene(scene)
50 engine.destroy() 66 engine.destroy()
51 67
52 68
53 when isMainModule: 69 when isMainModule: