@kmamal/sdl 1.0.0-5 → 1.0.0-8

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