@kmamal/sdl 0.9.0 → 0.9.2

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
@@ -91,14 +91,14 @@ const renderer = gpu.renderGPUDeviceToWindow({ device, window })
91
91
  // Clear screen to red
92
92
  const commandEncoder = device.createCommandEncoder()
93
93
  const renderPass = commandEncoder.beginRenderPass({
94
- colorAttachments: [
95
- {
96
- view: renderer.getCurrentTextureView(),
97
- clearValue: { r: 1.0, g: 0.0, b: 0.0, a: 1.0 },
98
- loadOp: 'clear',
99
- storeOp: 'store',
100
- },
101
- ],
94
+ colorAttachments: [
95
+ {
96
+ view: renderer.getCurrentTextureView(),
97
+ clearValue: { r: 1.0, g: 0.0, b: 0.0, a: 1.0 },
98
+ loadOp: 'clear',
99
+ storeOp: 'store',
100
+ },
101
+ ],
102
102
  })
103
103
  renderPass.end()
104
104
  device.queue.submit([ commandEncoder.finish() ])
@@ -196,6 +196,8 @@ There are more examples [in the `examples/` folder](https://github.com/kmamal/no
196
196
  * [window.utility](#windowutility)
197
197
  * [window.render(width, height, stride, format, buffer)](#windowrenderwidth-height-stride-format-buffer)
198
198
  * [window.setIcon(width, height, stride, format, buffer)](#windowseticonwidth-height-stride-format-buffer)
199
+ * [window.flash(untilFocused)](#windowflashuntilfocused)
200
+ * [window.stopFlashing()](#windowstopflashing)
199
201
  * [window.destroyed](#windowdestroyed)
200
202
  * [window.destroy()](#windowdestroy)
201
203
  * [sdl.keyboard](#sdlkeyboard)
@@ -836,8 +838,7 @@ Will be `true` if the window is visible.
836
838
 
837
839
  ### window.show([show])
838
840
 
839
- * `show: <boolean>` Set to `true` to make the window visible, `false` to hide it.
840
- Default: `true`
841
+ * `show: <boolean>` Set to `true` to make the window visible, `false` to hide it. Default: `true`
841
842
 
842
843
  Shows or hides the window.
843
844
 
@@ -1032,6 +1033,17 @@ Calls to [`render()`](#windowrenderwidth-height-stride-format-buffer) will fail.
1032
1033
 
1033
1034
  Set's the window's icon, usually displayed in the title bar and the taskbar.
1034
1035
 
1036
+ ### window.flash(untilFocused)
1037
+
1038
+ * `untilFocused: <boolean>` Whether to keep flashing the window until the user focuses it. Default: `false`
1039
+
1040
+ Flash the window briefly to get attention.
1041
+ If `untilFocused` is set, the window will flash until the user focuses it.
1042
+
1043
+ ### window.stopFlashing()
1044
+
1045
+ Stop the window from flashing.
1046
+
1035
1047
  ### window.destroyed
1036
1048
 
1037
1049
  * `<boolean>`
@@ -1429,8 +1441,7 @@ The hotspot represents the pixel that is considered to be under the mouse.
1429
1441
 
1430
1442
  ### sdl.mouse.showCursor([show])
1431
1443
 
1432
- * `show: <boolean>` If `true` then the mouse cursor will be visible.
1433
- Default `true`.
1444
+ * `show: <boolean>` If `true` then the mouse cursor will be visible. Default: `true`
1434
1445
 
1435
1446
  Changes the visibility of the mouse cursor.
1436
1447
 
@@ -1440,8 +1451,7 @@ Equivalent to [`sdl.mouse.showCursor(false)`](#sdlmouseshowcursorshow).
1440
1451
 
1441
1452
  ### sdl.mouse.capture([capture])
1442
1453
 
1443
- * `capture: <boolean>` If `true` the mouse will be captured by the current window.
1444
- Default `true`.
1454
+ * `capture: <boolean>` If `true` the mouse will be captured by the current window. Default: `true`
1445
1455
 
1446
1456
  When the mouse has been captured you will continue receiving mouse events even if the mouse is not over a window.
1447
1457
 
@@ -1690,9 +1700,9 @@ Will be `true` if the joystick has rumble motors.
1690
1700
 
1691
1701
  ### joystickInstance.rumble([low, [high, [duration]]])
1692
1702
 
1693
- * `low: <number>` The intensity of the low frequency rumble motor, from `0` to `1`. Default `1`.
1694
- * `high: <number>` The intensity of the high frequency rumble motor, from `0` to `1`. Default `1`.
1695
- * `duration: <number>` The duration of the rumble, in ms. Default `1e3`.
1703
+ * `low: <number>` The intensity of the low frequency rumble motor, from `0` to `1`. Default: `1`
1704
+ * `high: <number>` The intensity of the high frequency rumble motor, from `0` to `1`. Default: `1`
1705
+ * `duration: <number>` The duration of the rumble, in ms. Default: `1e3`
1696
1706
 
1697
1707
  Makes the joystick rumble for a set `duration`.
1698
1708
  Calling this function again before `duration` has ran out, overrides the previous call.
@@ -1710,9 +1720,9 @@ Will be `true` if the joystick has rumble motors on the triggers.
1710
1720
 
1711
1721
  ### joystickInstance.rumbleTriggers([left, [right, [duration]]])
1712
1722
 
1713
- * `left: <number>` The intensity of the left trigger rumble motor, from `0` to `1`. Default `1`.
1714
- * `right: <number>` The intensity of the right trigger rumble motor, from `0` to `1`. Default `1`.
1715
- * `duration: <number>` The duration of the rumble, in ms. Default `1e3`.
1723
+ * `left: <number>` The intensity of the left trigger rumble motor, from `0` to `1`. Default: `1`
1724
+ * `right: <number>` The intensity of the right trigger rumble motor, from `0` to `1`. Default: `1`
1725
+ * `duration: <number>` The duration of the rumble, in ms. Default: `1e3`
1716
1726
 
1717
1727
  Makes the joystick triggers rumble for a set `duration`.
1718
1728
  Calling this function again before `duration` has ran out, overrides the previous call.
@@ -1954,9 +1964,9 @@ Will be `true` if the controller has rumble motors.
1954
1964
 
1955
1965
  ### controllerInstance.rumble([low, [high, [duration]]])
1956
1966
 
1957
- * `low: <number>` The intensity of the low frequency rumble motor, from `0` to `1`. Default `1`.
1958
- * `high: <number>` The intensity of the high frequency rumble motor, from `0` to `1`. Default `1`.
1959
- * `duration: <number>` The duration of the rumble, in ms. Default `1e3`.
1967
+ * `low: <number>` The intensity of the low frequency rumble motor, from `0` to `1`. Default: `1`
1968
+ * `high: <number>` The intensity of the high frequency rumble motor, from `0` to `1`. Default: `1`
1969
+ * `duration: <number>` The duration of the rumble, in ms. Default: `1e3`
1960
1970
 
1961
1971
  Makes the controller rumble for a set `duration`.
1962
1972
  Calling this function again before `duration` has ran out, overrides the previous call.
@@ -1974,9 +1984,9 @@ Will be `true` if the controller has rumble motors on the triggers.
1974
1984
 
1975
1985
  ### controllerInstance.rumbleTriggers([left, [right, [duration]]])
1976
1986
 
1977
- * `left: <number>` The intensity of the left trigger rumble motor, from `0` to `1`. Default `1`.
1978
- * `right: <number>` The intensity of the right trigger rumble motor, from `0` to `1`. Default `1`.
1979
- * `duration: <number>` The duration of the rumble, in ms. Default `1e3`.
1987
+ * `left: <number>` The intensity of the left trigger rumble motor, from `0` to `1`. Default: `1`
1988
+ * `right: <number>` The intensity of the right trigger rumble motor, from `0` to `1`. Default: `1`
1989
+ * `duration: <number>` The duration of the rumble, in ms. Default: `1e3`
1980
1990
 
1981
1991
  Makes the controller triggers rumble for a set `duration`.
1982
1992
  Calling this function again before `duration` has ran out, overrides the previous call.
@@ -2283,10 +2293,10 @@ const playbackInstance = sdl.audio.openDevice({ type: 'playback' })
2283
2293
  * `type: <string>`: Should be either `'playback'` or `'recording'`.
2284
2294
  * `name: <string>|<null>`: The name of the device. Optional.
2285
2295
  * `options: <object>`
2286
- * `channels: <number>`: Number of audio channels. Valid values: `1`, `2`, `4`, `6`. Default `1`.
2287
- * `frequency: <number>`: The sampling frequency in frames per second. Default `48e3`.
2288
- * `format: `[`<SampleFormat>`](#sample-formats): The binary format for each sample. Default `'f32'`.
2289
- * `buffered: <number>`: Number of frames that will be buffered by the driver. Must be a power of `2`. Default `4096`.
2296
+ * `channels: <number>`: Number of audio channels. Valid values: `1`, `2`, `4`, `6`. Default: `1`
2297
+ * `frequency: <number>`: The sampling frequency in frames per second. Default: `48e3`
2298
+ * `format: `[`<SampleFormat>`](#sample-formats): The binary format for each sample. Default: `'f32'`
2299
+ * `buffered: <number>`: Number of frames that will be buffered by the driver. Must be a power of `2`. Default: `4096`
2290
2300
  * Returns: [`<AudioInstance>`](#class-audioinstance) an object representing the opened audio device instance.
2291
2301
 
2292
2302
  Initializes an audio device for playback/recording and returns a corresponding instance.
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "0.9.0",
2
+ "version": "0.9.2",
3
3
  "name": "@kmamal/sdl",
4
4
  "description": "SDL bindings for Node.js",
5
5
  "keywords": [
@@ -374,6 +374,20 @@ class Window extends EventsViaPoll {
374
374
  Bindings.window_setIcon(this._id, width, height, stride, _format, buffer)
375
375
  }
376
376
 
377
+ flash (untilFocused = false) {
378
+ if (this._destroyed) { throw Object.assign(new Error("window is destroyed"), { id: this._id }) }
379
+
380
+ if (typeof untilFocused !== 'boolean') { throw Object.assign(new Error("untilFocused must be a boolean"), { untilFocused }) }
381
+
382
+ Bindings.window_flash(this._id, untilFocused ? 2 : 1)
383
+ }
384
+
385
+ stopFlashing () {
386
+ if (this._destroyed) { throw Object.assign(new Error("window is destroyed"), { id: this._id }) }
387
+
388
+ Bindings.window_flash(this._id, 0)
389
+ }
390
+
377
391
  get destroyed () { return this._destroyed }
378
392
  destroy () {
379
393
  if (this._destroyed) { throw Object.assign(new Error("window is destroyed"), { id: this._id }) }
@@ -769,7 +769,7 @@ window_restore(napi_env env, napi_callback_info info)
769
769
  }
770
770
 
771
771
  napi_value
772
- window_setIcon(napi_env env, napi_callback_info info)
772
+ window_render(napi_env env, napi_callback_info info)
773
773
  {
774
774
  napi_value argv[6];
775
775
  size_t argc = sizeof(argv) / sizeof(napi_value);
@@ -789,14 +789,14 @@ window_setIcon(napi_env env, napi_callback_info info)
789
789
  void * pixels;
790
790
  CALL_NAPI(napi_get_buffer_info, argv[5], &pixels, nullptr);
791
791
 
792
- CALL_SDL_HELPER(window_setIcon, window_id, w, h, stride, format, pixels);
792
+ CALL_SDL_HELPER(window_render, window_id, w, h, stride, format, pixels);
793
793
 
794
794
  cleanup:
795
795
  return nullptr;
796
796
  }
797
797
 
798
798
  napi_value
799
- window_render(napi_env env, napi_callback_info info)
799
+ window_setIcon(napi_env env, napi_callback_info info)
800
800
  {
801
801
  napi_value argv[6];
802
802
  size_t argc = sizeof(argv) / sizeof(napi_value);
@@ -816,7 +816,26 @@ window_render(napi_env env, napi_callback_info info)
816
816
  void * pixels;
817
817
  CALL_NAPI(napi_get_buffer_info, argv[5], &pixels, nullptr);
818
818
 
819
- CALL_SDL_HELPER(window_render, window_id, w, h, stride, format, pixels);
819
+ CALL_SDL_HELPER(window_setIcon, window_id, w, h, stride, format, pixels);
820
+
821
+ cleanup:
822
+ return nullptr;
823
+ }
824
+
825
+ napi_value
826
+ window_flash(napi_env env, napi_callback_info info)
827
+ {
828
+ napi_value argv[2];
829
+ size_t argc = sizeof(argv) / sizeof(napi_value);
830
+ CALL_NAPI(napi_get_cb_info, info, &argc, argv, nullptr, nullptr);
831
+
832
+ int window_id;
833
+ CALL_NAPI(napi_get_value_int32, argv[0], &window_id);
834
+
835
+ int type;
836
+ CALL_NAPI(napi_get_value_int32, argv[1], &type);
837
+
838
+ CALL_SDL_HELPER(window_flash, window_id, type);
820
839
 
821
840
  cleanup:
822
841
  return nullptr;
@@ -1609,8 +1628,9 @@ init (napi_env env, napi_value exports)
1609
1628
  { "window_maximize", NULL, window_maximize, NULL, NULL, NULL, napi_enumerable, NULL },
1610
1629
  { "window_minimize", NULL, window_minimize, NULL, NULL, NULL, napi_enumerable, NULL },
1611
1630
  { "window_restore", NULL, window_restore, NULL, NULL, NULL, napi_enumerable, NULL },
1612
- { "window_setIcon", NULL, window_setIcon, NULL, NULL, NULL, napi_enumerable, NULL },
1613
1631
  { "window_render", NULL, window_render, NULL, NULL, NULL, napi_enumerable, NULL },
1632
+ { "window_setIcon", NULL, window_setIcon, NULL, NULL, NULL, napi_enumerable, NULL },
1633
+ { "window_flash", NULL, window_flash, NULL, NULL, NULL, napi_enumerable, NULL },
1614
1634
  { "window_destroy", NULL, window_destroy, NULL, NULL, NULL, napi_enumerable, NULL },
1615
1635
  { "keyboard_getKey", NULL, keyboard_getKey, NULL, NULL, NULL, napi_enumerable, NULL },
1616
1636
  { "keyboard_getScancode", NULL, keyboard_getScancode, NULL, NULL, NULL, napi_enumerable, NULL },
@@ -1651,7 +1671,7 @@ init (napi_env env, napi_value exports)
1651
1671
  { "cleanup", NULL, cleanup, NULL, NULL, NULL, napi_enumerable, NULL }
1652
1672
  };
1653
1673
 
1654
- CALL_NAPI(napi_define_properties, exports, 59, desc);
1674
+ CALL_NAPI(napi_define_properties, exports, 60, desc);
1655
1675
 
1656
1676
  cleanup:
1657
1677
  return exports;
@@ -4,18 +4,22 @@
4
4
  #include "cocoa-window.h"
5
5
 
6
6
  extern "C" CALayer * getCocoaGlView(NSWindow * window) {
7
- NSView * view = window.contentView;
8
- [view setWantsLayer:YES];
9
- [[view layer] setContentsScale:[window backingScaleFactor]];
10
- return view.layer;
7
+ @autoreleasepool {
8
+ NSView * view = [window contentView];
9
+ [view setWantsLayer:YES];
10
+ [[view layer] setContentsScale:[window backingScaleFactor]];
11
+ return [view layer];
12
+ }
11
13
  }
12
14
 
13
15
  extern "C" CALayer * getCocoaGpuView(NSWindow * window) {
14
- NSView * view = window.contentView;
15
- [view setWantsLayer:YES];
16
- [view setLayer:[CAMetalLayer layer]];
17
- [[view layer] setContentsScale:[window backingScaleFactor]];
18
- return view.layer;
16
+ @autoreleasepool {
17
+ NSView * view = [window contentView];
18
+ [view setWantsLayer:YES];
19
+ [view setLayer:[CAMetalLayer layer]];
20
+ [[view layer] setContentsScale:[window backingScaleFactor]];
21
+ return [view layer];
22
+ }
19
23
  }
20
24
 
21
25
  extern "C" void reenableInertialScrolling() {
@@ -942,12 +942,10 @@ _windowUpdateRenderer(
942
942
  *accelerated = info.flags & SDL_RENDERER_ACCELERATED;
943
943
  *vsync = info.flags & SDL_RENDERER_PRESENTVSYNC;
944
944
 
945
- ErrorMessage * error;
946
-
947
945
  int pixel_width, pixel_height;
948
946
  SDL_GetWindowSizeInPixels(window, &pixel_width, &pixel_height);
949
947
 
950
- error = _windowUpdateTexture(window, renderer, pixel_width, pixel_height, nullptr);
948
+ ErrorMessage * error = _windowUpdateTexture(window, renderer, pixel_width, pixel_height, nullptr);
951
949
  if (error != nullptr) { return error; }
952
950
 
953
951
  return nullptr;
@@ -1759,30 +1757,6 @@ window_restore (int window_id)
1759
1757
  return nullptr;
1760
1758
  }
1761
1759
 
1762
- ErrorMessage *
1763
- window_setIcon (
1764
- int window_id,
1765
- int w, int h, int stride,
1766
- unsigned int format,
1767
- void * pixels
1768
- ) {
1769
- SDL_Window * window = SDL_GetWindowFromID(window_id);
1770
- if (window == nullptr) {
1771
- RETURN_ERROR("SDL_GetWindowFromID(%d) error: %s\n", window_id, SDL_GetError());
1772
- }
1773
-
1774
- SDL_Surface* surface = SDL_CreateRGBSurfaceWithFormatFrom(pixels, w, h, SDL_BITSPERPIXEL(format), stride, format);
1775
- if (surface == nullptr) {
1776
- RETURN_ERROR("SDL_CreateRGBSurfaceWithFormatFrom(%d, %d, %d, %d) error: %s\n", window_id, w, h, format, SDL_GetError());
1777
- }
1778
-
1779
- SDL_SetWindowIcon(window, surface);
1780
-
1781
- SDL_FreeSurface(surface);
1782
-
1783
- return nullptr;
1784
- }
1785
-
1786
1760
  ErrorMessage *
1787
1761
  window_render (
1788
1762
  int window_id,
@@ -1857,6 +1831,52 @@ window_render (
1857
1831
  return nullptr;
1858
1832
  }
1859
1833
 
1834
+ ErrorMessage *
1835
+ window_setIcon (
1836
+ int window_id,
1837
+ int w, int h, int stride,
1838
+ unsigned int format,
1839
+ void * pixels
1840
+ ) {
1841
+ SDL_Window * window = SDL_GetWindowFromID(window_id);
1842
+ if (window == nullptr) {
1843
+ RETURN_ERROR("SDL_GetWindowFromID(%d) error: %s\n", window_id, SDL_GetError());
1844
+ }
1845
+
1846
+ SDL_Surface* surface = SDL_CreateRGBSurfaceWithFormatFrom(pixels, w, h, SDL_BITSPERPIXEL(format), stride, format);
1847
+ if (surface == nullptr) {
1848
+ RETURN_ERROR("SDL_CreateRGBSurfaceWithFormatFrom(%d, %d, %d, %d) error: %s\n", window_id, w, h, format, SDL_GetError());
1849
+ }
1850
+
1851
+ SDL_SetWindowIcon(window, surface);
1852
+
1853
+ SDL_FreeSurface(surface);
1854
+
1855
+ return nullptr;
1856
+ }
1857
+
1858
+ ErrorMessage *
1859
+ window_flash (int window_id, int type)
1860
+ {
1861
+ SDL_Window * window = SDL_GetWindowFromID(window_id);
1862
+ if (window == nullptr) {
1863
+ RETURN_ERROR("SDL_GetWindowFromID(%d) error: %s\n", window_id, SDL_GetError());
1864
+ }
1865
+
1866
+ SDL_FlashOperation op;
1867
+ switch (type) {
1868
+ case 2: op = SDL_FLASH_UNTIL_FOCUSED; break;
1869
+ case 1: op = SDL_FLASH_BRIEFLY; break;
1870
+ default: op = SDL_FLASH_CANCEL; break;
1871
+ }
1872
+
1873
+ if (SDL_FlashWindow(window, op) != 0) {
1874
+ RETURN_ERROR("SDL_FlashWindow(%d, %d) error: %s\n", window_id, type, SDL_GetError());
1875
+ }
1876
+
1877
+ return nullptr;
1878
+ }
1879
+
1860
1880
  ErrorMessage *
1861
1881
  window_destroy (int window_id)
1862
1882
  {
@@ -2318,7 +2338,7 @@ controller_open (int index, Variant & object)
2318
2338
  }
2319
2339
 
2320
2340
  ErrorMessage * error = _controllerGetState(controller, object);
2321
- if (error) { return error; }
2341
+ if (error != nullptr) { return error; }
2322
2342
 
2323
2343
  return nullptr;
2324
2344
  }
@@ -2341,7 +2361,7 @@ controller_addMappings (char ** mappings, int length)
2341
2361
  {
2342
2362
  for (int i = 0; i < length; i++) {
2343
2363
  if (SDL_GameControllerAddMapping(mappings[i]) == -1) {
2344
- RETURN_ERROR("SDL_GameControllerAddMapping(%s) error: %s\n", mappings[i], error);
2364
+ RETURN_ERROR("SDL_GameControllerAddMapping(%s) error: %s\n", mappings[i], SDL_GetError());
2345
2365
  }
2346
2366
  }
2347
2367
  return nullptr;
@@ -94,18 +94,19 @@ ErrorMessage * window_hide (int window_id);
94
94
  ErrorMessage * window_maximize (int window_id);
95
95
  ErrorMessage * window_minimize (int window_id);
96
96
  ErrorMessage * window_restore (int window_id);
97
- ErrorMessage * window_setIcon (
97
+ ErrorMessage * window_render (
98
98
  int window_id,
99
99
  int w, int h, int stride,
100
100
  unsigned int format,
101
101
  void * pixels
102
102
  );
103
- ErrorMessage * window_render (
103
+ ErrorMessage * window_setIcon (
104
104
  int window_id,
105
105
  int w, int h, int stride,
106
106
  unsigned int format,
107
107
  void * pixels
108
108
  );
109
+ ErrorMessage * window_flash (int window_id, int type);
109
110
  ErrorMessage * window_destroy (int window_id);
110
111
 
111
112
  ErrorMessage * keyboard_getKey (int scancode, const char ** key);
@@ -463,6 +463,9 @@ export namespace Sdl {
463
463
 
464
464
  setIcon (width: number, height: number, stride: number, format: Format, buffer: Buffer): void
465
465
 
466
+ flash (untilFocused: boolean): void
467
+ stopFlashing (): void
468
+
466
469
  readonly destroyed: boolean
467
470
  destroy (): void
468
471
  }