Mercurial > games > semicongine
comparison examples/E10_pong.nim @ 606:f41c1b78cf5b
add: upgrade all simple examples to new engine version
| author | Sam <sam@basx.dev> |
|---|---|
| date | Wed, 26 Apr 2023 02:15:43 +0700 |
| parents | db4e583b40c3 |
| children | 253dd797e719 |
comparison
equal
deleted
inserted
replaced
| 605:510c651cb27d | 606:f41c1b78cf5b |
|---|---|
| 1 import semicongine | 1 import semicongine |
| 2 | |
| 3 type | |
| 4 Vertex = object | |
| 5 position: PositionAttribute[Vec2] | |
| 6 color: ColorAttribute[Vec3] | |
| 7 transform: ModelTransformAttribute | |
| 8 Uniforms = object | |
| 9 view: ViewProjectionTransform | |
| 10 | 2 |
| 11 const | 3 const |
| 12 barcolor = RGBfromHex("5A3F00").gamma(2.2) | 4 barcolor = RGBfromHex("5A3F00").gamma(2.2) |
| 13 barSize = 0.1'f | 5 barSize = 0.1'f |
| 14 barWidth = 0.01'f | 6 barWidth = 0.01'f |
| 16 levelRatio = 1 | 8 levelRatio = 1 |
| 17 ballSize = 0.01'f | 9 ballSize = 0.01'f |
| 18 backgroundColor = RGBAfromHex("FAC034").gamma(2.2) | 10 backgroundColor = RGBAfromHex("FAC034").gamma(2.2) |
| 19 ballSpeed = 60'f | 11 ballSpeed = 60'f |
| 20 | 12 |
| 21 echo RGBAfromHex("FAC034") | |
| 22 echo RGBAfromHex("FAC034").gamma(2.2) | |
| 23 var | 13 var |
| 24 uniforms = Uniforms() | 14 level: Entity |
| 25 pipeline: RenderPipeline[Vertex, Uniforms] | 15 ballVelocity = newVec2f(1, 1).normalized * ballSpeed |
| 26 level: Thing | |
| 27 ballVelocity = Vec2([1'f, 1'f]).normalized * ballSpeed | |
| 28 | 16 |
| 17 #[ | |
| 29 proc globalUpdate(engine: var Engine; t, dt: float32) = | 18 proc globalUpdate(engine: var Engine; t, dt: float32) = |
| 30 var height = float32(engine.vulkan.frameSize.y) / float32( | 19 var height = float32(engine.vulkan.frameSize.y) / float32( |
| 31 engine.vulkan.frameSize.x) | 20 engine.vulkan.frameSize.x) |
| 32 var width = 1'f | 21 var width = 1'f |
| 33 uniforms.view.value = ortho[float32]( | 22 uniforms.view.value = ortho[float32]( |
| 66 barBottom = player.transform.col(3).y + barSize/2 | 55 barBottom = player.transform.col(3).y + barSize/2 |
| 67 ballTop = ball.transform.col(3).y - ballSize/2 | 56 ballTop = ball.transform.col(3).y - ballSize/2 |
| 68 ballBottom = ball.transform.col(3).y + ballSize/2 | 57 ballBottom = ball.transform.col(3).y + ballSize/2 |
| 69 if ballTop >= barTop and ballBottom <= barBottom: | 58 if ballTop >= barTop and ballBottom <= barBottom: |
| 70 ballVelocity[0] = abs(ballVelocity[0]) | 59 ballVelocity[0] = abs(ballVelocity[0]) |
| 71 | 60 ]# |
| 72 | 61 |
| 73 when isMainModule: | 62 when isMainModule: |
| 74 var myengine = igniteEngine("Pong") | 63 var myengine = initEngine("Pong") |
| 75 myengine.maxFPS = 61 | 64 level = newEntity("Level") |
| 76 level = newThing("Level") | 65 var playerbarmesh = rect() |
| 77 var playerbarmesh = quad[Vertex, Vec2, float32]() | |
| 78 playerbarmesh.vertexData.color.data = @[barcolor, barcolor, barcolor, barcolor] | 66 playerbarmesh.vertexData.color.data = @[barcolor, barcolor, barcolor, barcolor] |
| 79 playerbarmesh.vertexData.transform.data = @[Unit44] | 67 var playerbar = newEntity("playerbar", playerbarmesh) |
| 80 var playerbar = newThing("playerbar", playerbarmesh) | 68 playerbar.transform = scale3d(barWidth, barSize, 1'f) * translate3d(0.5'f, 0'f, 0'f) |
| 81 playerbar.transform = scale3d(barWidth, barSize, 1'f) * translate3d(0.5'f, | 69 var player = newEntity("player", playerbar) |
| 82 0'f, 0'f) | |
| 83 var player = newThing("player", playerbar) | |
| 84 player.transform = translate3d(0'f, 0.3'f, 0'f) | 70 player.transform = translate3d(0'f, 0.3'f, 0'f) |
| 85 level.add player | 71 level.add player |
| 86 | 72 |
| 87 var ballmesh = circle[Vertex, Vec2, float32]() | 73 var ballmesh = circle() |
| 88 ballmesh.vertexData.color.data = newSeq[Vec3]( | 74 ballmesh.vertexData.color.data = newSeq[Vec3](ballmesh.vertexData.position.data.len) |
| 89 ballmesh.vertexData.position.data.len) | |
| 90 for i in 0 ..< ballmesh.vertexData.color.data.len: | 75 for i in 0 ..< ballmesh.vertexData.color.data.len: |
| 91 ballmesh.vertexData.color.data[i] = ballcolor | 76 ballmesh.vertexData.color.data[i] = ballcolor |
| 92 ballmesh.vertexData.transform.data = @[Unit44] | 77 ballmesh.vertexData.transform.data = @[Unit44] |
| 93 var ball = newThing("ball", ballmesh) | 78 var ball = newEntity("ball", ballmesh) |
| 94 ball.transform = scale3d(ballSize, ballSize, 1'f) * translate3d(10'f, 10'f, 0'f) | 79 ball.transform = scale3d(ballSize, ballSize, 1'f) * translate3d(10'f, 10'f, 0'f) |
| 95 level.add ball | 80 level.add ball |
| 96 | 81 |
| 97 # upload data, prepare shaders, etc | |
| 98 const vertexShader = generateVertexShaderCode[Vertex, Uniforms]() | |
| 99 const fragmentShader = generateFragmentShaderCode[Vertex]() | |
| 100 pipeline = setupPipeline[Vertex, Uniforms, uint16]( | |
| 101 myengine, | |
| 102 level, | |
| 103 vertexShader, | |
| 104 fragmentShader | |
| 105 ) | |
| 106 pipeline.clearColor = backgroundColor | 82 pipeline.clearColor = backgroundColor |
| 107 # show something | 83 # show something |
| 108 myengine.run(pipeline, globalUpdate) | 84 myengine.run(pipeline, globalUpdate) |
| 109 pipeline.trash() | 85 |
| 110 myengine.trash() | 86 myengine.destroy() |
