annotate examples/E10_pong.nim @ 330:04531bec3583

did: remove some stuff from the heap, maybe nicer?
author Sam <sam@basx.dev>
date Sat, 02 Sep 2023 23:51:02 +0700
parents b145a05c2459
children 887ddc8d45fd
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
146
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
1 import std/times
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
2
271
95281f2db400 did: fix API changes broke examples
Sam <sam@basx.dev>
parents: 253
diff changeset
3 import ../src/semicongine
74
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
4
146
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
5 let
210
f3912838cd69 fix: always use rgba
Sam <sam@basx.dev>
parents: 203
diff changeset
6 barcolor = hexToColorAlpha("5A3F00").gamma(2.2).colorToHex()
74
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
7 barSize = 0.1'f
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
8 barWidth = 0.01'f
210
f3912838cd69 fix: always use rgba
Sam <sam@basx.dev>
parents: 203
diff changeset
9 ballcolor = hexToColorAlpha("B17F08").gamma(2.2).colorToHex()
74
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
10 ballSize = 0.01'f
146
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
11 backgroundColor = hexToColorAlpha("FAC034FF").gamma(2.2)
77
4b16d2af316a add: color functions + gamma correction
Sam <sam@basx.dev>
parents: 74
diff changeset
12 ballSpeed = 60'f
74
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
13
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
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
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
17
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
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
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
21 var playerbarmesh = rect(color=barcolor)
302
da0bd61abe91 did: change API for ECS
Sam <sam@basx.dev>
parents: 271
diff changeset
22 var playerbar = newEntity("playerbar", {"mesh": Component(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)
302
da0bd61abe91 did: change API for ECS
Sam <sam@basx.dev>
parents: 271
diff changeset
24 var player = newEntity("player", [], playerbar)
74
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
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
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
27
146
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
28 var ballmesh = circle(color=ballcolor)
302
da0bd61abe91 did: change API for ECS
Sam <sam@basx.dev>
parents: 271
diff changeset
29 var ball = newEntity("ball", {"mesh": Component(ballmesh)})
74
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
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
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
32
146
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
33 const
316
b145a05c2459 add: changing rendering system, not finished yet, also upgrading to Nim 2
Sam <sam@basx.dev>
parents: 302
diff changeset
34 inputs = @[
156
134647ed5b60 did: refactor memory selection
Sam <sam@basx.dev>
parents: 146
diff changeset
35 attr[Vec3f]("position"),
210
f3912838cd69 fix: always use rgba
Sam <sam@basx.dev>
parents: 203
diff changeset
36 attr[Vec4f]("color", memoryPerformanceHint=PreferFastWrite),
156
134647ed5b60 did: refactor memory selection
Sam <sam@basx.dev>
parents: 146
diff changeset
37 attr[Mat4]("transform", memoryPerformanceHint=PreferFastWrite, perInstance=true),
146
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
38 ]
316
b145a05c2459 add: changing rendering system, not finished yet, also upgrading to Nim 2
Sam <sam@basx.dev>
parents: 302
diff changeset
39 intermediate = @[attr[Vec4f]("outcolor")]
146
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
40 uniforms = @[attr[Mat4]("projection")]
316
b145a05c2459 add: changing rendering system, not finished yet, also upgrading to Nim 2
Sam <sam@basx.dev>
parents: 302
diff changeset
41 outputs = @[attr[Vec4f]("color")]
b145a05c2459 add: changing rendering system, not finished yet, also upgrading to Nim 2
Sam <sam@basx.dev>
parents: 302
diff changeset
42 (vertexCode, fragmentCode) = compileVertexFragmentShaderSet(
b145a05c2459 add: changing rendering system, not finished yet, also upgrading to Nim 2
Sam <sam@basx.dev>
parents: 302
diff changeset
43 inputs=inputs,
b145a05c2459 add: changing rendering system, not finished yet, also upgrading to Nim 2
Sam <sam@basx.dev>
parents: 302
diff changeset
44 intermediate=intermediate,
b145a05c2459 add: changing rendering system, not finished yet, also upgrading to Nim 2
Sam <sam@basx.dev>
parents: 302
diff changeset
45 outputs=outputs,
146
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
46 uniforms=uniforms,
316
b145a05c2459 add: changing rendering system, not finished yet, also upgrading to Nim 2
Sam <sam@basx.dev>
parents: 302
diff changeset
47 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
48 fragmentCode="color = outcolor;",
146
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
49 )
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
50
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
51 # set up rendering
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
52 myengine.setRenderer(myengine.gpuDevice.simpleForwardRenderPass(vertexCode, fragmentCode, clearColor=backgroundColor))
316
b145a05c2459 add: changing rendering system, not finished yet, also upgrading to Nim 2
Sam <sam@basx.dev>
parents: 302
diff changeset
53 myengine.addScene(level, inputs, @[], transformAttribute="transform")
203
84fd522fdf3f did: update examples to use new API for scene + scene globals
Sam <sam@basx.dev>
parents: 166
diff changeset
54 level.addShaderGlobal("projection", Unit4f32)
145
a4e6e76128e6 add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 85
diff changeset
55
146
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
56 var
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
57 winsize = myengine.getWindow().size
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
58 height = float32(winsize[1]) / float32(winsize[0])
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
59 width = 1'f
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
60 currentTime = cpuTime()
165
0644308904da add: option to show/hide cursor
Sam <sam@basx.dev>
parents: 164
diff changeset
61 showSystemCursor = true
166
5b0e27e448cb add: support for showing/hiding cursur, X11 fullscreen (win32 still missing)
Sam <sam@basx.dev>
parents: 165
diff changeset
62 fullscreen = false
5b0e27e448cb add: support for showing/hiding cursur, X11 fullscreen (win32 still missing)
Sam <sam@basx.dev>
parents: 165
diff changeset
63 while myengine.updateInputs() == Running and not myengine.keyIsDown(Escape):
165
0644308904da add: option to show/hide cursor
Sam <sam@basx.dev>
parents: 164
diff changeset
64 if myengine.keyWasPressed(C):
0644308904da add: option to show/hide cursor
Sam <sam@basx.dev>
parents: 164
diff changeset
65 if showSystemCursor:
0644308904da add: option to show/hide cursor
Sam <sam@basx.dev>
parents: 164
diff changeset
66 myengine.hideSystemCursor()
0644308904da add: option to show/hide cursor
Sam <sam@basx.dev>
parents: 164
diff changeset
67 else:
0644308904da add: option to show/hide cursor
Sam <sam@basx.dev>
parents: 164
diff changeset
68 myengine.showSystemCursor()
0644308904da add: option to show/hide cursor
Sam <sam@basx.dev>
parents: 164
diff changeset
69 showSystemCursor = not showSystemCursor
166
5b0e27e448cb add: support for showing/hiding cursur, X11 fullscreen (win32 still missing)
Sam <sam@basx.dev>
parents: 165
diff changeset
70 if myengine.keyWasPressed(F):
5b0e27e448cb add: support for showing/hiding cursur, X11 fullscreen (win32 still missing)
Sam <sam@basx.dev>
parents: 165
diff changeset
71 fullscreen = not fullscreen
5b0e27e448cb add: support for showing/hiding cursur, X11 fullscreen (win32 still missing)
Sam <sam@basx.dev>
parents: 165
diff changeset
72 myengine.fullscreen(fullscreen)
165
0644308904da add: option to show/hide cursor
Sam <sam@basx.dev>
parents: 164
diff changeset
73
146
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
74 let dt: float32 = cpuTime() - currentTime
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
75 currentTime = cpuTime()
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
76 if myengine.windowWasResized():
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
77 winsize = myengine.getWindow().size
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
78 height = float32(winsize[1]) / float32(winsize[0])
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
79 width = 1'f
248
952428f04ffc did: few fixes
Sam <sam@basx.dev>
parents: 210
diff changeset
80 setShaderGlobal(level, "projection", ortho(0, width, 0, height, 0, 1))
203
84fd522fdf3f did: update examples to use new API for scene + scene globals
Sam <sam@basx.dev>
parents: 166
diff changeset
81 var player = level.root.firstWithName("player")
146
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
82 if myengine.keyIsDown(Down) and (player.transform.col(3).y + barSize/2) < height:
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
83 player.transform = player.transform * translate3d(0'f, 1'f * dt, 0'f)
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
84 if myengine.keyIsDown(Up) and (player.transform.col(3).y - barSize/2) > 0:
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
85 player.transform = player.transform * translate3d(0'f, -1'f * dt, 0'f)
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
86
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
87 # bounce level
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
88 if ball.transform.col(3).x + ballSize/2 > width: ballVelocity[0] = -ballVelocity[0]
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
89 if ball.transform.col(3).y - ballSize/2 <= 0: ballVelocity[1] = -ballVelocity[1]
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
90 if ball.transform.col(3).y + ballSize/2 > height: ballVelocity[1] = -ballVelocity[1]
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
91
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
92 ball.transform = ball.transform * translate3d(ballVelocity[0] * dt, ballVelocity[1] * dt, 0'f32)
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
93
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
94 # loose
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
95 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
96 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
97 ballVelocity = newVec2f(1, 1).normalized * ballSpeed
146
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
98
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
99 # bar
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
100 if ball.transform.col(3).x - ballSize/2 <= barWidth:
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
101 let
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
102 barTop = player.transform.col(3).y - barSize/2
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
103 barBottom = player.transform.col(3).y + barSize/2
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
104 ballTop = ball.transform.col(3).y - ballSize/2
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
105 ballBottom = ball.transform.col(3).y + ballSize/2
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
106 if ballTop >= barTop and ballBottom <= barBottom:
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
107 ballVelocity[0] = abs(ballVelocity[0])
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
108
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
109 myengine.renderScene(level)