annotate examples/E10_pong.nim @ 1139:114f395b9144

did: finish refactoring and updated all tests accordingly
author sam <sam@basx.dev>
date Sat, 08 Jun 2024 14:58:25 +0700
parents 71315636ba82
children 5934c5615f13
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
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
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
3
1028
2ba3f18e7cad fix: pong example was not working yet
sam <sam@basx.dev>
parents: 336
diff changeset
4 import ../semicongine
74
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
5
146
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
6 let
1028
2ba3f18e7cad fix: pong example was not working yet
sam <sam@basx.dev>
parents: 336
diff changeset
7 barcolor = toRGBA("5A3F00").toSRGB().colorToHex()
74
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
8 barSize = 0.1'f
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
9 barWidth = 0.01'f
1028
2ba3f18e7cad fix: pong example was not working yet
sam <sam@basx.dev>
parents: 336
diff changeset
10 ballcolor = toRGBA("B17F08").toSRGB().colorToHex()
74
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
11 ballSize = 0.01'f
77
4b16d2af316a add: color functions + gamma correction
Sam <sam@basx.dev>
parents: 74
diff changeset
12 ballSpeed = 60'f
1028
2ba3f18e7cad fix: pong example was not working yet
sam <sam@basx.dev>
parents: 336
diff changeset
13 matDef = MaterialType(name: "default", vertexAttributes: {
2ba3f18e7cad fix: pong example was not working yet
sam <sam@basx.dev>
parents: 336
diff changeset
14 "position": Vec3F32,
2ba3f18e7cad fix: pong example was not working yet
sam <sam@basx.dev>
parents: 336
diff changeset
15 "color": Vec4F32,
2ba3f18e7cad fix: pong example was not working yet
sam <sam@basx.dev>
parents: 336
diff changeset
16 }.toTable)
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 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
1136
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 1028
diff changeset
20 ballVelocity = NewVec2f(1, 1).normalized * ballSpeed
74
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
21
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
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
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
24
1028
2ba3f18e7cad fix: pong example was not working yet
sam <sam@basx.dev>
parents: 336
diff changeset
25 var player = rect(color = barcolor, width = barWidth, height = barSize)
1139
114f395b9144 did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents: 1136
diff changeset
26 player.material = matDef.InitMaterialData(name = "player material")
1028
2ba3f18e7cad fix: pong example was not working yet
sam <sam@basx.dev>
parents: 336
diff changeset
27 var ball = circle(color = ballcolor)
1139
114f395b9144 did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents: 1136
diff changeset
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
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
30
146
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
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
2ba3f18e7cad fix: pong example was not working yet
sam <sam@basx.dev>
parents: 336
diff changeset
33 name = "default shader",
2ba3f18e7cad fix: pong example was not working yet
sam <sam@basx.dev>
parents: 336
diff changeset
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
2ba3f18e7cad fix: pong example was not working yet
sam <sam@basx.dev>
parents: 336
diff changeset
36 attr[Vec4f]("color", memoryPerformanceHint = PreferFastWrite),
2ba3f18e7cad fix: pong example was not working yet
sam <sam@basx.dev>
parents: 336
diff changeset
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
2ba3f18e7cad fix: pong example was not working yet
sam <sam@basx.dev>
parents: 336
diff changeset
39 intermediates = [attr[Vec4f]("outcolor")],
2ba3f18e7cad fix: pong example was not working yet
sam <sam@basx.dev>
parents: 336
diff changeset
40 uniforms = [attr[Mat4]("projection")],
2ba3f18e7cad fix: pong example was not working yet
sam <sam@basx.dev>
parents: 336
diff changeset
41 outputs = [attr[Vec4f]("color")],
2ba3f18e7cad fix: pong example was not working yet
sam <sam@basx.dev>
parents: 336
diff changeset
42 vertexCode = """outcolor = color; gl_Position = vec4(position, 1) * (transform * Uniforms.projection);""",
2ba3f18e7cad fix: pong example was not working yet
sam <sam@basx.dev>
parents: 336
diff changeset
43 fragmentCode = "color = outcolor;",
146
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
44 )
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
45
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
46 # set up rendering
1028
2ba3f18e7cad fix: pong example was not working yet
sam <sam@basx.dev>
parents: 336
diff changeset
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
2ba3f18e7cad fix: pong example was not working yet
sam <sam@basx.dev>
parents: 336
diff changeset
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
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
51 var
1028
2ba3f18e7cad fix: pong example was not working yet
sam <sam@basx.dev>
parents: 336
diff changeset
52 winsize = myengine.GetWindow().size
146
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
53 height = float32(winsize[1]) / float32(winsize[0])
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
54 width = 1'f
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
55 currentTime = cpuTime()
165
0644308904da add: option to show/hide cursor
Sam <sam@basx.dev>
parents: 164
diff changeset
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
2ba3f18e7cad fix: pong example was not working yet
sam <sam@basx.dev>
parents: 336
diff changeset
58 while myengine.UpdateInputs() and not KeyIsDown(Escape):
2ba3f18e7cad fix: pong example was not working yet
sam <sam@basx.dev>
parents: 336
diff changeset
59 if KeyWasPressed(C):
165
0644308904da add: option to show/hide cursor
Sam <sam@basx.dev>
parents: 164
diff changeset
60 if showSystemCursor:
1028
2ba3f18e7cad fix: pong example was not working yet
sam <sam@basx.dev>
parents: 336
diff changeset
61 myengine.HideSystemCursor()
165
0644308904da add: option to show/hide cursor
Sam <sam@basx.dev>
parents: 164
diff changeset
62 else:
1028
2ba3f18e7cad fix: pong example was not working yet
sam <sam@basx.dev>
parents: 336
diff changeset
63 myengine.ShowSystemCursor()
165
0644308904da add: option to show/hide cursor
Sam <sam@basx.dev>
parents: 164
diff changeset
64 showSystemCursor = not showSystemCursor
1028
2ba3f18e7cad fix: pong example was not working yet
sam <sam@basx.dev>
parents: 336
diff changeset
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
2ba3f18e7cad fix: pong example was not working yet
sam <sam@basx.dev>
parents: 336
diff changeset
67 myengine.Fullscreen = fullscreen
165
0644308904da add: option to show/hide cursor
Sam <sam@basx.dev>
parents: 164
diff changeset
68
146
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
69 let dt: float32 = cpuTime() - currentTime
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
70 currentTime = cpuTime()
1028
2ba3f18e7cad fix: pong example was not working yet
sam <sam@basx.dev>
parents: 336
diff changeset
71 if WindowWasResized():
2ba3f18e7cad fix: pong example was not working yet
sam <sam@basx.dev>
parents: 336
diff changeset
72 winsize = myengine.GetWindow().size
146
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
73 height = float32(winsize[1]) / float32(winsize[0])
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
74 width = 1'f
248
952428f04ffc did: few fixes
Sam <sam@basx.dev>
parents: 210
diff changeset
75 setShaderGlobal(level, "projection", ortho(0, width, 0, height, 0, 1))
1028
2ba3f18e7cad fix: pong example was not working yet
sam <sam@basx.dev>
parents: 336
diff changeset
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
2ba3f18e7cad fix: pong example was not working yet
sam <sam@basx.dev>
parents: 336
diff changeset
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
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
80
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
81 # bounce level
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
82 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
83 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
84 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
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
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
87
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
88 # loose
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
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)
1136
71315636ba82 did: refactor naming in tons of places
sam <sam@basx.dev>
parents: 1028
diff changeset
91 ballVelocity = NewVec2f(1, 1).normalized * ballSpeed
146
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
92
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
93 # bar
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
94 if ball.transform.col(3).x - ballSize/2 <= barWidth:
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
95 let
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
96 barTop = player.transform.col(3).y - barSize/2
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
97 barBottom = player.transform.col(3).y + barSize/2
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
98 ballTop = ball.transform.col(3).y - ballSize/2
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
99 ballBottom = ball.transform.col(3).y + ballSize/2
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
100 if ballTop >= barTop and ballBottom <= barBottom:
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
101 ballVelocity[0] = abs(ballVelocity[0])
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
102
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
103 myengine.renderScene(level)