annotate examples/E10_pong.nim @ 165:0644308904da

add: option to show/hide cursor
author Sam <sam@basx.dev>
date Sat, 29 Apr 2023 21:38:52 +0700
parents 7a0ca5c01095
children 5b0e27e448cb
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
74
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
3 import semicongine
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
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
6 barcolor = hexToColor("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
146
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
9 ballcolor = hexToColor("B17F08").gamma(2.2).colorToHex()
74
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
10 levelRatio = 1
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
11 ballSize = 0.01'f
146
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
12 backgroundColor = hexToColorAlpha("FAC034FF").gamma(2.2)
77
4b16d2af316a add: color functions + gamma correction
Sam <sam@basx.dev>
parents: 74
diff changeset
13 ballSpeed = 60'f
74
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
14
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
15 var
145
a4e6e76128e6 add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 85
diff changeset
16 level: Entity
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
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
18
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
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")
a4e6e76128e6 add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 85
diff changeset
21 level = newEntity("Level")
146
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
22 var playerbarmesh = rect(color=barcolor)
145
a4e6e76128e6 add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 85
diff changeset
23 var playerbar = newEntity("playerbar", playerbarmesh)
164
7a0ca5c01095 fix: buffer update with staging buffer not correctly working
Sam <sam@basx.dev>
parents: 163
diff changeset
24 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
25 var player = newEntity("player", playerbar)
74
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
26 player.transform = translate3d(0'f, 0.3'f, 0'f)
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
27 level.add player
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
28
146
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
29 var ballmesh = circle(color=ballcolor)
145
a4e6e76128e6 add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 85
diff changeset
30 var ball = newEntity("ball", ballmesh)
74
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
31 ball.transform = scale3d(ballSize, ballSize, 1'f) * translate3d(10'f, 10'f, 0'f)
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
32 level.add ball
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
33
146
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
34 const
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
35 vertexInput = @[
156
134647ed5b60 did: refactor memory selection
Sam <sam@basx.dev>
parents: 146
diff changeset
36 attr[Vec3f]("position"),
134647ed5b60 did: refactor memory selection
Sam <sam@basx.dev>
parents: 146
diff changeset
37 attr[Vec3f]("color", memoryPerformanceHint=PreferFastWrite),
134647ed5b60 did: refactor memory selection
Sam <sam@basx.dev>
parents: 146
diff changeset
38 attr[Mat4]("transform", memoryPerformanceHint=PreferFastWrite, perInstance=true),
146
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
39 ]
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
40 vertexOutput = @[attr[Vec3f]("outcolor")]
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
41 uniforms = @[attr[Mat4]("projection")]
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
42 fragOutput = @[attr[Vec4f]("color")]
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
43 vertexCode = compileGlslShader(
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
44 stage=VK_SHADER_STAGE_VERTEX_BIT,
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
45 inputs=vertexInput,
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
46 uniforms=uniforms,
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
47 outputs=vertexOutput,
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
48 main="""outcolor = color; gl_Position = vec4(position, 1) * (transform * Uniforms.projection);"""
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 fragmentCode = compileGlslShader(
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
51 stage=VK_SHADER_STAGE_FRAGMENT_BIT,
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
52 inputs=vertexOutput,
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
53 uniforms=uniforms,
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
54 outputs=fragOutput,
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
55 main="color = vec4(outcolor, 1);"
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
56 )
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
57
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
58 # set up rendering
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
59 myengine.setRenderer(myengine.gpuDevice.simpleForwardRenderPass(vertexCode, fragmentCode, clearColor=backgroundColor))
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
60 myengine.addScene(level, vertexInput, transformAttribute="transform")
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
61 var projection = initShaderGlobal("projection", Unit4f32)
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
62 level.add projection
145
a4e6e76128e6 add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 85
diff changeset
63
146
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
64 var
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
65 winsize = myengine.getWindow().size
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
66 height = float32(winsize[1]) / float32(winsize[0])
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
67 width = 1'f
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
68 currentTime = cpuTime()
165
0644308904da add: option to show/hide cursor
Sam <sam@basx.dev>
parents: 164
diff changeset
69 showSystemCursor = true
146
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
70 while myengine.updateInputs() == Running and not myengine.keyWasPressed(Escape):
165
0644308904da add: option to show/hide cursor
Sam <sam@basx.dev>
parents: 164
diff changeset
71 if myengine.keyWasPressed(C):
0644308904da add: option to show/hide cursor
Sam <sam@basx.dev>
parents: 164
diff changeset
72 if showSystemCursor:
0644308904da add: option to show/hide cursor
Sam <sam@basx.dev>
parents: 164
diff changeset
73 myengine.hideSystemCursor()
0644308904da add: option to show/hide cursor
Sam <sam@basx.dev>
parents: 164
diff changeset
74 else:
0644308904da add: option to show/hide cursor
Sam <sam@basx.dev>
parents: 164
diff changeset
75 myengine.showSystemCursor()
0644308904da add: option to show/hide cursor
Sam <sam@basx.dev>
parents: 164
diff changeset
76 showSystemCursor = not showSystemCursor
0644308904da add: option to show/hide cursor
Sam <sam@basx.dev>
parents: 164
diff changeset
77
146
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
78 let dt: float32 = cpuTime() - currentTime
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
79 currentTime = cpuTime()
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
80 if myengine.windowWasResized():
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
81 winsize = myengine.getWindow().size
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
82 height = float32(winsize[1]) / float32(winsize[0])
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
83 width = 1'f
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
84 setValue[Mat4](projection.value, ortho[float32](0'f, width, 0'f, height, 0'f, 1'f))
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
85 var player = level.firstWithName("player")
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
86 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
87 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
88 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
89 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
90
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
91 # bounce level
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
92 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
93 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
94 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
95
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
96 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
97
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
98 # loose
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
99 if ball.transform.col(3).x - ballSize/2 <= 0:
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
100 break
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
101
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
102 # bar
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
103 if ball.transform.col(3).x - ballSize/2 <= barWidth:
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
104 let
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
105 barTop = player.transform.col(3).y - barSize/2
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
106 barBottom = player.transform.col(3).y + barSize/2
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
107 ballTop = ball.transform.col(3).y - ballSize/2
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
108 ballBottom = ball.transform.col(3).y + ballSize/2
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
109 if ballTop >= barTop and ballBottom <= barBottom:
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
110 ballVelocity[0] = abs(ballVelocity[0])
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
111
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
112 myengine.renderScene(level)