comparison examples/E10_pong.nim @ 203:84fd522fdf3f

did: update examples to use new API for scene + scene globals
author Sam <sam@basx.dev>
date Mon, 08 May 2023 21:21:49 +0700
parents 5b0e27e448cb
children f3912838cd69
comparison
equal deleted inserted replaced
202:9bb3fdbecc52 203:84fd522fdf3f
5 let 5 let
6 barcolor = hexToColor("5A3F00").gamma(2.2).colorToHex() 6 barcolor = hexToColor("5A3F00").gamma(2.2).colorToHex()
7 barSize = 0.1'f 7 barSize = 0.1'f
8 barWidth = 0.01'f 8 barWidth = 0.01'f
9 ballcolor = hexToColor("B17F08").gamma(2.2).colorToHex() 9 ballcolor = hexToColor("B17F08").gamma(2.2).colorToHex()
10 levelRatio = 1
11 ballSize = 0.01'f 10 ballSize = 0.01'f
12 backgroundColor = hexToColorAlpha("FAC034FF").gamma(2.2) 11 backgroundColor = hexToColorAlpha("FAC034FF").gamma(2.2)
13 ballSpeed = 60'f 12 ballSpeed = 60'f
14 13
15 var 14 var
16 level: Entity 15 level: Scene
17 ballVelocity = newVec2f(1, 1).normalized * ballSpeed 16 ballVelocity = newVec2f(1, 1).normalized * ballSpeed
18 17
19 when isMainModule: 18 when isMainModule:
20 var myengine = initEngine("Pong") 19 var myengine = initEngine("Pong")
21 level = newEntity("Level") 20 level = newScene("scene", newEntity("Level"))
22 var playerbarmesh = rect(color=barcolor) 21 var playerbarmesh = rect(color=barcolor)
23 var playerbar = newEntity("playerbar", playerbarmesh) 22 var playerbar = newEntity("playerbar", playerbarmesh)
24 playerbar.transform = scale3d(barWidth, barSize, 1'f) * translate3d(0.5'f, 0'f, 0'f) 23 playerbar.transform = scale3d(barWidth, barSize, 1'f) * translate3d(0.5'f, 0'f, 0'f)
25 var player = newEntity("player", playerbar) 24 var player = newEntity("player", playerbar)
26 player.transform = translate3d(0'f, 0.3'f, 0'f) 25 player.transform = translate3d(0'f, 0.3'f, 0'f)
27 level.add player 26 level.root.add player
28 27
29 var ballmesh = circle(color=ballcolor) 28 var ballmesh = circle(color=ballcolor)
30 var ball = newEntity("ball", ballmesh) 29 var ball = newEntity("ball", ballmesh)
31 ball.transform = scale3d(ballSize, ballSize, 1'f) * translate3d(10'f, 10'f, 0'f) 30 ball.transform = scale3d(ballSize, ballSize, 1'f) * translate3d(10'f, 10'f, 0'f)
32 level.add ball 31 level.root.add ball
33 32
34 const 33 const
35 vertexInput = @[ 34 vertexInput = @[
36 attr[Vec3f]("position"), 35 attr[Vec3f]("position"),
37 attr[Vec3f]("color", memoryPerformanceHint=PreferFastWrite), 36 attr[Vec3f]("color", memoryPerformanceHint=PreferFastWrite),
56 ) 55 )
57 56
58 # set up rendering 57 # set up rendering
59 myengine.setRenderer(myengine.gpuDevice.simpleForwardRenderPass(vertexCode, fragmentCode, clearColor=backgroundColor)) 58 myengine.setRenderer(myengine.gpuDevice.simpleForwardRenderPass(vertexCode, fragmentCode, clearColor=backgroundColor))
60 myengine.addScene(level, vertexInput, transformAttribute="transform") 59 myengine.addScene(level, vertexInput, transformAttribute="transform")
61 var projection = initShaderGlobal("projection", Unit4f32) 60 level.addShaderGlobal("projection", Unit4f32)
62 level.add projection
63 61
64 var 62 var
65 winsize = myengine.getWindow().size 63 winsize = myengine.getWindow().size
66 height = float32(winsize[1]) / float32(winsize[0]) 64 height = float32(winsize[1]) / float32(winsize[0])
67 width = 1'f 65 width = 1'f
83 currentTime = cpuTime() 81 currentTime = cpuTime()
84 if myengine.windowWasResized(): 82 if myengine.windowWasResized():
85 winsize = myengine.getWindow().size 83 winsize = myengine.getWindow().size
86 height = float32(winsize[1]) / float32(winsize[0]) 84 height = float32(winsize[1]) / float32(winsize[0])
87 width = 1'f 85 width = 1'f
88 setValue[Mat4](projection.value, ortho[float32](0'f, width, 0'f, height, 0'f, 1'f)) 86 setShaderGlobal(level, "projection", ortho[float32](0'f, width, 0'f, height, 0'f, 1'f))
89 var player = level.firstWithName("player") 87 var player = level.root.firstWithName("player")
90 if myengine.keyIsDown(Down) and (player.transform.col(3).y + barSize/2) < height: 88 if myengine.keyIsDown(Down) and (player.transform.col(3).y + barSize/2) < height:
91 player.transform = player.transform * translate3d(0'f, 1'f * dt, 0'f) 89 player.transform = player.transform * translate3d(0'f, 1'f * dt, 0'f)
92 if myengine.keyIsDown(Up) and (player.transform.col(3).y - barSize/2) > 0: 90 if myengine.keyIsDown(Up) and (player.transform.col(3).y - barSize/2) > 0:
93 player.transform = player.transform * translate3d(0'f, -1'f * dt, 0'f) 91 player.transform = player.transform * translate3d(0'f, -1'f * dt, 0'f)
94 92