@kmamal/sdl 0.11.13 → 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 +249 -0
  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 +406 -318
  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,164 +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;
164
-
165
- SDL_GetWindowPosition(window, &x, &y);
166
-
167
- int pixel_width, pixel_height;
168
- SDL_GetWindowSizeInPixels(window, &pixel_width, &pixel_height);
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
+ }
169
165
 
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
- }
166
+ if (!SDL_StartTextInput(window)) { SDL_ClearError(); }
177
167
 
178
- Napi::Object native = Napi::Object::New(env);
168
+ Napi::Object native = Napi::Object::New(env);
179
169
 
180
- bool has_wm_info;
181
- std::string message_of_failed_wm_info;
182
- SDL_SysWMinfo sys_wm_info;
183
- SDL_VERSION(&(sys_wm_info.version));
184
- has_wm_info = SDL_GetWindowWMInfo(window, &sys_wm_info) == SDL_TRUE;
185
- if (!has_wm_info) {
186
- message_of_failed_wm_info = SDL_GetError();
187
- SDL_ClearError();
188
- }
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
+ }
189
177
 
190
- Napi::Value native_handle;
191
- if (has_wm_info) {
192
- NativeWindowHandle _native_handle;
193
- #if defined(__LINUX__)
194
- _native_handle = sys_wm_info.info.x11.window;
195
- #elif defined(__WIN32__)
196
- _native_handle = sys_wm_info.info.win.window;
197
- #elif defined(__MACOSX__)
198
- _native_handle = getCocoaWindowHandle(sys_wm_info.info.cocoa.window);
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());
199
198
  #endif
200
- native_handle = Napi::Buffer<NativeWindowHandle>::Copy(env, &_native_handle, 1);
201
- }
202
- else {
203
- native_handle = env.Null();
204
- }
205
- native.Set("handle", native_handle);
206
199
 
207
- if (is_opengl) {
208
- if (!has_wm_info) {
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
+ }
299
+
300
+ if (is_visible && !SDL_ShowWindow(window)) {
209
301
  std::ostringstream message;
210
- message << "Window has set { opengl: true } but SDL_GetWindowWMInfo failed with: " << message_of_failed_wm_info;
302
+ message << "SDL_ShowWindow(" << window_id << ") error: " << SDL_GetError();
211
303
  SDL_ClearError();
212
304
  throw Napi::Error::New(env, message.str());
213
305
  }
214
306
 
215
- GL_NativeWindow native_gl;
216
- #if defined(__LINUX__)
217
- native_gl = sys_wm_info.info.x11.window;
218
- #elif defined(__WIN32__)
219
- native_gl = sys_wm_info.info.win.window;
220
- #elif defined(__MACOSX__)
221
- native_gl = getCocoaGlView(sys_wm_info.info.cocoa.window);
222
- #endif
223
- native.Set("gl", Napi::Buffer<GL_NativeWindow>::Copy(env, &native_gl, 1));
224
- }
225
- else if (is_webgpu) {
226
- if (!has_wm_info) {
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;
312
+
313
+ SDL_GetWindowPosition(window, &x, &y);
314
+ SDL_GetWindowSize(window, &width, &height);
315
+
316
+ int pixel_width, pixel_height;
317
+ SDL_GetWindowSizeInPixels(window, &pixel_width, &pixel_height);
318
+
319
+ display = SDL_GetDisplayForWindow(window);
320
+ if (display == 0) {
227
321
  std::ostringstream message;
228
- message << "Window has set { webgpu: true } but SDL_GetWindowWMInfo failed with: " << message_of_failed_wm_info;
322
+ message << "SDL_GetDisplayForWindow(" << window_id << ") error: " << SDL_GetError();
229
323
  SDL_ClearError();
230
324
  throw Napi::Error::New(env, message.str());
231
325
  }
232
326
 
233
- GPU_NativeData native_gpu;
234
- #if defined(__LINUX__)
235
- native_gpu.display = sys_wm_info.info.x11.display;
236
- native_gpu.window = sys_wm_info.info.x11.window;
237
- #elif defined(__WIN32__)
238
- native_gpu.hwnd = sys_wm_info.info.win.window;
239
- native_gpu.hinstance = sys_wm_info.info.win.hinstance;
240
- #elif defined(__MACOSX__)
241
- native_gpu.layer = getCocoaGpuView(sys_wm_info.info.cocoa.window);
242
- #endif
243
- native.Set("gpu", Napi::Buffer<GPU_NativeData>::Copy(env, &native_gpu, 1));
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;
244
351
  }
245
- else {
246
- cachedTextures[window] = {};
247
- updateRenderer(env, window, &is_accelerated, &is_vsync);
352
+ catch (...) {
353
+ SDL_Renderer *renderer = SDL_GetRenderer(window);
354
+ if (renderer != nullptr) { SDL_DestroyRenderer(renderer); }
355
+ SDL_DestroyWindow(window);
356
+ throw;
248
357
  }
249
-
250
- if (is_visible) { SDL_ShowWindow(window); }
251
-
252
- Napi::Object result = Napi::Object::New(env);
253
- result.Set("id", window_id);
254
- result.Set("x", x);
255
- result.Set("y", y);
256
- result.Set("width", width);
257
- result.Set("height", height);
258
- result.Set("pixelWidth", pixel_width);
259
- result.Set("pixelHeight", pixel_height);
260
- result.Set("displayIndex", display);
261
- result.Set("fullscreen", is_fullscreen);
262
- result.Set("resizable", is_resizable);
263
- result.Set("borderless", is_borderless);
264
- result.Set("alwaysOnTop", is_always_on_top);
265
- result.Set("accelerated", is_accelerated);
266
- result.Set("vsync", is_vsync);
267
- result.Set("native", native);
268
- result.Set("skipTaskbar", should_skip_taskbar);
269
- result.Set("popupMenu", is_popup_menu);
270
- result.Set("tooltip", is_tooltip);
271
- result.Set("utility", is_utility);
272
-
273
- return result;
274
358
  }
275
359
 
276
360
  Napi::Value
@@ -281,16 +365,15 @@ window::setTitle (const Napi::CallbackInfo &info)
281
365
  int window_id = info[0].As<Napi::Number>().Int32Value();
282
366
  std:: string title = info[1].As<Napi::String>().Utf8Value();
283
367
 
284
- SDL_Window *window = SDL_GetWindowFromID(window_id);
285
- if (window == nullptr) {
368
+ SDL_Window *window = getWindow(env, window_id);
369
+
370
+ if (!SDL_SetWindowTitle(window, title.c_str())) {
286
371
  std::ostringstream message;
287
- message << "SDL_GetWindowFromID(" << window_id << ") error: " << SDL_GetError();
372
+ message << "SDL_SetWindowTitle(" << window_id << ", " << title << ") error: " << SDL_GetError();
288
373
  SDL_ClearError();
289
374
  throw Napi::Error::New(env, message.str());
290
375
  }
291
376
 
292
- SDL_SetWindowTitle(window, title.c_str());
293
-
294
377
  return env.Undefined();
295
378
  }
296
379
 
@@ -303,16 +386,15 @@ window::setPosition (const Napi::CallbackInfo &info)
303
386
  int x = info[1].As<Napi::Number>().Int32Value();
304
387
  int y = info[2].As<Napi::Number>().Int32Value();
305
388
 
306
- SDL_Window *window = SDL_GetWindowFromID(window_id);
307
- if (window == nullptr) {
389
+ SDL_Window *window = getWindow(env, window_id);
390
+
391
+ if (!SDL_SetWindowPosition(window, x, y)) {
308
392
  std::ostringstream message;
309
- message << "SDL_GetWindowFromID(" << window_id << ") error: " << SDL_GetError();
393
+ message << "SDL_SetWindowPosition(" << window_id << ", " << x << ", " << y << ") error: " << SDL_GetError();
310
394
  SDL_ClearError();
311
395
  throw Napi::Error::New(env, message.str());
312
396
  }
313
397
 
314
- SDL_SetWindowPosition(window, x, y);
315
-
316
398
  return env.Undefined();
317
399
  }
318
400
 
@@ -324,25 +406,31 @@ window::setSize (const Napi::CallbackInfo &info)
324
406
  int window_id = info[0].As<Napi::Number>().Int32Value();
325
407
  int width = info[1].As<Napi::Number>().Int32Value();
326
408
  int height = info[2].As<Napi::Number>().Int32Value();
409
+ bool in_pixels = info[3].As<Napi::Boolean>().Value();
327
410
 
328
- SDL_Window *window = SDL_GetWindowFromID(window_id);
329
- 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)) {
330
427
  std::ostringstream message;
331
- message << "SDL_GetWindowFromID(" << window_id << ") error: " << SDL_GetError();
428
+ message << "SDL_SetWindowSize(" << window_id << ", " << width << ", " << height << ") error: " << SDL_GetError();
332
429
  SDL_ClearError();
333
430
  throw Napi::Error::New(env, message.str());
334
431
  }
335
432
 
336
- SDL_SetWindowSize(window, width, height);
337
-
338
- int pixel_width, pixel_height;
339
- SDL_GetWindowSizeInPixels(window, &pixel_width, &pixel_height);
340
-
341
- Napi::Object result = Napi::Object::New(env);
342
- result.Set("pixelWidth", pixel_width);
343
- result.Set("pixelHeight", pixel_height);
344
-
345
- return result;
433
+ return env.Undefined();
346
434
  }
347
435
 
348
436
  Napi::Value
@@ -351,27 +439,18 @@ window::setFullscreen (const Napi::CallbackInfo &info)
351
439
  Napi::Env env = info.Env();
352
440
 
353
441
  int window_id = info[0].As<Napi::Number>().Int32Value();
354
- int is_fullscreen = info[1].As<Napi::Boolean>().Value();
442
+ bool is_fullscreen = info[1].As<Napi::Boolean>().Value();
355
443
 
356
- SDL_Window *window = SDL_GetWindowFromID(window_id);
357
- if (window == nullptr) {
358
- std::ostringstream message;
359
- message << "SDL_GetWindowFromID(" << window_id << ") error: " << SDL_GetError();
360
- SDL_ClearError();
361
- throw Napi::Error::New(env, message.str());
362
- }
444
+ SDL_Window *window = getWindow(env, window_id);
363
445
 
364
- if (SDL_SetWindowFullscreen(window, is_fullscreen ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0) < 0) {
446
+ if (!SDL_SetWindowFullscreen(window, is_fullscreen)) {
365
447
  std::ostringstream message;
366
448
  message << "SDL_SetWindowFullscreen(" << window_id << ", " << is_fullscreen << ") error: " << SDL_GetError();
367
449
  SDL_ClearError();
368
450
  throw Napi::Error::New(env, message.str());
369
451
  }
370
452
 
371
- int actual_flags = SDL_GetWindowFlags(window);
372
- is_fullscreen = actual_flags & (SDL_WINDOW_FULLSCREEN | SDL_WINDOW_FULLSCREEN_DESKTOP);
373
-
374
- return Napi::Boolean::New(env, is_fullscreen);
453
+ return env.Undefined();
375
454
  }
376
455
 
377
456
  Napi::Value
@@ -382,17 +461,16 @@ window::setResizable (const Napi::CallbackInfo &info)
382
461
  int window_id = info[0].As<Napi::Number>().Int32Value();
383
462
  bool is_resizable = info[1].As<Napi::Boolean>().Value();
384
463
 
385
- SDL_Window *window = SDL_GetWindowFromID(window_id);
386
- if (window == nullptr) {
464
+ SDL_Window *window = getWindow(env, window_id);
465
+
466
+ if (!SDL_SetWindowResizable(window, is_resizable)) {
387
467
  std::ostringstream message;
388
- message << "SDL_GetWindowFromID(" << window_id << ") error: " << SDL_GetError();
468
+ message << "SDL_SetWindowResizable(" << window_id << ", " << is_resizable << ") error: " << SDL_GetError();
389
469
  SDL_ClearError();
390
470
  throw Napi::Error::New(env, message.str());
391
471
  }
392
472
 
393
- SDL_SetWindowResizable(window, is_resizable ? SDL_TRUE : SDL_FALSE);
394
-
395
- int actual_flags = SDL_GetWindowFlags(window);
473
+ Uint64 actual_flags = SDL_GetWindowFlags(window);
396
474
  is_resizable = actual_flags & SDL_WINDOW_RESIZABLE;
397
475
 
398
476
  return Napi::Boolean::New(env, is_resizable);
@@ -406,39 +484,64 @@ window::setBorderless (const Napi::CallbackInfo &info)
406
484
  int window_id = info[0].As<Napi::Number>().Int32Value();
407
485
  bool is_borderless = info[1].As<Napi::Boolean>().Value();
408
486
 
409
- SDL_Window *window = SDL_GetWindowFromID(window_id);
410
- if (window == nullptr) {
487
+ SDL_Window *window = getWindow(env, window_id);
488
+
489
+ if (!SDL_SetWindowBordered(window, !is_borderless)) {
411
490
  std::ostringstream message;
412
- message << "SDL_GetWindowFromID(" << window_id << ") error: " << SDL_GetError();
491
+ message << "SDL_SetWindowBordered(" << window_id << ", " << !is_borderless << ") error: " << SDL_GetError();
413
492
  SDL_ClearError();
414
493
  throw Napi::Error::New(env, message.str());
415
494
  }
416
495
 
417
- SDL_SetWindowBordered(window, is_borderless ? SDL_FALSE : SDL_TRUE);
418
-
419
- int actual_flags = SDL_GetWindowFlags(window);
496
+ Uint64 actual_flags = SDL_GetWindowFlags(window);
420
497
  is_borderless = actual_flags & SDL_WINDOW_BORDERLESS;
421
498
 
422
499
  return Napi::Boolean::New(env, is_borderless);
423
500
  }
424
501
 
425
502
  Napi::Value
426
- window::setAcceleratedAndVsync (const Napi::CallbackInfo &info)
503
+ window::setRelativeMouseMode (const Napi::CallbackInfo &info)
427
504
  {
428
505
  Napi::Env env = info.Env();
429
506
 
430
507
  int window_id = info[0].As<Napi::Number>().Int32Value();
431
- bool is_accelerated = info[1].As<Napi::Boolean>().Value();
432
- bool is_vsync = info[2].As<Napi::Boolean>().Value();
508
+ bool relative = info[1].As<Napi::Boolean>().Value();
433
509
 
434
- SDL_Window *window = SDL_GetWindowFromID(window_id);
435
- if (window == nullptr) {
510
+ SDL_Window *window = getWindow(env, window_id);
511
+
512
+ if (!SDL_SetWindowRelativeMouseMode(window, relative)) {
436
513
  std::ostringstream message;
437
- message << "SDL_GetWindowFromID(" << window_id << ") error: " << SDL_GetError();
514
+ message << "SDL_SetWindowRelativeMouseMode(" << window_id << ", " << relative << ") error: " << SDL_GetError();
438
515
  SDL_ClearError();
439
516
  throw Napi::Error::New(env, message.str());
440
517
  }
441
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
+
442
545
  updateRenderer(env, window, &is_accelerated, &is_vsync);
443
546
 
444
547
  Napi::Object result = Napi::Object::New(env);
@@ -455,16 +558,15 @@ window::focus (const Napi::CallbackInfo &info)
455
558
 
456
559
  int window_id = info[0].As<Napi::Number>().Int32Value();
457
560
 
458
- SDL_Window *window = SDL_GetWindowFromID(window_id);
459
- if (window == nullptr) {
561
+ SDL_Window *window = getWindow(env, window_id);
562
+
563
+ if (!SDL_RaiseWindow(window)) {
460
564
  std::ostringstream message;
461
- message << "SDL_GetWindowFromID(" << window_id << ") error: " << SDL_GetError();
565
+ message << "SDL_RaiseWindow(" << window_id << ") error: " << SDL_GetError();
462
566
  SDL_ClearError();
463
567
  throw Napi::Error::New(env, message.str());
464
568
  }
465
569
 
466
- SDL_RaiseWindow(window);
467
-
468
570
  return env.Undefined();
469
571
  }
470
572
 
@@ -475,16 +577,15 @@ window::show (const Napi::CallbackInfo &info)
475
577
 
476
578
  int window_id = info[0].As<Napi::Number>().Int32Value();
477
579
 
478
- SDL_Window *window = SDL_GetWindowFromID(window_id);
479
- if (window == nullptr) {
580
+ SDL_Window *window = getWindow(env, window_id);
581
+
582
+ if (!SDL_ShowWindow(window)) {
480
583
  std::ostringstream message;
481
- message << "SDL_GetWindowFromID(" << window_id << ") error: " << SDL_GetError();
584
+ message << "SDL_ShowWindow(" << window_id << ") error: " << SDL_GetError();
482
585
  SDL_ClearError();
483
586
  throw Napi::Error::New(env, message.str());
484
587
  }
485
588
 
486
- SDL_ShowWindow(window);
487
-
488
589
  return env.Undefined();
489
590
  }
490
591
 
@@ -495,16 +596,15 @@ window::hide (const Napi::CallbackInfo &info)
495
596
 
496
597
  int window_id = info[0].As<Napi::Number>().Int32Value();
497
598
 
498
- SDL_Window *window = SDL_GetWindowFromID(window_id);
499
- if (window == nullptr) {
599
+ SDL_Window *window = getWindow(env, window_id);
600
+
601
+ if (!SDL_HideWindow(window)) {
500
602
  std::ostringstream message;
501
- message << "SDL_GetWindowFromID(" << window_id << ") error: " << SDL_GetError();
603
+ message << "SDL_HideWindow(" << window_id << ") error: " << SDL_GetError();
502
604
  SDL_ClearError();
503
605
  throw Napi::Error::New(env, message.str());
504
606
  }
505
607
 
506
- SDL_HideWindow(window);
507
-
508
608
  return env.Undefined();
509
609
  }
510
610
 
@@ -515,16 +615,15 @@ window::maximize (const Napi::CallbackInfo &info)
515
615
 
516
616
  int window_id = info[0].As<Napi::Number>().Int32Value();
517
617
 
518
- SDL_Window *window = SDL_GetWindowFromID(window_id);
519
- if (window == nullptr) {
618
+ SDL_Window *window = getWindow(env, window_id);
619
+
620
+ if (!SDL_MaximizeWindow(window)) {
520
621
  std::ostringstream message;
521
- message << "SDL_GetWindowFromID(" << window_id << ") error: " << SDL_GetError();
622
+ message << "SDL_MaximizeWindow(" << window_id << ") error: " << SDL_GetError();
522
623
  SDL_ClearError();
523
624
  throw Napi::Error::New(env, message.str());
524
625
  }
525
626
 
526
- SDL_MaximizeWindow(window);
527
-
528
627
  return env.Undefined();
529
628
  }
530
629
 
@@ -535,16 +634,15 @@ window::minimize (const Napi::CallbackInfo &info)
535
634
 
536
635
  int window_id = info[0].As<Napi::Number>().Int32Value();
537
636
 
538
- SDL_Window *window = SDL_GetWindowFromID(window_id);
539
- if (window == nullptr) {
637
+ SDL_Window *window = getWindow(env, window_id);
638
+
639
+ if (!SDL_MinimizeWindow(window)) {
540
640
  std::ostringstream message;
541
- message << "SDL_GetWindowFromID(" << window_id << ") error: " << SDL_GetError();
641
+ message << "SDL_MinimizeWindow(" << window_id << ") error: " << SDL_GetError();
542
642
  SDL_ClearError();
543
643
  throw Napi::Error::New(env, message.str());
544
644
  }
545
645
 
546
- SDL_MinimizeWindow(window);
547
-
548
646
  return env.Undefined();
549
647
  }
550
648
 
@@ -555,16 +653,15 @@ window::restore (const Napi::CallbackInfo &info)
555
653
 
556
654
  int window_id = info[0].As<Napi::Number>().Int32Value();
557
655
 
558
- SDL_Window *window = SDL_GetWindowFromID(window_id);
559
- if (window == nullptr) {
656
+ SDL_Window *window = getWindow(env, window_id);
657
+
658
+ if (!SDL_RestoreWindow(window)) {
560
659
  std::ostringstream message;
561
- message << "SDL_GetWindowFromID(" << window_id << ") error: " << SDL_GetError();
660
+ message << "SDL_RestoreWindow(" << window_id << ") error: " << SDL_GetError();
562
661
  SDL_ClearError();
563
662
  throw Napi::Error::New(env, message.str());
564
663
  }
565
664
 
566
- SDL_RestoreWindow(window);
567
-
568
665
  return env.Undefined();
569
666
  }
570
667
 
@@ -582,42 +679,38 @@ window::render (const Napi::CallbackInfo &info)
582
679
  SDL_ScaleMode scaling = static_cast<SDL_ScaleMode>(info[6].As<Napi::Number>().Int32Value());
583
680
  Napi::Value dstRectVal = info[7];
584
681
  bool hasDstRect = !dstRectVal.IsNull();
585
- SDL_Rect rect;
682
+ SDL_FRect rect;
586
683
  if (hasDstRect) {
587
684
  Napi::Object dstRect = dstRectVal.As<Napi::Object>();
588
- rect.x = dstRect.Get("x").As<Napi::Number>().Int32Value();
589
- rect.y = dstRect.Get("y").As<Napi::Number>().Int32Value();
590
- rect.w = dstRect.Get("width").As<Napi::Number>().Int32Value();
591
- 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();
592
689
  }
593
690
 
594
- SDL_Window *window = SDL_GetWindowFromID(window_id);
595
- if (window == nullptr) {
596
- std::ostringstream message;
597
- message << "SDL_GetWindowFromID(" << window_id << ") error: " << SDL_GetError();
598
- SDL_ClearError();
599
- throw Napi::Error::New(env, message.str());
600
- }
691
+ SDL_Window *window = getWindow(env, window_id);
601
692
 
602
693
  SDL_Renderer *renderer = SDL_GetRenderer(window);
603
694
  if (renderer == nullptr) {
604
695
  std::ostringstream message;
605
- message << "SDL_GetRenderer(" << window_id << ") error: " << SDL_GetError();
606
- SDL_ClearError();
696
+ message << "SDL_GetRenderer(" << window_id << ") error: window has no renderer";
607
697
  throw Napi::Error::New(env, message.str());
608
698
  }
609
699
 
610
- CachedTexture &cached = cachedTextures[window];
611
- SDL_Texture *texture = cached.texture;
700
+ SDL_PropertiesID props = SDL_GetWindowProperties(window);
701
+ SDL_Texture *texture = (SDL_Texture *) SDL_GetPointerProperty(props, TEXTURE_PROPERTY, nullptr);
612
702
 
613
703
  if (texture == nullptr
614
- || cached.width != width
615
- || cached.height != height
616
- || cached.format != format
704
+ || texture->w != width
705
+ || texture->h != height
706
+ || texture->format != (SDL_PixelFormat) format
617
707
  ) {
618
- if (texture != nullptr) { SDL_DestroyTexture(texture); }
708
+ if (texture != nullptr) {
709
+ SDL_DestroyTexture(texture);
710
+ SDL_SetPointerProperty(props, TEXTURE_PROPERTY, nullptr);
711
+ }
619
712
 
620
- texture = SDL_CreateTexture(renderer, format, SDL_TEXTUREACCESS_STREAMING, width, height);
713
+ texture = SDL_CreateTexture(renderer, (SDL_PixelFormat) format, SDL_TEXTUREACCESS_STREAMING, width, height);
621
714
  if (texture == nullptr) {
622
715
  std::ostringstream message;
623
716
  message << "SDL_CreateTexture(" << width << ", " << height << ", " << format << ") error: " << SDL_GetError();
@@ -625,43 +718,57 @@ window::render (const Napi::CallbackInfo &info)
625
718
  throw Napi::Error::New(env, message.str());
626
719
  }
627
720
 
628
- if (SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_NONE) < 0) {
721
+ if (!SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_NONE)) {
629
722
  std::ostringstream message;
630
723
  message << "SDL_SetTextureBlendMode(" << window_id << ") error: " << SDL_GetError();
631
724
  SDL_ClearError();
725
+ SDL_DestroyTexture(texture);
632
726
  throw Napi::Error::New(env, message.str());
633
727
  }
634
728
 
635
- cached.texture = texture;
636
- cached.width = width;
637
- cached.height = height;
638
- 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
+ }
639
736
  }
640
737
 
641
- if(SDL_SetTextureScaleMode(texture, scaling) < 0) {
738
+ if(!SDL_SetTextureScaleMode(texture, scaling)) {
642
739
  std::ostringstream message;
643
740
  message << "SDL_SetTextureScaleMode(" << window_id << ", " << scaling << ") error: " << SDL_GetError();
644
741
  SDL_ClearError();
645
742
  throw Napi::Error::New(env, message.str());
646
743
  }
647
744
 
648
- 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
+ }
649
751
 
650
- if (SDL_RenderClear(renderer) < 0) {
752
+ if (!SDL_RenderClear(renderer)) {
651
753
  std::ostringstream message;
652
754
  message << "SDL_RenderClear(" << window_id << ") error: " << SDL_GetError();
653
755
  SDL_ClearError();
654
756
  throw Napi::Error::New(env, message.str());
655
757
  }
656
758
 
657
- if (SDL_RenderCopy(renderer, texture, nullptr, hasDstRect ? &rect : nullptr) < 0) {
759
+ if (!SDL_RenderTexture(renderer, texture, nullptr, hasDstRect ? &rect : nullptr)) {
658
760
  std::ostringstream message;
659
- message << "SDL_RenderCopy(" << window_id << ") error: " << SDL_GetError();
761
+ message << "SDL_RenderTexture(" << window_id << ") error: " << SDL_GetError();
660
762
  SDL_ClearError();
661
763
  throw Napi::Error::New(env, message.str());
662
764
  }
663
765
 
664
- 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
+ }
665
772
 
666
773
  return env.Undefined();
667
774
  }
@@ -678,26 +785,25 @@ window::setIcon (const Napi::CallbackInfo &info)
678
785
  unsigned int format = info[4].As<Napi::Number>().Int32Value();
679
786
  void *pixels = info[5].As<Napi::Buffer<char>>().Data();
680
787
 
681
- SDL_Window *window = SDL_GetWindowFromID(window_id);
682
- 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) {
683
792
  std::ostringstream message;
684
- message << "SDL_GetWindowFromID(" << window_id << ") error: " << SDL_GetError();
793
+ message << "SDL_CreateSurfaceFrom(" << window_id << ", " << w << ", " << h << ", " << format << ") error: " << SDL_GetError();
685
794
  SDL_ClearError();
686
795
  throw Napi::Error::New(env, message.str());
687
796
  }
688
797
 
689
- SDL_Surface* surface = SDL_CreateRGBSurfaceWithFormatFrom(pixels, w, h, SDL_BITSPERPIXEL(format), stride, format);
690
- if (surface == nullptr) {
798
+ bool success = SDL_SetWindowIcon(window, surface);
799
+ SDL_DestroySurface(surface);
800
+ if (!success) {
691
801
  std::ostringstream message;
692
- message << "SDL_CreateRGBSurfaceWithFormatFrom(" << window_id << ", " << w << ", " << h << ", " << format << ") error: " << SDL_GetError();
802
+ message << "SDL_SetWindowIcon(" << window_id << ") error: " << SDL_GetError();
693
803
  SDL_ClearError();
694
804
  throw Napi::Error::New(env, message.str());
695
805
  }
696
806
 
697
- SDL_SetWindowIcon(window, surface);
698
-
699
- SDL_FreeSurface(surface);
700
-
701
807
  return env.Undefined();
702
808
  }
703
809
 
@@ -709,13 +815,7 @@ window::flash (const Napi::CallbackInfo &info)
709
815
  int window_id = info[0].As<Napi::Number>().Int32Value();
710
816
  int type = info[1].As<Napi::Number>().Int32Value();
711
817
 
712
- SDL_Window *window = SDL_GetWindowFromID(window_id);
713
- if (window == nullptr) {
714
- std::ostringstream message;
715
- message << "SDL_GetWindowFromID(" << window_id << ") error: " << SDL_GetError();
716
- SDL_ClearError();
717
- throw Napi::Error::New(env, message.str());
718
- }
818
+ SDL_Window *window = getWindow(env, window_id);
719
819
 
720
820
  SDL_FlashOperation op;
721
821
  switch (type) {
@@ -724,7 +824,7 @@ window::flash (const Napi::CallbackInfo &info)
724
824
  default: op = SDL_FLASH_CANCEL; break;
725
825
  }
726
826
 
727
- if (SDL_FlashWindow(window, op) < 0) {
827
+ if (!SDL_FlashWindow(window, op)) {
728
828
  std::ostringstream message;
729
829
  message << "SDL_FlashWindow(" << window_id << ", " << type << ") error: " << SDL_GetError();
730
830
  SDL_ClearError();
@@ -741,22 +841,10 @@ window::destroy (const Napi::CallbackInfo &info)
741
841
 
742
842
  int window_id = info[0].As<Napi::Number>().Int32Value();
743
843
 
744
- SDL_Window *window = SDL_GetWindowFromID(window_id);
745
- if (window == nullptr) {
746
- std::ostringstream message;
747
- message << "SDL_GetWindowFromID(" << window_id << ") error: " << SDL_GetError();
748
- SDL_ClearError();
749
- throw Napi::Error::New(env, message.str());
750
- }
844
+ SDL_Window *window = getWindow(env, window_id);
751
845
 
752
846
  SDL_Renderer *renderer = SDL_GetRenderer(window);
753
- if (renderer != nullptr) {
754
- SDL_DestroyRenderer(renderer);
755
-
756
- CachedTexture &cached = cachedTextures[window];
757
- if (cached.texture != nullptr) { SDL_DestroyTexture(cached.texture); }
758
- cachedTextures.erase(window);
759
- }
847
+ if (renderer != nullptr) { SDL_DestroyRenderer(renderer); }
760
848
 
761
849
  SDL_DestroyWindow(window);
762
850