comparison tests/test_font.nim @ 791:43432dad4797

did: remove some stuff from the heap, maybe nicer?
author Sam <sam@basx.dev>
date Sat, 02 Sep 2023 23:51:02 +0700
parents 754835bf175e
children 05fb85ba97dd
comparison
equal deleted inserted replaced
790:a979e5c581de 791:43432dad4797
2 import std/tables 2 import std/tables
3 3
4 import semicongine 4 import semicongine
5 5
6 proc main() = 6 proc main() =
7 var sampler = DefaultSampler()
8 sampler.magnification = VK_FILTER_NEAREST
9 sampler.minification = VK_FILTER_NEAREST
10 var font = loadFont("DejaVuSans.ttf", color=newVec4f(1, 0.5, 0.5, 1), resolution=20) 7 var font = loadFont("DejaVuSans.ttf", color=newVec4f(1, 0.5, 0.5, 1), resolution=20)
11 8
12 var scene = newScene("main", root=newEntity("rect")) 9 var textbox = initTextbox(32, font, "".toRunes)
13 10 var scene = Scene(name: "main", meshes: @[textbox.mesh])
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
24
25 const
26 vertexInput = @[
27 attr[Mat4]("transform", memoryPerformanceHint=PreferFastRead, perInstance=true),
28 attr[Vec3f]("position", memoryPerformanceHint=PreferFastRead),
29 attr[Vec2f]("uv", memoryPerformanceHint=PreferFastRead),
30 attr[uint8]("material", memoryPerformanceHint=PreferFastRead, perInstance=true),
31 ]
32 intermediate = @[attr[Vec2f]("uvout"), attr[uint8]("materialId", noInterpolation=true)]
33 samplers = @[attr[Sampler2DType]("textures", arrayCount=2)]
34 uniforms = @[attr[Mat4]("perspective")]
35 fragOutput = @[attr[Vec4f]("color")]
36 (vertexCode, fragmentCode) = compileVertexFragmentShaderSet(
37 inputs=vertexInput,
38 intermediate=intermediate,
39 outputs=fragOutput,
40 samplers=samplers,
41 uniforms=uniforms,
42 vertexCode="""gl_Position = vec4(position, 1.0) * (transform * Uniforms.perspective); uvout = uv; materialId = material;""",
43 fragmentCode="""color = texture(textures[materialId], uvout);""",
44 )
45
46 var engine = initEngine("Test fonts") 11 var engine = initEngine("Test fonts")
47 engine.setRenderer(engine.gpuDevice.simpleForwardRenderPass(vertexCode, fragmentCode)) 12 engine.initRenderer()
48 engine.addScene(scene, vertexInput, samplers, materialIndexAttribute="") 13 engine.addScene(scene)
49 scene.addShaderGlobal("perspective", Unit4F32) 14 scene.addShaderGlobal("perspective", Unit4F32)
50 15
51 while engine.updateInputs() == Running and not engine.keyIsDown(Escape): 16 while engine.updateInputs() == Running and not engine.keyIsDown(Escape):
52 if engine.windowWasResized(): 17 if engine.windowWasResized():
53 var winSize = engine.getWindow().size 18 var winSize = engine.getWindow().size