@kmamal/sdl 0.6.7 → 0.6.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -13,7 +13,11 @@ Prebuilt binaries are available for x64 architectures, and arm-based Macs.
13
13
 
14
14
  #### Canvas, WebGL, and WebGPU
15
15
 
16
- One goal of this project is to allow using Canvas (the 2D rendering context API), WebGL and WebGPU without a browser. You can already use [@kmamal/gl](https://github.com/kmamal/headless-gl#readme) to draw on windows using WebGL. WebGPU is also working, but it's not been fully integrated: you can use [@kmamal/gpu](https://github.com/kmamal/gpu#readme) for WebGPU compute and render, but it can't yet render directly to windows; that will come in a future version. Right now you have to go through an intermediate buffer. Same with the Canvas API, where you can use the [`canvas`](https://www.npmjs.com/package/canvas) package to draw to a buffer and then draw that to a window.
16
+ One goal of this project is to allow using Canvas (the 2D rendering context API), WebGL, and WebGPU without a browser.
17
+ You can already use [@kmamal/gl](https://github.com/kmamal/headless-gl#readme) to draw on windows using WebGL.
18
+ WebGPU is also working, but it's not been fully integrated: you can use [@kmamal/gpu](https://github.com/kmamal/gpu#readme) for compute and render, but it can't yet render directly to windows; that will come in a future version.
19
+ Right now you have to go through an intermediate buffer.
20
+ Same with the Canvas API, where you can use the [canvas](https://www.npmjs.com/package/canvas) package to draw to a buffer and then draw that to a window.
17
21
 
18
22
 
19
23
  ## Installation
@@ -29,21 +33,23 @@ npm install @kmamal/sdl
29
33
  (But if things go wrong do look over [here](#building-from-source))
30
34
 
31
35
 
32
- ## "Hello, World!" example
36
+ ## Examples
37
+
38
+ ### "Hello, World!"
33
39
 
34
40
  ```js
35
- const sdl = require('@kmamal/sdl')
41
+ import sdl from '@kmamal/sdl'
36
42
 
37
43
  const window = sdl.video.createWindow({ title: "Hello, World!" })
38
44
  window.on('*', console.log)
39
45
  ```
40
46
 
41
- ## Canvas example
47
+ ### Canvas
42
48
  ```js
43
49
  import sdl from '@kmamal/sdl'
44
50
  import canvas from 'canvas'
45
51
 
46
- const window = sdl.video.createWindow({ resizable: true })
52
+ const window = sdl.video.createWindow({ title: "Canvas" })
47
53
  const { width, height } = window
48
54
  const canvas = canvas.createCanvas(width, height)
49
55
  const ctx = canvas.getContext('2d')
@@ -55,15 +61,12 @@ const buffer = canvas.toBuffer('raw')
55
61
  window.render(width, height, width * 4, 'bgra32', buffer)
56
62
  ```
57
63
 
58
- ## WebGL example
64
+ ### WebGL
59
65
  ```js
60
- const sdl = require('@kmamal/sdl')
61
- const createContext = require('@kmamal/gl')
66
+ import sdl from '@kmamal/sdl'
67
+ import createContext from '@kmamal/gl'
62
68
 
63
- const window = sdl.video.createWindow({
64
- title: "WebGL",
65
- opengl: true,
66
- })
69
+ const window = sdl.video.createWindow({title: "WebGL", opengl: true })
67
70
  const { width, height, native } = window
68
71
  const gl = createContext(width, height, { window: native })
69
72
 
@@ -73,7 +76,7 @@ gl.clear(gl.COLOR_BUFFER_BIT)
73
76
  gl.swap()
74
77
  ```
75
78
 
76
- There are more examples [in the `examples/` folder](https://github.com/kmamal/node-sdl/tree/master/examples), covering several common use cases, including how to draw using Canvas and WebGL, as well as how to package the application for distribution.
79
+ There are more examples [in the `examples/` folder](https://github.com/kmamal/node-sdl/tree/master/examples).
77
80
 
78
81
 
79
82
  # API Reference
@@ -179,7 +182,7 @@ There are more examples [in the `examples/` folder](https://github.com/kmamal/no
179
182
  * [Event: 'deviceAdd'](#joystick-event-device-add)
180
183
  * [Event: 'deviceRemove'](#joystick-event-device-remove)
181
184
  * [sdl.joystick.devices](#sdljoystickdevices)
182
- * [sdl.joystick.openDevice(device)](#sdljoystickopendevicedevice-options)
185
+ * [sdl.joystick.openDevice(device)](#sdljoystickopendevicedevice)
183
186
  * [class JoystickInstance](#class-joystickinstance)
184
187
  * [Event: 'axisMotion'](#joystick-instance-event-axismotion)
185
188
  * [Event: 'ballMotion'](#event-ballmotion)
@@ -195,6 +198,7 @@ There are more examples [in the `examples/` folder](https://github.com/kmamal/no
195
198
  * [joystickInstance.buttons](#joystickinstancebuttons)
196
199
  * [joystickInstance.hats](#joystickinstancehats)
197
200
  * [joystickInstance.power](#joystickinstancepower)
201
+ * [joystickInstance.setPlayer(index)](#joystickinstancesetplayerindex)
198
202
  * [joystickInstance.hasLed](#joystickinstancehasled)
199
203
  * [joystickInstance.setLed(red, green, blue)](#joystickinstancesetledred-green-blue)
200
204
  * [joystickInstance.hasRumble](#joystickinstancehasrumble)
@@ -210,7 +214,7 @@ There are more examples [in the `examples/` folder](https://github.com/kmamal/no
210
214
  * [Event: 'deviceRemove'](#controller-event-device-remove)
211
215
  * [sdl.controller.addMappings(mappings)](#sdlcontrolleraddmappingsmappings)
212
216
  * [sdl.controller.devices](#sdlcontrollerdevices)
213
- * [sdl.controller.openDevice(device)](#sdlcontrolleropendevicedevice-options)
217
+ * [sdl.controller.openDevice(device)](#sdlcontrolleropendevicedevice)
214
218
  * [class ControllerInstance](#class-controllerinstance)
215
219
  * [Event: 'axisMotion'](#controller-instance-event-axismotion)
216
220
  * [Event: 'buttonDown'](#controller-instance-event-buttondown)
@@ -223,6 +227,7 @@ There are more examples [in the `examples/` folder](https://github.com/kmamal/no
223
227
  * [controllerInstance.axes](#controllerinstanceaxes)
224
228
  * [controllerInstance.buttons](#controllerinstancebuttons)
225
229
  * [controllerInstance.power](#controllerinstancepower)
230
+ * [controllerInstance.setPlayer(index)](#controllerinstancesetplayerindex)
226
231
  * [controllerInstance.hasLed](#controllerinstancehasled)
227
232
  * [controllerInstance.setLed(red, green, blue)](#controllerinstancesetledred-green-blue)
228
233
  * [controllerInstance.hasRumble](#controllerinstancehasrumble)
@@ -1397,7 +1402,7 @@ Sample output:
1397
1402
  ]
1398
1403
  ```
1399
1404
 
1400
- ### sdl.joystick.openDevice(device[, options])
1405
+ ### sdl.joystick.openDevice(device)
1401
1406
 
1402
1407
  * `device: <object>` An object from [`sdl.joystick.devices`](#sdljoystickdevices) that should be opened.
1403
1408
  * Returns: [`<joystickInstance>`](#class-joystickinstance) an object representing the opened joystick device instance.
@@ -1407,7 +1412,7 @@ Initializes a joystick device and returns a corresponding instance.
1407
1412
  ## class JoystickInstance
1408
1413
 
1409
1414
  This class is not directly exposed by the API so you can't use it with the `new` operator.
1410
- Instead, objects returned by [`sdl.joystick.openDevice()`](#sdljoystickopendevicedevice-options) are of this type.
1415
+ Instead, objects returned by [`sdl.joystick.openDevice()`](#sdljoystickopendevicedevice) are of this type.
1411
1416
 
1412
1417
  <a id="joystick-instance-event-axismotion"></a>
1413
1418
 
@@ -1509,6 +1514,12 @@ An array of values, each corresponding to the position of one of the joystick's
1509
1514
 
1510
1515
  The current power level of the joystick device.
1511
1516
 
1517
+ ### joystickInstance.setPlayer(index)
1518
+
1519
+ * `index: <number>` The player index to assign to the joystick.
1520
+
1521
+ Sets the player index of this joystick.
1522
+
1512
1523
  ### joystickInstance.hasLed
1513
1524
 
1514
1525
  * `<boolean>`
@@ -1584,7 +1595,8 @@ With a pure joystick instance it's impossible to know which axis or button corre
1584
1595
 
1585
1596
  Because controllers are an abstraction over joysticks, they operate on the same set of devices (if a joystick device and a controller device have the same id, then they refer to the same underlying physical device).
1586
1597
  For a joystick device to also be available as a controller device it needs a "mapping".
1587
- A mapping is a string that consists of the device's name, it's GUID, and a series of pairings between one joystick axis/button and the corresponding controller axis/button name. See the sample output [`here`](#sdlcontrollerdevices) for an example.
1598
+ A mapping is a string that consists of the device's name, it's GUID, and a series of pairings between one joystick axis/button and the corresponding controller axis/button name.
1599
+ See the sample output [`here`](#sdlcontrollerdevices) for an example.
1588
1600
  SDL has pretty good default controller mappings, but if you need more, there's a community sourced database available on https://github.com/gabomdq/SDL_GameControllerDB.
1589
1601
  You can add them via:
1590
1602
  ```js
@@ -1653,7 +1665,7 @@ Sample output:
1653
1665
  ]
1654
1666
  ```
1655
1667
 
1656
- ### sdl.controller.openDevice(device[, options])
1668
+ ### sdl.controller.openDevice(device)
1657
1669
 
1658
1670
  * `device: <object>` An object from [`sdl.controller.devices`](#sdlcontrollerdevices) that should be opened.
1659
1671
  * Returns: [`<ControllerInstance>`](#class-controllerinstance) an object representing the opened controller device instance.
@@ -1663,7 +1675,7 @@ Initializes an controller device and returns a corresponding instance.
1663
1675
  ## class ControllerInstance
1664
1676
 
1665
1677
  This class is not directly exposed by the API so you can't use it with the `new` operator.
1666
- Instead, objects returned by [`sdl.controller.openDevice()`](#sdlcontrolleropendevicedevice-options) are of this type.
1678
+ Instead, objects returned by [`sdl.controller.openDevice()`](#sdlcontrolleropendevicedevice) are of this type.
1667
1679
 
1668
1680
  <a id="controller-instance-event-axismotion"></a>
1669
1681
 
@@ -1722,17 +1734,42 @@ The controller's serial number, or `null` if it's not available.
1722
1734
 
1723
1735
  ### controllerInstance.axes
1724
1736
 
1725
- * `<number>[]`
1737
+ * `axes: <object>`
1738
+ * `leftStickX: <number>` Left stick horizontal position
1739
+ * `leftStickY: <number>` Left stick vertical position
1740
+ * `rightStickX: <number>` Right stick horizontal position
1741
+ * `rightStickY: <number>` Right stick vertical position
1742
+ * `leftTrigger: <number>` Left trigger position
1743
+ * `rightTrigger: <number>` Right trigger position
1726
1744
 
1727
- An array of values, each corresponding to the position of one of the controller's axes.
1745
+ An object mapping each axis of the controller's axes to its position.
1728
1746
  The values are normalized from `-1` to `+1`.
1729
1747
  It may be necessary to impose certain tolerances on these values to account for jitter.
1730
1748
 
1731
1749
  ### controllerInstance.buttons
1732
1750
 
1733
- * `<boolean>[]`
1734
-
1735
- An array of values, each corresponding to the state of one of the controller's buttons.
1751
+ * `buttons: <object>`
1752
+ * `dpadLeft: <boolean>` D-Pad left pressed
1753
+ * `dpadRight: <boolean>` D-Pad right pressed
1754
+ * `dpadUp: <boolean>` D-Pad up pressed
1755
+ * `dpadDown: <boolean>` D-Pad down pressed
1756
+ * `a: <boolean>` A button pressed
1757
+ * `b: <boolean>` B button pressed
1758
+ * `x: <boolean>` X button pressed
1759
+ * `y: <boolean>` Y button pressed
1760
+ * `guide: <boolean>` Middle button pressed
1761
+ * `back: <boolean>` Back button pressed
1762
+ * `start: <boolean>` Start button pressed
1763
+ * `leftStick: <boolean>` Left stick pressed
1764
+ * `rightStick: <boolean>` Right stick pressed
1765
+ * `leftShoulder: <boolean>` Left shoulder button pressed
1766
+ * `rightShoulder: <boolean>` Right shoulder button pressed
1767
+ * `paddle1: <boolean>` Paddle 1 pressed
1768
+ * `paddle2: <boolean>` Paddle 2 pressed
1769
+ * `paddle3: <boolean>` Paddle 3 pressed
1770
+ * `paddle4: <boolean>` Paddle 4 pressed
1771
+
1772
+ An object mapping each of the controller's buttons to a boolean value.
1736
1773
  The values will be `true` for buttons that are pressed and `false` otherwise.
1737
1774
 
1738
1775
  ### controllerInstance.power
@@ -1741,6 +1778,12 @@ The values will be `true` for buttons that are pressed and `false` otherwise.
1741
1778
 
1742
1779
  The current power level of the controller device.
1743
1780
 
1781
+ ### controllerInstance.setPlayer(index)
1782
+
1783
+ * `index: <number>` The player index to assign to the controller.
1784
+
1785
+ Sets the player index of this controller.
1786
+
1744
1787
  ### controllerInstance.hasLed
1745
1788
 
1746
1789
  * `<boolean>`
@@ -2236,7 +2279,8 @@ You will need to have `cmake` installed for that to work.
2236
2279
 
2237
2280
  ---
2238
2281
 
2239
- You could also have found your way to the "Building from source" section because you are trying to contribute to this package. There are some npm scripts in `package.json` that could be of use to you:
2282
+ You could also have found your way to the "Building from source" section because you are trying to contribute to this package.
2283
+ There are some npm scripts in `package.json` that could be of use to you:
2240
2284
  * `npm run clean` deletes all folders that are created during the build, as well as `node_modules`.
2241
2285
  * `npm run download-release` downloads the prebuilt binaries. This is the first thing the install script tries to do.
2242
2286
  * `npm run download-sdl` downloads the SDL headers and libraries from `@kmamal/build-sdl` so you can compile against them in later steps. This is the second step in the install script, after `download-release` has failed.
@@ -2245,6 +2289,6 @@ You could also have found your way to the "Building from source" section because
2245
2289
 
2246
2290
  The SDL headers and libs get downloaded to `sdl/`, the build happens in `build/`, and the final binaries get collected into `dist/`.
2247
2291
 
2248
- The way I normally work is I run `npm run clean` to start fresh, then run `NODE_SDL_FROM_SOURCE=1 npm i` once to prepare everything, then as I make changes I run `npm run build` ro re-build the package.
2292
+ The way I normally work is I run `npm run clean` to start fresh, then run `NODE_SDL_FROM_SOURCE=1 npm i` once to prepare everything, then as I make changes I run `npm run build` to re-build the package.
2249
2293
 
2250
2294
  Have fun!
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "0.6.7",
2
+ "version": "0.6.9",
3
3
  "name": "@kmamal/sdl",
4
4
  "description": "SDL bindings for Node.js",
5
5
  "keywords": [
package/scripts/test.mjs CHANGED
@@ -1,9 +1,11 @@
1
1
  import Path from 'node:path'
2
2
  import { execSync } from 'node:child_process'
3
- import { fstat } from 'node:fs'
3
+ import Fs from 'node:fs'
4
+ import { fileURLToPath } from 'url'
5
+ const __dirname = Path.dirname(fileURLToPath(import.meta.url))
4
6
 
5
- process.chdir(Path.resolve(__dirname, '../tests'))
6
- const files = await fstat.readdir('.')
7
+ process.chdir(Path.join(__dirname, '../tests'))
8
+ const files = await Fs.promises.readdir('.')
7
9
  for (const name of files) {
8
10
  if (!name.endsWith('.test.js')) { continue }
9
11
  execSync(`node ${name}`, { stdio: 'inherit' })
@@ -2,7 +2,7 @@ const Fs = require('node:fs')
2
2
  const Path = require('node:path')
3
3
 
4
4
  const dir = {}
5
- dir.root = Path.resolve(__dirname, '../..')
5
+ dir.root = Path.join(__dirname, '../..')
6
6
  dir.sdl = Path.join(dir.root, 'sdl')
7
7
  dir.build = Path.join(dir.root, 'build')
8
8
  dir.dist = Path.join(dir.root, 'dist')
@@ -1,9 +1,21 @@
1
1
  const Globals = require('../globals')
2
+ const Bindings = require('../bindings')
3
+ const { make: makeAudioDevice } = require('./device')
2
4
  const { EventsViaPoll } = require('../events/events-via-poll')
3
5
  const { AudioPlaybackInstance } = require('./audio-playback-instance')
4
6
  const { AudioRecordingInstance } = require('./audio-recording-instance')
5
7
  const { AudioFormatHelpers } = require('./format-helpers')
6
8
 
9
+ Globals.audioDevices.playback = Bindings.audio_getDevices(false)
10
+ for (const audioDevice of Globals.audioDevices.playback) {
11
+ makeAudioDevice(audioDevice)
12
+ }
13
+ Globals.audioDevices.recording = Bindings.audio_getDevices(true)
14
+ for (const audioDevice of Globals.audioDevices.recording) {
15
+ makeAudioDevice(audioDevice)
16
+ }
17
+
18
+
7
19
  const validEvents = [ 'deviceAdd', 'deviceRemove' ]
8
20
 
9
21
  const audio = new class extends EventsViaPoll {
@@ -50,6 +50,15 @@ class ControllerInstance extends EventsViaPoll {
50
50
  return Enums.powerLevelNames[power]
51
51
  }
52
52
 
53
+ setPlayer (player) {
54
+ if (this._closed) { throw Object.assign(new Error("instance is closed"), { id: this._device.id }) }
55
+
56
+ if (!Number.isFinite(player)) { throw Object.assign(new Error("player must be a number"), { player }) }
57
+ if (player < 0) { throw Object.assign(new Error("player must be non-negative"), { player }) }
58
+
59
+ Bindings.joystick_setPlayer(this._device.id, player)
60
+ }
61
+
53
62
  get hasLed () { return this._hasLed }
54
63
  setLed (red, green, blue) {
55
64
  if (this._closed) { throw Object.assign(new Error("instance is closed"), { id: this._device.id }) }
@@ -61,7 +70,7 @@ class ControllerInstance extends EventsViaPoll {
61
70
  if (!Number.isFinite(blue)) { throw Object.assign(new Error("blue must be a number"), { blue }) }
62
71
  if (blue < 0 || blue > 1) { throw Object.assign(new Error("blue must be between 0 and 1"), { blue }) }
63
72
 
64
- Bindings.joystick_rumble(this._device.id, red, green, blue)
73
+ Bindings.joystick_setLed(this._device.id, red, green, blue)
65
74
  }
66
75
 
67
76
  get hasRumble () { return this._hasRumble }
@@ -1,8 +1,19 @@
1
1
  const Bindings = require('../bindings')
2
2
  const Globals = require('../globals')
3
+ const {
4
+ make: makeControllerDevice,
5
+ filter: filterControllerDevice,
6
+ } = require('./device')
3
7
  const { EventsViaPoll } = require('../events/events-via-poll')
4
8
  const { ControllerInstance } = require('./controller-instance')
5
9
 
10
+ Globals.controllerDevices = Bindings.joystick_getDevices(false)
11
+ .filter(filterControllerDevice)
12
+ for (const controllerDevice of Globals.controllerDevices) {
13
+ makeControllerDevice(controllerDevice)
14
+ }
15
+
16
+
6
17
  const validEvents = [ 'deviceAdd', 'deviceRemove', 'deviceRemap' ]
7
18
 
8
19
  const controller = new class extends EventsViaPoll {
@@ -36,12 +36,16 @@ const reconcileDevices = (emitter, mainList, currList, compare) => {
36
36
  currDevice = currList[++currIndex]
37
37
  } else if (cmp < 0) {
38
38
  mainList.splice(mainIndex, 1)
39
- emitter.emit('deviceRemove', { device: mainDevice })
39
+ const type = 'deviceRemove'
40
+ const event = { type, device: mainDevice }
41
+ emitter.emit(type, event)
40
42
  mainDevice = mainList[mainIndex]
41
43
  } else {
42
44
  mainList.splice(mainIndex, 0, currDevice)
43
45
  mainDevice = mainList[++mainIndex]
44
- emitter.emit('deviceAdd', { device: currDevice })
46
+ const type = 'deviceAdd'
47
+ const event = { type, device: currDevice }
48
+ emitter.emit(type, event)
45
49
  currDevice = currList[++currIndex]
46
50
  }
47
51
  }
@@ -49,12 +53,16 @@ const reconcileDevices = (emitter, mainList, currList, compare) => {
49
53
  if (mainIndex < mainList.length) {
50
54
  while (mainIndex < mainList.length) {
51
55
  [ mainDevice ] = mainList.splice(mainIndex, 1)
52
- emitter.emit('deviceRemove', { device: mainDevice })
56
+ const type = 'deviceRemove'
57
+ const event = { type, device: mainDevice }
58
+ emitter.emit(type, event)
53
59
  }
54
60
  } else {
55
61
  while (currIndex < currList.length) {
56
62
  mainList.push(currDevice)
57
- emitter.emit('deviceAdd', { device: currDevice })
63
+ const type = 'deviceAdd'
64
+ const event = { type, device: currDevice }
65
+ emitter.emit(type, event)
58
66
  currDevice = currList[++currIndex]
59
67
  }
60
68
  }
@@ -67,7 +75,7 @@ const handleEvents = () => {
67
75
  const { family, type } = event
68
76
  if (family === undefined || type === undefined) { continue }
69
77
  delete event.family
70
- delete event.type
78
+ event.type = Enums.eventTypeNames[type]
71
79
 
72
80
  switch (family) {
73
81
  case Enums.eventFamily.app: {
@@ -140,25 +148,23 @@ const handleEvents = () => {
140
148
  case Enums.eventType.close: {
141
149
  let shouldPrevent = false
142
150
  const prevent = () => { shouldPrevent = true }
143
- window.emit('beforeClose', { prevent })
151
+ const type2 = 'beforeClose'
152
+ const event2 = { type: type2, prevent }
153
+ window.emit(type2, event2)
144
154
  if (shouldPrevent) { continue }
145
155
  } break
146
156
 
147
157
  default: continue
148
158
  }
149
159
 
150
- if (Object.keys(event).length > 0) {
151
- window.emit(Enums.eventTypeNames[type], event)
152
- } else {
153
- window.emit(Enums.eventTypeNames[type])
154
- }
160
+ window.emit(event.type, event)
155
161
 
156
162
  if (type === Enums.eventType.close) { window.destroy() }
157
163
  } break
158
164
 
159
165
  case Enums.eventFamily.keyboard: {
160
166
  const { windowId } = event
161
- delete event.window
167
+ delete event.windowId
162
168
 
163
169
  const window = Globals.windows.all.get(windowId)
164
170
  if (!window) { continue }
@@ -166,11 +172,7 @@ const handleEvents = () => {
166
172
  const { key } = event
167
173
  event.key = mapping[key] ?? (key?.length === 1 ? key : null)
168
174
 
169
- if (Object.keys(event).length > 0) {
170
- window.emit(Enums.eventTypeNames[type], event)
171
- } else {
172
- window.emit(Enums.eventTypeNames[type])
173
- }
175
+ window.emit(event.type, event)
174
176
  } break
175
177
 
176
178
  case Enums.eventFamily.mouse:
@@ -182,11 +184,7 @@ const handleEvents = () => {
182
184
  const window = Globals.windows.all.get(windowId)
183
185
  if (!window) { continue }
184
186
 
185
- if (Object.keys(event).length > 0) {
186
- window.emit(Enums.eventTypeNames[type], event)
187
- } else {
188
- window.emit(Enums.eventTypeNames[type])
189
- }
187
+ window.emit(event.type, event)
190
188
  } break
191
189
 
192
190
  case Enums.eventFamily.joystickDevice: {
@@ -249,7 +247,7 @@ const handleEvents = () => {
249
247
  case Enums.eventType.axisMotion: {
250
248
  for (const joystickInstance of collection) {
251
249
  joystickInstance._axes[event.axis] = event.value
252
- joystickInstance.emit('axisMotion', event)
250
+ joystickInstance.emit(event.type, event)
253
251
  }
254
252
  } break
255
253
 
@@ -258,20 +256,20 @@ const handleEvents = () => {
258
256
  const ball = joystickInstance._balls[event.ball]
259
257
  ball.x = event.x
260
258
  ball.y = event.y
261
- joystickInstance.emit('ballMotion', event)
259
+ joystickInstance.emit(event.type, event)
262
260
  }
263
261
  } break
264
262
 
265
263
  case Enums.eventType.buttonDown: {
266
264
  for (const joystickInstance of collection) {
267
265
  joystickInstance._buttons[event.button] = true
268
- joystickInstance.emit('buttonDown', event)
266
+ joystickInstance.emit(event.type, event)
269
267
  }
270
268
  } break
271
269
  case Enums.eventType.buttonUp: {
272
270
  for (const joystickInstance of collection) {
273
271
  joystickInstance._buttons[event.button] = false
274
- joystickInstance.emit('buttonUp', event)
272
+ joystickInstance.emit(event.type, event)
275
273
  }
276
274
  } break
277
275
 
@@ -279,7 +277,7 @@ const handleEvents = () => {
279
277
  event.value = Enums.hatPositionNames[event.value]
280
278
  for (const joystickInstance of collection) {
281
279
  joystickInstance._hats[event.hat] = event.value
282
- joystickInstance.emit('hatMotion', event)
280
+ joystickInstance.emit(event.type, event)
283
281
  }
284
282
  } break
285
283
 
@@ -299,7 +297,7 @@ const handleEvents = () => {
299
297
  event.axis = Enums.controllerAxisNames[event.axis]
300
298
  for (const controllerInstance of collection.values()) {
301
299
  controllerInstance._axes[event.axis] = event.value
302
- controllerInstance.emit('axisMotion', event)
300
+ controllerInstance.emit(event.type, event)
303
301
  }
304
302
  } break
305
303
 
@@ -307,7 +305,7 @@ const handleEvents = () => {
307
305
  event.button = Enums.controllerButtonNames[event.button]
308
306
  for (const controllerInstance of collection.values()) {
309
307
  controllerInstance._axes[event.button] = true
310
- controllerInstance.emit('buttonDown', event)
308
+ controllerInstance.emit(event.type, event)
311
309
  }
312
310
  } break
313
311
 
@@ -315,7 +313,7 @@ const handleEvents = () => {
315
313
  event.button = Enums.controllerButtonNames[event.button]
316
314
  for (const controllerInstance of collection.values()) {
317
315
  controllerInstance._axes[event.button] = true
318
- controllerInstance.emit('buttonUp', event)
316
+ controllerInstance.emit(event.type, event)
319
317
  }
320
318
  } break
321
319
 
@@ -327,7 +325,7 @@ const handleEvents = () => {
327
325
  for (const controllerInstance of collection.values()) {
328
326
  Object.assign(controllerInstance._axes, axes)
329
327
  Object.assign(controllerInstance._buttons, buttons)
330
- controllerInstance.emit('remap', event)
328
+ controllerInstance.emit(event.type, event)
331
329
  }
332
330
  } break
333
331
 
@@ -363,7 +361,7 @@ const handleEvents = () => {
363
361
  } break
364
362
 
365
363
  case Enums.eventFamily.clipboard: {
366
- clipboardModule.emit(Enums.eventTypeNames[type])
364
+ clipboardModule.emit(event.type, event)
367
365
  } break
368
366
 
369
367
  default: continue
@@ -1,19 +1,22 @@
1
1
  const Globals = require('../globals')
2
2
  const { sdl } = require('../sdl')
3
3
 
4
+
4
5
  const maybeTriggerQuit = () => {
5
6
  if (Globals.windows.all.size > 0) { return }
6
7
 
7
8
  let shouldPrevent = false
8
9
  const prevent = () => { shouldPrevent = true }
9
10
 
10
- const type2 = 'beforeQuit'
11
- const event2 = { prevent }
12
- sdl.emit(type2, event2)
11
+ const type1 = 'beforeQuit'
12
+ const event1 = { type: type1, prevent }
13
+ sdl.emit(type1, event1)
13
14
 
14
15
  if (shouldPrevent) { return }
15
16
 
16
- sdl.emit('quit')
17
+ const type2 = 'quit'
18
+ const event2 = { type: type2 }
19
+ sdl.emit(type2, event2)
17
20
  process.exit()
18
21
  }
19
22
 
@@ -1,7 +1,15 @@
1
+ const Bindings = require('../bindings')
1
2
  const Globals = require('../globals')
3
+ const { make: makeJoystickDevice } = require('./device')
2
4
  const { EventsViaPoll } = require('../events/events-via-poll')
3
5
  const { JoystickInstance } = require('./joystick-instance')
4
6
 
7
+ Globals.joystickDevices = Bindings.joystick_getDevices(false)
8
+ for (const joystickDevice of Globals.joystickDevices) {
9
+ makeJoystickDevice(joystickDevice)
10
+ }
11
+
12
+
5
13
  const validEvents = [ 'deviceAdd', 'deviceRemove' ]
6
14
 
7
15
  const joystick = new class extends EventsViaPoll {
@@ -55,6 +55,15 @@ class JoystickInstance extends EventsViaPoll {
55
55
  return Enums.powerLevelNames[power]
56
56
  }
57
57
 
58
+ setPlayer (player) {
59
+ if (this._closed) { throw Object.assign(new Error("instance is closed"), { id: this._device.id }) }
60
+
61
+ if (!Number.isFinite(player)) { throw Object.assign(new Error("player must be a number"), { player }) }
62
+ if (player < 0) { throw Object.assign(new Error("player must be non-negative"), { player }) }
63
+
64
+ Bindings.joystick_setPlayer(this._device.id, player)
65
+ }
66
+
58
67
  get hasLed () { return this._hasLed }
59
68
  setLed (red, green, blue) {
60
69
  if (this._closed) { throw Object.assign(new Error("instance is closed"), { id: this._device.id }) }
@@ -66,7 +75,7 @@ class JoystickInstance extends EventsViaPoll {
66
75
  if (!Number.isFinite(blue)) { throw Object.assign(new Error("blue must be a number"), { blue }) }
67
76
  if (blue < 0 || blue > 1) { throw Object.assign(new Error("blue must be between 0 and 1"), { blue }) }
68
77
 
69
- Bindings.joystick_rumble(this._device.id, red, green, blue)
78
+ Bindings.joystick_setLed(this._device.id, red, green, blue)
70
79
  }
71
80
 
72
81
  get hasRumble () { return this._hasRumble }
@@ -122,7 +122,7 @@ class Window extends EventsViaPoll {
122
122
 
123
123
  // This also keeps Node.js alive while windows are open
124
124
  this.on('close', () => {
125
- this.removeAllListeners()
125
+ process.nextTick(() => { this.removeAllListeners() })
126
126
  })
127
127
 
128
128
  // Manually emit an initial resize event for convenience
@@ -1715,7 +1715,7 @@ window_render (
1715
1715
  RETURN_ERROR("SDL_CreateRGBSurfaceWithFormatFrom(%d, %d, %d, %d, %d) error: %s\n", window_id, texture_width, texture_height, texture_stride, texture_format, SDL_GetError());
1716
1716
  }
1717
1717
 
1718
- if (SDL_BlitSurface(src_surface, NULL, dst_surface, NULL) != 0) {
1718
+ if (SDL_BlitScaled(src_surface, NULL, dst_surface, NULL) != 0) {
1719
1719
  RETURN_ERROR("SDL_BlitSurface(%d) error: %s\n", window_id, SDL_GetError());
1720
1720
  }
1721
1721
 
@@ -1,17 +1,18 @@
1
1
 
2
2
  export namespace Events {
3
3
 
4
- interface BaseEvent {}
4
+ interface BaseEvent { readonly type: string }
5
5
 
6
6
  type PreventCallback = () => void
7
7
 
8
8
  namespace App {
9
9
 
10
10
  interface BeforeQuit extends BaseEvent {
11
+ readonly type: 'beforeQuit'
11
12
  readonly prevent: PreventCallback
12
13
  }
13
14
 
14
- // interface Quit extends BaseEvent {}
15
+ interface Quit extends BaseEvent { readonly type: 'quit' }
15
16
 
16
17
  }
17
18
 
@@ -31,65 +32,75 @@ export namespace Events {
31
32
  readonly numlock: number
32
33
  }
33
34
 
35
+ interface KeyDownEvent extends KeyEvent {
36
+ readonly type: 'keyDown'
37
+ readonly repeat: number
38
+ }
39
+ interface KeyUpEvent extends KeyEvent { readonly type: 'keyUp' }
40
+
41
+ interface TextInput extends WindowEvent {
42
+ readonly type: 'textInput'
43
+ readonly text: string
44
+ }
45
+
34
46
  interface MouseEvent extends WindowEvent {
35
47
  readonly x: number
36
48
  readonly y: number
37
49
  readonly touch: boolean
38
50
  }
39
51
 
52
+ interface MouseMoveEvent extends MouseEvent { readonly type: 'mouseMove' }
53
+
40
54
  interface MouseButtonEvent extends MouseEvent {
41
55
  readonly button: number
42
56
  }
43
57
 
44
- // interface Show extends WindowEvent {}
45
- // interface Hide extends WindowEvent {}
46
- // interface Expose extends WindowEvent {}
47
- // interface Minimize extends WindowEvent {}
48
- // interface Maximize extends WindowEvent {}
49
- // interface Restore extends WindowEvent {}
58
+ interface MouseButtonDownEvent extends MouseButtonEvent { readonly type: 'mouseButtonDown' }
59
+ interface MouseButtonUpEvent extends MouseButtonEvent { readonly type: 'mouseButtonUp' }
60
+
61
+ interface MouseWheelEvent extends MouseEvent {
62
+ readonly type: 'flipped'
63
+ readonly dx: number
64
+ readonly dy: number
65
+ readonly flipped: boolean
66
+ }
67
+
68
+ interface Show extends WindowEvent { readonly type: 'show' }
69
+ interface Hide extends WindowEvent { readonly type: 'hide' }
70
+ interface Expose extends WindowEvent { readonly type: 'expose' }
71
+ interface Minimize extends WindowEvent { readonly type: 'minimize' }
72
+ interface Maximize extends WindowEvent { readonly type: 'maximize' }
73
+ interface Restore extends WindowEvent { readonly type: 'restore' }
50
74
  interface Move extends WindowEvent {
75
+ readonly type: 'move'
51
76
  readonly x: number,
52
77
  readonly y: number
53
78
  }
54
79
  interface Resize extends WindowEvent {
80
+ readonly type: 'resize'
55
81
  readonly width: number,
56
82
  readonly height: number
57
83
  }
58
- // interface Focus extends WindowEvent {}
59
- // interface Blur extends WindowEvent {}
60
- // interface Hover extends WindowEvent {}
61
- // interface Leave extends WindowEvent {}
84
+ interface Focus extends WindowEvent { readonly type: 'focus' }
85
+ interface Blur extends WindowEvent { readonly type: 'blur' }
86
+ interface Hover extends WindowEvent { readonly type: 'hover' }
87
+ interface Leave extends WindowEvent { readonly type: 'leave' }
62
88
  interface BeforeClose extends WindowEvent {
89
+ readonly type: 'beforeClose'
63
90
  readonly prevent: PreventCallback
64
91
  }
65
- // interface Close extends WindowEvent {}
92
+ interface Close extends WindowEvent { readonly type: 'close' }
66
93
 
67
- interface KeyDown extends KeyEvent {
68
- readonly repeat: number
69
- }
70
- interface KeyUp extends KeyEvent {}
71
-
72
- interface TextInput extends WindowEvent {
73
- readonly text: string
74
- }
75
-
76
- interface MouseButtonDown extends MouseButtonEvent {}
77
- interface MouseButtonUp extends MouseButtonEvent {}
78
- interface MouseMove extends MouseEvent {}
79
- interface MouseWheel extends MouseEvent {
80
- readonly dx: number
81
- readonly dy: number
82
- readonly flipped: boolean
83
- }
84
-
85
- // interface DropBegin extends WindowEvent {}
94
+ interface DropBegin extends WindowEvent { readonly type: 'dropBegin' }
86
95
  interface DropText extends WindowEvent {
96
+ readonly type: 'dropText'
87
97
  readonly text: string
88
98
  }
89
99
  interface DropFile extends WindowEvent {
100
+ readonly type: 'dropFile'
90
101
  readonly file: string
91
102
  }
92
- // interface DropComplete extends WindowEvent {}
103
+ interface DropComplete extends WindowEvent { readonly type: 'dropComplete' }
93
104
 
94
105
  }
95
106
 
@@ -99,34 +110,37 @@ export namespace Events {
99
110
  readonly device: Sdl.Joystick.Device
100
111
  }
101
112
 
102
- interface DeviceAdd extends DeviceEvent {}
103
- interface DeviceRemove extends DeviceEvent {}
113
+ interface DeviceAdd extends DeviceEvent { readonly type: 'deviceAdd' }
114
+ interface DeviceRemove extends DeviceEvent { readonly type: 'deviceRemove' }
104
115
 
105
116
  interface JoystickEvent extends BaseEvent {}
106
117
 
107
118
  interface AxisMotion extends JoystickEvent {
108
- axis: number,
109
- value: number
119
+ readonly type: 'axisMotion'
120
+ readonly axis: number,
121
+ readonly value: number
110
122
  }
111
123
 
112
124
  interface BallMotion extends JoystickEvent {
113
- ball: number,
114
- x: number
115
- y: number
125
+ readonly type: 'ballMotion'
126
+ readonly ball: number,
127
+ readonly x: number
128
+ readonly y: number
116
129
  }
117
130
 
118
131
  interface ButtonEvent extends JoystickEvent {
119
- button: number
132
+ readonly button: number
120
133
  }
121
- interface ButtonDown extends ButtonEvent {}
122
- interface ButtonUp extends ButtonEvent {}
134
+ interface ButtonDown extends ButtonEvent { readonly type: 'buttonDown' }
135
+ interface ButtonUp extends ButtonEvent { readonly type: 'buttonUp' }
123
136
 
124
137
  interface HatMotion extends JoystickEvent {
125
- hat: number,
126
- value: Sdl.Joystick.HatPosition
138
+ readonly type: 'hatMotion'
139
+ readonly hat: number,
140
+ readonly value: Sdl.Joystick.HatPosition
127
141
  }
128
142
 
129
- // interface Close extends JoystickEvent {}
143
+ interface Close extends JoystickEvent { readonly type: 'close' }
130
144
 
131
145
  }
132
146
 
@@ -136,25 +150,26 @@ export namespace Events {
136
150
  readonly device: Sdl.Controller.Device
137
151
  }
138
152
 
139
- interface DeviceAdd extends DeviceEvent {}
140
- interface DeviceRemove extends DeviceEvent {}
153
+ interface DeviceAdd extends DeviceEvent { readonly type: 'deviceAdd' }
154
+ interface DeviceRemove extends DeviceEvent { readonly type: 'deviceRemove' }
141
155
 
142
156
  interface ControllerEvent extends BaseEvent {}
143
157
 
144
158
  interface AxisMotion extends ControllerEvent {
145
- axis: number,
146
- value: number
159
+ readonly type: 'axisMotion'
160
+ readonly axis: number,
161
+ readonly value: number
147
162
  }
148
163
 
149
164
  interface ButtonEvent extends ControllerEvent {
150
- button: number
165
+ readonly button: number
151
166
  }
152
- interface ButtonDown extends ButtonEvent {}
153
- interface ButtonUp extends ButtonEvent {}
167
+ interface ButtonDown extends ButtonEvent { readonly type: 'buttonDown' }
168
+ interface ButtonUp extends ButtonEvent { readonly type: 'buttonUp' }
154
169
 
155
- interface Remap extends ControllerEvent {}
170
+ interface Remap extends ControllerEvent { readonly type: 'remap' }
156
171
 
157
- // interface Close extends ControllerEvent {}
172
+ interface Close extends ControllerEvent { readonly type: 'close' }
158
173
 
159
174
  }
160
175
 
@@ -164,16 +179,17 @@ export namespace Events {
164
179
  readonly device: Sdl.Audio.Device
165
180
  }
166
181
 
167
- interface DeviceAdd extends DeviceEvent {}
168
- interface DeviceRemove extends DeviceEvent {}
182
+ interface DeviceAdd extends DeviceEvent { readonly type: 'deviceAdd' }
183
+ interface DeviceRemove extends DeviceEvent { readonly type: 'deviceRemove' }
184
+
185
+ interface AudioEvent extends BaseEvent {}
169
186
 
170
- // interface AudioEvent extends BaseEvent {}
171
- // interface Close extends AudioEvent {}
187
+ interface Close extends AudioEvent { readonly type: 'close' }
172
188
  }
173
189
 
174
190
  namespace Clipboard {
175
191
 
176
- interface Update extends BaseEvent {}
192
+ interface Update extends BaseEvent { readonly type: 'update' }
177
193
 
178
194
  }
179
195
  }
@@ -271,20 +287,20 @@ export namespace Sdl {
271
287
  }
272
288
 
273
289
  class Window {
274
- on (event: 'show', listener: () => void): this
275
- on (event: 'hide', listener: () => void): this
276
- on (event: 'expose', listener: () => void): this
277
- on (event: 'minimize', listener: () => void): this
278
- on (event: 'maximize', listener: () => void): this
279
- on (event: 'restore', listener: () => void): this
290
+ on (event: 'show', listener: (event: Events.Window.Show) => void): this
291
+ on (event: 'hide', listener: (event: Events.Window.Hide) => void): this
292
+ on (event: 'expose', listener: (event: Events.Window.Expose) => void): this
293
+ on (event: 'minimize', listener: (event: Events.Window.Minimize) => void): this
294
+ on (event: 'maximize', listener: (event: Events.Window.Maximize) => void): this
295
+ on (event: 'restore', listener: (event: Events.Window.Restore) => void): this
280
296
  on (event: 'move', listener: (event: Events.Window.Move) => void): this
281
297
  on (event: 'resize', listener: (event: Events.Window.Resize) => void): this
282
- on (event: 'focus', listener: () => void): this
283
- on (event: 'blur', listener: () => void): this
284
- on (event: 'hover', listener: () => void): this
285
- on (event: 'leave', listener: () => void): this
298
+ on (event: 'focus', listener: (event: Event.Window.Focus) => void): this
299
+ on (event: 'blur', listener: (event: Event.Window.Blur) => void): this
300
+ on (event: 'hover', listener: (event: Event.Window.Hover) => void): this
301
+ on (event: 'leave', listener: (event: Event.Window.Leave) => void): this
286
302
  on (event: 'beforeClose', listener: (event: Events.Window.BeforeClose) => void): this
287
- on (event: 'close', listener: () => void): this
303
+ on (event: 'close', listener: (event: Event.Window.Close) => void): this
288
304
  on (event: 'keyDown', listener: (event: Events.Window.KeyDown) => void): this
289
305
  on (event: 'keyUp', listener: (event: Events.Window.KeyUp) => void): this
290
306
  on (event: 'textInput', listener: (event: Events.Window.TextInput) => void): this
@@ -292,10 +308,10 @@ export namespace Sdl {
292
308
  on (event: 'mouseButtonUp', listener: (event: Events.Window.MouseButtonUp) => void): this
293
309
  on (event: 'mouseMove', listener: (event: Events.Window.MouseMove) => void): this
294
310
  on (event: 'mouseWheel', listener: (event: Events.Window.MouseWheel) => void): this
295
- on (event: 'dropBegin', listener: () => void): this
311
+ on (event: 'dropBegin', listener: (event: Event.Window.DropBegin) => void): this
296
312
  on (event: 'dropText', listener: (event: Events.Window.DropText) => void): this
297
313
  on (event: 'dropFile', listener: (event: Events.Window.DropFile) => void): this
298
- on (event: 'dropComplete', listener: () => void): this
314
+ on (event: 'dropComplete', listener: (event: Event.Window.DropComplete) => void): this
299
315
 
300
316
  readonly id: number
301
317
 
@@ -729,7 +745,7 @@ export namespace Sdl {
729
745
  on (event: 'buttonDown', listener: (event: Events.Joystick.ButtonDown) => void): this
730
746
  on (event: 'buttonUp', listener: (event: Events.Joystick.ButtonUp) => void): this
731
747
  on (event: 'hatMotion', listener: (event: Events.Joystick.HatMotion) => void): this
732
- on (event: 'close', listener: () => void): this
748
+ on (event: 'close', listener: (event: Events.Joystick.Close) => void): this
733
749
 
734
750
  readonly device: Device
735
751
  readonly firmwareVersion: number
@@ -742,6 +758,8 @@ export namespace Sdl {
742
758
 
743
759
  readonly power: PowerLevel
744
760
 
761
+ setPlayer (index: number): void
762
+
745
763
  readonly hasLed: boolean
746
764
  setLed (red: number, green: number, blue: number): void
747
765
 
@@ -787,7 +805,7 @@ export namespace Sdl {
787
805
  on (event: 'buttonDown', listener: (event: Events.Controller.ButtonDown) => void): this
788
806
  on (event: 'buttonUp', listener: (event: Events.Controller.ButtonUp) => void): this
789
807
  on (event: 'remap', listener: (event: Events.Controller.Remap) => void): this
790
- on (event: 'close', listener: () => void): this
808
+ on (event: 'close', listener: (event: Events.Controller.Close) => void): this
791
809
 
792
810
  readonly device: Device
793
811
  readonly firmwareVersion: number
@@ -826,6 +844,8 @@ export namespace Sdl {
826
844
 
827
845
  readonly power: Joystick.PowerLevel
828
846
 
847
+ setPlayer (index: number): void
848
+
829
849
  readonly hasLed: boolean
830
850
  setLed (red: number, green: number, blue: number): void
831
851
 
@@ -890,7 +910,7 @@ export namespace Sdl {
890
910
  }
891
911
 
892
912
  class AudioInstance {
893
- on (event: 'close', listener: () => void): this
913
+ on (event: 'close', listener: (event: Events.Audio.Close) => void): this
894
914
 
895
915
  readonly id: number
896
916
  readonly device: Device
@@ -954,7 +974,7 @@ export namespace Sdl {
954
974
  namespace Clipboard {
955
975
 
956
976
  interface Module {
957
- on (event: 'update', listener: () => void): this
977
+ on (event: 'update', listener: (event: Events.Clipboard.Update) => void): this
958
978
 
959
979
  readonly text: string
960
980
  setText (text: string): void
@@ -964,7 +984,7 @@ export namespace Sdl {
964
984
 
965
985
  interface Module {
966
986
  on (event: 'beforeQuit', listener: (event: Events.App.BeforeQuit) => void): this
967
- on (event: 'quit', listener: () => void): this
987
+ on (event: 'quit', listener: (event: Events.App.Quit) => void): this
968
988
 
969
989
  readonly info: Info
970
990
  readonly video: Video.Module