@kmamal/sdl 0.6.8 → 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 +48 -9
- package/package.json +1 -1
- package/scripts/test.mjs +5 -3
- package/scripts/util/common.js +1 -1
- package/src/javascript/audio/index.js +12 -0
- package/src/javascript/controller/controller-instance.js +10 -1
- package/src/javascript/controller/index.js +11 -0
- package/src/javascript/joystick/index.js +8 -0
- package/src/javascript/joystick/joystick-instance.js +10 -1
- package/src/native/sdl-helpers.cpp +1 -1
- package/src/types/index.d.ts +4 -0
package/README.md
CHANGED
|
@@ -38,7 +38,7 @@ npm install @kmamal/sdl
|
|
|
38
38
|
### "Hello, World!"
|
|
39
39
|
|
|
40
40
|
```js
|
|
41
|
-
|
|
41
|
+
import sdl from '@kmamal/sdl'
|
|
42
42
|
|
|
43
43
|
const window = sdl.video.createWindow({ title: "Hello, World!" })
|
|
44
44
|
window.on('*', console.log)
|
|
@@ -63,8 +63,8 @@ window.render(width, height, width * 4, 'bgra32', buffer)
|
|
|
63
63
|
|
|
64
64
|
### WebGL
|
|
65
65
|
```js
|
|
66
|
-
|
|
67
|
-
|
|
66
|
+
import sdl from '@kmamal/sdl'
|
|
67
|
+
import createContext from '@kmamal/gl'
|
|
68
68
|
|
|
69
69
|
const window = sdl.video.createWindow({title: "WebGL", opengl: true })
|
|
70
70
|
const { width, height, native } = window
|
|
@@ -198,6 +198,7 @@ There are more examples [in the `examples/` folder](https://github.com/kmamal/no
|
|
|
198
198
|
* [joystickInstance.buttons](#joystickinstancebuttons)
|
|
199
199
|
* [joystickInstance.hats](#joystickinstancehats)
|
|
200
200
|
* [joystickInstance.power](#joystickinstancepower)
|
|
201
|
+
* [joystickInstance.setPlayer(index)](#joystickinstancesetplayerindex)
|
|
201
202
|
* [joystickInstance.hasLed](#joystickinstancehasled)
|
|
202
203
|
* [joystickInstance.setLed(red, green, blue)](#joystickinstancesetledred-green-blue)
|
|
203
204
|
* [joystickInstance.hasRumble](#joystickinstancehasrumble)
|
|
@@ -226,6 +227,7 @@ There are more examples [in the `examples/` folder](https://github.com/kmamal/no
|
|
|
226
227
|
* [controllerInstance.axes](#controllerinstanceaxes)
|
|
227
228
|
* [controllerInstance.buttons](#controllerinstancebuttons)
|
|
228
229
|
* [controllerInstance.power](#controllerinstancepower)
|
|
230
|
+
* [controllerInstance.setPlayer(index)](#controllerinstancesetplayerindex)
|
|
229
231
|
* [controllerInstance.hasLed](#controllerinstancehasled)
|
|
230
232
|
* [controllerInstance.setLed(red, green, blue)](#controllerinstancesetledred-green-blue)
|
|
231
233
|
* [controllerInstance.hasRumble](#controllerinstancehasrumble)
|
|
@@ -1512,6 +1514,12 @@ An array of values, each corresponding to the position of one of the joystick's
|
|
|
1512
1514
|
|
|
1513
1515
|
The current power level of the joystick device.
|
|
1514
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
|
+
|
|
1515
1523
|
### joystickInstance.hasLed
|
|
1516
1524
|
|
|
1517
1525
|
* `<boolean>`
|
|
@@ -1726,17 +1734,42 @@ The controller's serial number, or `null` if it's not available.
|
|
|
1726
1734
|
|
|
1727
1735
|
### controllerInstance.axes
|
|
1728
1736
|
|
|
1729
|
-
*
|
|
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
|
|
1730
1744
|
|
|
1731
|
-
An
|
|
1745
|
+
An object mapping each axis of the controller's axes to its position.
|
|
1732
1746
|
The values are normalized from `-1` to `+1`.
|
|
1733
1747
|
It may be necessary to impose certain tolerances on these values to account for jitter.
|
|
1734
1748
|
|
|
1735
1749
|
### controllerInstance.buttons
|
|
1736
1750
|
|
|
1737
|
-
*
|
|
1738
|
-
|
|
1739
|
-
|
|
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.
|
|
1740
1773
|
The values will be `true` for buttons that are pressed and `false` otherwise.
|
|
1741
1774
|
|
|
1742
1775
|
### controllerInstance.power
|
|
@@ -1745,6 +1778,12 @@ The values will be `true` for buttons that are pressed and `false` otherwise.
|
|
|
1745
1778
|
|
|
1746
1779
|
The current power level of the controller device.
|
|
1747
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
|
+
|
|
1748
1787
|
### controllerInstance.hasLed
|
|
1749
1788
|
|
|
1750
1789
|
* `<boolean>`
|
|
@@ -2250,6 +2289,6 @@ There are some npm scripts in `package.json` that could be of use to you:
|
|
|
2250
2289
|
|
|
2251
2290
|
The SDL headers and libs get downloaded to `sdl/`, the build happens in `build/`, and the final binaries get collected into `dist/`.
|
|
2252
2291
|
|
|
2253
|
-
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`
|
|
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.
|
|
2254
2293
|
|
|
2255
2294
|
Have fun!
|
package/package.json
CHANGED
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
|
|
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.
|
|
6
|
-
const files = await
|
|
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' })
|
package/scripts/util/common.js
CHANGED
|
@@ -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.
|
|
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.
|
|
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 {
|
|
@@ -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.
|
|
78
|
+
Bindings.joystick_setLed(this._device.id, red, green, blue)
|
|
70
79
|
}
|
|
71
80
|
|
|
72
81
|
get hasRumble () { return this._hasRumble }
|
|
@@ -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 (
|
|
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
|
|
package/src/types/index.d.ts
CHANGED
|
@@ -758,6 +758,8 @@ export namespace Sdl {
|
|
|
758
758
|
|
|
759
759
|
readonly power: PowerLevel
|
|
760
760
|
|
|
761
|
+
setPlayer (index: number): void
|
|
762
|
+
|
|
761
763
|
readonly hasLed: boolean
|
|
762
764
|
setLed (red: number, green: number, blue: number): void
|
|
763
765
|
|
|
@@ -842,6 +844,8 @@ export namespace Sdl {
|
|
|
842
844
|
|
|
843
845
|
readonly power: Joystick.PowerLevel
|
|
844
846
|
|
|
847
|
+
setPlayer (index: number): void
|
|
848
|
+
|
|
845
849
|
readonly hasLed: boolean
|
|
846
850
|
setLed (red: number, green: number, blue: number): void
|
|
847
851
|
|