annotate examples/E10_pong.nim @ 892:f2d7529b49ca

intermediate commit :P
author Sam <sam@basx.dev>
date Fri, 09 Feb 2024 20:02:55 +0700
parents 812b5e28f441
children 2ba3f18e7cad
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
607
64eb53f81cf6 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 606
diff changeset
1 import std/times
797
812b5e28f441 did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents: 777
diff changeset
2 import std/tables
607
64eb53f81cf6 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 606
diff changeset
3
732
dcc12ab20a91 did: fix API changes broke examples
Sam <sam@basx.dev>
parents: 714
diff changeset
4 import ../src/semicongine
535
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
5
607
64eb53f81cf6 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 606
diff changeset
6 let
671
d84b2e88776a fix: always use rgba
Sam <sam@basx.dev>
parents: 664
diff changeset
7 barcolor = hexToColorAlpha("5A3F00").gamma(2.2).colorToHex()
535
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
8 barSize = 0.1'f
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
9 barWidth = 0.01'f
671
d84b2e88776a fix: always use rgba
Sam <sam@basx.dev>
parents: 664
diff changeset
10 ballcolor = hexToColorAlpha("B17F08").gamma(2.2).colorToHex()
535
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
11 ballSize = 0.01'f
538
c1f16a095637 add: color functions + gamma correction
Sam <sam@basx.dev>
parents: 535
diff changeset
12 ballSpeed = 60'f
797
812b5e28f441 did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents: 777
diff changeset
13 material = Material(name: "default")
535
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
14
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
15 var
664
c33c8e156e3e did: update examples to use new API for scene + scene globals
Sam <sam@basx.dev>
parents: 627
diff changeset
16 level: Scene
606
f41c1b78cf5b add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 546
diff changeset
17 ballVelocity = newVec2f(1, 1).normalized * ballSpeed
535
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
18
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
19 when isMainModule:
606
f41c1b78cf5b add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 546
diff changeset
20 var myengine = initEngine("Pong")
535
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
21
797
812b5e28f441 did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents: 777
diff changeset
22 var player = rect(color=barcolor, width=barWidth, height=barSize)
812b5e28f441 did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents: 777
diff changeset
23 player.material = material
812b5e28f441 did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents: 777
diff changeset
24 var ball = circle(color=ballcolor)
812b5e28f441 did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents: 777
diff changeset
25 ball.material = material
812b5e28f441 did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents: 777
diff changeset
26 level = Scene(name: "scene", meshes: @[ball, player])
535
21c276c0a968 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
27
607
64eb53f81cf6 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 606
diff changeset
28 const
797
812b5e28f441 did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents: 777
diff changeset
29 shaderConfiguration = createShaderConfiguration(
812b5e28f441 did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents: 777
diff changeset
30 inputs=[
812b5e28f441 did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents: 777
diff changeset
31 attr[Vec3f]("position"),
812b5e28f441 did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents: 777
diff changeset
32 attr[Vec4f]("color", memoryPerformanceHint=PreferFastWrite),
812b5e28f441 did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents: 777
diff changeset
33 attr[Mat4]("transform", memoryPerformanceHint=PreferFastWrite, perInstance=true),
812b5e28f441 did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents: 777
diff changeset
34 ],
812b5e28f441 did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents: 777
diff changeset
35 intermediates=[attr[Vec4f]("outcolor")],
812b5e28f441 did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents: 777
diff changeset
36 uniforms=[attr[Mat4]("projection")],
812b5e28f441 did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents: 777
diff changeset
37 outputs=[attr[Vec4f]("color")],
777
754835bf175e add: changing rendering system, not finished yet, also upgrading to Nim 2
Sam <sam@basx.dev>
parents: 763
diff changeset
38 vertexCode="""outcolor = color; gl_Position = vec4(position, 1) * (transform * Uniforms.projection);""",
754835bf175e add: changing rendering system, not finished yet, also upgrading to Nim 2
Sam <sam@basx.dev>
parents: 763
diff changeset
39 fragmentCode="color = outcolor;",
607
64eb53f81cf6 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 606
diff changeset
40 )
64eb53f81cf6 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 606
diff changeset
41
64eb53f81cf6 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 606
diff changeset
42 # set up rendering
797
812b5e28f441 did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents: 777
diff changeset
43 myengine.initRenderer({"default": shaderConfiguration}.toTable)
664
c33c8e156e3e did: update examples to use new API for scene + scene globals
Sam <sam@basx.dev>
parents: 627
diff changeset
44 level.addShaderGlobal("projection", Unit4f32)
797
812b5e28f441 did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents: 777
diff changeset
45 myengine.addScene(level)
606
f41c1b78cf5b add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 546
diff changeset
46
607
64eb53f81cf6 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 606
diff changeset
47 var
64eb53f81cf6 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 606
diff changeset
48 winsize = myengine.getWindow().size
64eb53f81cf6 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 606
diff changeset
49 height = float32(winsize[1]) / float32(winsize[0])
64eb53f81cf6 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 606
diff changeset
50 width = 1'f
64eb53f81cf6 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 606
diff changeset
51 currentTime = cpuTime()
626
bf2f4a9cd962 add: option to show/hide cursor
Sam <sam@basx.dev>
parents: 625
diff changeset
52 showSystemCursor = true
627
34e536f7001f add: support for showing/hiding cursur, X11 fullscreen (win32 still missing)
Sam <sam@basx.dev>
parents: 626
diff changeset
53 fullscreen = false
34e536f7001f add: support for showing/hiding cursur, X11 fullscreen (win32 still missing)
Sam <sam@basx.dev>
parents: 626
diff changeset
54 while myengine.updateInputs() == Running and not myengine.keyIsDown(Escape):
626
bf2f4a9cd962 add: option to show/hide cursor
Sam <sam@basx.dev>
parents: 625
diff changeset
55 if myengine.keyWasPressed(C):
bf2f4a9cd962 add: option to show/hide cursor
Sam <sam@basx.dev>
parents: 625
diff changeset
56 if showSystemCursor:
bf2f4a9cd962 add: option to show/hide cursor
Sam <sam@basx.dev>
parents: 625
diff changeset
57 myengine.hideSystemCursor()
bf2f4a9cd962 add: option to show/hide cursor
Sam <sam@basx.dev>
parents: 625
diff changeset
58 else:
bf2f4a9cd962 add: option to show/hide cursor
Sam <sam@basx.dev>
parents: 625
diff changeset
59 myengine.showSystemCursor()
bf2f4a9cd962 add: option to show/hide cursor
Sam <sam@basx.dev>
parents: 625
diff changeset
60 showSystemCursor = not showSystemCursor
627
34e536f7001f add: support for showing/hiding cursur, X11 fullscreen (win32 still missing)
Sam <sam@basx.dev>
parents: 626
diff changeset
61 if myengine.keyWasPressed(F):
34e536f7001f add: support for showing/hiding cursur, X11 fullscreen (win32 still missing)
Sam <sam@basx.dev>
parents: 626
diff changeset
62 fullscreen = not fullscreen
34e536f7001f add: support for showing/hiding cursur, X11 fullscreen (win32 still missing)
Sam <sam@basx.dev>
parents: 626
diff changeset
63 myengine.fullscreen(fullscreen)
626
bf2f4a9cd962 add: option to show/hide cursor
Sam <sam@basx.dev>
parents: 625
diff changeset
64
607
64eb53f81cf6 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 606
diff changeset
65 let dt: float32 = cpuTime() - currentTime
64eb53f81cf6 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 606
diff changeset
66 currentTime = cpuTime()
64eb53f81cf6 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 606
diff changeset
67 if myengine.windowWasResized():
64eb53f81cf6 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 606
diff changeset
68 winsize = myengine.getWindow().size
64eb53f81cf6 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 606
diff changeset
69 height = float32(winsize[1]) / float32(winsize[0])
64eb53f81cf6 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 606
diff changeset
70 width = 1'f
709
3b00747b2451 did: few fixes
Sam <sam@basx.dev>
parents: 671
diff changeset
71 setShaderGlobal(level, "projection", ortho(0, width, 0, height, 0, 1))
607
64eb53f81cf6 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 606
diff changeset
72 if myengine.keyIsDown(Down) and (player.transform.col(3).y + barSize/2) < height:
797
812b5e28f441 did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents: 777
diff changeset
73 player.transform = player.transform * translate(0'f, 1'f * dt, 0'f)
607
64eb53f81cf6 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 606
diff changeset
74 if myengine.keyIsDown(Up) and (player.transform.col(3).y - barSize/2) > 0:
797
812b5e28f441 did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents: 777
diff changeset
75 player.transform = player.transform * translate(0'f, -1'f * dt, 0'f)
607
64eb53f81cf6 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 606
diff changeset
76
64eb53f81cf6 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 606
diff changeset
77 # bounce level
64eb53f81cf6 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 606
diff changeset
78 if ball.transform.col(3).x + ballSize/2 > width: ballVelocity[0] = -ballVelocity[0]
64eb53f81cf6 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 606
diff changeset
79 if ball.transform.col(3).y - ballSize/2 <= 0: ballVelocity[1] = -ballVelocity[1]
64eb53f81cf6 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 606
diff changeset
80 if ball.transform.col(3).y + ballSize/2 > height: ballVelocity[1] = -ballVelocity[1]
64eb53f81cf6 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 606
diff changeset
81
797
812b5e28f441 did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents: 777
diff changeset
82 ball.transform = ball.transform * translate(ballVelocity[0] * dt, ballVelocity[1] * dt, 0'f32)
607
64eb53f81cf6 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 606
diff changeset
83
64eb53f81cf6 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 606
diff changeset
84 # loose
64eb53f81cf6 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 606
diff changeset
85 if ball.transform.col(3).x - ballSize/2 <= 0:
797
812b5e28f441 did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents: 777
diff changeset
86 ball.transform = scale(ballSize, ballSize, 1'f) * translate(30'f, 30'f, 0'f)
627
34e536f7001f add: support for showing/hiding cursur, X11 fullscreen (win32 still missing)
Sam <sam@basx.dev>
parents: 626
diff changeset
87 ballVelocity = newVec2f(1, 1).normalized * ballSpeed
607
64eb53f81cf6 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 606
diff changeset
88
64eb53f81cf6 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 606
diff changeset
89 # bar
64eb53f81cf6 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 606
diff changeset
90 if ball.transform.col(3).x - ballSize/2 <= barWidth:
64eb53f81cf6 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 606
diff changeset
91 let
64eb53f81cf6 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 606
diff changeset
92 barTop = player.transform.col(3).y - barSize/2
64eb53f81cf6 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 606
diff changeset
93 barBottom = player.transform.col(3).y + barSize/2
64eb53f81cf6 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 606
diff changeset
94 ballTop = ball.transform.col(3).y - ballSize/2
64eb53f81cf6 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 606
diff changeset
95 ballBottom = ball.transform.col(3).y + ballSize/2
64eb53f81cf6 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 606
diff changeset
96 if ballTop >= barTop and ballBottom <= barBottom:
64eb53f81cf6 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 606
diff changeset
97 ballVelocity[0] = abs(ballVelocity[0])
64eb53f81cf6 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 606
diff changeset
98
64eb53f81cf6 add: improvments and E10 (pong)
Sam <sam@basx.dev>
parents: 606
diff changeset
99 myengine.renderScene(level)