Mercurial > games > semicongine
annotate 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 |
rev | line source |
---|---|
146 | 1 import std/times |
336
887ddc8d45fd
did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents:
316
diff
changeset
|
2 import std/tables |
146 | 3 |
271 | 4 import ../src/semicongine |
74 | 5 |
146 | 6 let |
210 | 7 barcolor = hexToColorAlpha("5A3F00").gamma(2.2).colorToHex() |
74 | 8 barSize = 0.1'f |
9 barWidth = 0.01'f | |
210 | 10 ballcolor = hexToColorAlpha("B17F08").gamma(2.2).colorToHex() |
74 | 11 ballSize = 0.01'f |
77 | 12 ballSpeed = 60'f |
336
887ddc8d45fd
did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents:
316
diff
changeset
|
13 material = Material(name: "default") |
74 | 14 |
15 var | |
203
84fd522fdf3f
did: update examples to use new API for scene + scene globals
Sam <sam@basx.dev>
parents:
166
diff
changeset
|
16 level: Scene |
145
a4e6e76128e6
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
85
diff
changeset
|
17 ballVelocity = newVec2f(1, 1).normalized * ballSpeed |
74 | 18 |
19 when isMainModule: | |
145
a4e6e76128e6
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
85
diff
changeset
|
20 var myengine = initEngine("Pong") |
74 | 21 |
336
887ddc8d45fd
did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents:
316
diff
changeset
|
22 var player = rect(color=barcolor, width=barWidth, height=barSize) |
887ddc8d45fd
did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents:
316
diff
changeset
|
23 player.material = material |
887ddc8d45fd
did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents:
316
diff
changeset
|
24 var ball = circle(color=ballcolor) |
887ddc8d45fd
did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents:
316
diff
changeset
|
25 ball.material = material |
887ddc8d45fd
did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents:
316
diff
changeset
|
26 level = Scene(name: "scene", meshes: @[ball, player]) |
74 | 27 |
146 | 28 const |
336
887ddc8d45fd
did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents:
316
diff
changeset
|
29 shaderConfiguration = createShaderConfiguration( |
887ddc8d45fd
did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents:
316
diff
changeset
|
30 inputs=[ |
887ddc8d45fd
did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents:
316
diff
changeset
|
31 attr[Vec3f]("position"), |
887ddc8d45fd
did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents:
316
diff
changeset
|
32 attr[Vec4f]("color", memoryPerformanceHint=PreferFastWrite), |
887ddc8d45fd
did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents:
316
diff
changeset
|
33 attr[Mat4]("transform", memoryPerformanceHint=PreferFastWrite, perInstance=true), |
887ddc8d45fd
did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents:
316
diff
changeset
|
34 ], |
887ddc8d45fd
did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents:
316
diff
changeset
|
35 intermediates=[attr[Vec4f]("outcolor")], |
887ddc8d45fd
did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents:
316
diff
changeset
|
36 uniforms=[attr[Mat4]("projection")], |
887ddc8d45fd
did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents:
316
diff
changeset
|
37 outputs=[attr[Vec4f]("color")], |
316
b145a05c2459
add: changing rendering system, not finished yet, also upgrading to Nim 2
Sam <sam@basx.dev>
parents:
302
diff
changeset
|
38 vertexCode="""outcolor = color; gl_Position = vec4(position, 1) * (transform * Uniforms.projection);""", |
b145a05c2459
add: changing rendering system, not finished yet, also upgrading to Nim 2
Sam <sam@basx.dev>
parents:
302
diff
changeset
|
39 fragmentCode="color = outcolor;", |
146 | 40 ) |
41 | |
42 # set up rendering | |
336
887ddc8d45fd
did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents:
316
diff
changeset
|
43 myengine.initRenderer({"default": shaderConfiguration}.toTable) |
203
84fd522fdf3f
did: update examples to use new API for scene + scene globals
Sam <sam@basx.dev>
parents:
166
diff
changeset
|
44 level.addShaderGlobal("projection", Unit4f32) |
336
887ddc8d45fd
did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents:
316
diff
changeset
|
45 myengine.addScene(level) |
145
a4e6e76128e6
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
85
diff
changeset
|
46 |
146 | 47 var |
48 winsize = myengine.getWindow().size | |
49 height = float32(winsize[1]) / float32(winsize[0]) | |
50 width = 1'f | |
51 currentTime = cpuTime() | |
165 | 52 showSystemCursor = true |
166
5b0e27e448cb
add: support for showing/hiding cursur, X11 fullscreen (win32 still missing)
Sam <sam@basx.dev>
parents:
165
diff
changeset
|
53 fullscreen = false |
5b0e27e448cb
add: support for showing/hiding cursur, X11 fullscreen (win32 still missing)
Sam <sam@basx.dev>
parents:
165
diff
changeset
|
54 while myengine.updateInputs() == Running and not myengine.keyIsDown(Escape): |
165 | 55 if myengine.keyWasPressed(C): |
56 if showSystemCursor: | |
57 myengine.hideSystemCursor() | |
58 else: | |
59 myengine.showSystemCursor() | |
60 showSystemCursor = not showSystemCursor | |
166
5b0e27e448cb
add: support for showing/hiding cursur, X11 fullscreen (win32 still missing)
Sam <sam@basx.dev>
parents:
165
diff
changeset
|
61 if myengine.keyWasPressed(F): |
5b0e27e448cb
add: support for showing/hiding cursur, X11 fullscreen (win32 still missing)
Sam <sam@basx.dev>
parents:
165
diff
changeset
|
62 fullscreen = not fullscreen |
5b0e27e448cb
add: support for showing/hiding cursur, X11 fullscreen (win32 still missing)
Sam <sam@basx.dev>
parents:
165
diff
changeset
|
63 myengine.fullscreen(fullscreen) |
165 | 64 |
146 | 65 let dt: float32 = cpuTime() - currentTime |
66 currentTime = cpuTime() | |
67 if myengine.windowWasResized(): | |
68 winsize = myengine.getWindow().size | |
69 height = float32(winsize[1]) / float32(winsize[0]) | |
70 width = 1'f | |
248 | 71 setShaderGlobal(level, "projection", ortho(0, width, 0, height, 0, 1)) |
146 | 72 if myengine.keyIsDown(Down) and (player.transform.col(3).y + barSize/2) < height: |
336
887ddc8d45fd
did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents:
316
diff
changeset
|
73 player.transform = player.transform * translate(0'f, 1'f * dt, 0'f) |
146 | 74 if myengine.keyIsDown(Up) and (player.transform.col(3).y - barSize/2) > 0: |
336
887ddc8d45fd
did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents:
316
diff
changeset
|
75 player.transform = player.transform * translate(0'f, -1'f * dt, 0'f) |
146 | 76 |
77 # bounce level | |
78 if ball.transform.col(3).x + ballSize/2 > width: ballVelocity[0] = -ballVelocity[0] | |
79 if ball.transform.col(3).y - ballSize/2 <= 0: ballVelocity[1] = -ballVelocity[1] | |
80 if ball.transform.col(3).y + ballSize/2 > height: ballVelocity[1] = -ballVelocity[1] | |
81 | |
336
887ddc8d45fd
did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents:
316
diff
changeset
|
82 ball.transform = ball.transform * translate(ballVelocity[0] * dt, ballVelocity[1] * dt, 0'f32) |
146 | 83 |
84 # loose | |
85 if ball.transform.col(3).x - ballSize/2 <= 0: | |
336
887ddc8d45fd
did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents:
316
diff
changeset
|
86 ball.transform = scale(ballSize, ballSize, 1'f) * translate(30'f, 30'f, 0'f) |
166
5b0e27e448cb
add: support for showing/hiding cursur, X11 fullscreen (win32 still missing)
Sam <sam@basx.dev>
parents:
165
diff
changeset
|
87 ballVelocity = newVec2f(1, 1).normalized * ballSpeed |
146 | 88 |
89 # bar | |
90 if ball.transform.col(3).x - ballSize/2 <= barWidth: | |
91 let | |
92 barTop = player.transform.col(3).y - barSize/2 | |
93 barBottom = player.transform.col(3).y + barSize/2 | |
94 ballTop = ball.transform.col(3).y - ballSize/2 | |
95 ballBottom = ball.transform.col(3).y + ballSize/2 | |
96 if ballTop >= barTop and ballBottom <= barBottom: | |
97 ballVelocity[0] = abs(ballVelocity[0]) | |
98 | |
99 myengine.renderScene(level) |