annotate examples/E10_pong.nim @ 1206:77822066a419 compiletime-tests

del: unnecessary files
author sam <sam@basx.dev>
date Tue, 16 Jul 2024 15:17:20 +0700
parents 5934c5615f13
children
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
1140
5934c5615f13 did: update all examples to work with latest refactoring
sam <sam@basx.dev>
parents: 1139
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
1140
5934c5615f13 did: update all examples to work with latest refactoring
sam <sam@basx.dev>
parents: 1139
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
1140
5934c5615f13 did: update all examples to work with latest refactoring
sam <sam@basx.dev>
parents: 1139
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:
1140
5934c5615f13 did: update all examples to work with latest refactoring
sam <sam@basx.dev>
parents: 1139
diff changeset
23 var myengine = InitEngine("Pong")
74
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
24
1140
5934c5615f13 did: update all examples to work with latest refactoring
sam <sam@basx.dev>
parents: 1139
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")
1140
5934c5615f13 did: update all examples to work with latest refactoring
sam <sam@basx.dev>
parents: 1139
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
1140
5934c5615f13 did: update all examples to work with latest refactoring
sam <sam@basx.dev>
parents: 1139
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 = [
1140
5934c5615f13 did: update all examples to work with latest refactoring
sam <sam@basx.dev>
parents: 1139
diff changeset
35 Attr[Vec3f]("position"),
5934c5615f13 did: update all examples to work with latest refactoring
sam <sam@basx.dev>
parents: 1139
diff changeset
36 Attr[Vec4f]("color", memoryPerformanceHint = PreferFastWrite),
5934c5615f13 did: update all examples to work with latest refactoring
sam <sam@basx.dev>
parents: 1139
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 ],
1140
5934c5615f13 did: update all examples to work with latest refactoring
sam <sam@basx.dev>
parents: 1139
diff changeset
39 intermediates = [Attr[Vec4f]("outcolor")],
5934c5615f13 did: update all examples to work with latest refactoring
sam <sam@basx.dev>
parents: 1139
diff changeset
40 uniforms = [Attr[Mat4]("projection")],
5934c5615f13 did: update all examples to work with latest refactoring
sam <sam@basx.dev>
parents: 1139
diff changeset
41 outputs = [Attr[Vec4f]("color")],
1028
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
1140
5934c5615f13 did: update all examples to work with latest refactoring
sam <sam@basx.dev>
parents: 1139
diff changeset
47 myengine.InitRenderer({matDef: shaderConfiguration})
5934c5615f13 did: update all examples to work with latest refactoring
sam <sam@basx.dev>
parents: 1139
diff changeset
48 level.AddShaderGlobal("projection", Unit4f32)
5934c5615f13 did: update all examples to work with latest refactoring
sam <sam@basx.dev>
parents: 1139
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
1140
5934c5615f13 did: update all examples to work with latest refactoring
sam <sam@basx.dev>
parents: 1139
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():
1140
5934c5615f13 did: update all examples to work with latest refactoring
sam <sam@basx.dev>
parents: 1139
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
1140
5934c5615f13 did: update all examples to work with latest refactoring
sam <sam@basx.dev>
parents: 1139
diff changeset
75 SetShaderGlobal(level, "projection", Ortho(0, width, 0, height, 0, 1))
5934c5615f13 did: update all examples to work with latest refactoring
sam <sam@basx.dev>
parents: 1139
diff changeset
76 if KeyIsDown(Down) and (player.transform.Col(3).y + barSize/2) < height:
5934c5615f13 did: update all examples to work with latest refactoring
sam <sam@basx.dev>
parents: 1139
diff changeset
77 player.transform = player.transform * Translate(0'f, 1'f * dt, 0'f)
5934c5615f13 did: update all examples to work with latest refactoring
sam <sam@basx.dev>
parents: 1139
diff changeset
78 if KeyIsDown(Up) and (player.transform.Col(3).y - barSize/2) > 0:
5934c5615f13 did: update all examples to work with latest refactoring
sam <sam@basx.dev>
parents: 1139
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
1140
5934c5615f13 did: update all examples to work with latest refactoring
sam <sam@basx.dev>
parents: 1139
diff changeset
82 if ball.transform.Col(3).x + ballSize/2 > width: ballVelocity[0] = -ballVelocity[0]
5934c5615f13 did: update all examples to work with latest refactoring
sam <sam@basx.dev>
parents: 1139
diff changeset
83 if ball.transform.Col(3).y - ballSize/2 <= 0: ballVelocity[1] = -ballVelocity[1]
5934c5615f13 did: update all examples to work with latest refactoring
sam <sam@basx.dev>
parents: 1139
diff changeset
84 if ball.transform.Col(3).y + ballSize/2 > height: ballVelocity[1] = -ballVelocity[1]
146
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
85
1140
5934c5615f13 did: update all examples to work with latest refactoring
sam <sam@basx.dev>
parents: 1139
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
1140
5934c5615f13 did: update all examples to work with latest refactoring
sam <sam@basx.dev>
parents: 1139
diff changeset
89 if ball.transform.Col(3).x - ballSize/2 <= 0:
5934c5615f13 did: update all examples to work with latest refactoring
sam <sam@basx.dev>
parents: 1139
diff changeset
90 ball.transform = Scale(ballSize, ballSize, 1'f) * Translate(30'f, 30'f, 0'f)
5934c5615f13 did: update all examples to work with latest refactoring
sam <sam@basx.dev>
parents: 1139
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
1140
5934c5615f13 did: update all examples to work with latest refactoring
sam <sam@basx.dev>
parents: 1139
diff changeset
94 if ball.transform.Col(3).x - ballSize/2 <= barWidth:
146
253dd797e719 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 145
diff changeset
95 let
1140
5934c5615f13 did: update all examples to work with latest refactoring
sam <sam@basx.dev>
parents: 1139
diff changeset
96 barTop = player.transform.Col(3).y - barSize/2
5934c5615f13 did: update all examples to work with latest refactoring
sam <sam@basx.dev>
parents: 1139
diff changeset
97 barBottom = player.transform.Col(3).y + barSize/2
5934c5615f13 did: update all examples to work with latest refactoring
sam <sam@basx.dev>
parents: 1139
diff changeset
98 ballTop = ball.transform.Col(3).y - ballSize/2
5934c5615f13 did: update all examples to work with latest refactoring
sam <sam@basx.dev>
parents: 1139
diff changeset
99 ballBottom = ball.transform.Col(3).y + ballSize/2
146
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
1140
5934c5615f13 did: update all examples to work with latest refactoring
sam <sam@basx.dev>
parents: 1139
diff changeset
103 myengine.RenderScene(level)
5934c5615f13 did: update all examples to work with latest refactoring
sam <sam@basx.dev>
parents: 1139
diff changeset
104 myengine.Destroy()