Mercurial > games > semicongine
annotate examples/E10_pong.nim @ 1028:2ba3f18e7cad
fix: pong example was not working yet
author | sam <sam@basx.dev> |
---|---|
date | Wed, 22 May 2024 03:52:20 +0700 |
parents | 887ddc8d45fd |
children | 71315636ba82 |
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 |
1028 | 4 import ../semicongine |
74 | 5 |
146 | 6 let |
1028 | 7 barcolor = toRGBA("5A3F00").toSRGB().colorToHex() |
74 | 8 barSize = 0.1'f |
9 barWidth = 0.01'f | |
1028 | 10 ballcolor = toRGBA("B17F08").toSRGB().colorToHex() |
74 | 11 ballSize = 0.01'f |
77 | 12 ballSpeed = 60'f |
1028 | 13 matDef = MaterialType(name: "default", vertexAttributes: { |
14 "position": Vec3F32, | |
15 "color": Vec4F32, | |
16 }.toTable) | |
74 | 17 |
18 var | |
203
84fd522fdf3f
did: update examples to use new API for scene + scene globals
Sam <sam@basx.dev>
parents:
166
diff
changeset
|
19 level: Scene |
145
a4e6e76128e6
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
85
diff
changeset
|
20 ballVelocity = newVec2f(1, 1).normalized * ballSpeed |
74 | 21 |
22 when isMainModule: | |
145
a4e6e76128e6
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
85
diff
changeset
|
23 var myengine = initEngine("Pong") |
74 | 24 |
1028 | 25 var player = rect(color = barcolor, width = barWidth, height = barSize) |
26 player.material = matDef.initMaterialData(name = "player material") | |
27 var ball = circle(color = ballcolor) | |
28 ball.material = matDef.initMaterialData(name = "player material") | |
336
887ddc8d45fd
did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents:
316
diff
changeset
|
29 level = Scene(name: "scene", meshes: @[ball, player]) |
74 | 30 |
146 | 31 const |
336
887ddc8d45fd
did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents:
316
diff
changeset
|
32 shaderConfiguration = createShaderConfiguration( |
1028 | 33 name = "default shader", |
34 inputs = [ | |
336
887ddc8d45fd
did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents:
316
diff
changeset
|
35 attr[Vec3f]("position"), |
1028 | 36 attr[Vec4f]("color", memoryPerformanceHint = PreferFastWrite), |
37 attr[Mat4]("transform", memoryPerformanceHint = PreferFastWrite, perInstance = true), | |
336
887ddc8d45fd
did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents:
316
diff
changeset
|
38 ], |
1028 | 39 intermediates = [attr[Vec4f]("outcolor")], |
40 uniforms = [attr[Mat4]("projection")], | |
41 outputs = [attr[Vec4f]("color")], | |
42 vertexCode = """outcolor = color; gl_Position = vec4(position, 1) * (transform * Uniforms.projection);""", | |
43 fragmentCode = "color = outcolor;", | |
146 | 44 ) |
45 | |
46 # set up rendering | |
1028 | 47 myengine.initRenderer({matDef: shaderConfiguration}) |
203
84fd522fdf3f
did: update examples to use new API for scene + scene globals
Sam <sam@basx.dev>
parents:
166
diff
changeset
|
48 level.addShaderGlobal("projection", Unit4f32) |
1028 | 49 myengine.loadScene(level) |
145
a4e6e76128e6
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
85
diff
changeset
|
50 |
146 | 51 var |
1028 | 52 winsize = myengine.GetWindow().size |
146 | 53 height = float32(winsize[1]) / float32(winsize[0]) |
54 width = 1'f | |
55 currentTime = cpuTime() | |
165 | 56 showSystemCursor = true |
166
5b0e27e448cb
add: support for showing/hiding cursur, X11 fullscreen (win32 still missing)
Sam <sam@basx.dev>
parents:
165
diff
changeset
|
57 fullscreen = false |
1028 | 58 while myengine.UpdateInputs() and not KeyIsDown(Escape): |
59 if KeyWasPressed(C): | |
165 | 60 if showSystemCursor: |
1028 | 61 myengine.HideSystemCursor() |
165 | 62 else: |
1028 | 63 myengine.ShowSystemCursor() |
165 | 64 showSystemCursor = not showSystemCursor |
1028 | 65 if KeyWasPressed(F): |
166
5b0e27e448cb
add: support for showing/hiding cursur, X11 fullscreen (win32 still missing)
Sam <sam@basx.dev>
parents:
165
diff
changeset
|
66 fullscreen = not fullscreen |
1028 | 67 myengine.Fullscreen = fullscreen |
165 | 68 |
146 | 69 let dt: float32 = cpuTime() - currentTime |
70 currentTime = cpuTime() | |
1028 | 71 if WindowWasResized(): |
72 winsize = myengine.GetWindow().size | |
146 | 73 height = float32(winsize[1]) / float32(winsize[0]) |
74 width = 1'f | |
248 | 75 setShaderGlobal(level, "projection", ortho(0, width, 0, height, 0, 1)) |
1028 | 76 if 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
|
77 player.transform = player.transform * translate(0'f, 1'f * dt, 0'f) |
1028 | 78 if 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
|
79 player.transform = player.transform * translate(0'f, -1'f * dt, 0'f) |
146 | 80 |
81 # bounce level | |
82 if ball.transform.col(3).x + ballSize/2 > width: ballVelocity[0] = -ballVelocity[0] | |
83 if ball.transform.col(3).y - ballSize/2 <= 0: ballVelocity[1] = -ballVelocity[1] | |
84 if ball.transform.col(3).y + ballSize/2 > height: ballVelocity[1] = -ballVelocity[1] | |
85 | |
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 = ball.transform * translate(ballVelocity[0] * dt, ballVelocity[1] * dt, 0'f32) |
146 | 87 |
88 # loose | |
89 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
|
90 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
|
91 ballVelocity = newVec2f(1, 1).normalized * ballSpeed |
146 | 92 |
93 # bar | |
94 if ball.transform.col(3).x - ballSize/2 <= barWidth: | |
95 let | |
96 barTop = player.transform.col(3).y - barSize/2 | |
97 barBottom = player.transform.col(3).y + barSize/2 | |
98 ballTop = ball.transform.col(3).y - ballSize/2 | |
99 ballBottom = ball.transform.col(3).y + ballSize/2 | |
100 if ballTop >= barTop and ballBottom <= barBottom: | |
101 ballVelocity[0] = abs(ballVelocity[0]) | |
102 | |
103 myengine.renderScene(level) |