annotate examples/E10_pong.nim @ 613:40f5d804eca6

fix: wrong layers activated
author Sam <sam@basx.dev>
date Thu, 27 Apr 2023 20:48:48 +0700
parents 64eb53f81cf6
children 134647ed5b60
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
607
64eb53f81cf6 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 606
diff changeset
1 import std/times
64eb53f81cf6 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 606
diff changeset
2
535
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
3 import semicongine
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
4
607
64eb53f81cf6 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 606
diff changeset
5 let
64eb53f81cf6 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 606
diff changeset
6 barcolor = hexToColor("5A3F00").gamma(2.2).colorToHex()
535
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
7 barSize = 0.1'f
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
8 barWidth = 0.01'f
607
64eb53f81cf6 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 606
diff changeset
9 ballcolor = hexToColor("B17F08").gamma(2.2).colorToHex()
535
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
10 levelRatio = 1
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
11 ballSize = 0.01'f
607
64eb53f81cf6 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 606
diff changeset
12 backgroundColor = hexToColorAlpha("FAC034FF").gamma(2.2)
538
c1f16a095637 add: color functions + gamma correction
Sam <sam@basx.dev>
parents: 535
diff changeset
13 ballSpeed = 60'f
535
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
14
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
15 var
606
f41c1b78cf5b add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 546
diff changeset
16 level: Entity
f41c1b78cf5b add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 546
diff changeset
17 ballVelocity = newVec2f(1, 1).normalized * ballSpeed
535
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
18
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
19 when isMainModule:
606
f41c1b78cf5b add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 546
diff changeset
20 var myengine = initEngine("Pong")
f41c1b78cf5b add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 546
diff changeset
21 level = newEntity("Level")
607
64eb53f81cf6 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 606
diff changeset
22 var playerbarmesh = rect(color=barcolor)
606
f41c1b78cf5b add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 546
diff changeset
23 var playerbar = newEntity("playerbar", playerbarmesh)
f41c1b78cf5b add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 546
diff changeset
24 playerbar.transform = scale3d(barWidth, barSize, 1'f) * translate3d(0.5'f, 0'f, 0'f)
f41c1b78cf5b add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 546
diff changeset
25 var player = newEntity("player", playerbar)
535
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
26 player.transform = translate3d(0'f, 0.3'f, 0'f)
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
27 level.add player
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
28
607
64eb53f81cf6 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 606
diff changeset
29 var ballmesh = circle(color=ballcolor)
606
f41c1b78cf5b add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 546
diff changeset
30 var ball = newEntity("ball", ballmesh)
535
21c276c0a968 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)
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
32 level.add ball
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
33
607
64eb53f81cf6 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 606
diff changeset
34 const
64eb53f81cf6 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 606
diff changeset
35 vertexInput = @[
64eb53f81cf6 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 606
diff changeset
36 attr[Vec3f]("position", memoryLocation=VRAM),
64eb53f81cf6 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 606
diff changeset
37 attr[Vec3f]("color", memoryLocation=VRAMVisible),
64eb53f81cf6 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 606
diff changeset
38 attr[Mat4]("transform", memoryLocation=VRAMVisible, perInstance=true),
64eb53f81cf6 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 606
diff changeset
39 ]
64eb53f81cf6 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 606
diff changeset
40 vertexOutput = @[attr[Vec3f]("outcolor")]
64eb53f81cf6 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 606
diff changeset
41 uniforms = @[attr[Mat4]("projection")]
64eb53f81cf6 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 606
diff changeset
42 fragOutput = @[attr[Vec4f]("color")]
64eb53f81cf6 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 606
diff changeset
43 vertexCode = compileGlslShader(
64eb53f81cf6 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 606
diff changeset
44 stage=VK_SHADER_STAGE_VERTEX_BIT,
64eb53f81cf6 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 606
diff changeset
45 inputs=vertexInput,
64eb53f81cf6 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 606
diff changeset
46 uniforms=uniforms,
64eb53f81cf6 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 606
diff changeset
47 outputs=vertexOutput,
64eb53f81cf6 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 606
diff changeset
48 main="""outcolor = color; gl_Position = vec4(position, 1) * (transform * Uniforms.projection);"""
64eb53f81cf6 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 606
diff changeset
49 )
64eb53f81cf6 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 606
diff changeset
50 fragmentCode = compileGlslShader(
64eb53f81cf6 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 606
diff changeset
51 stage=VK_SHADER_STAGE_FRAGMENT_BIT,
64eb53f81cf6 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 606
diff changeset
52 inputs=vertexOutput,
64eb53f81cf6 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 606
diff changeset
53 uniforms=uniforms,
64eb53f81cf6 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 606
diff changeset
54 outputs=fragOutput,
64eb53f81cf6 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 606
diff changeset
55 main="color = vec4(outcolor, 1);"
64eb53f81cf6 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 606
diff changeset
56 )
64eb53f81cf6 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 606
diff changeset
57
64eb53f81cf6 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 606
diff changeset
58 # set up rendering
64eb53f81cf6 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 606
diff changeset
59 myengine.setRenderer(myengine.gpuDevice.simpleForwardRenderPass(vertexCode, fragmentCode, clearColor=backgroundColor))
64eb53f81cf6 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 606
diff changeset
60 myengine.addScene(level, vertexInput, transformAttribute="transform")
64eb53f81cf6 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 606
diff changeset
61 var projection = initShaderGlobal("projection", Unit4f32)
64eb53f81cf6 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 606
diff changeset
62 level.add projection
606
f41c1b78cf5b add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 546
diff changeset
63
607
64eb53f81cf6 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 606
diff changeset
64 var
64eb53f81cf6 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 606
diff changeset
65 winsize = myengine.getWindow().size
64eb53f81cf6 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 606
diff changeset
66 height = float32(winsize[1]) / float32(winsize[0])
64eb53f81cf6 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 606
diff changeset
67 width = 1'f
64eb53f81cf6 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 606
diff changeset
68 currentTime = cpuTime()
64eb53f81cf6 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 606
diff changeset
69 while myengine.updateInputs() == Running and not myengine.keyWasPressed(Escape):
64eb53f81cf6 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 606
diff changeset
70 let dt: float32 = cpuTime() - currentTime
64eb53f81cf6 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 606
diff changeset
71 currentTime = cpuTime()
64eb53f81cf6 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 606
diff changeset
72 if myengine.windowWasResized():
64eb53f81cf6 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 606
diff changeset
73 winsize = myengine.getWindow().size
64eb53f81cf6 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 606
diff changeset
74 height = float32(winsize[1]) / float32(winsize[0])
64eb53f81cf6 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 606
diff changeset
75 width = 1'f
64eb53f81cf6 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 606
diff changeset
76 setValue[Mat4](projection.value, ortho[float32](0'f, width, 0'f, height, 0'f, 1'f))
64eb53f81cf6 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 606
diff changeset
77 var player = level.firstWithName("player")
64eb53f81cf6 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 606
diff changeset
78 if myengine.keyIsDown(Down) and (player.transform.col(3).y + barSize/2) < height:
64eb53f81cf6 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 606
diff changeset
79 player.transform = player.transform * translate3d(0'f, 1'f * dt, 0'f)
64eb53f81cf6 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 606
diff changeset
80 if myengine.keyIsDown(Up) and (player.transform.col(3).y - barSize/2) > 0:
64eb53f81cf6 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 606
diff changeset
81 player.transform = player.transform * translate3d(0'f, -1'f * dt, 0'f)
64eb53f81cf6 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 606
diff changeset
82
64eb53f81cf6 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 606
diff changeset
83 # bounce level
64eb53f81cf6 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 606
diff changeset
84 if ball.transform.col(3).x + ballSize/2 > width: ballVelocity[0] = -ballVelocity[0]
64eb53f81cf6 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 606
diff changeset
85 if ball.transform.col(3).y - ballSize/2 <= 0: ballVelocity[1] = -ballVelocity[1]
64eb53f81cf6 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 606
diff changeset
86 if ball.transform.col(3).y + ballSize/2 > height: ballVelocity[1] = -ballVelocity[1]
64eb53f81cf6 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 606
diff changeset
87
64eb53f81cf6 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 606
diff changeset
88 ball.transform = ball.transform * translate3d(ballVelocity[0] * dt, ballVelocity[1] * dt, 0'f32)
64eb53f81cf6 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 606
diff changeset
89
64eb53f81cf6 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 606
diff changeset
90 # loose
64eb53f81cf6 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 606
diff changeset
91 if ball.transform.col(3).x - ballSize/2 <= 0:
64eb53f81cf6 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 606
diff changeset
92 break
64eb53f81cf6 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 606
diff changeset
93
64eb53f81cf6 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 606
diff changeset
94 # bar
64eb53f81cf6 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 606
diff changeset
95 if ball.transform.col(3).x - ballSize/2 <= barWidth:
64eb53f81cf6 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 606
diff changeset
96 let
64eb53f81cf6 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 606
diff changeset
97 barTop = player.transform.col(3).y - barSize/2
64eb53f81cf6 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 606
diff changeset
98 barBottom = player.transform.col(3).y + barSize/2
64eb53f81cf6 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 606
diff changeset
99 ballTop = ball.transform.col(3).y - ballSize/2
64eb53f81cf6 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 606
diff changeset
100 ballBottom = ball.transform.col(3).y + ballSize/2
64eb53f81cf6 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 606
diff changeset
101 if ballTop >= barTop and ballBottom <= barBottom:
64eb53f81cf6 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 606
diff changeset
102 ballVelocity[0] = abs(ballVelocity[0])
64eb53f81cf6 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 606
diff changeset
103
64eb53f81cf6 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 606
diff changeset
104 myengine.renderScene(level)