Mercurial > games > semicongine
comparison examples/E10_pong.nim @ 336:887ddc8d45fd
did: update examples to work with improved scenegraph/material api, notice removed complexity!
| author | Sam <sam@basx.dev> |
|---|---|
| date | Tue, 05 Sep 2023 00:28:35 +0700 |
| parents | b145a05c2459 |
| children | 2ba3f18e7cad |
comparison
equal
deleted
inserted
replaced
| 335:f05b4bef44d1 | 336:887ddc8d45fd |
|---|---|
| 1 import std/times | 1 import std/times |
| 2 import std/tables | |
| 2 | 3 |
| 3 import ../src/semicongine | 4 import ../src/semicongine |
| 4 | 5 |
| 5 let | 6 let |
| 6 barcolor = hexToColorAlpha("5A3F00").gamma(2.2).colorToHex() | 7 barcolor = hexToColorAlpha("5A3F00").gamma(2.2).colorToHex() |
| 7 barSize = 0.1'f | 8 barSize = 0.1'f |
| 8 barWidth = 0.01'f | 9 barWidth = 0.01'f |
| 9 ballcolor = hexToColorAlpha("B17F08").gamma(2.2).colorToHex() | 10 ballcolor = hexToColorAlpha("B17F08").gamma(2.2).colorToHex() |
| 10 ballSize = 0.01'f | 11 ballSize = 0.01'f |
| 11 backgroundColor = hexToColorAlpha("FAC034FF").gamma(2.2) | |
| 12 ballSpeed = 60'f | 12 ballSpeed = 60'f |
| 13 material = Material(name: "default") | |
| 13 | 14 |
| 14 var | 15 var |
| 15 level: Scene | 16 level: Scene |
| 16 ballVelocity = newVec2f(1, 1).normalized * ballSpeed | 17 ballVelocity = newVec2f(1, 1).normalized * ballSpeed |
| 17 | 18 |
| 18 when isMainModule: | 19 when isMainModule: |
| 19 var myengine = initEngine("Pong") | 20 var myengine = initEngine("Pong") |
| 20 level = newScene("scene", newEntity("Level")) | |
| 21 var playerbarmesh = rect(color=barcolor) | |
| 22 var playerbar = newEntity("playerbar", {"mesh": Component(playerbarmesh)}) | |
| 23 playerbar.transform = scale3d(barWidth, barSize, 1'f) * translate3d(0.5'f, 0'f, 0'f) | |
| 24 var player = newEntity("player", [], playerbar) | |
| 25 player.transform = translate3d(0'f, 0.3'f, 0'f) | |
| 26 level.root.add player | |
| 27 | 21 |
| 28 var ballmesh = circle(color=ballcolor) | 22 var player = rect(color=barcolor, width=barWidth, height=barSize) |
| 29 var ball = newEntity("ball", {"mesh": Component(ballmesh)}) | 23 player.material = material |
| 30 ball.transform = scale3d(ballSize, ballSize, 1'f) * translate3d(10'f, 10'f, 0'f) | 24 var ball = circle(color=ballcolor) |
| 31 level.root.add ball | 25 ball.material = material |
| 26 level = Scene(name: "scene", meshes: @[ball, player]) | |
| 32 | 27 |
| 33 const | 28 const |
| 34 inputs = @[ | 29 shaderConfiguration = createShaderConfiguration( |
| 35 attr[Vec3f]("position"), | 30 inputs=[ |
| 36 attr[Vec4f]("color", memoryPerformanceHint=PreferFastWrite), | 31 attr[Vec3f]("position"), |
| 37 attr[Mat4]("transform", memoryPerformanceHint=PreferFastWrite, perInstance=true), | 32 attr[Vec4f]("color", memoryPerformanceHint=PreferFastWrite), |
| 38 ] | 33 attr[Mat4]("transform", memoryPerformanceHint=PreferFastWrite, perInstance=true), |
| 39 intermediate = @[attr[Vec4f]("outcolor")] | 34 ], |
| 40 uniforms = @[attr[Mat4]("projection")] | 35 intermediates=[attr[Vec4f]("outcolor")], |
| 41 outputs = @[attr[Vec4f]("color")] | 36 uniforms=[attr[Mat4]("projection")], |
| 42 (vertexCode, fragmentCode) = compileVertexFragmentShaderSet( | 37 outputs=[attr[Vec4f]("color")], |
| 43 inputs=inputs, | |
| 44 intermediate=intermediate, | |
| 45 outputs=outputs, | |
| 46 uniforms=uniforms, | |
| 47 vertexCode="""outcolor = color; gl_Position = vec4(position, 1) * (transform * Uniforms.projection);""", | 38 vertexCode="""outcolor = color; gl_Position = vec4(position, 1) * (transform * Uniforms.projection);""", |
| 48 fragmentCode="color = outcolor;", | 39 fragmentCode="color = outcolor;", |
| 49 ) | 40 ) |
| 50 | 41 |
| 51 # set up rendering | 42 # set up rendering |
| 52 myengine.setRenderer(myengine.gpuDevice.simpleForwardRenderPass(vertexCode, fragmentCode, clearColor=backgroundColor)) | 43 myengine.initRenderer({"default": shaderConfiguration}.toTable) |
| 53 myengine.addScene(level, inputs, @[], transformAttribute="transform") | |
| 54 level.addShaderGlobal("projection", Unit4f32) | 44 level.addShaderGlobal("projection", Unit4f32) |
| 45 myengine.addScene(level) | |
| 55 | 46 |
| 56 var | 47 var |
| 57 winsize = myengine.getWindow().size | 48 winsize = myengine.getWindow().size |
| 58 height = float32(winsize[1]) / float32(winsize[0]) | 49 height = float32(winsize[1]) / float32(winsize[0]) |
| 59 width = 1'f | 50 width = 1'f |
| 76 if myengine.windowWasResized(): | 67 if myengine.windowWasResized(): |
| 77 winsize = myengine.getWindow().size | 68 winsize = myengine.getWindow().size |
| 78 height = float32(winsize[1]) / float32(winsize[0]) | 69 height = float32(winsize[1]) / float32(winsize[0]) |
| 79 width = 1'f | 70 width = 1'f |
| 80 setShaderGlobal(level, "projection", ortho(0, width, 0, height, 0, 1)) | 71 setShaderGlobal(level, "projection", ortho(0, width, 0, height, 0, 1)) |
| 81 var player = level.root.firstWithName("player") | |
| 82 if myengine.keyIsDown(Down) and (player.transform.col(3).y + barSize/2) < height: | 72 if myengine.keyIsDown(Down) and (player.transform.col(3).y + barSize/2) < height: |
| 83 player.transform = player.transform * translate3d(0'f, 1'f * dt, 0'f) | 73 player.transform = player.transform * translate(0'f, 1'f * dt, 0'f) |
| 84 if myengine.keyIsDown(Up) and (player.transform.col(3).y - barSize/2) > 0: | 74 if myengine.keyIsDown(Up) and (player.transform.col(3).y - barSize/2) > 0: |
| 85 player.transform = player.transform * translate3d(0'f, -1'f * dt, 0'f) | 75 player.transform = player.transform * translate(0'f, -1'f * dt, 0'f) |
| 86 | 76 |
| 87 # bounce level | 77 # bounce level |
| 88 if ball.transform.col(3).x + ballSize/2 > width: ballVelocity[0] = -ballVelocity[0] | 78 if ball.transform.col(3).x + ballSize/2 > width: ballVelocity[0] = -ballVelocity[0] |
| 89 if ball.transform.col(3).y - ballSize/2 <= 0: ballVelocity[1] = -ballVelocity[1] | 79 if ball.transform.col(3).y - ballSize/2 <= 0: ballVelocity[1] = -ballVelocity[1] |
| 90 if ball.transform.col(3).y + ballSize/2 > height: ballVelocity[1] = -ballVelocity[1] | 80 if ball.transform.col(3).y + ballSize/2 > height: ballVelocity[1] = -ballVelocity[1] |
| 91 | 81 |
| 92 ball.transform = ball.transform * translate3d(ballVelocity[0] * dt, ballVelocity[1] * dt, 0'f32) | 82 ball.transform = ball.transform * translate(ballVelocity[0] * dt, ballVelocity[1] * dt, 0'f32) |
| 93 | 83 |
| 94 # loose | 84 # loose |
| 95 if ball.transform.col(3).x - ballSize/2 <= 0: | 85 if ball.transform.col(3).x - ballSize/2 <= 0: |
| 96 ball.transform = scale3d(ballSize, ballSize, 1'f) * translate3d(30'f, 30'f, 0'f) | 86 ball.transform = scale(ballSize, ballSize, 1'f) * translate(30'f, 30'f, 0'f) |
| 97 ballVelocity = newVec2f(1, 1).normalized * ballSpeed | 87 ballVelocity = newVec2f(1, 1).normalized * ballSpeed |
| 98 | 88 |
| 99 # bar | 89 # bar |
| 100 if ball.transform.col(3).x - ballSize/2 <= barWidth: | 90 if ball.transform.col(3).x - ballSize/2 <= barWidth: |
| 101 let | 91 let |
