annotate examples/E04_input.nim @ 316:b145a05c2459

add: changing rendering system, not finished yet, also upgrading to Nim 2
author Sam <sam@basx.dev>
date Mon, 07 Aug 2023 00:23:00 +0700
parents da0bd61abe91
children 887ddc8d45fd
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
66
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
1 import std/enumerate
64
e766d138cc5d add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 61
diff changeset
2 import std/typetraits
e766d138cc5d add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 61
diff changeset
3 import std/math
38
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
4
271
95281f2db400 did: fix API changes broke examples
Sam <sam@basx.dev>
parents: 253
diff changeset
5 import ../src/semicongine
38
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
6
64
e766d138cc5d add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 61
diff changeset
7 const
e766d138cc5d add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 61
diff changeset
8 arrow = @[
143
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
9 newVec3f(-1, -1),
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
10 newVec3f(1, -1),
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
11 newVec3f(-0.3, -0.3),
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
12 newVec3f(-0.3, -0.3),
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
13 newVec3f(-1, 1),
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
14 newVec3f(-1, -1),
64
e766d138cc5d add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 61
diff changeset
15 ]
66
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
16 # keyboard layout, specifying rows with key widths, negative numbers are empty spaces
64
e766d138cc5d add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 61
diff changeset
17 keyrows = (
143
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
18 [1.0, -0.6, 1.0, 1.0, 1.0, 1.0, -0.5, 1.0, 1.0, 1.0, 1.0, -0.5, 1.0, 1.0, 1.0, 1.0, -0.1, 1.0, 1.0, 1.0],
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
19 [1.2, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.8, -0.1, 1.0, 1.0, 1.0],
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
20 [1.8, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, -1.5, 1.0, 1.0, 1.0],
66
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
21 [2.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0],
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
22 [2.6, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 2.8, -1.3, 1.0],
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
23 [1.5, 1.5, 1.5, 6, 1.5, 1.5, -1.2, 1.5, -0.1, 1.0, 1.0, 1.0],
64
e766d138cc5d add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 61
diff changeset
24 )
66
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
25 keyDimension = 50'f32
64
e766d138cc5d add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 61
diff changeset
26 keyGap = 10'f32
210
f3912838cd69 fix: always use rgba
Sam <sam@basx.dev>
parents: 203
diff changeset
27 backgroundColor = newVec4f(0.6705882352941176, 0.6078431372549019, 0.5882352941176471, 1)
f3912838cd69 fix: always use rgba
Sam <sam@basx.dev>
parents: 203
diff changeset
28 baseColor = newVec4f(0.9411764705882353, 0.9058823529411765, 0.8470588235294118, 1)
f3912838cd69 fix: always use rgba
Sam <sam@basx.dev>
parents: 203
diff changeset
29 activeColor = newVec4f(0.6509803921568628, 0.22745098039215686, 0.3137254901960784, 1)
69
d830a1c0f86e fix: mouse coord, colors
Sam <sam@basx.dev>
parents: 67
diff changeset
30 arrow_colors = @[
d830a1c0f86e fix: mouse coord, colors
Sam <sam@basx.dev>
parents: 67
diff changeset
31 baseColor * 0.9'f32,
d830a1c0f86e fix: mouse coord, colors
Sam <sam@basx.dev>
parents: 67
diff changeset
32 baseColor * 0.9'f32,
d830a1c0f86e fix: mouse coord, colors
Sam <sam@basx.dev>
parents: 67
diff changeset
33 baseColor * 0.9'f32,
d830a1c0f86e fix: mouse coord, colors
Sam <sam@basx.dev>
parents: 67
diff changeset
34 baseColor * 0.8'f32,
d830a1c0f86e fix: mouse coord, colors
Sam <sam@basx.dev>
parents: 67
diff changeset
35 baseColor * 0.8'f32,
d830a1c0f86e fix: mouse coord, colors
Sam <sam@basx.dev>
parents: 67
diff changeset
36 baseColor * 0.8'f32,
d830a1c0f86e fix: mouse coord, colors
Sam <sam@basx.dev>
parents: 67
diff changeset
37 ]
66
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
38 keyIndices = [
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
39 Escape, F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, PrintScreen,
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
40 ScrollLock, Pause,
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
41
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
42 NumberRowExtra1, `1`, `2`, `3`, `4`, `5`, `6`, `7`, `8`, `9`, `0`,
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
43
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
44 NumberRowExtra2, NumberRowExtra3, Backspace, Insert, Home, PageUp,
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
45 Tab, Q, W, Key.E, R, T, Key.Y, U, I, O, P, LetterRow1Extra1,
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
46 LetterRow1Extra2, Delete, End, PageDown,
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
47
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
48 CapsLock, A, S, D, F, G, H, J, K, L, LetterRow2Extra1, LetterRow2Extra2,
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
49 LetterRow2Extra3, Enter,
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
50
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
51 ShiftL, Key.Z, Key.X, C, V, B, N, M, LetterRow3Extra1, LetterRow3Extra2,
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
52 LetterRow3Extra3, ShiftR, Up,
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
53
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
54 CtrlL, SuperL, AltL, Space, AltR, SuperR, CtrlR, Left, Down, Right
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
55 ]
38
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
56
145
a4e6e76128e6 add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 143
diff changeset
57 # build keyboard and cursor meshes
38
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
58 var
203
84fd522fdf3f did: update examples to use new API for scene + scene globals
Sam <sam@basx.dev>
parents: 163
diff changeset
59 scene: Scene
143
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
60 keyvertexpos: seq[Vec3f]
210
f3912838cd69 fix: always use rgba
Sam <sam@basx.dev>
parents: 203
diff changeset
61 keyvertexcolor: seq[Vec4f]
66
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
62 keymeshindices: seq[array[3, uint16]]
143
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
63 rowpos = newVec2f(0, 0)
64
e766d138cc5d add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 61
diff changeset
64 i = 0'u16
66
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
65 firstRow = true
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
66 rowWidth = 0'f32
64
e766d138cc5d add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 61
diff changeset
67 for row in keyrows.fields:
e766d138cc5d add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 61
diff changeset
68 for key in row:
66
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
69 let keySpace = float32(keyDimension * key)
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
70 if key > 0:
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
71 if keyIndices[i div 4] == Enter:
143
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
72 keyvertexpos.add newVec3f(rowpos[0], rowpos[1] - keyDimension - keyGap)
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
73 keyvertexpos.add newVec3f(rowpos[0] + keySpace, rowpos[1] - keyDimension - keyGap)
66
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
74 else:
143
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
75 keyvertexpos.add newVec3f(rowpos[0], rowpos[1])
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
76 keyvertexpos.add newVec3f(rowpos[0] + keySpace, rowpos[1])
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
77 keyvertexpos.add newVec3f(rowpos[0] + keySpace, rowpos[1] + keyDimension)
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
78 keyvertexpos.add newVec3f(rowpos[0], rowpos[1] + keyDimension)
66
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
79 keyvertexcolor.add [baseColor, baseColor, baseColor, baseColor]
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
80 keymeshindices.add [i, i + 1, i + 2]
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
81 keymeshindices.add [i + 2, i + 3, i]
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
82 rowpos[0] += keySpace + keyGap
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
83 i += 4
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
84 else:
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
85 rowpos[0] += -keySpace + keyGap
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
86 if firstRow:
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
87 rowWidth = rowpos[0]
64
e766d138cc5d add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 61
diff changeset
88 rowpos[0] = 0
66
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
89 rowpos[1] += keyDimension + keyGap * (if firstRow: 2'f32 else: 1'f32)
44bae6c72834 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 64
diff changeset
90 firstRow = false
38
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
91
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
92
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
93 when isMainModule:
143
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
94 var myengine = initEngine("Input")
38
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
95
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
96 # transform the cursor a bit to make it look nice
64
e766d138cc5d add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 61
diff changeset
97 let cursorscale = (
e766d138cc5d add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 61
diff changeset
98 scale2d(20'f32, 20'f32) *
e766d138cc5d add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 61
diff changeset
99 translate2d(1'f32, 1'f32) *
e766d138cc5d add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 61
diff changeset
100 rotate2d(-float32(PI) / 4'f32) *
e766d138cc5d add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 61
diff changeset
101 scale2d(0.5'f32, 1'f32) *
e766d138cc5d add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 61
diff changeset
102 rotate2d(float32(PI) / 4'f32)
e766d138cc5d add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 61
diff changeset
103 )
143
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
104 var positions = arrow
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
105 for i in 0 ..< positions.len:
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
106 positions[i] = cursorscale * newVec3f(positions[i].x, positions[i].y)
57
547f3a374271 did: refactor Vector names
Sam <sam@basx.dev>
parents: 56
diff changeset
107
145
a4e6e76128e6 add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 143
diff changeset
108 # define mesh objects
a4e6e76128e6 add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 143
diff changeset
109 var
a4e6e76128e6 add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 143
diff changeset
110 cursormesh = newMesh(
a4e6e76128e6 add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 143
diff changeset
111 positions=positions,
a4e6e76128e6 add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 143
diff changeset
112 colors=arrow_colors,
a4e6e76128e6 add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 143
diff changeset
113 instanceCount=1,
a4e6e76128e6 add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 143
diff changeset
114 )
a4e6e76128e6 add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 143
diff changeset
115 keyboardmesh = newMesh(
a4e6e76128e6 add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 143
diff changeset
116 positions=keyvertexpos,
a4e6e76128e6 add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 143
diff changeset
117 colors=keyvertexcolor,
a4e6e76128e6 add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 143
diff changeset
118 indices=keymeshindices
a4e6e76128e6 add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 143
diff changeset
119 )
a4e6e76128e6 add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 143
diff changeset
120 backgroundmesh = newMesh(
a4e6e76128e6 add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 143
diff changeset
121 positions= @[
a4e6e76128e6 add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 143
diff changeset
122 newVec3f(0'f32, 0'f32),
a4e6e76128e6 add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 143
diff changeset
123 newVec3f(1'f32, 0'f32),
a4e6e76128e6 add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 143
diff changeset
124 newVec3f(1'f32, 1'f32),
a4e6e76128e6 add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 143
diff changeset
125 newVec3f(0'f32, 1'f32),
a4e6e76128e6 add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 143
diff changeset
126 ],
a4e6e76128e6 add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 143
diff changeset
127 colors= @[
a4e6e76128e6 add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 143
diff changeset
128 backgroundColor,
a4e6e76128e6 add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 143
diff changeset
129 backgroundColor,
a4e6e76128e6 add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 143
diff changeset
130 backgroundColor,
a4e6e76128e6 add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 143
diff changeset
131 backgroundColor,
a4e6e76128e6 add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 143
diff changeset
132 ],
a4e6e76128e6 add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 143
diff changeset
133 indices= @[[0'u16, 1'u16, 2'u16], [2'u16, 3'u16, 0'u16]],
a4e6e76128e6 add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 143
diff changeset
134 )
38
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
135
145
a4e6e76128e6 add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 143
diff changeset
136 # define mesh objects
203
84fd522fdf3f did: update examples to use new API for scene + scene globals
Sam <sam@basx.dev>
parents: 163
diff changeset
137 scene = newScene("scene", newEntity("scene"))
302
da0bd61abe91 did: change API for ECS
Sam <sam@basx.dev>
parents: 271
diff changeset
138 scene.root.add newEntity("background", {"mesh": Component(backgroundmesh)})
da0bd61abe91 did: change API for ECS
Sam <sam@basx.dev>
parents: 271
diff changeset
139 let keyboard = newEntity("keyboard", {"mesh": Component(keyboardmesh)})
143
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
140 keyboard.transform = translate3d(
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
141 -float32(rowWidth) / 2'f32,
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
142 -float32(tupleLen(keyRows) * (keyDimension + keyGap) - keyGap) / 2'f32,
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
143 0'f32
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
144 )
302
da0bd61abe91 did: change API for ECS
Sam <sam@basx.dev>
parents: 271
diff changeset
145 scene.root.add newEntity("keyboard-center", [], keyboard)
da0bd61abe91 did: change API for ECS
Sam <sam@basx.dev>
parents: 271
diff changeset
146 scene.root.add newEntity("cursor", {"mesh": Component(cursormesh)})
38
c3c963e7c1a6 did: tons of stuff, input, refactoring, fix some errors, some template improvment, sorry for super-commit
Sam <sam@basx.dev>
parents:
diff changeset
147
145
a4e6e76128e6 add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 143
diff changeset
148 # shaders
143
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
149 const
316
b145a05c2459 add: changing rendering system, not finished yet, also upgrading to Nim 2
Sam <sam@basx.dev>
parents: 302
diff changeset
150 inputs = @[
163
25d97fa0ad5c fix: API change for shader attributes
Sam <sam@basx.dev>
parents: 146
diff changeset
151 attr[Vec3f]("position"),
210
f3912838cd69 fix: always use rgba
Sam <sam@basx.dev>
parents: 203
diff changeset
152 attr[Vec4f]("color", memoryPerformanceHint=PreferFastWrite),
163
25d97fa0ad5c fix: API change for shader attributes
Sam <sam@basx.dev>
parents: 146
diff changeset
153 attr[Mat4]("transform", memoryPerformanceHint=PreferFastWrite, perInstance=true),
143
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
154 ]
316
b145a05c2459 add: changing rendering system, not finished yet, also upgrading to Nim 2
Sam <sam@basx.dev>
parents: 302
diff changeset
155 intermediate = @[attr[Vec4f]("outcolor")]
143
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
156 uniforms = @[attr[Mat4]("projection")]
316
b145a05c2459 add: changing rendering system, not finished yet, also upgrading to Nim 2
Sam <sam@basx.dev>
parents: 302
diff changeset
157 outputs = @[attr[Vec4f]("color")]
b145a05c2459 add: changing rendering system, not finished yet, also upgrading to Nim 2
Sam <sam@basx.dev>
parents: 302
diff changeset
158 (vertexCode, fragmentCode) = compileVertexFragmentShaderSet(
b145a05c2459 add: changing rendering system, not finished yet, also upgrading to Nim 2
Sam <sam@basx.dev>
parents: 302
diff changeset
159 inputs=inputs,
b145a05c2459 add: changing rendering system, not finished yet, also upgrading to Nim 2
Sam <sam@basx.dev>
parents: 302
diff changeset
160 intermediate=intermediate,
b145a05c2459 add: changing rendering system, not finished yet, also upgrading to Nim 2
Sam <sam@basx.dev>
parents: 302
diff changeset
161 outputs=outputs,
143
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
162 uniforms=uniforms,
316
b145a05c2459 add: changing rendering system, not finished yet, also upgrading to Nim 2
Sam <sam@basx.dev>
parents: 302
diff changeset
163 vertexCode="""outcolor = color; gl_Position = vec4(position, 1) * (transform * Uniforms.projection);""",
b145a05c2459 add: changing rendering system, not finished yet, also upgrading to Nim 2
Sam <sam@basx.dev>
parents: 302
diff changeset
164 fragmentCode="color = outcolor;",
143
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
165 )
145
a4e6e76128e6 add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 143
diff changeset
166
a4e6e76128e6 add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 143
diff changeset
167 # set up rendering
143
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
168 myengine.setRenderer(myengine.gpuDevice.simpleForwardRenderPass(vertexCode, fragmentCode, clearColor=newVec4f(0, 0, 0.5)))
316
b145a05c2459 add: changing rendering system, not finished yet, also upgrading to Nim 2
Sam <sam@basx.dev>
parents: 302
diff changeset
169 myengine.addScene(scene, inputs, @[], transformAttribute="transform")
203
84fd522fdf3f did: update examples to use new API for scene + scene globals
Sam <sam@basx.dev>
parents: 163
diff changeset
170 scene.addShaderGlobal("projection", Unit4f32)
145
a4e6e76128e6 add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 143
diff changeset
171
a4e6e76128e6 add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 143
diff changeset
172 # mainloop
a4e6e76128e6 add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 143
diff changeset
173 while myengine.updateInputs() == Running:
a4e6e76128e6 add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 143
diff changeset
174 if myengine.windowWasResized():
203
84fd522fdf3f did: update examples to use new API for scene + scene globals
Sam <sam@basx.dev>
parents: 163
diff changeset
175 setShaderGlobal(scene, "projection",
247
beb41c93aa3f fix: gltf loading
Sam <sam@basx.dev>
parents: 210
diff changeset
176 ortho(
beb41c93aa3f fix: gltf loading
Sam <sam@basx.dev>
parents: 210
diff changeset
177 0, float32(myengine.getWindow().size[0]),
beb41c93aa3f fix: gltf loading
Sam <sam@basx.dev>
parents: 210
diff changeset
178 0, float32(myengine.getWindow().size[1]),
beb41c93aa3f fix: gltf loading
Sam <sam@basx.dev>
parents: 210
diff changeset
179 0, 1,
203
84fd522fdf3f did: update examples to use new API for scene + scene globals
Sam <sam@basx.dev>
parents: 163
diff changeset
180 )
84fd522fdf3f did: update examples to use new API for scene + scene globals
Sam <sam@basx.dev>
parents: 163
diff changeset
181 )
145
a4e6e76128e6 add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 143
diff changeset
182 let
a4e6e76128e6 add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 143
diff changeset
183 winsize = myengine.getWindow().size
a4e6e76128e6 add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 143
diff changeset
184 center = translate3d(float32(winsize[0]) / 2'f32, float32(winsize[1]) / 2'f32, 0.1'f32)
203
84fd522fdf3f did: update examples to use new API for scene + scene globals
Sam <sam@basx.dev>
parents: 163
diff changeset
185 scene.root.firstWithName("keyboard-center").transform = center
84fd522fdf3f did: update examples to use new API for scene + scene globals
Sam <sam@basx.dev>
parents: 163
diff changeset
186 scene.root.firstWithName("background").transform = scale3d(float32(winsize[0]), float32(winsize[1]), 1'f32)
145
a4e6e76128e6 add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 143
diff changeset
187
a4e6e76128e6 add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 143
diff changeset
188 let mousePos = translate3d(myengine.mousePosition().x + 20, myengine.mousePosition().y + 20, 0'f32)
203
84fd522fdf3f did: update examples to use new API for scene + scene globals
Sam <sam@basx.dev>
parents: 163
diff changeset
189 scene.root.firstWithName("cursor").transform = mousePos
143
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
190
302
da0bd61abe91 did: change API for ECS
Sam <sam@basx.dev>
parents: 271
diff changeset
191 var mesh = Mesh(scene.root.firstWithName("keyboard")["mesh"])
143
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
192 for (index, key) in enumerate(keyIndices):
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
193 if myengine.keyWasPressed(key):
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
194 let baseIndex = uint32(index * 4)
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
195 mesh.updateMeshData("color", baseIndex + 0, activeColor)
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
196 mesh.updateMeshData("color", baseIndex + 1, activeColor)
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
197 mesh.updateMeshData("color", baseIndex + 2, activeColor)
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
198 mesh.updateMeshData("color", baseIndex + 3, activeColor)
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
199 if myengine.keyWasReleased(key):
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
200 let baseIndex = uint32(index * 4)
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
201 mesh.updateMeshData("color", baseIndex + 0, baseColor)
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
202 mesh.updateMeshData("color", baseIndex + 1, baseColor)
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
203 mesh.updateMeshData("color", baseIndex + 2, baseColor)
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
204 mesh.updateMeshData("color", baseIndex + 3, baseColor)
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
205
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
206 myengine.renderScene(scene)
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
207
8ce634aa6ea6 did: update example 04
Sam <sam@basx.dev>
parents: 74
diff changeset
208 myengine.destroy()