@kmamal/sdl 0.11.4 → 0.11.5

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
@@ -10,13 +10,13 @@ Provides window management, input events (keyboard, mouse, joysticks, controller
10
10
  It should work on Linux, Mac, and Windows.
11
11
  Prebuilt binaries are available for x64 architectures, arm-based Macs, and Raspberry Pi.
12
12
 
13
- #### Canvas, WebGL, and WebGPU
13
+ #### Canvas2D, WebGL, and WebGPU
14
14
 
15
- One goal of this project is to allow using Canvas, WebGL, and WebGPU without a browser.
16
- You can use the [canvas](https://www.npmjs.com/package/canvas) package to render using the canvas API.
15
+ One goal of this project is to allow using Canvas2D, WebGL, and WebGPU without a browser.
16
+ You can use the [@napi-rs/canvas](https://www.npmjs.com/package/@napi-rs/canvas) package to render using the Canvas2D API.
17
17
  For WebGL you can use [@kmamal/gl](https://github.com/kmamal/headless-gl#readme) and for WebGPU you can use [@kmamal/gpu](https://github.com/kmamal/gpu#readme).
18
18
  Both WebGL and WebGPU support rendering directly to the window (without any intermediate buffer copying).
19
- Canvas still uses an intermediate buffer, but that might change in the future.
19
+ Canvas2D still uses an intermediate buffer, but that might change in the future.
20
20
 
21
21
 
22
22
  ## Installation
@@ -43,14 +43,14 @@ const window = sdl.video.createWindow({ title: "Hello, World!" })
43
43
  window.on('*', console.log)
44
44
  ```
45
45
 
46
- ### Canvas
46
+ ### Canvas2D
47
47
 
48
48
  ```js
49
49
  import sdl from '@kmamal/sdl'
50
- import { createCanvas } from 'canvas'
50
+ import { createCanvas } from '@napi-rs/canvas'
51
51
 
52
52
  // Setup
53
- const window = sdl.video.createWindow({ title: "Canvas" })
53
+ const window = sdl.video.createWindow({ title: "Canvas2D" })
54
54
  const { pixelWidth: width, pixelHeight: height } = window
55
55
  const canvas = createCanvas(width, height)
56
56
  const ctx = canvas.getContext('2d')
@@ -60,8 +60,8 @@ ctx.fillStyle = 'red'
60
60
  ctx.fillRect(0, 0, width, height)
61
61
 
62
62
  // Render to window
63
- const buffer = canvas.toBuffer('raw')
64
- window.render(width, height, width * 4, 'bgra32', buffer)
63
+ const buffer = Buffer.from(ctx.getImageData(0, 0, width, height).data)
64
+ window.render(width, height, width * 4, 'rgba32', buffer)
65
65
  ```
66
66
 
67
67
  ### WebGL
@@ -148,6 +148,7 @@ Check the [`examples/`](https://github.com/kmamal/node-sdl/tree/master/examples#
148
148
  * [Event: 'restore'](#event-restore)
149
149
  * [Event: 'move'](#event-move)
150
150
  * [Event: 'resize'](#event-resize)
151
+ * [Event: 'displayChange'](#event-displaychange)
151
152
  * [Event: 'focus'](#event-focus)
152
153
  * [Event: 'blur'](#event-blur)
153
154
  * [Event: 'hover'](#event-hover)
@@ -177,6 +178,7 @@ Check the [`examples/`](https://github.com/kmamal/node-sdl/tree/master/examples#
177
178
  * [window.pixelHeight](#windowpixelheight)
178
179
  * [window.setSize(width, height)](#windowsetsizewidth-height)
179
180
  * [window.setSizeInPixels(pixelWidth, pixelHeight)](#windowsetsizeinpixelspixelwidth-pixelheight)
181
+ * [window.display](#windowdisplay)
180
182
  * [window.visible](#windowvisible)
181
183
  * [window.show([show])](#windowshowshow)
182
184
  * [window.hide()](#windowhide)
@@ -654,6 +656,12 @@ Fired when the window changes position.
654
656
 
655
657
  Fired when the window changes size.
656
658
 
659
+ ### Event: 'displayChange'
660
+
661
+ * `display: <object>` An object from [`sdl.video.displays`](#sdlvideodisplays) indicating the window's new display.
662
+
663
+ Fired when a window moves from one display to another.
664
+
657
665
  ### Event: 'focus'
658
666
 
659
667
  Fired when a window gains the keyboard focus.
@@ -851,6 +859,13 @@ Changes the size of the window.
851
859
  Changes the size of the window.
852
860
  This function only behaves differently from [`window.setSize()`](#windowsetsizewidth-height) for [high-dpi](#high-dpi) displays.
853
861
 
862
+ ### window.display
863
+
864
+ * `<object>`
865
+
866
+ An object from [`sdl.video.displays`](#sdlvideodisplays) indicating the display this window belongs to.
867
+ If the window spans multiple displays, then the display containing the center of the window is returned.
868
+
854
869
  ### window.visible
855
870
 
856
871
  * `<boolean>`
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "0.11.4",
2
+ "version": "0.11.5",
3
3
  "name": "@kmamal/sdl",
4
4
  "description": "SDL bindings for Node.js",
5
5
  "keywords": [
@@ -33,13 +33,15 @@ const reconcileDevices = (emitter, mainList, currList, compare) => {
33
33
  if (cmp === 0) {
34
34
  mainDevice = mainList[++mainIndex]
35
35
  currDevice = currList[++currIndex]
36
- } else if (cmp < 0) {
36
+ }
37
+ else if (cmp < 0) {
37
38
  mainList.splice(mainIndex, 1)
38
39
  const type = 'deviceRemove'
39
40
  const event = { type, device: mainDevice }
40
41
  emitter.emit(type, event)
41
42
  mainDevice = mainList[mainIndex]
42
- } else {
43
+ }
44
+ else {
43
45
  mainList.splice(mainIndex, 0, currDevice)
44
46
  mainDevice = mainList[++mainIndex]
45
47
  const type = 'deviceAdd'
@@ -56,7 +58,8 @@ const reconcileDevices = (emitter, mainList, currList, compare) => {
56
58
  const event = { type, device: mainDevice }
57
59
  emitter.emit(type, event)
58
60
  }
59
- } else {
61
+ }
62
+ else {
60
63
  while (currIndex < currList.length) {
61
64
  mainList.push(currDevice)
62
65
  const type = 'deviceAdd'
@@ -116,6 +119,11 @@ const handleEvent = (event) => {
116
119
  window._pixelWidth = event.pixelWidth
117
120
  window._pixelHeight = event.pixelHeight
118
121
  } break
122
+ case 'displayChange': {
123
+ window._displayIndex = event.displayIndex
124
+ delete event.displayIndex
125
+ event.display = window.display
126
+ } break
119
127
 
120
128
  case 'show': {
121
129
  window._visible = true
@@ -12,6 +12,7 @@ const validEvents = [
12
12
  'restore',
13
13
  'move',
14
14
  'resize',
15
+ 'displayChange',
15
16
  'focus',
16
17
  'blur',
17
18
  'hover',
@@ -121,6 +122,7 @@ class Window extends EventsViaPoll {
121
122
  this._height = result.height
122
123
  this._pixelWidth = result.pixelWidth
123
124
  this._pixelHeight = result.pixelHeight
125
+ this._displayIndex = result.displayIndex
124
126
  this._fullscreen = result.fullscreen
125
127
  this._resizable = result.resizable
126
128
  this._borderless = result.borderless
@@ -237,6 +239,10 @@ class Window extends EventsViaPoll {
237
239
  get pixelWidth () { return this._pixelWidth }
238
240
  get pixelHeight () { return this._pixelHeight }
239
241
 
242
+ get display () {
243
+ return Globals.displays[this._displayIndex]
244
+ }
245
+
240
246
  get visible () { return this._visible }
241
247
  show (show = true) {
242
248
  if (this._destroyed) { throw Object.assign(new Error("window is destroyed"), { id: this._id }) }
@@ -27,6 +27,7 @@ std::string events::types::QUIT;
27
27
  std::string events::types::DISPLAY_ADD;
28
28
  std::string events::types::DISPLAY_REMOVE;
29
29
  std::string events::types::DISPLAY_ORIENT;
30
+ std::string events::types::DISPLAY_CHANGE;
30
31
  std::string events::types::SHOW;
31
32
  std::string events::types::HIDE;
32
33
  std::string events::types::EXPOSE;
@@ -128,6 +129,11 @@ events::dispatchEvent(const SDL_Event &event)
128
129
  packed.Set("pixelHeight", Napi::Number::New(env, pixel_height));
129
130
  break;
130
131
  }
132
+ case SDL_WINDOWEVENT_DISPLAY_CHANGED: {
133
+ packed.Set("type", events::types::DISPLAY_CHANGE);
134
+ packed.Set("displayIndex", Napi::Number::New(env, event.window.data1));
135
+ break;
136
+ }
131
137
  case SDL_WINDOWEVENT_MINIMIZED: { packed.Set("type", events::types::MINIMIZE); break; }
132
138
  case SDL_WINDOWEVENT_MAXIMIZED: { packed.Set("type", events::types::MAXIMIZE); break; }
133
139
  case SDL_WINDOWEVENT_RESTORED: { packed.Set("type", events::types::RESTORE); break; }
@@ -28,6 +28,7 @@ namespace events {
28
28
  extern std::string DISPLAY_ADD;
29
29
  extern std::string DISPLAY_REMOVE;
30
30
  extern std::string DISPLAY_ORIENT;
31
+ extern std::string DISPLAY_CHANGE;
31
32
  extern std::string SHOW;
32
33
  extern std::string HIDE;
33
34
  extern std::string EXPOSE;
@@ -58,6 +58,7 @@ global::initialize(const Napi::CallbackInfo &info)
58
58
  events::types::DISPLAY_ADD = "displayAdd";
59
59
  events::types::DISPLAY_REMOVE = "displayRemove";
60
60
  events::types::DISPLAY_ORIENT = "displayOrient";
61
+ events::types::DISPLAY_CHANGE = "displayChange";
61
62
  events::types::SHOW = "show";
62
63
  events::types::HIDE = "hide";
63
64
  events::types::EXPOSE = "expose";
@@ -10,7 +10,6 @@ struct CachedTexture {
10
10
  int width;
11
11
  int height;
12
12
  unsigned int format;
13
- SDL_ScaleMode scaling;
14
13
  };
15
14
 
16
15
  std::map<SDL_Window*, CachedTexture> cachedTextures;
@@ -168,6 +167,14 @@ window::create (const Napi::CallbackInfo &info)
168
167
  int pixel_width, pixel_height;
169
168
  SDL_GetWindowSizeInPixels(window, &pixel_width, &pixel_height);
170
169
 
170
+ display = SDL_GetWindowDisplayIndex(window);
171
+ if (display < 0) {
172
+ std::ostringstream message;
173
+ message << "SDL_GetWindowDisplayIndex(" << window_id << ") error: " << SDL_GetError();
174
+ SDL_ClearError();
175
+ throw Napi::Error::New(env, message.str());
176
+ }
177
+
171
178
  Napi::Object native = Napi::Object::New(env);
172
179
 
173
180
  SDL_SysWMinfo sys_wm_info;
@@ -228,6 +235,7 @@ window::create (const Napi::CallbackInfo &info)
228
235
  result.Set("height", Napi::Number::New(env, height));
229
236
  result.Set("pixelWidth", Napi::Number::New(env, pixel_width));
230
237
  result.Set("pixelHeight", Napi::Number::New(env, pixel_height));
238
+ result.Set("displayIndex", Napi::Number::New(env, display));
231
239
  result.Set("fullscreen", Napi::Boolean::New(env, is_fullscreen));
232
240
  result.Set("resizable", Napi::Boolean::New(env, is_resizable));
233
241
  result.Set("borderless", Napi::Boolean::New(env, is_borderless));
@@ -584,7 +592,6 @@ window::render (const Napi::CallbackInfo &info)
584
592
  || cached.width != width
585
593
  || cached.height != height
586
594
  || cached.format != format
587
- || cached.scaling != scaling
588
595
  ) {
589
596
  if (texture != nullptr) { SDL_DestroyTexture(texture); }
590
597
 
@@ -597,18 +604,17 @@ window::render (const Napi::CallbackInfo &info)
597
604
  throw Napi::Error::New(env, message.str());
598
605
  }
599
606
 
600
- if(SDL_SetTextureScaleMode(texture, scaling) < 0) {
601
- std::ostringstream message;
602
- message << "SDL_SetTextureScaleMode(" << window_id << ", " << scaling << ") error: " << SDL_GetError();
603
- SDL_ClearError();
604
- throw Napi::Error::New(env, message.str());
605
- }
606
-
607
607
  cached.texture = texture;
608
608
  cached.width = width;
609
609
  cached.height = height;
610
610
  cached.format = format;
611
- cached.scaling = scaling;
611
+ }
612
+
613
+ if(SDL_SetTextureScaleMode(texture, scaling) < 0) {
614
+ std::ostringstream message;
615
+ message << "SDL_SetTextureScaleMode(" << window_id << ", " << scaling << ") error: " << SDL_GetError();
616
+ SDL_ClearError();
617
+ throw Napi::Error::New(env, message.str());
612
618
  }
613
619
 
614
620
  SDL_UpdateTexture(texture, nullptr, pixels, stride);
@@ -86,6 +86,10 @@ export namespace Events {
86
86
  readonly pixelWidth: number
87
87
  readonly pixelHeight: number
88
88
  }
89
+ export interface DisplayChange extends WindowEvent {
90
+ readonly type: 'displayChange'
91
+ readonly display: Sdl.Video.Display
92
+ }
89
93
  export interface Focus extends WindowEvent { readonly type: 'focus' }
90
94
  export interface Blur extends WindowEvent { readonly type: 'blur' }
91
95
  export interface Hover extends WindowEvent { readonly type: 'hover' }
@@ -123,6 +127,7 @@ export namespace Events {
123
127
  | Restore
124
128
  | Move
125
129
  | Resize
130
+ | DisplayChange
126
131
  | Focus
127
132
  | Blur
128
133
  | Hover
@@ -388,6 +393,7 @@ export namespace Sdl {
388
393
  on (event: 'restore', listener: (event: Events.Window.Restore) => void): this
389
394
  on (event: 'move', listener: (event: Events.Window.Move) => void): this
390
395
  on (event: 'resize', listener: (event: Events.Window.Resize) => void): this
396
+ on (event: 'displayChange', listener: (event: Events.Window.DisplayChange) => void): this
391
397
  on (event: 'focus', listener: (event: Events.Window.Focus) => void): this
392
398
  on (event: 'blur', listener: (event: Events.Window.Blur) => void): this
393
399
  on (event: 'hover', listener: (event: Events.Window.Hover) => void): this
@@ -424,6 +430,8 @@ export namespace Sdl {
424
430
  readonly pixelHeight: number
425
431
  setSizeInPixels (pixelWidth: number, pixelHeight: number): void
426
432
 
433
+ readonly display: Display
434
+
427
435
  readonly visible: boolean
428
436
  show (show?: boolean): void
429
437
  hide (): void