Mercurial > games > semicongine
comparison examples/E10_pong.nim @ 1028:2ba3f18e7cad
fix: pong example was not working yet
author | sam <sam@basx.dev> |
---|---|
date | Wed, 22 May 2024 03:52:20 +0700 |
parents | 887ddc8d45fd |
children | 71315636ba82 |
comparison
equal
deleted
inserted
replaced
1027:d6c27f0ed3e4 | 1028:2ba3f18e7cad |
---|---|
1 import std/times | 1 import std/times |
2 import std/tables | 2 import std/tables |
3 | 3 |
4 import ../src/semicongine | 4 import ../semicongine |
5 | 5 |
6 let | 6 let |
7 barcolor = hexToColorAlpha("5A3F00").gamma(2.2).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 = hexToColorAlpha("B17F08").gamma(2.2).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 material = Material(name: "default") | 13 matDef = MaterialType(name: "default", vertexAttributes: { |
14 "position": Vec3F32, | |
15 "color": Vec4F32, | |
16 }.toTable) | |
14 | 17 |
15 var | 18 var |
16 level: Scene | 19 level: Scene |
17 ballVelocity = newVec2f(1, 1).normalized * ballSpeed | 20 ballVelocity = newVec2f(1, 1).normalized * ballSpeed |
18 | 21 |
19 when isMainModule: | 22 when isMainModule: |
20 var myengine = initEngine("Pong") | 23 var myengine = initEngine("Pong") |
21 | 24 |
22 var player = rect(color=barcolor, width=barWidth, height=barSize) | 25 var player = rect(color = barcolor, width = barWidth, height = barSize) |
23 player.material = material | 26 player.material = matDef.initMaterialData(name = "player material") |
24 var ball = circle(color=ballcolor) | 27 var ball = circle(color = ballcolor) |
25 ball.material = material | 28 ball.material = matDef.initMaterialData(name = "player material") |
26 level = Scene(name: "scene", meshes: @[ball, player]) | 29 level = Scene(name: "scene", meshes: @[ball, player]) |
27 | 30 |
28 const | 31 const |
29 shaderConfiguration = createShaderConfiguration( | 32 shaderConfiguration = createShaderConfiguration( |
30 inputs=[ | 33 name = "default shader", |
34 inputs = [ | |
31 attr[Vec3f]("position"), | 35 attr[Vec3f]("position"), |
32 attr[Vec4f]("color", memoryPerformanceHint=PreferFastWrite), | 36 attr[Vec4f]("color", memoryPerformanceHint = PreferFastWrite), |
33 attr[Mat4]("transform", memoryPerformanceHint=PreferFastWrite, perInstance=true), | 37 attr[Mat4]("transform", memoryPerformanceHint = PreferFastWrite, perInstance = true), |
34 ], | 38 ], |
35 intermediates=[attr[Vec4f]("outcolor")], | 39 intermediates = [attr[Vec4f]("outcolor")], |
36 uniforms=[attr[Mat4]("projection")], | 40 uniforms = [attr[Mat4]("projection")], |
37 outputs=[attr[Vec4f]("color")], | 41 outputs = [attr[Vec4f]("color")], |
38 vertexCode="""outcolor = color; gl_Position = vec4(position, 1) * (transform * Uniforms.projection);""", | 42 vertexCode = """outcolor = color; gl_Position = vec4(position, 1) * (transform * Uniforms.projection);""", |
39 fragmentCode="color = outcolor;", | 43 fragmentCode = "color = outcolor;", |
40 ) | 44 ) |
41 | 45 |
42 # set up rendering | 46 # set up rendering |
43 myengine.initRenderer({"default": shaderConfiguration}.toTable) | 47 myengine.initRenderer({matDef: shaderConfiguration}) |
44 level.addShaderGlobal("projection", Unit4f32) | 48 level.addShaderGlobal("projection", Unit4f32) |
45 myengine.addScene(level) | 49 myengine.loadScene(level) |
46 | 50 |
47 var | 51 var |
48 winsize = myengine.getWindow().size | 52 winsize = myengine.GetWindow().size |
49 height = float32(winsize[1]) / float32(winsize[0]) | 53 height = float32(winsize[1]) / float32(winsize[0]) |
50 width = 1'f | 54 width = 1'f |
51 currentTime = cpuTime() | 55 currentTime = cpuTime() |
52 showSystemCursor = true | 56 showSystemCursor = true |
53 fullscreen = false | 57 fullscreen = false |
54 while myengine.updateInputs() == Running and not myengine.keyIsDown(Escape): | 58 while myengine.UpdateInputs() and not KeyIsDown(Escape): |
55 if myengine.keyWasPressed(C): | 59 if KeyWasPressed(C): |
56 if showSystemCursor: | 60 if showSystemCursor: |
57 myengine.hideSystemCursor() | 61 myengine.HideSystemCursor() |
58 else: | 62 else: |
59 myengine.showSystemCursor() | 63 myengine.ShowSystemCursor() |
60 showSystemCursor = not showSystemCursor | 64 showSystemCursor = not showSystemCursor |
61 if myengine.keyWasPressed(F): | 65 if KeyWasPressed(F): |
62 fullscreen = not fullscreen | 66 fullscreen = not fullscreen |
63 myengine.fullscreen(fullscreen) | 67 myengine.Fullscreen = fullscreen |
64 | 68 |
65 let dt: float32 = cpuTime() - currentTime | 69 let dt: float32 = cpuTime() - currentTime |
66 currentTime = cpuTime() | 70 currentTime = cpuTime() |
67 if myengine.windowWasResized(): | 71 if WindowWasResized(): |
68 winsize = myengine.getWindow().size | 72 winsize = myengine.GetWindow().size |
69 height = float32(winsize[1]) / float32(winsize[0]) | 73 height = float32(winsize[1]) / float32(winsize[0]) |
70 width = 1'f | 74 width = 1'f |
71 setShaderGlobal(level, "projection", ortho(0, width, 0, height, 0, 1)) | 75 setShaderGlobal(level, "projection", ortho(0, width, 0, height, 0, 1)) |
72 if myengine.keyIsDown(Down) and (player.transform.col(3).y + barSize/2) < height: | 76 if KeyIsDown(Down) and (player.transform.col(3).y + barSize/2) < height: |
73 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) |
74 if myengine.keyIsDown(Up) and (player.transform.col(3).y - barSize/2) > 0: | 78 if KeyIsDown(Up) and (player.transform.col(3).y - barSize/2) > 0: |
75 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) |
76 | 80 |
77 # bounce level | 81 # bounce level |
78 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] |
79 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] |