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