diff 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
line wrap: on
line diff
--- a/examples/E10_pong.nim	Wed Apr 26 02:15:43 2023 +0700
+++ b/examples/E10_pong.nim	Thu Apr 27 00:23:23 2023 +0700
@@ -1,86 +1,104 @@
+import std/times
+
 import semicongine
 
-const
-  barcolor = RGBfromHex("5A3F00").gamma(2.2)
+let
+  barcolor = hexToColor("5A3F00").gamma(2.2).colorToHex()
   barSize = 0.1'f
   barWidth = 0.01'f
-  ballcolor = RGBfromHex("B17F08").gamma(2.2)
+  ballcolor = hexToColor("B17F08").gamma(2.2).colorToHex()
   levelRatio = 1
   ballSize = 0.01'f
-  backgroundColor = RGBAfromHex("FAC034").gamma(2.2)
+  backgroundColor = hexToColorAlpha("FAC034FF").gamma(2.2)
   ballSpeed = 60'f
 
 var
   level: Entity
   ballVelocity = newVec2f(1, 1).normalized * ballSpeed
 
-#[
-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 * ballSpeed
-    ball.transform[0, 3] = width / 2
-    ball.transform[1, 3] = height / 2
-
-  # 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 = initEngine("Pong")
   level = newEntity("Level")
-  var playerbarmesh = rect()
-  playerbarmesh.vertexData.color.data = @[barcolor, barcolor, barcolor, barcolor]
+  var playerbarmesh = rect(color=barcolor)
   var playerbar = newEntity("playerbar", playerbarmesh)
   playerbar.transform = scale3d(barWidth, barSize, 1'f) * translate3d(0.5'f, 0'f, 0'f)
   var player = newEntity("player", playerbar)
   player.transform = translate3d(0'f, 0.3'f, 0'f)
   level.add player
 
-  var ballmesh = circle()
-  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 ballmesh = circle(color=ballcolor)
   var ball = newEntity("ball", ballmesh)
   ball.transform = scale3d(ballSize, ballSize, 1'f) * translate3d(10'f, 10'f, 0'f)
   level.add ball
 
-  pipeline.clearColor = backgroundColor
-  # show something
-  myengine.run(pipeline, globalUpdate)
+  const
+    vertexInput = @[
+      attr[Vec3f]("position", memoryLocation=VRAM),
+      attr[Vec3f]("color", memoryLocation=VRAMVisible),
+      attr[Mat4]("transform", memoryLocation=VRAMVisible, perInstance=true),
+    ]
+    vertexOutput = @[attr[Vec3f]("outcolor")]
+    uniforms = @[attr[Mat4]("projection")]
+    fragOutput = @[attr[Vec4f]("color")]
+    vertexCode = compileGlslShader(
+      stage=VK_SHADER_STAGE_VERTEX_BIT,
+      inputs=vertexInput,
+      uniforms=uniforms,
+      outputs=vertexOutput,
+      main="""outcolor = color; gl_Position = vec4(position, 1) * (transform * Uniforms.projection);"""
+    )
+    fragmentCode = compileGlslShader(
+      stage=VK_SHADER_STAGE_FRAGMENT_BIT,
+      inputs=vertexOutput,
+      uniforms=uniforms,
+      outputs=fragOutput,
+      main="color = vec4(outcolor, 1);"
+    )
+
+  # set up rendering
+  myengine.setRenderer(myengine.gpuDevice.simpleForwardRenderPass(vertexCode, fragmentCode, clearColor=backgroundColor))
+  myengine.addScene(level, vertexInput, transformAttribute="transform")
+  var projection = initShaderGlobal("projection", Unit4f32)
+  level.add projection
 
-  myengine.destroy()
+  var
+    winsize = myengine.getWindow().size
+    height = float32(winsize[1]) / float32(winsize[0])
+    width = 1'f
+    currentTime = cpuTime()
+  while myengine.updateInputs() == Running and not myengine.keyWasPressed(Escape):
+    let dt: float32 = cpuTime() - currentTime
+    currentTime = cpuTime()
+    if myengine.windowWasResized():
+      winsize = myengine.getWindow().size
+      height = float32(winsize[1]) / float32(winsize[0])
+      width = 1'f
+      setValue[Mat4](projection.value, ortho[float32](0'f, width, 0'f, height, 0'f, 1'f))
+    var player = level.firstWithName("player")
+    if myengine.keyIsDown(Down) and (player.transform.col(3).y + barSize/2) < height:
+      player.transform = player.transform * translate3d(0'f, 1'f * dt, 0'f)
+    if myengine.keyIsDown(Up) and (player.transform.col(3).y - barSize/2) > 0:
+      player.transform = player.transform * translate3d(0'f, -1'f * dt, 0'f)
+
+    # 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]
+
+    ball.transform = ball.transform * translate3d(ballVelocity[0] * dt, ballVelocity[1] * dt, 0'f32)
+
+    # loose
+    if ball.transform.col(3).x - ballSize/2 <= 0:
+      break
+
+    # 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])
+
+    myengine.renderScene(level)