Mercurial > games > semicongine
annotate examples/E04_input.nim @ 62:7153eadb07eb
fix: some formatting, bug when opening on windows.
author | Sam <sam@basx.dev> |
---|---|
date | Wed, 25 Jan 2023 23:56:59 +0700 |
parents | 0f04ba283558 |
children | e766d138cc5d |
rev | line source |
---|---|
60
c57285d292b6
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents:
59
diff
changeset
|
1 import std/strutils |
38
c3c963e7c1a6
did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff
changeset
|
2 import std/times |
c3c963e7c1a6
did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff
changeset
|
3 |
56
94d7eed3f118
did: cleanup main namespace, add: better coordinate handling in input example
Sam <sam@basx.dev>
parents:
40
diff
changeset
|
4 import semicongine |
38
c3c963e7c1a6
did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff
changeset
|
5 |
c3c963e7c1a6
did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff
changeset
|
6 type |
c3c963e7c1a6
did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff
changeset
|
7 # define type of vertex |
c3c963e7c1a6
did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff
changeset
|
8 VertexDataA = object |
59
d7d9420ba675
did: use new vector and matrix names for simpler code
Sam <sam@basx.dev>
parents:
58
diff
changeset
|
9 position: PositionAttribute[Vec2] |
d7d9420ba675
did: use new vector and matrix names for simpler code
Sam <sam@basx.dev>
parents:
58
diff
changeset
|
10 color: ColorAttribute[Vec3] |
61
0f04ba283558
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
60
diff
changeset
|
11 transform: ModelTransformAttribute |
38
c3c963e7c1a6
did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff
changeset
|
12 Uniforms = object |
59
d7d9420ba675
did: use new vector and matrix names for simpler code
Sam <sam@basx.dev>
parents:
58
diff
changeset
|
13 projection: Descriptor[Mat44] |
d7d9420ba675
did: use new vector and matrix names for simpler code
Sam <sam@basx.dev>
parents:
58
diff
changeset
|
14 cursor: Descriptor[Vec2] |
38
c3c963e7c1a6
did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff
changeset
|
15 |
c3c963e7c1a6
did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff
changeset
|
16 var |
c3c963e7c1a6
did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff
changeset
|
17 pipeline: RenderPipeline[VertexDataA, Uniforms] |
c3c963e7c1a6
did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff
changeset
|
18 uniforms: Uniforms |
60
c57285d292b6
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents:
59
diff
changeset
|
19 scene: Thing |
c57285d292b6
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents:
59
diff
changeset
|
20 time: float |
38
c3c963e7c1a6
did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff
changeset
|
21 |
c3c963e7c1a6
did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff
changeset
|
22 |
c3c963e7c1a6
did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff
changeset
|
23 proc globalUpdate(engine: var Engine, dt: float32) = |
60
c57285d292b6
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents:
59
diff
changeset
|
24 time += dt |
c57285d292b6
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents:
59
diff
changeset
|
25 uniforms.cursor.value = engine.input.mousePos |
56
94d7eed3f118
did: cleanup main namespace, add: better coordinate handling in input example
Sam <sam@basx.dev>
parents:
40
diff
changeset
|
26 uniforms.projection.value = ortho[float32]( |
60
c57285d292b6
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents:
59
diff
changeset
|
27 0'f32, float32(engine.vulkan.frameSize.x), |
c57285d292b6
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents:
59
diff
changeset
|
28 0'f32, float32(engine.vulkan.frameSize.y), |
56
94d7eed3f118
did: cleanup main namespace, add: better coordinate handling in input example
Sam <sam@basx.dev>
parents:
40
diff
changeset
|
29 0'f32, 1'f32, |
94d7eed3f118
did: cleanup main namespace, add: better coordinate handling in input example
Sam <sam@basx.dev>
parents:
40
diff
changeset
|
30 ) |
60
c57285d292b6
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents:
59
diff
changeset
|
31 engine.vulkan.device.updateUniformData(pipeline, uniforms) |
38
c3c963e7c1a6
did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff
changeset
|
32 |
60
c57285d292b6
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents:
59
diff
changeset
|
33 let cursor = firstPartWithName[Mesh[VertexDataA]](scene, "cursor") |
c57285d292b6
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents:
59
diff
changeset
|
34 if cursor != nil: |
c57285d292b6
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents:
59
diff
changeset
|
35 for c in cursor.vertexData.color.data.mitems: |
c57285d292b6
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents:
59
diff
changeset
|
36 c[1] = (sin(time * 8) * 0.5 + 0.5) * 0.2 |
c57285d292b6
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents:
59
diff
changeset
|
37 c[2] = (sin(time * 8) * 0.5 + 0.5) * 0.2 |
c57285d292b6
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents:
59
diff
changeset
|
38 engine.vulkan.device.updateVertexData(cursor.vertexData.color) |
61
0f04ba283558
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
60
diff
changeset
|
39 |
0f04ba283558
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
60
diff
changeset
|
40 var trans = translate3d(engine.input.mousePos.x, engine.input.mousePos.y, 0'f32) |
0f04ba283558
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
60
diff
changeset
|
41 # cursor.vertexData.transform.data = @[trans.transposed()] |
0f04ba283558
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
60
diff
changeset
|
42 engine.vulkan.device.updateVertexData(cursor.vertexData.transform) |
60
c57285d292b6
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents:
59
diff
changeset
|
43 |
c57285d292b6
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents:
59
diff
changeset
|
44 |
38
c3c963e7c1a6
did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff
changeset
|
45 const |
c3c963e7c1a6
did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff
changeset
|
46 shape = @[ |
60
c57285d292b6
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents:
59
diff
changeset
|
47 Vec2([ - 1'f32, - 1'f32]), |
c57285d292b6
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents:
59
diff
changeset
|
48 Vec2([1'f32, - 1'f32]), |
59
d7d9420ba675
did: use new vector and matrix names for simpler code
Sam <sam@basx.dev>
parents:
58
diff
changeset
|
49 Vec2([-0.3'f32, -0.3'f32]), |
d7d9420ba675
did: use new vector and matrix names for simpler code
Sam <sam@basx.dev>
parents:
58
diff
changeset
|
50 Vec2([-0.3'f32, -0.3'f32]), |
60
c57285d292b6
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents:
59
diff
changeset
|
51 Vec2([ - 1'f32, 1'f32]), |
c57285d292b6
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents:
59
diff
changeset
|
52 Vec2([ - 1'f32, - 1'f32]), |
38
c3c963e7c1a6
did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff
changeset
|
53 ] |
c3c963e7c1a6
did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff
changeset
|
54 colors = @[ |
59
d7d9420ba675
did: use new vector and matrix names for simpler code
Sam <sam@basx.dev>
parents:
58
diff
changeset
|
55 Vec3([1'f32, 0'f32, 0'f32]), |
d7d9420ba675
did: use new vector and matrix names for simpler code
Sam <sam@basx.dev>
parents:
58
diff
changeset
|
56 Vec3([1'f32, 0'f32, 0'f32]), |
d7d9420ba675
did: use new vector and matrix names for simpler code
Sam <sam@basx.dev>
parents:
58
diff
changeset
|
57 Vec3([1'f32, 0'f32, 0'f32]), |
d7d9420ba675
did: use new vector and matrix names for simpler code
Sam <sam@basx.dev>
parents:
58
diff
changeset
|
58 Vec3([0.8'f32, 0'f32, 0'f32]), |
d7d9420ba675
did: use new vector and matrix names for simpler code
Sam <sam@basx.dev>
parents:
58
diff
changeset
|
59 Vec3([0.8'f32, 0'f32, 0'f32]), |
d7d9420ba675
did: use new vector and matrix names for simpler code
Sam <sam@basx.dev>
parents:
58
diff
changeset
|
60 Vec3([0.8'f32, 0'f32, 0'f32]), |
38
c3c963e7c1a6
did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff
changeset
|
61 ] |
c3c963e7c1a6
did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff
changeset
|
62 |
c3c963e7c1a6
did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff
changeset
|
63 when isMainModule: |
c3c963e7c1a6
did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff
changeset
|
64 var myengine = igniteEngine("Input") |
c3c963e7c1a6
did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff
changeset
|
65 |
57 | 66 var cursormesh = new Mesh[VertexDataA] |
67 cursormesh.vertexData = VertexDataA( | |
60
c57285d292b6
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents:
59
diff
changeset
|
68 position: PositionAttribute[Vec2](data: shape, useOnDeviceMemory: true), |
59
d7d9420ba675
did: use new vector and matrix names for simpler code
Sam <sam@basx.dev>
parents:
58
diff
changeset
|
69 color: ColorAttribute[Vec3](data: colors), |
61
0f04ba283558
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
60
diff
changeset
|
70 transform: ModelTransformAttribute(data: @[Unit44]), |
38
c3c963e7c1a6
did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff
changeset
|
71 ) |
c3c963e7c1a6
did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff
changeset
|
72 # transform the cursor a bit to make it look nice |
57 | 73 for i in 0 ..< cursormesh.vertexData.position.data.len: |
38
c3c963e7c1a6
did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff
changeset
|
74 let cursorscale = ( |
56
94d7eed3f118
did: cleanup main namespace, add: better coordinate handling in input example
Sam <sam@basx.dev>
parents:
40
diff
changeset
|
75 scale2d(20'f32, 20'f32) * |
38
c3c963e7c1a6
did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff
changeset
|
76 translate2d(1'f32, 1'f32) * |
c3c963e7c1a6
did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff
changeset
|
77 rotate2d(-float32(PI) / 4'f32) * |
c3c963e7c1a6
did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff
changeset
|
78 scale2d(0.5'f32, 1'f32) * |
c3c963e7c1a6
did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff
changeset
|
79 rotate2d(float32(PI) / 4'f32) |
c3c963e7c1a6
did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff
changeset
|
80 ) |
60
c57285d292b6
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents:
59
diff
changeset
|
81 let pos = Vec3([cursormesh.vertexData.position.data[i][0], |
c57285d292b6
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents:
59
diff
changeset
|
82 cursormesh.vertexData.position.data[i][1], 1'f32]) |
57 | 83 cursormesh.vertexData.position.data[i] = (cursorscale * pos).xy |
84 | |
85 var boxmesh = new Mesh[VertexDataA] | |
86 boxmesh.vertexData = VertexDataA( | |
59
d7d9420ba675
did: use new vector and matrix names for simpler code
Sam <sam@basx.dev>
parents:
58
diff
changeset
|
87 position: PositionAttribute[Vec2](data: shape), |
d7d9420ba675
did: use new vector and matrix names for simpler code
Sam <sam@basx.dev>
parents:
58
diff
changeset
|
88 color: ColorAttribute[Vec3](data: colors), |
61
0f04ba283558
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
60
diff
changeset
|
89 transform: ModelTransformAttribute(data: @[Unit44]), |
57 | 90 ) |
60
c57285d292b6
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents:
59
diff
changeset
|
91 for i in 0 ..< boxmesh.vertexData.position.data.len: |
c57285d292b6
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents:
59
diff
changeset
|
92 let boxscale = translate2d(100'f32, 100'f32) * scale2d(100'f32, 100'f32) |
c57285d292b6
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents:
59
diff
changeset
|
93 let pos = Vec3([boxmesh.vertexData.position.data[i][0], |
c57285d292b6
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents:
59
diff
changeset
|
94 boxmesh.vertexData.position.data[i][1], 1'f32]) |
c57285d292b6
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents:
59
diff
changeset
|
95 boxmesh.vertexData.position.data[i] = (boxscale * pos).xy |
38
c3c963e7c1a6
did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff
changeset
|
96 |
60
c57285d292b6
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents:
59
diff
changeset
|
97 scene = newThing("scene") |
c57285d292b6
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents:
59
diff
changeset
|
98 scene.add newThing("cursor", cursormesh) |
c57285d292b6
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents:
59
diff
changeset
|
99 scene.add newThing("a box", boxmesh, newTransform(Unit44), newTransform( |
c57285d292b6
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents:
59
diff
changeset
|
100 translate3d(1'f32, 0'f32, 0'f32))) |
c57285d292b6
did: deep refactoring of handling vertrex attribute and buffer updates, don't ask ;(
Sam <sam@basx.dev>
parents:
59
diff
changeset
|
101 scene.add newTransform(scale3d(1.5'f32, 1.5'f32, 1.5'f32)) |
38
c3c963e7c1a6
did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff
changeset
|
102 |
c3c963e7c1a6
did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff
changeset
|
103 # upload data, prepare shaders, etc |
c3c963e7c1a6
did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff
changeset
|
104 const vertexShader = generateVertexShaderCode[VertexDataA, Uniforms](""" |
61
0f04ba283558
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
60
diff
changeset
|
105 out_position = uniforms.projection * transform * vec4(position, 0, 1); |
38
c3c963e7c1a6
did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff
changeset
|
106 """) |
c3c963e7c1a6
did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff
changeset
|
107 const fragmentShader = generateFragmentShaderCode[VertexDataA]() |
c3c963e7c1a6
did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff
changeset
|
108 pipeline = setupPipeline[VertexDataA, Uniforms, uint16]( |
c3c963e7c1a6
did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff
changeset
|
109 myengine, |
c3c963e7c1a6
did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff
changeset
|
110 scene, |
c3c963e7c1a6
did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff
changeset
|
111 vertexShader, |
c3c963e7c1a6
did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff
changeset
|
112 fragmentShader |
c3c963e7c1a6
did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff
changeset
|
113 ) |
c3c963e7c1a6
did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff
changeset
|
114 # show something |
c3c963e7c1a6
did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff
changeset
|
115 myengine.run(pipeline, globalUpdate) |
c3c963e7c1a6
did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff
changeset
|
116 pipeline.trash() |
c3c963e7c1a6
did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff
changeset
|
117 myengine.trash() |