annotate examples/E10_pong.nim @ 561:4a9fc283c3af

add: initial version of better shader-shit
author Sam <sam@basx.dev>
date Fri, 17 Mar 2023 01:11:18 +0700
parents db4e583b40c3
children a4e6e76128e6
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
535
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
1 import semicongine
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
2
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
3 type
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
4 Vertex = object
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
5 position: PositionAttribute[Vec2]
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
6 color: ColorAttribute[Vec3]
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
7 transform: ModelTransformAttribute
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
8 Uniforms = object
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
9 view: ViewProjectionTransform
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
10
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
11 const
538
c1f16a095637 add: color functions + gamma correction
Sam <sam@basx.dev>
parents: 535
diff changeset
12 barcolor = RGBfromHex("5A3F00").gamma(2.2)
535
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
13 barSize = 0.1'f
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
14 barWidth = 0.01'f
538
c1f16a095637 add: color functions + gamma correction
Sam <sam@basx.dev>
parents: 535
diff changeset
15 ballcolor = RGBfromHex("B17F08").gamma(2.2)
535
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
16 levelRatio = 1
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
17 ballSize = 0.01'f
538
c1f16a095637 add: color functions + gamma correction
Sam <sam@basx.dev>
parents: 535
diff changeset
18 backgroundColor = RGBAfromHex("FAC034").gamma(2.2)
c1f16a095637 add: color functions + gamma correction
Sam <sam@basx.dev>
parents: 535
diff changeset
19 ballSpeed = 60'f
535
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
20
546
db4e583b40c3 did: a few fixes for new API
Sam <sam@basx.dev>
parents: 538
diff changeset
21 echo RGBAfromHex("FAC034")
db4e583b40c3 did: a few fixes for new API
Sam <sam@basx.dev>
parents: 538
diff changeset
22 echo RGBAfromHex("FAC034").gamma(2.2)
535
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
23 var
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
24 uniforms = Uniforms()
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
25 pipeline: RenderPipeline[Vertex, Uniforms]
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
26 level: Thing
538
c1f16a095637 add: color functions + gamma correction
Sam <sam@basx.dev>
parents: 535
diff changeset
27 ballVelocity = Vec2([1'f, 1'f]).normalized * ballSpeed
535
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
28
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
29 proc globalUpdate(engine: var Engine; t, dt: float32) =
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
30 var height = float32(engine.vulkan.frameSize.y) / float32(
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
31 engine.vulkan.frameSize.x)
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
32 var width = 1'f
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
33 uniforms.view.value = ortho[float32](
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
34 0'f, width,
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
35 0'f, height,
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
36 0'f, 1'f,
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
37 )
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
38 engine.vulkan.device.updateUniformData(pipeline, uniforms)
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
39 var player = level.firstWithName("player")
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
40 if Down in engine.input.keysDown and (player.transform.col(3).y + barSize/2) < height:
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
41 player.transform = player.transform * translate3d(0'f, 1'f * dt, 0'f)
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
42 if Up in engine.input.keysDown and (player.transform.col(3).y - barSize/2) > 0:
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
43 player.transform = player.transform * translate3d(0'f, -1'f * dt, 0'f)
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
44
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
45 var ball = level.firstWithName("ball")
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
46 ball.transform = ball.transform * translate3d(ballVelocity[0] * dt,
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
47 ballVelocity[1] * dt, 0'f)
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
48
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
49 # loose
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
50 if ball.transform.col(3).x - ballSize/2 <= 0:
538
c1f16a095637 add: color functions + gamma correction
Sam <sam@basx.dev>
parents: 535
diff changeset
51 ballVelocity = Vec2([1'f, 1'f]).normalized * ballSpeed
c1f16a095637 add: color functions + gamma correction
Sam <sam@basx.dev>
parents: 535
diff changeset
52 ball.transform[0, 3] = width / 2
c1f16a095637 add: color functions + gamma correction
Sam <sam@basx.dev>
parents: 535
diff changeset
53 ball.transform[1, 3] = height / 2
535
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
54
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
55 # bounce level
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
56 if ball.transform.col(3).x + ballSize/2 > width: ballVelocity[
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
57 0] = -ballVelocity[0]
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
58 if ball.transform.col(3).y - ballSize/2 <= 0: ballVelocity[1] = -ballVelocity[1]
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
59 if ball.transform.col(3).y + ballSize/2 > height: ballVelocity[
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
60 1] = -ballVelocity[1]
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
61
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
62 # bar
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
63 if ball.transform.col(3).x - ballSize/2 <= barWidth:
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
64 let
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
65 barTop = player.transform.col(3).y - barSize/2
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
66 barBottom = player.transform.col(3).y + barSize/2
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
67 ballTop = ball.transform.col(3).y - ballSize/2
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
68 ballBottom = ball.transform.col(3).y + ballSize/2
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
69 if ballTop >= barTop and ballBottom <= barBottom:
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
70 ballVelocity[0] = abs(ballVelocity[0])
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
71
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
72
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
73 when isMainModule:
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
74 var myengine = igniteEngine("Pong")
538
c1f16a095637 add: color functions + gamma correction
Sam <sam@basx.dev>
parents: 535
diff changeset
75 myengine.maxFPS = 61
535
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
76 level = newThing("Level")
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
77 var playerbarmesh = quad[Vertex, Vec2, float32]()
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
78 playerbarmesh.vertexData.color.data = @[barcolor, barcolor, barcolor, barcolor]
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
79 playerbarmesh.vertexData.transform.data = @[Unit44]
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
80 var playerbar = newThing("playerbar", playerbarmesh)
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
81 playerbar.transform = scale3d(barWidth, barSize, 1'f) * translate3d(0.5'f,
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
82 0'f, 0'f)
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
83 var player = newThing("player", playerbar)
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
84 player.transform = translate3d(0'f, 0.3'f, 0'f)
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
85 level.add player
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
86
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
87 var ballmesh = circle[Vertex, Vec2, float32]()
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
88 ballmesh.vertexData.color.data = newSeq[Vec3](
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
89 ballmesh.vertexData.position.data.len)
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
90 for i in 0 ..< ballmesh.vertexData.color.data.len:
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
91 ballmesh.vertexData.color.data[i] = ballcolor
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
92 ballmesh.vertexData.transform.data = @[Unit44]
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
93 var ball = newThing("ball", ballmesh)
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
94 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
95 level.add ball
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
96
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
97 # upload data, prepare shaders, etc
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
98 const vertexShader = generateVertexShaderCode[Vertex, Uniforms]()
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
99 const fragmentShader = generateFragmentShaderCode[Vertex]()
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
100 pipeline = setupPipeline[Vertex, Uniforms, uint16](
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
101 myengine,
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
102 level,
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
103 vertexShader,
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
104 fragmentShader
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
105 )
538
c1f16a095637 add: color functions + gamma correction
Sam <sam@basx.dev>
parents: 535
diff changeset
106 pipeline.clearColor = backgroundColor
535
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
107 # show something
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
108 myengine.run(pipeline, globalUpdate)
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
109 pipeline.trash()
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
110 myengine.trash()