@kmamal/sdl 0.11.12 → 1.0.0-2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (86) hide show
  1. package/CHANGELOG.md +257 -1
  2. package/README.md +26 -2685
  3. package/binding.gyp +82 -65
  4. package/package.json +17 -11
  5. package/scripts/build-sdl.mjs +15 -7
  6. package/scripts/build.mjs +30 -15
  7. package/scripts/download-release.mjs +9 -6
  8. package/scripts/download-sdl.mjs +9 -6
  9. package/scripts/install.mjs +13 -5
  10. package/scripts/util/common.js +9 -1
  11. package/src/javascript/audio/audio-playback-stream.js +50 -0
  12. package/src/javascript/audio/audio-recording-stream.js +32 -0
  13. package/src/javascript/audio/{audio-instance.js → audio-stream.js} +35 -32
  14. package/src/javascript/audio/device.js +34 -6
  15. package/src/javascript/audio/format-helpers.js +26 -35
  16. package/src/javascript/audio/index.js +37 -26
  17. package/src/javascript/audio/prevent-exit.js +19 -9
  18. package/src/javascript/cleanup.js +19 -10
  19. package/src/javascript/enums.js +5 -0
  20. package/src/javascript/events/events-via-poll.js +25 -7
  21. package/src/javascript/events/index.js +274 -175
  22. package/src/javascript/gamepad/device.js +23 -0
  23. package/src/javascript/gamepad/gamepad-instance.js +209 -0
  24. package/src/javascript/gamepad/index.js +51 -0
  25. package/src/javascript/globals.js +8 -5
  26. package/src/javascript/helpers.js +34 -7
  27. package/src/javascript/index.js +5 -3
  28. package/src/javascript/joystick/device.js +5 -10
  29. package/src/javascript/joystick/index.js +7 -7
  30. package/src/javascript/joystick/joystick-instance.js +79 -42
  31. package/src/javascript/joystick/shared.js +88 -0
  32. package/src/javascript/keyboard/index.js +6 -4
  33. package/src/javascript/keyboard/key-mapping.js +70 -28
  34. package/src/javascript/mouse/index.js +21 -10
  35. package/src/javascript/sensor/devices.js +17 -0
  36. package/src/javascript/sensor/index.js +2 -16
  37. package/src/javascript/sensor/sensor-instance.js +20 -6
  38. package/src/javascript/touch/devices.js +17 -0
  39. package/src/javascript/touch/index.js +3 -7
  40. package/src/javascript/video/format-helpers.js +99 -0
  41. package/src/javascript/video/index.js +37 -5
  42. package/src/javascript/video/window.js +280 -134
  43. package/src/native/audio.cpp +182 -58
  44. package/src/native/audio.h +10 -5
  45. package/src/native/clipboard.cpp +22 -5
  46. package/src/native/cocoa-window.h +1 -0
  47. package/src/native/cocoa-window.mm +1 -1
  48. package/src/native/enums.cpp +91 -58
  49. package/src/native/events.cpp +437 -284
  50. package/src/native/events.h +18 -12
  51. package/src/native/gamepad.cpp +185 -0
  52. package/src/native/gamepad.h +27 -0
  53. package/src/native/global.cpp +173 -167
  54. package/src/native/global.h +0 -2
  55. package/src/native/joystick.cpp +287 -225
  56. package/src/native/joystick.h +7 -6
  57. package/src/native/keyboard.cpp +28 -8
  58. package/src/native/keyboard.h +4 -2
  59. package/src/native/module.cpp +14 -8
  60. package/src/native/mouse.cpp +54 -34
  61. package/src/native/mouse.h +0 -1
  62. package/src/native/power.cpp +10 -4
  63. package/src/native/power.h +1 -1
  64. package/src/native/sensor.cpp +52 -52
  65. package/src/native/sensor.h +1 -1
  66. package/src/native/touch.cpp +45 -24
  67. package/src/native/touch.h +1 -1
  68. package/src/native/video.cpp +115 -75
  69. package/src/native/video.h +5 -2
  70. package/src/native/window.cpp +415 -305
  71. package/src/native/window.h +3 -1
  72. package/src/types/helpers.d.ts +44 -19
  73. package/src/types/index.d.ts +343 -255
  74. package/scripts/util/fetch.js +0 -85
  75. package/src/javascript/audio/audio-playback-instance.js +0 -23
  76. package/src/javascript/audio/audio-recording-instance.js +0 -17
  77. package/src/javascript/controller/controller-instance.js +0 -160
  78. package/src/javascript/controller/device.js +0 -26
  79. package/src/javascript/controller/index.js +0 -33
  80. package/src/javascript/events/reconcile-audio-devices.js +0 -16
  81. package/src/javascript/events/reconcile-displays.js +0 -17
  82. package/src/javascript/events/reconcile-joystick-and-controller-devices.js +0 -40
  83. package/src/javascript/events/reconcile.js +0 -61
  84. package/src/javascript/video/display.js +0 -4
  85. package/src/native/controller.cpp +0 -192
  86. package/src/native/controller.h +0 -26
@@ -1,36 +1,49 @@
1
1
  #include "window.h"
2
- #include <SDL.h>
3
- #include <SDL_syswm.h>
2
+ #include <SDL3/SDL.h>
4
3
  #include <string>
5
4
  #include <sstream>
6
- #include <map>
5
+ #include <cstdint>
6
+ #include <cmath>
7
+ #include <algorithm>
7
8
 
8
- struct CachedTexture {
9
- SDL_Texture *texture;
10
- int width;
11
- int height;
12
- unsigned int format;
13
- };
9
+ static const char *TEXTURE_PROPERTY = "kmamal.sdl.texture";
14
10
 
15
- std::map<SDL_Window*, CachedTexture> cachedTextures;
11
+ static SDL_Window *
12
+ getWindow (Napi::Env &env, int window_id)
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());
20
+ }
21
+ return window;
22
+ }
16
23
 
17
- #if defined(__LINUX__)
18
- #define NativeWindowHandle Window
19
- #define GL_NativeWindow Window
20
- struct GPU_NativeData {
21
- Display *display;
22
- Window window;
24
+ #if defined(SDL_PLATFORM_LINUX) || defined(SDL_PLATFORM_FREEBSD) || defined(SDL_PLATFORM_OPENBSD) || defined(SDL_PLATFORM_NETBSD)
25
+ #define PLATFORM_LINUX_OR_BSD
26
+ #endif
27
+
28
+ #if defined(PLATFORM_LINUX_OR_BSD)
29
+ struct LinuxNativeData {
30
+ uint64_t subsystem; // 1 = x11, 2 = wayland
31
+ void *display; // Display* | wl_display*
32
+ uintptr_t window; // Window XID | wl_surface* / wl_egl_window*
23
33
  };
34
+ #define NativeWindowHandle LinuxNativeData
35
+ #define GL_NativeWindow LinuxNativeData
36
+ #define GPU_NativeData LinuxNativeData
24
37
  #define GPU_WINDOW_FLAG SDL_WINDOW_VULKAN
25
- #elif defined(__WIN32__)
26
- #define NativeWindowHandle HWND
27
- #define GL_NativeWindow HWND
38
+ #elif defined(SDL_PLATFORM_WIN32)
39
+ #define NativeWindowHandle void *
40
+ #define GL_NativeWindow void *
28
41
  struct GPU_NativeData {
29
- HWND hwnd;
30
- HINSTANCE hinstance;
42
+ void *hwnd;
43
+ void *hinstance;
31
44
  };
32
45
  #define GPU_WINDOW_FLAG 0
33
- #elif defined(__MACOSX__)
46
+ #elif defined(SDL_PLATFORM_MACOS)
34
47
  #include "cocoa-window.h"
35
48
  #define NativeWindowHandle NSView *
36
49
  #define GL_NativeWindow CALayer *
@@ -50,50 +63,39 @@ updateRenderer(
50
63
  int window_id = SDL_GetWindowID(window);
51
64
  // Not likely to fail.
52
65
 
53
- CachedTexture &cached = cachedTextures[window];
54
- if (cached.texture != nullptr) {
55
- SDL_DestroyTexture(cached.texture);
56
- cached.texture = nullptr;
57
- }
58
-
59
66
  SDL_Renderer *old_renderer = SDL_GetRenderer(window);
60
67
  if (old_renderer != nullptr) {
61
68
  SDL_DestroyRenderer(old_renderer);
69
+ SDL_SetPointerProperty(SDL_GetWindowProperties(window), TEXTURE_PROPERTY, nullptr);
62
70
  }
63
71
 
64
- SDL_Renderer *renderer;
65
- for (int i = 0; i < 4; i++) {
66
- int renderer_flags = 0
67
- | (*is_accelerated ? SDL_RENDERER_ACCELERATED : SDL_RENDERER_SOFTWARE)
68
- | (*is_vsync ? SDL_RENDERER_PRESENTVSYNC : 0);
69
-
70
- renderer = SDL_CreateRenderer(window, -1, renderer_flags);
72
+ SDL_Renderer *renderer = nullptr;
73
+ for (int i = 0; i < 2; i++) {
74
+ renderer = SDL_CreateRenderer(window, *is_accelerated ? nullptr : SDL_SOFTWARE_RENDERER);
71
75
  if (renderer != nullptr) {
72
76
  SDL_ClearError();
73
77
  break;
74
78
  }
75
79
 
76
- *is_vsync = !*is_vsync;
77
- if (i % 2) { *is_accelerated = !*is_accelerated; }
80
+ *is_accelerated = !*is_accelerated;
78
81
  }
79
82
 
80
83
  if (renderer == nullptr) {
81
84
  std::ostringstream message;
82
- message << "SDL_CreateRenderer(" << window_id << ", " << is_accelerated << ", " << is_vsync << ") error: " << SDL_GetError();
85
+ message << "SDL_CreateRenderer(" << window_id << ", " << *is_accelerated << ", " << *is_vsync << ") error: " << SDL_GetError();
83
86
  SDL_ClearError();
84
87
  throw Napi::Error::New(env, message.str());
85
88
  }
86
89
 
87
- SDL_RendererInfo info;
88
- if (SDL_GetRendererInfo(renderer, &info) < 0) {
89
- std::ostringstream message;
90
- message << "SDL_GetRendererInfo(" << window_id << ", " << is_accelerated << ", " << is_vsync << ") error: " << SDL_GetError();
91
- SDL_ClearError();
92
- throw Napi::Error::New(env, message.str());
93
- }
90
+ if (!SDL_SetRenderVSync(renderer, *is_vsync ? 1 : 0)) { SDL_ClearError(); }
91
+
92
+ const char *renderer_name = SDL_GetRendererName(renderer);
93
+ if (renderer_name == nullptr) { SDL_ClearError(); }
94
+ else { *is_accelerated = SDL_strcmp(renderer_name, SDL_SOFTWARE_RENDERER) != 0; }
94
95
 
95
- *is_accelerated = info.flags & SDL_RENDERER_ACCELERATED;
96
- *is_vsync = info.flags & SDL_RENDERER_PRESENTVSYNC;
96
+ int vsync;
97
+ if (!SDL_GetRenderVSync(renderer, &vsync)) { SDL_ClearError(); }
98
+ else { *is_vsync = vsync != 0; }
97
99
  }
98
100
 
99
101
 
@@ -104,8 +106,8 @@ window::create (const Napi::CallbackInfo &info)
104
106
 
105
107
  std:: string title = info[0].As<Napi::String>().Utf8Value();
106
108
  int display = info[1].As<Napi::Number>().Int32Value();
107
- int x = info[2].IsNull() ? SDL_WINDOWPOS_CENTERED_DISPLAY(display) : info[2].As<Napi::Number>().Int32Value();
108
- int y = info[3].IsNull() ? SDL_WINDOWPOS_CENTERED_DISPLAY(display) : info[3].As<Napi::Number>().Int32Value();
109
+ int x = info[2].IsNull() ? (int) SDL_WINDOWPOS_CENTERED_DISPLAY(display) : info[2].As<Napi::Number>().Int32Value();
110
+ int y = info[3].IsNull() ? (int) SDL_WINDOWPOS_CENTERED_DISPLAY(display) : info[3].As<Napi::Number>().Int32Value();
109
111
  int width = info[4].IsNull() ? 640 : info[4].As<Napi::Number>().Int32Value();
110
112
  int height = info[5].IsNull() ? 480 : info[5].As<Napi::Number>().Int32Value();
111
113
  bool is_visible = info[6].As<Napi::Boolean>().Value();
@@ -113,142 +115,246 @@ window::create (const Napi::CallbackInfo &info)
113
115
  bool is_resizable = info[8].As<Napi::Boolean>().Value();
114
116
  bool is_borderless = info[9].As<Napi::Boolean>().Value();
115
117
  bool is_always_on_top = info[10].As<Napi::Boolean>().Value();
116
- bool is_accelerated = info[11].As<Napi::Boolean>().Value();
117
- bool is_vsync = info[12].As<Napi::Boolean>().Value();
118
118
  bool is_opengl = info[13].As<Napi::Boolean>().Value();
119
119
  bool is_webgpu = info[14].As<Napi::Boolean>().Value();
120
- bool should_skip_taskbar = info[15].As<Napi::Boolean>().Value();
121
- bool is_popup_menu = info[16].As<Napi::Boolean>().Value();
122
- bool is_tooltip = info[17].As<Napi::Boolean>().Value();
123
- bool is_utility = info[18].As<Napi::Boolean>().Value();
124
-
125
- int desired_flags = 0
126
- | SDL_WINDOW_HIDDEN | SDL_WINDOW_ALLOW_HIGHDPI
127
- | (is_fullscreen ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0)
120
+ bool has_renderer = !is_opengl && !is_webgpu;
121
+ bool is_accelerated = has_renderer && info[11].As<Napi::Boolean>().Value();
122
+ bool is_vsync = has_renderer && info[12].As<Napi::Boolean>().Value();
123
+
124
+ Uint64 desired_flags = 0
125
+ | SDL_WINDOW_HIDDEN | SDL_WINDOW_HIGH_PIXEL_DENSITY
126
+ | (is_fullscreen ? SDL_WINDOW_FULLSCREEN : 0)
128
127
  | (is_resizable ? SDL_WINDOW_RESIZABLE : 0)
129
128
  | (is_borderless ? SDL_WINDOW_BORDERLESS : 0)
130
129
  | (is_always_on_top ? SDL_WINDOW_ALWAYS_ON_TOP : 0)
131
130
  | (is_opengl ? SDL_WINDOW_OPENGL : 0)
132
131
  | (is_webgpu ? GPU_WINDOW_FLAG : 0)
133
- | (should_skip_taskbar ? SDL_WINDOW_SKIP_TASKBAR : 0)
134
- | (is_popup_menu ? SDL_WINDOW_POPUP_MENU : 0)
135
- | (is_tooltip ? SDL_WINDOW_TOOLTIP : 0)
136
- | (is_utility ? SDL_WINDOW_UTILITY : 0)
137
132
  ;
138
133
 
139
- SDL_Window *window = SDL_CreateWindow(title.c_str(), x, y, width, height, desired_flags);
140
- if (window == nullptr) {
134
+ SDL_PropertiesID create_props = SDL_CreateProperties();
135
+ if (create_props == 0) {
141
136
  std::ostringstream message;
142
- message << "SDL_CreateWindow() error: " << SDL_GetError();
137
+ message << "SDL_CreateProperties() error: " << SDL_GetError();
143
138
  SDL_ClearError();
144
139
  throw Napi::Error::New(env, message.str());
145
140
  }
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);
146
147
 
147
- int window_id = SDL_GetWindowID(window);
148
- if (window_id == 0) {
148
+ SDL_Window *window = SDL_CreateWindowWithProperties(create_props);
149
+ SDL_DestroyProperties(create_props);
150
+ if (window == nullptr) {
149
151
  std::ostringstream message;
150
- message << "SDL_GetWindowID() error: " << SDL_GetError();
152
+ message << "SDL_CreateWindow() error: " << SDL_GetError();
151
153
  SDL_ClearError();
152
154
  throw Napi::Error::New(env, message.str());
153
155
  }
154
156
 
155
- int actual_flags = SDL_GetWindowFlags(window);
156
- is_fullscreen = actual_flags & (SDL_WINDOW_FULLSCREEN | SDL_WINDOW_FULLSCREEN_DESKTOP);
157
- is_resizable = actual_flags & SDL_WINDOW_RESIZABLE;
158
- is_borderless = actual_flags & SDL_WINDOW_BORDERLESS;
159
- is_always_on_top = actual_flags & SDL_WINDOW_ALWAYS_ON_TOP;
160
- should_skip_taskbar = actual_flags & SDL_WINDOW_SKIP_TASKBAR;
161
- is_popup_menu = actual_flags & SDL_WINDOW_POPUP_MENU;
162
- is_tooltip = actual_flags & SDL_WINDOW_TOOLTIP;
163
- is_utility = actual_flags & SDL_WINDOW_UTILITY;
157
+ try {
158
+ int window_id = SDL_GetWindowID(window);
159
+ 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());
164
+ }
164
165
 
165
- SDL_GetWindowPosition(window, &x, &y);
166
+ if (!SDL_StartTextInput(window)) { SDL_ClearError(); }
166
167
 
167
- int pixel_width, pixel_height;
168
- SDL_GetWindowSizeInPixels(window, &pixel_width, &pixel_height);
168
+ Napi::Object native = Napi::Object::New(env);
169
169
 
170
- display = SDL_GetWindowDisplayIndex(window);
171
- if (display < 0) {
172
- std::ostringstream message;
173
- message << "SDL_GetWindowDisplayIndex(" << window_id << ") error: " << SDL_GetError();
174
- SDL_ClearError();
175
- throw Napi::Error::New(env, message.str());
176
- }
170
+ SDL_PropertiesID window_props = SDL_GetWindowProperties(window);
171
+ bool has_wm_info = window_props != 0;
172
+ std::string message_of_failed_wm_info;
173
+ if (!has_wm_info) {
174
+ message_of_failed_wm_info = SDL_GetError();
175
+ SDL_ClearError();
176
+ }
177
177
 
178
- Napi::Object native = Napi::Object::New(env);
178
+ #if defined(PLATFORM_LINUX_OR_BSD)
179
+ const char *video_driver = SDL_GetCurrentVideoDriver();
180
+ bool is_x11 = false;
181
+ if (has_wm_info) {
182
+ bool is_wayland = SDL_strcmp(video_driver, "wayland") == 0;
183
+ is_x11 = SDL_strcmp(video_driver, "x11") == 0;
184
+ if (!is_x11 && !is_wayland) {
185
+ has_wm_info = false;
186
+ message_of_failed_wm_info = "native handles are only supported under the x11 and wayland video drivers";
187
+ }
188
+ }
189
+
190
+ if (has_wm_info) {
191
+ native.Set("subsystem", Napi::String::New(env, is_x11 ? "x11" : "wayland"));
192
+ }
193
+ else {
194
+ native.Set("subsystem", env.Null());
195
+ }
196
+ #else
197
+ native.Set("subsystem", env.Null());
198
+ #endif
179
199
 
180
- SDL_SysWMinfo sys_wm_info;
181
- SDL_VERSION(&(sys_wm_info.version));
182
- if (SDL_GetWindowWMInfo(window, &sys_wm_info) == SDL_FALSE) {
183
- std::ostringstream message;
184
- message << "SDL_GetWindowWMInfo(" << window_id << ") error: " << SDL_GetError();
185
- SDL_ClearError();
186
- throw Napi::Error::New(env, message.str());
187
- }
200
+ Napi::Value native_handle;
201
+ if (has_wm_info) {
202
+ NativeWindowHandle _native_handle;
203
+ #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
+ }
218
+ #elif defined(SDL_PLATFORM_WIN32)
219
+ _native_handle = SDL_GetPointerProperty(window_props, SDL_PROP_WINDOW_WIN32_HWND_POINTER, nullptr);
220
+ #elif defined(SDL_PLATFORM_MACOS)
221
+ NSWindow *cocoa_window = (NSWindow *) SDL_GetPointerProperty(window_props, SDL_PROP_WINDOW_COCOA_WINDOW_POINTER, nullptr);
222
+ _native_handle = getCocoaWindowHandle(cocoa_window);
223
+ #endif
224
+ native_handle = Napi::Buffer<NativeWindowHandle>::Copy(env, &_native_handle, 1);
225
+ }
226
+ else {
227
+ native_handle = env.Null();
228
+ }
229
+ native.Set("handle", native_handle);
230
+
231
+ if (is_opengl) {
232
+ if (!has_wm_info) {
233
+ std::ostringstream message;
234
+ message << "Window has set { opengl: true } but querying the native handles failed with: " << message_of_failed_wm_info;
235
+ SDL_ClearError();
236
+ throw Napi::Error::New(env, message.str());
237
+ }
238
+
239
+ GL_NativeWindow native_gl;
240
+ #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
+ #elif defined(SDL_PLATFORM_WIN32)
256
+ native_gl = SDL_GetPointerProperty(window_props, SDL_PROP_WINDOW_WIN32_HWND_POINTER, nullptr);
257
+ #elif defined(SDL_PLATFORM_MACOS)
258
+ NSWindow *cocoa_gl_window = (NSWindow *) SDL_GetPointerProperty(window_props, SDL_PROP_WINDOW_COCOA_WINDOW_POINTER, nullptr);
259
+ native_gl = getCocoaGlView(cocoa_gl_window);
260
+ #endif
261
+ native.Set("gl", Napi::Buffer<GL_NativeWindow>::Copy(env, &native_gl, 1));
262
+ }
263
+ else if (is_webgpu) {
264
+ if (!has_wm_info) {
265
+ std::ostringstream message;
266
+ message << "Window has set { webgpu: true } but querying the native handles failed with: " << message_of_failed_wm_info;
267
+ SDL_ClearError();
268
+ throw Napi::Error::New(env, message.str());
269
+ }
270
+
271
+ GPU_NativeData native_gpu;
272
+ #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
+ }
287
+ #elif defined(SDL_PLATFORM_WIN32)
288
+ native_gpu.hwnd = SDL_GetPointerProperty(window_props, SDL_PROP_WINDOW_WIN32_HWND_POINTER, nullptr);
289
+ native_gpu.hinstance = SDL_GetPointerProperty(window_props, SDL_PROP_WINDOW_WIN32_INSTANCE_POINTER, nullptr);
290
+ #elif defined(SDL_PLATFORM_MACOS)
291
+ NSWindow *cocoa_gpu_window = (NSWindow *) SDL_GetPointerProperty(window_props, SDL_PROP_WINDOW_COCOA_WINDOW_POINTER, nullptr);
292
+ native_gpu.layer = getCocoaGpuView(cocoa_gpu_window);
293
+ #endif
294
+ native.Set("gpu", Napi::Buffer<GPU_NativeData>::Copy(env, &native_gpu, 1));
295
+ }
296
+ else {
297
+ updateRenderer(env, window, &is_accelerated, &is_vsync);
298
+ }
188
299
 
189
- NativeWindowHandle native_handle;
190
- #if defined(__LINUX__)
191
- native_handle = sys_wm_info.info.x11.window;
192
- #elif defined(__WIN32__)
193
- native_handle = sys_wm_info.info.win.window;
194
- #elif defined(__MACOSX__)
195
- native_handle = getCocoaWindowHandle(sys_wm_info.info.cocoa.window);
196
- #endif
197
- native.Set("handle", Napi::Buffer<NativeWindowHandle>::Copy(env, &native_handle, 1));
198
-
199
- if (is_opengl) {
200
- GL_NativeWindow native_gl;
201
- #if defined(__LINUX__)
202
- native_gl = sys_wm_info.info.x11.window;
203
- #elif defined(__WIN32__)
204
- native_gl = sys_wm_info.info.win.window;
205
- #elif defined(__MACOSX__)
206
- native_gl = getCocoaGlView(sys_wm_info.info.cocoa.window);
207
- #endif
208
- native.Set("gl", Napi::Buffer<GL_NativeWindow>::Copy(env, &native_gl, 1));
209
- }
210
- else if (is_webgpu) {
211
- GPU_NativeData native_gpu;
212
- #if defined(__LINUX__)
213
- native_gpu.display = sys_wm_info.info.x11.display;
214
- native_gpu.window = sys_wm_info.info.x11.window;
215
- #elif defined(__WIN32__)
216
- native_gpu.hwnd = sys_wm_info.info.win.window;
217
- native_gpu.hinstance = sys_wm_info.info.win.hinstance;
218
- #elif defined(__MACOSX__)
219
- native_gpu.layer = getCocoaGpuView(sys_wm_info.info.cocoa.window);
220
- #endif
221
- native.Set("gpu", Napi::Buffer<GPU_NativeData>::Copy(env, &native_gpu, 1));
222
- }
223
- else {
224
- cachedTextures[window] = {};
225
- updateRenderer(env, window, &is_accelerated, &is_vsync);
226
- }
300
+ 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());
305
+ }
227
306
 
228
- if (is_visible) { SDL_ShowWindow(window); }
307
+ Uint64 actual_flags = SDL_GetWindowFlags(window);
308
+ is_fullscreen = actual_flags & SDL_WINDOW_FULLSCREEN;
309
+ is_resizable = actual_flags & SDL_WINDOW_RESIZABLE;
310
+ is_borderless = actual_flags & SDL_WINDOW_BORDERLESS;
311
+ is_always_on_top = actual_flags & SDL_WINDOW_ALWAYS_ON_TOP;
229
312
 
230
- Napi::Object result = Napi::Object::New(env);
231
- result.Set("id", window_id);
232
- result.Set("x", x);
233
- result.Set("y", y);
234
- result.Set("width", width);
235
- result.Set("height", height);
236
- result.Set("pixelWidth", pixel_width);
237
- result.Set("pixelHeight", pixel_height);
238
- result.Set("displayIndex", display);
239
- result.Set("fullscreen", is_fullscreen);
240
- result.Set("resizable", is_resizable);
241
- result.Set("borderless", is_borderless);
242
- result.Set("alwaysOnTop", is_always_on_top);
243
- result.Set("accelerated", is_accelerated);
244
- result.Set("vsync", is_vsync);
245
- result.Set("native", native);
246
- result.Set("skipTaskbar", should_skip_taskbar);
247
- result.Set("popupMenu", is_popup_menu);
248
- result.Set("tooltip", is_tooltip);
249
- result.Set("utility", is_utility);
313
+ SDL_GetWindowPosition(window, &x, &y);
314
+ SDL_GetWindowSize(window, &width, &height);
250
315
 
251
- return result;
316
+ int pixel_width, pixel_height;
317
+ SDL_GetWindowSizeInPixels(window, &pixel_width, &pixel_height);
318
+
319
+ display = SDL_GetDisplayForWindow(window);
320
+ 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());
325
+ }
326
+
327
+ Napi::Object result = Napi::Object::New(env);
328
+ result.Set("id", window_id);
329
+ result.Set("x", x);
330
+ result.Set("y", y);
331
+ result.Set("width", width);
332
+ result.Set("height", height);
333
+ result.Set("pixelWidth", pixel_width);
334
+ result.Set("pixelHeight", pixel_height);
335
+ result.Set("displayId", display);
336
+ result.Set("fullscreen", is_fullscreen);
337
+ result.Set("resizable", is_resizable);
338
+ result.Set("borderless", is_borderless);
339
+ result.Set("alwaysOnTop", is_always_on_top);
340
+ if (has_renderer) {
341
+ result.Set("accelerated", is_accelerated);
342
+ result.Set("vsync", is_vsync);
343
+ }
344
+ else {
345
+ result.Set("accelerated", env.Null());
346
+ result.Set("vsync", env.Null());
347
+ }
348
+ result.Set("native", native);
349
+
350
+ return result;
351
+ }
352
+ catch (...) {
353
+ SDL_Renderer *renderer = SDL_GetRenderer(window);
354
+ if (renderer != nullptr) { SDL_DestroyRenderer(renderer); }
355
+ SDL_DestroyWindow(window);
356
+ throw;
357
+ }
252
358
  }
253
359
 
254
360
  Napi::Value
@@ -259,16 +365,15 @@ window::setTitle (const Napi::CallbackInfo &info)
259
365
  int window_id = info[0].As<Napi::Number>().Int32Value();
260
366
  std:: string title = info[1].As<Napi::String>().Utf8Value();
261
367
 
262
- SDL_Window *window = SDL_GetWindowFromID(window_id);
263
- if (window == nullptr) {
368
+ SDL_Window *window = getWindow(env, window_id);
369
+
370
+ if (!SDL_SetWindowTitle(window, title.c_str())) {
264
371
  std::ostringstream message;
265
- message << "SDL_GetWindowFromID(" << window_id << ") error: " << SDL_GetError();
372
+ message << "SDL_SetWindowTitle(" << window_id << ", " << title << ") error: " << SDL_GetError();
266
373
  SDL_ClearError();
267
374
  throw Napi::Error::New(env, message.str());
268
375
  }
269
376
 
270
- SDL_SetWindowTitle(window, title.c_str());
271
-
272
377
  return env.Undefined();
273
378
  }
274
379
 
@@ -281,16 +386,15 @@ window::setPosition (const Napi::CallbackInfo &info)
281
386
  int x = info[1].As<Napi::Number>().Int32Value();
282
387
  int y = info[2].As<Napi::Number>().Int32Value();
283
388
 
284
- SDL_Window *window = SDL_GetWindowFromID(window_id);
285
- if (window == nullptr) {
389
+ SDL_Window *window = getWindow(env, window_id);
390
+
391
+ if (!SDL_SetWindowPosition(window, x, y)) {
286
392
  std::ostringstream message;
287
- message << "SDL_GetWindowFromID(" << window_id << ") error: " << SDL_GetError();
393
+ message << "SDL_SetWindowPosition(" << window_id << ", " << x << ", " << y << ") error: " << SDL_GetError();
288
394
  SDL_ClearError();
289
395
  throw Napi::Error::New(env, message.str());
290
396
  }
291
397
 
292
- SDL_SetWindowPosition(window, x, y);
293
-
294
398
  return env.Undefined();
295
399
  }
296
400
 
@@ -302,25 +406,31 @@ window::setSize (const Napi::CallbackInfo &info)
302
406
  int window_id = info[0].As<Napi::Number>().Int32Value();
303
407
  int width = info[1].As<Napi::Number>().Int32Value();
304
408
  int height = info[2].As<Napi::Number>().Int32Value();
409
+ bool in_pixels = info[3].As<Napi::Boolean>().Value();
305
410
 
306
- SDL_Window *window = SDL_GetWindowFromID(window_id);
307
- if (window == nullptr) {
411
+ SDL_Window *window = getWindow(env, window_id);
412
+
413
+ if (in_pixels) {
414
+ float density = SDL_GetWindowPixelDensity(window);
415
+ 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());
420
+ }
421
+
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);
424
+ }
425
+
426
+ if (!SDL_SetWindowSize(window, width, height)) {
308
427
  std::ostringstream message;
309
- message << "SDL_GetWindowFromID(" << window_id << ") error: " << SDL_GetError();
428
+ message << "SDL_SetWindowSize(" << window_id << ", " << width << ", " << height << ") error: " << SDL_GetError();
310
429
  SDL_ClearError();
311
430
  throw Napi::Error::New(env, message.str());
312
431
  }
313
432
 
314
- SDL_SetWindowSize(window, width, height);
315
-
316
- int pixel_width, pixel_height;
317
- SDL_GetWindowSizeInPixels(window, &pixel_width, &pixel_height);
318
-
319
- Napi::Object result = Napi::Object::New(env);
320
- result.Set("pixelWidth", pixel_width);
321
- result.Set("pixelHeight", pixel_height);
322
-
323
- return result;
433
+ return env.Undefined();
324
434
  }
325
435
 
326
436
  Napi::Value
@@ -329,27 +439,18 @@ window::setFullscreen (const Napi::CallbackInfo &info)
329
439
  Napi::Env env = info.Env();
330
440
 
331
441
  int window_id = info[0].As<Napi::Number>().Int32Value();
332
- int is_fullscreen = info[1].As<Napi::Boolean>().Value();
442
+ bool is_fullscreen = info[1].As<Napi::Boolean>().Value();
333
443
 
334
- SDL_Window *window = SDL_GetWindowFromID(window_id);
335
- if (window == nullptr) {
336
- std::ostringstream message;
337
- message << "SDL_GetWindowFromID(" << window_id << ") error: " << SDL_GetError();
338
- SDL_ClearError();
339
- throw Napi::Error::New(env, message.str());
340
- }
444
+ SDL_Window *window = getWindow(env, window_id);
341
445
 
342
- if (SDL_SetWindowFullscreen(window, is_fullscreen ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0) < 0) {
446
+ if (!SDL_SetWindowFullscreen(window, is_fullscreen)) {
343
447
  std::ostringstream message;
344
448
  message << "SDL_SetWindowFullscreen(" << window_id << ", " << is_fullscreen << ") error: " << SDL_GetError();
345
449
  SDL_ClearError();
346
450
  throw Napi::Error::New(env, message.str());
347
451
  }
348
452
 
349
- int actual_flags = SDL_GetWindowFlags(window);
350
- is_fullscreen = actual_flags & (SDL_WINDOW_FULLSCREEN | SDL_WINDOW_FULLSCREEN_DESKTOP);
351
-
352
- return Napi::Boolean::New(env, is_fullscreen);
453
+ return env.Undefined();
353
454
  }
354
455
 
355
456
  Napi::Value
@@ -360,17 +461,16 @@ window::setResizable (const Napi::CallbackInfo &info)
360
461
  int window_id = info[0].As<Napi::Number>().Int32Value();
361
462
  bool is_resizable = info[1].As<Napi::Boolean>().Value();
362
463
 
363
- SDL_Window *window = SDL_GetWindowFromID(window_id);
364
- if (window == nullptr) {
464
+ SDL_Window *window = getWindow(env, window_id);
465
+
466
+ if (!SDL_SetWindowResizable(window, is_resizable)) {
365
467
  std::ostringstream message;
366
- message << "SDL_GetWindowFromID(" << window_id << ") error: " << SDL_GetError();
468
+ message << "SDL_SetWindowResizable(" << window_id << ", " << is_resizable << ") error: " << SDL_GetError();
367
469
  SDL_ClearError();
368
470
  throw Napi::Error::New(env, message.str());
369
471
  }
370
472
 
371
- SDL_SetWindowResizable(window, is_resizable ? SDL_TRUE : SDL_FALSE);
372
-
373
- int actual_flags = SDL_GetWindowFlags(window);
473
+ Uint64 actual_flags = SDL_GetWindowFlags(window);
374
474
  is_resizable = actual_flags & SDL_WINDOW_RESIZABLE;
375
475
 
376
476
  return Napi::Boolean::New(env, is_resizable);
@@ -384,39 +484,64 @@ window::setBorderless (const Napi::CallbackInfo &info)
384
484
  int window_id = info[0].As<Napi::Number>().Int32Value();
385
485
  bool is_borderless = info[1].As<Napi::Boolean>().Value();
386
486
 
387
- SDL_Window *window = SDL_GetWindowFromID(window_id);
388
- if (window == nullptr) {
487
+ SDL_Window *window = getWindow(env, window_id);
488
+
489
+ if (!SDL_SetWindowBordered(window, !is_borderless)) {
389
490
  std::ostringstream message;
390
- message << "SDL_GetWindowFromID(" << window_id << ") error: " << SDL_GetError();
491
+ message << "SDL_SetWindowBordered(" << window_id << ", " << !is_borderless << ") error: " << SDL_GetError();
391
492
  SDL_ClearError();
392
493
  throw Napi::Error::New(env, message.str());
393
494
  }
394
495
 
395
- SDL_SetWindowBordered(window, is_borderless ? SDL_FALSE : SDL_TRUE);
396
-
397
- int actual_flags = SDL_GetWindowFlags(window);
496
+ Uint64 actual_flags = SDL_GetWindowFlags(window);
398
497
  is_borderless = actual_flags & SDL_WINDOW_BORDERLESS;
399
498
 
400
499
  return Napi::Boolean::New(env, is_borderless);
401
500
  }
402
501
 
403
502
  Napi::Value
404
- window::setAcceleratedAndVsync (const Napi::CallbackInfo &info)
503
+ window::setRelativeMouseMode (const Napi::CallbackInfo &info)
405
504
  {
406
505
  Napi::Env env = info.Env();
407
506
 
408
507
  int window_id = info[0].As<Napi::Number>().Int32Value();
409
- bool is_accelerated = info[1].As<Napi::Boolean>().Value();
410
- bool is_vsync = info[2].As<Napi::Boolean>().Value();
508
+ bool relative = info[1].As<Napi::Boolean>().Value();
411
509
 
412
- SDL_Window *window = SDL_GetWindowFromID(window_id);
413
- if (window == nullptr) {
510
+ SDL_Window *window = getWindow(env, window_id);
511
+
512
+ if (!SDL_SetWindowRelativeMouseMode(window, relative)) {
414
513
  std::ostringstream message;
415
- message << "SDL_GetWindowFromID(" << window_id << ") error: " << SDL_GetError();
514
+ message << "SDL_SetWindowRelativeMouseMode(" << window_id << ", " << relative << ") error: " << SDL_GetError();
416
515
  SDL_ClearError();
417
516
  throw Napi::Error::New(env, message.str());
418
517
  }
419
518
 
519
+ return Napi::Boolean::New(env, SDL_GetWindowRelativeMouseMode(window));
520
+ }
521
+
522
+ Napi::Value
523
+ window::getMouseCaptured (const Napi::CallbackInfo &info)
524
+ {
525
+ Napi::Env env = info.Env();
526
+
527
+ int window_id = info[0].As<Napi::Number>().Int32Value();
528
+
529
+ SDL_Window *window = getWindow(env, window_id);
530
+
531
+ return Napi::Boolean::New(env, (SDL_GetWindowFlags(window) & SDL_WINDOW_MOUSE_CAPTURE) != 0);
532
+ }
533
+
534
+ Napi::Value
535
+ window::setAcceleratedAndVsync (const Napi::CallbackInfo &info)
536
+ {
537
+ Napi::Env env = info.Env();
538
+
539
+ int window_id = info[0].As<Napi::Number>().Int32Value();
540
+ bool is_accelerated = info[1].As<Napi::Boolean>().Value();
541
+ bool is_vsync = info[2].As<Napi::Boolean>().Value();
542
+
543
+ SDL_Window *window = getWindow(env, window_id);
544
+
420
545
  updateRenderer(env, window, &is_accelerated, &is_vsync);
421
546
 
422
547
  Napi::Object result = Napi::Object::New(env);
@@ -433,16 +558,15 @@ window::focus (const Napi::CallbackInfo &info)
433
558
 
434
559
  int window_id = info[0].As<Napi::Number>().Int32Value();
435
560
 
436
- SDL_Window *window = SDL_GetWindowFromID(window_id);
437
- if (window == nullptr) {
561
+ SDL_Window *window = getWindow(env, window_id);
562
+
563
+ if (!SDL_RaiseWindow(window)) {
438
564
  std::ostringstream message;
439
- message << "SDL_GetWindowFromID(" << window_id << ") error: " << SDL_GetError();
565
+ message << "SDL_RaiseWindow(" << window_id << ") error: " << SDL_GetError();
440
566
  SDL_ClearError();
441
567
  throw Napi::Error::New(env, message.str());
442
568
  }
443
569
 
444
- SDL_RaiseWindow(window);
445
-
446
570
  return env.Undefined();
447
571
  }
448
572
 
@@ -453,16 +577,15 @@ window::show (const Napi::CallbackInfo &info)
453
577
 
454
578
  int window_id = info[0].As<Napi::Number>().Int32Value();
455
579
 
456
- SDL_Window *window = SDL_GetWindowFromID(window_id);
457
- if (window == nullptr) {
580
+ SDL_Window *window = getWindow(env, window_id);
581
+
582
+ if (!SDL_ShowWindow(window)) {
458
583
  std::ostringstream message;
459
- message << "SDL_GetWindowFromID(" << window_id << ") error: " << SDL_GetError();
584
+ message << "SDL_ShowWindow(" << window_id << ") error: " << SDL_GetError();
460
585
  SDL_ClearError();
461
586
  throw Napi::Error::New(env, message.str());
462
587
  }
463
588
 
464
- SDL_ShowWindow(window);
465
-
466
589
  return env.Undefined();
467
590
  }
468
591
 
@@ -473,16 +596,15 @@ window::hide (const Napi::CallbackInfo &info)
473
596
 
474
597
  int window_id = info[0].As<Napi::Number>().Int32Value();
475
598
 
476
- SDL_Window *window = SDL_GetWindowFromID(window_id);
477
- if (window == nullptr) {
599
+ SDL_Window *window = getWindow(env, window_id);
600
+
601
+ if (!SDL_HideWindow(window)) {
478
602
  std::ostringstream message;
479
- message << "SDL_GetWindowFromID(" << window_id << ") error: " << SDL_GetError();
603
+ message << "SDL_HideWindow(" << window_id << ") error: " << SDL_GetError();
480
604
  SDL_ClearError();
481
605
  throw Napi::Error::New(env, message.str());
482
606
  }
483
607
 
484
- SDL_HideWindow(window);
485
-
486
608
  return env.Undefined();
487
609
  }
488
610
 
@@ -493,16 +615,15 @@ window::maximize (const Napi::CallbackInfo &info)
493
615
 
494
616
  int window_id = info[0].As<Napi::Number>().Int32Value();
495
617
 
496
- SDL_Window *window = SDL_GetWindowFromID(window_id);
497
- if (window == nullptr) {
618
+ SDL_Window *window = getWindow(env, window_id);
619
+
620
+ if (!SDL_MaximizeWindow(window)) {
498
621
  std::ostringstream message;
499
- message << "SDL_GetWindowFromID(" << window_id << ") error: " << SDL_GetError();
622
+ message << "SDL_MaximizeWindow(" << window_id << ") error: " << SDL_GetError();
500
623
  SDL_ClearError();
501
624
  throw Napi::Error::New(env, message.str());
502
625
  }
503
626
 
504
- SDL_MaximizeWindow(window);
505
-
506
627
  return env.Undefined();
507
628
  }
508
629
 
@@ -513,16 +634,15 @@ window::minimize (const Napi::CallbackInfo &info)
513
634
 
514
635
  int window_id = info[0].As<Napi::Number>().Int32Value();
515
636
 
516
- SDL_Window *window = SDL_GetWindowFromID(window_id);
517
- if (window == nullptr) {
637
+ SDL_Window *window = getWindow(env, window_id);
638
+
639
+ if (!SDL_MinimizeWindow(window)) {
518
640
  std::ostringstream message;
519
- message << "SDL_GetWindowFromID(" << window_id << ") error: " << SDL_GetError();
641
+ message << "SDL_MinimizeWindow(" << window_id << ") error: " << SDL_GetError();
520
642
  SDL_ClearError();
521
643
  throw Napi::Error::New(env, message.str());
522
644
  }
523
645
 
524
- SDL_MinimizeWindow(window);
525
-
526
646
  return env.Undefined();
527
647
  }
528
648
 
@@ -533,16 +653,15 @@ window::restore (const Napi::CallbackInfo &info)
533
653
 
534
654
  int window_id = info[0].As<Napi::Number>().Int32Value();
535
655
 
536
- SDL_Window *window = SDL_GetWindowFromID(window_id);
537
- if (window == nullptr) {
656
+ SDL_Window *window = getWindow(env, window_id);
657
+
658
+ if (!SDL_RestoreWindow(window)) {
538
659
  std::ostringstream message;
539
- message << "SDL_GetWindowFromID(" << window_id << ") error: " << SDL_GetError();
660
+ message << "SDL_RestoreWindow(" << window_id << ") error: " << SDL_GetError();
540
661
  SDL_ClearError();
541
662
  throw Napi::Error::New(env, message.str());
542
663
  }
543
664
 
544
- SDL_RestoreWindow(window);
545
-
546
665
  return env.Undefined();
547
666
  }
548
667
 
@@ -560,42 +679,38 @@ window::render (const Napi::CallbackInfo &info)
560
679
  SDL_ScaleMode scaling = static_cast<SDL_ScaleMode>(info[6].As<Napi::Number>().Int32Value());
561
680
  Napi::Value dstRectVal = info[7];
562
681
  bool hasDstRect = !dstRectVal.IsNull();
563
- SDL_Rect rect;
682
+ SDL_FRect rect;
564
683
  if (hasDstRect) {
565
684
  Napi::Object dstRect = dstRectVal.As<Napi::Object>();
566
- rect.x = dstRect.Get("x").As<Napi::Number>().Int32Value();
567
- rect.y = dstRect.Get("y").As<Napi::Number>().Int32Value();
568
- rect.w = dstRect.Get("width").As<Napi::Number>().Int32Value();
569
- rect.h = dstRect.Get("height").As<Napi::Number>().Int32Value();
685
+ rect.x = dstRect.Get("x").As<Napi::Number>().FloatValue();
686
+ rect.y = dstRect.Get("y").As<Napi::Number>().FloatValue();
687
+ rect.w = dstRect.Get("width").As<Napi::Number>().FloatValue();
688
+ rect.h = dstRect.Get("height").As<Napi::Number>().FloatValue();
570
689
  }
571
690
 
572
- SDL_Window *window = SDL_GetWindowFromID(window_id);
573
- if (window == nullptr) {
574
- std::ostringstream message;
575
- message << "SDL_GetWindowFromID(" << window_id << ") error: " << SDL_GetError();
576
- SDL_ClearError();
577
- throw Napi::Error::New(env, message.str());
578
- }
691
+ SDL_Window *window = getWindow(env, window_id);
579
692
 
580
693
  SDL_Renderer *renderer = SDL_GetRenderer(window);
581
694
  if (renderer == nullptr) {
582
695
  std::ostringstream message;
583
- message << "SDL_GetRenderer(" << window_id << ") error: " << SDL_GetError();
584
- SDL_ClearError();
696
+ message << "SDL_GetRenderer(" << window_id << ") error: window has no renderer";
585
697
  throw Napi::Error::New(env, message.str());
586
698
  }
587
699
 
588
- CachedTexture &cached = cachedTextures[window];
589
- SDL_Texture *texture = cached.texture;
700
+ SDL_PropertiesID props = SDL_GetWindowProperties(window);
701
+ SDL_Texture *texture = (SDL_Texture *) SDL_GetPointerProperty(props, TEXTURE_PROPERTY, nullptr);
590
702
 
591
703
  if (texture == nullptr
592
- || cached.width != width
593
- || cached.height != height
594
- || cached.format != format
704
+ || texture->w != width
705
+ || texture->h != height
706
+ || texture->format != (SDL_PixelFormat) format
595
707
  ) {
596
- if (texture != nullptr) { SDL_DestroyTexture(texture); }
708
+ if (texture != nullptr) {
709
+ SDL_DestroyTexture(texture);
710
+ SDL_SetPointerProperty(props, TEXTURE_PROPERTY, nullptr);
711
+ }
597
712
 
598
- texture = SDL_CreateTexture(renderer, format, SDL_TEXTUREACCESS_STREAMING, width, height);
713
+ texture = SDL_CreateTexture(renderer, (SDL_PixelFormat) format, SDL_TEXTUREACCESS_STREAMING, width, height);
599
714
  if (texture == nullptr) {
600
715
  std::ostringstream message;
601
716
  message << "SDL_CreateTexture(" << width << ", " << height << ", " << format << ") error: " << SDL_GetError();
@@ -603,43 +718,57 @@ window::render (const Napi::CallbackInfo &info)
603
718
  throw Napi::Error::New(env, message.str());
604
719
  }
605
720
 
606
- if (SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_NONE) < 0) {
721
+ if (!SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_NONE)) {
607
722
  std::ostringstream message;
608
723
  message << "SDL_SetTextureBlendMode(" << window_id << ") error: " << SDL_GetError();
609
724
  SDL_ClearError();
725
+ SDL_DestroyTexture(texture);
610
726
  throw Napi::Error::New(env, message.str());
611
727
  }
612
728
 
613
- cached.texture = texture;
614
- cached.width = width;
615
- cached.height = height;
616
- cached.format = format;
729
+ if (!SDL_SetPointerProperty(props, TEXTURE_PROPERTY, texture)) {
730
+ std::ostringstream message;
731
+ message << "SDL_SetPointerProperty(" << window_id << ") error: " << SDL_GetError();
732
+ SDL_ClearError();
733
+ SDL_DestroyTexture(texture);
734
+ throw Napi::Error::New(env, message.str());
735
+ }
617
736
  }
618
737
 
619
- if(SDL_SetTextureScaleMode(texture, scaling) < 0) {
738
+ if(!SDL_SetTextureScaleMode(texture, scaling)) {
620
739
  std::ostringstream message;
621
740
  message << "SDL_SetTextureScaleMode(" << window_id << ", " << scaling << ") error: " << SDL_GetError();
622
741
  SDL_ClearError();
623
742
  throw Napi::Error::New(env, message.str());
624
743
  }
625
744
 
626
- SDL_UpdateTexture(texture, nullptr, pixels, stride);
745
+ 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());
750
+ }
627
751
 
628
- if (SDL_RenderClear(renderer) < 0) {
752
+ if (!SDL_RenderClear(renderer)) {
629
753
  std::ostringstream message;
630
754
  message << "SDL_RenderClear(" << window_id << ") error: " << SDL_GetError();
631
755
  SDL_ClearError();
632
756
  throw Napi::Error::New(env, message.str());
633
757
  }
634
758
 
635
- if (SDL_RenderCopy(renderer, texture, nullptr, hasDstRect ? &rect : nullptr) < 0) {
759
+ if (!SDL_RenderTexture(renderer, texture, nullptr, hasDstRect ? &rect : nullptr)) {
636
760
  std::ostringstream message;
637
- message << "SDL_RenderCopy(" << window_id << ") error: " << SDL_GetError();
761
+ message << "SDL_RenderTexture(" << window_id << ") error: " << SDL_GetError();
638
762
  SDL_ClearError();
639
763
  throw Napi::Error::New(env, message.str());
640
764
  }
641
765
 
642
- SDL_RenderPresent(renderer);
766
+ 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());
771
+ }
643
772
 
644
773
  return env.Undefined();
645
774
  }
@@ -656,26 +785,25 @@ window::setIcon (const Napi::CallbackInfo &info)
656
785
  unsigned int format = info[4].As<Napi::Number>().Int32Value();
657
786
  void *pixels = info[5].As<Napi::Buffer<char>>().Data();
658
787
 
659
- SDL_Window *window = SDL_GetWindowFromID(window_id);
660
- if (window == nullptr) {
788
+ SDL_Window *window = getWindow(env, window_id);
789
+
790
+ SDL_Surface* surface = SDL_CreateSurfaceFrom(w, h, (SDL_PixelFormat) format, pixels, stride);
791
+ if (surface == nullptr) {
661
792
  std::ostringstream message;
662
- message << "SDL_GetWindowFromID(" << window_id << ") error: " << SDL_GetError();
793
+ message << "SDL_CreateSurfaceFrom(" << window_id << ", " << w << ", " << h << ", " << format << ") error: " << SDL_GetError();
663
794
  SDL_ClearError();
664
795
  throw Napi::Error::New(env, message.str());
665
796
  }
666
797
 
667
- SDL_Surface* surface = SDL_CreateRGBSurfaceWithFormatFrom(pixels, w, h, SDL_BITSPERPIXEL(format), stride, format);
668
- if (surface == nullptr) {
798
+ bool success = SDL_SetWindowIcon(window, surface);
799
+ SDL_DestroySurface(surface);
800
+ if (!success) {
669
801
  std::ostringstream message;
670
- message << "SDL_CreateRGBSurfaceWithFormatFrom(" << window_id << ", " << w << ", " << h << ", " << format << ") error: " << SDL_GetError();
802
+ message << "SDL_SetWindowIcon(" << window_id << ") error: " << SDL_GetError();
671
803
  SDL_ClearError();
672
804
  throw Napi::Error::New(env, message.str());
673
805
  }
674
806
 
675
- SDL_SetWindowIcon(window, surface);
676
-
677
- SDL_FreeSurface(surface);
678
-
679
807
  return env.Undefined();
680
808
  }
681
809
 
@@ -687,13 +815,7 @@ window::flash (const Napi::CallbackInfo &info)
687
815
  int window_id = info[0].As<Napi::Number>().Int32Value();
688
816
  int type = info[1].As<Napi::Number>().Int32Value();
689
817
 
690
- SDL_Window *window = SDL_GetWindowFromID(window_id);
691
- if (window == nullptr) {
692
- std::ostringstream message;
693
- message << "SDL_GetWindowFromID(" << window_id << ") error: " << SDL_GetError();
694
- SDL_ClearError();
695
- throw Napi::Error::New(env, message.str());
696
- }
818
+ SDL_Window *window = getWindow(env, window_id);
697
819
 
698
820
  SDL_FlashOperation op;
699
821
  switch (type) {
@@ -702,7 +824,7 @@ window::flash (const Napi::CallbackInfo &info)
702
824
  default: op = SDL_FLASH_CANCEL; break;
703
825
  }
704
826
 
705
- if (SDL_FlashWindow(window, op) < 0) {
827
+ if (!SDL_FlashWindow(window, op)) {
706
828
  std::ostringstream message;
707
829
  message << "SDL_FlashWindow(" << window_id << ", " << type << ") error: " << SDL_GetError();
708
830
  SDL_ClearError();
@@ -719,22 +841,10 @@ window::destroy (const Napi::CallbackInfo &info)
719
841
 
720
842
  int window_id = info[0].As<Napi::Number>().Int32Value();
721
843
 
722
- SDL_Window *window = SDL_GetWindowFromID(window_id);
723
- if (window == nullptr) {
724
- std::ostringstream message;
725
- message << "SDL_GetWindowFromID(" << window_id << ") error: " << SDL_GetError();
726
- SDL_ClearError();
727
- throw Napi::Error::New(env, message.str());
728
- }
844
+ SDL_Window *window = getWindow(env, window_id);
729
845
 
730
846
  SDL_Renderer *renderer = SDL_GetRenderer(window);
731
- if (renderer != nullptr) {
732
- SDL_DestroyRenderer(renderer);
733
-
734
- CachedTexture &cached = cachedTextures[window];
735
- if (cached.texture != nullptr) { SDL_DestroyTexture(cached.texture); }
736
- cachedTextures.erase(window);
737
- }
847
+ if (renderer != nullptr) { SDL_DestroyRenderer(renderer); }
738
848
 
739
849
  SDL_DestroyWindow(window);
740
850