comparison examples/E10_pong.nim @ 607:64eb53f81cf6

add: improvments and E10 (pong)
author Sam <sam@basx.dev>
date Thu, 27 Apr 2023 00:23:23 +0700
parents f41c1b78cf5b
children 134647ed5b60
comparison
equal deleted inserted replaced
606:f41c1b78cf5b 607:64eb53f81cf6
1 import std/times
2
1 import semicongine 3 import semicongine
2 4
3 const 5 let
4 barcolor = RGBfromHex("5A3F00").gamma(2.2) 6 barcolor = hexToColor("5A3F00").gamma(2.2).colorToHex()
5 barSize = 0.1'f 7 barSize = 0.1'f
6 barWidth = 0.01'f 8 barWidth = 0.01'f
7 ballcolor = RGBfromHex("B17F08").gamma(2.2) 9 ballcolor = hexToColor("B17F08").gamma(2.2).colorToHex()
8 levelRatio = 1 10 levelRatio = 1
9 ballSize = 0.01'f 11 ballSize = 0.01'f
10 backgroundColor = RGBAfromHex("FAC034").gamma(2.2) 12 backgroundColor = hexToColorAlpha("FAC034FF").gamma(2.2)
11 ballSpeed = 60'f 13 ballSpeed = 60'f
12 14
13 var 15 var
14 level: Entity 16 level: Entity
15 ballVelocity = newVec2f(1, 1).normalized * ballSpeed 17 ballVelocity = newVec2f(1, 1).normalized * ballSpeed
16 18
17 #[
18 proc globalUpdate(engine: var Engine; t, dt: float32) =
19 var height = float32(engine.vulkan.frameSize.y) / float32(
20 engine.vulkan.frameSize.x)
21 var width = 1'f
22 uniforms.view.value = ortho[float32](
23 0'f, width,
24 0'f, height,
25 0'f, 1'f,
26 )
27 engine.vulkan.device.updateUniformData(pipeline, uniforms)
28 var player = level.firstWithName("player")
29 if Down in engine.input.keysDown and (player.transform.col(3).y + barSize/2) < height:
30 player.transform = player.transform * translate3d(0'f, 1'f * dt, 0'f)
31 if Up in engine.input.keysDown and (player.transform.col(3).y - barSize/2) > 0:
32 player.transform = player.transform * translate3d(0'f, -1'f * dt, 0'f)
33
34 var ball = level.firstWithName("ball")
35 ball.transform = ball.transform * translate3d(ballVelocity[0] * dt,
36 ballVelocity[1] * dt, 0'f)
37
38 # loose
39 if ball.transform.col(3).x - ballSize/2 <= 0:
40 ballVelocity = Vec2([1'f, 1'f]).normalized * ballSpeed
41 ball.transform[0, 3] = width / 2
42 ball.transform[1, 3] = height / 2
43
44 # bounce level
45 if ball.transform.col(3).x + ballSize/2 > width: ballVelocity[
46 0] = -ballVelocity[0]
47 if ball.transform.col(3).y - ballSize/2 <= 0: ballVelocity[1] = -ballVelocity[1]
48 if ball.transform.col(3).y + ballSize/2 > height: ballVelocity[
49 1] = -ballVelocity[1]
50
51 # bar
52 if ball.transform.col(3).x - ballSize/2 <= barWidth:
53 let
54 barTop = player.transform.col(3).y - barSize/2
55 barBottom = player.transform.col(3).y + barSize/2
56 ballTop = ball.transform.col(3).y - ballSize/2
57 ballBottom = ball.transform.col(3).y + ballSize/2
58 if ballTop >= barTop and ballBottom <= barBottom:
59 ballVelocity[0] = abs(ballVelocity[0])
60 ]#
61
62 when isMainModule: 19 when isMainModule:
63 var myengine = initEngine("Pong") 20 var myengine = initEngine("Pong")
64 level = newEntity("Level") 21 level = newEntity("Level")
65 var playerbarmesh = rect() 22 var playerbarmesh = rect(color=barcolor)
66 playerbarmesh.vertexData.color.data = @[barcolor, barcolor, barcolor, barcolor]
67 var playerbar = newEntity("playerbar", playerbarmesh) 23 var playerbar = newEntity("playerbar", playerbarmesh)
68 playerbar.transform = scale3d(barWidth, barSize, 1'f) * translate3d(0.5'f, 0'f, 0'f) 24 playerbar.transform = scale3d(barWidth, barSize, 1'f) * translate3d(0.5'f, 0'f, 0'f)
69 var player = newEntity("player", playerbar) 25 var player = newEntity("player", playerbar)
70 player.transform = translate3d(0'f, 0.3'f, 0'f) 26 player.transform = translate3d(0'f, 0.3'f, 0'f)
71 level.add player 27 level.add player
72 28
73 var ballmesh = circle() 29 var ballmesh = circle(color=ballcolor)
74 ballmesh.vertexData.color.data = newSeq[Vec3](ballmesh.vertexData.position.data.len)
75 for i in 0 ..< ballmesh.vertexData.color.data.len:
76 ballmesh.vertexData.color.data[i] = ballcolor
77 ballmesh.vertexData.transform.data = @[Unit44]
78 var ball = newEntity("ball", ballmesh) 30 var ball = newEntity("ball", ballmesh)
79 ball.transform = scale3d(ballSize, ballSize, 1'f) * translate3d(10'f, 10'f, 0'f) 31 ball.transform = scale3d(ballSize, ballSize, 1'f) * translate3d(10'f, 10'f, 0'f)
80 level.add ball 32 level.add ball
81 33
82 pipeline.clearColor = backgroundColor 34 const
83 # show something 35 vertexInput = @[
84 myengine.run(pipeline, globalUpdate) 36 attr[Vec3f]("position", memoryLocation=VRAM),
37 attr[Vec3f]("color", memoryLocation=VRAMVisible),
38 attr[Mat4]("transform", memoryLocation=VRAMVisible, perInstance=true),
39 ]
40 vertexOutput = @[attr[Vec3f]("outcolor")]
41 uniforms = @[attr[Mat4]("projection")]
42 fragOutput = @[attr[Vec4f]("color")]
43 vertexCode = compileGlslShader(
44 stage=VK_SHADER_STAGE_VERTEX_BIT,
45 inputs=vertexInput,
46 uniforms=uniforms,
47 outputs=vertexOutput,
48 main="""outcolor = color; gl_Position = vec4(position, 1) * (transform * Uniforms.projection);"""
49 )
50 fragmentCode = compileGlslShader(
51 stage=VK_SHADER_STAGE_FRAGMENT_BIT,
52 inputs=vertexOutput,
53 uniforms=uniforms,
54 outputs=fragOutput,
55 main="color = vec4(outcolor, 1);"
56 )
85 57
86 myengine.destroy() 58 # set up rendering
59 myengine.setRenderer(myengine.gpuDevice.simpleForwardRenderPass(vertexCode, fragmentCode, clearColor=backgroundColor))
60 myengine.addScene(level, vertexInput, transformAttribute="transform")
61 var projection = initShaderGlobal("projection", Unit4f32)
62 level.add projection
63
64 var
65 winsize = myengine.getWindow().size
66 height = float32(winsize[1]) / float32(winsize[0])
67 width = 1'f
68 currentTime = cpuTime()
69 while myengine.updateInputs() == Running and not myengine.keyWasPressed(Escape):
70 let dt: float32 = cpuTime() - currentTime
71 currentTime = cpuTime()
72 if myengine.windowWasResized():
73 winsize = myengine.getWindow().size
74 height = float32(winsize[1]) / float32(winsize[0])
75 width = 1'f
76 setValue[Mat4](projection.value, ortho[float32](0'f, width, 0'f, height, 0'f, 1'f))
77 var player = level.firstWithName("player")
78 if myengine.keyIsDown(Down) and (player.transform.col(3).y + barSize/2) < height:
79 player.transform = player.transform * translate3d(0'f, 1'f * dt, 0'f)
80 if myengine.keyIsDown(Up) and (player.transform.col(3).y - barSize/2) > 0:
81 player.transform = player.transform * translate3d(0'f, -1'f * dt, 0'f)
82
83 # bounce level
84 if ball.transform.col(3).x + ballSize/2 > width: ballVelocity[0] = -ballVelocity[0]
85 if ball.transform.col(3).y - ballSize/2 <= 0: ballVelocity[1] = -ballVelocity[1]
86 if ball.transform.col(3).y + ballSize/2 > height: ballVelocity[1] = -ballVelocity[1]
87
88 ball.transform = ball.transform * translate3d(ballVelocity[0] * dt, ballVelocity[1] * dt, 0'f32)
89
90 # loose
91 if ball.transform.col(3).x - ballSize/2 <= 0:
92 break
93
94 # bar
95 if ball.transform.col(3).x - ballSize/2 <= barWidth:
96 let
97 barTop = player.transform.col(3).y - barSize/2
98 barBottom = player.transform.col(3).y + barSize/2
99 ballTop = ball.transform.col(3).y - ballSize/2
100 ballBottom = ball.transform.col(3).y + ballSize/2
101 if ballTop >= barTop and ballBottom <= barBottom:
102 ballVelocity[0] = abs(ballVelocity[0])
103
104 myengine.renderScene(level)