@kmamal/sdl 0.9.4 → 0.9.6-0

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 (55) hide show
  1. package/README.md +34 -15
  2. package/binding.gyp +25 -5
  3. package/package.json +7 -4
  4. package/src/javascript/audio/audio-instance.js +9 -6
  5. package/src/javascript/cleanup.js +1 -1
  6. package/src/javascript/clipboard/index.js +2 -1
  7. package/src/javascript/controller/controller-instance.js +6 -0
  8. package/src/javascript/enums.js +1 -1
  9. package/src/javascript/events/index.js +302 -305
  10. package/src/javascript/index.js +1 -1
  11. package/src/javascript/joystick/joystick-instance.js +7 -0
  12. package/src/javascript/keyboard/index.js +3 -3
  13. package/src/javascript/keyboard/key-mapping.js +14 -6
  14. package/src/javascript/mouse/index.js +13 -12
  15. package/src/javascript/power/index.js +0 -2
  16. package/src/javascript/sensor/sensor-instance.js +1 -5
  17. package/src/javascript/video/window.js +17 -16
  18. package/src/native/audio.cpp +178 -0
  19. package/src/native/audio.h +21 -0
  20. package/src/native/clipboard.cpp +45 -0
  21. package/src/native/clipboard.h +13 -0
  22. package/src/native/cocoa-global.h +8 -0
  23. package/src/native/cocoa-global.mm +8 -0
  24. package/src/native/cocoa-window.h +9 -4
  25. package/src/native/cocoa-window.mm +6 -9
  26. package/src/native/controller.cpp +154 -0
  27. package/src/native/controller.h +17 -0
  28. package/src/native/enums.cpp +477 -0
  29. package/src/native/enums.h +67 -0
  30. package/src/native/events.cpp +339 -0
  31. package/src/native/events.h +15 -0
  32. package/src/native/global.cpp +187 -0
  33. package/src/native/global.h +15 -0
  34. package/src/native/joystick.cpp +388 -0
  35. package/src/native/joystick.h +22 -0
  36. package/src/native/keyboard.cpp +55 -0
  37. package/src/native/keyboard.h +18 -0
  38. package/src/native/module.cpp +98 -0
  39. package/src/native/mouse.cpp +186 -0
  40. package/src/native/mouse.h +21 -0
  41. package/src/native/power.cpp +18 -0
  42. package/src/native/power.h +12 -0
  43. package/src/native/sensor.cpp +117 -0
  44. package/src/native/sensor.h +15 -0
  45. package/src/native/video.cpp +90 -0
  46. package/src/native/video.h +12 -0
  47. package/src/native/window.cpp +755 -0
  48. package/src/native/window.h +29 -0
  49. package/src/types/index.d.ts +7 -4
  50. package/scripts/test.mjs +0 -12
  51. package/src/native/asprintf.c +0 -47
  52. package/src/native/asprintf.h +0 -37
  53. package/src/native/binding.cpp +0 -1680
  54. package/src/native/sdl-helpers.cpp +0 -2619
  55. package/src/native/sdl-helpers.h +0 -182
@@ -0,0 +1,755 @@
1
+ #include "window.h"
2
+ #include <SDL.h>
3
+ #include <SDL_syswm.h>
4
+ #include <string>
5
+ #include <sstream>
6
+
7
+
8
+ #if defined(__LINUX__)
9
+ #define GL_NativeWindow Window
10
+ struct GPU_NativeData {
11
+ Display *display;
12
+ Window window;
13
+ };
14
+ #define GPU_WINDOW_FLAG SDL_WINDOW_VULKAN
15
+ #elif defined(__WIN32__)
16
+ #define GL_NativeWindow HWND
17
+ struct GPU_NativeData {
18
+ HWND hwnd;
19
+ HINSTANCE hinstance;
20
+ };
21
+ #define GPU_WINDOW_FLAG 0
22
+ #elif defined(__MACOSX__)
23
+ #include "cocoa-window.h"
24
+ #define GL_NativeWindow CALayer *
25
+ struct GPU_NativeData {
26
+ CALayer *layer;
27
+ };
28
+ #define GPU_WINDOW_FLAG SDL_WINDOW_METAL
29
+ #endif
30
+
31
+
32
+ void
33
+ updateTexture(
34
+ Napi::Env &env,
35
+ SDL_Window *window,
36
+ SDL_Renderer *renderer,
37
+ int pixel_width, int pixel_height,
38
+ SDL_Texture **texture_dst
39
+ ) {
40
+ int window_id = SDL_GetWindowID(window);
41
+ // Not likely to fail.
42
+
43
+ int format = SDL_GetWindowPixelFormat(window);
44
+ if (format == SDL_PIXELFORMAT_UNKNOWN) {
45
+ std::ostringstream message;
46
+ message << "SDL_GetWindowPixelFormat(" << window_id << ") error: " << SDL_GetError();
47
+ SDL_ClearError();
48
+ throw Napi::Error::New(env, message.str());
49
+ }
50
+
51
+ SDL_Texture *texture = SDL_CreateTexture(renderer, format, SDL_TEXTUREACCESS_STREAMING, pixel_width, pixel_height);
52
+ if (texture == nullptr) {
53
+ std::ostringstream message;
54
+ message << "SDL_CreateTexture(" << window_id << ") error: " << SDL_GetError();
55
+ SDL_ClearError();
56
+ throw Napi::Error::New(env, message.str());
57
+ }
58
+
59
+ if (SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_NONE) < 0) {
60
+ std::ostringstream message;
61
+ message << "SDL_SetTextureBlendMode(" << window_id << ") error: " << SDL_GetError();
62
+ SDL_ClearError();
63
+ throw Napi::Error::New(env, message.str());
64
+ }
65
+
66
+ SDL_Texture *old_texture = (SDL_Texture *) SDL_SetWindowData(window, "texture", texture);
67
+ if (old_texture != nullptr) { SDL_DestroyTexture(old_texture); }
68
+
69
+ if (texture_dst != nullptr) { *texture_dst = texture; }
70
+ }
71
+
72
+ void
73
+ updateRenderer(
74
+ Napi::Env &env,
75
+ SDL_Window *window,
76
+ bool *is_accelerated,
77
+ bool *is_vsync
78
+ ) {
79
+ int window_id = SDL_GetWindowID(window);
80
+ // Not likely to fail.
81
+
82
+ SDL_Renderer *old_renderer = SDL_GetRenderer(window);
83
+ if (old_renderer != nullptr) {
84
+ SDL_DestroyRenderer(old_renderer);
85
+ SDL_SetWindowData(window, "texture", nullptr);
86
+ }
87
+
88
+ int renderer_flags = 0
89
+ | (*is_accelerated ? SDL_RENDERER_ACCELERATED : SDL_RENDERER_SOFTWARE)
90
+ | (*is_accelerated && *is_vsync ? SDL_RENDERER_PRESENTVSYNC : 0);
91
+
92
+ SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, renderer_flags);
93
+ if (renderer == nullptr) {
94
+ std::ostringstream message;
95
+ message << "SDL_CreateRenderer(" << window_id << ", " << is_accelerated << ", " << is_vsync << ") error: " << SDL_GetError();
96
+ SDL_ClearError();
97
+ throw Napi::Error::New(env, message.str());
98
+ }
99
+
100
+ SDL_RendererInfo info;
101
+ if (SDL_GetRendererInfo(renderer, &info) < 0) {
102
+ std::ostringstream message;
103
+ message << "SDL_GetRendererInfo(" << window_id << ", " << is_accelerated << ", " << is_vsync << ") error: " << SDL_GetError();
104
+ SDL_ClearError();
105
+ throw Napi::Error::New(env, message.str());
106
+ }
107
+
108
+ *is_accelerated = info.flags & SDL_RENDERER_ACCELERATED;
109
+ *is_vsync = info.flags & SDL_RENDERER_PRESENTVSYNC;
110
+
111
+ int pixel_width, pixel_height;
112
+ SDL_GetWindowSizeInPixels(window, &pixel_width, &pixel_height);
113
+
114
+ updateTexture(env, window, renderer, pixel_width, pixel_height, nullptr);
115
+ }
116
+
117
+
118
+ Napi::Value
119
+ window::create (const Napi::CallbackInfo &info)
120
+ {
121
+ Napi::Env env = info.Env();
122
+
123
+ std:: string title = info[0].As<Napi::String>().Utf8Value();
124
+ int display = info[1].As<Napi::Number>().Int32Value();
125
+ int x = info[2].IsNull() ? SDL_WINDOWPOS_CENTERED_DISPLAY(display) : info[2].As<Napi::Number>().Int32Value();
126
+ int y = info[3].IsNull() ? SDL_WINDOWPOS_CENTERED_DISPLAY(display) : info[3].As<Napi::Number>().Int32Value();
127
+ int width = info[4].IsNull() ? 640 : info[4].As<Napi::Number>().Int32Value();
128
+ int height = info[5].IsNull() ? 480 : info[5].As<Napi::Number>().Int32Value();
129
+ bool is_visible = info[6].As<Napi::Boolean>().Value();
130
+ bool is_fullscreen = info[7].As<Napi::Boolean>().Value();
131
+ bool is_resizable = info[8].As<Napi::Boolean>().Value();
132
+ bool is_borderless = info[9].As<Napi::Boolean>().Value();
133
+ bool is_always_on_top = info[10].As<Napi::Boolean>().Value();
134
+ bool is_accelerated = info[11].As<Napi::Boolean>().Value();
135
+ bool is_vsync = info[12].As<Napi::Boolean>().Value();
136
+ bool is_opengl = info[13].As<Napi::Boolean>().Value();
137
+ bool is_webgpu = info[14].As<Napi::Boolean>().Value();
138
+ bool should_skip_taskbar = info[15].As<Napi::Boolean>().Value();
139
+ bool is_popup_menu = info[16].As<Napi::Boolean>().Value();
140
+ bool is_tooltip = info[17].As<Napi::Boolean>().Value();
141
+ bool is_utility = info[18].As<Napi::Boolean>().Value();
142
+
143
+ int desired_flags = 0
144
+ | SDL_WINDOW_HIDDEN | SDL_WINDOW_ALLOW_HIGHDPI
145
+ | (is_fullscreen ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0)
146
+ | (is_resizable ? SDL_WINDOW_RESIZABLE : 0)
147
+ | (is_borderless ? SDL_WINDOW_BORDERLESS : 0)
148
+ | (is_always_on_top ? SDL_WINDOW_ALWAYS_ON_TOP : 0)
149
+ | (is_opengl ? SDL_WINDOW_OPENGL : 0)
150
+ | (is_webgpu ? GPU_WINDOW_FLAG : 0)
151
+ | (should_skip_taskbar ? SDL_WINDOW_SKIP_TASKBAR : 0)
152
+ | (is_popup_menu ? SDL_WINDOW_POPUP_MENU : 0)
153
+ | (is_tooltip ? SDL_WINDOW_TOOLTIP : 0)
154
+ | (is_utility ? SDL_WINDOW_UTILITY : 0)
155
+ ;
156
+
157
+ SDL_Window *window = SDL_CreateWindow(title.c_str(), x, y, width, height, desired_flags);
158
+ if (window == nullptr) {
159
+ std::ostringstream message;
160
+ message << "SDL_CreateWindow() error: " << SDL_GetError();
161
+ SDL_ClearError();
162
+ throw Napi::Error::New(env, message.str());
163
+ }
164
+
165
+ int window_id = SDL_GetWindowID(window);
166
+ if (window_id == 0) {
167
+ std::ostringstream message;
168
+ message << "SDL_GetWindowID() error: " << SDL_GetError();
169
+ SDL_ClearError();
170
+ throw Napi::Error::New(env, message.str());
171
+ }
172
+
173
+ int actual_flags = SDL_GetWindowFlags(window);
174
+ is_fullscreen = actual_flags & (SDL_WINDOW_FULLSCREEN | SDL_WINDOW_FULLSCREEN_DESKTOP);
175
+ is_resizable = actual_flags & SDL_WINDOW_RESIZABLE;
176
+ is_borderless = actual_flags & SDL_WINDOW_BORDERLESS;
177
+ is_always_on_top = actual_flags & SDL_WINDOW_ALWAYS_ON_TOP;
178
+ should_skip_taskbar = actual_flags & SDL_WINDOW_SKIP_TASKBAR;
179
+ is_popup_menu = actual_flags & SDL_WINDOW_POPUP_MENU;
180
+ is_tooltip = actual_flags & SDL_WINDOW_TOOLTIP;
181
+ is_utility = actual_flags & SDL_WINDOW_UTILITY;
182
+
183
+ SDL_GetWindowPosition(window, &x, &y);
184
+
185
+ int pixel_width, pixel_height;
186
+ SDL_GetWindowSizeInPixels(window, &pixel_width, &pixel_height);
187
+
188
+ Napi::Value native;
189
+
190
+ if (is_opengl || is_webgpu) {
191
+ SDL_SysWMinfo info;
192
+ SDL_VERSION(&(info.version));
193
+ bool got_info = SDL_GetWindowWMInfo(window, &info);
194
+ if (got_info == SDL_FALSE) {
195
+ std::ostringstream message;
196
+ message << "SDL_GetWindowWMInfo(" << window_id << ") error: " << SDL_GetError();
197
+ SDL_ClearError();
198
+ throw Napi::Error::New(env, message.str());
199
+ }
200
+
201
+ if (is_opengl) {
202
+ int native_size = sizeof(GL_NativeWindow);
203
+ Napi::Buffer<char> native_buffer = Napi::Buffer<char>::New(env, native_size);
204
+ GL_NativeWindow *native_window = (GL_NativeWindow *) native_buffer.Data();
205
+ native = native_buffer;
206
+
207
+ #if defined(__LINUX__)
208
+ *native_window = info.info.x11.window;
209
+ #elif defined(__WIN32__)
210
+ *native_window = info.info.win.window;
211
+ #elif defined(__MACOSX__)
212
+ *native_window = getCocoaGlView(info.info.cocoa.window);
213
+ #endif
214
+ } else if (is_webgpu) {
215
+ int native_size = sizeof(GPU_NativeData);
216
+ Napi::Buffer<char> native_buffer = Napi::Buffer<char>::New(env, native_size);
217
+ GPU_NativeData *native_data = (GPU_NativeData *) native_buffer.Data();
218
+ native = native_buffer;
219
+
220
+ #if defined(__LINUX__)
221
+ native_data->display = info.info.x11.display;
222
+ native_data->window = info.info.x11.window;
223
+ #elif defined(__WIN32__)
224
+ native_data->hwnd = info.info.win.window;
225
+ native_data->hinstance = info.info.win.hinstance;
226
+ #elif defined(__MACOSX__)
227
+ native_data->layer = getCocoaGpuView(info.info.cocoa.window);
228
+ #endif
229
+ }
230
+ }
231
+ else {
232
+ updateRenderer(env, window, &is_accelerated, &is_vsync);
233
+ native = env.Null();
234
+ }
235
+
236
+ if (is_visible) { SDL_ShowWindow(window); }
237
+
238
+ Napi::Object result = Napi::Object::New(env);
239
+ result.Set("id", Napi::Number::New(env, window_id));
240
+ result.Set("x", Napi::Number::New(env, x));
241
+ result.Set("y", Napi::Number::New(env, y));
242
+ result.Set("width", Napi::Number::New(env, width));
243
+ result.Set("height", Napi::Number::New(env, height));
244
+ result.Set("pixelWidth", Napi::Number::New(env, pixel_width));
245
+ result.Set("pixelHeight", Napi::Number::New(env, pixel_height));
246
+ result.Set("fullscreen", Napi::Boolean::New(env, is_fullscreen));
247
+ result.Set("resizable", Napi::Boolean::New(env, is_resizable));
248
+ result.Set("borderless", Napi::Boolean::New(env, is_borderless));
249
+ result.Set("alwaysOnTop", Napi::Boolean::New(env, is_always_on_top));
250
+ result.Set("accelerated", Napi::Boolean::New(env, is_accelerated));
251
+ result.Set("vsync", Napi::Boolean::New(env, is_vsync));
252
+ result.Set("native", native);
253
+ result.Set("skipTaskbar", Napi::Boolean::New(env, should_skip_taskbar));
254
+ result.Set("popupMenu", Napi::Boolean::New(env, is_popup_menu));
255
+ result.Set("tooltip", Napi::Boolean::New(env, is_tooltip));
256
+ result.Set("utility", Napi::Boolean::New(env, is_utility));
257
+
258
+ return result;
259
+ }
260
+
261
+ Napi::Value
262
+ window::setTitle (const Napi::CallbackInfo &info)
263
+ {
264
+ Napi::Env env = info.Env();
265
+
266
+ int window_id = info[0].As<Napi::Number>().Int32Value();
267
+ std:: string title = info[1].As<Napi::String>().Utf8Value();
268
+
269
+ SDL_Window *window = SDL_GetWindowFromID(window_id);
270
+ if (window == nullptr) {
271
+ std::ostringstream message;
272
+ message << "SDL_GetWindowFromID(" << window_id << ") error: " << SDL_GetError();
273
+ SDL_ClearError();
274
+ throw Napi::Error::New(env, message.str());
275
+ }
276
+
277
+ SDL_SetWindowTitle(window, title.c_str());
278
+
279
+ return env.Undefined();
280
+ }
281
+
282
+ Napi::Value
283
+ window::setPosition (const Napi::CallbackInfo &info)
284
+ {
285
+ Napi::Env env = info.Env();
286
+
287
+ int window_id = info[0].As<Napi::Number>().Int32Value();
288
+ int x = info[1].As<Napi::Number>().Int32Value();
289
+ int y = info[2].As<Napi::Number>().Int32Value();
290
+
291
+ SDL_Window *window = SDL_GetWindowFromID(window_id);
292
+ if (window == nullptr) {
293
+ std::ostringstream message;
294
+ message << "SDL_GetWindowFromID(" << window_id << ") error: " << SDL_GetError();
295
+ SDL_ClearError();
296
+ throw Napi::Error::New(env, message.str());
297
+ }
298
+
299
+ SDL_SetWindowPosition(window, x, y);
300
+
301
+ return env.Undefined();
302
+ }
303
+
304
+ Napi::Value
305
+ window::setSize (const Napi::CallbackInfo &info)
306
+ {
307
+ Napi::Env env = info.Env();
308
+
309
+ int window_id = info[0].As<Napi::Number>().Int32Value();
310
+ int width = info[1].As<Napi::Number>().Int32Value();
311
+ int height = info[2].As<Napi::Number>().Int32Value();
312
+
313
+ SDL_Window *window = SDL_GetWindowFromID(window_id);
314
+ if (window == nullptr) {
315
+ std::ostringstream message;
316
+ message << "SDL_GetWindowFromID(" << window_id << ") error: " << SDL_GetError();
317
+ SDL_ClearError();
318
+ throw Napi::Error::New(env, message.str());
319
+ }
320
+
321
+ SDL_SetWindowSize(window, width, height);
322
+
323
+ int pixel_width, pixel_height;
324
+ SDL_GetWindowSizeInPixels(window, &pixel_width, &pixel_height);
325
+
326
+ Napi::Object result = Napi::Object::New(env);
327
+ result.Set("pixelWidth", Napi::Number::New(env, pixel_width));
328
+ result.Set("pixelHeight", Napi::Number::New(env, pixel_height));
329
+
330
+ return result;
331
+ }
332
+
333
+ Napi::Value
334
+ window::setFullscreen (const Napi::CallbackInfo &info)
335
+ {
336
+ Napi::Env env = info.Env();
337
+
338
+ int window_id = info[0].As<Napi::Number>().Int32Value();
339
+ int is_fullscreen = info[1].As<Napi::Boolean>().Value();
340
+
341
+ SDL_Window *window = SDL_GetWindowFromID(window_id);
342
+ if (window == nullptr) {
343
+ std::ostringstream message;
344
+ message << "SDL_GetWindowFromID(" << window_id << ") error: " << SDL_GetError();
345
+ SDL_ClearError();
346
+ throw Napi::Error::New(env, message.str());
347
+ }
348
+
349
+ if (SDL_SetWindowFullscreen(window, is_fullscreen ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0) < 0) {
350
+ std::ostringstream message;
351
+ message << "SDL_SetWindowFullscreen(" << window_id << ", " << is_fullscreen << ") error: " << SDL_GetError();
352
+ SDL_ClearError();
353
+ throw Napi::Error::New(env, message.str());
354
+ }
355
+
356
+ int actual_flags = SDL_GetWindowFlags(window);
357
+ is_fullscreen = actual_flags & (SDL_WINDOW_FULLSCREEN | SDL_WINDOW_FULLSCREEN_DESKTOP);
358
+
359
+ return Napi::Boolean::New(env, is_fullscreen);
360
+ }
361
+
362
+ Napi::Value
363
+ window::setResizable (const Napi::CallbackInfo &info)
364
+ {
365
+ Napi::Env env = info.Env();
366
+
367
+ int window_id = info[0].As<Napi::Number>().Int32Value();
368
+ bool is_resizable = info[1].As<Napi::Boolean>().Value();
369
+
370
+ SDL_Window *window = SDL_GetWindowFromID(window_id);
371
+ if (window == nullptr) {
372
+ std::ostringstream message;
373
+ message << "SDL_GetWindowFromID(" << window_id << ") error: " << SDL_GetError();
374
+ SDL_ClearError();
375
+ throw Napi::Error::New(env, message.str());
376
+ }
377
+
378
+ SDL_SetWindowResizable(window, is_resizable ? SDL_TRUE : SDL_FALSE);
379
+
380
+ int actual_flags = SDL_GetWindowFlags(window);
381
+ is_resizable = actual_flags & SDL_WINDOW_RESIZABLE;
382
+
383
+ return Napi::Boolean::New(env, is_resizable);
384
+ }
385
+
386
+ Napi::Value
387
+ window::setBorderless (const Napi::CallbackInfo &info)
388
+ {
389
+ Napi::Env env = info.Env();
390
+
391
+ int window_id = info[0].As<Napi::Number>().Int32Value();
392
+ bool is_borderless = info[1].As<Napi::Boolean>().Value();
393
+
394
+ SDL_Window *window = SDL_GetWindowFromID(window_id);
395
+ if (window == nullptr) {
396
+ std::ostringstream message;
397
+ message << "SDL_GetWindowFromID(" << window_id << ") error: " << SDL_GetError();
398
+ SDL_ClearError();
399
+ throw Napi::Error::New(env, message.str());
400
+ }
401
+
402
+ SDL_SetWindowBordered(window, is_borderless ? SDL_FALSE : SDL_TRUE);
403
+
404
+ int actual_flags = SDL_GetWindowFlags(window);
405
+ is_borderless = actual_flags & SDL_WINDOW_BORDERLESS;
406
+
407
+ return Napi::Boolean::New(env, is_borderless);
408
+ }
409
+
410
+ Napi::Value
411
+ window::setAcceleratedAndVsync (const Napi::CallbackInfo &info)
412
+ {
413
+ Napi::Env env = info.Env();
414
+
415
+ int window_id = info[0].As<Napi::Number>().Int32Value();
416
+ bool is_accelerated = info[1].As<Napi::Boolean>().Value();
417
+ bool is_vsync = info[2].As<Napi::Boolean>().Value();
418
+
419
+ SDL_Window *window = SDL_GetWindowFromID(window_id);
420
+ if (window == nullptr) {
421
+ std::ostringstream message;
422
+ message << "SDL_GetWindowFromID(" << window_id << ") error: " << SDL_GetError();
423
+ SDL_ClearError();
424
+ throw Napi::Error::New(env, message.str());
425
+ }
426
+
427
+ updateRenderer(env, window, &is_accelerated, &is_vsync);
428
+
429
+ Napi::Object result = Napi::Object::New(env);
430
+ result.Set("accelerated", Napi::Boolean::New(env, is_accelerated));
431
+ result.Set("vsync", Napi::Boolean::New(env, is_vsync));
432
+
433
+ return result;
434
+ }
435
+
436
+ Napi::Value
437
+ window::focus (const Napi::CallbackInfo &info)
438
+ {
439
+ Napi::Env env = info.Env();
440
+
441
+ int window_id = info[0].As<Napi::Number>().Int32Value();
442
+
443
+ SDL_Window *window = SDL_GetWindowFromID(window_id);
444
+ if (window == nullptr) {
445
+ std::ostringstream message;
446
+ message << "SDL_GetWindowFromID(" << window_id << ") error: " << SDL_GetError();
447
+ SDL_ClearError();
448
+ throw Napi::Error::New(env, message.str());
449
+ }
450
+
451
+ SDL_RaiseWindow(window);
452
+
453
+ return env.Undefined();
454
+ }
455
+
456
+ Napi::Value
457
+ window::show (const Napi::CallbackInfo &info)
458
+ {
459
+ Napi::Env env = info.Env();
460
+
461
+ int window_id = info[0].As<Napi::Number>().Int32Value();
462
+
463
+ SDL_Window *window = SDL_GetWindowFromID(window_id);
464
+ if (window == nullptr) {
465
+ std::ostringstream message;
466
+ message << "SDL_GetWindowFromID(" << window_id << ") error: " << SDL_GetError();
467
+ SDL_ClearError();
468
+ throw Napi::Error::New(env, message.str());
469
+ }
470
+
471
+ SDL_ShowWindow(window);
472
+
473
+ return env.Undefined();
474
+ }
475
+
476
+ Napi::Value
477
+ window::hide (const Napi::CallbackInfo &info)
478
+ {
479
+ Napi::Env env = info.Env();
480
+
481
+ int window_id = info[0].As<Napi::Number>().Int32Value();
482
+
483
+ SDL_Window *window = SDL_GetWindowFromID(window_id);
484
+ if (window == nullptr) {
485
+ std::ostringstream message;
486
+ message << "SDL_GetWindowFromID(" << window_id << ") error: " << SDL_GetError();
487
+ SDL_ClearError();
488
+ throw Napi::Error::New(env, message.str());
489
+ }
490
+
491
+ SDL_HideWindow(window);
492
+
493
+ return env.Undefined();
494
+ }
495
+
496
+ Napi::Value
497
+ window::maximize (const Napi::CallbackInfo &info)
498
+ {
499
+ Napi::Env env = info.Env();
500
+
501
+ int window_id = info[0].As<Napi::Number>().Int32Value();
502
+
503
+ SDL_Window *window = SDL_GetWindowFromID(window_id);
504
+ if (window == nullptr) {
505
+ std::ostringstream message;
506
+ message << "SDL_GetWindowFromID(" << window_id << ") error: " << SDL_GetError();
507
+ SDL_ClearError();
508
+ throw Napi::Error::New(env, message.str());
509
+ }
510
+
511
+ SDL_MaximizeWindow(window);
512
+
513
+ return env.Undefined();
514
+ }
515
+
516
+ Napi::Value
517
+ window::minimize (const Napi::CallbackInfo &info)
518
+ {
519
+ Napi::Env env = info.Env();
520
+
521
+ int window_id = info[0].As<Napi::Number>().Int32Value();
522
+
523
+ SDL_Window *window = SDL_GetWindowFromID(window_id);
524
+ if (window == nullptr) {
525
+ std::ostringstream message;
526
+ message << "SDL_GetWindowFromID(" << window_id << ") error: " << SDL_GetError();
527
+ SDL_ClearError();
528
+ throw Napi::Error::New(env, message.str());
529
+ }
530
+
531
+ SDL_MinimizeWindow(window);
532
+
533
+ return env.Undefined();
534
+ }
535
+
536
+ Napi::Value
537
+ window::restore (const Napi::CallbackInfo &info)
538
+ {
539
+ Napi::Env env = info.Env();
540
+
541
+ int window_id = info[0].As<Napi::Number>().Int32Value();
542
+
543
+ SDL_Window *window = SDL_GetWindowFromID(window_id);
544
+ if (window == nullptr) {
545
+ std::ostringstream message;
546
+ message << "SDL_GetWindowFromID(" << window_id << ") error: " << SDL_GetError();
547
+ SDL_ClearError();
548
+ throw Napi::Error::New(env, message.str());
549
+ }
550
+
551
+ SDL_RestoreWindow(window);
552
+
553
+ return env.Undefined();
554
+ }
555
+
556
+ Napi::Value
557
+ window::render (const Napi::CallbackInfo &info)
558
+ {
559
+ Napi::Env env = info.Env();
560
+
561
+ int window_id = info[0].As<Napi::Number>().Int32Value();
562
+ int w = info[1].As<Napi::Number>().Int32Value();
563
+ int h = info[2].As<Napi::Number>().Int32Value();
564
+ int stride = info[3].As<Napi::Number>().Int32Value();
565
+ unsigned int format = info[4].As<Napi::Number>().Int32Value();
566
+ void *pixels = info[5].As<Napi::Buffer<char>>().Data();
567
+
568
+ SDL_Window *window = SDL_GetWindowFromID(window_id);
569
+ if (window == nullptr) {
570
+ std::ostringstream message;
571
+ message << "SDL_GetWindowFromID(" << window_id << ") error: " << SDL_GetError();
572
+ SDL_ClearError();
573
+ throw Napi::Error::New(env, message.str());
574
+ }
575
+
576
+ SDL_Renderer *renderer = SDL_GetRenderer(window);
577
+ if (renderer == nullptr) {
578
+ std::ostringstream message;
579
+ message << "SDL_GetRenderer(" << window_id << ") error: " << SDL_GetError();
580
+ SDL_ClearError();
581
+ throw Napi::Error::New(env, message.str());
582
+ }
583
+
584
+ SDL_Texture *texture = (SDL_Texture *) SDL_GetWindowData(window, "texture");
585
+ if (texture == nullptr) {
586
+ std::ostringstream message;
587
+ message << "SDL_GetWindowData(" << window_id << ") error: " << SDL_GetError();
588
+ SDL_ClearError();
589
+ throw Napi::Error::New(env, message.str());
590
+ }
591
+
592
+ unsigned int texture_format;
593
+ int texture_width, texture_height;
594
+ if (SDL_QueryTexture(texture, &texture_format, nullptr, &texture_width, &texture_height) < 0) {
595
+ std::ostringstream message;
596
+ message << "SDL_QueryTexture(" << window_id << ") error: " << SDL_GetError();
597
+ SDL_ClearError();
598
+ throw Napi::Error::New(env, message.str());
599
+ }
600
+
601
+ int pixel_width, pixel_height;
602
+ SDL_GetWindowSizeInPixels(window, &pixel_width, &pixel_height);
603
+
604
+ if (pixel_width != texture_width || pixel_height != texture_height) {
605
+ updateTexture(env, window, renderer, pixel_width, pixel_height, &texture);
606
+ texture_width = pixel_width;
607
+ texture_height = pixel_height;
608
+ }
609
+
610
+ SDL_Surface *src_surface = SDL_CreateRGBSurfaceWithFormatFrom(pixels, w, h, SDL_BITSPERPIXEL(format), stride, format);
611
+ if (src_surface == nullptr) {
612
+ std::ostringstream message;
613
+ message << "SDL_CreateRGBSurfaceWithFormatFrom(" << window_id << ", " << w << ", " << h << ", " << stride << ", " << format << ") error: " << SDL_GetError();
614
+ SDL_ClearError();
615
+ throw Napi::Error::New(env, message.str());
616
+ }
617
+
618
+ if (SDL_SetSurfaceBlendMode(src_surface, SDL_BLENDMODE_NONE) < 0) {
619
+ std::ostringstream message;
620
+ message << "SDL_SetSurfaceBlendMode(" << window_id << ") error: " << SDL_GetError();
621
+ SDL_ClearError();
622
+ throw Napi::Error::New(env, message.str());
623
+ }
624
+
625
+ void *texture_pixels;
626
+ int texture_stride;
627
+ if (SDL_LockTexture(texture, nullptr, &texture_pixels, &texture_stride) < 0) {
628
+ std::ostringstream message;
629
+ message << "SDL_LockTexture(" << window_id << ", " << texture_width << ", " << texture_height << ", " << texture_stride << ", " << texture_format << ") error: " << SDL_GetError();
630
+ SDL_ClearError();
631
+ throw Napi::Error::New(env, message.str());
632
+ }
633
+
634
+ SDL_Surface *dst_surface = SDL_CreateRGBSurfaceWithFormatFrom(texture_pixels, texture_width, texture_height, SDL_BITSPERPIXEL(texture_format), texture_stride, texture_format);
635
+ if (dst_surface == nullptr) {
636
+ std::ostringstream message;
637
+ message << "SDL_CreateRGBSurfaceWithFormatFrom(" << window_id << ", " << texture_width << ", " << texture_height << ", " << texture_stride << ", " << texture_format << ") error: " << SDL_GetError();
638
+ SDL_ClearError();
639
+ throw Napi::Error::New(env, message.str());
640
+ }
641
+
642
+ if (SDL_BlitScaled(src_surface, NULL, dst_surface, NULL) < 0) {
643
+ std::ostringstream message;
644
+ message << "SDL_BlitSurface(" << window_id << ") error: " << SDL_GetError();
645
+ SDL_ClearError();
646
+ throw Napi::Error::New(env, message.str());
647
+ }
648
+
649
+ SDL_UnlockTexture(texture);
650
+
651
+ if (SDL_RenderCopy(renderer, texture, nullptr, nullptr) < 0) {
652
+ std::ostringstream message;
653
+ message << "SDL_RenderCopy(" << window_id << ") error: " << SDL_GetError();
654
+ SDL_ClearError();
655
+ throw Napi::Error::New(env, message.str());
656
+ }
657
+
658
+ SDL_RenderPresent(renderer);
659
+
660
+ SDL_FreeSurface(src_surface);
661
+ SDL_FreeSurface(dst_surface);
662
+
663
+ return env.Undefined();
664
+ }
665
+
666
+ Napi::Value
667
+ window::setIcon (const Napi::CallbackInfo &info)
668
+ {
669
+ Napi::Env env = info.Env();
670
+
671
+ int window_id = info[0].As<Napi::Number>().Int32Value();
672
+ int w = info[1].As<Napi::Number>().Int32Value();
673
+ int h = info[2].As<Napi::Number>().Int32Value();
674
+ int stride = info[3].As<Napi::Number>().Int32Value();
675
+ unsigned int format = info[4].As<Napi::Number>().Int32Value();
676
+ void *pixels = info[5].As<Napi::Buffer<char>>().Data();
677
+
678
+ SDL_Window *window = SDL_GetWindowFromID(window_id);
679
+ if (window == nullptr) {
680
+ std::ostringstream message;
681
+ message << "SDL_GetWindowFromID(" << window_id << ") error: " << SDL_GetError();
682
+ SDL_ClearError();
683
+ throw Napi::Error::New(env, message.str());
684
+ }
685
+
686
+ SDL_Surface* surface = SDL_CreateRGBSurfaceWithFormatFrom(pixels, w, h, SDL_BITSPERPIXEL(format), stride, format);
687
+ if (surface == nullptr) {
688
+ std::ostringstream message;
689
+ message << "SDL_CreateRGBSurfaceWithFormatFrom(" << window_id << ", " << w << ", " << h << ", " << format << ") error: " << SDL_GetError();
690
+ SDL_ClearError();
691
+ throw Napi::Error::New(env, message.str());
692
+ }
693
+
694
+ SDL_SetWindowIcon(window, surface);
695
+
696
+ SDL_FreeSurface(surface);
697
+
698
+ return env.Undefined();
699
+ }
700
+
701
+ Napi::Value
702
+ window::flash (const Napi::CallbackInfo &info)
703
+ {
704
+ Napi::Env env = info.Env();
705
+
706
+ int window_id = info[0].As<Napi::Number>().Int32Value();
707
+ int type = info[1].As<Napi::Number>().Int32Value();
708
+
709
+ SDL_Window *window = SDL_GetWindowFromID(window_id);
710
+ if (window == nullptr) {
711
+ std::ostringstream message;
712
+ message << "SDL_GetWindowFromID(" << window_id << ") error: " << SDL_GetError();
713
+ SDL_ClearError();
714
+ throw Napi::Error::New(env, message.str());
715
+ }
716
+
717
+ SDL_FlashOperation op;
718
+ switch (type) {
719
+ case 2: op = SDL_FLASH_UNTIL_FOCUSED; break;
720
+ case 1: op = SDL_FLASH_BRIEFLY; break;
721
+ default: op = SDL_FLASH_CANCEL; break;
722
+ }
723
+
724
+ if (SDL_FlashWindow(window, op) < 0) {
725
+ std::ostringstream message;
726
+ message << "SDL_FlashWindow(" << window_id << ", " << type << ") error: " << SDL_GetError();
727
+ SDL_ClearError();
728
+ throw Napi::Error::New(env, message.str());
729
+ }
730
+
731
+ return env.Undefined();
732
+ }
733
+
734
+ Napi::Value
735
+ window::destroy (const Napi::CallbackInfo &info)
736
+ {
737
+ Napi::Env env = info.Env();
738
+
739
+ int window_id = info[0].As<Napi::Number>().Int32Value();
740
+
741
+ SDL_Window *window = SDL_GetWindowFromID(window_id);
742
+ if (window == nullptr) {
743
+ std::ostringstream message;
744
+ message << "SDL_GetWindowFromID(" << window_id << ") error: " << SDL_GetError();
745
+ SDL_ClearError();
746
+ throw Napi::Error::New(env, message.str());
747
+ }
748
+
749
+ SDL_Renderer *renderer = SDL_GetRenderer(window);
750
+ if (renderer != nullptr) { SDL_DestroyRenderer(renderer); }
751
+
752
+ SDL_DestroyWindow(window);
753
+
754
+ return env.Undefined();
755
+ }