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