Mercurial > games > semicongine
annotate tests/test_font.nim @ 733:07c755e65a4a
add: final font-rendering, API changes fixed
author | Sam <sam@basx.dev> |
---|---|
date | Tue, 30 May 2023 16:59:01 +0700 |
parents | 443791a202c8 |
children | b145a05c2459 |
rev | line source |
---|---|
733
07c755e65a4a
add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents:
726
diff
changeset
|
1 import std/unicode |
07c755e65a4a
add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents:
726
diff
changeset
|
2 import std/tables |
07c755e65a4a
add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents:
726
diff
changeset
|
3 |
726 | 4 import semicongine |
5 | |
6 proc main() = | |
7 var sampler = DefaultSampler() | |
8 sampler.magnification = VK_FILTER_NEAREST | |
9 sampler.minification = VK_FILTER_NEAREST | |
733
07c755e65a4a
add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents:
726
diff
changeset
|
10 var font = loadFont("DejaVuSans.ttf", color=newVec4f(1, 0.5, 0.5, 1), resolution=20) |
07c755e65a4a
add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents:
726
diff
changeset
|
11 |
07c755e65a4a
add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents:
726
diff
changeset
|
12 var scene = newScene("main", root=newEntity("rect")) |
726 | 13 |
733
07c755e65a4a
add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents:
726
diff
changeset
|
14 var flag = rect() |
07c755e65a4a
add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents:
726
diff
changeset
|
15 flag.setInstanceData("material", @[0'u8]) |
07c755e65a4a
add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents:
726
diff
changeset
|
16 # scene.root.add flag |
07c755e65a4a
add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents:
726
diff
changeset
|
17 scene.addMaterial Material(name: "material", textures: {"textures": Texture(image: loadImage("flag.png"), sampler: sampler)}.toTable) |
07c755e65a4a
add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents:
726
diff
changeset
|
18 |
07c755e65a4a
add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents:
726
diff
changeset
|
19 var textbox = newTextbox(32, font, "".toRunes) |
07c755e65a4a
add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents:
726
diff
changeset
|
20 scene.addMaterial Material(name: "fontMaterial", textures: {"textures": font.fontAtlas}.toTable) |
07c755e65a4a
add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents:
726
diff
changeset
|
21 textbox.mesh.setInstanceData("material", @[1'u8]) |
07c755e65a4a
add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents:
726
diff
changeset
|
22 textbox.transform = scale3d(0.1, 0.1) |
07c755e65a4a
add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents:
726
diff
changeset
|
23 scene.root.add textbox |
726 | 24 |
25 const | |
26 vertexInput = @[ | |
733
07c755e65a4a
add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents:
726
diff
changeset
|
27 attr[Mat4]("transform", memoryPerformanceHint=PreferFastRead, perInstance=true), |
726 | 28 attr[Vec3f]("position", memoryPerformanceHint=PreferFastRead), |
29 attr[Vec2f]("uv", memoryPerformanceHint=PreferFastRead), | |
733
07c755e65a4a
add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents:
726
diff
changeset
|
30 attr[uint8]("material", memoryPerformanceHint=PreferFastRead, perInstance=true), |
726 | 31 ] |
733
07c755e65a4a
add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents:
726
diff
changeset
|
32 intermediate = @[attr[Vec2f]("uvout"), attr[uint8]("materialId", noInterpolation=true)] |
07c755e65a4a
add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents:
726
diff
changeset
|
33 samplers = @[attr[Sampler2DType]("textures", arrayCount=2)] |
07c755e65a4a
add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents:
726
diff
changeset
|
34 uniforms = @[attr[Mat4]("perspective")] |
726 | 35 fragOutput = @[attr[Vec4f]("color")] |
733
07c755e65a4a
add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents:
726
diff
changeset
|
36 (vertexCode, fragmentCode) = compileVertexFragmentShaderSet( |
726 | 37 inputs=vertexInput, |
733
07c755e65a4a
add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents:
726
diff
changeset
|
38 intermediate=intermediate, |
07c755e65a4a
add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents:
726
diff
changeset
|
39 outputs=fragOutput, |
726 | 40 samplers=samplers, |
733
07c755e65a4a
add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents:
726
diff
changeset
|
41 uniforms=uniforms, |
07c755e65a4a
add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents:
726
diff
changeset
|
42 vertexCode="""gl_Position = vec4(position, 1.0) * (transform * Uniforms.perspective); uvout = uv; materialId = material;""", |
07c755e65a4a
add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents:
726
diff
changeset
|
43 fragmentCode="""color = texture(textures[materialId], uvout);""", |
726 | 44 ) |
733
07c755e65a4a
add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents:
726
diff
changeset
|
45 |
07c755e65a4a
add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents:
726
diff
changeset
|
46 var engine = initEngine("Test fonts") |
726 | 47 engine.setRenderer(engine.gpuDevice.simpleForwardRenderPass(vertexCode, fragmentCode)) |
48 engine.addScene(scene, vertexInput, samplers) | |
733
07c755e65a4a
add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents:
726
diff
changeset
|
49 scene.addShaderGlobal("perspective", Unit4F32) |
07c755e65a4a
add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents:
726
diff
changeset
|
50 |
726 | 51 while engine.updateInputs() == Running and not engine.keyIsDown(Escape): |
733
07c755e65a4a
add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents:
726
diff
changeset
|
52 if engine.windowWasResized(): |
07c755e65a4a
add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents:
726
diff
changeset
|
53 var winSize = engine.getWindow().size |
07c755e65a4a
add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents:
726
diff
changeset
|
54 scene.setShaderGlobal("perspective", orthoWindowAspect(winSize[1] / winSize[0])) |
07c755e65a4a
add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents:
726
diff
changeset
|
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]: |
07c755e65a4a
add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents:
726
diff
changeset
|
56 if engine.keyWasPressed(c): |
07c755e65a4a
add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents:
726
diff
changeset
|
57 if engine.keyIsDown(ShiftL) or engine.keyIsDown(ShiftR): |
07c755e65a4a
add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents:
726
diff
changeset
|
58 textbox.text = textbox.text & ($c).toRunes |
07c755e65a4a
add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents:
726
diff
changeset
|
59 else: |
07c755e65a4a
add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents:
726
diff
changeset
|
60 textbox.text = textbox.text & ($c).toRunes[0].toLower() |
07c755e65a4a
add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents:
726
diff
changeset
|
61 if engine.keyWasPressed(Space): |
07c755e65a4a
add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents:
726
diff
changeset
|
62 textbox.text = textbox.text & " ".toRunes[0] |
07c755e65a4a
add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents:
726
diff
changeset
|
63 if engine.keyWasPressed(Backspace) and textbox.text.len > 0: |
07c755e65a4a
add: final font-rendering, API changes fixed
Sam <sam@basx.dev>
parents:
726
diff
changeset
|
64 textbox.text = textbox.text[0 ..< ^1] |
726 | 65 engine.renderScene(scene) |
66 engine.destroy() | |
67 | |
68 | |
69 when isMainModule: | |
70 main() |