@kmamal/sdl 0.3.7 → 0.4.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
@@ -5,14 +5,19 @@
5
5
  [![Vulnerabilities](https://img.shields.io/snyk/vulnerabilities/npm/%2540kmamal%252Fsdl)](https://snyk.io/test/npm/@kmamal/sdl)
6
6
  [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
7
7
 
8
- SDL2 bindings for Node.js. Provides window management, input events, audio playback/recording, and clipboard manipulation. Use together with [@kmamal/gl](https://github.com/kmamal/headless-gl#readme) for WebGL drawing.
8
+ SDL2 bindings for Node.js.
9
+ Provides window management, input events, audio playback/recording, and clipboard manipulation.
10
+ You can use it together with [@kmamal/gl](https://github.com/kmamal/headless-gl#readme) to get native WebGL drawing without having to use a browser.
9
11
 
10
- It should work on Linux, Mac, and Windows. Prebuilt binaries are available for x64 architectures.
12
+ It should work on Linux, Mac, and Windows.
13
+ Prebuilt binaries are available for x64 architectures.
11
14
 
12
15
 
13
16
  ## Installation
14
17
 
15
- SDL2 is bundled along with the binaries so a separate installation is not necessary. This package is self-contained. Just run:
18
+ SDL2 is bundled along with the binaries so a separate installation is not necessary.
19
+ This package is self-contained.
20
+ Just run:
16
21
 
17
22
  ```
18
23
  npm install @kmamal/sdl
@@ -24,9 +29,20 @@ npm install @kmamal/sdl
24
29
  ## Example
25
30
 
26
31
  ```js
27
- import sdl from '@kmamal/sdl'
28
-
29
- sdl.video.createWindow({ title: "Hello, World!" })
32
+ const sdl = require('@kmamal/sdl')
33
+ const createContext = require('@kmamal/gl')
34
+
35
+ const window = sdl.video.createWindow({
36
+ title: "Hello, World!"
37
+ opengl: true,
38
+ })
39
+
40
+ // Clear screen to red
41
+ const { width, height, native } = window
42
+ const gl = createContext(width, height, { window: native })
43
+ gl.clearColor(1, 0, 0, 1)
44
+ gl.clear(gl.COLOR_BUFFER_BIT)
45
+ gl.swap()
30
46
  ```
31
47
 
32
48
  There are more examples [in the `examples/` folder](https://github.com/kmamal/node-sdl/tree/master/examples), covering several common use cases, including how to draw using Canvas and WebGL, as well as how to package the application for distribution.
@@ -92,6 +108,10 @@ There are more examples [in the `examples/` folder](https://github.com/kmamal/no
92
108
  * [window.setResizable(resizable)](#windowsetresizableresizable)
93
109
  * [window.borderless](#windowborderless)
94
110
  * [window.setBorderless(borderless)](#windowsetborderlessborderless)
111
+ * [window.accelerated](#windowaccelerated)
112
+ * [window.setAccelerated(accelerated)](#windowsetacceleratedaccelerated)
113
+ * [window.vsync](#windowvsync)
114
+ * [window.setVsync(vsync)](#windowsetvsyncvsync)
95
115
  * [window.opengl](#windowopengl)
96
116
  * [window.native](#windownative)
97
117
  * [window.maximized](#windowmaximized)
@@ -173,11 +193,15 @@ There are more examples [in the `examples/` folder](https://github.com/kmamal/no
173
193
 
174
194
  * `prevent: <function (void) => void>` Call this to prevent the application from closing.
175
195
 
176
- Fired to indicate that the user has requested the application to close (usually by closing the last window). If you need to display any confirmation dialogs you should call `event.prevent()` and handle termination manually. If `prevent` is not called, then this event will be followed by a [`'quit'`](#event-quit) event.
196
+ Fired to indicate that the user has requested the application to close (usually by closing the last window).
197
+ If you need to display any confirmation dialogs you should call `event.prevent()` and handle termination manually.
198
+ If `prevent` is not called, then this event will be followed by a [`'quit'`](#event-quit) event.
177
199
 
178
200
  ### Event: 'quit'
179
201
 
180
- Indicates that the application is about to close. Handle any cleanup here. This event will be followed by a call to `process.exit()`.
202
+ Indicates that the application is about to close.
203
+ Handle any cleanup here.
204
+ This event will be followed by a call to `process.exit()`.
181
205
 
182
206
  ### sdl.info
183
207
 
@@ -196,7 +220,9 @@ Indicates that the application is about to close. Handle any cleanup here. This
196
220
  * `all: <string>[]` A list of all audio drivers.
197
221
  * `current: <string>` The audio driver that is currently selected.
198
222
 
199
- This object is filled with the information produced during the initialization of SDL2. All the values will remain constant throughout the execution of the program. If you want to initialize SDL2 with drivers other than the default ones, you can do so via its [environment variables](https://wiki.libsdl.org/FAQUsingSDL).
223
+ This object is filled with the information produced during the initialization of SDL2.
224
+ All the values will remain constant throughout the execution of the program.
225
+ If you want to initialize SDL2 with drivers other than the default ones, you can do so via its [environment variables](https://wiki.libsdl.org/FAQUsingSDL).
200
226
 
201
227
  Sample data for Ubuntu:
202
228
 
@@ -315,7 +341,9 @@ String values used to represent how the pixels of an image are stored in a Buffe
315
341
  * `vertical: <number>` The vertical density.
316
342
  * `diagonal: <number>` The diagonal density.
317
343
 
318
- A list of all detected displays. Sample output for two side-to-side monitors is below. Notice how the geometries don't overlap:
344
+ A list of all detected displays.
345
+ Sample output for two side-to-side monitors is below.
346
+ Notice how the geometries don't overlap:
319
347
 
320
348
  ```js
321
349
  [
@@ -369,6 +397,8 @@ The window that the mouse is hovered over, or `null` if the mouse is not over a
369
397
  * `fullscreen: <boolean>` Set to `true` to create the window in fullscreen mode. Default: `false`
370
398
  * `resizable: <boolean>` Set to `true` to allow resizing the window by dragging its borders. Default: `false`
371
399
  * `borderless: <boolean>` Set to `true` to completely hide the window's borders and title bar. Default: `false`
400
+ * `accelerated: <boolean>` Set to `false` to disable hardware accelerated rendering. Default: `true`
401
+ * `vsync: <boolean>` Set to `false` to disable frame rate synchronization. Default: `true`
372
402
  * `opengl: <boolean>` Set to `true` to create an OpenGL-compatible window (for use with [@kmamal/gl](https://github.com/kmamal/headless-gl#readme)). Default: `false`
373
403
  * Returns: [`<Window>`](#class-window) an object representing the new window.
374
404
 
@@ -377,12 +407,15 @@ Creates a new window.
377
407
  The following restrictions apply:
378
408
  * If you specify the `display` option, you can't also specify the `x` or `y` options, and vice-versa.
379
409
  * The `resizable` and `borderless` options can't both be true.
410
+ * The `vsync` option only applies to windows that are also `accelerated`.
380
411
 
381
- If you set the `opengl` option, then you can only render to the window with OpenGL calls. Calls to [`render()`](#windowrenderwidth-height-stride-format-buffer) will fail.
412
+ If you set the `opengl` option, then you can only render to the window with OpenGL calls.
413
+ Calls to [`render()`](#windowrenderwidth-height-stride-format-buffer) will fail.
382
414
 
383
415
  ## class Window
384
416
 
385
- This class is not directly exposed by the API so you can't use it with the `new` operator. Instead, objects returned by [`sdl.video.createWindow()`](#sdlvideocreatewindowoptions) are of this type.
417
+ This class is not directly exposed by the API so you can't use it with the `new` operator.
418
+ Instead, objects returned by [`sdl.video.createWindow()`](#sdlvideocreatewindowoptions) are of this type.
386
419
 
387
420
  ### Event: 'show'
388
421
 
@@ -442,11 +475,15 @@ Fired when the mouse leaves the window.
442
475
 
443
476
  * `prevent: <function (void) => void>` Call this to prevent the window from closing.
444
477
 
445
- Fired to indicate that the user has requested the window to close (usually by clicking the "x" button). If you need to display any confirmation dialogs you should call `event.prevent()` and handle destruction manually. If `prevent` is not called, then this event will be followed by a `'close'` event.
478
+ Fired to indicate that the user has requested the window to close (usually by clicking the "x" button).
479
+ If you need to display any confirmation dialogs you should call `event.prevent()` and handle destruction manually.
480
+ If `prevent` is not called, then this event will be followed by a [`'close'`](#event-close) event.
446
481
 
447
482
  ### Event: 'close'
448
483
 
449
- Indicates that the window is about to be destroyed. Handle any cleanup here. This event will be followed by a call to `window.destroy()`.
484
+ Indicates that the window is about to be destroyed.
485
+ Handle any cleanup here.
486
+ This event will be followed by a call to [`window.destroy()`](#windowdestroy).
450
487
 
451
488
  ### Event: 'keyDown'
452
489
 
@@ -604,7 +641,8 @@ Will be `true` if the window is visible.
604
641
 
605
642
  ### window.show([show])
606
643
 
607
- * `show: <boolean>` Set to `true` to make the window visible, `false` to hide it. Default: `true`
644
+ * `show: <boolean>` Set to `true` to make the window visible, `false` to hide it.
645
+ Default: `true`
608
646
 
609
647
  Shows or hides the window.
610
648
 
@@ -616,7 +654,8 @@ Equivalent to [`window.show(false)`](#windowshowshow).
616
654
 
617
655
  * `<boolean>`
618
656
 
619
- Will be `true` if the window is fullscreen. A fullscreen window is displayed over the entire screen.
657
+ Will be `true` if the window is fullscreen.
658
+ A fullscreen window is displayed over the entire screen.
620
659
 
621
660
  ### window.setFullscreen(fullscreen)
622
661
 
@@ -628,7 +667,8 @@ Changes the window's fullscreen property.
628
667
 
629
668
  * `<boolean>`
630
669
 
631
- Will be `true` if the window is resizable. A resizable window can be resized by dragging it's borders.
670
+ Will be `true` if the window is resizable.
671
+ A resizable window can be resized by dragging it's borders.
632
672
 
633
673
  ### window.setResizable(resizable)
634
674
 
@@ -640,7 +680,8 @@ Changes the window's resizable property.
640
680
 
641
681
  * `<boolean>`
642
682
 
643
- Will be `true` if the window is borderless. A borderless window has no borders or title bar.
683
+ Will be `true` if the window is borderless.
684
+ A borderless window has no borders or title bar.
644
685
 
645
686
  ### window.setBorderless(borderless)
646
687
 
@@ -648,17 +689,46 @@ Will be `true` if the window is borderless. A borderless window has no borders o
648
689
 
649
690
  Changes the window's borderless property.
650
691
 
692
+ ### window.accelerated
693
+
694
+ * `<boolean>`
695
+
696
+ Will be `true` if the window is using hardware accelerated rendering.
697
+
698
+ ### window.setAccelerated(accelerated)
699
+
700
+ * `accelerated: <boolean>` The new value of the property.
701
+
702
+ Changes the window's accelerated property.
703
+
704
+ ### window.vsync
705
+
706
+ * `<boolean>`
707
+
708
+ Will be `true` if the window is using vsync.
709
+ A window with vsync enabled will have its frame rate synchronized to the display's refresh rate to prevent tearing.
710
+ Note that vsync can only be used if that window is also [`accelerated`](#windowaccelerated)
711
+
712
+ ### window.setVsync(vsync)
713
+
714
+ * `vsync: <boolean>` The new value of the property.
715
+
716
+ Changes the window's vsync property.
717
+
651
718
  ### window.opengl
652
719
 
653
720
  * `<boolean>`
654
721
 
655
- Will be `true` if the window was created in OpenGl mode. In OpenGL mode, you can only render to the window with OpenGL calls. Calls to [`render()`](#windowrenderwidth-height-stride-format-buffer) will fail.
722
+ Will be `true` if the window was created in OpenGl mode.
723
+ In OpenGL mode, you can only render to the window with OpenGL calls.
724
+ Calls to [`render()`](#windowrenderwidth-height-stride-format-buffer) will fail.
656
725
 
657
726
  ### window.native
658
727
 
659
728
  * `<any>`
660
729
 
661
- Holds a copy of the native (platform-specific) representation of the window. Only used for passing to [@kmamal/gl](https://github.com/kmamal/headless-gl#readme).
730
+ Holds a copy of the native (platform-specific) representation of the window.
731
+ Only used for passing to [@kmamal/gl](https://github.com/kmamal/headless-gl#readme).
662
732
 
663
733
  ### window.maximized
664
734
 
@@ -704,7 +774,8 @@ Will be `true` if the mouse is over the window.
704
774
 
705
775
  * `width, height, stride, format, buffer: `[`<Image>`](#image-data) The image to display on the window.
706
776
 
707
- Displays an image in the window. This functions preforms no scaling (or positioning). To fill the whole window you must provide an image that matches the window's `width` and `height`.
777
+ Displays an image in the window.
778
+ The provided image will be stretched over the entire window.
708
779
 
709
780
  ### window.setIcon(width, height, stride, format, buffer)
710
781
 
@@ -716,7 +787,8 @@ Set's the window's icon, usually displayed in the title bar and the taskbar.
716
787
 
717
788
  * `<boolean>`
718
789
 
719
- Will be `true` if the window is destroyed. A destroyed window object should not be used any further.
790
+ Will be `true` if the window is destroyed.
791
+ A destroyed window object should not be used any further.
720
792
 
721
793
  ### window.destroy()
722
794
 
@@ -727,9 +799,14 @@ Destroys the window.
727
799
 
728
800
  ### Audio data
729
801
 
730
- The [`playbackInstance.enqueue()`](#playbackinstanceenqueuebuffer-bytes) function expects a buffer of audio data as input and the [`recordingInstance.dequeue()`](#recordinginstancedequeuebuffer-bytes) function returns a buffer of audio data as output. The format of the data in these buffers depends on the options you passed to [`sdl.audio.openDevice()`](#sdlaudioopendevicedevice-options).
802
+ The [`playbackInstance.enqueue()`](#playbackinstanceenqueuebuffer-bytes) function expects a buffer of audio data as input and the [`recordingInstance.dequeue()`](#recordinginstancedequeuebuffer-bytes) function returns a buffer of audio data as output.
803
+ The format of the data in these buffers depends on the options you passed to [`sdl.audio.openDevice()`](#sdlaudioopendevicedevice-options).
731
804
 
732
- Audio data are a sequence of frames, with each frame being a sequence of samples. A _sample_ is a single number representing the intensity of a single audio channel at a specific point in time. For audio with multiple channels, each point in time is represented by one sample per channel. This is called a _frame_. The samples in a frame are arranged as follows:
805
+ Audio data are a sequence of frames, with each frame being a sequence of samples.
806
+ A _sample_ is a single number representing the intensity of a single audio channel at a specific point in time.
807
+ For audio with multiple channels, each point in time is represented by one sample per channel.
808
+ This is called a _frame_.
809
+ The samples in a frame are arranged as follows:
733
810
  * For 1 channel (mono) a frame contains just the one sample.
734
811
  * For 2 channels (stereo) the frame's first sample will be the one for the left channel, followed by the one for the right channel.
735
812
  * For 4 channels (quad) the layout is front-left, front-right, rear-left, rear-right.
@@ -802,13 +879,16 @@ String values used to represent how audio samples are stored in a Buffer.
802
879
 
803
880
  * `device: <object>`: An object from [`sdl.audio.devices`](#sdlaudioinstances) indicating the device that caused the event.
804
881
 
805
- Fired when a new audio device becomes available. Check [`sdl.audio.devices`](#sdlaudioinstances) to get the new list of audio devices.
882
+ Fired when a new audio device becomes available.
883
+ Check [`sdl.audio.devices`](#sdlaudioinstances) to get the new list of audio devices.
806
884
 
807
885
  ### Event: 'deviceRemove'
808
886
 
809
887
  * `device: <object>`: An object from [`sdl.audio.devices`](#sdlaudioinstances) indicating the device that caused the event.
810
888
 
811
- Fired when an existing audio device is removed. Check [`sdl.audio.devices`](#sdlaudioinstances) to get the new list of audio devices. When this event is emitted, all instances that were opened from the removed device are closed automatically.
889
+ Fired when an existing audio device is removed.
890
+ Check [`sdl.audio.devices`](#sdlaudioinstances) to get the new list of audio devices.
891
+ When this event is emitted, all instances that were opened from the removed device are closed automatically.
812
892
 
813
893
  ### sdl.audio.bytesPerSample(format)
814
894
 
@@ -845,7 +925,8 @@ Helper function which maps each sample format to the sample value that correspon
845
925
  * `offset: <number>` The position from which to read the sample. Default: `0`
846
926
  * Returns: `<number>` The value of the sample read.
847
927
 
848
- Helper function which calls the appropriate `read*` method of `Buffer` based on the format argument. For example, a call to `sdl.audio.readSample('f32', buffer, offset)` would be equivalent to `buffer.readFloatLE(offset)`.
928
+ Helper function which calls the appropriate `read*` method of `Buffer` based on the format argument.
929
+ For example, a call to `sdl.audio.readSample('f32', buffer, offset)` would be equivalent to `buffer.readFloatLE(offset)`.
849
930
 
850
931
  ### sdl.audio.writeSample(format, buffer, value[, offset])
851
932
 
@@ -855,7 +936,8 @@ Helper function which calls the appropriate `read*` method of `Buffer` based on
855
936
  * `offset: <number>` The position at which to write the sample. Default: `0`
856
937
  * Returns: `<number>` The updated `offset`.
857
938
 
858
- Helper function which calls the appropriate `write*` method of `Buffer` based on the format argument. For example, a call to `sdl.audio.writeSample('f32', buffer, value, offset)` would be equivalent to `buffer.writeFloatLE(value, offset)`.
939
+ Helper function which calls the appropriate `write*` method of `Buffer` based on the format argument.
940
+ For example, a call to `sdl.audio.writeSample('f32', buffer, value, offset)` would be equivalent to `buffer.writeFloatLE(value, offset)`.
859
941
 
860
942
  ### sdl.audio.devices
861
943
 
@@ -863,7 +945,8 @@ Helper function which calls the appropriate `write*` method of `Buffer` based on
863
945
  * `name: <string>` The name of the device.
864
946
  * `recording: <boolean>` Will be `true` if this is a recording device.
865
947
 
866
- A list of all the detected audio devices. Sample output for PulseAudio:
948
+ A list of all the detected audio devices.
949
+ Sample output for PulseAudio:
867
950
 
868
951
  ```js
869
952
  [
@@ -883,16 +966,21 @@ A list of all the detected audio devices. Sample output for PulseAudio:
883
966
  * `buffered: <number>`: Number of frames that will be buffered by the driver. Must be a power of `2`. Default `4096`.
884
967
  * Returns: [`<AudioInstance>`](#class-audioinstance) an object representing the opened audio device instance.
885
968
 
886
- Initializes an audio device for playback/recording and returns a corresponding instance. If the opened device is a playback device, then the returned object will be an [`AudioPlaybackInstance`](#class-audioplaybackinstance-extends-audioinstance), otherwise it will be an [`AudioRecordingInstance`](#class-audiorecordinginstance-extends-audioinstance).
969
+ Initializes an audio device for playback/recording and returns a corresponding instance.
970
+ If the opened device is a playback device, then the returned object will be an [`AudioPlaybackInstance`](#class-audioplaybackinstance-extends-audioinstance), otherwise it will be an [`AudioRecordingInstance`](#class-audiorecordinginstance-extends-audioinstance).
887
971
 
888
- The `channels`, `frequency` and `format` options together define what the data in the `Buffer` objects you read from or write to the instance will look like. See also the section on [audio data](#audio-data).
972
+ The `channels`, `frequency` and `format` options together define what the data in the `Buffer` objects you read from or write to the instance will look like.
973
+ See also the section on [audio data](#audio-data).
889
974
 
890
- The `buffered` option specifies the "delay" that you will experience between the application and the audio driver. With smaller values you will have smaller delays, but you will also have to read/write data from/to the driver more frequently. Applications such as virtual instruments that need to play audio in reaction to user input will want to change this option to lower values.
975
+ The `buffered` option specifies the "delay" that you will experience between the application and the audio driver.
976
+ With smaller values you will have smaller delays, but you will also have to read/write data from/to the driver more frequently.
977
+ Applications such as virtual instruments that need to play audio in reaction to user input will want to change this option to lower values.
891
978
 
892
979
 
893
980
  ## class AudioInstance
894
981
 
895
- This class is not directly exposed by the API so you can't use it with the `new` operator. It only serves as the base class for [`AudioPlaybackInstance`](#class-audioplaybackinstance-extends-audioinstance) and [`AudioRecordingInstance`](#class-audiorecordinginstance-extends-audioinstance).
982
+ This class is not directly exposed by the API so you can't use it with the `new` operator.
983
+ It only serves as the base class for [`AudioPlaybackInstance`](#class-audioplaybackinstance-extends-audioinstance) and [`AudioRecordingInstance`](#class-audiorecordinginstance-extends-audioinstance).
896
984
 
897
985
  ### audioInstance.id
898
986
 
@@ -960,7 +1048,8 @@ The sample value that corresponds to silence, based on the format the instance w
960
1048
  * `offset: <number>` The position from which to read the sample. Default: `0`
961
1049
  * Returns: `<number>` The value of the sample read.
962
1050
 
963
- Helper function which calls the appropriate `read*` method of `Buffer` based on the format the instance was opened with. For example, for an instance opened with the `'f32'` sample format, a call to `audioinstance.readSample(buffer, offset)` would be equivalent to `buffer.readFloatLE(offset)`.
1051
+ Helper function which calls the appropriate `read*` method of `Buffer` based on the format the instance was opened with.
1052
+ For example, for an instance opened with the `'f32'` sample format, a call to `audioinstance.readSample(buffer, offset)` would be equivalent to `buffer.readFloatLE(offset)`.
964
1053
 
965
1054
  ### audioInstance.writeSample(buffer, value[, offset])
966
1055
 
@@ -969,7 +1058,8 @@ Helper function which calls the appropriate `read*` method of `Buffer` based on
969
1058
  * `offset: <number>` The position at which to write the sample. Default: `0`
970
1059
  * Returns: `<number>` The updated `offset`.
971
1060
 
972
- Helper function which calls the appropriate `write*` method of `Buffer` based on the format the instance was opened with. For example, for an instance opened with the `'f32'` sample format, a call to `audioinstance.writeSample(buffer, value, offset)` would be equivalent to `buffer.writeFloatLE(value, offset)`.
1061
+ Helper function which calls the appropriate `write*` method of `Buffer` based on the format the instance was opened with.
1062
+ For example, for an instance opened with the `'f32'` sample format, a call to `audioinstance.writeSample(buffer, value, offset)` would be equivalent to `buffer.writeFloatLE(value, offset)`.
973
1063
 
974
1064
  ### audioInstance.buffered
975
1065
 
@@ -1007,7 +1097,8 @@ Clears the queued data.
1007
1097
 
1008
1098
  * `<boolean>`
1009
1099
 
1010
- Will be `true` if the instance is closed. A closed instance object should not be used any further.
1100
+ Will be `true` if the instance is closed.
1101
+ A closed instance object should not be used any further.
1011
1102
 
1012
1103
  ### audioInstance.close()
1013
1104
 
@@ -1016,7 +1107,8 @@ Closes the instance.
1016
1107
 
1017
1108
  ## class AudioPlaybackInstance extends AudioInstance
1018
1109
 
1019
- This class is not directly exposed by the API so you can't use it with the `new` operator. Instead, objects returned by [`sdl.audio.openDevice()`](#sdlaudioopendevicedevice-options) are of this type, if a playback device is opened.
1110
+ This class is not directly exposed by the API so you can't use it with the `new` operator.
1111
+ Instead, objects returned by [`sdl.audio.openDevice()`](#sdlaudioopendevicedevice-options) are of this type, if a playback device is opened.
1020
1112
 
1021
1113
  ### playbackInstance.enqueue(buffer[, bytes])
1022
1114
 
@@ -1028,7 +1120,8 @@ Takes generated audio data from the buffer, and writes it to the queue, from whi
1028
1120
 
1029
1121
  ## class AudioRecordingInstance extends AudioInstance
1030
1122
 
1031
- This class is not directly exposed by the API so you can't use it with the `new` operator. Instead, objects returned by [`sdl.audio.openDevice()`](#sdlaudioopendevicedevice-options) are of this type, if a recording device is opened.
1123
+ This class is not directly exposed by the API so you can't use it with the `new` operator.
1124
+ Instead, objects returned by [`sdl.audio.openDevice()`](#sdlaudioopendevicedevice-options) are of this type, if a recording device is opened.
1032
1125
 
1033
1126
  ### recordingInstance.dequeue(buffer[, bytes])
1034
1127
 
@@ -1043,20 +1136,32 @@ Takes recorded audio data that has been put on the queue, and writes it to the p
1043
1136
 
1044
1137
  There are three levels at which you can deal with the keyboard: physical keys ([scancodes](#enum-scancode)), virtual keys ([keys](#virtual-keys)), and text ([`'textInput'`](#event-textinput) events).
1045
1138
 
1046
- On the physical level, each of the physical keys corresponds to a number: the key's scancode. For any given keyboard, the same key will always produce the same scancode. If your application cares about the layout of the keyboard (for example using the "WASD" keys as a substitute for arrow keys), then you should handle key events at this level using the `scancode` property of [`'keyDown'`](#event-keydown) and [`'keyUp'`](#event-keyup) events.
1139
+ On the physical level, each of the physical keys corresponds to a number: the key's scancode.
1140
+ For any given keyboard, the same key will always produce the same scancode.
1141
+ If your application cares about the layout of the keyboard (for example using the "WASD" keys as a substitute for arrow keys), then you should handle key events at this level using the `scancode` property of [`'keyDown'`](#event-keydown) and [`'keyUp'`](#event-keyup) events.
1047
1142
 
1048
1143
  For the most part it's better to treat scancode values as arbitrary/meaningless, but SDL does provide a scancode enumeration with values based on the [USB usage page standard](https://www.usb.org/sites/default/files/documents/hut1_12v2.pdf) so you should be able to derive some meaning from the scancodes if your keyboard is compatible.
1049
1144
 
1050
- More commonly, you don't care about the physical key itself but about the "meaning" associated with each key: the character that it produces ("a", "b", "@", " ", .e.t.c) or the function that it corresponds to ("Esc", "F4", "Ctrl", e.t.c.). Your operating system provides a "keyboard mapping" that associates physical keys with their corresponding meaning. Changing the keyboard mapping (for example by changing the language from English to German) will also change the corresponding meaning for each key (in the English-German example: the "y" and "z" keys will be switched). These meanings are represented as [virtual key strings](#virtual-keys). If your application cares about the meaning associated with individual keys then you should handle key events at this level using the `key` property of [`'keyDown'`](#event-keydown) and [`'keyUp'`](#event-keyup) events.
1145
+ More commonly, you don't care about the physical key itself but about the "meaning" associated with each key: the character that it produces ("a", "b", "@", " ", .e.t.c) or the function that it corresponds to ("Esc", "F4", "Ctrl", e.t.c.).
1146
+ Your operating system provides a "keyboard mapping" that associates physical keys with their corresponding meaning.
1147
+ Changing the keyboard mapping (for example by changing the language from English to German) will also change the corresponding meaning for each key (in the English-German example: the "y" and "z" keys will be switched).
1148
+ These meanings are represented as [virtual key strings](#virtual-keys).
1149
+ If your application cares about the meaning associated with individual keys then you should handle key events at this level using the `key` property of [`'keyDown'`](#event-keydown) and [`'keyUp'`](#event-keyup) events.
1051
1150
 
1052
- Note that not all physical keys correspond to a well-defined meaning and thus don't have a virtual key value associated with them. The key events for these keys will have a `null` value for the `key` property.
1151
+ Note that not all physical keys correspond to a well-defined meaning and thus don't have a virtual key value associated with them.
1152
+ The key events for these keys will have a `null` value for the `key` property.
1053
1153
 
1054
- But sometimes the application doesn't care about individual keys at all, but about the resulting text that the user is entering. Consider for example what happens when a user on a Greek keyboard layout enters an accent mark "´" followed by the letter "α" to produce the character "ά": Two keys were pressed, but only a single character was produced. Trying to handle text input by manually translating key presses to text is not a very viable solution. It's better to let the OS handle all the text logic, and get the final text by handling the rasulting ([`'textInput'`](#event-textinput)) events.
1154
+ But sometimes the application doesn't care about individual keys at all, but about the resulting text that the user is entering.
1155
+ Consider for example what happens when a user on a Greek keyboard layout enters an accent mark "´" followed by the letter "α" to produce the character "ά": Two keys were pressed, but only a single character was produced.
1156
+ Trying to handle text input by manually translating key presses to text is not a very viable solution.
1157
+ It's better to let the OS handle all the text logic, and get the final text by handling the rasulting ([`'textInput'`](#event-textinput)) events.
1055
1158
 
1056
1159
 
1057
1160
  ### Enum: SCANCODE
1058
1161
 
1059
- Used to represent physical keys on the keyboard. The same key will always produce the same scancode. Values are based on the [USB usage page standard](https://www.usb.org/sites/default/files/documents/hut1_12v2.pdf).
1162
+ Used to represent physical keys on the keyboard.
1163
+ The same key will always produce the same scancode.
1164
+ Values are based on the [USB usage page standard](https://www.usb.org/sites/default/files/documents/hut1_12v2.pdf).
1060
1165
 
1061
1166
  | Value | Corresponding `SDL_Scancode` | Comment |
1062
1167
  | --- | --- | --- |
@@ -1306,7 +1411,11 @@ Used to represent physical keys on the keyboard. The same key will always produc
1306
1411
 
1307
1412
  ### Virtual keys
1308
1413
 
1309
- String values used to represent virtual keys in the context of the current keyboard mapping. Note that some keys do not correspond to any virtual key. A Key can be either one of the values below __or__ any unicode character. Keys that produce characters are represented by that character. All others are represented by one of these values:
1414
+ String values used to represent virtual keys in the context of the current keyboard mapping.
1415
+ Note that some keys do not correspond to any virtual key.
1416
+ A Key can be either one of the values below __or__ any unicode character.
1417
+ Keys that produce characters are represented by that character.
1418
+ All others are represented by one of these values:
1310
1419
 
1311
1420
  `'&&'`, `'+/-'`, `'||'`, `'00'`, `'000'`, `'again'`, `'alt'`, `'altErase'`, `'app1'`, `'app2'`, `'application'`, `'audioFastForward'`, `'audioMute'`, `'audioNext'`, `'audioPlay'`, `'audioPrev'`, `'audioRewind'`, `'audioStop'`, `'back'`, `'backspace'`, `'binary'`, `'bookmarks'`, `'brightnessDown'`, `'brightnessUp'`, `'calculator'`, `'cancel'`, `'capsLock'`, `'clear'`, `'clearEntry'`, `'computer'`, `'copy'`, `'crSel'`, `'ctrl'`, `'currencySubUnit'`, `'currencyUnit'`, `'cut'`, `'decimal'`, `'decimalSeparator'`, `'delete'`, `'displaySwitch'`, `'down'`, `'eject'`, `'end'`, `'enter'`, `'escape'`, `'execute'`, `'exSel'`, `'f1'`, `'f2'`, `'f3'`, `'f4'`, `'f5'`, `'f6'`, `'f7'`, `'f8'`, `'f9'`, `'f10'`, `'f11'`, `'f12'`, `'f13'`, `'f14'`, `'f15'`, `'f16'`, `'f17'`, `'f18'`, `'f19'`, `'f20'`, `'f21'`, `'f22'`, `'f23'`, `'f24'`, `'find'`, `'forward'`, `'gui'`, `'help'`, `'hexadecimal'`, `'home'`, `'illumDown'`, `'illumToggle'`, `'illumUp'`, `'insert'`, `'left'`, `'mail'`, `'mediaSelect'`, `'memAdd'`, `'memClear'`, `'memDivide'`, `'memMultiply'`, `'memRecall'`, `'memStore'`, `'memSubtract'`, `'menu'`, `'modeSwitch'`, `'mute'`, `'numlock'`, `'octal'`, `'oper'`, `'out'`, `'pageDown'`, `'pageUp'`, `'paste'`, `'pause'`, `'power'`, `'printScreen'`, `'prior'`, `'refresh'`, `'return'`, `'right'`, `'scrollLock'`, `'search'`, `'select'`, `'separator'`, `'shift'`, `'sleep'`, `'space'`, `'stop'`, `'sysReq'`, `'tab'`, `'thousandsSeparator'`, `'undo'`, `'up'`, `'volumeDown'`, `'volumeUp'`, `'www'`, `'xor'`.
1312
1421
 
@@ -1322,13 +1431,15 @@ Maps a scancode to the corresponding key based on the current keyboard mapping.
1322
1431
  * `key: `[`<Key>`](#virtual-keys)
1323
1432
  * Returns: [`<Scancode>`](#enum-scancode)
1324
1433
 
1325
- Maps a key to the corresponding scancode based on the current keyboard mapping. If multiple physical keys produce the same virtual key, then only the first one will be returned.
1434
+ Maps a key to the corresponding scancode based on the current keyboard mapping.
1435
+ If multiple physical keys produce the same virtual key, then only the first one will be returned.
1326
1436
 
1327
1437
  ### sdl.keyboard.getState()
1328
1438
 
1329
1439
  * Returns: `<boolean[]>` an object representing the state of each key.
1330
1440
 
1331
- The returned array can be indexed with [`Scancode`](#enum-scancode) values. Pressed keys will correspond to `true` values.
1441
+ The returned array can be indexed with [`Scancode`](#enum-scancode) values.
1442
+ Pressed keys will correspond to `true` values.
1332
1443
 
1333
1444
 
1334
1445
  ## sdl.mouse
@@ -1354,7 +1465,8 @@ String values used to represent the types of cursors available on most systems.
1354
1465
 
1355
1466
  ### Enum: BUTTON
1356
1467
 
1357
- Used to represent the buttons on a mouse. A mouse can have many buttons, but the values for the three most common ones are represented in this enum.
1468
+ Used to represent the buttons on a mouse.
1469
+ A mouse can have many buttons, but the values for the three most common ones are represented in this enum.
1358
1470
 
1359
1471
  | Value | Corresponding `SDL_BUTTON_*` |
1360
1472
  | --- | --- |
@@ -1396,11 +1508,13 @@ Changes the icon that is displayed for the mouse cursor.
1396
1508
  * `x: <number>` The x position of the cursor image's hotspot.
1397
1509
  * `y: <number>` The y position of the cursor image's hotspot.
1398
1510
 
1399
- Sets a image to be the mouse cursor. The hotspot represents the pixel that is considered to be under the mouse.
1511
+ Sets a image to be the mouse cursor.
1512
+ The hotspot represents the pixel that is considered to be under the mouse.
1400
1513
 
1401
1514
  ### sdl.mouse.showCursor([show])
1402
1515
 
1403
- * `show: <boolean>` If `true` then the mouse cursor will be visible. Default `true`.
1516
+ * `show: <boolean>` If `true` then the mouse cursor will be visible.
1517
+ Default `true`.
1404
1518
 
1405
1519
  Changes the visibility of the mouse cursor.
1406
1520
 
@@ -1410,7 +1524,8 @@ Equivalent to [`sdl.mouse.showCursor(false)`](#sdlmouseshowcursorshow).
1410
1524
 
1411
1525
  ### sdl.mouse.capture([capture])
1412
1526
 
1413
- * `capture: <boolean>` If `true` the mouse will be captured by the current window. Default `true`.
1527
+ * `capture: <boolean>` If `true` the mouse will be captured by the current window.
1528
+ Default `true`.
1414
1529
 
1415
1530
  When the mouse has been captured you will continue receiving mouse events even if the mouse is not over a window.
1416
1531
 
@@ -1423,7 +1538,8 @@ Equivalent to [`sdl.mouse.capture(false)`](#sdlmousecapturecapture).
1423
1538
 
1424
1539
  ### Event: 'update'
1425
1540
 
1426
- Fired when the contents of the clipboard have changed. Check [`sdl.clipboard.text`](#sdlclipboardtext) to get the new contents of the clipboard.
1541
+ Fired when the contents of the clipboard have changed.
1542
+ Check [`sdl.clipboard.text`](#sdlclipboardtext) to get the new contents of the clipboard.
1427
1543
 
1428
1544
  ### sdl.clipboard.text
1429
1545
 
@@ -1446,7 +1562,8 @@ Changes the text contents of the clipboard.
1446
1562
 
1447
1563
  This should not be necessary, since this package bundles the SDL2 runtime library along with its binaries, but you never know...
1448
1564
 
1449
- This package depends on SDL2 `>=2.0.5`. To install it on your system you might have to download one of the [SDL2 Runtime Binaries](https://www.libsdl.org/download-2.0.php).
1565
+ This package depends on SDL2 `>=2.0.5`.
1566
+ To install it on your system you might have to download one of the [SDL2 Runtime Binaries](https://www.libsdl.org/download-2.0.php).
1450
1567
  * __Linux:__ Run `sudo apt install libsdl2-2.0-0` or whatever the equivalent command is for your package manager.
1451
1568
  * __Mac:__ Download the `.dmg` file, open it, and copy the `SDL2.framework` to `/Library/Frameworks`.
1452
1569
  * __Windows:__ Download the `.zip` file, extract it, and copy the `.dll` files to a path Node.js can link them from. This will usually be your system directory (`C:\Windows`) but see [here](https://docs.microsoft.com/en-us/windows/win32/dlls/dynamic-link-library-search-order) for other paths where Windows will look for `.dll` files to link.
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "0.3.7",
2
+ "version": "0.4.0",
3
3
  "name": "@kmamal/sdl",
4
4
  "description": "SDL2 bindings for Node.js",
5
5
  "keywords": [
@@ -93,6 +93,7 @@ class AudioInstance {
93
93
 
94
94
  return Bindings.audio_getQueueSize(this._id)
95
95
  }
96
+
96
97
  clearQueue () {
97
98
  if (this._closed) { throw Object.assign(new Error("instance is closed"), { id: this._id }) }
98
99
 
@@ -45,6 +45,8 @@ class Window extends EventsViaPoll {
45
45
  fullscreen = false,
46
46
  resizable = false,
47
47
  borderless = false,
48
+ accelerated = true,
49
+ vsync = true,
48
50
  opengl = false,
49
51
  } = options
50
52
 
@@ -64,6 +66,8 @@ class Window extends EventsViaPoll {
64
66
  if (typeof fullscreen !== 'boolean') { throw Object.assign(new Error("fullscreen must be a boolean"), { fullscreen }) }
65
67
  if (typeof resizable !== 'boolean') { throw Object.assign(new Error("resizable must be a boolean"), { resizable }) }
66
68
  if (typeof borderless !== 'boolean') { throw Object.assign(new Error("borderless must be a boolean"), { borderless }) }
69
+ if (typeof accelerated !== 'boolean') { throw Object.assign(new Error("accelerated must be a boolean"), { accelerated }) }
70
+ if (typeof vsync !== 'boolean') { throw Object.assign(new Error("vsync must be a boolean"), { vsync }) }
67
71
  if (typeof opengl !== 'boolean') { throw Object.assign(new Error("opengl must be a boolean"), { opengl }) }
68
72
  if (display && (Number.isFinite(x) || Number.isFinite(y))) { throw Object.assign(new Error("display and x/y are mutually exclusive"), { display, x, y }) }
69
73
  if (resizable && borderless) { throw Object.assign(new Error("resizable and borderless are mutually exclusive"), { resizable, borderless }) }
@@ -87,6 +91,8 @@ class Window extends EventsViaPoll {
87
91
  fullscreen,
88
92
  resizable,
89
93
  borderless,
94
+ accelerated,
95
+ vsync,
90
96
  opengl,
91
97
  )
92
98
 
@@ -102,6 +108,8 @@ class Window extends EventsViaPoll {
102
108
  this._fullscreen = fullscreen
103
109
  this._resizable = resizable
104
110
  this._borderless = borderless
111
+ this._accelerated = accelerated
112
+ this._vsync = vsync
105
113
  this._opengl = opengl
106
114
 
107
115
  this._focused = false
@@ -112,8 +120,10 @@ class Window extends EventsViaPoll {
112
120
 
113
121
  Globals.windows.all.set(this._id, this)
114
122
 
123
+ // Keep node.js alive while windows are open
115
124
  this.once('close', () => {})
116
125
 
126
+ // Manually emit an initial resize event (for convenience)
117
127
  process.nextTick(() => {
118
128
  this.emit('resize', {
119
129
  width: this._width,
@@ -196,6 +206,24 @@ class Window extends EventsViaPoll {
196
206
  Bindings.window_setBorderless(this._id, borderless)
197
207
  }
198
208
 
209
+ get accelerated () { return this._accelerated }
210
+ setAccelerated (accelerated) {
211
+ if (this._destroyed) { throw Object.assign(new Error("window is destroyed"), { id: this._id }) }
212
+
213
+ if (typeof accelerated !== 'boolean') { throw Object.assign(new Error("accelerated must be a boolean"), { accelerated }) }
214
+ this._accelerated = accelerated
215
+ Bindings.window_setAcceleratedAndVsync(this._id, accelerated, this._vsync)
216
+ }
217
+
218
+ get vsync () { return this._vsync }
219
+ setVsync (vsync) {
220
+ if (this._destroyed) { throw Object.assign(new Error("window is destroyed"), { id: this._id }) }
221
+
222
+ if (typeof vsync !== 'boolean') { throw Object.assign(new Error("vsync must be a boolean"), { vsync }) }
223
+ this._vsync = vsync
224
+ Bindings.window_setAcceleratedAndVsync(this._id, this._accelerated, vsync)
225
+ }
226
+
199
227
  get opengl () { return this._opengl }
200
228
  get native () { return this._native }
201
229
 
@@ -231,7 +231,7 @@ window_create(napi_env env, napi_callback_info info)
231
231
  int * height = nullptr;
232
232
  void * native_pointer = nullptr;
233
233
 
234
- napi_value argv[11];
234
+ napi_value argv[13];
235
235
  size_t argc = sizeof(argv) / sizeof(napi_value);
236
236
  CALL_NAPI(napi_get_cb_info, info, &argc, argv, nullptr, nullptr);
237
237
 
@@ -269,19 +269,21 @@ window_create(napi_env env, napi_callback_info info)
269
269
  CALL_NAPI(napi_get_value_int32, argv[5], y);
270
270
  }
271
271
 
272
- bool visible, fullscreen, resizable, borderless, opengl;
272
+ bool visible, fullscreen, resizable, borderless, accelerated, vsync, opengl;
273
273
  CALL_NAPI(napi_get_value_bool, argv[6], &visible);
274
274
  CALL_NAPI(napi_get_value_bool, argv[7], &fullscreen);
275
275
  CALL_NAPI(napi_get_value_bool, argv[8], &resizable);
276
276
  CALL_NAPI(napi_get_value_bool, argv[9], &borderless);
277
- CALL_NAPI(napi_get_value_bool, argv[10], &opengl);
277
+ CALL_NAPI(napi_get_value_bool, argv[10], &accelerated);
278
+ CALL_NAPI(napi_get_value_bool, argv[11], &vsync);
279
+ CALL_NAPI(napi_get_value_bool, argv[12], &opengl);
278
280
 
279
281
  int window_id;
280
282
  int native_pointer_size;
281
283
  CALL_SDL_HELPER(window_create,
282
284
  title, display,
283
285
  &x, &y, &width, &height,
284
- visible, fullscreen, resizable, borderless, opengl,
286
+ visible, fullscreen, resizable, borderless, accelerated, vsync, opengl,
285
287
  &window_id, &native_pointer, &native_pointer_size
286
288
  );
287
289
 
@@ -437,10 +439,30 @@ window_setBorderless(napi_env env, napi_callback_info info)
437
439
  int window_id;
438
440
  CALL_NAPI(napi_get_value_int32, argv[0], &window_id);
439
441
 
440
- bool resizable;
441
- CALL_NAPI(napi_get_value_bool, argv[1], &resizable);
442
+ bool borderless;
443
+ CALL_NAPI(napi_get_value_bool, argv[1], &borderless);
444
+
445
+ CALL_SDL_HELPER(window_setBorderless, window_id, borderless);
446
+
447
+ cleanup:
448
+ return nullptr;
449
+ }
450
+
451
+ napi_value
452
+ window_setAcceleratedAndVsync(napi_env env, napi_callback_info info)
453
+ {
454
+ napi_value argv[3];
455
+ size_t argc = sizeof(argv) / sizeof(napi_value);
456
+ CALL_NAPI(napi_get_cb_info, info, &argc, argv, nullptr, nullptr);
457
+
458
+ int window_id;
459
+ CALL_NAPI(napi_get_value_int32, argv[0], &window_id);
460
+
461
+ bool accelerated, vsync;
462
+ CALL_NAPI(napi_get_value_bool, argv[1], &accelerated);
463
+ CALL_NAPI(napi_get_value_bool, argv[2], &vsync);
442
464
 
443
- CALL_SDL_HELPER(window_setBorderless, window_id, resizable);
465
+ CALL_SDL_HELPER(window_setAcceleratedAndVsync, window_id, accelerated, vsync);
444
466
 
445
467
  cleanup:
446
468
  return nullptr;
@@ -1188,6 +1210,7 @@ init (napi_env env, napi_value exports)
1188
1210
  { "window_setFullscreen", NULL, window_setFullscreen, NULL, NULL, NULL, napi_enumerable, NULL },
1189
1211
  { "window_setResizable", NULL, window_setResizable, NULL, NULL, NULL, napi_enumerable, NULL },
1190
1212
  { "window_setBorderless", NULL, window_setBorderless, NULL, NULL, NULL, napi_enumerable, NULL },
1213
+ { "window_setAcceleratedAndVsync", NULL, window_setAcceleratedAndVsync, NULL, NULL, NULL, napi_enumerable, NULL },
1191
1214
  { "window_focus", NULL, window_focus, NULL, NULL, NULL, napi_enumerable, NULL },
1192
1215
  { "window_show", NULL, window_show, NULL, NULL, NULL, napi_enumerable, NULL },
1193
1216
  { "window_hide", NULL, window_hide, NULL, NULL, NULL, napi_enumerable, NULL },
@@ -1227,7 +1250,7 @@ init (napi_env env, napi_value exports)
1227
1250
  { "cleanup", NULL, cleanup, NULL, NULL, NULL, napi_enumerable, NULL }
1228
1251
  };
1229
1252
 
1230
- CALL_NAPI(napi_define_properties, exports, 47, desc);
1253
+ CALL_NAPI(napi_define_properties, exports, 48, desc);
1231
1254
 
1232
1255
  cleanup:
1233
1256
  return exports;
@@ -715,6 +715,64 @@ video_getDisplays(Variant & list)
715
715
  return nullptr;
716
716
  }
717
717
 
718
+ ErrorMessage *
719
+ _updateTexture(
720
+ SDL_Window * window,
721
+ SDL_Renderer * renderer,
722
+ int format, int width, int height,
723
+ SDL_Texture ** texture_dst
724
+ ) {
725
+ int window_id = SDL_GetWindowID(window);
726
+
727
+ SDL_Texture * texture = SDL_CreateTexture(renderer, format, SDL_TEXTUREACCESS_STREAMING, width, height);
728
+ if (texture == nullptr) {
729
+ RETURN_ERROR("SDL_CreateTexture(%d) error: %s\n", window_id, SDL_GetError());
730
+ }
731
+
732
+ if (SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_NONE)) {
733
+ RETURN_ERROR("SDL_SetTextureBlendMode(%d) error: %s\n", window_id, SDL_GetError());
734
+ }
735
+
736
+ SDL_Texture * old_texture = (SDL_Texture *) SDL_SetWindowData(window, "texture", texture);
737
+ if (old_texture != nullptr) { SDL_DestroyTexture(old_texture); }
738
+
739
+ if (texture_dst != nullptr) { *texture_dst = texture; }
740
+
741
+ return nullptr;
742
+ }
743
+
744
+ ErrorMessage *
745
+ _updateRenderer(
746
+ SDL_Window * window,
747
+ bool accelerated,
748
+ bool vsync
749
+ ) {
750
+ int window_id = SDL_GetWindowID(window);
751
+
752
+ SDL_Renderer * old_renderer = SDL_GetRenderer(window);
753
+ if (old_renderer != nullptr) { SDL_DestroyRenderer(old_renderer); }
754
+
755
+ int renderer_flags = 0
756
+ | (accelerated ? SDL_RENDERER_ACCELERATED : SDL_RENDERER_SOFTWARE)
757
+ | (accelerated && vsync ? SDL_RENDERER_PRESENTVSYNC : 0);
758
+
759
+ SDL_Renderer * renderer = SDL_CreateRenderer(window, -1, renderer_flags);
760
+ if (renderer == nullptr) {
761
+ RETURN_ERROR("SDL_CreateRenderer(%d, %d) error: %s\n", window_id, renderer_flags, SDL_GetError());
762
+ }
763
+
764
+ SDL_RendererInfo info;
765
+ SDL_GetRendererInfo(renderer, &info);
766
+
767
+ int width, height;
768
+ SDL_GetWindowSize(window, &width, &height);
769
+ int format = SDL_GetWindowPixelFormat(window);
770
+ ErrorMessage * error = _updateTexture(window, renderer, format, width, height, nullptr);
771
+ if (error != nullptr) { return error; }
772
+
773
+ return nullptr;
774
+ }
775
+
718
776
  ErrorMessage *
719
777
  window_create (
720
778
  const char * title, int display,
@@ -723,6 +781,8 @@ window_create (
723
781
  bool fullscreen,
724
782
  bool resizable,
725
783
  bool borderless,
784
+ bool accelerated,
785
+ bool vsync,
726
786
  bool opengl,
727
787
  int * window_id, void ** native_pointer, int * native_pointer_size
728
788
  ) {
@@ -767,7 +827,7 @@ window_create (
767
827
  SDL_VERSION(&(info.version));
768
828
  bool got_info = SDL_GetWindowWMInfo(window, &info);
769
829
  if (!got_info) {
770
- RETURN_ERROR("SDL_GetWindowWMInfo() error: %s\n", SDL_GetError());
830
+ RETURN_ERROR("SDL_GetWindowWMInfo(%d) error: %s\n", *window_id, SDL_GetError());
771
831
  }
772
832
 
773
833
  int size = sizeof(NativeWindow);
@@ -776,6 +836,9 @@ window_create (
776
836
  *native_pointer = pointer;
777
837
  *native_pointer_size = size;
778
838
 
839
+ ErrorMessage * error = _updateRenderer(window, accelerated, vsync);
840
+ if (error != nullptr) { return error; }
841
+
779
842
  return nullptr;
780
843
  }
781
844
 
@@ -859,6 +922,20 @@ window_setBorderless (int window_id, bool borderless)
859
922
  return nullptr;
860
923
  }
861
924
 
925
+ ErrorMessage *
926
+ window_setAcceleratedAndVsync (int window_id, bool accelerated, bool vsync)
927
+ {
928
+ SDL_Window * window = SDL_GetWindowFromID(window_id);
929
+ if (window == nullptr) {
930
+ RETURN_ERROR("SDL_GetWindowFromID(%d) error: %s\n", window_id, SDL_GetError());
931
+ }
932
+
933
+ ErrorMessage * error = _updateRenderer(window, accelerated, vsync);
934
+ if (error != nullptr) { return error; }
935
+
936
+ return nullptr;
937
+ }
938
+
862
939
  ErrorMessage *
863
940
  window_focus (int window_id)
864
941
  {
@@ -951,7 +1028,7 @@ window_setIcon (
951
1028
 
952
1029
  SDL_Surface* surface = SDL_CreateRGBSurfaceWithFormatFrom(pixels, w, h, SDL_BITSPERPIXEL(format), stride, format);
953
1030
  if (surface == nullptr) {
954
- RETURN_ERROR("SDL_CreateRGBSurfaceWithFormatFrom(%d, %d, %d) error: %s\n", w, h, format, SDL_GetError());
1031
+ RETURN_ERROR("SDL_CreateRGBSurfaceWithFormatFrom(%d, %d, %d, %d) error: %s\n", window_id, w, h, format, SDL_GetError());
955
1032
  }
956
1033
 
957
1034
  SDL_SetWindowIcon(window, surface);
@@ -973,27 +1050,64 @@ window_render (
973
1050
  RETURN_ERROR("SDL_GetWindowFromID(%d) error: %s\n", window_id, SDL_GetError());
974
1051
  }
975
1052
 
976
- SDL_Surface* src_surface = SDL_CreateRGBSurfaceWithFormatFrom(pixels, w, h, SDL_BITSPERPIXEL(format), stride, format);
977
- SDL_SetSurfaceBlendMode(src_surface, SDL_BLENDMODE_NONE);
1053
+ SDL_Renderer * renderer = SDL_GetRenderer(window);
1054
+ if (renderer == nullptr) {
1055
+ RETURN_ERROR("SDL_GetRenderer(%d) error: %s\n", window_id, SDL_GetError());
1056
+ }
1057
+
1058
+ SDL_Texture * texture = (SDL_Texture *) SDL_GetWindowData(window, "texture");
1059
+ if (texture == nullptr) {
1060
+ RETURN_ERROR("SDL_GetWindowData(%d) error: %s\n", window_id, SDL_GetError());
1061
+ }
1062
+
1063
+ unsigned int texture_format;
1064
+ int texture_width, texture_height;
1065
+ if (SDL_QueryTexture(texture, &texture_format, nullptr, &texture_width, &texture_height)) {
1066
+ RETURN_ERROR("SDL_QueryTexture(%d) error: %s\n", window_id, SDL_GetError());
1067
+ }
1068
+
1069
+ int window_width, window_height;
1070
+ SDL_GetWindowSize(window, &window_width, &window_height);
1071
+
1072
+ if (window_width != texture_width || window_height != texture_height) {
1073
+ int format = SDL_GetWindowPixelFormat(window);
1074
+ ErrorMessage * error = _updateTexture(window, renderer, format, window_width, window_height, &texture);
1075
+ if (error != nullptr) { return error; }
1076
+ }
1077
+
1078
+ SDL_Surface * src_surface = SDL_CreateRGBSurfaceWithFormatFrom(pixels, w, h, SDL_BITSPERPIXEL(format), stride, format);
978
1079
  if (src_surface == nullptr) {
979
- RETURN_ERROR("SDL_CreateRGBSurfaceWithFormatFrom(%d, %d, %d) error: %s\n", w, h, format, SDL_GetError());
1080
+ RETURN_ERROR("SDL_CreateRGBSurfaceWithFormatFrom(%d, %d, %d, %d) error: %s\n", window_id, w, h, format, SDL_GetError());
980
1081
  }
981
1082
 
982
- SDL_Surface * window_surface = SDL_GetWindowSurface(window);
983
- if (window_surface == nullptr) {
984
- RETURN_ERROR("SDL_GetWindowSurface(%d) error: %s\n", window_id, SDL_GetError());
1083
+ if (SDL_SetSurfaceBlendMode(src_surface, SDL_BLENDMODE_NONE)) {
1084
+ RETURN_ERROR("SDL_SetTextureBlendMode(%d) error: %s\n", window_id, SDL_GetError());
985
1085
  }
986
1086
 
987
- if (SDL_BlitSurface(src_surface, NULL, window_surface, NULL) != 0) {
988
- RETURN_ERROR("SDL_LowerBlit(%d) error: %s\n", window_id, SDL_GetError());
1087
+ void *texture_pixels;
1088
+ int texture_stride;
1089
+ SDL_LockTexture(texture, nullptr, &texture_pixels, &texture_stride);
1090
+
1091
+ SDL_Surface * dst_surface = SDL_CreateRGBSurfaceWithFormatFrom(texture_pixels, texture_width, texture_height, SDL_BITSPERPIXEL(texture_format), texture_stride, texture_format);
1092
+ if (dst_surface == nullptr) {
1093
+ RETURN_ERROR("SDL_CreateRGBSurfaceWithFormatFrom(%d, %d, %d, %d) error: %s\n", window_id, texture_width, texture_height, texture_format, SDL_GetError());
989
1094
  }
990
1095
 
991
- SDL_FreeSurface(src_surface);
1096
+ if (SDL_BlitSurface(src_surface, NULL, dst_surface, NULL) != 0) {
1097
+ RETURN_ERROR("SDL_BlitSurface(%d) error: %s\n", window_id, SDL_GetError());
1098
+ }
992
1099
 
993
- if (SDL_UpdateWindowSurface(window) != 0) {
994
- RETURN_ERROR("SDL_UpdateWindowSurface(%d) error: %s\n", window_id, SDL_GetError());
1100
+ SDL_UnlockTexture(texture);
1101
+
1102
+ if (SDL_RenderCopy(renderer, texture, nullptr, nullptr)) {
1103
+ RETURN_ERROR("SDL_RenderCopy(%d) error: %s\n", window_id, SDL_GetError());
995
1104
  }
996
1105
 
1106
+ SDL_RenderPresent(renderer);
1107
+
1108
+ SDL_FreeSurface(src_surface);
1109
+ SDL_FreeSurface(dst_surface);
1110
+
997
1111
  return nullptr;
998
1112
  }
999
1113
 
@@ -1005,6 +1119,15 @@ window_destroy (int window_id)
1005
1119
  RETURN_ERROR("SDL_GetWindowFromID(%d) error: %s\n", window_id, SDL_GetError());
1006
1120
  }
1007
1121
 
1122
+ SDL_Renderer * renderer = SDL_GetRenderer(window);
1123
+ if (renderer == nullptr) {
1124
+ RETURN_ERROR("SDL_GetRenderer(%d) error: %s\n", window_id, SDL_GetError());
1125
+ }
1126
+
1127
+ SDL_Texture * texture = (SDL_Texture *) SDL_GetWindowData(window, "texture");
1128
+
1129
+ SDL_DestroyTexture(texture);
1130
+ SDL_DestroyRenderer(renderer);
1008
1131
  SDL_DestroyWindow(window);
1009
1132
 
1010
1133
  return nullptr;
@@ -53,6 +53,8 @@ ErrorMessage * window_create (
53
53
  bool fullscreen,
54
54
  bool resizable,
55
55
  bool borderless,
56
+ bool accelerated,
57
+ bool vsync,
56
58
  bool opengl,
57
59
  int * window_id, void ** native_pointer, int * native_pointer_size
58
60
  );
@@ -62,6 +64,7 @@ ErrorMessage * window_setSize (int window_id, int width, int height);
62
64
  ErrorMessage * window_setFullscreen (int window_id, bool fullscreen);
63
65
  ErrorMessage * window_setResizable (int window_id, bool resizable);
64
66
  ErrorMessage * window_setBorderless (int window_id, bool borderless);
67
+ ErrorMessage * window_setAcceleratedAndVsync (int window_id, bool accelerated, bool vsync);
65
68
  ErrorMessage * window_focus (int window_id);
66
69
  ErrorMessage * window_show (int window_id);
67
70
  ErrorMessage * window_hide (int window_id);