changeset 74:779607656b12

fix: stuff, add working pong
author Sam <sam@basx.dev>
date Mon, 06 Feb 2023 23:32:59 +0700
parents 17c12b79faaa
children a7f950307d2d
files examples/E01_hello_triangle.nim examples/E02_squares.nim examples/E03_hello_cube.nim examples/E04_input.nim examples/E10_pong.nim
diffstat 5 files changed, 110 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/examples/E01_hello_triangle.nim	Sun Feb 05 21:57:59 2023 +0700
+++ b/examples/E01_hello_triangle.nim	Mon Feb 06 23:32:59 2023 +0700
@@ -12,7 +12,7 @@
 
 var pipeline: RenderPipeline[VertexDataA, void]
 
-proc globalUpdate(engine: var Engine, dt: float32) =
+proc globalUpdate(engine: var Engine, t, dt: float32) =
   discard
 
 # vertex data (types must match the above VertexAttributes)
--- a/examples/E02_squares.nim	Sun Feb 05 21:57:59 2023 +0700
+++ b/examples/E02_squares.nim	Mon Feb 06 23:32:59 2023 +0700
@@ -17,7 +17,7 @@
   pipeline: RenderPipeline[VertexDataA, Uniforms]
   uniformdata = Uniforms(t: Descriptor[float32](value: 0'f32))
 
-proc globalUpdate(engine: var Engine, dt: float32) =
+proc globalUpdate(engine: var Engine, t, dt: float32) =
   uniformdata.t.value += dt
   engine.vulkan.device.updateUniformData(pipeline, uniformdata)
 
--- a/examples/E03_hello_cube.nim	Sun Feb 05 21:57:59 2023 +0700
+++ b/examples/E03_hello_cube.nim	Mon Feb 06 23:32:59 2023 +0700
@@ -26,13 +26,11 @@
 var
   pipeline: RenderPipeline[VertexDataA, Uniforms]
   uniforms: Uniforms
-  t: float32
 
 
-proc globalUpdate(engine: var Engine, dt: float32) =
+proc globalUpdate(engine: var Engine, t, dt: float32) =
   let ratio = float32(engine.vulkan.frameSize.y) / float32(
       engine.vulkan.frameSize.x)
-  t += dt
   uniforms.model.value = translate3d(0'f32, 0'f32, 10'f32) * rotate3d(t,
       Yf32) #  * rotate3d(float32(PI), Yf32)
 
--- a/examples/E04_input.nim	Sun Feb 05 21:57:59 2023 +0700
+++ b/examples/E04_input.nim	Mon Feb 06 23:32:59 2023 +0700
@@ -110,7 +110,7 @@
   firstRow = false
 
 
-proc globalUpdate(engine: var Engine, dt: float32) =
+proc globalUpdate(engine: var Engine, t, dt: float32) =
   uniforms.projection.value = ortho[float32](
     0'f32, float32(engine.vulkan.frameSize.x),
     0'f32, float32(engine.vulkan.frameSize.y),
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/examples/E10_pong.nim	Mon Feb 06 23:32:59 2023 +0700
@@ -0,0 +1,106 @@
+import semicongine
+
+type
+  Vertex = object
+    position: PositionAttribute[Vec2]
+    color: ColorAttribute[Vec3]
+    transform: ModelTransformAttribute
+  Uniforms = object
+    view: ViewProjectionTransform
+
+const
+  barcolor = Vec3([1'f, 0'f, 0'f])
+  barSize = 0.1'f
+  barWidth = 0.01'f
+  ballcolor = Vec3([0'f, 0'f, 0'f])
+  levelRatio = 1
+  ballSize = 0.01'f
+
+var
+  uniforms = Uniforms()
+  pipeline: RenderPipeline[Vertex, Uniforms]
+  level: Thing
+  ballVelocity = Vec2([1'f, 1'f]).normalized * 30'f
+
+proc globalUpdate(engine: var Engine; t, dt: float32) =
+  var height = float32(engine.vulkan.frameSize.y) / float32(
+      engine.vulkan.frameSize.x)
+  var width = 1'f
+  uniforms.view.value = ortho[float32](
+    0'f, width,
+    0'f, height,
+    0'f, 1'f,
+  )
+  engine.vulkan.device.updateUniformData(pipeline, uniforms)
+  var player = level.firstWithName("player")
+  if Down in engine.input.keysDown and (player.transform.col(3).y + barSize/2) < height:
+    player.transform = player.transform * translate3d(0'f, 1'f * dt, 0'f)
+  if Up in engine.input.keysDown and (player.transform.col(3).y - barSize/2) > 0:
+    player.transform = player.transform * translate3d(0'f, -1'f * dt, 0'f)
+
+  var ball = level.firstWithName("ball")
+  ball.transform = ball.transform * translate3d(ballVelocity[0] * dt,
+      ballVelocity[1] * dt, 0'f)
+
+  # loose
+  if ball.transform.col(3).x - ballSize/2 <= 0:
+    ballVelocity = Vec2([1'f, 1'f]).normalized * 30'f
+    ball.transform[0, 3] = 0.5
+    ball.transform[1, 3] = 0.5
+
+  # bounce level
+  if ball.transform.col(3).x + ballSize/2 > width: ballVelocity[
+      0] = -ballVelocity[0]
+  if ball.transform.col(3).y - ballSize/2 <= 0: ballVelocity[1] = -ballVelocity[1]
+  if ball.transform.col(3).y + ballSize/2 > height: ballVelocity[
+      1] = -ballVelocity[1]
+
+  # bar
+  if ball.transform.col(3).x - ballSize/2 <= barWidth:
+    let
+      barTop = player.transform.col(3).y - barSize/2
+      barBottom = player.transform.col(3).y + barSize/2
+      ballTop = ball.transform.col(3).y - ballSize/2
+      ballBottom = ball.transform.col(3).y + ballSize/2
+    if ballTop >= barTop and ballBottom <= barBottom:
+      ballVelocity[0] = abs(ballVelocity[0])
+
+
+when isMainModule:
+  var myengine = igniteEngine("Pong")
+  myengine.fps = 60
+  level = newThing("Level")
+  var playerbarmesh = quad[Vertex, Vec2, float32]()
+  playerbarmesh.vertexData.color.data = @[barcolor, barcolor, barcolor, barcolor]
+  playerbarmesh.vertexData.transform.data = @[Unit44]
+  var playerbar = newThing("playerbar", playerbarmesh)
+  playerbar.transform = scale3d(barWidth, barSize, 1'f) * translate3d(0.5'f,
+      0'f, 0'f)
+  var player = newThing("player", playerbar)
+  player.transform = translate3d(0'f, 0.3'f, 0'f)
+  level.add player
+
+  var ballmesh = circle[Vertex, Vec2, float32]()
+  ballmesh.vertexData.color.data = newSeq[Vec3](
+      ballmesh.vertexData.position.data.len)
+  for i in 0 ..< ballmesh.vertexData.color.data.len:
+    ballmesh.vertexData.color.data[i] = ballcolor
+  ballmesh.vertexData.transform.data = @[Unit44]
+  var ball = newThing("ball", ballmesh)
+  ball.transform = scale3d(ballSize, ballSize, 1'f) * translate3d(10'f, 10'f, 0'f)
+  level.add ball
+
+  # upload data, prepare shaders, etc
+  const vertexShader = generateVertexShaderCode[Vertex, Uniforms]()
+  const fragmentShader = generateFragmentShaderCode[Vertex]()
+  pipeline = setupPipeline[Vertex, Uniforms, uint16](
+    myengine,
+    level,
+    vertexShader,
+    fragmentShader
+  )
+  pipeline.clearColor = Vec4([1.0'f, 1.0'f, 1.0'f, 1.0'f])
+  # show something
+  myengine.run(pipeline, globalUpdate)
+  pipeline.trash()
+  myengine.trash()