Mercurial > games > semicongine
annotate examples/E10_pong.nim @ 784:fa39e67dded7
add: first complete working version of multiple materials and shaders per scene, yipie :)
author | Sam <sam@basx.dev> |
---|---|
date | Sat, 19 Aug 2023 22:24:06 +0700 |
parents | 754835bf175e |
children | 887ddc8d45fd |
rev | line source |
---|---|
607 | 1 import std/times |
2 | |
732 | 3 import ../src/semicongine |
535 | 4 |
607 | 5 let |
671 | 6 barcolor = hexToColorAlpha("5A3F00").gamma(2.2).colorToHex() |
535 | 7 barSize = 0.1'f |
8 barWidth = 0.01'f | |
671 | 9 ballcolor = hexToColorAlpha("B17F08").gamma(2.2).colorToHex() |
535 | 10 ballSize = 0.01'f |
607 | 11 backgroundColor = hexToColorAlpha("FAC034FF").gamma(2.2) |
538 | 12 ballSpeed = 60'f |
535 | 13 |
14 var | |
664
c33c8e156e3e
did: update examples to use new API for scene + scene globals
Sam <sam@basx.dev>
parents:
627
diff
changeset
|
15 level: Scene |
606
f41c1b78cf5b
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
546
diff
changeset
|
16 ballVelocity = newVec2f(1, 1).normalized * ballSpeed |
535 | 17 |
18 when isMainModule: | |
606
f41c1b78cf5b
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
546
diff
changeset
|
19 var myengine = initEngine("Pong") |
664
c33c8e156e3e
did: update examples to use new API for scene + scene globals
Sam <sam@basx.dev>
parents:
627
diff
changeset
|
20 level = newScene("scene", newEntity("Level")) |
607 | 21 var playerbarmesh = rect(color=barcolor) |
763 | 22 var playerbar = newEntity("playerbar", {"mesh": Component(playerbarmesh)}) |
625
c48ceb622b27
fix: buffer update with staging buffer not correctly working
Sam <sam@basx.dev>
parents:
624
diff
changeset
|
23 playerbar.transform = scale3d(barWidth, barSize, 1'f) * translate3d(0.5'f, 0'f, 0'f) |
763 | 24 var player = newEntity("player", [], playerbar) |
535 | 25 player.transform = translate3d(0'f, 0.3'f, 0'f) |
664
c33c8e156e3e
did: update examples to use new API for scene + scene globals
Sam <sam@basx.dev>
parents:
627
diff
changeset
|
26 level.root.add player |
535 | 27 |
607 | 28 var ballmesh = circle(color=ballcolor) |
763 | 29 var ball = newEntity("ball", {"mesh": Component(ballmesh)}) |
535 | 30 ball.transform = scale3d(ballSize, ballSize, 1'f) * translate3d(10'f, 10'f, 0'f) |
664
c33c8e156e3e
did: update examples to use new API for scene + scene globals
Sam <sam@basx.dev>
parents:
627
diff
changeset
|
31 level.root.add ball |
535 | 32 |
607 | 33 const |
777
754835bf175e
add: changing rendering system, not finished yet, also upgrading to Nim 2
Sam <sam@basx.dev>
parents:
763
diff
changeset
|
34 inputs = @[ |
617 | 35 attr[Vec3f]("position"), |
671 | 36 attr[Vec4f]("color", memoryPerformanceHint=PreferFastWrite), |
617 | 37 attr[Mat4]("transform", memoryPerformanceHint=PreferFastWrite, perInstance=true), |
607 | 38 ] |
777
754835bf175e
add: changing rendering system, not finished yet, also upgrading to Nim 2
Sam <sam@basx.dev>
parents:
763
diff
changeset
|
39 intermediate = @[attr[Vec4f]("outcolor")] |
607 | 40 uniforms = @[attr[Mat4]("projection")] |
777
754835bf175e
add: changing rendering system, not finished yet, also upgrading to Nim 2
Sam <sam@basx.dev>
parents:
763
diff
changeset
|
41 outputs = @[attr[Vec4f]("color")] |
754835bf175e
add: changing rendering system, not finished yet, also upgrading to Nim 2
Sam <sam@basx.dev>
parents:
763
diff
changeset
|
42 (vertexCode, fragmentCode) = compileVertexFragmentShaderSet( |
754835bf175e
add: changing rendering system, not finished yet, also upgrading to Nim 2
Sam <sam@basx.dev>
parents:
763
diff
changeset
|
43 inputs=inputs, |
754835bf175e
add: changing rendering system, not finished yet, also upgrading to Nim 2
Sam <sam@basx.dev>
parents:
763
diff
changeset
|
44 intermediate=intermediate, |
754835bf175e
add: changing rendering system, not finished yet, also upgrading to Nim 2
Sam <sam@basx.dev>
parents:
763
diff
changeset
|
45 outputs=outputs, |
607 | 46 uniforms=uniforms, |
777
754835bf175e
add: changing rendering system, not finished yet, also upgrading to Nim 2
Sam <sam@basx.dev>
parents:
763
diff
changeset
|
47 vertexCode="""outcolor = color; gl_Position = vec4(position, 1) * (transform * Uniforms.projection);""", |
754835bf175e
add: changing rendering system, not finished yet, also upgrading to Nim 2
Sam <sam@basx.dev>
parents:
763
diff
changeset
|
48 fragmentCode="color = outcolor;", |
607 | 49 ) |
50 | |
51 # set up rendering | |
52 myengine.setRenderer(myengine.gpuDevice.simpleForwardRenderPass(vertexCode, fragmentCode, clearColor=backgroundColor)) | |
777
754835bf175e
add: changing rendering system, not finished yet, also upgrading to Nim 2
Sam <sam@basx.dev>
parents:
763
diff
changeset
|
53 myengine.addScene(level, inputs, @[], transformAttribute="transform") |
664
c33c8e156e3e
did: update examples to use new API for scene + scene globals
Sam <sam@basx.dev>
parents:
627
diff
changeset
|
54 level.addShaderGlobal("projection", Unit4f32) |
606
f41c1b78cf5b
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
546
diff
changeset
|
55 |
607 | 56 var |
57 winsize = myengine.getWindow().size | |
58 height = float32(winsize[1]) / float32(winsize[0]) | |
59 width = 1'f | |
60 currentTime = cpuTime() | |
626 | 61 showSystemCursor = true |
627
34e536f7001f
add: support for showing/hiding cursur, X11 fullscreen (win32 still missing)
Sam <sam@basx.dev>
parents:
626
diff
changeset
|
62 fullscreen = false |
34e536f7001f
add: support for showing/hiding cursur, X11 fullscreen (win32 still missing)
Sam <sam@basx.dev>
parents:
626
diff
changeset
|
63 while myengine.updateInputs() == Running and not myengine.keyIsDown(Escape): |
626 | 64 if myengine.keyWasPressed(C): |
65 if showSystemCursor: | |
66 myengine.hideSystemCursor() | |
67 else: | |
68 myengine.showSystemCursor() | |
69 showSystemCursor = not showSystemCursor | |
627
34e536f7001f
add: support for showing/hiding cursur, X11 fullscreen (win32 still missing)
Sam <sam@basx.dev>
parents:
626
diff
changeset
|
70 if myengine.keyWasPressed(F): |
34e536f7001f
add: support for showing/hiding cursur, X11 fullscreen (win32 still missing)
Sam <sam@basx.dev>
parents:
626
diff
changeset
|
71 fullscreen = not fullscreen |
34e536f7001f
add: support for showing/hiding cursur, X11 fullscreen (win32 still missing)
Sam <sam@basx.dev>
parents:
626
diff
changeset
|
72 myengine.fullscreen(fullscreen) |
626 | 73 |
607 | 74 let dt: float32 = cpuTime() - currentTime |
75 currentTime = cpuTime() | |
76 if myengine.windowWasResized(): | |
77 winsize = myengine.getWindow().size | |
78 height = float32(winsize[1]) / float32(winsize[0]) | |
79 width = 1'f | |
709 | 80 setShaderGlobal(level, "projection", ortho(0, width, 0, height, 0, 1)) |
664
c33c8e156e3e
did: update examples to use new API for scene + scene globals
Sam <sam@basx.dev>
parents:
627
diff
changeset
|
81 var player = level.root.firstWithName("player") |
607 | 82 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) | |
84 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) | |
86 | |
87 # bounce level | |
88 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] | |
90 if ball.transform.col(3).y + ballSize/2 > height: ballVelocity[1] = -ballVelocity[1] | |
91 | |
92 ball.transform = ball.transform * translate3d(ballVelocity[0] * dt, ballVelocity[1] * dt, 0'f32) | |
93 | |
94 # loose | |
95 if ball.transform.col(3).x - ballSize/2 <= 0: | |
627
34e536f7001f
add: support for showing/hiding cursur, X11 fullscreen (win32 still missing)
Sam <sam@basx.dev>
parents:
626
diff
changeset
|
96 ball.transform = scale3d(ballSize, ballSize, 1'f) * translate3d(30'f, 30'f, 0'f) |
34e536f7001f
add: support for showing/hiding cursur, X11 fullscreen (win32 still missing)
Sam <sam@basx.dev>
parents:
626
diff
changeset
|
97 ballVelocity = newVec2f(1, 1).normalized * ballSpeed |
607 | 98 |
99 # bar | |
100 if ball.transform.col(3).x - ballSize/2 <= barWidth: | |
101 let | |
102 barTop = player.transform.col(3).y - barSize/2 | |
103 barBottom = player.transform.col(3).y + barSize/2 | |
104 ballTop = ball.transform.col(3).y - ballSize/2 | |
105 ballBottom = ball.transform.col(3).y + ballSize/2 | |
106 if ballTop >= barTop and ballBottom <= barBottom: | |
107 ballVelocity[0] = abs(ballVelocity[0]) | |
108 | |
109 myengine.renderScene(level) |