Mercurial > games > semicongine
annotate examples/E10_pong.nim @ 661:9995702c34b2
add: few improvments for working with textures
| author | Sam <sam@basx.dev> |
|---|---|
| date | Sun, 07 May 2023 18:13:39 +0700 |
| parents | 34e536f7001f |
| children | 84fd522fdf3f |
| rev | line source |
|---|---|
| 607 | 1 import std/times |
| 2 | |
| 535 | 3 import semicongine |
| 4 | |
| 607 | 5 let |
| 6 barcolor = hexToColor("5A3F00").gamma(2.2).colorToHex() | |
| 535 | 7 barSize = 0.1'f |
| 8 barWidth = 0.01'f | |
| 607 | 9 ballcolor = hexToColor("B17F08").gamma(2.2).colorToHex() |
| 535 | 10 levelRatio = 1 |
| 11 ballSize = 0.01'f | |
| 607 | 12 backgroundColor = hexToColorAlpha("FAC034FF").gamma(2.2) |
| 538 | 13 ballSpeed = 60'f |
| 535 | 14 |
| 15 var | |
|
606
f41c1b78cf5b
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
546
diff
changeset
|
16 level: Entity |
|
f41c1b78cf5b
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
546
diff
changeset
|
17 ballVelocity = newVec2f(1, 1).normalized * ballSpeed |
| 535 | 18 |
| 19 when isMainModule: | |
|
606
f41c1b78cf5b
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
546
diff
changeset
|
20 var myengine = initEngine("Pong") |
|
f41c1b78cf5b
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
546
diff
changeset
|
21 level = newEntity("Level") |
| 607 | 22 var playerbarmesh = rect(color=barcolor) |
|
606
f41c1b78cf5b
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
546
diff
changeset
|
23 var playerbar = newEntity("playerbar", playerbarmesh) |
|
625
c48ceb622b27
fix: buffer update with staging buffer not correctly working
Sam <sam@basx.dev>
parents:
624
diff
changeset
|
24 playerbar.transform = scale3d(barWidth, barSize, 1'f) * translate3d(0.5'f, 0'f, 0'f) |
|
606
f41c1b78cf5b
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
546
diff
changeset
|
25 var player = newEntity("player", playerbar) |
| 535 | 26 player.transform = translate3d(0'f, 0.3'f, 0'f) |
| 27 level.add player | |
| 28 | |
| 607 | 29 var ballmesh = circle(color=ballcolor) |
|
606
f41c1b78cf5b
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
546
diff
changeset
|
30 var ball = newEntity("ball", ballmesh) |
| 535 | 31 ball.transform = scale3d(ballSize, ballSize, 1'f) * translate3d(10'f, 10'f, 0'f) |
| 32 level.add ball | |
| 33 | |
| 607 | 34 const |
| 35 vertexInput = @[ | |
| 617 | 36 attr[Vec3f]("position"), |
| 37 attr[Vec3f]("color", memoryPerformanceHint=PreferFastWrite), | |
| 38 attr[Mat4]("transform", memoryPerformanceHint=PreferFastWrite, perInstance=true), | |
| 607 | 39 ] |
| 40 vertexOutput = @[attr[Vec3f]("outcolor")] | |
| 41 uniforms = @[attr[Mat4]("projection")] | |
| 42 fragOutput = @[attr[Vec4f]("color")] | |
| 43 vertexCode = compileGlslShader( | |
| 44 stage=VK_SHADER_STAGE_VERTEX_BIT, | |
| 45 inputs=vertexInput, | |
| 46 uniforms=uniforms, | |
| 47 outputs=vertexOutput, | |
| 48 main="""outcolor = color; gl_Position = vec4(position, 1) * (transform * Uniforms.projection);""" | |
| 49 ) | |
| 50 fragmentCode = compileGlslShader( | |
| 51 stage=VK_SHADER_STAGE_FRAGMENT_BIT, | |
| 52 inputs=vertexOutput, | |
| 53 uniforms=uniforms, | |
| 54 outputs=fragOutput, | |
| 55 main="color = vec4(outcolor, 1);" | |
| 56 ) | |
| 57 | |
| 58 # set up rendering | |
| 59 myengine.setRenderer(myengine.gpuDevice.simpleForwardRenderPass(vertexCode, fragmentCode, clearColor=backgroundColor)) | |
| 60 myengine.addScene(level, vertexInput, transformAttribute="transform") | |
| 61 var projection = initShaderGlobal("projection", Unit4f32) | |
| 62 level.add projection | |
|
606
f41c1b78cf5b
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
546
diff
changeset
|
63 |
| 607 | 64 var |
| 65 winsize = myengine.getWindow().size | |
| 66 height = float32(winsize[1]) / float32(winsize[0]) | |
| 67 width = 1'f | |
| 68 currentTime = cpuTime() | |
| 626 | 69 showSystemCursor = true |
|
627
34e536f7001f
add: support for showing/hiding cursur, X11 fullscreen (win32 still missing)
Sam <sam@basx.dev>
parents:
626
diff
changeset
|
70 fullscreen = false |
|
34e536f7001f
add: support for showing/hiding cursur, X11 fullscreen (win32 still missing)
Sam <sam@basx.dev>
parents:
626
diff
changeset
|
71 while myengine.updateInputs() == Running and not myengine.keyIsDown(Escape): |
| 626 | 72 if myengine.keyWasPressed(C): |
| 73 if showSystemCursor: | |
| 74 myengine.hideSystemCursor() | |
| 75 else: | |
| 76 myengine.showSystemCursor() | |
| 77 showSystemCursor = not showSystemCursor | |
|
627
34e536f7001f
add: support for showing/hiding cursur, X11 fullscreen (win32 still missing)
Sam <sam@basx.dev>
parents:
626
diff
changeset
|
78 if myengine.keyWasPressed(F): |
|
34e536f7001f
add: support for showing/hiding cursur, X11 fullscreen (win32 still missing)
Sam <sam@basx.dev>
parents:
626
diff
changeset
|
79 fullscreen = not fullscreen |
|
34e536f7001f
add: support for showing/hiding cursur, X11 fullscreen (win32 still missing)
Sam <sam@basx.dev>
parents:
626
diff
changeset
|
80 myengine.fullscreen(fullscreen) |
| 626 | 81 |
| 607 | 82 let dt: float32 = cpuTime() - currentTime |
| 83 currentTime = cpuTime() | |
| 84 if myengine.windowWasResized(): | |
| 85 winsize = myengine.getWindow().size | |
| 86 height = float32(winsize[1]) / float32(winsize[0]) | |
| 87 width = 1'f | |
| 88 setValue[Mat4](projection.value, ortho[float32](0'f, width, 0'f, height, 0'f, 1'f)) | |
| 89 var player = level.firstWithName("player") | |
| 90 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) | |
| 92 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) | |
| 94 | |
| 95 # bounce level | |
| 96 if ball.transform.col(3).x + ballSize/2 > width: ballVelocity[0] = -ballVelocity[0] | |
| 97 if ball.transform.col(3).y - ballSize/2 <= 0: ballVelocity[1] = -ballVelocity[1] | |
| 98 if ball.transform.col(3).y + ballSize/2 > height: ballVelocity[1] = -ballVelocity[1] | |
| 99 | |
| 100 ball.transform = ball.transform * translate3d(ballVelocity[0] * dt, ballVelocity[1] * dt, 0'f32) | |
| 101 | |
| 102 # loose | |
| 103 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
|
104 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
|
105 ballVelocity = newVec2f(1, 1).normalized * ballSpeed |
| 607 | 106 |
| 107 # bar | |
| 108 if ball.transform.col(3).x - ballSize/2 <= barWidth: | |
| 109 let | |
| 110 barTop = player.transform.col(3).y - barSize/2 | |
| 111 barBottom = player.transform.col(3).y + barSize/2 | |
| 112 ballTop = ball.transform.col(3).y - ballSize/2 | |
| 113 ballBottom = ball.transform.col(3).y + ballSize/2 | |
| 114 if ballTop >= barTop and ballBottom <= barBottom: | |
| 115 ballVelocity[0] = abs(ballVelocity[0]) | |
| 116 | |
| 117 myengine.renderScene(level) |
