@kmamal/sdl 0.10.10 → 0.10.12
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 +70 -35
- package/package.json +1 -1
- package/scripts/install.mjs +2 -2
- package/src/javascript/audio/audio-instance.js +4 -3
- package/src/javascript/audio/audio-playback-instance.js +2 -1
- package/src/javascript/audio/audio-recording-instance.js +2 -1
- package/src/javascript/clipboard/index.js +1 -1
- package/src/javascript/controller/controller-instance.js +6 -4
- package/src/javascript/events/index.js +12 -3
- package/src/javascript/globals.js +1 -1
- package/src/javascript/joystick/joystick-instance.js +10 -8
- package/src/javascript/keyboard/index.js +1 -1
- package/src/javascript/mouse/index.js +8 -8
- package/src/javascript/video/window.js +44 -17
- package/src/native/enums.cpp +6 -0
- package/src/native/window.cpp +8 -0
- package/src/types/index.d.ts +8 -1
package/README.md
CHANGED
|
@@ -25,7 +25,7 @@ SDL is bundled along with the binaries so a separate installation is not necessa
|
|
|
25
25
|
This package is self-contained.
|
|
26
26
|
Just run:
|
|
27
27
|
|
|
28
|
-
```
|
|
28
|
+
```bash
|
|
29
29
|
npm install @kmamal/sdl
|
|
30
30
|
```
|
|
31
31
|
|
|
@@ -44,6 +44,7 @@ window.on('*', console.log)
|
|
|
44
44
|
```
|
|
45
45
|
|
|
46
46
|
### Canvas
|
|
47
|
+
|
|
47
48
|
```js
|
|
48
49
|
import sdl from '@kmamal/sdl'
|
|
49
50
|
import { createCanvas } from 'canvas'
|
|
@@ -64,6 +65,7 @@ window.render(width, height, width * 4, 'bgra32', buffer)
|
|
|
64
65
|
```
|
|
65
66
|
|
|
66
67
|
### WebGL
|
|
68
|
+
|
|
67
69
|
```js
|
|
68
70
|
import sdl from '@kmamal/sdl'
|
|
69
71
|
import createContext from '@kmamal/gl'
|
|
@@ -82,6 +84,7 @@ gl.swap()
|
|
|
82
84
|
```
|
|
83
85
|
|
|
84
86
|
### WebGPU
|
|
87
|
+
|
|
85
88
|
```js
|
|
86
89
|
import sdl from '@kmamal/sdl'
|
|
87
90
|
import gpu from '@kmamal/gpu'
|
|
@@ -173,6 +176,7 @@ Check the [`examples/`](https://github.com/kmamal/node-sdl/tree/master/examples#
|
|
|
173
176
|
* [window.pixelWidth](#windowpixelwidth)
|
|
174
177
|
* [window.pixelHeight](#windowpixelheight)
|
|
175
178
|
* [window.setSize(width, height)](#windowsetsizewidth-height)
|
|
179
|
+
* [window.setSizeInPixels(pixelWidth, pixelHeight)](#windowsetsizeinpixelspixelwidth-pixelheight)
|
|
176
180
|
* [window.visible](#windowvisible)
|
|
177
181
|
* [window.show([show])](#windowshowshow)
|
|
178
182
|
* [window.hide()](#windowhide)
|
|
@@ -202,7 +206,7 @@ Check the [`examples/`](https://github.com/kmamal/node-sdl/tree/master/examples#
|
|
|
202
206
|
* [window.popupMenu](#windowpopupmenu)
|
|
203
207
|
* [window.tooltip](#windowtooltip)
|
|
204
208
|
* [window.utility](#windowutility)
|
|
205
|
-
* [window.render(width, height, stride, format, buffer)](#windowrenderwidth-height-stride-format-buffer)
|
|
209
|
+
* [window.render(width, height, stride, format, buffer[, scaling])](#windowrenderwidth-height-stride-format-buffer-scaling)
|
|
206
210
|
* [window.setIcon(width, height, stride, format, buffer)](#windowseticonwidth-height-stride-format-buffer)
|
|
207
211
|
* [window.flash(untilFocused)](#windowflashuntilfocused)
|
|
208
212
|
* [window.stopFlashing()](#windowstopflashing)
|
|
@@ -230,6 +234,7 @@ Check the [`examples/`](https://github.com/kmamal/node-sdl/tree/master/examples#
|
|
|
230
234
|
* [sdl.mouse.uncapture()](#sdlmouseuncapture)
|
|
231
235
|
* [sdl.joystick](#sdljoystick)
|
|
232
236
|
* [Hat positions](#hat-positions)
|
|
237
|
+
* [Power levels](#power-levels)
|
|
233
238
|
* [Event: 'deviceAdd'](#joystick-event-deviceadd)
|
|
234
239
|
* [Event: 'deviceRemove'](#joystick-event-deviceremove)
|
|
235
240
|
* [sdl.joystick.devices](#sdljoystickdevices)
|
|
@@ -339,7 +344,7 @@ Check the [`examples/`](https://github.com/kmamal/node-sdl/tree/master/examples#
|
|
|
339
344
|
* [class AudioRecordingInstance extends AudioInstance](#class-audiorecordinginstance-extends-audioinstance)
|
|
340
345
|
* [recordingInstance.dequeue(buffer[, bytes])](#recordinginstancedequeuebuffer-bytes)
|
|
341
346
|
* [sdl.clipboard](#sdlclipboard)
|
|
342
|
-
* [Event: 'update'](#event-update)
|
|
347
|
+
* [Event: 'update'](#clipboard-event-update)
|
|
343
348
|
* [sdl.clipboard.text](#sdlclipboardtext)
|
|
344
349
|
* [sdl.clipboard.setText(text)](#sdlclipboardsettexttext)
|
|
345
350
|
* [sdl.power](#sdlpower)
|
|
@@ -401,11 +406,13 @@ Sample data for Ubuntu:
|
|
|
401
406
|
### Image data
|
|
402
407
|
|
|
403
408
|
There are 3 places in the API where you will need to provide an image to the library:
|
|
404
|
-
|
|
409
|
+
|
|
410
|
+
* [`window.render()`](#windowrenderwidth-height-stride-format-buffer-scaling)
|
|
405
411
|
* [`window.setIcon()`](#windowseticonwidth-height-stride-format-buffer)
|
|
406
412
|
* [`mouse.setCursorImage()`](#sdlmousesetcursorimagewidth-height-stride-format-buffer-x-y)
|
|
407
413
|
|
|
408
414
|
All three of these functions accept the image as a series of arguments:
|
|
415
|
+
|
|
409
416
|
* `width: <number>` The width of the image in pixels.
|
|
410
417
|
* `height: <number>` The height of the image in pixels.
|
|
411
418
|
* `stride: <number>` How many bytes each row of the image takes up in the buffer. This is usually equal to `width * bytesPerPixel`, but can be larger if the rows of the buffer are padded to always be some multiple of bytes.
|
|
@@ -591,6 +598,7 @@ The window that the mouse is hovered over, or `null` if the mouse is not over a
|
|
|
591
598
|
Creates a new window.
|
|
592
599
|
|
|
593
600
|
The following restrictions apply:
|
|
601
|
+
|
|
594
602
|
* If you specify the `display` option, you can't also specify the `x` or `y` options, and vice-versa.
|
|
595
603
|
* The `resizable` and `borderless` options are mutually exclusive.
|
|
596
604
|
* The `opengl` and `webgpu` options are mutually exclusive.
|
|
@@ -598,7 +606,7 @@ The following restrictions apply:
|
|
|
598
606
|
* The `accelerated` and `vsync` options have no effect if either `opengl` or `webgpu` is also specified.
|
|
599
607
|
|
|
600
608
|
If you set the `opengl` or `webgpu` options, then you can only render to the window with OpenGL/WebGPU calls.
|
|
601
|
-
Calls to [`render()`](#windowrenderwidth-height-stride-format-buffer) will fail.
|
|
609
|
+
Calls to [`render()`](#windowrenderwidth-height-stride-format-buffer-scaling) will fail.
|
|
602
610
|
|
|
603
611
|
## class Window
|
|
604
612
|
|
|
@@ -819,13 +827,13 @@ The window's height.
|
|
|
819
827
|
|
|
820
828
|
* `<number>`
|
|
821
829
|
|
|
822
|
-
The window's width in pixels. This will be larger than `width` on [high-dpi](#high-dpi) displays.
|
|
830
|
+
The window's width in pixels. This will be larger than [`width`](#windowwidth) on [high-dpi](#high-dpi) displays.
|
|
823
831
|
|
|
824
832
|
### window.pixelHeight
|
|
825
833
|
|
|
826
834
|
* `<number>`
|
|
827
835
|
|
|
828
|
-
The window's height in pixels. This will be larger than `height` on [high-dpi](#high-dpi) displays.
|
|
836
|
+
The window's height in pixels. This will be larger than [`height`](#windowheight) on [high-dpi](#high-dpi) displays.
|
|
829
837
|
|
|
830
838
|
### window.setSize(width, height)
|
|
831
839
|
|
|
@@ -834,6 +842,14 @@ The window's height in pixels. This will be larger than `height` on [high-dpi](#
|
|
|
834
842
|
|
|
835
843
|
Changes the size of the window.
|
|
836
844
|
|
|
845
|
+
### window.setSizeInPixels(pixelWidth, pixelHeight)
|
|
846
|
+
|
|
847
|
+
* `pixelWidth: <number>`: The new width in pixels.
|
|
848
|
+
* `pixelHeight: <number>`: The new height in pixels.
|
|
849
|
+
|
|
850
|
+
Changes the size of the window.
|
|
851
|
+
This function only behaves differently from [`window.setSize()`](#windowsetsizewidth-height) for [high-dpi](#high-dpi) displays.
|
|
852
|
+
|
|
837
853
|
### window.visible
|
|
838
854
|
|
|
839
855
|
* `<boolean>`
|
|
@@ -932,7 +948,7 @@ If you have set the `opengl` or `webgpu` options, then calls to this function wi
|
|
|
932
948
|
|
|
933
949
|
Will be `true` if the window was created in OpenGl mode.
|
|
934
950
|
In OpenGL mode, you can only render to the window with OpenGL calls.
|
|
935
|
-
Calls to [`render()`](#windowrenderwidth-height-stride-format-buffer) will fail.
|
|
951
|
+
Calls to [`render()`](#windowrenderwidth-height-stride-format-buffer-scaling) will fail.
|
|
936
952
|
|
|
937
953
|
### window.webgpu
|
|
938
954
|
|
|
@@ -940,7 +956,7 @@ Calls to [`render()`](#windowrenderwidth-height-stride-format-buffer) will fail.
|
|
|
940
956
|
|
|
941
957
|
Will be `true` if the window was created in WebGPU mode.
|
|
942
958
|
In WebGPU mode, you can only render to the window with WebGPU calls.
|
|
943
|
-
Calls to [`render()`](#windowrenderwidth-height-stride-format-buffer) will fail.
|
|
959
|
+
Calls to [`render()`](#windowrenderwidth-height-stride-format-buffer-scaling) will fail.
|
|
944
960
|
|
|
945
961
|
### window.native
|
|
946
962
|
|
|
@@ -1021,15 +1037,24 @@ X11 only.
|
|
|
1021
1037
|
Will be `true` if the window was created with `utility: true`.
|
|
1022
1038
|
Such a window will always be treated as a utility window.
|
|
1023
1039
|
|
|
1024
|
-
### window.render(width, height, stride, format, buffer)
|
|
1040
|
+
### window.render(width, height, stride, format, buffer[, scaling])
|
|
1025
1041
|
|
|
1026
1042
|
* `width, height, stride, format, buffer: `[`<Image>`](#image-data) The image to display on the window.
|
|
1043
|
+
* `scaling: <string>` How to scale the image to match the window size. Default `'nearest'`
|
|
1027
1044
|
|
|
1028
1045
|
Displays an image in the window.
|
|
1029
|
-
|
|
1046
|
+
If the dimensions of the image do not match the dimansions of the window, then the image will be stretched over the entire surface of the window.
|
|
1047
|
+
The `scaling` argument controls how exactly the scaling is implemented.
|
|
1048
|
+
Possible values are:
|
|
1049
|
+
|
|
1050
|
+
| Value | Corresponding `SDL_ScaleMode` | Description |
|
|
1051
|
+
| --- | --- | --- |
|
|
1052
|
+
| `'nearest'` | `SDL_ScaleModeNearest` | nearest pixel sampling |
|
|
1053
|
+
| `'linear'` | `SDL_ScaleModeLinear` | linear filtering |
|
|
1054
|
+
| `'best'` | `SDL_ScaleModeBest` | anisotropic filtering |
|
|
1030
1055
|
|
|
1031
1056
|
If you set the `opengl` or `webgpu` options, then you can only render to the window with OpenGL/WebGPU calls.
|
|
1032
|
-
Calls to [`render()`](#windowrenderwidth-height-stride-format-buffer) will fail.
|
|
1057
|
+
Calls to [`render()`](#windowrenderwidth-height-stride-format-buffer-scaling) will fail.
|
|
1033
1058
|
|
|
1034
1059
|
### window.setIcon(width, height, stride, format, buffer)
|
|
1035
1060
|
|
|
@@ -1107,6 +1132,7 @@ The same key will always produce the same scancode.
|
|
|
1107
1132
|
Values are based on the [USB usage page standard](https://www.usb.org/sites/default/files/documents/hut1_12v2.pdf).
|
|
1108
1133
|
|
|
1109
1134
|
<details>
|
|
1135
|
+
|
|
1110
1136
|
<summary>Click to expand table</summary>
|
|
1111
1137
|
|
|
1112
1138
|
| Value | Corresponding `SDL_Scancode` | Comment |
|
|
@@ -1353,6 +1379,7 @@ Values are based on the [USB usage page standard](https://www.usb.org/sites/defa
|
|
|
1353
1379
|
| `sdl.keyboard.SCANCODE.APP2` | `SDL_SCANCODE_APP2` | |
|
|
1354
1380
|
| `sdl.keyboard.SCANCODE.AUDIOREWIND` | `SDL_SCANCODE_AUDIOREWIND` | |
|
|
1355
1381
|
| `sdl.keyboard.SCANCODE.AUDIOFASTFORWARD` | `SDL_SCANCODE_AUDIOFASTFORWARD` | |
|
|
1382
|
+
|
|
1356
1383
|
</details>
|
|
1357
1384
|
|
|
1358
1385
|
### sdl.keyboard.getKey(scancode)
|
|
@@ -1494,6 +1521,19 @@ String values used to represent the positions of a joystick hat
|
|
|
1494
1521
|
| `'leftup'` | `SDL_HAT_LEFTUP` |
|
|
1495
1522
|
| `'leftdown'` | `SDL_HAT_LEFTDOWN` |
|
|
1496
1523
|
|
|
1524
|
+
### Power levels
|
|
1525
|
+
|
|
1526
|
+
String values used to represent the power level of a joystick or controller
|
|
1527
|
+
|
|
1528
|
+
| Value | Corresponding `SDL_JoystickPowerLevel` |
|
|
1529
|
+
| --- | --- |
|
|
1530
|
+
| `'empty'` | `SDL_JOYSTICK_POWER_EMPTY` |
|
|
1531
|
+
| `'low'` | `SDL_JOYSTICK_POWER_LOW` |
|
|
1532
|
+
| `'medium'` | `SDL_JOYSTICK_POWER_MEDIUM` |
|
|
1533
|
+
| `'full'` | `SDL_JOYSTICK_POWER_FULL` |
|
|
1534
|
+
| `'wired'` | `SDL_JOYSTICK_POWER_WIRED` |
|
|
1535
|
+
| `'max'` | `SDL_JOYSTICK_POWER_MAX` |
|
|
1536
|
+
|
|
1497
1537
|
<a id="joystick-event-deviceadd"></a>
|
|
1498
1538
|
|
|
1499
1539
|
### Event: 'deviceAdd'
|
|
@@ -1668,21 +1708,10 @@ An array of values, each corresponding to the position of one of the joystick's
|
|
|
1668
1708
|
|
|
1669
1709
|
### joystickInstance.power
|
|
1670
1710
|
|
|
1671
|
-
* `<PowerLevel
|
|
1711
|
+
* [`<PowerLevel>`](#power-levels)`|<null>`
|
|
1672
1712
|
|
|
1673
1713
|
The current power level of the joystick device, or `null` if it is unknown.
|
|
1674
1714
|
|
|
1675
|
-
Possible values are:
|
|
1676
|
-
|
|
1677
|
-
| Value | Corresponding `SDL_JoystickPowerLevel` |
|
|
1678
|
-
| --- | --- |
|
|
1679
|
-
| `'empty'` | `SDL_JOYSTICK_POWER_EMPTY` |
|
|
1680
|
-
| `'low'` | `SDL_JOYSTICK_POWER_LOW` |
|
|
1681
|
-
| `'medium'` | `SDL_JOYSTICK_POWER_MEDIUM` |
|
|
1682
|
-
| `'full'` | `SDL_JOYSTICK_POWER_FULL` |
|
|
1683
|
-
| `'wired'` | `SDL_JOYSTICK_POWER_WIRED` |
|
|
1684
|
-
| `'max'` | `SDL_JOYSTICK_POWER_MAX` |
|
|
1685
|
-
|
|
1686
1715
|
### joystickInstance.setPlayer(index)
|
|
1687
1716
|
|
|
1688
1717
|
* `index: <number>` The player index to assign to the joystick.
|
|
@@ -1772,6 +1801,7 @@ A mapping is a string that consists of the device's name, it's GUID, and a serie
|
|
|
1772
1801
|
See the sample output [`here`](#sdlcontrollerdevices) for an example.
|
|
1773
1802
|
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.
|
|
1774
1803
|
You can add them via:
|
|
1804
|
+
|
|
1775
1805
|
```js
|
|
1776
1806
|
const url = 'https://raw.githubusercontent.com/gabomdq/SDL_GameControllerDB/master/gamecontrollerdb.txt'
|
|
1777
1807
|
const result = await fetch(url)
|
|
@@ -1947,9 +1977,9 @@ The values will be `true` for buttons that are pressed and `false` otherwise.
|
|
|
1947
1977
|
|
|
1948
1978
|
### controllerInstance.power
|
|
1949
1979
|
|
|
1950
|
-
* [`<PowerLevel>`](#power-levels)
|
|
1980
|
+
* [`<PowerLevel>`](#power-levels)`|<null>`
|
|
1951
1981
|
|
|
1952
|
-
The current power level of the
|
|
1982
|
+
The current power level of the joystick device, or `null` if it is unknown.
|
|
1953
1983
|
|
|
1954
1984
|
### controllerInstance.setPlayer(index)
|
|
1955
1985
|
|
|
@@ -2066,7 +2096,7 @@ Instead, objects returned by [`sdl.sensor.openDevice()`](#sdlsensoropendevicedev
|
|
|
2066
2096
|
### Event: 'update'
|
|
2067
2097
|
|
|
2068
2098
|
Fired when the sensor's data changes.
|
|
2069
|
-
Get the new data by accessing [`sensorInstance.data`](#
|
|
2099
|
+
Get the new data by accessing [`sensorInstance.data`](#sensorinstancedata)
|
|
2070
2100
|
|
|
2071
2101
|
<a id="sensor-instance-event-close"></a>
|
|
2072
2102
|
|
|
@@ -2084,7 +2114,7 @@ The [device](#sdlsensordevices) from which this instance was opened.
|
|
|
2084
2114
|
### sensorInstance.data
|
|
2085
2115
|
|
|
2086
2116
|
* `<object>`
|
|
2087
|
-
* `timestamp: <number>|<null>` The time this measurement was taken
|
|
2117
|
+
* `timestamp: <number>|<null>` The time this measurement was taken __in microseconds__, or `null` if that's not available.
|
|
2088
2118
|
* `x: <number>` X axis value.
|
|
2089
2119
|
* `y: <number>` Y axis value.
|
|
2090
2120
|
* `z: <number>` Z axis value.
|
|
@@ -2126,6 +2156,7 @@ An audio buffer is a sequence of frames, and each frame is a sequence of samples
|
|
|
2126
2156
|
A _sample_ is a single number representing the intensity of an audio channel at a point in time.
|
|
2127
2157
|
For audio with multiple channels, each point in time is represented by multiple samples (one per channel) that together make up a _frame_.
|
|
2128
2158
|
The samples in a frame are arranged as follows:
|
|
2159
|
+
|
|
2129
2160
|
* For 1 channel (mono) a frame contains just the one sample.
|
|
2130
2161
|
* For 2 channels (stereo) the frame's first sample will be the one for the left channel, followed by the one for the right channel.
|
|
2131
2162
|
* For 4 channels (quad) the layout is front-left, front-right, rear-left, rear-right.
|
|
@@ -2491,6 +2522,8 @@ Takes recorded audio data that has been put on the queue, and writes it to the p
|
|
|
2491
2522
|
|
|
2492
2523
|
## sdl.clipboard
|
|
2493
2524
|
|
|
2525
|
+
<a id="clipboard-event-update"></a>
|
|
2526
|
+
|
|
2494
2527
|
### Event: 'update'
|
|
2495
2528
|
|
|
2496
2529
|
Fired when the contents of the clipboard have changed.
|
|
@@ -2514,7 +2547,7 @@ Changes the text contents of the clipboard.
|
|
|
2514
2547
|
### sdl.power.info
|
|
2515
2548
|
|
|
2516
2549
|
* `<object>`
|
|
2517
|
-
* `state: <string
|
|
2550
|
+
* `state: <string>|<null>` One of `'noBattery'`, `'battery'`, `'charging'`, `'charged'`. Will be `null` if it can't be determinded.
|
|
2518
2551
|
* `seconds: <number>|<null>` Seconds of battery life left. Will be `null` if not running on battery, or if it can't be determinded.
|
|
2519
2552
|
* `percent: <number>|<null>` Percentage of battery life left. Will be `null` if not running on battery, or if it can't be determinded.
|
|
2520
2553
|
|
|
@@ -2533,12 +2566,13 @@ This is still possible! Even if the threads do not have access to the SDL-relate
|
|
|
2533
2566
|
Since these are just helpers and don't call any SLD code underneath it's safe to use them. They are made available through the `@kmamal/sdl/helpers` sub-module that does not load any of the native code. For an example of their use see [this example](https://github.com/kmamal/node-sdl/blob/master/examples/12-audio-thread/audio-worker.js).
|
|
2534
2567
|
|
|
2535
2568
|
The functions included in `@kmamal/sdl/helpers` are:
|
|
2536
|
-
|
|
2537
|
-
|
|
2538
|
-
|
|
2539
|
-
|
|
2540
|
-
|
|
2541
|
-
|
|
2569
|
+
|
|
2570
|
+
* [`sdl.audio.bytesPerSample`](#sdlaudiobytespersampleformat)
|
|
2571
|
+
* [`sdl.audio.minSampleValue`](#sdlaudiominsamplevalueformat)
|
|
2572
|
+
* [`sdl.audio.maxSampleValue`](#sdlaudiomaxsamplevalueformat)
|
|
2573
|
+
* [`sdl.audio.zeroSampleValue`](#sdlaudiozerosamplevalueformat)
|
|
2574
|
+
* [`sdl.audio.readSample`](#sdlaudioreadsampleformat-buffer-offset)
|
|
2575
|
+
* [`sdl.audio.writeSample`](#sdlaudiowritesampleformat-buffer-value-offset)
|
|
2542
2576
|
|
|
2543
2577
|
|
|
2544
2578
|
## Building from source
|
|
@@ -2560,6 +2594,7 @@ You will need to have `cmake` installed for that to work.
|
|
|
2560
2594
|
|
|
2561
2595
|
You could also have found your way to the "Building from source" section because you are trying to contribute to this package.
|
|
2562
2596
|
There are some npm scripts in `package.json` that could be of use to you:
|
|
2597
|
+
|
|
2563
2598
|
* `npm run clean` deletes all folders that are created during the build, as well as `node_modules`.
|
|
2564
2599
|
* `npm run download-release` downloads the prebuilt binaries. This is the first thing the install script tries to do.
|
|
2565
2600
|
* `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.
|
package/package.json
CHANGED
package/scripts/install.mjs
CHANGED
|
@@ -3,7 +3,7 @@ if (!process.env.NODE_SDL_FROM_SOURCE) {
|
|
|
3
3
|
try {
|
|
4
4
|
await import('./download-release.mjs')
|
|
5
5
|
process.exit(0)
|
|
6
|
-
} catch (
|
|
6
|
+
} catch (_) {
|
|
7
7
|
console.log("failed to download release")
|
|
8
8
|
}
|
|
9
9
|
} else {
|
|
@@ -12,7 +12,7 @@ if (!process.env.NODE_SDL_FROM_SOURCE) {
|
|
|
12
12
|
|
|
13
13
|
try {
|
|
14
14
|
await import('./download-sdl.mjs')
|
|
15
|
-
} catch (
|
|
15
|
+
} catch (_) {
|
|
16
16
|
console.log("failed to download sdl")
|
|
17
17
|
await import('./build-sdl.mjs')
|
|
18
18
|
}
|
|
@@ -22,12 +22,13 @@ class AudioInstance extends EventsViaPoll {
|
|
|
22
22
|
if (name !== undefined && name !== null && typeof name !== 'string') { throw Object.assign(new Error("name must be a string"), { name }) }
|
|
23
23
|
if (typeof type !== 'string') { throw Object.assign(new Error("type must be a string"), { type }) }
|
|
24
24
|
if (type !== 'playback' && type !== 'recording') { throw Object.assign(new Error("type must be either 'playback' or 'recording'"), { type }) }
|
|
25
|
-
if (!Number.
|
|
25
|
+
if (!Number.isInteger(channels)) { throw Object.assign(new Error("channels must be an integer"), { channels }) }
|
|
26
26
|
if (![ 1, 2, 4, 6 ].includes(channels)) { throw Object.assign(new Error("invalid channels"), { channels }) }
|
|
27
|
-
if (!Number.
|
|
27
|
+
if (!Number.isInteger(frequency)) { throw Object.assign(new Error("frequency must be an integer"), { frequency }) }
|
|
28
28
|
if (frequency <= 0) { throw Object.assign(new Error("invalid frequency"), { frequency }) }
|
|
29
29
|
if (typeof format !== 'string') { throw Object.assign(new Error("format must be a string"), { format }) }
|
|
30
|
-
if (!Number.
|
|
30
|
+
if (!Number.isInteger(buffered)) { throw Object.assign(new Error("buffered must be an integer"), { buffered }) }
|
|
31
|
+
if (buffered <= 0) { throw Object.assign(new Error("invalid buffered"), { buffered }) }
|
|
31
32
|
if (buffered !== 2 ** (32 - Math.clz32(buffered) - 1)) { throw Object.assign(new Error("invalid buffered"), { buffered }) }
|
|
32
33
|
|
|
33
34
|
const _format = Enums.audioFormat[format]
|
|
@@ -7,7 +7,8 @@ class AudioPlaybackInstance extends AudioInstance {
|
|
|
7
7
|
if (this._closed) { throw Object.assign(new Error("instance is closed"), { id: this._id }) }
|
|
8
8
|
|
|
9
9
|
if (!(buffer instanceof Buffer)) { throw Object.assign(new Error("buffer must be a Buffer"), { buffer }) }
|
|
10
|
-
if (!Number.
|
|
10
|
+
if (!Number.isInteger(numBytes)) { throw Object.assign(new Error("numBytes must be an integer"), { numBytes }) }
|
|
11
|
+
if (numBytes <= 0) { throw Object.assign(new Error("invalid numBytes"), { numBytes }) }
|
|
11
12
|
if (buffer.length < numBytes) { throw Object.assign(new Error("buffer is smaller than expected"), { buffer, numBytes }) }
|
|
12
13
|
|
|
13
14
|
Bindings.audio_enqueue(this._id, buffer, numBytes)
|
|
@@ -6,7 +6,8 @@ class AudioRecordingInstance extends AudioInstance {
|
|
|
6
6
|
if (this._closed) { throw Object.assign(new Error("instance is closed"), { id: this._id }) }
|
|
7
7
|
|
|
8
8
|
if (!(buffer instanceof Buffer)) { throw Object.assign(new Error("buffer must be a Buffer"), { buffer }) }
|
|
9
|
-
if (!Number.
|
|
9
|
+
if (!Number.isInteger(numBytes)) { throw Object.assign(new Error("numBytes must be an integer"), { numBytes }) }
|
|
10
|
+
if (numBytes <= 0) { throw Object.assign(new Error("invalid numBytes"), { numBytes }) }
|
|
10
11
|
if (buffer.length < numBytes) { throw Object.assign(new Error("buffer is smaller than expected"), { buffer, numBytes }) }
|
|
11
12
|
|
|
12
13
|
return Bindings.audio_dequeue(this._id, buffer, numBytes)
|
|
@@ -44,12 +44,12 @@ class ControllerInstance extends EventsViaPoll {
|
|
|
44
44
|
get serialNumber () { return this._serialNumber }
|
|
45
45
|
|
|
46
46
|
get axes () {
|
|
47
|
-
Globals.events.poll()
|
|
47
|
+
Globals.events.poll()
|
|
48
48
|
return this._axes
|
|
49
49
|
}
|
|
50
50
|
|
|
51
51
|
get buttons () {
|
|
52
|
-
Globals.events.poll()
|
|
52
|
+
Globals.events.poll()
|
|
53
53
|
return this._buttons
|
|
54
54
|
}
|
|
55
55
|
|
|
@@ -58,8 +58,8 @@ class ControllerInstance extends EventsViaPoll {
|
|
|
58
58
|
setPlayer (player) {
|
|
59
59
|
if (this._closed) { throw Object.assign(new Error("instance is closed"), { id: this._device.id }) }
|
|
60
60
|
|
|
61
|
-
if (!Number.
|
|
62
|
-
if (player < 0) { throw Object.assign(new Error("player
|
|
61
|
+
if (!Number.isInteger(player)) { throw Object.assign(new Error("player must be an integer"), { player }) }
|
|
62
|
+
if (player < 0) { throw Object.assign(new Error("invalid player"), { player }) }
|
|
63
63
|
|
|
64
64
|
Bindings.joystick_setPlayer(this._device.id, player)
|
|
65
65
|
}
|
|
@@ -93,6 +93,7 @@ class ControllerInstance extends EventsViaPoll {
|
|
|
93
93
|
if (!Number.isFinite(highFreqRumble)) { throw Object.assign(new Error("highFreqRumble must be a number"), { highFreqRumble }) }
|
|
94
94
|
if (highFreqRumble < 0 || highFreqRumble > 1) { throw Object.assign(new Error("highFreqRumble must be between 0 and 1"), { highFreqRumble }) }
|
|
95
95
|
if (!Number.isFinite(duration)) { throw Object.assign(new Error("duration must be a number"), { duration }) }
|
|
96
|
+
if (duration < 0) { throw Object.assign(new Error("invalid duration"), { duration }) }
|
|
96
97
|
|
|
97
98
|
// Globals.events.poll() // Errors if it hasn't been called at least once
|
|
98
99
|
Bindings.joystick_rumble(this._device.id, lowFreqRumble, highFreqRumble, duration)
|
|
@@ -116,6 +117,7 @@ class ControllerInstance extends EventsViaPoll {
|
|
|
116
117
|
if (!Number.isFinite(rightRumble)) { throw Object.assign(new Error("rightRumble must be a number"), { rightRumble }) }
|
|
117
118
|
if (rightRumble < 0 || rightRumble > 1) { throw Object.assign(new Error("rightRumble must be between 0 and 1"), { rightRumble }) }
|
|
118
119
|
if (!Number.isFinite(duration)) { throw Object.assign(new Error("duration must be a number"), { duration }) }
|
|
120
|
+
if (duration < 0) { throw Object.assign(new Error("invalid duration"), { duration }) }
|
|
119
121
|
|
|
120
122
|
// Globals.events.poll() // Errors if it hasn't been called at least once
|
|
121
123
|
Bindings.joystick_rumbleTriggers(this._device.id, leftRumble, rightRumble, duration)
|
|
@@ -312,14 +312,14 @@ const handleEvent = (event) => {
|
|
|
312
312
|
|
|
313
313
|
case 'buttonDown': {
|
|
314
314
|
for (const controllerInstance of collection.values()) {
|
|
315
|
-
controllerInstance.
|
|
315
|
+
controllerInstance._buttons[event.button] = true
|
|
316
316
|
controllerInstance.emit(event.type, event)
|
|
317
317
|
}
|
|
318
318
|
} break
|
|
319
319
|
|
|
320
320
|
case 'buttonUp': {
|
|
321
321
|
for (const controllerInstance of collection.values()) {
|
|
322
|
-
controllerInstance.
|
|
322
|
+
controllerInstance._buttons[event.button] = false
|
|
323
323
|
controllerInstance.emit(event.type, event)
|
|
324
324
|
}
|
|
325
325
|
} break
|
|
@@ -394,7 +394,16 @@ const handleEvent = (event) => {
|
|
|
394
394
|
}
|
|
395
395
|
|
|
396
396
|
|
|
397
|
-
|
|
397
|
+
let polling = false
|
|
398
|
+
|
|
399
|
+
const poll = () => {
|
|
400
|
+
if (polling) { return }
|
|
401
|
+
polling = true
|
|
402
|
+
|
|
403
|
+
Bindings.events_poll(handleEvent)
|
|
404
|
+
|
|
405
|
+
polling = false
|
|
406
|
+
}
|
|
398
407
|
|
|
399
408
|
let pollInterval = null
|
|
400
409
|
|
|
@@ -47,22 +47,22 @@ class JoystickInstance extends EventsViaPoll {
|
|
|
47
47
|
get serialNumber () { return this._serialNumber }
|
|
48
48
|
|
|
49
49
|
get axes () {
|
|
50
|
-
Globals.events.poll()
|
|
50
|
+
Globals.events.poll()
|
|
51
51
|
return this._axes
|
|
52
52
|
}
|
|
53
53
|
|
|
54
54
|
get balls () {
|
|
55
|
-
Globals.events.poll()
|
|
55
|
+
Globals.events.poll()
|
|
56
56
|
return this._balls
|
|
57
57
|
}
|
|
58
58
|
|
|
59
59
|
get buttons () {
|
|
60
|
-
Globals.events.poll()
|
|
60
|
+
Globals.events.poll()
|
|
61
61
|
return this._buttons
|
|
62
62
|
}
|
|
63
63
|
|
|
64
64
|
get hats () {
|
|
65
|
-
Globals.events.poll()
|
|
65
|
+
Globals.events.poll()
|
|
66
66
|
return this._hats
|
|
67
67
|
}
|
|
68
68
|
|
|
@@ -71,8 +71,8 @@ class JoystickInstance extends EventsViaPoll {
|
|
|
71
71
|
setPlayer (player) {
|
|
72
72
|
if (this._closed) { throw Object.assign(new Error("instance is closed"), { id: this._device.id }) }
|
|
73
73
|
|
|
74
|
-
if (!Number.
|
|
75
|
-
if (player < 0) { throw Object.assign(new Error("player
|
|
74
|
+
if (!Number.isInteger(player)) { throw Object.assign(new Error("player must be an integer"), { player }) }
|
|
75
|
+
if (player < 0) { throw Object.assign(new Error("invalid player"), { player }) }
|
|
76
76
|
|
|
77
77
|
Bindings.joystick_setPlayer(this._device.id, player)
|
|
78
78
|
}
|
|
@@ -105,7 +105,8 @@ class JoystickInstance extends EventsViaPoll {
|
|
|
105
105
|
if (lowFreqRumble < 0 || lowFreqRumble > 1) { throw Object.assign(new Error("lowFreqRumble must be between 0 and 1"), { lowFreqRumble }) }
|
|
106
106
|
if (!Number.isFinite(highFreqRumble)) { throw Object.assign(new Error("highFreqRumble must be a number"), { highFreqRumble }) }
|
|
107
107
|
if (highFreqRumble < 0 || highFreqRumble > 1) { throw Object.assign(new Error("highFreqRumble must be between 0 and 1"), { highFreqRumble }) }
|
|
108
|
-
if (!Number.
|
|
108
|
+
if (!Number.isInteger(duration)) { throw Object.assign(new Error("duration must be an integer"), { duration }) }
|
|
109
|
+
if (duration < 0) { throw Object.assign(new Error("invalid duration"), { duration }) }
|
|
109
110
|
|
|
110
111
|
// Globals.events.poll() // Errors if it hasn't been called at least once
|
|
111
112
|
Bindings.joystick_rumble(this._device.id, lowFreqRumble, highFreqRumble, duration)
|
|
@@ -128,7 +129,8 @@ class JoystickInstance extends EventsViaPoll {
|
|
|
128
129
|
if (leftRumble < 0 || leftRumble > 1) { throw Object.assign(new Error("leftRumble must be between 0 and 1"), { leftRumble }) }
|
|
129
130
|
if (!Number.isFinite(rightRumble)) { throw Object.assign(new Error("rightRumble must be a number"), { rightRumble }) }
|
|
130
131
|
if (rightRumble < 0 || rightRumble > 1) { throw Object.assign(new Error("rightRumble must be between 0 and 1"), { rightRumble }) }
|
|
131
|
-
if (!Number.
|
|
132
|
+
if (!Number.isInteger(duration)) { throw Object.assign(new Error("duration must be an integer"), { duration }) }
|
|
133
|
+
if (duration < 0) { throw Object.assign(new Error("invalid duration"), { duration }) }
|
|
132
134
|
|
|
133
135
|
// Globals.events.poll() // Errors if it hasn't been called at least once
|
|
134
136
|
Bindings.joystick_rumbleTriggers(this._device.id, leftRumble, rightRumble, duration)
|
|
@@ -7,7 +7,7 @@ const keyboard = {
|
|
|
7
7
|
get SCANCODE () { return Enums.scancodes },
|
|
8
8
|
|
|
9
9
|
getKey (scancode) {
|
|
10
|
-
if (!Number.
|
|
10
|
+
if (!Number.isInteger(scancode)) { throw Object.assign(new Error("scancode must be an integer"), { scancode }) }
|
|
11
11
|
if (scancode < 0 || scancode >= 512) { throw Object.assign(new Error("invalid scancode"), { scancode }) }
|
|
12
12
|
|
|
13
13
|
const _key = Bindings.keyboard_getKey(scancode)
|
|
@@ -5,7 +5,7 @@ const mouse = {
|
|
|
5
5
|
get BUTTON () { return Enums.mouseButtons },
|
|
6
6
|
|
|
7
7
|
getButton (button) {
|
|
8
|
-
if (!Number.
|
|
8
|
+
if (!Number.isInteger(button)) { throw Object.assign(new Error("button must be an integer"), { button }) }
|
|
9
9
|
if (button < 0 || button >= 32) { throw Object.assign(new Error("invalid button"), { button }) }
|
|
10
10
|
|
|
11
11
|
return Bindings.mouse_getButton(button)
|
|
@@ -14,8 +14,8 @@ const mouse = {
|
|
|
14
14
|
get position () { return Bindings.mouse_getPosition() },
|
|
15
15
|
|
|
16
16
|
setPosition (x, y) {
|
|
17
|
-
if (!Number.
|
|
18
|
-
if (!Number.
|
|
17
|
+
if (!Number.isInteger(x)) { throw Object.assign(new Error("x must be an integer"), { x }) }
|
|
18
|
+
if (!Number.isInteger(y)) { throw Object.assign(new Error("y must be an integer"), { y }) }
|
|
19
19
|
|
|
20
20
|
Bindings.mouse_setPosition(x, y)
|
|
21
21
|
},
|
|
@@ -32,18 +32,18 @@ const mouse = {
|
|
|
32
32
|
resetCursor () { Bindings.mouse_resetCursor() },
|
|
33
33
|
|
|
34
34
|
setCursorImage (width, height, stride, format, buffer, x, y) {
|
|
35
|
-
if (!Number.
|
|
35
|
+
if (!Number.isInteger(width)) { throw Object.assign(new Error("width must be an integer"), { width }) }
|
|
36
36
|
if (width <= 0) { throw Object.assign(new Error("invalid width"), { width }) }
|
|
37
|
-
if (!Number.
|
|
37
|
+
if (!Number.isInteger(height)) { throw Object.assign(new Error("height must be an integer"), { height }) }
|
|
38
38
|
if (height <= 0) { throw Object.assign(new Error("invalid height"), { height }) }
|
|
39
|
-
if (!Number.
|
|
39
|
+
if (!Number.isInteger(stride)) { throw Object.assign(new Error("stride must be an integer"), { stride }) }
|
|
40
40
|
if (stride < width) { throw Object.assign(new Error("invalid stride"), { stride, width }) }
|
|
41
41
|
if (typeof format !== 'string') { throw Object.assign(new Error("format must be a string"), { format }) }
|
|
42
42
|
if (!(buffer instanceof Buffer)) { throw Object.assign(new Error("buffer must be a Buffer"), { buffer }) }
|
|
43
43
|
if (buffer.length < stride * height) { throw Object.assign(new Error("buffer is smaller than expected"), { buffer, stride, height }) }
|
|
44
|
-
if (!Number.
|
|
44
|
+
if (!Number.isInteger(x)) { throw Object.assign(new Error("x must be an integer"), { x }) }
|
|
45
45
|
if (x < 0 || x >= width) { throw Object.assign(new Error("invalid x"), { x }) }
|
|
46
|
-
if (!Number.
|
|
46
|
+
if (!Number.isInteger(y)) { throw Object.assign(new Error("y must be an integer"), { y }) }
|
|
47
47
|
if (y < 0 || y >= height) { throw Object.assign(new Error("invalid y"), { y }) }
|
|
48
48
|
|
|
49
49
|
const _format = Enums.pixelFormat[format]
|
|
@@ -57,14 +57,14 @@ class Window extends EventsViaPoll {
|
|
|
57
57
|
|
|
58
58
|
if (typeof title !== 'string') { throw Object.assign(new Error("title must be a string"), { title }) }
|
|
59
59
|
if (display !== null && typeof display !== 'object') { throw Object.assign(new Error("display must be an object"), { display }) }
|
|
60
|
-
if (x !== null && !Number.
|
|
61
|
-
if (y !== null && !Number.
|
|
60
|
+
if (x !== null && !Number.isInteger(x)) { throw Object.assign(new Error("x must be an integer"), { x }) }
|
|
61
|
+
if (y !== null && !Number.isInteger(y)) { throw Object.assign(new Error("y must be an integer"), { y }) }
|
|
62
62
|
if (width !== null) {
|
|
63
|
-
if (!Number.
|
|
63
|
+
if (!Number.isInteger(width)) { throw Object.assign(new Error("width must be an integer"), { width }) }
|
|
64
64
|
if (width <= 0) { throw Object.assign(new Error("invalid width"), { width }) }
|
|
65
65
|
}
|
|
66
66
|
if (height !== null) {
|
|
67
|
-
if (!Number.
|
|
67
|
+
if (!Number.isInteger(height)) { throw Object.assign(new Error("height must be an integer"), { height }) }
|
|
68
68
|
if (height <= 0) { throw Object.assign(new Error("invalid height"), { height }) }
|
|
69
69
|
}
|
|
70
70
|
if (typeof visible !== 'boolean') { throw Object.assign(new Error("visible must be a boolean"), { visible }) }
|
|
@@ -80,7 +80,7 @@ class Window extends EventsViaPoll {
|
|
|
80
80
|
if (typeof popupMenu !== 'boolean') { throw Object.assign(new Error("popupMenu must be a boolean"), { popupMenu }) }
|
|
81
81
|
if (typeof tooltip !== 'boolean') { throw Object.assign(new Error("tooltip must be a boolean"), { tooltip }) }
|
|
82
82
|
if (typeof utility !== 'boolean') { throw Object.assign(new Error("utility must be a boolean"), { utility }) }
|
|
83
|
-
if (display && (
|
|
83
|
+
if (display !== null && (x !== null || y !== null)) { throw Object.assign(new Error("display and x/y are mutually exclusive"), { display, x, y }) }
|
|
84
84
|
if (resizable && borderless) { throw Object.assign(new Error("resizable and borderless are mutually exclusive"), { resizable, borderless }) }
|
|
85
85
|
if (opengl && webgpu) { throw Object.assign(new Error("opengl and webgpu are mutually exclusive"), { opengl, webgpu }) }
|
|
86
86
|
|
|
@@ -186,8 +186,8 @@ class Window extends EventsViaPoll {
|
|
|
186
186
|
setPosition (x, y) {
|
|
187
187
|
if (this._destroyed) { throw Object.assign(new Error("window is destroyed"), { id: this._id }) }
|
|
188
188
|
|
|
189
|
-
if (!Number.
|
|
190
|
-
if (!Number.
|
|
189
|
+
if (!Number.isInteger(x)) { throw Object.assign(new Error("x must be an integer"), { x }) }
|
|
190
|
+
if (!Number.isInteger(y)) { throw Object.assign(new Error("y must be an integer"), { y }) }
|
|
191
191
|
|
|
192
192
|
Bindings.window_setPosition(this._id, x, y)
|
|
193
193
|
this._x = x
|
|
@@ -199,12 +199,35 @@ class Window extends EventsViaPoll {
|
|
|
199
199
|
setSize (width, height) {
|
|
200
200
|
if (this._destroyed) { throw Object.assign(new Error("window is destroyed"), { id: this._id }) }
|
|
201
201
|
|
|
202
|
-
if (!Number.
|
|
202
|
+
if (!Number.isInteger(width)) { throw Object.assign(new Error("width must be an integer"), { width }) }
|
|
203
203
|
if (width <= 0) { throw Object.assign(new Error("invalid width"), { width }) }
|
|
204
|
-
if (!Number.
|
|
204
|
+
if (!Number.isInteger(height)) { throw Object.assign(new Error("height must be an integer"), { height }) }
|
|
205
205
|
if (height <= 0) { throw Object.assign(new Error("invalid height"), { height }) }
|
|
206
206
|
|
|
207
207
|
const { pixelWidth, pixelHeight } = Bindings.window_setSize(this._id, width, height)
|
|
208
|
+
this._width = Math.floor(width)
|
|
209
|
+
this._height = Math.floor(height)
|
|
210
|
+
this._pixelWidth = pixelWidth
|
|
211
|
+
this._pixelHeight = pixelHeight
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
setSizeInPixels (pixelWidth, pixelHeight) {
|
|
215
|
+
if (this._destroyed) { throw Object.assign(new Error("window is destroyed"), { id: this._id }) }
|
|
216
|
+
|
|
217
|
+
if (!Number.isInteger(pixelWidth)) { throw Object.assign(new Error("pixelWidth must be an integer"), { pixelWidth }) }
|
|
218
|
+
if (pixelWidth <= 0) { throw Object.assign(new Error("invalid pixelWidth"), { pixelWidth }) }
|
|
219
|
+
if (!Number.isInteger(pixelHeight)) { throw Object.assign(new Error("pixelHeight must be an integer"), { pixelHeight }) }
|
|
220
|
+
if (pixelHeight <= 0) { throw Object.assign(new Error("invalid pixelHeight"), { pixelHeight }) }
|
|
221
|
+
|
|
222
|
+
const xRatio = this._width / this._pixelWidth
|
|
223
|
+
const yRatio = this._height / this._pixelHeight
|
|
224
|
+
const width = pixelWidth * xRatio
|
|
225
|
+
const height = pixelHeight * yRatio
|
|
226
|
+
|
|
227
|
+
if (!Number.isInteger(width)) { throw Object.assign(new Error(`pixelWidth must be a multiple of ${xRatio}`), { pixelWidth }) }
|
|
228
|
+
if (!Number.isInteger(height)) { throw Object.assign(new Error(`pixelWidth must be a multiple of ${yRatio}`), { pixelWidth }) }
|
|
229
|
+
|
|
230
|
+
Bindings.window_setSize(this._id, width, height)
|
|
208
231
|
this._width = width
|
|
209
232
|
this._height = height
|
|
210
233
|
this._pixelWidth = pixelWidth
|
|
@@ -334,36 +357,40 @@ class Window extends EventsViaPoll {
|
|
|
334
357
|
get tooltip () { return this._tooltip }
|
|
335
358
|
get utility () { return this._utility }
|
|
336
359
|
|
|
337
|
-
render (width, height, stride, format, buffer) {
|
|
360
|
+
render (width, height, stride, format, buffer, scaling = 'nearest') {
|
|
338
361
|
if (this._destroyed) { throw Object.assign(new Error("window is destroyed"), { id: this._id }) }
|
|
339
362
|
|
|
340
363
|
if (this._opengl) { throw new Error("can't call render in opengl mode") }
|
|
341
364
|
if (this._webgpu) { throw new Error("can't call render in webgpu mode") }
|
|
342
365
|
|
|
343
|
-
if (!Number.
|
|
366
|
+
if (!Number.isInteger(width)) { throw Object.assign(new Error("width must be an integer"), { width }) }
|
|
344
367
|
if (width <= 0) { throw Object.assign(new Error("invalid width"), { width }) }
|
|
345
|
-
if (!Number.
|
|
368
|
+
if (!Number.isInteger(height)) { throw Object.assign(new Error("height must be an integer"), { height }) }
|
|
346
369
|
if (height <= 0) { throw Object.assign(new Error("invalid height"), { height }) }
|
|
347
|
-
if (!Number.
|
|
370
|
+
if (!Number.isInteger(stride)) { throw Object.assign(new Error("stride must be an integer"), { stride }) }
|
|
348
371
|
if (stride < width) { throw Object.assign(new Error("invalid stride"), { stride, width }) }
|
|
349
372
|
if (typeof format !== 'string') { throw Object.assign(new Error("format must be a string"), { format }) }
|
|
350
373
|
if (!(buffer instanceof Buffer)) { throw Object.assign(new Error("buffer must be a Buffer"), { buffer }) }
|
|
351
374
|
if (buffer.length < stride * height) { throw Object.assign(new Error("buffer is smaller than expected"), { buffer, stride, height }) }
|
|
375
|
+
if (scaling !== undefined && typeof scaling !== 'string') { throw Object.assign(new Error("scaling must be a string"), { scaling }) }
|
|
352
376
|
|
|
353
377
|
const _format = Enums.pixelFormat[format]
|
|
354
378
|
if (_format === undefined) { throw Object.assign(new Error("invalid format"), { format }) }
|
|
355
379
|
|
|
356
|
-
|
|
380
|
+
const _scaling = Enums.scaleMode[scaling]
|
|
381
|
+
if (_scaling === undefined) { throw Object.assign(new Error("invalid scaling"), { scaling }) }
|
|
382
|
+
|
|
383
|
+
Bindings.window_render(this._id, width, height, stride, _format, buffer, _scaling)
|
|
357
384
|
}
|
|
358
385
|
|
|
359
386
|
setIcon (width, height, stride, format, buffer) {
|
|
360
387
|
if (this._destroyed) { throw Object.assign(new Error("window is destroyed"), { id: this._id }) }
|
|
361
388
|
|
|
362
|
-
if (!Number.
|
|
389
|
+
if (!Number.isInteger(width)) { throw Object.assign(new Error("width must be an integer"), { width }) }
|
|
363
390
|
if (width <= 0) { throw Object.assign(new Error("invalid width"), { width }) }
|
|
364
|
-
if (!Number.
|
|
391
|
+
if (!Number.isInteger(height)) { throw Object.assign(new Error("height must be an integer"), { height }) }
|
|
365
392
|
if (height <= 0) { throw Object.assign(new Error("invalid height"), { height }) }
|
|
366
|
-
if (!Number.
|
|
393
|
+
if (!Number.isInteger(stride)) { throw Object.assign(new Error("stride must be an integer"), { stride }) }
|
|
367
394
|
if (stride < width) { throw Object.assign(new Error("invalid stride"), { stride, width }) }
|
|
368
395
|
if (typeof format !== 'string') { throw Object.assign(new Error("format must be a string"), { format }) }
|
|
369
396
|
if (!(buffer instanceof Buffer)) { throw Object.assign(new Error("buffer must be a Buffer"), { buffer }) }
|
package/src/native/enums.cpp
CHANGED
|
@@ -44,6 +44,11 @@ enums::get(const Napi::CallbackInfo &info)
|
|
|
44
44
|
pixel_formats.Set("nv12", Napi::Number::New(env, SDL_PIXELFORMAT_NV12));
|
|
45
45
|
pixel_formats.Set("nv21", Napi::Number::New(env, SDL_PIXELFORMAT_NV21));
|
|
46
46
|
|
|
47
|
+
Napi::Object scale_mode = Napi::Object::New(env);
|
|
48
|
+
scale_mode.Set("nearest", Napi::Number::New(env, SDL_ScaleModeNearest));
|
|
49
|
+
scale_mode.Set("linear", Napi::Number::New(env, SDL_ScaleModeLinear));
|
|
50
|
+
scale_mode.Set("best", Napi::Number::New(env, SDL_ScaleModeBest));
|
|
51
|
+
|
|
47
52
|
Napi::Object audio_formats = Napi::Object::New(env);
|
|
48
53
|
audio_formats.Set("s8", Napi::Number::New(env, AUDIO_S8));
|
|
49
54
|
audio_formats.Set("u8", Napi::Number::New(env, AUDIO_U8));
|
|
@@ -330,6 +335,7 @@ enums::get(const Napi::CallbackInfo &info)
|
|
|
330
335
|
|
|
331
336
|
Napi::Object all = Napi::Object::New(env);
|
|
332
337
|
all.Set("pixelFormat", pixel_formats);
|
|
338
|
+
all.Set("scaleMode", scale_mode);
|
|
333
339
|
all.Set("audioFormat", audio_formats);
|
|
334
340
|
all.Set("scancodes", scancodes);
|
|
335
341
|
all.Set("mouseButtons", mouse_buttons);
|
package/src/native/window.cpp
CHANGED
|
@@ -529,6 +529,7 @@ window::render (const Napi::CallbackInfo &info)
|
|
|
529
529
|
int stride = info[3].As<Napi::Number>().Int32Value();
|
|
530
530
|
unsigned int format = info[4].As<Napi::Number>().Int32Value();
|
|
531
531
|
void *pixels = info[5].As<Napi::Buffer<char>>().Data();
|
|
532
|
+
SDL_ScaleMode scaling = static_cast<SDL_ScaleMode>(info[6].As<Napi::Number>().Int32Value());
|
|
532
533
|
|
|
533
534
|
SDL_Window *window = SDL_GetWindowFromID(window_id);
|
|
534
535
|
if (window == nullptr) {
|
|
@@ -571,6 +572,13 @@ window::render (const Napi::CallbackInfo &info)
|
|
|
571
572
|
throw Napi::Error::New(env, message.str());
|
|
572
573
|
}
|
|
573
574
|
|
|
575
|
+
if(SDL_SetTextureScaleMode(texture, scaling) < 0) {
|
|
576
|
+
std::ostringstream message;
|
|
577
|
+
message << "SDL_SetTextureScaleMode(" << window_id << ", " << scaling << ") error: " << SDL_GetError();
|
|
578
|
+
SDL_ClearError();
|
|
579
|
+
throw Napi::Error::New(env, message.str());
|
|
580
|
+
}
|
|
581
|
+
|
|
574
582
|
if (SDL_RenderClear(renderer) < 0) {
|
|
575
583
|
SDL_DestroyTexture(texture);
|
|
576
584
|
SDL_FreeSurface(surface);
|
package/src/types/index.d.ts
CHANGED
|
@@ -351,6 +351,11 @@ export namespace Sdl {
|
|
|
351
351
|
| 'nv12'
|
|
352
352
|
| 'nv21'
|
|
353
353
|
|
|
354
|
+
export type Scaling
|
|
355
|
+
= 'nearest'
|
|
356
|
+
| 'linear'
|
|
357
|
+
| 'best'
|
|
358
|
+
|
|
354
359
|
export interface Display {
|
|
355
360
|
readonly name: string
|
|
356
361
|
readonly format: Format
|
|
@@ -417,6 +422,7 @@ export namespace Sdl {
|
|
|
417
422
|
|
|
418
423
|
readonly pixelWidth: number
|
|
419
424
|
readonly pixelHeight: number
|
|
425
|
+
setSizeInPixels (pixelWidth: number, pixelHeight: number): void
|
|
420
426
|
|
|
421
427
|
readonly visible: boolean
|
|
422
428
|
show (show?: boolean): void
|
|
@@ -461,7 +467,7 @@ export namespace Sdl {
|
|
|
461
467
|
readonly tooltip: boolean
|
|
462
468
|
readonly utility: boolean
|
|
463
469
|
|
|
464
|
-
render (width: number, height: number, stride: number, format: Format, buffer: Buffer): void
|
|
470
|
+
render (width: number, height: number, stride: number, format: Format, buffer: Buffer, scaling?: Scaling): void
|
|
465
471
|
|
|
466
472
|
setIcon (width: number, height: number, stride: number, format: Format, buffer: Buffer): void
|
|
467
473
|
|
|
@@ -470,6 +476,7 @@ export namespace Sdl {
|
|
|
470
476
|
|
|
471
477
|
readonly destroyed: boolean
|
|
472
478
|
destroy (): void
|
|
479
|
+
destroyGently (): void
|
|
473
480
|
}
|
|
474
481
|
|
|
475
482
|
interface Module {
|