@kmamal/sdl 0.11.2 → 0.11.4

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
@@ -281,6 +281,7 @@ Check the [`examples/`](https://github.com/kmamal/node-sdl/tree/master/examples#
281
281
  * [controllerInstance.device](#controllerinstancedevice)
282
282
  * [controllerInstance.firmwareVersion](#controllerinstancefirmwareversion)
283
283
  * [controllerInstance.serialNumber](#controllerinstanceserialnumber)
284
+ * [controllerInstance.steamHandle](#controllerinstancesteamhandle)
284
285
  * [controllerInstance.axes](#controllerinstanceaxes)
285
286
  * [controllerInstance.buttons](#controllerinstancebuttons)
286
287
  * [controllerInstance.power](#controllerinstancepower)
@@ -1947,6 +1948,13 @@ The controller's firmware version.
1947
1948
 
1948
1949
  The controller's serial number, or `null` if it's not available.
1949
1950
 
1951
+ ### controllerInstance.steamHandle
1952
+
1953
+ * `<Buffer>|<null>`
1954
+
1955
+ The controller's steam handle, or `null` if it's not available.
1956
+ The `Buffer` will contain an `InputHandle_t` for the controller that can be used with the [Steam Input API](https://partner.steamgames.com/doc/api/ISteamInput)
1957
+
1950
1958
  ### controllerInstance.axes
1951
1959
 
1952
1960
  * `<object>`
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "0.11.2",
2
+ "version": "0.11.4",
3
3
  "name": "@kmamal/sdl",
4
4
  "description": "SDL bindings for Node.js",
5
5
  "keywords": [
package/scripts/build.mjs CHANGED
@@ -55,3 +55,8 @@ await Promise.all([
55
55
  }))
56
56
  })(),
57
57
  ])
58
+
59
+ // Strip binaries on linux
60
+ if (C.platform === 'linux') {
61
+ execSync(`strip -s ${Path.join(C.dir.dist, 'sdl.node')}`)
62
+ }
@@ -21,6 +21,7 @@ class ControllerInstance extends EventsViaPoll {
21
21
  this._hasLed = result.hasLed
22
22
  this._hasRumble = result.hasRumble
23
23
  this._hasRumbleTriggers = result.hasRumbleTriggers
24
+ this._steamHandle = result.steamHandle
24
25
  this._axes = result.axes
25
26
  this._buttons = result.buttons
26
27
 
@@ -42,6 +43,7 @@ class ControllerInstance extends EventsViaPoll {
42
43
  get device () { return this._device }
43
44
  get firmwareVersion () { return this._firmwareVersion }
44
45
  get serialNumber () { return this._serialNumber }
46
+ get steamHandle () { return this._steamHandle }
45
47
 
46
48
  get axes () {
47
49
  Globals.events.poll()
@@ -118,16 +118,19 @@ controller::open (const Napi::CallbackInfo &info)
118
118
  throw Napi::Error::New(env, message.str());
119
119
  }
120
120
 
121
- int firmware = SDL_JoystickGetFirmwareVersion(joystick);
122
- Napi::Value firmware_version = firmware == 0 ? env.Null() : Napi::Number::New(env, firmware);
121
+ int _firmware_version = SDL_JoystickGetFirmwareVersion(joystick);
122
+ Napi::Value firmware_version = _firmware_version == 0 ? env.Null() : Napi::Number::New(env, _firmware_version);
123
123
 
124
- const char *serial = SDL_JoystickGetSerial(joystick);
125
- Napi::Value serial_number = serial == 0 ? env.Null() : Napi::String::New(env, serial);
124
+ const char *_serial_number = SDL_JoystickGetSerial(joystick);
125
+ Napi::Value serial_number = _serial_number == 0 ? env.Null() : Napi::String::New(env, _serial_number);
126
126
 
127
127
  bool has_led = SDL_JoystickHasLED(joystick);
128
128
  bool has_rumble = SDL_JoystickHasRumble(joystick);
129
129
  bool has_rumble_triggers = SDL_JoystickHasRumbleTriggers(joystick);
130
130
 
131
+ Uint64 _steam_handle = SDL_GameControllerGetSteamHandle(controller);
132
+ Napi::Value steam_handle = _steam_handle == 0 ? env.Null() : Napi::Buffer<Uint64>::Copy(env, &_steam_handle, 1);
133
+
131
134
  Napi::Object result = Napi::Object::New(env);
132
135
  // result.Set("id", Napi::Number::New(env, joystick_id)); // NOTE: Unused. Same as the device id
133
136
  result.Set("firmwareVersion", firmware_version);
@@ -135,6 +138,7 @@ controller::open (const Napi::CallbackInfo &info)
135
138
  result.Set("hasLed", Napi::Boolean::New(env, has_led));
136
139
  result.Set("hasRumble", Napi::Boolean::New(env, has_rumble));
137
140
  result.Set("hasRumbleTriggers", Napi::Boolean::New(env, has_rumble_triggers));
141
+ result.Set("steamHandle", steam_handle);
138
142
 
139
143
  controller::getState(env, controller, result);
140
144
 
@@ -64,17 +64,17 @@ joystick::_getDevices (Napi::Env &env)
64
64
  char guid_string[33];
65
65
  SDL_JoystickGetGUIDString(guid, guid_string, 33);
66
66
 
67
- int vendor_number = SDL_JoystickGetDeviceVendor(i);
68
- Napi::Value vendor = vendor_number == 0 ? env.Null() : Napi::Number::New(env, vendor_number);
67
+ int _vendor = SDL_JoystickGetDeviceVendor(i);
68
+ Napi::Value vendor = _vendor == 0 ? env.Null() : Napi::Number::New(env, _vendor);
69
69
 
70
- int product_number = SDL_JoystickGetDeviceProduct(i);
71
- Napi::Value product = product_number == 0 ? env.Null() : Napi::Number::New(env, product_number);
70
+ int _product = SDL_JoystickGetDeviceProduct(i);
71
+ Napi::Value product = _product == 0 ? env.Null() : Napi::Number::New(env, _product);
72
72
 
73
- int version_number = SDL_JoystickGetDeviceProductVersion(i);
74
- Napi::Value version = version_number == 0 ? env.Null() : Napi::Number::New(env, version_number);
73
+ int _Version = SDL_JoystickGetDeviceProductVersion(i);
74
+ Napi::Value version = _Version == 0 ? env.Null() : Napi::Number::New(env, _Version);
75
75
 
76
- int player_number = SDL_JoystickGetDevicePlayerIndex(i);
77
- Napi::Value player = player_number == -1 ? env.Null() : Napi::Number::New(env, player_number);
76
+ int _player = SDL_JoystickGetDevicePlayerIndex(i);
77
+ Napi::Value player = _player == -1 ? env.Null() : Napi::Number::New(env, _player);
78
78
 
79
79
  bool is_controller = SDL_IsGameController(i);
80
80
 
@@ -144,11 +144,11 @@ joystick::open (const Napi::CallbackInfo &info)
144
144
  throw Napi::Error::New(env, message.str());
145
145
  }
146
146
 
147
- int firmware = SDL_JoystickGetFirmwareVersion(joystick);
148
- Napi::Value firmware_version = firmware == 0 ? env.Null() : Napi::Number::New(env, firmware);
147
+ int _firmware_version = SDL_JoystickGetFirmwareVersion(joystick);
148
+ Napi::Value firmware_version = _firmware_version == 0 ? env.Null() : Napi::Number::New(env, _firmware_version);
149
149
 
150
- const char *serial = SDL_JoystickGetSerial(joystick);
151
- Napi::Value serial_number = serial == nullptr ? env.Null() : Napi::String::New(env, serial);
150
+ const char *_serial_number = SDL_JoystickGetSerial(joystick);
151
+ Napi::Value serial_number = _serial_number == nullptr ? env.Null() : Napi::String::New(env, _serial_number);
152
152
 
153
153
  SDL_bool has_led = SDL_JoystickHasLED(joystick);
154
154
  SDL_bool has_rumble = SDL_JoystickHasRumble(joystick);
@@ -11,13 +11,17 @@ power::getInfo (const Napi::CallbackInfo &info)
11
11
  {
12
12
  Napi::Env env = info.Env();
13
13
 
14
- int seconds, percent;
15
- SDL_PowerState state = SDL_GetPowerInfo(&seconds, &percent);
14
+ int _seconds, _percent;
15
+ SDL_PowerState _state = SDL_GetPowerInfo(&_seconds, &_percent);
16
+
17
+ Napi::Value state = _state == SDL_POWERSTATE_UNKNOWN ? env.Null() : Napi::String::New(env, power::states[_state]);
18
+ Napi::Value seconds = _seconds == -1 ? env.Null() : Napi::Number::New(env, _seconds);
19
+ Napi::Value percent = _percent == -1 ? env.Null() : Napi::Number::New(env, _percent);
16
20
 
17
21
  Napi::Object result = Napi::Object::New(env);
18
- result.Set("state", state == SDL_POWERSTATE_UNKNOWN ? env.Null() : Napi::String::New(env, power::states[state]));
19
- result.Set("seconds", seconds == -1 ? env.Null() : Napi::Number::New(env, seconds));
20
- result.Set("percent", percent == -1 ? env.Null() : Napi::Number::New(env, percent));
22
+ result.Set("state", state);
23
+ result.Set("seconds", seconds);
24
+ result.Set("percent", percent);
21
25
 
22
26
  return result;
23
27
  }
@@ -33,8 +33,10 @@ sensor::getDevices (const Napi::CallbackInfo &info)
33
33
  throw Napi::Error::New(env, message.str());
34
34
  }
35
35
 
36
- SDL_SensorType type = SDL_SensorGetDeviceType(i);
36
+ SDL_SensorType _type = SDL_SensorGetDeviceType(i);
37
37
  // This function can only error if the index is invalid.
38
+ Napi::Value type = _type == SDL_SENSOR_UNKNOWN ? env.Null() : Napi::String::New(env, sensor::types[_type]);
39
+ Napi::Value side = _type == SDL_SENSOR_UNKNOWN || _type == SDL_SENSOR_ACCEL || _type == SDL_SENSOR_GYRO ? env.Null() : Napi::String::New(env, sensor::sides[_type]);
38
40
 
39
41
  const char *name = SDL_SensorGetDeviceName(i);
40
42
  // This function can only error if the index is invalid.
@@ -43,8 +45,8 @@ sensor::getDevices (const Napi::CallbackInfo &info)
43
45
  device.Set("_index", Napi::Number::New(env, i));
44
46
  device.Set("id", Napi::Number::New(env, id));
45
47
  device.Set("name", Napi::String::New(env, name));
46
- device.Set("type", type == SDL_SENSOR_UNKNOWN ? env.Null() : Napi::String::New(env, sensor::types[type]));
47
- device.Set("side", type == SDL_SENSOR_UNKNOWN || type == SDL_SENSOR_ACCEL || type == SDL_SENSOR_GYRO ? env.Null() : Napi::String::New(env, sensor::sides[type]));
48
+ device.Set("type", type);
49
+ device.Set("side", side);
48
50
 
49
51
  devices.Set(i, device);
50
52
  }
@@ -3,7 +3,17 @@
3
3
  #include <SDL_syswm.h>
4
4
  #include <string>
5
5
  #include <sstream>
6
+ #include <map>
6
7
 
8
+ struct CachedTexture {
9
+ SDL_Texture *texture;
10
+ int width;
11
+ int height;
12
+ unsigned int format;
13
+ SDL_ScaleMode scaling;
14
+ };
15
+
16
+ std::map<SDL_Window*, CachedTexture> cachedTextures;
7
17
 
8
18
  #if defined(__LINUX__)
9
19
  #define NativeWindowHandle Window
@@ -31,7 +41,6 @@
31
41
  #define GPU_WINDOW_FLAG SDL_WINDOW_METAL
32
42
  #endif
33
43
 
34
-
35
44
  void
36
45
  updateRenderer(
37
46
  Napi::Env &env,
@@ -42,6 +51,12 @@ updateRenderer(
42
51
  int window_id = SDL_GetWindowID(window);
43
52
  // Not likely to fail.
44
53
 
54
+ CachedTexture &cached = cachedTextures[window];
55
+ if (cached.texture != nullptr) {
56
+ SDL_DestroyTexture(cached.texture);
57
+ cached.texture = nullptr;
58
+ }
59
+
45
60
  SDL_Renderer *old_renderer = SDL_GetRenderer(window);
46
61
  if (old_renderer != nullptr) {
47
62
  SDL_DestroyRenderer(old_renderer);
@@ -164,49 +179,42 @@ window::create (const Napi::CallbackInfo &info)
164
179
  throw Napi::Error::New(env, message.str());
165
180
  }
166
181
 
167
- int native_handle_size = sizeof(NativeWindowHandle);
168
- Napi::Buffer<char> native_handle_buffer = Napi::Buffer<char>::New(env, native_handle_size);
169
- NativeWindowHandle *native_handle = (NativeWindowHandle *) native_handle_buffer.Data();
170
- native.Set("handle", native_handle_buffer);
171
-
182
+ NativeWindowHandle native_handle;
172
183
  #if defined(__LINUX__)
173
- *native_handle = sys_wm_info.info.x11.window;
184
+ native_handle = sys_wm_info.info.x11.window;
174
185
  #elif defined(__WIN32__)
175
- *native_handle = sys_wm_info.info.win.window;
186
+ native_handle = sys_wm_info.info.win.window;
176
187
  #elif defined(__MACOSX__)
177
- *native_handle = getCocoaWindowHandle(sys_wm_info.info.cocoa.window);
188
+ native_handle = getCocoaWindowHandle(sys_wm_info.info.cocoa.window);
178
189
  #endif
190
+ native.Set("handle", Napi::Buffer<NativeWindowHandle>::Copy(env, &native_handle, 1));
179
191
 
180
192
  if (is_opengl) {
181
- int native_gl_size = sizeof(GL_NativeWindow);
182
- Napi::Buffer<char> native_gl_buffer = Napi::Buffer<char>::New(env, native_gl_size);
183
- GL_NativeWindow *native_gl = (GL_NativeWindow *) native_gl_buffer.Data();
184
- native.Set("gl", native_gl_buffer);
185
-
193
+ GL_NativeWindow native_gl;
186
194
  #if defined(__LINUX__)
187
- *native_gl = sys_wm_info.info.x11.window;
195
+ native_gl = sys_wm_info.info.x11.window;
188
196
  #elif defined(__WIN32__)
189
- *native_gl = sys_wm_info.info.win.window;
197
+ native_gl = sys_wm_info.info.win.window;
190
198
  #elif defined(__MACOSX__)
191
- *native_gl = getCocoaGlView(sys_wm_info.info.cocoa.window);
199
+ native_gl = getCocoaGlView(sys_wm_info.info.cocoa.window);
192
200
  #endif
193
- } else if (is_webgpu) {
194
- int native_gpu_size = sizeof(GPU_NativeData);
195
- Napi::Buffer<char> native_gpu_buffer = Napi::Buffer<char>::New(env, native_gpu_size);
196
- GPU_NativeData *native_gpu = (GPU_NativeData *) native_gpu_buffer.Data();
197
- native.Set("gpu", native_gpu_buffer);
198
-
201
+ native.Set("gl", Napi::Buffer<GL_NativeWindow>::Copy(env, &native_gl, 1));
202
+ }
203
+ else if (is_webgpu) {
204
+ GPU_NativeData native_gpu;
199
205
  #if defined(__LINUX__)
200
- native_gpu->display = sys_wm_info.info.x11.display;
201
- native_gpu->window = sys_wm_info.info.x11.window;
206
+ native_gpu.display = sys_wm_info.info.x11.display;
207
+ native_gpu.window = sys_wm_info.info.x11.window;
202
208
  #elif defined(__WIN32__)
203
- native_gpu->hwnd = sys_wm_info.info.win.window;
204
- native_gpu->hinstance = sys_wm_info.info.win.hinstance;
209
+ native_gpu.hwnd = sys_wm_info.info.win.window;
210
+ native_gpu.hinstance = sys_wm_info.info.win.hinstance;
205
211
  #elif defined(__MACOSX__)
206
- native_gpu->layer = getCocoaGpuView(sys_wm_info.info.cocoa.window);
212
+ native_gpu.layer = getCocoaGpuView(sys_wm_info.info.cocoa.window);
207
213
  #endif
214
+ native.Set("gpu", Napi::Buffer<GPU_NativeData>::Copy(env, &native_gpu, 1));
208
215
  }
209
216
  else {
217
+ cachedTextures[window] = {};
210
218
  updateRenderer(env, window, &is_accelerated, &is_vsync);
211
219
  }
212
220
 
@@ -536,8 +544,8 @@ window::render (const Napi::CallbackInfo &info)
536
544
  Napi::Env env = info.Env();
537
545
 
538
546
  int window_id = info[0].As<Napi::Number>().Int32Value();
539
- int w = info[1].As<Napi::Number>().Int32Value();
540
- int h = info[2].As<Napi::Number>().Int32Value();
547
+ int width = info[1].As<Napi::Number>().Int32Value();
548
+ int height = info[2].As<Napi::Number>().Int32Value();
541
549
  int stride = info[3].As<Napi::Number>().Int32Value();
542
550
  unsigned int format = info[4].As<Napi::Number>().Int32Value();
543
551
  void *pixels = info[5].As<Napi::Buffer<char>>().Data();
@@ -569,42 +577,43 @@ window::render (const Napi::CallbackInfo &info)
569
577
  throw Napi::Error::New(env, message.str());
570
578
  }
571
579
 
572
- SDL_Surface *surface = SDL_CreateRGBSurfaceWithFormatFrom(pixels, w, h, SDL_BITSPERPIXEL(format), stride, format);
573
- if (surface == nullptr) {
574
- std::ostringstream message;
575
- message << "SDL_CreateRGBSurfaceWithFormatFrom(" << window_id << ", " << w << ", " << h << ", " << stride << ", " << format << ") error: " << SDL_GetError();
576
- SDL_ClearError();
577
- throw Napi::Error::New(env, message.str());
578
- }
580
+ CachedTexture &cached = cachedTextures[window];
581
+ SDL_Texture *texture = cached.texture;
579
582
 
580
- if (SDL_SetSurfaceBlendMode(surface, SDL_BLENDMODE_NONE) < 0) {
581
- std::ostringstream message;
582
- message << "SDL_SetSurfaceBlendMode(" << window_id << ") error: " << SDL_GetError();
583
- SDL_ClearError();
584
- throw Napi::Error::New(env, message.str());
585
- }
583
+ if (texture == nullptr
584
+ || cached.width != width
585
+ || cached.height != height
586
+ || cached.format != format
587
+ || cached.scaling != scaling
588
+ ) {
589
+ if (texture != nullptr) { SDL_DestroyTexture(texture); }
586
590
 
587
- SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, surface);
588
- if (texture == nullptr) {
589
- SDL_FreeSurface(surface);
591
+ texture = SDL_CreateTexture(renderer, format, SDL_TEXTUREACCESS_STREAMING, width, height);
590
592
 
591
- std::ostringstream message;
592
- message << "SDL_CreateTextureFromSurface(" << window_id << ") error: " << SDL_GetError();
593
- SDL_ClearError();
594
- throw Napi::Error::New(env, message.str());
595
- }
593
+ if (SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_NONE) < 0) {
594
+ std::ostringstream message;
595
+ message << "SDL_SetTextureBlendMode(" << window_id << ") error: " << SDL_GetError();
596
+ SDL_ClearError();
597
+ throw Napi::Error::New(env, message.str());
598
+ }
596
599
 
597
- if(SDL_SetTextureScaleMode(texture, scaling) < 0) {
598
- std::ostringstream message;
599
- message << "SDL_SetTextureScaleMode(" << window_id << ", " << scaling << ") error: " << SDL_GetError();
600
- SDL_ClearError();
601
- throw Napi::Error::New(env, message.str());
600
+ if(SDL_SetTextureScaleMode(texture, scaling) < 0) {
601
+ std::ostringstream message;
602
+ message << "SDL_SetTextureScaleMode(" << window_id << ", " << scaling << ") error: " << SDL_GetError();
603
+ SDL_ClearError();
604
+ throw Napi::Error::New(env, message.str());
605
+ }
606
+
607
+ cached.texture = texture;
608
+ cached.width = width;
609
+ cached.height = height;
610
+ cached.format = format;
611
+ cached.scaling = scaling;
602
612
  }
603
613
 
604
- if (SDL_RenderClear(renderer) < 0) {
605
- SDL_DestroyTexture(texture);
606
- SDL_FreeSurface(surface);
614
+ SDL_UpdateTexture(texture, nullptr, pixels, stride);
607
615
 
616
+ if (SDL_RenderClear(renderer) < 0) {
608
617
  std::ostringstream message;
609
618
  message << "SDL_RenderClear(" << window_id << ") error: " << SDL_GetError();
610
619
  SDL_ClearError();
@@ -612,9 +621,6 @@ window::render (const Napi::CallbackInfo &info)
612
621
  }
613
622
 
614
623
  if (SDL_RenderCopy(renderer, texture, nullptr, hasDstRect ? &rect : nullptr) < 0) {
615
- SDL_DestroyTexture(texture);
616
- SDL_FreeSurface(surface);
617
-
618
624
  std::ostringstream message;
619
625
  message << "SDL_RenderCopy(" << window_id << ") error: " << SDL_GetError();
620
626
  SDL_ClearError();
@@ -623,8 +629,6 @@ window::render (const Napi::CallbackInfo &info)
623
629
 
624
630
  SDL_RenderPresent(renderer);
625
631
 
626
- SDL_DestroyTexture(texture);
627
- SDL_FreeSurface(surface);
628
632
  return env.Undefined();
629
633
  }
630
634
 
@@ -712,7 +716,13 @@ window::destroy (const Napi::CallbackInfo &info)
712
716
  }
713
717
 
714
718
  SDL_Renderer *renderer = SDL_GetRenderer(window);
715
- if (renderer != nullptr) { SDL_DestroyRenderer(renderer); }
719
+ if (renderer != nullptr) {
720
+ SDL_DestroyRenderer(renderer);
721
+
722
+ CachedTexture &cached = cachedTextures[window];
723
+ if (cached.texture != nullptr) { SDL_DestroyTexture(cached.texture); }
724
+ cachedTextures.erase(window);
725
+ }
716
726
 
717
727
  SDL_DestroyWindow(window);
718
728
 
@@ -985,6 +985,7 @@ export namespace Sdl {
985
985
  readonly device: Device
986
986
  readonly firmwareVersion: number
987
987
  readonly serialNumber: string
988
+ readonly steamHandle: Buffer | null
988
989
 
989
990
  readonly axes: {
990
991
  readonly leftStickX: number