Mercurial > games > semicongine
annotate examples/E04_input.nim @ 912:c72b246d8413
add: config loader; fix: circle-mesh
author | Sam <sam@basx.dev> |
---|---|
date | Sun, 10 Mar 2024 20:02:11 +0700 |
parents | 668eed376029 |
children | c66503386e8b |
rev | line source |
---|---|
527 | 1 import std/enumerate |
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 |
525 | 3 import std/typetraits |
4 import std/math | |
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
5 |
732 | 6 import ../src/semicongine |
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
7 |
525 | 8 const |
9 arrow = @[ | |
604 | 10 newVec3f(-1, -1), |
11 newVec3f(1, -1), | |
12 newVec3f(-0.3, -0.3), | |
13 newVec3f(-0.3, -0.3), | |
14 newVec3f(-1, 1), | |
15 newVec3f(-1, -1), | |
525 | 16 ] |
527 | 17 # keyboard layout, specifying rows with key widths, negative numbers are empty spaces |
525 | 18 keyrows = ( |
604 | 19 [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], |
20 [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], | |
21 [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 | 22 [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], |
23 [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], | |
24 [1.5, 1.5, 1.5, 6, 1.5, 1.5, -1.2, 1.5, -0.1, 1.0, 1.0, 1.0], | |
525 | 25 ) |
527 | 26 keyDimension = 50'f32 |
525 | 27 keyGap = 10'f32 |
671 | 28 backgroundColor = newVec4f(0.6705882352941176, 0.6078431372549019, 0.5882352941176471, 1) |
29 baseColor = newVec4f(0.9411764705882353, 0.9058823529411765, 0.8470588235294118, 1) | |
30 activeColor = newVec4f(0.6509803921568628, 0.22745098039215686, 0.3137254901960784, 1) | |
530 | 31 arrow_colors = @[ |
32 baseColor * 0.9'f32, | |
33 baseColor * 0.9'f32, | |
34 baseColor * 0.9'f32, | |
35 baseColor * 0.8'f32, | |
36 baseColor * 0.8'f32, | |
37 baseColor * 0.8'f32, | |
38 ] | |
527 | 39 keyIndices = [ |
40 Escape, F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, PrintScreen, | |
41 ScrollLock, Pause, | |
42 | |
43 NumberRowExtra1, `1`, `2`, `3`, `4`, `5`, `6`, `7`, `8`, `9`, `0`, | |
44 | |
45 NumberRowExtra2, NumberRowExtra3, Backspace, Insert, Home, PageUp, | |
46 Tab, Q, W, Key.E, R, T, Key.Y, U, I, O, P, LetterRow1Extra1, | |
47 LetterRow1Extra2, Delete, End, PageDown, | |
48 | |
49 CapsLock, A, S, D, F, G, H, J, K, L, LetterRow2Extra1, LetterRow2Extra2, | |
50 LetterRow2Extra3, Enter, | |
51 | |
52 ShiftL, Key.Z, Key.X, C, V, B, N, M, LetterRow3Extra1, LetterRow3Extra2, | |
53 LetterRow3Extra3, ShiftR, Up, | |
54 | |
55 CtrlL, SuperL, AltL, Space, AltR, SuperR, CtrlR, Left, Down, Right | |
56 ] | |
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
57 |
606
f41c1b78cf5b
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
604
diff
changeset
|
58 # 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
|
59 var |
664
c33c8e156e3e
did: update examples to use new API for scene + scene globals
Sam <sam@basx.dev>
parents:
624
diff
changeset
|
60 scene: Scene |
604 | 61 keyvertexpos: seq[Vec3f] |
671 | 62 keyvertexcolor: seq[Vec4f] |
527 | 63 keymeshindices: seq[array[3, uint16]] |
604 | 64 rowpos = newVec2f(0, 0) |
525 | 65 i = 0'u16 |
527 | 66 firstRow = true |
67 rowWidth = 0'f32 | |
525 | 68 for row in keyrows.fields: |
69 for key in row: | |
527 | 70 let keySpace = float32(keyDimension * key) |
71 if key > 0: | |
72 if keyIndices[i div 4] == Enter: | |
604 | 73 keyvertexpos.add newVec3f(rowpos[0], rowpos[1] - keyDimension - keyGap) |
74 keyvertexpos.add newVec3f(rowpos[0] + keySpace, rowpos[1] - keyDimension - keyGap) | |
527 | 75 else: |
604 | 76 keyvertexpos.add newVec3f(rowpos[0], rowpos[1]) |
77 keyvertexpos.add newVec3f(rowpos[0] + keySpace, rowpos[1]) | |
78 keyvertexpos.add newVec3f(rowpos[0] + keySpace, rowpos[1] + keyDimension) | |
79 keyvertexpos.add newVec3f(rowpos[0], rowpos[1] + keyDimension) | |
527 | 80 keyvertexcolor.add [baseColor, baseColor, baseColor, baseColor] |
81 keymeshindices.add [i, i + 1, i + 2] | |
82 keymeshindices.add [i + 2, i + 3, i] | |
83 rowpos[0] += keySpace + keyGap | |
84 i += 4 | |
85 else: | |
86 rowpos[0] += -keySpace + keyGap | |
87 if firstRow: | |
88 rowWidth = rowpos[0] | |
525 | 89 rowpos[0] = 0 |
527 | 90 rowpos[1] += keyDimension + keyGap * (if firstRow: 2'f32 else: 1'f32) |
91 firstRow = false | |
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
92 |
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 when isMainModule: |
604 | 95 var myengine = initEngine("Input") |
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
96 |
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
97 # transform the cursor a bit to make it look nice |
525 | 98 let cursorscale = ( |
99 scale2d(20'f32, 20'f32) * | |
100 translate2d(1'f32, 1'f32) * | |
101 rotate2d(-float32(PI) / 4'f32) * | |
102 scale2d(0.5'f32, 1'f32) * | |
103 rotate2d(float32(PI) / 4'f32) | |
104 ) | |
604 | 105 var positions = arrow |
106 for i in 0 ..< positions.len: | |
107 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
|
108 |
606
f41c1b78cf5b
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
604
diff
changeset
|
109 # define mesh objects |
f41c1b78cf5b
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
604
diff
changeset
|
110 var |
797
812b5e28f441
did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents:
777
diff
changeset
|
111 material = Material(name: "default") |
606
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, |
797
812b5e28f441
did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents:
777
diff
changeset
|
115 material=material, |
606
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, |
797
812b5e28f441
did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents:
777
diff
changeset
|
120 indices=keymeshindices, |
812b5e28f441
did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents:
777
diff
changeset
|
121 material=material, |
606
f41c1b78cf5b
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
604
diff
changeset
|
122 ) |
f41c1b78cf5b
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
604
diff
changeset
|
123 backgroundmesh = newMesh( |
f41c1b78cf5b
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
604
diff
changeset
|
124 positions= @[ |
f41c1b78cf5b
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
604
diff
changeset
|
125 newVec3f(0'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, 0'f32), |
f41c1b78cf5b
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
604
diff
changeset
|
127 newVec3f(1'f32, 1'f32), |
f41c1b78cf5b
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
604
diff
changeset
|
128 newVec3f(0'f32, 1'f32), |
f41c1b78cf5b
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
604
diff
changeset
|
129 ], |
f41c1b78cf5b
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
604
diff
changeset
|
130 colors= @[ |
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 backgroundColor, |
f41c1b78cf5b
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
604
diff
changeset
|
135 ], |
f41c1b78cf5b
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
604
diff
changeset
|
136 indices= @[[0'u16, 1'u16, 2'u16], [2'u16, 3'u16, 0'u16]], |
797
812b5e28f441
did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents:
777
diff
changeset
|
137 material=material, |
606
f41c1b78cf5b
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
604
diff
changeset
|
138 ) |
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
139 |
606
f41c1b78cf5b
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
604
diff
changeset
|
140 # define mesh objects |
797
812b5e28f441
did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents:
777
diff
changeset
|
141 var keyboard_center = translate( |
604 | 142 -float32(rowWidth) / 2'f32, |
143 -float32(tupleLen(keyRows) * (keyDimension + keyGap) - keyGap) / 2'f32, | |
144 0'f32 | |
145 ) | |
797
812b5e28f441
did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents:
777
diff
changeset
|
146 scene = Scene(name: "scene", meshes: @[backgroundmesh, keyboardmesh, cursormesh]) |
522
f2c97bdbb0b3
did: rename and update older demos to work with new APIs
Sam <sam@basx.dev>
parents:
diff
changeset
|
147 |
606
f41c1b78cf5b
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
604
diff
changeset
|
148 # shaders |
604 | 149 const |
797
812b5e28f441
did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents:
777
diff
changeset
|
150 shaderConfiguration = createShaderConfiguration( |
812b5e28f441
did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents:
777
diff
changeset
|
151 inputs=[ |
812b5e28f441
did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents:
777
diff
changeset
|
152 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
|
153 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
|
154 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
|
155 ], |
812b5e28f441
did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents:
777
diff
changeset
|
156 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
|
157 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
|
158 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
|
159 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
|
160 fragmentCode="color = outcolor;", |
604 | 161 ) |
606
f41c1b78cf5b
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
604
diff
changeset
|
162 |
f41c1b78cf5b
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
604
diff
changeset
|
163 # 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
|
164 myengine.initRenderer({"default": shaderConfiguration}.toTable) |
664
c33c8e156e3e
did: update examples to use new API for scene + scene globals
Sam <sam@basx.dev>
parents:
624
diff
changeset
|
165 scene.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
|
166 myengine.addScene(scene) |
812b5e28f441
did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents:
777
diff
changeset
|
167 myengine.hideSystemCursor() |
606
f41c1b78cf5b
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
604
diff
changeset
|
168 |
f41c1b78cf5b
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
604
diff
changeset
|
169 # mainloop |
f41c1b78cf5b
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
604
diff
changeset
|
170 while myengine.updateInputs() == Running: |
f41c1b78cf5b
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
604
diff
changeset
|
171 if myengine.windowWasResized(): |
797
812b5e28f441
did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents:
777
diff
changeset
|
172 scene.setShaderGlobal("projection", |
708 | 173 ortho( |
174 0, float32(myengine.getWindow().size[0]), | |
175 0, float32(myengine.getWindow().size[1]), | |
176 0, 1, | |
664
c33c8e156e3e
did: update examples to use new API for scene + scene globals
Sam <sam@basx.dev>
parents:
624
diff
changeset
|
177 ) |
c33c8e156e3e
did: update examples to use new API for scene + scene globals
Sam <sam@basx.dev>
parents:
624
diff
changeset
|
178 ) |
606
f41c1b78cf5b
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
604
diff
changeset
|
179 let |
f41c1b78cf5b
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
604
diff
changeset
|
180 winsize = myengine.getWindow().size |
797
812b5e28f441
did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents:
777
diff
changeset
|
181 center = translate(float32(winsize[0]) / 2'f32, float32(winsize[1]) / 2'f32, 0.1'f32) |
812b5e28f441
did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents:
777
diff
changeset
|
182 keyboardmesh.transform = keyboard_center * center |
812b5e28f441
did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents:
777
diff
changeset
|
183 backgroundmesh.transform = scale(float32(winsize[0]), float32(winsize[1]), 1'f32) |
606
f41c1b78cf5b
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
604
diff
changeset
|
184 |
797
812b5e28f441
did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents:
777
diff
changeset
|
185 let mousePos = translate(myengine.mousePosition().x + 20, myengine.mousePosition().y + 20, 0'f32) |
812b5e28f441
did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents:
777
diff
changeset
|
186 cursormesh.transform = mousePos |
604 | 187 |
188 for (index, key) in enumerate(keyIndices): | |
189 if myengine.keyWasPressed(key): | |
797
812b5e28f441
did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents:
777
diff
changeset
|
190 let baseIndex = index * 4 |
800
668eed376029
add: nice mesh API, fix: copying of whole scenedata all the time
Sam <sam@basx.dev>
parents:
797
diff
changeset
|
191 keyboardmesh["color", baseIndex + 0] = activeColor |
668eed376029
add: nice mesh API, fix: copying of whole scenedata all the time
Sam <sam@basx.dev>
parents:
797
diff
changeset
|
192 keyboardmesh["color", baseIndex + 1] = activeColor |
668eed376029
add: nice mesh API, fix: copying of whole scenedata all the time
Sam <sam@basx.dev>
parents:
797
diff
changeset
|
193 keyboardmesh["color", baseIndex + 2] = activeColor |
668eed376029
add: nice mesh API, fix: copying of whole scenedata all the time
Sam <sam@basx.dev>
parents:
797
diff
changeset
|
194 keyboardmesh["color", baseIndex + 3] = activeColor |
604 | 195 if myengine.keyWasReleased(key): |
797
812b5e28f441
did: update examples to work with improved scenegraph/material api, notice removed complexity!
Sam <sam@basx.dev>
parents:
777
diff
changeset
|
196 let baseIndex = index * 4 |
800
668eed376029
add: nice mesh API, fix: copying of whole scenedata all the time
Sam <sam@basx.dev>
parents:
797
diff
changeset
|
197 keyboardmesh["color", baseIndex + 0] = baseColor |
668eed376029
add: nice mesh API, fix: copying of whole scenedata all the time
Sam <sam@basx.dev>
parents:
797
diff
changeset
|
198 keyboardmesh["color", baseIndex + 1] = baseColor |
668eed376029
add: nice mesh API, fix: copying of whole scenedata all the time
Sam <sam@basx.dev>
parents:
797
diff
changeset
|
199 keyboardmesh["color", baseIndex + 2] = baseColor |
668eed376029
add: nice mesh API, fix: copying of whole scenedata all the time
Sam <sam@basx.dev>
parents:
797
diff
changeset
|
200 keyboardmesh["color", baseIndex + 3] = baseColor |
604 | 201 |
202 myengine.renderScene(scene) | |
203 | |
204 myengine.destroy() |