Mercurial > games > semicongine
annotate examples/E10_pong.nim @ 1164:7b4d4d85d9f5 compiletime-tests
add: descriptor sets allocation
author | sam <sam@basx.dev> |
---|---|
date | Sat, 22 Jun 2024 02:26:16 +0700 |
parents | 5934c5615f13 |
children |
rev | line source |
---|---|
146 | 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 | 3 |
1028 | 4 import ../semicongine |
74 | 5 |
146 | 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 | 8 barSize = 0.1'f |
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 | 11 ballSize = 0.01'f |
77 | 12 ballSpeed = 60'f |
1028 | 13 matDef = MaterialType(name: "default", vertexAttributes: { |
14 "position": Vec3F32, | |
15 "color": Vec4F32, | |
16 }.toTable) | |
74 | 17 |
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 | 21 |
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 | 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 | 30 |
146 | 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 | 33 name = "default shader", |
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 | 42 vertexCode = """outcolor = color; gl_Position = vec4(position, 1) * (transform * Uniforms.projection);""", |
43 fragmentCode = "color = outcolor;", | |
146 | 44 ) |
45 | |
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 | 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 | 53 height = float32(winsize[1]) / float32(winsize[0]) |
54 width = 1'f | |
55 currentTime = cpuTime() | |
165 | 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 | 58 while myengine.UpdateInputs() and not KeyIsDown(Escape): |
59 if KeyWasPressed(C): | |
165 | 60 if showSystemCursor: |
1028 | 61 myengine.HideSystemCursor() |
165 | 62 else: |
1028 | 63 myengine.ShowSystemCursor() |
165 | 64 showSystemCursor = not showSystemCursor |
1028 | 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 | 67 myengine.Fullscreen = fullscreen |
165 | 68 |
146 | 69 let dt: float32 = cpuTime() - currentTime |
70 currentTime = cpuTime() | |
1028 | 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 | 73 height = float32(winsize[1]) / float32(winsize[0]) |
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 | 80 |
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 | 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 | 87 |
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 | 92 |
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 | 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 | 100 if ballTop >= barTop and ballBottom <= barBottom: |
101 ballVelocity[0] = abs(ballVelocity[0]) | |
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() |