Mercurial > games > semicongine
changeset 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 | d3e014c3551c |
files | config.nims examples/E01_hello_triangle.nim examples/E02_squares.nim examples/E03_hello_cube.nim examples/E04_input.nim examples/E10_pong.nim |
diffstat | 6 files changed, 121 insertions(+), 115 deletions(-) [+] |
line wrap: on
line diff
--- a/config.nims Sat Jun 08 14:58:25 2024 +0700 +++ b/config.nims Sat Jun 08 15:16:17 2024 +0700 @@ -54,6 +54,11 @@ exec("nim build_dev_zip --run tests/test_resources.nim") exec("nim build_dev_dir --run tests/test_resources.nim") +task example_all, "Run all example programs": + for file in listFiles("examples"): + if file.endsWith(".nim"): + exec(&"nim build_dev --run {file}") + task publish, "publish all build": for file in listDirs("build/debug/linux"): exec(&"scp -r {file} sam@mail.basx.dev:/var/www/public.basx.dev/semicongine/debug/linux/")
--- a/examples/E01_hello_triangle.nim Sat Jun 08 14:58:25 2024 +0700 +++ b/examples/E01_hello_triangle.nim Sat Jun 08 15:16:17 2024 +0700 @@ -4,14 +4,14 @@ # shader setup const - shaderConfiguration = createShaderConfiguration( + shaderConfiguration = CreateShaderConfiguration( name = "default shader", inputs = [ - attr[Vec3f]("position"), - attr[Vec4f]("color"), + Attr[Vec3f]("position"), + Attr[Vec4f]("color"), ], - intermediates = [attr[Vec4f]("outcolor")], - outputs = [attr[Vec4f]("color")], + intermediates = [Attr[Vec4f]("outcolor")], + outputs = [Attr[Vec4f]("color")], vertexCode = "gl_Position = vec4(position, 1.0); outcolor = color;", fragmentCode = "color = outcolor;", ) @@ -19,19 +19,19 @@ # scene setup var scene = Scene(name: "scene", - meshes: @[newMesh( + meshes: @[NewMesh( positions = [NewVec3f(-0.5, 0.5), NewVec3f(0, -0.5), NewVec3f(0.5, 0.5)], colors = [NewVec4f(1, 0, 0, 1), NewVec4f(0, 1, 0, 1), NewVec4f(0, 0, 1, 1)], material = VERTEX_COLORED_MATERIAL.InitMaterialData() )] ) - myengine = initEngine("Hello triangle", showFps = true) + myengine = InitEngine("Hello triangle", showFps = true) -myengine.initRenderer({VERTEX_COLORED_MATERIAL: shaderConfiguration}, inFlightFrames = 2) -myengine.loadScene(scene) +myengine.InitRenderer({VERTEX_COLORED_MATERIAL: shaderConfiguration}, inFlightFrames = 2) +myengine.LoadScene(scene) while myengine.UpdateInputs() and not KeyWasPressed(Escape): - transform[Vec3f](scene.meshes[0][], "position", scale(1.001, 1.001)) - myengine.renderScene(scene) + Transform[Vec3f](scene.meshes[0][], "position", Scale(1.001, 1.001)) + myengine.RenderScene(scene) -myengine.destroy() +myengine.Destroy()
--- a/examples/E02_squares.nim Sat Jun 08 14:58:25 2024 +0700 +++ b/examples/E02_squares.nim Sat Jun 08 15:16:17 2024 +0700 @@ -43,16 +43,16 @@ const - shaderConfiguration = createShaderConfiguration( + shaderConfiguration = CreateShaderConfiguration( name = "default shader", inputs = [ - attr[Vec3f]("position"), - attr[Vec4f]("color", memoryPerformanceHint = PreferFastWrite), - attr[uint32]("index"), + Attr[Vec3f]("position"), + Attr[Vec4f]("color", memoryPerformanceHint = PreferFastWrite), + Attr[uint32]("index"), ], - intermediates = [attr[Vec4f]("outcolor")], - uniforms = [attr[float32]("time")], - outputs = [attr[Vec4f]("color")], + intermediates = [Attr[Vec4f]("outcolor")], + uniforms = [Attr[float32]("time")], + outputs = [Attr[Vec4f]("color")], vertexCode = """ float pos_weight = index / 100.0; // add some gamma correction? float t = sin(Uniforms.time * 0.5) * 0.5 + 0.5; @@ -68,22 +68,22 @@ "color": Vec4F32, "index": UInt32, }.toTable) - var squaremesh = newMesh( + var squaremesh = NewMesh( positions = vertices, indices = indices, colors = colors, ) - squaremesh[].initVertexAttribute("index", iValues.toSeq) + squaremesh[].InitVertexAttribute("index", iValues.toSeq) squaremesh.material = matDef.InitMaterialData(name = "default") - var myengine = initEngine("Squares") - myengine.initRenderer({matDef: shaderConfiguration}) + var myengine = InitEngine("Squares") + myengine.InitRenderer({matDef: shaderConfiguration}) var scene = Scene(name: "scene", meshes: @[squaremesh]) - scene.addShaderGlobal("time", 0.0'f32) - myengine.loadScene(scene) + scene.AddShaderGlobal("time", 0.0'f32) + myengine.LoadScene(scene) while myengine.UpdateInputs() and not KeyWasPressed(Escape): - scene.setShaderGlobal("time", getShaderGlobal[float32](scene, "time") + 0.0005'f) - myengine.renderScene(scene) + scene.SetShaderGlobal("time", GetShaderGlobal[float32](scene, "time") + 0.0005'f) + myengine.RenderScene(scene) - myengine.destroy() + myengine.Destroy()
--- a/examples/E03_hello_cube.nim Sat Jun 08 14:58:25 2024 +0700 +++ b/examples/E03_hello_cube.nim Sat Jun 08 15:16:17 2024 +0700 @@ -40,45 +40,45 @@ tris.add [off + 2'u16, off + 3'u16, off + 0'u16] when isMainModule: - var myengine = initEngine("Hello cube") + var myengine = InitEngine("Hello cube") const - shaderConfiguration = createShaderConfiguration( + shaderConfiguration = CreateShaderConfiguration( name = "default shader", inputs = [ - attr[Vec3f]("position"), - attr[Vec4f]("color", memoryPerformanceHint = PreferFastWrite), + Attr[Vec3f]("position"), + Attr[Vec4f]("color", memoryPerformanceHint = PreferFastWrite), ], - intermediates = [attr[Vec4f]("outcolor")], + intermediates = [Attr[Vec4f]("outcolor")], uniforms = [ - attr[Mat4]("projection"), - attr[Mat4]("view"), - attr[Mat4]("model"), + Attr[Mat4]("projection"), + Attr[Mat4]("view"), + Attr[Mat4]("model"), ], - outputs = [attr[Vec4f]("color")], + outputs = [Attr[Vec4f]("color")], vertexCode = """outcolor = color; gl_Position = (Uniforms.projection * Uniforms.view * Uniforms.model) * vec4(position, 1);""", fragmentCode = "color = outcolor;", ) var matDef = MaterialType(name: "default material", vertexAttributes: {"position": Vec3F32, "color": Vec4F32}.toTable) - var cube = Scene(name: "scene", meshes: @[newMesh(positions = cube_pos, indices = tris, colors = cube_color, material = matDef.InitMaterialData(name = "default"))]) - cube.addShaderGlobal("projection", Unit4f32) - cube.addShaderGlobal("view", Unit4f32) - cube.addShaderGlobal("model", Unit4f32) - myengine.initRenderer({matDef: shaderConfiguration}) - myengine.loadScene(cube) + var cube = Scene(name: "scene", meshes: @[NewMesh(positions = cube_pos, indices = tris, colors = cube_color, material = matDef.InitMaterialData(name = "default"))]) + cube.AddShaderGlobal("projection", Unit4f32) + cube.AddShaderGlobal("view", Unit4f32) + cube.AddShaderGlobal("model", Unit4f32) + myengine.InitRenderer({matDef: shaderConfiguration}) + myengine.LoadScene(cube) var t: float32 = cpuTime() while myengine.UpdateInputs() and not KeyWasPressed(Escape): - setShaderGlobal(cube, "model", translate(0'f32, 0'f32, 10'f32) * rotate(t, Yf32)) - setShaderGlobal(cube, "projection", - perspective( + SetShaderGlobal(cube, "model", Translate(0'f32, 0'f32, 10'f32) * Rotate(t, Yf32)) + SetShaderGlobal(cube, "projection", + Perspective( float32(PI / 4), - float32(myengine.GetWindow().size[0]) / float32(myengine.GetWindow().size[1]), + float32(myengine.GetWindow().Size[0]) / float32(myengine.GetWindow().Size[1]), 0.1'f32, 100'f32 ) ) t = cpuTime() - myengine.renderScene(cube) + myengine.RenderScene(cube) - myengine.destroy() + myengine.Destroy()
--- a/examples/E04_input.nim Sat Jun 08 14:58:25 2024 +0700 +++ b/examples/E04_input.nim Sat Jun 08 15:16:17 2024 +0700 @@ -92,15 +92,15 @@ when isMainModule: - var myengine = initEngine("Input") + var myengine = InitEngine("Input") # transform the cursor a bit to make it look nice let cursorscale = ( - scale2d(20'f32, 20'f32) * - translate2d(1'f32, 1'f32) * - rotate2d(-float32(PI) / 4'f32) * - scale2d(0.5'f32, 1'f32) * - rotate2d(float32(PI) / 4'f32) + Scale2d(20'f32, 20'f32) * + Translate2d(1'f32, 1'f32) * + Rotate2d(-float32(PI) / 4'f32) * + Scale2d(0.5'f32, 1'f32) * + Rotate2d(float32(PI) / 4'f32) ) var positions = arrow for i in 0 ..< positions.len: @@ -112,18 +112,18 @@ "position": Vec3F32, "color": Vec4F32, }.toTable) - cursormesh = newMesh( + cursormesh = NewMesh( positions = positions, colors = arrow_colors, material = matDef.InitMaterialData(), ) - keyboardmesh = newMesh( + keyboardmesh = NewMesh( positions = keyvertexpos, colors = keyvertexcolor, indices = keymeshindices, material = matDef.InitMaterialData(), ) - backgroundmesh = newMesh( + backgroundmesh = NewMesh( positions = @[ NewVec3f(0'f32, 0'f32), NewVec3f(1'f32, 0'f32), @@ -141,7 +141,7 @@ ) # define mesh objects - var keyboard_center = translate( + var keyboard_center = Translate( -float32(rowWidth) / 2'f32, -float32(tupleLen(keyRows) * (keyDimension + keyGap) - keyGap) / 2'f32, 0'f32 @@ -150,43 +150,43 @@ # shaders const - shaderConfiguration = createShaderConfiguration( + shaderConfiguration = CreateShaderConfiguration( name = "default shader", inputs = [ - attr[Vec3f]("position"), - attr[Vec4f]("color", memoryPerformanceHint = PreferFastWrite), - attr[Mat4]("transform", memoryPerformanceHint = PreferFastWrite, perInstance = true), + Attr[Vec3f]("position"), + Attr[Vec4f]("color", memoryPerformanceHint = PreferFastWrite), + Attr[Mat4]("transform", memoryPerformanceHint = PreferFastWrite, perInstance = true), ], - intermediates = [attr[Vec4f]("outcolor")], - uniforms = [attr[Mat4]("projection")], - outputs = [attr[Vec4f]("color")], + intermediates = [Attr[Vec4f]("outcolor")], + uniforms = [Attr[Mat4]("projection")], + outputs = [Attr[Vec4f]("color")], vertexCode = """outcolor = color; gl_Position = vec4(position, 1) * (transform * Uniforms.projection);""", fragmentCode = "color = outcolor;", ) # set up rendering - myengine.initRenderer({matDef: shaderConfiguration}) - scene.addShaderGlobal("projection", Unit4f32) - myengine.loadScene(scene) + myengine.InitRenderer({matDef: shaderConfiguration}) + scene.AddShaderGlobal("projection", Unit4f32) + myengine.LoadScene(scene) myengine.HideSystemCursor() # mainloop while myengine.UpdateInputs(): if WindowWasResized(): - scene.setShaderGlobal("projection", - ortho( - 0, float32(myengine.GetWindow().size[0]), - 0, float32(myengine.GetWindow().size[1]), + scene.SetShaderGlobal("projection", + Ortho( + 0, float32(myengine.GetWindow().Size[0]), + 0, float32(myengine.GetWindow().Size[1]), 0, 1, ) ) let - winsize = myengine.GetWindow().size - center = translate(float32(winsize[0]) / 2'f32, float32(winsize[1]) / 2'f32, 0.1'f32) + winsize = myengine.GetWindow().Size + center = Translate(float32(winsize[0]) / 2'f32, float32(winsize[1]) / 2'f32, 0.1'f32) keyboardmesh.transform = keyboard_center * center - backgroundmesh.transform = scale(float32(winsize[0]), float32(winsize[1]), 1'f32) + backgroundmesh.transform = Scale(float32(winsize[0]), float32(winsize[1]), 1'f32) - let mousePos = translate(MousePosition().x + 20, MousePosition().y + 20, 0'f32) + let mousePos = Translate(MousePosition().x + 20, MousePosition().y + 20, 0'f32) cursormesh.transform = mousePos for (index, key) in enumerate(keyIndices): @@ -203,6 +203,6 @@ keyboardmesh["color", baseIndex + 2] = baseColor keyboardmesh["color", baseIndex + 3] = baseColor - myengine.renderScene(scene) + myengine.RenderScene(scene) - myengine.destroy() + myengine.Destroy()
--- a/examples/E10_pong.nim Sat Jun 08 14:58:25 2024 +0700 +++ b/examples/E10_pong.nim Sat Jun 08 15:16:17 2024 +0700 @@ -4,10 +4,10 @@ import ../semicongine let - barcolor = toRGBA("5A3F00").toSRGB().colorToHex() + barcolor = ToRGBA("5A3F00").ToSRGB().ColorToHex() barSize = 0.1'f barWidth = 0.01'f - ballcolor = toRGBA("B17F08").toSRGB().colorToHex() + ballcolor = ToRGBA("B17F08").ToSRGB().ColorToHex() ballSize = 0.01'f ballSpeed = 60'f matDef = MaterialType(name: "default", vertexAttributes: { @@ -17,39 +17,39 @@ var level: Scene - ballVelocity = NewVec2f(1, 1).normalized * ballSpeed + ballVelocity = NewVec2f(1, 1).Normalized * ballSpeed when isMainModule: - var myengine = initEngine("Pong") + var myengine = InitEngine("Pong") - var player = rect(color = barcolor, width = barWidth, height = barSize) + var player = Rect(color = barcolor, width = barWidth, height = barSize) player.material = matDef.InitMaterialData(name = "player material") - var ball = circle(color = ballcolor) + var ball = Circle(color = ballcolor) ball.material = matDef.InitMaterialData(name = "player material") level = Scene(name: "scene", meshes: @[ball, player]) const - shaderConfiguration = createShaderConfiguration( + shaderConfiguration = CreateShaderConfiguration( name = "default shader", inputs = [ - attr[Vec3f]("position"), - attr[Vec4f]("color", memoryPerformanceHint = PreferFastWrite), - attr[Mat4]("transform", memoryPerformanceHint = PreferFastWrite, perInstance = true), + Attr[Vec3f]("position"), + Attr[Vec4f]("color", memoryPerformanceHint = PreferFastWrite), + Attr[Mat4]("transform", memoryPerformanceHint = PreferFastWrite, perInstance = true), ], - intermediates = [attr[Vec4f]("outcolor")], - uniforms = [attr[Mat4]("projection")], - outputs = [attr[Vec4f]("color")], + intermediates = [Attr[Vec4f]("outcolor")], + uniforms = [Attr[Mat4]("projection")], + outputs = [Attr[Vec4f]("color")], vertexCode = """outcolor = color; gl_Position = vec4(position, 1) * (transform * Uniforms.projection);""", fragmentCode = "color = outcolor;", ) # set up rendering - myengine.initRenderer({matDef: shaderConfiguration}) - level.addShaderGlobal("projection", Unit4f32) - myengine.loadScene(level) + myengine.InitRenderer({matDef: shaderConfiguration}) + level.AddShaderGlobal("projection", Unit4f32) + myengine.LoadScene(level) var - winsize = myengine.GetWindow().size + winsize = myengine.GetWindow().Size height = float32(winsize[1]) / float32(winsize[0]) width = 1'f currentTime = cpuTime() @@ -69,35 +69,36 @@ let dt: float32 = cpuTime() - currentTime currentTime = cpuTime() if WindowWasResized(): - winsize = myengine.GetWindow().size + winsize = myengine.GetWindow().Size height = float32(winsize[1]) / float32(winsize[0]) width = 1'f - setShaderGlobal(level, "projection", ortho(0, width, 0, height, 0, 1)) - if KeyIsDown(Down) and (player.transform.col(3).y + barSize/2) < height: - player.transform = player.transform * translate(0'f, 1'f * dt, 0'f) - if KeyIsDown(Up) and (player.transform.col(3).y - barSize/2) > 0: - player.transform = player.transform * translate(0'f, -1'f * dt, 0'f) + SetShaderGlobal(level, "projection", Ortho(0, width, 0, height, 0, 1)) + if KeyIsDown(Down) and (player.transform.Col(3).y + barSize/2) < height: + player.transform = player.transform * Translate(0'f, 1'f * dt, 0'f) + if KeyIsDown(Up) and (player.transform.Col(3).y - barSize/2) > 0: + player.transform = player.transform * Translate(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] + 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 * translate(ballVelocity[0] * dt, ballVelocity[1] * dt, 0'f32) + ball.transform = ball.transform * Translate(ballVelocity[0] * dt, ballVelocity[1] * dt, 0'f32) # loose - if ball.transform.col(3).x - ballSize/2 <= 0: - ball.transform = scale(ballSize, ballSize, 1'f) * translate(30'f, 30'f, 0'f) - ballVelocity = NewVec2f(1, 1).normalized * ballSpeed + if ball.transform.Col(3).x - ballSize/2 <= 0: + ball.transform = Scale(ballSize, ballSize, 1'f) * Translate(30'f, 30'f, 0'f) + ballVelocity = NewVec2f(1, 1).Normalized * ballSpeed # bar - if ball.transform.col(3).x - ballSize/2 <= barWidth: + 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 + 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) + myengine.RenderScene(level) + myengine.Destroy()