annotate examples/E04_input.nim @ 608:1698c8ab9203

del: legacy
author Sam <sam@basx.dev>
date Thu, 27 Apr 2023 00:24:10 +0700
parents 64eb53f81cf6
children 3f13de7d8ec4
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
527
de433a47e635 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 525
diff changeset
1 import std/enumerate
522
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
2 import std/strutils
525
0285ff2281b1 add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 522
diff changeset
3 import std/typetraits
522
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
4 import std/times
525
0285ff2281b1 add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 522
diff changeset
5 import std/math
522
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
6
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
7 import semicongine
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
8
525
0285ff2281b1 add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 522
diff changeset
9 const
0285ff2281b1 add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 522
diff changeset
10 arrow = @[
604
fdd80220b5ff did: update example 04
Sam <sam@basx.dev>
parents: 535
diff changeset
11 newVec3f(-1, -1),
fdd80220b5ff did: update example 04
Sam <sam@basx.dev>
parents: 535
diff changeset
12 newVec3f(1, -1),
fdd80220b5ff did: update example 04
Sam <sam@basx.dev>
parents: 535
diff changeset
13 newVec3f(-0.3, -0.3),
fdd80220b5ff did: update example 04
Sam <sam@basx.dev>
parents: 535
diff changeset
14 newVec3f(-0.3, -0.3),
fdd80220b5ff did: update example 04
Sam <sam@basx.dev>
parents: 535
diff changeset
15 newVec3f(-1, 1),
fdd80220b5ff did: update example 04
Sam <sam@basx.dev>
parents: 535
diff changeset
16 newVec3f(-1, -1),
525
0285ff2281b1 add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 522
diff changeset
17 ]
527
de433a47e635 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 525
diff changeset
18 # keyboard layout, specifying rows with key widths, negative numbers are empty spaces
525
0285ff2281b1 add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 522
diff changeset
19 keyrows = (
604
fdd80220b5ff did: update example 04
Sam <sam@basx.dev>
parents: 535
diff changeset
20 [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],
fdd80220b5ff did: update example 04
Sam <sam@basx.dev>
parents: 535
diff changeset
21 [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],
fdd80220b5ff did: update example 04
Sam <sam@basx.dev>
parents: 535
diff changeset
22 [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],
527
de433a47e635 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 525
diff changeset
23 [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],
de433a47e635 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 525
diff changeset
24 [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],
de433a47e635 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 525
diff changeset
25 [1.5, 1.5, 1.5, 6, 1.5, 1.5, -1.2, 1.5, -0.1, 1.0, 1.0, 1.0],
525
0285ff2281b1 add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 522
diff changeset
26 )
527
de433a47e635 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 525
diff changeset
27 keyDimension = 50'f32
525
0285ff2281b1 add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 522
diff changeset
28 keyGap = 10'f32
604
fdd80220b5ff did: update example 04
Sam <sam@basx.dev>
parents: 535
diff changeset
29 backgroundColor = newVec3f(0.6705882352941176, 0.6078431372549019, 0.5882352941176471)
fdd80220b5ff did: update example 04
Sam <sam@basx.dev>
parents: 535
diff changeset
30 baseColor = newVec3f(0.9411764705882353, 0.9058823529411765, 0.8470588235294118'f32)
fdd80220b5ff did: update example 04
Sam <sam@basx.dev>
parents: 535
diff changeset
31 activeColor = newVec3f(0.6509803921568628'f32, 0.22745098039215686, 0.3137254901960784'f32)
530
cfa230e1f3b9 fix: mouse coord, colors
Sam <sam@basx.dev>
parents: 528
diff changeset
32 arrow_colors = @[
cfa230e1f3b9 fix: mouse coord, colors
Sam <sam@basx.dev>
parents: 528
diff changeset
33 baseColor * 0.9'f32,
cfa230e1f3b9 fix: mouse coord, colors
Sam <sam@basx.dev>
parents: 528
diff changeset
34 baseColor * 0.9'f32,
cfa230e1f3b9 fix: mouse coord, colors
Sam <sam@basx.dev>
parents: 528
diff changeset
35 baseColor * 0.9'f32,
cfa230e1f3b9 fix: mouse coord, colors
Sam <sam@basx.dev>
parents: 528
diff changeset
36 baseColor * 0.8'f32,
cfa230e1f3b9 fix: mouse coord, colors
Sam <sam@basx.dev>
parents: 528
diff changeset
37 baseColor * 0.8'f32,
cfa230e1f3b9 fix: mouse coord, colors
Sam <sam@basx.dev>
parents: 528
diff changeset
38 baseColor * 0.8'f32,
cfa230e1f3b9 fix: mouse coord, colors
Sam <sam@basx.dev>
parents: 528
diff changeset
39 ]
527
de433a47e635 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 525
diff changeset
40 keyIndices = [
de433a47e635 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 525
diff changeset
41 Escape, F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, PrintScreen,
de433a47e635 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 525
diff changeset
42 ScrollLock, Pause,
de433a47e635 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 525
diff changeset
43
de433a47e635 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 525
diff changeset
44 NumberRowExtra1, `1`, `2`, `3`, `4`, `5`, `6`, `7`, `8`, `9`, `0`,
de433a47e635 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 525
diff changeset
45
de433a47e635 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 525
diff changeset
46 NumberRowExtra2, NumberRowExtra3, Backspace, Insert, Home, PageUp,
de433a47e635 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 525
diff changeset
47 Tab, Q, W, Key.E, R, T, Key.Y, U, I, O, P, LetterRow1Extra1,
de433a47e635 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 525
diff changeset
48 LetterRow1Extra2, Delete, End, PageDown,
de433a47e635 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 525
diff changeset
49
de433a47e635 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 525
diff changeset
50 CapsLock, A, S, D, F, G, H, J, K, L, LetterRow2Extra1, LetterRow2Extra2,
de433a47e635 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 525
diff changeset
51 LetterRow2Extra3, Enter,
de433a47e635 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 525
diff changeset
52
de433a47e635 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 525
diff changeset
53 ShiftL, Key.Z, Key.X, C, V, B, N, M, LetterRow3Extra1, LetterRow3Extra2,
de433a47e635 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 525
diff changeset
54 LetterRow3Extra3, ShiftR, Up,
de433a47e635 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 525
diff changeset
55
de433a47e635 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 525
diff changeset
56 CtrlL, SuperL, AltL, Space, AltR, SuperR, CtrlR, Left, Down, Right
de433a47e635 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 525
diff changeset
57 ]
522
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
58
606
f41c1b78cf5b add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 604
diff changeset
59 # build keyboard and cursor meshes
522
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
60 var
604
fdd80220b5ff did: update example 04
Sam <sam@basx.dev>
parents: 535
diff changeset
61 scene: Entity
fdd80220b5ff did: update example 04
Sam <sam@basx.dev>
parents: 535
diff changeset
62 keyvertexpos: seq[Vec3f]
fdd80220b5ff did: update example 04
Sam <sam@basx.dev>
parents: 535
diff changeset
63 keyvertexcolor: seq[Vec3f]
527
de433a47e635 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 525
diff changeset
64 keymeshindices: seq[array[3, uint16]]
604
fdd80220b5ff did: update example 04
Sam <sam@basx.dev>
parents: 535
diff changeset
65 rowpos = newVec2f(0, 0)
525
0285ff2281b1 add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 522
diff changeset
66 i = 0'u16
527
de433a47e635 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 525
diff changeset
67 firstRow = true
de433a47e635 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 525
diff changeset
68 rowWidth = 0'f32
525
0285ff2281b1 add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 522
diff changeset
69 for row in keyrows.fields:
0285ff2281b1 add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 522
diff changeset
70 for key in row:
527
de433a47e635 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 525
diff changeset
71 let keySpace = float32(keyDimension * key)
de433a47e635 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 525
diff changeset
72 if key > 0:
de433a47e635 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 525
diff changeset
73 if keyIndices[i div 4] == Enter:
604
fdd80220b5ff did: update example 04
Sam <sam@basx.dev>
parents: 535
diff changeset
74 keyvertexpos.add newVec3f(rowpos[0], rowpos[1] - keyDimension - keyGap)
fdd80220b5ff did: update example 04
Sam <sam@basx.dev>
parents: 535
diff changeset
75 keyvertexpos.add newVec3f(rowpos[0] + keySpace, rowpos[1] - keyDimension - keyGap)
527
de433a47e635 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 525
diff changeset
76 else:
604
fdd80220b5ff did: update example 04
Sam <sam@basx.dev>
parents: 535
diff changeset
77 keyvertexpos.add newVec3f(rowpos[0], rowpos[1])
fdd80220b5ff did: update example 04
Sam <sam@basx.dev>
parents: 535
diff changeset
78 keyvertexpos.add newVec3f(rowpos[0] + keySpace, rowpos[1])
fdd80220b5ff did: update example 04
Sam <sam@basx.dev>
parents: 535
diff changeset
79 keyvertexpos.add newVec3f(rowpos[0] + keySpace, rowpos[1] + keyDimension)
fdd80220b5ff did: update example 04
Sam <sam@basx.dev>
parents: 535
diff changeset
80 keyvertexpos.add newVec3f(rowpos[0], rowpos[1] + keyDimension)
527
de433a47e635 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 525
diff changeset
81 keyvertexcolor.add [baseColor, baseColor, baseColor, baseColor]
de433a47e635 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 525
diff changeset
82 keymeshindices.add [i, i + 1, i + 2]
de433a47e635 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 525
diff changeset
83 keymeshindices.add [i + 2, i + 3, i]
de433a47e635 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 525
diff changeset
84 rowpos[0] += keySpace + keyGap
de433a47e635 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 525
diff changeset
85 i += 4
de433a47e635 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 525
diff changeset
86 else:
de433a47e635 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 525
diff changeset
87 rowpos[0] += -keySpace + keyGap
de433a47e635 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 525
diff changeset
88 if firstRow:
de433a47e635 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 525
diff changeset
89 rowWidth = rowpos[0]
525
0285ff2281b1 add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 522
diff changeset
90 rowpos[0] = 0
527
de433a47e635 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 525
diff changeset
91 rowpos[1] += keyDimension + keyGap * (if firstRow: 2'f32 else: 1'f32)
de433a47e635 add: nicer keyboard demo
Sam <sam@basx.dev>
parents: 525
diff changeset
92 firstRow = false
522
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
93
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
94
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
95 when isMainModule:
604
fdd80220b5ff did: update example 04
Sam <sam@basx.dev>
parents: 535
diff changeset
96 var myengine = initEngine("Input")
522
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
97
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
98 # transform the cursor a bit to make it look nice
525
0285ff2281b1 add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 522
diff changeset
99 let cursorscale = (
0285ff2281b1 add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 522
diff changeset
100 scale2d(20'f32, 20'f32) *
0285ff2281b1 add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 522
diff changeset
101 translate2d(1'f32, 1'f32) *
0285ff2281b1 add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 522
diff changeset
102 rotate2d(-float32(PI) / 4'f32) *
0285ff2281b1 add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 522
diff changeset
103 scale2d(0.5'f32, 1'f32) *
0285ff2281b1 add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 522
diff changeset
104 rotate2d(float32(PI) / 4'f32)
0285ff2281b1 add: keyboard/mouse inpute test
Sam <sam@basx.dev>
parents: 522
diff changeset
105 )
604
fdd80220b5ff did: update example 04
Sam <sam@basx.dev>
parents: 535
diff changeset
106 var positions = arrow
fdd80220b5ff did: update example 04
Sam <sam@basx.dev>
parents: 535
diff changeset
107 for i in 0 ..< positions.len:
fdd80220b5ff did: update example 04
Sam <sam@basx.dev>
parents: 535
diff changeset
108 positions[i] = cursorscale * newVec3f(positions[i].x, positions[i].y)
522
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
109
606
f41c1b78cf5b add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 604
diff changeset
110 # define mesh objects
f41c1b78cf5b add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 604
diff changeset
111 var
f41c1b78cf5b add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 604
diff changeset
112 cursormesh = newMesh(
f41c1b78cf5b add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 604
diff changeset
113 positions=positions,
f41c1b78cf5b add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 604
diff changeset
114 colors=arrow_colors,
f41c1b78cf5b add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 604
diff changeset
115 instanceCount=1,
f41c1b78cf5b add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 604
diff changeset
116 )
f41c1b78cf5b add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 604
diff changeset
117 keyboardmesh = newMesh(
f41c1b78cf5b add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 604
diff changeset
118 positions=keyvertexpos,
f41c1b78cf5b add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 604
diff changeset
119 colors=keyvertexcolor,
f41c1b78cf5b add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 604
diff changeset
120 indices=keymeshindices
f41c1b78cf5b add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 604
diff changeset
121 )
f41c1b78cf5b add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 604
diff changeset
122 backgroundmesh = newMesh(
f41c1b78cf5b add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 604
diff changeset
123 positions= @[
f41c1b78cf5b add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 604
diff changeset
124 newVec3f(0'f32, 0'f32),
f41c1b78cf5b add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 604
diff changeset
125 newVec3f(1'f32, 0'f32),
f41c1b78cf5b add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 604
diff changeset
126 newVec3f(1'f32, 1'f32),
f41c1b78cf5b add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 604
diff changeset
127 newVec3f(0'f32, 1'f32),
f41c1b78cf5b add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 604
diff changeset
128 ],
f41c1b78cf5b add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 604
diff changeset
129 colors= @[
f41c1b78cf5b add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 604
diff changeset
130 backgroundColor,
f41c1b78cf5b add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 604
diff changeset
131 backgroundColor,
f41c1b78cf5b add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 604
diff changeset
132 backgroundColor,
f41c1b78cf5b add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 604
diff changeset
133 backgroundColor,
f41c1b78cf5b add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 604
diff changeset
134 ],
f41c1b78cf5b add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 604
diff changeset
135 indices= @[[0'u16, 1'u16, 2'u16], [2'u16, 3'u16, 0'u16]],
f41c1b78cf5b add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 604
diff changeset
136 )
522
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
137
606
f41c1b78cf5b add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 604
diff changeset
138 # define mesh objects
604
fdd80220b5ff did: update example 04
Sam <sam@basx.dev>
parents: 535
diff changeset
139 scene = newEntity("scene")
fdd80220b5ff did: update example 04
Sam <sam@basx.dev>
parents: 535
diff changeset
140 scene.add newEntity("background", backgroundmesh)
fdd80220b5ff did: update example 04
Sam <sam@basx.dev>
parents: 535
diff changeset
141 let keyboard = newEntity("keyboard", keyboardmesh)
fdd80220b5ff did: update example 04
Sam <sam@basx.dev>
parents: 535
diff changeset
142 keyboard.transform = translate3d(
fdd80220b5ff did: update example 04
Sam <sam@basx.dev>
parents: 535
diff changeset
143 -float32(rowWidth) / 2'f32,
fdd80220b5ff did: update example 04
Sam <sam@basx.dev>
parents: 535
diff changeset
144 -float32(tupleLen(keyRows) * (keyDimension + keyGap) - keyGap) / 2'f32,
fdd80220b5ff did: update example 04
Sam <sam@basx.dev>
parents: 535
diff changeset
145 0'f32
fdd80220b5ff did: update example 04
Sam <sam@basx.dev>
parents: 535
diff changeset
146 )
fdd80220b5ff did: update example 04
Sam <sam@basx.dev>
parents: 535
diff changeset
147 scene.add newEntity("keyboard-center", keyboard)
fdd80220b5ff did: update example 04
Sam <sam@basx.dev>
parents: 535
diff changeset
148 scene.add newEntity("cursor", cursormesh)
522
f2c97bdbb0b3 did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff changeset
149
606
f41c1b78cf5b add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 604
diff changeset
150 # shaders
604
fdd80220b5ff did: update example 04
Sam <sam@basx.dev>
parents: 535
diff changeset
151 const
fdd80220b5ff did: update example 04
Sam <sam@basx.dev>
parents: 535
diff changeset
152 vertexInput = @[
fdd80220b5ff did: update example 04
Sam <sam@basx.dev>
parents: 535
diff changeset
153 attr[Vec3f]("position", memoryLocation=VRAM),
fdd80220b5ff did: update example 04
Sam <sam@basx.dev>
parents: 535
diff changeset
154 attr[Vec3f]("color", memoryLocation=VRAMVisible),
fdd80220b5ff did: update example 04
Sam <sam@basx.dev>
parents: 535
diff changeset
155 attr[Mat4]("transform", memoryLocation=VRAMVisible, perInstance=true),
fdd80220b5ff did: update example 04
Sam <sam@basx.dev>
parents: 535
diff changeset
156 ]
fdd80220b5ff did: update example 04
Sam <sam@basx.dev>
parents: 535
diff changeset
157 vertexOutput = @[attr[Vec3f]("outcolor")]
fdd80220b5ff did: update example 04
Sam <sam@basx.dev>
parents: 535
diff changeset
158 uniforms = @[attr[Mat4]("projection")]
fdd80220b5ff did: update example 04
Sam <sam@basx.dev>
parents: 535
diff changeset
159 fragOutput = @[attr[Vec4f]("color")]
fdd80220b5ff did: update example 04
Sam <sam@basx.dev>
parents: 535
diff changeset
160 vertexCode = compileGlslShader(
fdd80220b5ff did: update example 04
Sam <sam@basx.dev>
parents: 535
diff changeset
161 stage=VK_SHADER_STAGE_VERTEX_BIT,
fdd80220b5ff did: update example 04
Sam <sam@basx.dev>
parents: 535
diff changeset
162 inputs=vertexInput,
fdd80220b5ff did: update example 04
Sam <sam@basx.dev>
parents: 535
diff changeset
163 uniforms=uniforms,
fdd80220b5ff did: update example 04
Sam <sam@basx.dev>
parents: 535
diff changeset
164 outputs=vertexOutput,
fdd80220b5ff did: update example 04
Sam <sam@basx.dev>
parents: 535
diff changeset
165 main="""outcolor = color; gl_Position = vec4(position, 1) * (transform * Uniforms.projection);"""
fdd80220b5ff did: update example 04
Sam <sam@basx.dev>
parents: 535
diff changeset
166 )
fdd80220b5ff did: update example 04
Sam <sam@basx.dev>
parents: 535
diff changeset
167 fragmentCode = compileGlslShader(
fdd80220b5ff did: update example 04
Sam <sam@basx.dev>
parents: 535
diff changeset
168 stage=VK_SHADER_STAGE_FRAGMENT_BIT,
fdd80220b5ff did: update example 04
Sam <sam@basx.dev>
parents: 535
diff changeset
169 inputs=vertexOutput,
fdd80220b5ff did: update example 04
Sam <sam@basx.dev>
parents: 535
diff changeset
170 uniforms=uniforms,
fdd80220b5ff did: update example 04
Sam <sam@basx.dev>
parents: 535
diff changeset
171 outputs=fragOutput,
fdd80220b5ff did: update example 04
Sam <sam@basx.dev>
parents: 535
diff changeset
172 main="color = vec4(outcolor, 1);"
fdd80220b5ff did: update example 04
Sam <sam@basx.dev>
parents: 535
diff changeset
173 )
606
f41c1b78cf5b add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 604
diff changeset
174
f41c1b78cf5b add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 604
diff changeset
175 # set up rendering
604
fdd80220b5ff did: update example 04
Sam <sam@basx.dev>
parents: 535
diff changeset
176 myengine.setRenderer(myengine.gpuDevice.simpleForwardRenderPass(vertexCode, fragmentCode, clearColor=newVec4f(0, 0, 0.5)))
fdd80220b5ff did: update example 04
Sam <sam@basx.dev>
parents: 535
diff changeset
177 myengine.addScene(scene, vertexInput, transformAttribute="transform")
fdd80220b5ff did: update example 04
Sam <sam@basx.dev>
parents: 535
diff changeset
178 var projection = initShaderGlobal("projection", Unit4f32)
fdd80220b5ff did: update example 04
Sam <sam@basx.dev>
parents: 535
diff changeset
179 scene.add projection
606
f41c1b78cf5b add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 604
diff changeset
180
f41c1b78cf5b add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 604
diff changeset
181 # mainloop
f41c1b78cf5b add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 604
diff changeset
182 while myengine.updateInputs() == Running:
f41c1b78cf5b add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 604
diff changeset
183 if myengine.windowWasResized():
f41c1b78cf5b add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 604
diff changeset
184 setValue[Mat4](projection.value, ortho[float32](
f41c1b78cf5b add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 604
diff changeset
185 0'f32, float32(myengine.getWindow().size[0]),
f41c1b78cf5b add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 604
diff changeset
186 0'f32, float32(myengine.getWindow().size[1]),
f41c1b78cf5b add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 604
diff changeset
187 0'f32, 1'f32,
f41c1b78cf5b add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 604
diff changeset
188 ))
f41c1b78cf5b add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 604
diff changeset
189 let
f41c1b78cf5b add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 604
diff changeset
190 winsize = myengine.getWindow().size
f41c1b78cf5b add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 604
diff changeset
191 center = translate3d(float32(winsize[0]) / 2'f32, float32(winsize[1]) / 2'f32, 0.1'f32)
f41c1b78cf5b add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 604
diff changeset
192 scene.firstWithName("keyboard-center").transform = center
f41c1b78cf5b add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 604
diff changeset
193 scene.firstWithName("background").transform = scale3d(float32(winsize[0]), float32(winsize[1]), 1'f32)
f41c1b78cf5b add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 604
diff changeset
194
f41c1b78cf5b add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 604
diff changeset
195 let mousePos = translate3d(myengine.mousePosition().x + 20, myengine.mousePosition().y + 20, 0'f32)
604
fdd80220b5ff did: update example 04
Sam <sam@basx.dev>
parents: 535
diff changeset
196 scene.firstWithName("cursor").transform = mousePos
fdd80220b5ff did: update example 04
Sam <sam@basx.dev>
parents: 535
diff changeset
197
fdd80220b5ff did: update example 04
Sam <sam@basx.dev>
parents: 535
diff changeset
198 var mesh = Mesh(scene.firstWithName("keyboard").components[0])
fdd80220b5ff did: update example 04
Sam <sam@basx.dev>
parents: 535
diff changeset
199 for (index, key) in enumerate(keyIndices):
fdd80220b5ff did: update example 04
Sam <sam@basx.dev>
parents: 535
diff changeset
200 if myengine.keyWasPressed(key):
fdd80220b5ff did: update example 04
Sam <sam@basx.dev>
parents: 535
diff changeset
201 let baseIndex = uint32(index * 4)
fdd80220b5ff did: update example 04
Sam <sam@basx.dev>
parents: 535
diff changeset
202 mesh.updateMeshData("color", baseIndex + 0, activeColor)
fdd80220b5ff did: update example 04
Sam <sam@basx.dev>
parents: 535
diff changeset
203 mesh.updateMeshData("color", baseIndex + 1, activeColor)
fdd80220b5ff did: update example 04
Sam <sam@basx.dev>
parents: 535
diff changeset
204 mesh.updateMeshData("color", baseIndex + 2, activeColor)
fdd80220b5ff did: update example 04
Sam <sam@basx.dev>
parents: 535
diff changeset
205 mesh.updateMeshData("color", baseIndex + 3, activeColor)
fdd80220b5ff did: update example 04
Sam <sam@basx.dev>
parents: 535
diff changeset
206 if myengine.keyWasReleased(key):
fdd80220b5ff did: update example 04
Sam <sam@basx.dev>
parents: 535
diff changeset
207 let baseIndex = uint32(index * 4)
fdd80220b5ff did: update example 04
Sam <sam@basx.dev>
parents: 535
diff changeset
208 mesh.updateMeshData("color", baseIndex + 0, baseColor)
fdd80220b5ff did: update example 04
Sam <sam@basx.dev>
parents: 535
diff changeset
209 mesh.updateMeshData("color", baseIndex + 1, baseColor)
fdd80220b5ff did: update example 04
Sam <sam@basx.dev>
parents: 535
diff changeset
210 mesh.updateMeshData("color", baseIndex + 2, baseColor)
fdd80220b5ff did: update example 04
Sam <sam@basx.dev>
parents: 535
diff changeset
211 mesh.updateMeshData("color", baseIndex + 3, baseColor)
fdd80220b5ff did: update example 04
Sam <sam@basx.dev>
parents: 535
diff changeset
212
fdd80220b5ff did: update example 04
Sam <sam@basx.dev>
parents: 535
diff changeset
213 myengine.renderScene(scene)
fdd80220b5ff did: update example 04
Sam <sam@basx.dev>
parents: 535
diff changeset
214
fdd80220b5ff did: update example 04
Sam <sam@basx.dev>
parents: 535
diff changeset
215 myengine.destroy()