@kmamal/sdl 1.0.0-5 → 1.0.0-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.
Files changed (90) hide show
  1. package/CHANGELOG.md +12 -6
  2. package/README.md +2 -2
  3. package/binding.gyp +21 -4
  4. package/package.json +5 -12
  5. package/scripts/build.mjs +3 -1
  6. package/scripts/install.mjs +2 -0
  7. package/scripts/util/common.js +3 -2
  8. package/src/javascript/audio/audio-playback-stream.js +9 -10
  9. package/src/javascript/audio/audio-recording-stream.js +9 -8
  10. package/src/javascript/audio/audio-stream.js +101 -44
  11. package/src/javascript/audio/conversion.js +41 -0
  12. package/src/javascript/audio/devices.js +11 -0
  13. package/src/javascript/audio/events.js +65 -0
  14. package/src/javascript/audio/format-helpers.js +8 -4
  15. package/src/javascript/audio/index.js +25 -20
  16. package/src/javascript/audio/prevent-exit.js +13 -10
  17. package/src/javascript/audio/validate.js +9 -0
  18. package/src/javascript/cleanup.js +9 -6
  19. package/src/javascript/clipboard/index.js +8 -3
  20. package/src/javascript/device-instance.js +61 -0
  21. package/src/javascript/events/events-via-poll.js +32 -28
  22. package/src/javascript/events/index.js +85 -479
  23. package/src/javascript/events/try.js +25 -0
  24. package/src/javascript/gamepad/{device.js → devices.js} +4 -5
  25. package/src/javascript/gamepad/events.js +66 -0
  26. package/src/javascript/gamepad/gamepad-instance.js +12 -160
  27. package/src/javascript/gamepad/index.js +15 -16
  28. package/src/javascript/globals.js +10 -1
  29. package/src/javascript/index.js +2 -5
  30. package/src/javascript/joystick/devices.js +43 -0
  31. package/src/javascript/joystick/events.js +129 -0
  32. package/src/javascript/joystick/index.js +9 -18
  33. package/src/javascript/joystick/joystick-device-instance.js +176 -0
  34. package/src/javascript/joystick/joystick-instance.js +12 -158
  35. package/src/javascript/keyboard/index.js +20 -10
  36. package/src/javascript/keyboard/key-mapping.js +46 -27
  37. package/src/javascript/mouse/index.js +36 -29
  38. package/src/javascript/sensor/events.js +21 -0
  39. package/src/javascript/sensor/index.js +13 -2
  40. package/src/javascript/sensor/sensor-instance.js +7 -47
  41. package/src/javascript/subsystems.js +56 -0
  42. package/src/javascript/touch/index.js +9 -4
  43. package/src/javascript/validate.js +119 -0
  44. package/src/javascript/video/conversion.js +63 -0
  45. package/src/javascript/video/events.js +179 -0
  46. package/src/javascript/video/format-helpers.js +10 -8
  47. package/src/javascript/video/index.js +28 -14
  48. package/src/javascript/video/validate.js +45 -0
  49. package/src/javascript/video/window.js +196 -167
  50. package/src/native/audio.cpp +457 -153
  51. package/src/native/audio.h +25 -19
  52. package/src/native/clipboard.cpp +8 -37
  53. package/src/native/clipboard.h +2 -2
  54. package/src/native/enums.cpp +16 -1
  55. package/src/native/enums.h +1 -1
  56. package/src/native/events.cpp +351 -357
  57. package/src/native/events.h +70 -68
  58. package/src/native/gamepad.cpp +147 -103
  59. package/src/native/gamepad.h +10 -11
  60. package/src/native/global.cpp +183 -340
  61. package/src/native/global.h +51 -5
  62. package/src/native/joystick.cpp +204 -275
  63. package/src/native/joystick.h +21 -15
  64. package/src/native/keyboard.cpp +63 -25
  65. package/src/native/keyboard.h +5 -4
  66. package/src/native/module.cpp +23 -3
  67. package/src/native/mouse.cpp +28 -59
  68. package/src/native/mouse.h +9 -9
  69. package/src/native/packed.cpp +99 -0
  70. package/src/native/packed.h +86 -0
  71. package/src/native/power.cpp +13 -1
  72. package/src/native/power.h +3 -3
  73. package/src/native/sensor.cpp +78 -69
  74. package/src/native/sensor.h +7 -7
  75. package/src/native/testing.cpp +148 -0
  76. package/src/native/testing.h +17 -0
  77. package/src/native/threads.cpp +301 -0
  78. package/src/native/threads.h +36 -0
  79. package/src/native/touch.cpp +38 -44
  80. package/src/native/touch.h +3 -2
  81. package/src/native/video.cpp +211 -98
  82. package/src/native/video.h +7 -4
  83. package/src/native/window.cpp +201 -219
  84. package/src/native/window.h +24 -20
  85. package/src/types/index.d.ts +157 -70
  86. package/src/javascript/audio/device.js +0 -37
  87. package/src/javascript/joystick/device.js +0 -13
  88. package/src/javascript/joystick/shared.js +0 -90
  89. package/src/javascript/worker.js +0 -88
  90. package/src/types/worker.d.ts +0 -71
@@ -1,24 +1,30 @@
1
1
  #include "window.h"
2
+ #include "global.h"
2
3
  #include <SDL3/SDL.h>
3
4
  #include <string>
4
- #include <sstream>
5
5
  #include <cstdint>
6
6
  #include <cmath>
7
7
  #include <algorithm>
8
8
 
9
9
  static const char *TEXTURE_PROPERTY = "kmamal.sdl.texture";
10
10
 
11
- static SDL_Window *
12
- getWindow (Napi::Env &env, int window_id)
11
+ void
12
+ window::resetTexture (SDL_Window *window)
13
13
  {
14
- SDL_Window *window = SDL_GetWindowFromID(window_id);
15
- if (window == nullptr) {
16
- std::ostringstream message;
17
- message << "SDL_GetWindowFromID(" << window_id << ") error: invalid window id";
18
- SDL_ClearError();
19
- throw Napi::Error::New(env, message.str());
14
+ SDL_PropertiesID props = SDL_GetWindowProperties(window);
15
+ SDL_Texture *texture = (SDL_Texture *) SDL_GetPointerProperty(props, TEXTURE_PROPERTY, nullptr);
16
+ if (texture != nullptr) {
17
+ SDL_DestroyTexture(texture);
18
+ SDL_SetPointerProperty(props, TEXTURE_PROPERTY, nullptr);
20
19
  }
21
- return window;
20
+ }
21
+
22
+ static Napi::FunctionReference hit_test_handler;
23
+
24
+ static SDL_Window *
25
+ getWindow (Napi::Env &env, SDL_WindowID window_id)
26
+ {
27
+ return global::fromIdOrThrow(env, SDL_GetWindowFromID(window_id), "SDL_GetWindowFromID", "window", window_id);
22
28
  }
23
29
 
24
30
  #if defined(SDL_PLATFORM_LINUX) || defined(SDL_PLATFORM_FREEBSD) || defined(SDL_PLATFORM_OPENBSD) || defined(SDL_PLATFORM_NETBSD)
@@ -35,6 +41,25 @@ getWindow (Napi::Env &env, int window_id)
35
41
  #define GL_NativeWindow LinuxNativeData
36
42
  #define GPU_NativeData LinuxNativeData
37
43
  #define GPU_WINDOW_FLAG SDL_WINDOW_VULKAN
44
+
45
+ static LinuxNativeData
46
+ getLinuxNativeData (SDL_PropertiesID window_props, bool is_x11, bool use_egl_window)
47
+ {
48
+ if (is_x11) {
49
+ return {
50
+ 1,
51
+ SDL_GetPointerProperty(window_props, SDL_PROP_WINDOW_X11_DISPLAY_POINTER, nullptr),
52
+ (uintptr_t) SDL_GetNumberProperty(window_props, SDL_PROP_WINDOW_X11_WINDOW_NUMBER, 0),
53
+ };
54
+ }
55
+ return {
56
+ 2,
57
+ SDL_GetPointerProperty(window_props, SDL_PROP_WINDOW_WAYLAND_DISPLAY_POINTER, nullptr),
58
+ (uintptr_t) SDL_GetPointerProperty(window_props, use_egl_window
59
+ ? SDL_PROP_WINDOW_WAYLAND_EGL_WINDOW_POINTER
60
+ : SDL_PROP_WINDOW_WAYLAND_SURFACE_POINTER, nullptr),
61
+ };
62
+ }
38
63
  #elif defined(SDL_PLATFORM_WIN32)
39
64
  #define NativeWindowHandle void *
40
65
  #define GL_NativeWindow void *
@@ -53,16 +78,18 @@ getWindow (Napi::Env &env, int window_id)
53
78
  #define GPU_WINDOW_FLAG SDL_WINDOW_METAL
54
79
  #endif
55
80
 
56
- void
57
- updateRenderer(
81
+ static void
82
+ updateRenderer (
58
83
  Napi::Env &env,
59
84
  SDL_Window *window,
60
85
  bool *is_accelerated,
61
86
  int *vsync
62
87
  ) {
63
- int window_id = SDL_GetWindowID(window);
88
+ SDL_WindowID window_id = SDL_GetWindowID(window);
64
89
  // Not likely to fail.
65
90
 
91
+ // The renderer is recreated even when only vsync changed and
92
+ // SDL_SetRenderVSync() alone would do. The code is simpler this way.
66
93
  SDL_Renderer *old_renderer = SDL_GetRenderer(window);
67
94
  if (old_renderer != nullptr) {
68
95
  SDL_DestroyRenderer(old_renderer);
@@ -81,13 +108,11 @@ updateRenderer(
81
108
  }
82
109
 
83
110
  if (renderer == nullptr) {
84
- std::ostringstream message;
85
- message << "SDL_CreateRenderer(" << window_id << ", " << *is_accelerated << ", " << *vsync << ") error: " << SDL_GetError();
86
- SDL_ClearError();
87
- throw Napi::Error::New(env, message.str());
111
+ throw global::sdlError(env, "SDL_CreateRenderer", window_id, *is_accelerated, *vsync);
88
112
  }
89
113
 
90
114
  if (!SDL_SetRenderVSync(renderer, *vsync)) { SDL_ClearError(); }
115
+ if (!SDL_SetRenderDrawColor(renderer, 0, 0, 0, SDL_ALPHA_OPAQUE)) { SDL_ClearError(); }
91
116
 
92
117
  const char *renderer_name = SDL_GetRendererName(renderer);
93
118
  if (renderer_name == nullptr) { SDL_ClearError(); }
@@ -104,8 +129,8 @@ window::create (const Napi::CallbackInfo &info)
104
129
  {
105
130
  Napi::Env env = info.Env();
106
131
 
107
- std:: string title = info[0].As<Napi::String>().Utf8Value();
108
- int display = info[1].As<Napi::Number>().Int32Value();
132
+ std::string title = info[0].As<Napi::String>().Utf8Value();
133
+ SDL_DisplayID display = info[1].As<Napi::Number>().Uint32Value();
109
134
  int x = info[2].IsNull() ? (int) SDL_WINDOWPOS_CENTERED_DISPLAY(display) : info[2].As<Napi::Number>().Int32Value();
110
135
  int y = info[3].IsNull() ? (int) SDL_WINDOWPOS_CENTERED_DISPLAY(display) : info[3].As<Napi::Number>().Int32Value();
111
136
  int width = info[4].IsNull() ? 640 : info[4].As<Napi::Number>().Int32Value();
@@ -133,34 +158,37 @@ window::create (const Napi::CallbackInfo &info)
133
158
 
134
159
  SDL_PropertiesID create_props = SDL_CreateProperties();
135
160
  if (create_props == 0) {
136
- std::ostringstream message;
137
- message << "SDL_CreateProperties() error: " << SDL_GetError();
138
- SDL_ClearError();
139
- throw Napi::Error::New(env, message.str());
161
+ throw global::sdlError(env, "SDL_CreateProperties");
162
+ }
163
+ if (!SDL_SetStringProperty(create_props, SDL_PROP_WINDOW_CREATE_TITLE_STRING, title.c_str())) {
164
+ Napi::Error error = global::sdlError(env, "SDL_SetStringProperty", title);
165
+ SDL_DestroyProperties(create_props);
166
+ throw error;
167
+ }
168
+ bool numbers_set = true
169
+ && SDL_SetNumberProperty(create_props, SDL_PROP_WINDOW_CREATE_X_NUMBER, x)
170
+ && SDL_SetNumberProperty(create_props, SDL_PROP_WINDOW_CREATE_Y_NUMBER, y)
171
+ && SDL_SetNumberProperty(create_props, SDL_PROP_WINDOW_CREATE_WIDTH_NUMBER, width)
172
+ && SDL_SetNumberProperty(create_props, SDL_PROP_WINDOW_CREATE_HEIGHT_NUMBER, height)
173
+ && SDL_SetNumberProperty(create_props, SDL_PROP_WINDOW_CREATE_FLAGS_NUMBER, desired_flags);
174
+ if (!numbers_set) {
175
+ Napi::Error error = global::sdlError(env, "SDL_SetNumberProperty", x, y, width, height, desired_flags);
176
+ SDL_DestroyProperties(create_props);
177
+ throw error;
140
178
  }
141
- SDL_SetStringProperty(create_props, SDL_PROP_WINDOW_CREATE_TITLE_STRING, title.c_str());
142
- SDL_SetNumberProperty(create_props, SDL_PROP_WINDOW_CREATE_X_NUMBER, x);
143
- SDL_SetNumberProperty(create_props, SDL_PROP_WINDOW_CREATE_Y_NUMBER, y);
144
- SDL_SetNumberProperty(create_props, SDL_PROP_WINDOW_CREATE_WIDTH_NUMBER, width);
145
- SDL_SetNumberProperty(create_props, SDL_PROP_WINDOW_CREATE_HEIGHT_NUMBER, height);
146
- SDL_SetNumberProperty(create_props, SDL_PROP_WINDOW_CREATE_FLAGS_NUMBER, desired_flags);
147
179
 
148
180
  SDL_Window *window = SDL_CreateWindowWithProperties(create_props);
149
- SDL_DestroyProperties(create_props);
150
181
  if (window == nullptr) {
151
- std::ostringstream message;
152
- message << "SDL_CreateWindow() error: " << SDL_GetError();
153
- SDL_ClearError();
154
- throw Napi::Error::New(env, message.str());
182
+ Napi::Error error = global::sdlError(env, "SDL_CreateWindowWithProperties", title, x, y, width, height, desired_flags);
183
+ SDL_DestroyProperties(create_props);
184
+ throw error;
155
185
  }
186
+ SDL_DestroyProperties(create_props);
156
187
 
157
188
  try {
158
- int window_id = SDL_GetWindowID(window);
189
+ SDL_WindowID window_id = SDL_GetWindowID(window);
159
190
  if (window_id == 0) {
160
- std::ostringstream message;
161
- message << "SDL_GetWindowID() error: " << SDL_GetError();
162
- SDL_ClearError();
163
- throw Napi::Error::New(env, message.str());
191
+ throw global::sdlError(env, "SDL_GetWindowID");
164
192
  }
165
193
 
166
194
  if (!SDL_StartTextInput(window)) { SDL_ClearError(); }
@@ -197,24 +225,12 @@ window::create (const Napi::CallbackInfo &info)
197
225
  native.Set("subsystem", env.Null());
198
226
  #endif
199
227
 
228
+ // Queried before updateRenderer(), and on X11 SDL's GL renderer recreates the window, so this handle may be stale; it only matters for opengl/webgpu windows, which have no renderer.
200
229
  Napi::Value native_handle;
201
230
  if (has_wm_info) {
202
231
  NativeWindowHandle _native_handle;
203
232
  #if defined(PLATFORM_LINUX_OR_BSD)
204
- if (is_x11) {
205
- _native_handle = {
206
- 1,
207
- SDL_GetPointerProperty(window_props, SDL_PROP_WINDOW_X11_DISPLAY_POINTER, nullptr),
208
- (uintptr_t) SDL_GetNumberProperty(window_props, SDL_PROP_WINDOW_X11_WINDOW_NUMBER, 0),
209
- };
210
- }
211
- else {
212
- _native_handle = {
213
- 2,
214
- SDL_GetPointerProperty(window_props, SDL_PROP_WINDOW_WAYLAND_DISPLAY_POINTER, nullptr),
215
- (uintptr_t) SDL_GetPointerProperty(window_props, SDL_PROP_WINDOW_WAYLAND_SURFACE_POINTER, nullptr),
216
- };
217
- }
233
+ _native_handle = getLinuxNativeData(window_props, is_x11, false);
218
234
  #elif defined(SDL_PLATFORM_WIN32)
219
235
  _native_handle = SDL_GetPointerProperty(window_props, SDL_PROP_WINDOW_WIN32_HWND_POINTER, nullptr);
220
236
  #elif defined(SDL_PLATFORM_MACOS)
@@ -238,20 +254,7 @@ window::create (const Napi::CallbackInfo &info)
238
254
 
239
255
  GL_NativeWindow native_gl;
240
256
  #if defined(PLATFORM_LINUX_OR_BSD)
241
- if (is_x11) {
242
- native_gl = {
243
- 1,
244
- SDL_GetPointerProperty(window_props, SDL_PROP_WINDOW_X11_DISPLAY_POINTER, nullptr),
245
- (uintptr_t) SDL_GetNumberProperty(window_props, SDL_PROP_WINDOW_X11_WINDOW_NUMBER, 0),
246
- };
247
- }
248
- else {
249
- native_gl = {
250
- 2,
251
- SDL_GetPointerProperty(window_props, SDL_PROP_WINDOW_WAYLAND_DISPLAY_POINTER, nullptr),
252
- (uintptr_t) SDL_GetPointerProperty(window_props, SDL_PROP_WINDOW_WAYLAND_EGL_WINDOW_POINTER, nullptr),
253
- };
254
- }
257
+ native_gl = getLinuxNativeData(window_props, is_x11, true);
255
258
  #elif defined(SDL_PLATFORM_WIN32)
256
259
  native_gl = SDL_GetPointerProperty(window_props, SDL_PROP_WINDOW_WIN32_HWND_POINTER, nullptr);
257
260
  #elif defined(SDL_PLATFORM_MACOS)
@@ -270,20 +273,7 @@ window::create (const Napi::CallbackInfo &info)
270
273
 
271
274
  GPU_NativeData native_gpu;
272
275
  #if defined(PLATFORM_LINUX_OR_BSD)
273
- if (is_x11) {
274
- native_gpu = {
275
- 1,
276
- SDL_GetPointerProperty(window_props, SDL_PROP_WINDOW_X11_DISPLAY_POINTER, nullptr),
277
- (uintptr_t) SDL_GetNumberProperty(window_props, SDL_PROP_WINDOW_X11_WINDOW_NUMBER, 0),
278
- };
279
- }
280
- else {
281
- native_gpu = {
282
- 2,
283
- SDL_GetPointerProperty(window_props, SDL_PROP_WINDOW_WAYLAND_DISPLAY_POINTER, nullptr),
284
- (uintptr_t) SDL_GetPointerProperty(window_props, SDL_PROP_WINDOW_WAYLAND_SURFACE_POINTER, nullptr),
285
- };
286
- }
276
+ native_gpu = getLinuxNativeData(window_props, is_x11, false);
287
277
  #elif defined(SDL_PLATFORM_WIN32)
288
278
  native_gpu.hwnd = SDL_GetPointerProperty(window_props, SDL_PROP_WINDOW_WIN32_HWND_POINTER, nullptr);
289
279
  native_gpu.hinstance = SDL_GetPointerProperty(window_props, SDL_PROP_WINDOW_WIN32_INSTANCE_POINTER, nullptr);
@@ -298,34 +288,39 @@ window::create (const Napi::CallbackInfo &info)
298
288
  }
299
289
 
300
290
  if (is_visible && !SDL_ShowWindow(window)) {
301
- std::ostringstream message;
302
- message << "SDL_ShowWindow(" << window_id << ") error: " << SDL_GetError();
303
- SDL_ClearError();
304
- throw Napi::Error::New(env, message.str());
291
+ throw global::sdlError(env, "SDL_ShowWindow", window_id);
305
292
  }
306
293
 
307
294
  Uint64 actual_flags = SDL_GetWindowFlags(window);
295
+ is_visible = !(actual_flags & SDL_WINDOW_HIDDEN);
308
296
  is_fullscreen = actual_flags & SDL_WINDOW_FULLSCREEN;
309
297
  is_resizable = actual_flags & SDL_WINDOW_RESIZABLE;
310
298
  is_borderless = actual_flags & SDL_WINDOW_BORDERLESS;
311
299
  is_always_on_top = actual_flags & SDL_WINDOW_ALWAYS_ON_TOP;
300
+ bool is_minimized = actual_flags & SDL_WINDOW_MINIMIZED;
301
+ bool is_maximized = actual_flags & SDL_WINDOW_MAXIMIZED;
312
302
 
313
- SDL_GetWindowPosition(window, &x, &y);
314
- SDL_GetWindowSize(window, &width, &height);
303
+ if (!SDL_GetWindowPosition(window, &x, &y)) {
304
+ throw global::sdlError(env, "SDL_GetWindowPosition", window_id);
305
+ }
306
+ if (!SDL_GetWindowSize(window, &width, &height)) {
307
+ throw global::sdlError(env, "SDL_GetWindowSize", window_id);
308
+ }
315
309
 
316
310
  int pixel_width, pixel_height;
317
- SDL_GetWindowSizeInPixels(window, &pixel_width, &pixel_height);
311
+ if (!SDL_GetWindowSizeInPixels(window, &pixel_width, &pixel_height)) {
312
+ throw global::sdlError(env, "SDL_GetWindowSizeInPixels", window_id);
313
+ }
318
314
 
319
315
  display = SDL_GetDisplayForWindow(window);
320
316
  if (display == 0) {
321
- std::ostringstream message;
322
- message << "SDL_GetDisplayForWindow(" << window_id << ") error: " << SDL_GetError();
323
- SDL_ClearError();
324
- throw Napi::Error::New(env, message.str());
317
+ throw global::sdlError(env, "SDL_GetDisplayForWindow", window_id);
325
318
  }
326
319
 
327
320
  Napi::Object result = Napi::Object::New(env);
328
321
  result.Set("id", window_id);
322
+ result.Set("title", SDL_GetWindowTitle(window));
323
+ result.Set("visible", is_visible);
329
324
  result.Set("x", x);
330
325
  result.Set("y", y);
331
326
  result.Set("width", width);
@@ -337,6 +332,8 @@ window::create (const Napi::CallbackInfo &info)
337
332
  result.Set("resizable", is_resizable);
338
333
  result.Set("borderless", is_borderless);
339
334
  result.Set("alwaysOnTop", is_always_on_top);
335
+ result.Set("minimized", is_minimized);
336
+ result.Set("maximized", is_maximized);
340
337
  if (has_renderer) {
341
338
  result.Set("accelerated", is_accelerated);
342
339
  result.Set("vsync", vsync);
@@ -350,6 +347,7 @@ window::create (const Napi::CallbackInfo &info)
350
347
  return result;
351
348
  }
352
349
  catch (...) {
350
+ // SDL_DestroyWindow() tears the renderer down but doesn't free it, so without this it would leak until SDL_Quit().
353
351
  SDL_Renderer *renderer = SDL_GetRenderer(window);
354
352
  if (renderer != nullptr) { SDL_DestroyRenderer(renderer); }
355
353
  SDL_DestroyWindow(window);
@@ -362,19 +360,16 @@ window::setTitle (const Napi::CallbackInfo &info)
362
360
  {
363
361
  Napi::Env env = info.Env();
364
362
 
365
- int window_id = info[0].As<Napi::Number>().Int32Value();
366
- std:: string title = info[1].As<Napi::String>().Utf8Value();
363
+ SDL_WindowID window_id = info[0].As<Napi::Number>().Uint32Value();
364
+ std::string title = info[1].As<Napi::String>().Utf8Value();
367
365
 
368
366
  SDL_Window *window = getWindow(env, window_id);
369
367
 
370
368
  if (!SDL_SetWindowTitle(window, title.c_str())) {
371
- std::ostringstream message;
372
- message << "SDL_SetWindowTitle(" << window_id << ", " << title << ") error: " << SDL_GetError();
373
- SDL_ClearError();
374
- throw Napi::Error::New(env, message.str());
369
+ throw global::sdlError(env, "SDL_SetWindowTitle", window_id, title);
375
370
  }
376
371
 
377
- return env.Undefined();
372
+ return Napi::String::New(env, SDL_GetWindowTitle(window));
378
373
  }
379
374
 
380
375
  Napi::Value
@@ -382,17 +377,14 @@ window::setPosition (const Napi::CallbackInfo &info)
382
377
  {
383
378
  Napi::Env env = info.Env();
384
379
 
385
- int window_id = info[0].As<Napi::Number>().Int32Value();
380
+ SDL_WindowID window_id = info[0].As<Napi::Number>().Uint32Value();
386
381
  int x = info[1].As<Napi::Number>().Int32Value();
387
382
  int y = info[2].As<Napi::Number>().Int32Value();
388
383
 
389
384
  SDL_Window *window = getWindow(env, window_id);
390
385
 
391
386
  if (!SDL_SetWindowPosition(window, x, y)) {
392
- std::ostringstream message;
393
- message << "SDL_SetWindowPosition(" << window_id << ", " << x << ", " << y << ") error: " << SDL_GetError();
394
- SDL_ClearError();
395
- throw Napi::Error::New(env, message.str());
387
+ throw global::sdlError(env, "SDL_SetWindowPosition", window_id, x, y);
396
388
  }
397
389
 
398
390
  return env.Undefined();
@@ -403,7 +395,7 @@ window::setSize (const Napi::CallbackInfo &info)
403
395
  {
404
396
  Napi::Env env = info.Env();
405
397
 
406
- int window_id = info[0].As<Napi::Number>().Int32Value();
398
+ SDL_WindowID window_id = info[0].As<Napi::Number>().Uint32Value();
407
399
  int width = info[1].As<Napi::Number>().Int32Value();
408
400
  int height = info[2].As<Napi::Number>().Int32Value();
409
401
  bool in_pixels = info[3].As<Napi::Boolean>().Value();
@@ -413,21 +405,15 @@ window::setSize (const Napi::CallbackInfo &info)
413
405
  if (in_pixels) {
414
406
  float density = SDL_GetWindowPixelDensity(window);
415
407
  if (density == 0.0f) {
416
- std::ostringstream message;
417
- message << "SDL_GetWindowPixelDensity(" << window_id << ") error: " << SDL_GetError();
418
- SDL_ClearError();
419
- throw Napi::Error::New(env, message.str());
408
+ throw global::sdlError(env, "SDL_GetWindowPixelDensity", window_id);
420
409
  }
421
410
 
422
- width = std::clamp<long>(std::lround(width / (double) density), 1, INT32_MAX);
423
- height = std::clamp<long>(std::lround(height / (double) density), 1, INT32_MAX);
411
+ width = (int) std::clamp<long long>(std::llround(width / (double) density), 1, INT32_MAX);
412
+ height = (int) std::clamp<long long>(std::llround(height / (double) density), 1, INT32_MAX);
424
413
  }
425
414
 
426
415
  if (!SDL_SetWindowSize(window, width, height)) {
427
- std::ostringstream message;
428
- message << "SDL_SetWindowSize(" << window_id << ", " << width << ", " << height << ") error: " << SDL_GetError();
429
- SDL_ClearError();
430
- throw Napi::Error::New(env, message.str());
416
+ throw global::sdlError(env, "SDL_SetWindowSize", window_id, width, height);
431
417
  }
432
418
 
433
419
  return env.Undefined();
@@ -438,16 +424,13 @@ window::setFullscreen (const Napi::CallbackInfo &info)
438
424
  {
439
425
  Napi::Env env = info.Env();
440
426
 
441
- int window_id = info[0].As<Napi::Number>().Int32Value();
427
+ SDL_WindowID window_id = info[0].As<Napi::Number>().Uint32Value();
442
428
  bool is_fullscreen = info[1].As<Napi::Boolean>().Value();
443
429
 
444
430
  SDL_Window *window = getWindow(env, window_id);
445
431
 
446
432
  if (!SDL_SetWindowFullscreen(window, is_fullscreen)) {
447
- std::ostringstream message;
448
- message << "SDL_SetWindowFullscreen(" << window_id << ", " << is_fullscreen << ") error: " << SDL_GetError();
449
- SDL_ClearError();
450
- throw Napi::Error::New(env, message.str());
433
+ throw global::sdlError(env, "SDL_SetWindowFullscreen", window_id, is_fullscreen);
451
434
  }
452
435
 
453
436
  return env.Undefined();
@@ -458,16 +441,13 @@ window::setResizable (const Napi::CallbackInfo &info)
458
441
  {
459
442
  Napi::Env env = info.Env();
460
443
 
461
- int window_id = info[0].As<Napi::Number>().Int32Value();
444
+ SDL_WindowID window_id = info[0].As<Napi::Number>().Uint32Value();
462
445
  bool is_resizable = info[1].As<Napi::Boolean>().Value();
463
446
 
464
447
  SDL_Window *window = getWindow(env, window_id);
465
448
 
466
449
  if (!SDL_SetWindowResizable(window, is_resizable)) {
467
- std::ostringstream message;
468
- message << "SDL_SetWindowResizable(" << window_id << ", " << is_resizable << ") error: " << SDL_GetError();
469
- SDL_ClearError();
470
- throw Napi::Error::New(env, message.str());
450
+ throw global::sdlError(env, "SDL_SetWindowResizable", window_id, is_resizable);
471
451
  }
472
452
 
473
453
  Uint64 actual_flags = SDL_GetWindowFlags(window);
@@ -481,16 +461,13 @@ window::setBorderless (const Napi::CallbackInfo &info)
481
461
  {
482
462
  Napi::Env env = info.Env();
483
463
 
484
- int window_id = info[0].As<Napi::Number>().Int32Value();
464
+ SDL_WindowID window_id = info[0].As<Napi::Number>().Uint32Value();
485
465
  bool is_borderless = info[1].As<Napi::Boolean>().Value();
486
466
 
487
467
  SDL_Window *window = getWindow(env, window_id);
488
468
 
489
469
  if (!SDL_SetWindowBordered(window, !is_borderless)) {
490
- std::ostringstream message;
491
- message << "SDL_SetWindowBordered(" << window_id << ", " << !is_borderless << ") error: " << SDL_GetError();
492
- SDL_ClearError();
493
- throw Napi::Error::New(env, message.str());
470
+ throw global::sdlError(env, "SDL_SetWindowBordered", window_id, !is_borderless);
494
471
  }
495
472
 
496
473
  Uint64 actual_flags = SDL_GetWindowFlags(window);
@@ -504,16 +481,13 @@ window::setRelativeMouseMode (const Napi::CallbackInfo &info)
504
481
  {
505
482
  Napi::Env env = info.Env();
506
483
 
507
- int window_id = info[0].As<Napi::Number>().Int32Value();
484
+ SDL_WindowID window_id = info[0].As<Napi::Number>().Uint32Value();
508
485
  bool relative = info[1].As<Napi::Boolean>().Value();
509
486
 
510
487
  SDL_Window *window = getWindow(env, window_id);
511
488
 
512
489
  if (!SDL_SetWindowRelativeMouseMode(window, relative)) {
513
- std::ostringstream message;
514
- message << "SDL_SetWindowRelativeMouseMode(" << window_id << ", " << relative << ") error: " << SDL_GetError();
515
- SDL_ClearError();
516
- throw Napi::Error::New(env, message.str());
490
+ throw global::sdlError(env, "SDL_SetWindowRelativeMouseMode", window_id, relative);
517
491
  }
518
492
 
519
493
  return Napi::Boolean::New(env, SDL_GetWindowRelativeMouseMode(window));
@@ -524,7 +498,7 @@ window::getMouseCaptured (const Napi::CallbackInfo &info)
524
498
  {
525
499
  Napi::Env env = info.Env();
526
500
 
527
- int window_id = info[0].As<Napi::Number>().Int32Value();
501
+ SDL_WindowID window_id = info[0].As<Napi::Number>().Uint32Value();
528
502
 
529
503
  SDL_Window *window = getWindow(env, window_id);
530
504
 
@@ -536,7 +510,7 @@ window::setAcceleratedAndVsync (const Napi::CallbackInfo &info)
536
510
  {
537
511
  Napi::Env env = info.Env();
538
512
 
539
- int window_id = info[0].As<Napi::Number>().Int32Value();
513
+ SDL_WindowID window_id = info[0].As<Napi::Number>().Uint32Value();
540
514
  bool is_accelerated = info[1].As<Napi::Boolean>().Value();
541
515
  int vsync = info[2].As<Napi::Number>().Int32Value();
542
516
 
@@ -556,15 +530,12 @@ window::focus (const Napi::CallbackInfo &info)
556
530
  {
557
531
  Napi::Env env = info.Env();
558
532
 
559
- int window_id = info[0].As<Napi::Number>().Int32Value();
533
+ SDL_WindowID window_id = info[0].As<Napi::Number>().Uint32Value();
560
534
 
561
535
  SDL_Window *window = getWindow(env, window_id);
562
536
 
563
537
  if (!SDL_RaiseWindow(window)) {
564
- std::ostringstream message;
565
- message << "SDL_RaiseWindow(" << window_id << ") error: " << SDL_GetError();
566
- SDL_ClearError();
567
- throw Napi::Error::New(env, message.str());
538
+ throw global::sdlError(env, "SDL_RaiseWindow", window_id);
568
539
  }
569
540
 
570
541
  return env.Undefined();
@@ -575,15 +546,12 @@ window::show (const Napi::CallbackInfo &info)
575
546
  {
576
547
  Napi::Env env = info.Env();
577
548
 
578
- int window_id = info[0].As<Napi::Number>().Int32Value();
549
+ SDL_WindowID window_id = info[0].As<Napi::Number>().Uint32Value();
579
550
 
580
551
  SDL_Window *window = getWindow(env, window_id);
581
552
 
582
553
  if (!SDL_ShowWindow(window)) {
583
- std::ostringstream message;
584
- message << "SDL_ShowWindow(" << window_id << ") error: " << SDL_GetError();
585
- SDL_ClearError();
586
- throw Napi::Error::New(env, message.str());
554
+ throw global::sdlError(env, "SDL_ShowWindow", window_id);
587
555
  }
588
556
 
589
557
  return env.Undefined();
@@ -594,15 +562,12 @@ window::hide (const Napi::CallbackInfo &info)
594
562
  {
595
563
  Napi::Env env = info.Env();
596
564
 
597
- int window_id = info[0].As<Napi::Number>().Int32Value();
565
+ SDL_WindowID window_id = info[0].As<Napi::Number>().Uint32Value();
598
566
 
599
567
  SDL_Window *window = getWindow(env, window_id);
600
568
 
601
569
  if (!SDL_HideWindow(window)) {
602
- std::ostringstream message;
603
- message << "SDL_HideWindow(" << window_id << ") error: " << SDL_GetError();
604
- SDL_ClearError();
605
- throw Napi::Error::New(env, message.str());
570
+ throw global::sdlError(env, "SDL_HideWindow", window_id);
606
571
  }
607
572
 
608
573
  return env.Undefined();
@@ -613,15 +578,12 @@ window::maximize (const Napi::CallbackInfo &info)
613
578
  {
614
579
  Napi::Env env = info.Env();
615
580
 
616
- int window_id = info[0].As<Napi::Number>().Int32Value();
581
+ SDL_WindowID window_id = info[0].As<Napi::Number>().Uint32Value();
617
582
 
618
583
  SDL_Window *window = getWindow(env, window_id);
619
584
 
620
585
  if (!SDL_MaximizeWindow(window)) {
621
- std::ostringstream message;
622
- message << "SDL_MaximizeWindow(" << window_id << ") error: " << SDL_GetError();
623
- SDL_ClearError();
624
- throw Napi::Error::New(env, message.str());
586
+ throw global::sdlError(env, "SDL_MaximizeWindow", window_id);
625
587
  }
626
588
 
627
589
  return env.Undefined();
@@ -632,15 +594,12 @@ window::minimize (const Napi::CallbackInfo &info)
632
594
  {
633
595
  Napi::Env env = info.Env();
634
596
 
635
- int window_id = info[0].As<Napi::Number>().Int32Value();
597
+ SDL_WindowID window_id = info[0].As<Napi::Number>().Uint32Value();
636
598
 
637
599
  SDL_Window *window = getWindow(env, window_id);
638
600
 
639
601
  if (!SDL_MinimizeWindow(window)) {
640
- std::ostringstream message;
641
- message << "SDL_MinimizeWindow(" << window_id << ") error: " << SDL_GetError();
642
- SDL_ClearError();
643
- throw Napi::Error::New(env, message.str());
602
+ throw global::sdlError(env, "SDL_MinimizeWindow", window_id);
644
603
  }
645
604
 
646
605
  return env.Undefined();
@@ -651,15 +610,12 @@ window::restore (const Napi::CallbackInfo &info)
651
610
  {
652
611
  Napi::Env env = info.Env();
653
612
 
654
- int window_id = info[0].As<Napi::Number>().Int32Value();
613
+ SDL_WindowID window_id = info[0].As<Napi::Number>().Uint32Value();
655
614
 
656
615
  SDL_Window *window = getWindow(env, window_id);
657
616
 
658
617
  if (!SDL_RestoreWindow(window)) {
659
- std::ostringstream message;
660
- message << "SDL_RestoreWindow(" << window_id << ") error: " << SDL_GetError();
661
- SDL_ClearError();
662
- throw Napi::Error::New(env, message.str());
618
+ throw global::sdlError(env, "SDL_RestoreWindow", window_id);
663
619
  }
664
620
 
665
621
  return env.Undefined();
@@ -670,7 +626,7 @@ window::render (const Napi::CallbackInfo &info)
670
626
  {
671
627
  Napi::Env env = info.Env();
672
628
 
673
- int window_id = info[0].As<Napi::Number>().Int32Value();
629
+ SDL_WindowID window_id = info[0].As<Napi::Number>().Uint32Value();
674
630
  int width = info[1].As<Napi::Number>().Int32Value();
675
631
  int height = info[2].As<Napi::Number>().Int32Value();
676
632
  int stride = info[3].As<Napi::Number>().Int32Value();
@@ -679,7 +635,7 @@ window::render (const Napi::CallbackInfo &info)
679
635
  SDL_ScaleMode scaling = static_cast<SDL_ScaleMode>(info[6].As<Napi::Number>().Int32Value());
680
636
  Napi::Value dstRectVal = info[7];
681
637
  bool hasDstRect = !dstRectVal.IsNull();
682
- SDL_FRect rect;
638
+ SDL_FRect rect = {};
683
639
  if (hasDstRect) {
684
640
  Napi::Object dstRect = dstRectVal.As<Napi::Object>();
685
641
  rect.x = dstRect.Get("x").As<Napi::Number>().FloatValue();
@@ -697,6 +653,18 @@ window::render (const Napi::CallbackInfo &info)
697
653
  throw Napi::Error::New(env, message.str());
698
654
  }
699
655
 
656
+ if (hasDstRect) {
657
+ float x2, y2;
658
+ if (false
659
+ || !SDL_RenderCoordinatesFromWindow(renderer, rect.x + rect.w, rect.y + rect.h, &x2, &y2)
660
+ || !SDL_RenderCoordinatesFromWindow(renderer, rect.x, rect.y, &rect.x, &rect.y)
661
+ ) {
662
+ throw global::sdlError(env, "SDL_RenderCoordinatesFromWindow", window_id);
663
+ }
664
+ rect.w = x2 - rect.x;
665
+ rect.h = y2 - rect.y;
666
+ }
667
+
700
668
  SDL_PropertiesID props = SDL_GetWindowProperties(window);
701
669
  SDL_Texture *texture = (SDL_Texture *) SDL_GetPointerProperty(props, TEXTURE_PROPERTY, nullptr);
702
670
 
@@ -712,62 +680,40 @@ window::render (const Napi::CallbackInfo &info)
712
680
 
713
681
  texture = SDL_CreateTexture(renderer, (SDL_PixelFormat) format, SDL_TEXTUREACCESS_STREAMING, width, height);
714
682
  if (texture == nullptr) {
715
- std::ostringstream message;
716
- message << "SDL_CreateTexture(" << width << ", " << height << ", " << format << ") error: " << SDL_GetError();
717
- SDL_ClearError();
718
- throw Napi::Error::New(env, message.str());
683
+ throw global::sdlError(env, "SDL_CreateTexture", width, height, format);
719
684
  }
720
685
 
721
686
  if (!SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_NONE)) {
722
- std::ostringstream message;
723
- message << "SDL_SetTextureBlendMode(" << window_id << ") error: " << SDL_GetError();
724
- SDL_ClearError();
687
+ Napi::Error error = global::sdlError(env, "SDL_SetTextureBlendMode", window_id);
725
688
  SDL_DestroyTexture(texture);
726
- throw Napi::Error::New(env, message.str());
689
+ throw error;
727
690
  }
728
691
 
729
692
  if (!SDL_SetPointerProperty(props, TEXTURE_PROPERTY, texture)) {
730
- std::ostringstream message;
731
- message << "SDL_SetPointerProperty(" << window_id << ") error: " << SDL_GetError();
732
- SDL_ClearError();
693
+ Napi::Error error = global::sdlError(env, "SDL_SetPointerProperty", window_id);
733
694
  SDL_DestroyTexture(texture);
734
- throw Napi::Error::New(env, message.str());
695
+ throw error;
735
696
  }
736
697
  }
737
698
 
738
699
  if(!SDL_SetTextureScaleMode(texture, scaling)) {
739
- std::ostringstream message;
740
- message << "SDL_SetTextureScaleMode(" << window_id << ", " << scaling << ") error: " << SDL_GetError();
741
- SDL_ClearError();
742
- throw Napi::Error::New(env, message.str());
700
+ throw global::sdlError(env, "SDL_SetTextureScaleMode", window_id, scaling);
743
701
  }
744
702
 
745
703
  if (!SDL_UpdateTexture(texture, nullptr, pixels, stride)) {
746
- std::ostringstream message;
747
- message << "SDL_UpdateTexture(" << window_id << ") error: " << SDL_GetError();
748
- SDL_ClearError();
749
- throw Napi::Error::New(env, message.str());
704
+ throw global::sdlError(env, "SDL_UpdateTexture", window_id);
750
705
  }
751
706
 
752
707
  if (!SDL_RenderClear(renderer)) {
753
- std::ostringstream message;
754
- message << "SDL_RenderClear(" << window_id << ") error: " << SDL_GetError();
755
- SDL_ClearError();
756
- throw Napi::Error::New(env, message.str());
708
+ throw global::sdlError(env, "SDL_RenderClear", window_id);
757
709
  }
758
710
 
759
711
  if (!SDL_RenderTexture(renderer, texture, nullptr, hasDstRect ? &rect : nullptr)) {
760
- std::ostringstream message;
761
- message << "SDL_RenderTexture(" << window_id << ") error: " << SDL_GetError();
762
- SDL_ClearError();
763
- throw Napi::Error::New(env, message.str());
712
+ throw global::sdlError(env, "SDL_RenderTexture", window_id);
764
713
  }
765
714
 
766
715
  if (!SDL_RenderPresent(renderer)) {
767
- std::ostringstream message;
768
- message << "SDL_RenderPresent(" << window_id << ") error: " << SDL_GetError();
769
- SDL_ClearError();
770
- throw Napi::Error::New(env, message.str());
716
+ throw global::sdlError(env, "SDL_RenderPresent", window_id);
771
717
  }
772
718
 
773
719
  return env.Undefined();
@@ -778,7 +724,7 @@ window::setIcon (const Napi::CallbackInfo &info)
778
724
  {
779
725
  Napi::Env env = info.Env();
780
726
 
781
- int window_id = info[0].As<Napi::Number>().Int32Value();
727
+ SDL_WindowID window_id = info[0].As<Napi::Number>().Uint32Value();
782
728
  int w = info[1].As<Napi::Number>().Int32Value();
783
729
  int h = info[2].As<Napi::Number>().Int32Value();
784
730
  int stride = info[3].As<Napi::Number>().Int32Value();
@@ -789,20 +735,15 @@ window::setIcon (const Napi::CallbackInfo &info)
789
735
 
790
736
  SDL_Surface* surface = SDL_CreateSurfaceFrom(w, h, (SDL_PixelFormat) format, pixels, stride);
791
737
  if (surface == nullptr) {
792
- std::ostringstream message;
793
- message << "SDL_CreateSurfaceFrom(" << window_id << ", " << w << ", " << h << ", " << format << ") error: " << SDL_GetError();
794
- SDL_ClearError();
795
- throw Napi::Error::New(env, message.str());
738
+ throw global::sdlError(env, "SDL_CreateSurfaceFrom", window_id, w, h, format);
796
739
  }
797
740
 
798
- bool success = SDL_SetWindowIcon(window, surface);
799
- SDL_DestroySurface(surface);
800
- if (!success) {
801
- std::ostringstream message;
802
- message << "SDL_SetWindowIcon(" << window_id << ") error: " << SDL_GetError();
803
- SDL_ClearError();
804
- throw Napi::Error::New(env, message.str());
741
+ if (!SDL_SetWindowIcon(window, surface)) {
742
+ Napi::Error error = global::sdlError(env, "SDL_SetWindowIcon", window_id);
743
+ SDL_DestroySurface(surface);
744
+ throw error;
805
745
  }
746
+ SDL_DestroySurface(surface);
806
747
 
807
748
  return env.Undefined();
808
749
  }
@@ -812,7 +753,7 @@ window::flash (const Napi::CallbackInfo &info)
812
753
  {
813
754
  Napi::Env env = info.Env();
814
755
 
815
- int window_id = info[0].As<Napi::Number>().Int32Value();
756
+ SDL_WindowID window_id = info[0].As<Napi::Number>().Uint32Value();
816
757
  int type = info[1].As<Napi::Number>().Int32Value();
817
758
 
818
759
  SDL_Window *window = getWindow(env, window_id);
@@ -825,10 +766,50 @@ window::flash (const Napi::CallbackInfo &info)
825
766
  }
826
767
 
827
768
  if (!SDL_FlashWindow(window, op)) {
828
- std::ostringstream message;
829
- message << "SDL_FlashWindow(" << window_id << ", " << type << ") error: " << SDL_GetError();
830
- SDL_ClearError();
831
- throw Napi::Error::New(env, message.str());
769
+ throw global::sdlError(env, "SDL_FlashWindow", window_id, type);
770
+ }
771
+
772
+ return env.Undefined();
773
+ }
774
+
775
+ SDL_HitTestResult SDLCALL
776
+ window::hitTest (SDL_Window *window, const SDL_Point *area, void *)
777
+ {
778
+ Napi::Env env = hit_test_handler.Env();
779
+ Napi::HandleScope scope(env);
780
+
781
+ Napi::Value result = hit_test_handler.Call({
782
+ Napi::Number::New(env, SDL_GetWindowID(window)),
783
+ Napi::Number::New(env, area->x),
784
+ Napi::Number::New(env, area->y),
785
+ });
786
+
787
+ return (SDL_HitTestResult) result.As<Napi::Number>().Int32Value();
788
+ }
789
+
790
+ Napi::Value
791
+ window::registerHitTestHandler (const Napi::CallbackInfo &info)
792
+ {
793
+ Napi::Env env = info.Env();
794
+
795
+ hit_test_handler = Napi::Persistent(info[0].As<Napi::Function>());
796
+ hit_test_handler.SuppressDestruct();
797
+
798
+ return env.Undefined();
799
+ }
800
+
801
+ Napi::Value
802
+ window::setHitTest (const Napi::CallbackInfo &info)
803
+ {
804
+ Napi::Env env = info.Env();
805
+
806
+ SDL_WindowID window_id = info[0].As<Napi::Number>().Uint32Value();
807
+ bool enabled = info[1].As<Napi::Boolean>().Value();
808
+
809
+ SDL_Window *window = getWindow(env, window_id);
810
+
811
+ if (!SDL_SetWindowHitTest(window, enabled ? window::hitTest : nullptr, nullptr)) {
812
+ throw global::sdlError(env, "SDL_SetWindowHitTest", window_id, enabled);
832
813
  }
833
814
 
834
815
  return env.Undefined();
@@ -839,10 +820,11 @@ window::destroy (const Napi::CallbackInfo &info)
839
820
  {
840
821
  Napi::Env env = info.Env();
841
822
 
842
- int window_id = info[0].As<Napi::Number>().Int32Value();
823
+ SDL_WindowID window_id = info[0].As<Napi::Number>().Uint32Value();
843
824
 
844
825
  SDL_Window *window = getWindow(env, window_id);
845
826
 
827
+ // SDL_DestroyWindow() tears the renderer down but doesn't free it, so without this it would leak until SDL_Quit().
846
828
  SDL_Renderer *renderer = SDL_GetRenderer(window);
847
829
  if (renderer != nullptr) { SDL_DestroyRenderer(renderer); }
848
830