Mercurial > games > semicongine
comparison examples/E10_pong.nim @ 1140:5934c5615f13
did: update all examples to work with latest refactoring
| author | sam <sam@basx.dev> |
|---|---|
| date | Sat, 08 Jun 2024 15:16:17 +0700 |
| parents | 114f395b9144 |
| children |
comparison
equal
deleted
inserted
replaced
| 1139:114f395b9144 | 1140:5934c5615f13 |
|---|---|
| 2 import std/tables | 2 import std/tables |
| 3 | 3 |
| 4 import ../semicongine | 4 import ../semicongine |
| 5 | 5 |
| 6 let | 6 let |
| 7 barcolor = toRGBA("5A3F00").toSRGB().colorToHex() | 7 barcolor = ToRGBA("5A3F00").ToSRGB().ColorToHex() |
| 8 barSize = 0.1'f | 8 barSize = 0.1'f |
| 9 barWidth = 0.01'f | 9 barWidth = 0.01'f |
| 10 ballcolor = toRGBA("B17F08").toSRGB().colorToHex() | 10 ballcolor = ToRGBA("B17F08").ToSRGB().ColorToHex() |
| 11 ballSize = 0.01'f | 11 ballSize = 0.01'f |
| 12 ballSpeed = 60'f | 12 ballSpeed = 60'f |
| 13 matDef = MaterialType(name: "default", vertexAttributes: { | 13 matDef = MaterialType(name: "default", vertexAttributes: { |
| 14 "position": Vec3F32, | 14 "position": Vec3F32, |
| 15 "color": Vec4F32, | 15 "color": Vec4F32, |
| 16 }.toTable) | 16 }.toTable) |
| 17 | 17 |
| 18 var | 18 var |
| 19 level: Scene | 19 level: Scene |
| 20 ballVelocity = NewVec2f(1, 1).normalized * ballSpeed | 20 ballVelocity = NewVec2f(1, 1).Normalized * ballSpeed |
| 21 | 21 |
| 22 when isMainModule: | 22 when isMainModule: |
| 23 var myengine = initEngine("Pong") | 23 var myengine = InitEngine("Pong") |
| 24 | 24 |
| 25 var player = rect(color = barcolor, width = barWidth, height = barSize) | 25 var player = Rect(color = barcolor, width = barWidth, height = barSize) |
| 26 player.material = matDef.InitMaterialData(name = "player material") | 26 player.material = matDef.InitMaterialData(name = "player material") |
| 27 var ball = circle(color = ballcolor) | 27 var ball = Circle(color = ballcolor) |
| 28 ball.material = matDef.InitMaterialData(name = "player material") | 28 ball.material = matDef.InitMaterialData(name = "player material") |
| 29 level = Scene(name: "scene", meshes: @[ball, player]) | 29 level = Scene(name: "scene", meshes: @[ball, player]) |
| 30 | 30 |
| 31 const | 31 const |
| 32 shaderConfiguration = createShaderConfiguration( | 32 shaderConfiguration = CreateShaderConfiguration( |
| 33 name = "default shader", | 33 name = "default shader", |
| 34 inputs = [ | 34 inputs = [ |
| 35 attr[Vec3f]("position"), | 35 Attr[Vec3f]("position"), |
| 36 attr[Vec4f]("color", memoryPerformanceHint = PreferFastWrite), | 36 Attr[Vec4f]("color", memoryPerformanceHint = PreferFastWrite), |
| 37 attr[Mat4]("transform", memoryPerformanceHint = PreferFastWrite, perInstance = true), | 37 Attr[Mat4]("transform", memoryPerformanceHint = PreferFastWrite, perInstance = true), |
| 38 ], | 38 ], |
| 39 intermediates = [attr[Vec4f]("outcolor")], | 39 intermediates = [Attr[Vec4f]("outcolor")], |
| 40 uniforms = [attr[Mat4]("projection")], | 40 uniforms = [Attr[Mat4]("projection")], |
| 41 outputs = [attr[Vec4f]("color")], | 41 outputs = [Attr[Vec4f]("color")], |
| 42 vertexCode = """outcolor = color; gl_Position = vec4(position, 1) * (transform * Uniforms.projection);""", | 42 vertexCode = """outcolor = color; gl_Position = vec4(position, 1) * (transform * Uniforms.projection);""", |
| 43 fragmentCode = "color = outcolor;", | 43 fragmentCode = "color = outcolor;", |
| 44 ) | 44 ) |
| 45 | 45 |
| 46 # set up rendering | 46 # set up rendering |
| 47 myengine.initRenderer({matDef: shaderConfiguration}) | 47 myengine.InitRenderer({matDef: shaderConfiguration}) |
| 48 level.addShaderGlobal("projection", Unit4f32) | 48 level.AddShaderGlobal("projection", Unit4f32) |
| 49 myengine.loadScene(level) | 49 myengine.LoadScene(level) |
| 50 | 50 |
| 51 var | 51 var |
| 52 winsize = myengine.GetWindow().size | 52 winsize = myengine.GetWindow().Size |
| 53 height = float32(winsize[1]) / float32(winsize[0]) | 53 height = float32(winsize[1]) / float32(winsize[0]) |
| 54 width = 1'f | 54 width = 1'f |
| 55 currentTime = cpuTime() | 55 currentTime = cpuTime() |
| 56 showSystemCursor = true | 56 showSystemCursor = true |
| 57 fullscreen = false | 57 fullscreen = false |
| 67 myengine.Fullscreen = fullscreen | 67 myengine.Fullscreen = fullscreen |
| 68 | 68 |
| 69 let dt: float32 = cpuTime() - currentTime | 69 let dt: float32 = cpuTime() - currentTime |
| 70 currentTime = cpuTime() | 70 currentTime = cpuTime() |
| 71 if WindowWasResized(): | 71 if WindowWasResized(): |
| 72 winsize = myengine.GetWindow().size | 72 winsize = myengine.GetWindow().Size |
| 73 height = float32(winsize[1]) / float32(winsize[0]) | 73 height = float32(winsize[1]) / float32(winsize[0]) |
| 74 width = 1'f | 74 width = 1'f |
| 75 setShaderGlobal(level, "projection", ortho(0, width, 0, height, 0, 1)) | 75 SetShaderGlobal(level, "projection", Ortho(0, width, 0, height, 0, 1)) |
| 76 if KeyIsDown(Down) and (player.transform.col(3).y + barSize/2) < height: | 76 if KeyIsDown(Down) and (player.transform.Col(3).y + barSize/2) < height: |
| 77 player.transform = player.transform * translate(0'f, 1'f * dt, 0'f) | 77 player.transform = player.transform * Translate(0'f, 1'f * dt, 0'f) |
| 78 if KeyIsDown(Up) and (player.transform.col(3).y - barSize/2) > 0: | 78 if KeyIsDown(Up) and (player.transform.Col(3).y - barSize/2) > 0: |
| 79 player.transform = player.transform * translate(0'f, -1'f * dt, 0'f) | 79 player.transform = player.transform * Translate(0'f, -1'f * dt, 0'f) |
| 80 | 80 |
| 81 # bounce level | 81 # bounce level |
| 82 if ball.transform.col(3).x + ballSize/2 > width: ballVelocity[0] = -ballVelocity[0] | 82 if ball.transform.Col(3).x + ballSize/2 > width: ballVelocity[0] = -ballVelocity[0] |
| 83 if ball.transform.col(3).y - ballSize/2 <= 0: ballVelocity[1] = -ballVelocity[1] | 83 if ball.transform.Col(3).y - ballSize/2 <= 0: ballVelocity[1] = -ballVelocity[1] |
| 84 if ball.transform.col(3).y + ballSize/2 > height: ballVelocity[1] = -ballVelocity[1] | 84 if ball.transform.Col(3).y + ballSize/2 > height: ballVelocity[1] = -ballVelocity[1] |
| 85 | 85 |
| 86 ball.transform = ball.transform * translate(ballVelocity[0] * dt, ballVelocity[1] * dt, 0'f32) | 86 ball.transform = ball.transform * Translate(ballVelocity[0] * dt, ballVelocity[1] * dt, 0'f32) |
| 87 | 87 |
| 88 # loose | 88 # loose |
| 89 if ball.transform.col(3).x - ballSize/2 <= 0: | 89 if ball.transform.Col(3).x - ballSize/2 <= 0: |
| 90 ball.transform = scale(ballSize, ballSize, 1'f) * translate(30'f, 30'f, 0'f) | 90 ball.transform = Scale(ballSize, ballSize, 1'f) * Translate(30'f, 30'f, 0'f) |
| 91 ballVelocity = NewVec2f(1, 1).normalized * ballSpeed | 91 ballVelocity = NewVec2f(1, 1).Normalized * ballSpeed |
| 92 | 92 |
| 93 # bar | 93 # bar |
| 94 if ball.transform.col(3).x - ballSize/2 <= barWidth: | 94 if ball.transform.Col(3).x - ballSize/2 <= barWidth: |
| 95 let | 95 let |
| 96 barTop = player.transform.col(3).y - barSize/2 | 96 barTop = player.transform.Col(3).y - barSize/2 |
| 97 barBottom = player.transform.col(3).y + barSize/2 | 97 barBottom = player.transform.Col(3).y + barSize/2 |
| 98 ballTop = ball.transform.col(3).y - ballSize/2 | 98 ballTop = ball.transform.Col(3).y - ballSize/2 |
| 99 ballBottom = ball.transform.col(3).y + ballSize/2 | 99 ballBottom = ball.transform.Col(3).y + ballSize/2 |
| 100 if ballTop >= barTop and ballBottom <= barBottom: | 100 if ballTop >= barTop and ballBottom <= barBottom: |
| 101 ballVelocity[0] = abs(ballVelocity[0]) | 101 ballVelocity[0] = abs(ballVelocity[0]) |
| 102 | 102 |
| 103 myengine.renderScene(level) | 103 myengine.RenderScene(level) |
| 104 myengine.Destroy() |
