@kmamal/sdl 0.7.7 → 0.8.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.
package/README.md CHANGED
@@ -2,7 +2,6 @@
2
2
 
3
3
  [![Package](https://img.shields.io/npm/v/%2540kmamal%252Fsdl)](https://www.npmjs.com/package/@kmamal/sdl)
4
4
  [![Dependencies](https://img.shields.io/librariesio/release/npm/@kmamal/sdl)](https://libraries.io/npm/@kmamal%2Fsdl)
5
- [![Vulnerabilities](https://img.shields.io/snyk/vulnerabilities/npm/%2540kmamal%252Fsdl)](https://snyk.io/test/npm/@kmamal/sdl)
6
5
  [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
7
6
 
8
7
  SDL bindings for Node.js.
@@ -50,7 +49,7 @@ import sdl from '@kmamal/sdl'
50
49
  import { createCanvas } from 'canvas'
51
50
 
52
51
  const window = sdl.video.createWindow({ title: "Canvas" })
53
- const { width, height } = window
52
+ const { pixelWidth: width, pixelHeight: height } = window
54
53
  const canvas = createCanvas(width, height)
55
54
  const ctx = canvas.getContext('2d')
56
55
 
@@ -67,7 +66,7 @@ import sdl from '@kmamal/sdl'
67
66
  import createContext from '@kmamal/gl'
68
67
 
69
68
  const window = sdl.video.createWindow({title: "WebGL", opengl: true })
70
- const { width, height, native } = window
69
+ const { pixelWidth: width, pixelHeight: height, native } = window
71
70
  const gl = createContext(width, height, { window: native })
72
71
 
73
72
  // Clear screen to red
@@ -88,6 +87,7 @@ There are more examples [in the `examples/` folder](https://github.com/kmamal/no
88
87
  * [sdl.video](#sdlvideo)
89
88
  * [Image data](#image-data)
90
89
  * [Pixel formats](#pixel-formats)
90
+ * [High-DPI](#highdpi)
91
91
  * [sdl.video.displays](#sdlvideodisplays)
92
92
  * [sdl.video.windows](#sdlvideowindows)
93
93
  * [sdl.video.focused](#sdlvideofocused)
@@ -127,6 +127,8 @@ There are more examples [in the `examples/` folder](https://github.com/kmamal/no
127
127
  * [window.setPosition(x, y)](#windowsetpositionx-y)
128
128
  * [window.width](#windowwidth)
129
129
  * [window.height](#windowheight)
130
+ * [window.pixelWidth](#windowpixelwidth)
131
+ * [window.pixelHeight](#windowpixelheight)
130
132
  * [window.setSize(width, height)](#windowsetsizewidth-height)
131
133
  * [window.visible](#windowvisible)
132
134
  * [window.show([show])](#windowshowshow)
@@ -356,7 +358,7 @@ All three of these functions accept the image as a series of arguments:
356
358
  So for example, to fill the window with a red+green gradient you could do:
357
359
 
358
360
  ```js
359
- const { width, height } = window
361
+ const { pixelWidth: width, pixelHeight: height } = window
360
362
  const stride = width * 4
361
363
  const buffer = Buffer.alloc(stride * height)
362
364
 
@@ -416,6 +418,16 @@ String values used to represent how the pixels of an image are stored in a Buffe
416
418
  | `'nv12'` | `SDL_PIXELFORMAT_NV12` | planar mode: Y + U/V interleaved (2 planes) |
417
419
  | `'nv21'` | `SDL_PIXELFORMAT_NV21` | planar mode: Y + V/U interleaved (2 planes) |
418
420
 
421
+
422
+ ### High-DPI
423
+
424
+ On a high-dpi display, windows have more pixels that their `width` and `height` would indicate.
425
+ On such systems `width` and `height` (and all other measurements such as `x` and `y`) are in "points".
426
+ Points are abstract and don't have to correspond to pixels.
427
+ If you need to know a window's width and height in pixels, you should use the `pixelWidth` and `pixelHeight` properties.
428
+ You should be doing this always, since you don't know beforehand if your program will be running on a high-dpi system.
429
+
430
+
419
431
  ### sdl.video.displays
420
432
 
421
433
  * `<object>[]`
@@ -547,6 +559,8 @@ Fired when the window changes position.
547
559
 
548
560
  * `width: <number>` The window's new width.
549
561
  * `height: <number>` The window's new height.
562
+ * `pixelWidth: <number>` The window's new width in pixels. See [high-dpi](#high-dpi).
563
+ * `pixelHeight: <number>` The window's new height in pixels. See [high-dpi](#high-dpi).
550
564
 
551
565
  Fired when the window changes size.
552
566
 
@@ -720,6 +734,18 @@ The window's width.
720
734
 
721
735
  The window's height.
722
736
 
737
+ ### window.pixelWidth
738
+
739
+ * `<number>`
740
+
741
+ The window's width in pixels. This will be larger than `width` on [high-dpi](#high-dpi) displays.
742
+
743
+ ### window.pixelHeight
744
+
745
+ * `<number>`
746
+
747
+ The window's height in pixels. This will be larger than `height` on [high-dpi](#high-dpi) displays.
748
+
723
749
  ### window.setSize(width, height)
724
750
 
725
751
  * `width: <number>`: The new width.
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "0.7.7",
2
+ "version": "0.8.0",
3
3
  "name": "@kmamal/sdl",
4
4
  "description": "SDL bindings for Node.js",
5
5
  "keywords": [
@@ -27,6 +27,8 @@ class ControllerInstance extends EventsViaPoll {
27
27
 
28
28
  this._device = device
29
29
 
30
+ this._rumbleTimeout = null
31
+ this._rumbleTriggersTimeout = null
30
32
  this._closed = false
31
33
 
32
34
  Globals.controllerInstances.all.add(this)
@@ -83,10 +85,18 @@ class ControllerInstance extends EventsViaPoll {
83
85
  if (highFreqRumble < 0 || highFreqRumble > 1) { throw Object.assign(new Error("highFreqRumble must be between 0 and 1"), { highFreqRumble }) }
84
86
  if (!Number.isFinite(duration)) { throw Object.assign(new Error("duration must be a number"), { duration }) }
85
87
 
88
+ Globals.events.poll()
86
89
  Bindings.joystick_rumble(this._device.id, lowFreqRumble, highFreqRumble, duration)
90
+ clearTimeout(this._rumbleTimeout)
91
+ this._rumbleTimeout = setTimeout(() => { this.stopRumble() }, duration)
87
92
  }
88
93
 
89
- stopRumble () { this.rumble(0, 0) }
94
+ stopRumble () {
95
+ clearTimeout(this._rumbleTimeout)
96
+ this._rumbleTimeout = null
97
+ Bindings.joystick_rumble(this._device.id, 0, 0, 0)
98
+ Globals.events.poll()
99
+ }
90
100
 
91
101
  get hasRumbleTriggers () { return this._hasRumbleTriggers }
92
102
  rumbleTriggers (leftRumble = 1, rightRumble = 1, duration = 1e3) {
@@ -98,10 +108,18 @@ class ControllerInstance extends EventsViaPoll {
98
108
  if (rightRumble < 0 || rightRumble > 1) { throw Object.assign(new Error("rightRumble must be between 0 and 1"), { rightRumble }) }
99
109
  if (!Number.isFinite(duration)) { throw Object.assign(new Error("duration must be a number"), { duration }) }
100
110
 
111
+ Globals.events.poll()
101
112
  Bindings.joystick_rumbleTriggers(this._device.id, leftRumble, rightRumble, duration)
113
+ clearTimeout(this._rumbleTriggersTimeout)
114
+ this._rumbleTriggersTimeout = setTimeout(() => { this.stopRumble() }, duration)
102
115
  }
103
116
 
104
- stopRumbleTriggers () { this.rumbleTriggers(0, 0) }
117
+ stopRumbleTriggers () {
118
+ clearTimeout(this._rumbleTriggersTimeout)
119
+ this._rumbleTriggersTimeout = null
120
+ Bindings.joystick_rumbleTriggers(this._device.id, 0, 0, 0)
121
+ Globals.events.poll()
122
+ }
105
123
 
106
124
  get closed () { return this._closed }
107
125
  close () {
@@ -68,7 +68,7 @@ const reconcileDevices = (emitter, mainList, currList, compare) => {
68
68
  }
69
69
 
70
70
 
71
- const handleEvents = () => {
71
+ const poll = () => {
72
72
  let event
73
73
  while ((event = Bindings.events_poll())) {
74
74
  const { family, type } = event
@@ -92,6 +92,8 @@ const handleEvents = () => {
92
92
  case Enums.eventType.resize: {
93
93
  window._width = event.width
94
94
  window._height = event.height
95
+ window._pixelWidth = event.pixelWidth
96
+ window._pixelHeight = event.pixelHeight
95
97
  } break
96
98
 
97
99
  case Enums.eventType.show: {
@@ -368,7 +370,7 @@ const handleEvents = () => {
368
370
  let eventsInterval = null
369
371
 
370
372
  const startPolling = () => {
371
- eventsInterval = setInterval(handleEvents, 0)
373
+ eventsInterval = setInterval(poll, 0)
372
374
  }
373
375
 
374
376
  const stopPolling = () => {
@@ -379,4 +381,5 @@ const stopPolling = () => {
379
381
  Globals.events = {
380
382
  startPolling,
381
383
  stopPolling,
384
+ poll,
382
385
  }
@@ -21,5 +21,5 @@ module.exports = {
21
21
  byId: new Map(),
22
22
  },
23
23
 
24
- events: null, // Set later to { startPolling, stopPolling }
24
+ events: null, // Set later to { startPolling, stopPolling, poll }
25
25
  }
@@ -30,6 +30,8 @@ class JoystickInstance extends EventsViaPoll {
30
30
 
31
31
  this._device = device
32
32
 
33
+ this._rumbleTimeout = null
34
+ this._rumbleTriggersTimeout = null
33
35
  this._closed = false
34
36
 
35
37
  Globals.joystickInstances.all.add(this)
@@ -88,10 +90,18 @@ class JoystickInstance extends EventsViaPoll {
88
90
  if (highFreqRumble < 0 || highFreqRumble > 1) { throw Object.assign(new Error("highFreqRumble must be between 0 and 1"), { highFreqRumble }) }
89
91
  if (!Number.isFinite(duration)) { throw Object.assign(new Error("duration must be a number"), { duration }) }
90
92
 
93
+ Globals.events.poll()
91
94
  Bindings.joystick_rumble(this._device.id, lowFreqRumble, highFreqRumble, duration)
95
+ clearTimeout(this._rumbleTimeout)
96
+ this._rumbleTimeout = setTimeout(() => { this.stopRumble() }, duration)
92
97
  }
93
98
 
94
- stopRumble () { this.rumble(0, 0) }
99
+ stopRumble () {
100
+ clearTimeout(this._rumbleTimeout)
101
+ this._rumbleTimeout = null
102
+ Bindings.joystick_rumble(this._device.id, 0, 0, 0)
103
+ Globals.events.poll()
104
+ }
95
105
 
96
106
  get hasRumbleTriggers () { return this._hasRumbleTriggers }
97
107
  rumbleTriggers (leftRumble = 1, rightRumble = 1, duration = 1e3) {
@@ -103,10 +113,18 @@ class JoystickInstance extends EventsViaPoll {
103
113
  if (rightRumble < 0 || rightRumble > 1) { throw Object.assign(new Error("rightRumble must be between 0 and 1"), { rightRumble }) }
104
114
  if (!Number.isFinite(duration)) { throw Object.assign(new Error("duration must be a number"), { duration }) }
105
115
 
116
+ Globals.events.poll()
106
117
  Bindings.joystick_rumbleTriggers(this._device.id, leftRumble, rightRumble, duration)
118
+ clearTimeout(this._rumbleTriggersTimeout)
119
+ this._rumbleTriggersTimeout = setTimeout(() => { this.stopRumble() }, duration)
107
120
  }
108
121
 
109
- stopRumbleTriggers () { this.rumbleTriggers(0, 0) }
122
+ stopRumbleTriggers () {
123
+ clearTimeout(this._rumbleTriggersTimeout)
124
+ this._rumbleTriggersTimeout = null
125
+ Bindings.joystick_rumbleTriggers(this._device.id, 0, 0, 0)
126
+ Globals.events.poll()
127
+ }
110
128
 
111
129
  get closed () { return this._closed }
112
130
  close () {
@@ -115,6 +115,8 @@ class Window extends EventsViaPoll {
115
115
  this._y = result.y
116
116
  this._width = result.width
117
117
  this._height = result.height
118
+ this._pixelWidth = result.pixelWidth
119
+ this._pixelHeight = result.pixelHeight
118
120
  this._fullscreen = result.fullscreen
119
121
  this._resizable = result.resizable
120
122
  this._borderless = result.borderless
@@ -157,6 +159,8 @@ class Window extends EventsViaPoll {
157
159
  this.emit('resize', {
158
160
  width: this._width,
159
161
  height: this._height,
162
+ pixelWidth: this._pixelWidth,
163
+ pixelHeight: this._pixelHeight,
160
164
  type: 'resize',
161
165
  })
162
166
  })
@@ -197,11 +201,16 @@ class Window extends EventsViaPoll {
197
201
  if (!Number.isFinite(height)) { throw Object.assign(new Error("height must be a number"), { height }) }
198
202
  if (height <= 0) { throw Object.assign(new Error("invalid height"), { height }) }
199
203
 
200
- Bindings.window_setSize(this._id, width, height)
204
+ const { pixelWidth, pixelHeight } = Bindings.window_setSize(this._id, width, height)
201
205
  this._width = width
202
206
  this._height = height
207
+ this._pixelWidth = pixelWidth
208
+ this._pixelHeight = pixelHeight
203
209
  }
204
210
 
211
+ get pixelWidth () { return this._pixelWidth }
212
+ get pixelHeight () { return this._pixelHeight }
213
+
205
214
  get visible () { return this._visible }
206
215
  show (show = true) {
207
216
  if (this._destroyed) { throw Object.assign(new Error("window is destroyed"), { id: this._id }) }
@@ -341,30 +341,31 @@ window_create(napi_env env, napi_callback_info info)
341
341
  CALL_NAPI(napi_get_value_int32, argv[5], y);
342
342
  }
343
343
 
344
- bool visible, fullscreen, resizable, borderless, alwaysOnTop,
344
+ bool visible, fullscreen, resizable, borderless, always_on_top,
345
345
  accelerated, vsync, opengl,
346
- skipTaskbar, popupMenu, tooltip, utility;
346
+ skip_taskbar, popup_menu, tooltip, utility;
347
347
  CALL_NAPI(napi_get_value_bool, argv[6], &visible);
348
348
  CALL_NAPI(napi_get_value_bool, argv[7], &fullscreen);
349
349
  CALL_NAPI(napi_get_value_bool, argv[8], &resizable);
350
350
  CALL_NAPI(napi_get_value_bool, argv[9], &borderless);
351
- CALL_NAPI(napi_get_value_bool, argv[10], &alwaysOnTop);
351
+ CALL_NAPI(napi_get_value_bool, argv[10], &always_on_top);
352
352
  CALL_NAPI(napi_get_value_bool, argv[11], &accelerated);
353
353
  CALL_NAPI(napi_get_value_bool, argv[12], &vsync);
354
354
  CALL_NAPI(napi_get_value_bool, argv[13], &opengl);
355
- CALL_NAPI(napi_get_value_bool, argv[14], &skipTaskbar);
356
- CALL_NAPI(napi_get_value_bool, argv[15], &popupMenu);
355
+ CALL_NAPI(napi_get_value_bool, argv[14], &skip_taskbar);
356
+ CALL_NAPI(napi_get_value_bool, argv[15], &popup_menu);
357
357
  CALL_NAPI(napi_get_value_bool, argv[16], &tooltip);
358
358
  CALL_NAPI(napi_get_value_bool, argv[17], &utility);
359
359
 
360
- int window_id;
361
- int native_pointer_size;
360
+ int pixel_width, pixel_height, window_id, native_pointer_size;
361
+
362
362
  CALL_SDL_HELPER(window_create,
363
363
  title, display,
364
364
  &x, &y, &width, &height,
365
- visible, &fullscreen, &resizable, &borderless, &alwaysOnTop,
365
+ &pixel_width, &pixel_height,
366
+ visible, &fullscreen, &resizable, &borderless, &always_on_top,
366
367
  &accelerated, &vsync, opengl,
367
- &skipTaskbar, &popupMenu, &tooltip, &utility,
368
+ &skip_taskbar, &popup_menu, &tooltip, &utility,
368
369
  &window_id, &native_pointer, &native_pointer_size
369
370
  );
370
371
 
@@ -391,6 +392,14 @@ window_create(napi_env env, napi_callback_info info)
391
392
  CALL_NAPI(napi_create_string_latin1, "height", 6, &key);
392
393
  CALL_NAPI(napi_set_property, result, key, value);
393
394
 
395
+ CALL_NAPI(napi_create_int32, pixel_width, &value);
396
+ CALL_NAPI(napi_create_string_latin1, "pixelWidth", 10, &key);
397
+ CALL_NAPI(napi_set_property, result, key, value);
398
+
399
+ CALL_NAPI(napi_create_int32, pixel_height, &value);
400
+ CALL_NAPI(napi_create_string_latin1, "pixelHeight", 11, &key);
401
+ CALL_NAPI(napi_set_property, result, key, value);
402
+
394
403
  CALL_NAPI(napi_create_int32, fullscreen, &value);
395
404
  CALL_NAPI(napi_coerce_to_bool, value, &value);
396
405
  CALL_NAPI(napi_create_string_latin1, "fullscreen", 10, &key);
@@ -406,7 +415,7 @@ window_create(napi_env env, napi_callback_info info)
406
415
  CALL_NAPI(napi_create_string_latin1, "borderless", 10, &key);
407
416
  CALL_NAPI(napi_set_property, result, key, value);
408
417
 
409
- CALL_NAPI(napi_create_int32, alwaysOnTop, &value);
418
+ CALL_NAPI(napi_create_int32, always_on_top, &value);
410
419
  CALL_NAPI(napi_coerce_to_bool, value, &value);
411
420
  CALL_NAPI(napi_create_string_latin1, "alwaysOnTop", 11, &key);
412
421
  CALL_NAPI(napi_set_property, result, key, value);
@@ -421,12 +430,12 @@ window_create(napi_env env, napi_callback_info info)
421
430
  CALL_NAPI(napi_create_string_latin1, "vsync", 5, &key);
422
431
  CALL_NAPI(napi_set_property, result, key, value);
423
432
 
424
- CALL_NAPI(napi_create_int32, skipTaskbar, &value);
433
+ CALL_NAPI(napi_create_int32, skip_taskbar, &value);
425
434
  CALL_NAPI(napi_coerce_to_bool, value, &value);
426
435
  CALL_NAPI(napi_create_string_latin1, "skipTaskbar", 11, &key);
427
436
  CALL_NAPI(napi_set_property, result, key, value);
428
437
 
429
- CALL_NAPI(napi_create_int32, popupMenu, &value);
438
+ CALL_NAPI(napi_create_int32, popup_menu, &value);
430
439
  CALL_NAPI(napi_coerce_to_bool, value, &value);
431
440
  CALL_NAPI(napi_create_string_latin1, "popupMenu", 9, &key);
432
441
  CALL_NAPI(napi_set_property, result, key, value);
@@ -505,6 +514,8 @@ window_setPosition(napi_env env, napi_callback_info info)
505
514
  napi_value
506
515
  window_setSize(napi_env env, napi_callback_info info)
507
516
  {
517
+ napi_value result = nullptr;
518
+
508
519
  napi_value argv[3];
509
520
  size_t argc = sizeof(argv) / sizeof(napi_value);
510
521
  CALL_NAPI(napi_get_cb_info, info, &argc, argv, nullptr, nullptr);
@@ -516,10 +527,25 @@ window_setSize(napi_env env, napi_callback_info info)
516
527
  CALL_NAPI(napi_get_value_int32, argv[1], &width);
517
528
  CALL_NAPI(napi_get_value_int32, argv[2], &height);
518
529
 
519
- CALL_SDL_HELPER(window_setSize, window_id, width, height);
530
+ int pixel_width, pixel_height;
531
+ CALL_SDL_HELPER(window_setSize,
532
+ window_id, width, height,
533
+ &pixel_width, &pixel_height
534
+ );
535
+
536
+ napi_value key, value;
537
+ CALL_NAPI(napi_create_object, &result);
538
+
539
+ CALL_NAPI(napi_create_int32, pixel_width, &value);
540
+ CALL_NAPI(napi_create_string_latin1, "pixelWidth", 10, &key);
541
+ CALL_NAPI(napi_set_property, result, key, value);
542
+
543
+ CALL_NAPI(napi_create_int32, pixel_height, &value);
544
+ CALL_NAPI(napi_create_string_latin1, "pixelHeight", 11, &key);
545
+ CALL_NAPI(napi_set_property, result, key, value);
520
546
 
521
547
  cleanup:
522
- return nullptr;
548
+ return result;
523
549
  }
524
550
 
525
551
  napi_value
@@ -693,7 +693,6 @@ initialize (Variant & object)
693
693
  // Hints
694
694
  {
695
695
  SDL_SetHint(SDL_HINT_FRAMEBUFFER_ACCELERATION, "0");
696
- SDL_SetHint(SDL_HINT_XINPUT_ENABLED, "0");
697
696
  }
698
697
 
699
698
  MAKE_MAP(object);
@@ -840,6 +839,87 @@ initialize (Variant & object)
840
839
  }
841
840
 
842
841
 
842
+ ErrorMessage *
843
+ _windowGetPixelSize(SDL_Window * window, int * pixel_width, int * pixel_height)
844
+ {
845
+ SDL_Renderer * renderer = SDL_GetRenderer(window);
846
+ if (renderer == nullptr) {
847
+ RETURN_ERROR("SDL_GetRenderer() error: %s\n", SDL_GetError());
848
+ }
849
+
850
+ if (SDL_GetRendererOutputSize(renderer, pixel_width, pixel_height) != 0) {
851
+ RETURN_ERROR("SDL_GetRendererOutputSize() error: %s\n", SDL_GetError());
852
+ }
853
+
854
+ return nullptr;
855
+ }
856
+
857
+ ErrorMessage *
858
+ _windowUpdateTexture(
859
+ SDL_Window * window,
860
+ SDL_Renderer * renderer,
861
+ int pixel_width, int pixel_height,
862
+ SDL_Texture ** texture_dst
863
+ ) {
864
+ int window_id = SDL_GetWindowID(window);
865
+
866
+ int format = SDL_GetWindowPixelFormat(window);
867
+ SDL_Texture * texture = SDL_CreateTexture(renderer, format, SDL_TEXTUREACCESS_STREAMING, pixel_width, pixel_height);
868
+ if (texture == nullptr) {
869
+ RETURN_ERROR("SDL_CreateTexture(%d) error: %s\n", window_id, SDL_GetError());
870
+ }
871
+
872
+ if (SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_NONE)) {
873
+ RETURN_ERROR("SDL_SetTextureBlendMode(%d) error: %s\n", window_id, SDL_GetError());
874
+ }
875
+
876
+ SDL_Texture * old_texture = (SDL_Texture *) SDL_SetWindowData(window, "texture", texture);
877
+ if (old_texture != nullptr) { SDL_DestroyTexture(old_texture); }
878
+
879
+ if (texture_dst != nullptr) { *texture_dst = texture; }
880
+
881
+ return nullptr;
882
+ }
883
+
884
+ ErrorMessage *
885
+ _windowUpdateRenderer(
886
+ SDL_Window * window,
887
+ bool * accelerated,
888
+ bool * vsync
889
+ ) {
890
+ int window_id = SDL_GetWindowID(window);
891
+
892
+ SDL_Renderer * old_renderer = SDL_GetRenderer(window);
893
+ if (old_renderer != nullptr) { SDL_DestroyRenderer(old_renderer); }
894
+
895
+ int renderer_flags = 0
896
+ | (*accelerated ? SDL_RENDERER_ACCELERATED : SDL_RENDERER_SOFTWARE)
897
+ | (*accelerated && *vsync ? SDL_RENDERER_PRESENTVSYNC : 0);
898
+
899
+ SDL_Renderer * renderer = SDL_CreateRenderer(window, -1, renderer_flags);
900
+ if (renderer == nullptr) {
901
+ RETURN_ERROR("SDL_CreateRenderer(%d, %d) error: %s\n", window_id, renderer_flags, SDL_GetError());
902
+ }
903
+
904
+ SDL_RendererInfo info;
905
+ SDL_GetRendererInfo(renderer, &info);
906
+
907
+ *accelerated = info.flags & SDL_RENDERER_ACCELERATED;
908
+ *vsync = info.flags & SDL_RENDERER_PRESENTVSYNC;
909
+
910
+ ErrorMessage * error;
911
+
912
+ int pixel_width, pixel_height;
913
+ error = _windowGetPixelSize(window, &pixel_width, &pixel_height);
914
+ if (error != nullptr) { return error; }
915
+
916
+ error = _windowUpdateTexture(window, renderer, pixel_width, pixel_height, nullptr);
917
+ if (error != nullptr) { return error; }
918
+
919
+ return nullptr;
920
+ }
921
+
922
+
843
923
  ErrorMessage *
844
924
  _controllerGetState (SDL_GameController * controller, Variant & object)
845
925
  {
@@ -925,9 +1005,20 @@ packageEvent (const SDL_Event & event, Variant & object)
925
1005
  break;
926
1006
  }
927
1007
  case SDL_WINDOWEVENT_SIZE_CHANGED: {
1008
+ SDL_Window * window = SDL_GetWindowFromID(event.window.windowID);
1009
+ if (window == nullptr) {
1010
+ RETURN_ERROR("SDL_CreateWindow() error: %s\n", SDL_GetError());
1011
+ }
1012
+
1013
+ int pixel_width, pixel_height;
1014
+ ErrorMessage * error = _windowGetPixelSize(window, &pixel_width, &pixel_height);
1015
+ if (error != nullptr) { return error; }
1016
+
928
1017
  SET_NUM(object, "type", (int) EventType::RESIZE);
929
1018
  SET_NUM(object, "width", event.window.data1);
930
1019
  SET_NUM(object, "height", event.window.data2);
1020
+ SET_NUM(object, "pixelWidth", pixel_width);
1021
+ SET_NUM(object, "pixelHeight", pixel_height);
931
1022
  break;
932
1023
  }
933
1024
  case SDL_WINDOWEVENT_MINIMIZED: { SET_NUM(object, "type", (int) EventType::MINIMIZE); break; }
@@ -1204,7 +1295,8 @@ events_poll (Variant & object)
1204
1295
 
1205
1296
  if (!got_event) { return nullptr; }
1206
1297
 
1207
- packageEvent(event, object);
1298
+ ErrorMessage * error = packageEvent(event, object);
1299
+ if (error != nullptr) { return error; }
1208
1300
 
1209
1301
  return nullptr;
1210
1302
  }
@@ -1219,7 +1311,8 @@ events_wait (int timeout, Variant & object)
1219
1311
 
1220
1312
  if (!got_event) { return nullptr; }
1221
1313
 
1222
- packageEvent(event, object);
1314
+ ErrorMessage * error = packageEvent(event, object);
1315
+ if (error != nullptr) { return error; }
1223
1316
 
1224
1317
  return nullptr;
1225
1318
  }
@@ -1308,87 +1401,26 @@ video_getDisplays(Variant & list)
1308
1401
  return nullptr;
1309
1402
  }
1310
1403
 
1311
- ErrorMessage *
1312
- _windowUpdateTexture(
1313
- SDL_Window * window,
1314
- SDL_Renderer * renderer,
1315
- int width, int height,
1316
- SDL_Texture ** texture_dst
1317
- ) {
1318
- int window_id = SDL_GetWindowID(window);
1319
-
1320
- int format = SDL_GetWindowPixelFormat(window);
1321
- SDL_Texture * texture = SDL_CreateTexture(renderer, format, SDL_TEXTUREACCESS_STREAMING, width, height);
1322
- if (texture == nullptr) {
1323
- RETURN_ERROR("SDL_CreateTexture(%d) error: %s\n", window_id, SDL_GetError());
1324
- }
1325
-
1326
- if (SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_NONE)) {
1327
- RETURN_ERROR("SDL_SetTextureBlendMode(%d) error: %s\n", window_id, SDL_GetError());
1328
- }
1329
-
1330
- SDL_Texture * old_texture = (SDL_Texture *) SDL_SetWindowData(window, "texture", texture);
1331
- if (old_texture != nullptr) { SDL_DestroyTexture(old_texture); }
1332
-
1333
- if (texture_dst != nullptr) { *texture_dst = texture; }
1334
-
1335
- return nullptr;
1336
- }
1337
-
1338
- ErrorMessage *
1339
- _windowUpdateRenderer(
1340
- SDL_Window * window,
1341
- bool * accelerated,
1342
- bool * vsync
1343
- ) {
1344
- int window_id = SDL_GetWindowID(window);
1345
-
1346
- SDL_Renderer * old_renderer = SDL_GetRenderer(window);
1347
- if (old_renderer != nullptr) { SDL_DestroyRenderer(old_renderer); }
1348
-
1349
- int renderer_flags = 0
1350
- | (*accelerated ? SDL_RENDERER_ACCELERATED : SDL_RENDERER_SOFTWARE)
1351
- | (*accelerated && *vsync ? SDL_RENDERER_PRESENTVSYNC : 0);
1352
-
1353
- SDL_Renderer * renderer = SDL_CreateRenderer(window, -1, renderer_flags);
1354
- if (renderer == nullptr) {
1355
- RETURN_ERROR("SDL_CreateRenderer(%d, %d) error: %s\n", window_id, renderer_flags, SDL_GetError());
1356
- }
1357
-
1358
- SDL_RendererInfo info;
1359
- SDL_GetRendererInfo(renderer, &info);
1360
-
1361
- *accelerated = info.flags & SDL_RENDERER_ACCELERATED;
1362
- *vsync = info.flags & SDL_RENDERER_PRESENTVSYNC;
1363
-
1364
- int width, height;
1365
- SDL_GetWindowSize(window, &width, &height);
1366
- ErrorMessage * error = _windowUpdateTexture(window, renderer, width, height, nullptr);
1367
- if (error != nullptr) { return error; }
1368
-
1369
- return nullptr;
1370
- }
1371
1404
 
1372
1405
  ErrorMessage *
1373
1406
  window_create (
1374
1407
  const char * title, int display,
1375
1408
  int ** x, int ** y, int ** width, int ** height,
1409
+ int * pixel_width, int * pixel_height,
1376
1410
  bool visible,
1377
1411
  bool * fullscreen,
1378
1412
  bool * resizable,
1379
1413
  bool * borderless,
1380
- bool * alwaysOnTop,
1414
+ bool * always_on_top,
1381
1415
  bool * accelerated,
1382
1416
  bool * vsync,
1383
1417
  bool opengl,
1384
- bool * skipTaskbar,
1385
- bool * popupMenu,
1418
+ bool * skip_taskbar,
1419
+ bool * popup_menu,
1386
1420
  bool * tooltip,
1387
1421
  bool * utility,
1388
1422
  int * window_id, void ** native_pointer, int * native_pointer_size
1389
1423
  ) {
1390
- SDL_Window * window;
1391
-
1392
1424
  if (*x == nullptr) {
1393
1425
  *x = (int *) malloc(sizeof(int));
1394
1426
  **x = SDL_WINDOWPOS_CENTERED_DISPLAY(display);
@@ -1406,18 +1438,18 @@ window_create (
1406
1438
  **height = 480;
1407
1439
  }
1408
1440
  int desired_flags = 0
1409
- | SDL_WINDOW_HIDDEN
1441
+ | SDL_WINDOW_HIDDEN | SDL_WINDOW_ALLOW_HIGHDPI
1410
1442
  | (*fullscreen ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0)
1411
1443
  | (*resizable ? SDL_WINDOW_RESIZABLE : 0)
1412
1444
  | (*borderless ? SDL_WINDOW_BORDERLESS : 0)
1413
- | (*alwaysOnTop ? SDL_WINDOW_ALWAYS_ON_TOP : 0)
1445
+ | (*always_on_top ? SDL_WINDOW_ALWAYS_ON_TOP : 0)
1414
1446
  | (opengl ? SDL_WINDOW_OPENGL : 0)
1415
- | (*skipTaskbar ? SDL_WINDOW_SKIP_TASKBAR : 0)
1416
- | (*popupMenu ? SDL_WINDOW_POPUP_MENU : 0)
1447
+ | (*skip_taskbar ? SDL_WINDOW_SKIP_TASKBAR : 0)
1448
+ | (*popup_menu ? SDL_WINDOW_POPUP_MENU : 0)
1417
1449
  | (*tooltip ? SDL_WINDOW_TOOLTIP : 0)
1418
1450
  | (*utility ? SDL_WINDOW_UTILITY : 0);
1419
1451
 
1420
- window = SDL_CreateWindow(title, **x, **y, **width, **height, desired_flags);
1452
+ SDL_Window * window = SDL_CreateWindow(title, **x, **y, **width, **height, desired_flags);
1421
1453
  if (window == nullptr) {
1422
1454
  RETURN_ERROR("SDL_CreateWindow() error: %s\n", SDL_GetError());
1423
1455
  }
@@ -1428,9 +1460,9 @@ window_create (
1428
1460
  *fullscreen = actual_flags & (SDL_WINDOW_FULLSCREEN | SDL_WINDOW_FULLSCREEN_DESKTOP);
1429
1461
  *resizable = actual_flags & SDL_WINDOW_RESIZABLE;
1430
1462
  *borderless = actual_flags & SDL_WINDOW_BORDERLESS;
1431
- *alwaysOnTop = actual_flags & SDL_WINDOW_ALWAYS_ON_TOP;
1432
- *skipTaskbar = actual_flags & SDL_WINDOW_SKIP_TASKBAR;
1433
- *popupMenu = actual_flags & SDL_WINDOW_POPUP_MENU;
1463
+ *always_on_top = actual_flags & SDL_WINDOW_ALWAYS_ON_TOP;
1464
+ *skip_taskbar = actual_flags & SDL_WINDOW_SKIP_TASKBAR;
1465
+ *popup_menu = actual_flags & SDL_WINDOW_POPUP_MENU;
1434
1466
  *tooltip = actual_flags & SDL_WINDOW_TOOLTIP;
1435
1467
  *utility = actual_flags & SDL_WINDOW_UTILITY;
1436
1468
 
@@ -1454,7 +1486,12 @@ window_create (
1454
1486
  *native_pointer_size = size;
1455
1487
  }
1456
1488
 
1457
- ErrorMessage * error = _windowUpdateRenderer(window, accelerated, vsync);
1489
+ ErrorMessage * error;
1490
+
1491
+ error = _windowUpdateRenderer(window, accelerated, vsync);
1492
+ if (error != nullptr) { return error; }
1493
+
1494
+ error = _windowGetPixelSize(window, pixel_width, pixel_height);
1458
1495
  if (error != nullptr) { return error; }
1459
1496
 
1460
1497
  if (visible) { SDL_ShowWindow(window); }
@@ -1489,7 +1526,7 @@ window_setPosition (int window_id, int x, int y)
1489
1526
  }
1490
1527
 
1491
1528
  ErrorMessage *
1492
- window_setSize (int window_id, int width, int height)
1529
+ window_setSize (int window_id, int width, int height, int * pixel_width, int * pixel_height)
1493
1530
  {
1494
1531
  SDL_Window * window = SDL_GetWindowFromID(window_id);
1495
1532
  if (window == nullptr) {
@@ -1498,6 +1535,9 @@ window_setSize (int window_id, int width, int height)
1498
1535
 
1499
1536
  SDL_SetWindowSize(window, width, height);
1500
1537
 
1538
+ ErrorMessage * error = _windowGetPixelSize(window, pixel_width, pixel_height);
1539
+ if (error != nullptr) { return error; }
1540
+
1501
1541
  return nullptr;
1502
1542
  }
1503
1543
 
@@ -1695,14 +1735,15 @@ window_render (
1695
1735
  RETURN_ERROR("SDL_QueryTexture(%d) error: %s\n", window_id, SDL_GetError());
1696
1736
  }
1697
1737
 
1698
- int window_width, window_height;
1699
- SDL_GetWindowSize(window, &window_width, &window_height);
1738
+ int pixel_width, pixel_height;
1739
+ ErrorMessage * error = _windowGetPixelSize(window, &pixel_width, &pixel_height);
1740
+ if (error != nullptr) { return error; }
1700
1741
 
1701
- if (window_width != texture_width || window_height != texture_height) {
1702
- ErrorMessage * error = _windowUpdateTexture(window, renderer, window_width, window_height, &texture);
1742
+ if (pixel_width != texture_width || pixel_height != texture_height) {
1743
+ ErrorMessage * error = _windowUpdateTexture(window, renderer, pixel_width, pixel_height, &texture);
1703
1744
  if (error != nullptr) { return error; }
1704
- texture_width = window_width;
1705
- texture_height = window_height;
1745
+ texture_width = pixel_width;
1746
+ texture_height = pixel_height;
1706
1747
  }
1707
1748
 
1708
1749
  SDL_Surface * src_surface = SDL_CreateRGBSurfaceWithFormatFrom(pixels, w, h, SDL_BITSPERPIXEL(format), stride, format);
@@ -56,6 +56,7 @@ ErrorMessage * video_getDisplays (Variant & list);
56
56
  ErrorMessage * window_create (
57
57
  const char * title, int display,
58
58
  int ** x, int ** y, int ** width, int ** height,
59
+ int * pixel_width, int * pixel_height,
59
60
  bool visible,
60
61
  bool * fullscreen,
61
62
  bool * resizable,
@@ -72,7 +73,10 @@ ErrorMessage * window_create (
72
73
  );
73
74
  ErrorMessage * window_setTitle (int window_id, const char * title);
74
75
  ErrorMessage * window_setPosition (int window_id, int x, int y);
75
- ErrorMessage * window_setSize (int window_id, int width, int height);
76
+ ErrorMessage * window_setSize (
77
+ int window_id, int width, int height,
78
+ int * pixel_width, int * pixel_height
79
+ );
76
80
  ErrorMessage * window_setFullscreen (int window_id, bool * fullscreen);
77
81
  ErrorMessage * window_setResizable (int window_id, bool * resizable);
78
82
  ErrorMessage * window_setBorderless (int window_id, bool * borderless);
@@ -67,8 +67,10 @@ export namespace Events {
67
67
  }
68
68
  interface Resize extends WindowEvent {
69
69
  readonly type: 'resize'
70
- readonly width: number,
70
+ readonly width: number
71
71
  readonly height: number
72
+ readonly pixelWidth: number
73
+ readonly pixelHeight: number
72
74
  }
73
75
  interface Focus extends WindowEvent { readonly type: 'focus' }
74
76
  interface Blur extends WindowEvent { readonly type: 'blur' }
@@ -380,6 +382,9 @@ export namespace Sdl {
380
382
  readonly height: number
381
383
  setSize (width: number, height: number): void
382
384
 
385
+ readonly pixelWidth: number
386
+ readonly pixelHeight: number
387
+
383
388
  readonly visible: boolean
384
389
  show (show?: boolean): void
385
390
  hide (): void