@kmamal/sdl 0.9.6-1 → 0.10.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/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "0.9.6-1",
2
+ "version": "0.10.0",
3
3
  "name": "@kmamal/sdl",
4
4
  "description": "SDL bindings for Node.js",
5
5
  "keywords": [
@@ -64,8 +64,9 @@
64
64
  "version": "2.26.4"
65
65
  },
66
66
  "devDependencies": {
67
- "@kmamal/testing": "^0.0.27",
68
- "@types/node": "^20.11.5",
67
+ "@kmamal/evdev": "^0.0.1",
68
+ "@kmamal/testing": "^0.0.28",
69
+ "@types/node": "^20.11.6",
69
70
  "node-addon-api": "^7.1.0",
70
71
  "node-gyp": "^10.0.1"
71
72
  }
@@ -11,6 +11,7 @@ dir.publish = Path.join(dir.root, 'publish')
11
11
  const pkgPath = Path.join(dir.root, 'package.json')
12
12
  const pkg = JSON.parse(Fs.readFileSync(pkgPath).toString())
13
13
  const version = pkg.version
14
+ const isPrerelease = version.includes('-')
14
15
  const [ , owner, repo ] = pkg.repository.url.match(/([^/:]+)\/([^/]+).git$/u)
15
16
 
16
17
  const { platform, arch } = process
@@ -23,6 +24,7 @@ sdl.assetName = `SDL-v${sdl.version}-${platform}-${targetArch}.tar.gz`
23
24
  module.exports = {
24
25
  dir,
25
26
  version,
27
+ isPrerelease,
26
28
  owner,
27
29
  repo,
28
30
  platform,
@@ -1,6 +1,6 @@
1
+ const Globals = require('../globals')
1
2
  const Bindings = require('../bindings')
2
3
  const Enums = require('../enums')
3
- const Globals = require('../globals')
4
4
  const { EventsViaPoll } = require('../events/events-via-poll')
5
5
  const { AudioFormatHelpers } = require('./format-helpers')
6
6
 
@@ -22,6 +22,7 @@ const audio = new class extends EventsViaPoll {
22
22
  constructor () { super(validEvents) }
23
23
 
24
24
  get devices () {
25
+ Globals.events.poll()
25
26
  return [
26
27
  ...Globals.audioDevices.playback,
27
28
  ...Globals.audioDevices.recording,
@@ -1,3 +1,4 @@
1
+ const Globals = require('../globals')
1
2
  const Bindings = require('../bindings')
2
3
  const { EventsViaPoll } = require('../events/events-via-poll')
3
4
 
@@ -6,7 +7,11 @@ const validEvents = [ 'update' ]
6
7
  const clipboard = new class extends EventsViaPoll {
7
8
  constructor () { super(validEvents) }
8
9
 
9
- get text () { return Bindings.clipboard_getText() }
10
+ get text () {
11
+ Globals.events.poll() // TODO
12
+ return Bindings.clipboard_getText()
13
+ }
14
+
10
15
  setText (text) {
11
16
  if (typeof text !== 'string') { throw Object.assign(new Error("text must be a string"), { text }) }
12
17
 
@@ -1,6 +1,5 @@
1
- const Bindings = require('../bindings')
2
- const Enums = require('../enums')
3
1
  const Globals = require('../globals')
2
+ const Bindings = require('../bindings')
4
3
  const { EventsViaPoll } = require('../events/events-via-poll')
5
4
 
6
5
  const validEvents = [
@@ -44,8 +43,15 @@ class ControllerInstance extends EventsViaPoll {
44
43
  get firmwareVersion () { return this._firmwareVersion }
45
44
  get serialNumber () { return this._serialNumber }
46
45
 
47
- get axes () { return this._axes }
48
- get buttons () { return this._buttons }
46
+ get axes () {
47
+ Globals.events.poll() // TODO
48
+ return this._axes
49
+ }
50
+
51
+ get buttons () {
52
+ Globals.events.poll() // TODO
53
+ return this._buttons
54
+ }
49
55
 
50
56
  get power () { return Bindings.joystick_getPower(this._device.id) }
51
57
 
@@ -88,7 +94,7 @@ class ControllerInstance extends EventsViaPoll {
88
94
  if (highFreqRumble < 0 || highFreqRumble > 1) { throw Object.assign(new Error("highFreqRumble must be between 0 and 1"), { highFreqRumble }) }
89
95
  if (!Number.isFinite(duration)) { throw Object.assign(new Error("duration must be a number"), { duration }) }
90
96
 
91
- Globals.events.poll()
97
+ // Globals.events.poll() // Errors if it hasn't been called at least once
92
98
  Bindings.joystick_rumble(this._device.id, lowFreqRumble, highFreqRumble, duration)
93
99
  clearTimeout(this._rumbleTimeout)
94
100
  this._rumbleTimeout = setTimeout(() => { this.stopRumble() }, duration)
@@ -111,7 +117,7 @@ class ControllerInstance extends EventsViaPoll {
111
117
  if (rightRumble < 0 || rightRumble > 1) { throw Object.assign(new Error("rightRumble must be between 0 and 1"), { rightRumble }) }
112
118
  if (!Number.isFinite(duration)) { throw Object.assign(new Error("duration must be a number"), { duration }) }
113
119
 
114
- Globals.events.poll()
120
+ // Globals.events.poll() // Errors if it hasn't been called at least once
115
121
  Bindings.joystick_rumbleTriggers(this._device.id, leftRumble, rightRumble, duration)
116
122
  clearTimeout(this._rumbleTriggersTimeout)
117
123
  this._rumbleTriggersTimeout = setTimeout(() => { this.stopRumble() }, duration)
@@ -1,5 +1,5 @@
1
- const Bindings = require('../bindings')
2
1
  const Globals = require('../globals')
2
+ const Bindings = require('../bindings')
3
3
  const {
4
4
  make: makeControllerDevice,
5
5
  filter: filterControllerDevice,
@@ -19,7 +19,10 @@ const validEvents = [ 'deviceAdd', 'deviceRemove' ]
19
19
  const controller = new class extends EventsViaPoll {
20
20
  constructor () { super(validEvents) }
21
21
 
22
- get devices () { return Globals.controllerDevices }
22
+ get devices () {
23
+ Globals.events.poll()
24
+ return Globals.controllerDevices
25
+ }
23
26
 
24
27
  openDevice (device) { return new ControllerInstance(device) }
25
28
 
@@ -1,5 +1,5 @@
1
- const { EventEmitter } = require('events')
2
1
  const Globals = require('../globals')
2
+ const { EventEmitter } = require('events')
3
3
 
4
4
  let _ID = 0
5
5
  const activeEmitters = new Set()
@@ -27,7 +27,7 @@ class EventsViaPoll extends EventEmitter {
27
27
  activeEmitters.delete(id)
28
28
  if (activeEmitters.size !== 0) { return }
29
29
 
30
- Globals.events.stopPolling()
30
+ Globals.events.switchToPollingSlow()
31
31
  })
32
32
 
33
33
  this.on('newListener', (type) => {
@@ -41,7 +41,7 @@ class EventsViaPoll extends EventEmitter {
41
41
  activeEmitters.add(id)
42
42
  if (activeEmitters.size !== 1) { return }
43
43
 
44
- Globals.events.startPolling()
44
+ Globals.events.switchToPollingFast()
45
45
  })
46
46
  }
47
47
 
@@ -1,5 +1,5 @@
1
- const Bindings = require('../bindings')
2
1
  const Globals = require('../globals')
2
+ const Bindings = require('../bindings')
3
3
  const { video: videoModule } = require('../video')
4
4
  const { mapping } = require('../keyboard/key-mapping')
5
5
  const { joystick: joystickModule } = require('../joystick')
@@ -392,19 +392,30 @@ const handleEvent = (event) => {
392
392
 
393
393
  const poll = () => { Bindings.events_poll(handleEvent) }
394
394
 
395
- let eventsInterval = null
395
+ let pollInterval = null
396
+
397
+ const switchToPollingFast = () => {
398
+ clearInterval(pollInterval)
399
+ pollInterval = setInterval(poll, 0)
400
+ }
396
401
 
397
- const startPolling = () => {
398
- eventsInterval = setInterval(poll, 0)
402
+ const switchToPollingSlow = () => {
403
+ clearInterval(pollInterval)
404
+ pollInterval = setInterval(poll, 1e3)
405
+ pollInterval.unref()
399
406
  }
400
407
 
401
408
  const stopPolling = () => {
402
- clearInterval(eventsInterval)
403
- eventsInterval = null
409
+ clearInterval(pollInterval)
410
+ pollInterval = null
404
411
  }
405
412
 
413
+ switchToPollingSlow()
414
+ poll()
415
+
406
416
  Globals.events = {
407
- startPolling,
408
- stopPolling,
409
417
  poll,
418
+ switchToPollingFast,
419
+ switchToPollingSlow,
420
+ stopPolling,
410
421
  }
@@ -26,5 +26,5 @@ module.exports = {
26
26
  },
27
27
  audioInstances: new Map(),
28
28
 
29
- events: null, // Set later to { startPolling, stopPolling, poll }
29
+ events: null, // Set later from events.js
30
30
  }
@@ -1,5 +1,5 @@
1
- const Bindings = require('../bindings')
2
1
  const Globals = require('../globals')
2
+ const Bindings = require('../bindings')
3
3
  const { make: makeJoystickDevice } = require('./device')
4
4
  const { EventsViaPoll } = require('../events/events-via-poll')
5
5
  const { JoystickInstance } = require('./joystick-instance')
@@ -15,7 +15,10 @@ const validEvents = [ 'deviceAdd', 'deviceRemove' ]
15
15
  const joystick = new class extends EventsViaPoll {
16
16
  constructor () { super(validEvents) }
17
17
 
18
- get devices () { return Globals.joystickDevices }
18
+ get devices () {
19
+ Globals.events.poll()
20
+ return Globals.joystickDevices
21
+ }
19
22
 
20
23
  openDevice (device) { return new JoystickInstance(device) }
21
24
  }()
@@ -1,5 +1,5 @@
1
- const Bindings = require('../bindings')
2
1
  const Globals = require('../globals')
2
+ const Bindings = require('../bindings')
3
3
  const { EventsViaPoll } = require('../events/events-via-poll')
4
4
 
5
5
  const validEvents = [
@@ -46,10 +46,25 @@ class JoystickInstance extends EventsViaPoll {
46
46
  get firmwareVersion () { return this._firmwareVersion }
47
47
  get serialNumber () { return this._serialNumber }
48
48
 
49
- get axes () { return this._axes }
50
- get balls () { return this._balls }
51
- get buttons () { return this._buttons }
52
- get hats () { return this._hats }
49
+ get axes () {
50
+ Globals.events.poll() // TODO
51
+ return this._axes
52
+ }
53
+
54
+ get balls () {
55
+ Globals.events.poll() // TODO
56
+ return this._balls
57
+ }
58
+
59
+ get buttons () {
60
+ Globals.events.poll() // TODO
61
+ return this._buttons
62
+ }
63
+
64
+ get hats () {
65
+ Globals.events.poll() // TODO
66
+ return this._hats
67
+ }
53
68
 
54
69
  get power () { return Bindings.joystick_getPower(this._device.id) }
55
70
 
@@ -92,7 +107,7 @@ class JoystickInstance extends EventsViaPoll {
92
107
  if (highFreqRumble < 0 || highFreqRumble > 1) { throw Object.assign(new Error("highFreqRumble must be between 0 and 1"), { highFreqRumble }) }
93
108
  if (!Number.isFinite(duration)) { throw Object.assign(new Error("duration must be a number"), { duration }) }
94
109
 
95
- Globals.events.poll()
110
+ // Globals.events.poll() // Errors if it hasn't been called at least once
96
111
  Bindings.joystick_rumble(this._device.id, lowFreqRumble, highFreqRumble, duration)
97
112
  clearTimeout(this._rumbleTimeout)
98
113
  this._rumbleTimeout = setTimeout(() => { this.stopRumble() }, duration)
@@ -115,7 +130,7 @@ class JoystickInstance extends EventsViaPoll {
115
130
  if (rightRumble < 0 || rightRumble > 1) { throw Object.assign(new Error("rightRumble must be between 0 and 1"), { rightRumble }) }
116
131
  if (!Number.isFinite(duration)) { throw Object.assign(new Error("duration must be a number"), { duration }) }
117
132
 
118
- Globals.events.poll()
133
+ // Globals.events.poll() // Errors if it hasn't been called at least once
119
134
  Bindings.joystick_rumbleTriggers(this._device.id, leftRumble, rightRumble, duration)
120
135
  clearTimeout(this._rumbleTriggersTimeout)
121
136
  this._rumbleTriggersTimeout = setTimeout(() => { this.stopRumble() }, duration)
@@ -1,3 +1,4 @@
1
+ const Globals = require('../globals')
1
2
  const Bindings = require('../bindings')
2
3
  const Enums = require('../enums')
3
4
  const { mapping, reverseMapping } = require('./key-mapping')
@@ -22,7 +23,10 @@ const keyboard = {
22
23
  return Bindings.keyboard_getScancode(_key)
23
24
  },
24
25
 
25
- getState () { return Bindings.keyboard_getState() },
26
+ getState () {
27
+ Globals.events.poll()
28
+ return Bindings.keyboard_getState()
29
+ },
26
30
  }
27
31
 
28
32
  module.exports = { keyboard }
@@ -1,5 +1,5 @@
1
- const Bindings = require('../bindings')
2
1
  const Globals = require('../globals')
2
+ const Bindings = require('../bindings')
3
3
  const { EventsViaPoll } = require('../events/events-via-poll')
4
4
 
5
5
  const validEvents = [
@@ -1,16 +1,20 @@
1
- const Bindings = require('../bindings')
2
1
  const Globals = require('../globals')
2
+ const Bindings = require('../bindings')
3
3
  const { EventsViaPoll } = require('../events/events-via-poll')
4
4
  const { Window } = require('./window')
5
5
 
6
6
  Globals.displays = Bindings.video_getDisplays()
7
7
 
8
+
8
9
  const validEvents = [ 'displayAdd', 'displayRemove', 'displayOrient' ]
9
10
 
10
11
  const video = new class extends EventsViaPoll {
11
12
  constructor () { super(validEvents) }
12
13
 
13
- get displays () { return Globals.displays }
14
+ get displays () {
15
+ Globals.events.poll()
16
+ return Globals.displays
17
+ }
14
18
 
15
19
  get windows () { return [ ...Globals.windows.all.values() ] }
16
20
  get focused () { return Globals.windows.focused }
@@ -1,5 +1,5 @@
1
- const Bindings = require('../bindings')
2
1
  const Globals = require('../globals')
2
+ const Bindings = require('../bindings')
3
3
  const Enums = require('../enums')
4
4
  const { EventsViaPoll } = require('../events/events-via-poll')
5
5
 
@@ -8,9 +8,13 @@
8
8
  Napi::Array
9
9
  audio::_getDevices(Napi::Env &env, bool is_capture)
10
10
  {
11
+ const char *error = SDL_GetError();
12
+ if (error != global::no_error) { fprintf(stderr, "SDL silent error: %s\n", error); }
13
+ SDL_ClearError();
14
+
11
15
  int num_devices = SDL_GetNumAudioDevices(is_capture ? 1 : 0);
12
16
  if (num_devices == -1) {
13
- const char *error = SDL_GetError();
17
+ error = SDL_GetError();
14
18
  if (error != global::no_error) {
15
19
  std::ostringstream message;
16
20
  message << "SDL_GetNumAudioDevices(" << is_capture << ") error: " << error;
@@ -151,8 +155,12 @@ audio::dequeue (const Napi::CallbackInfo &info)
151
155
  void *dst = info[1].As<Napi::Buffer<char>>().Data();
152
156
  int size = info[2].As<Napi::Number>().Int32Value();
153
157
 
154
- int num = SDL_DequeueAudio(audio_id, dst, size);
155
158
  const char *error = SDL_GetError();
159
+ if (error != global::no_error) { fprintf(stderr, "SDL silent error: %s\n", error); }
160
+ SDL_ClearError();
161
+
162
+ int num = SDL_DequeueAudio(audio_id, dst, size);
163
+ error = SDL_GetError();
156
164
  if (error != global::no_error) {
157
165
  std::ostringstream message;
158
166
  message << "SDL_DequeueAudio(" << audio_id << ") error: " << error;
@@ -14,7 +14,9 @@ std::map<SDL_GameControllerButton, std::string> controller::buttons;
14
14
  void
15
15
  controller::getState (Napi::Env &env, SDL_GameController *controller, Napi::Object dst)
16
16
  {
17
- const char *error;
17
+ const char *error = SDL_GetError();
18
+ if (error != global::no_error) { fprintf(stderr, "SDL silent error: %s\n", error); }
19
+ SDL_ClearError();
18
20
 
19
21
  Napi::Object axes = Napi::Object::New(env);
20
22
  axes.Set("leftStickX", Napi::Number::New(env, joystick::mapAxis(SDL_GameControllerGetAxis(controller, SDL_CONTROLLER_AXIS_LEFTX))));
@@ -60,8 +60,8 @@ std::string events::types::REMAP;
60
60
  std::string events::types::UPDATE;
61
61
 
62
62
 
63
- Napi::Env *poll_env = nullptr;
64
- Napi::Function *poll_callback = nullptr;
63
+ static Napi::Env *poll_env = nullptr;
64
+ static Napi::Function *poll_callback = nullptr;
65
65
 
66
66
  Napi::Object
67
67
  packageEvent (Napi::Env &env, const SDL_Event &event)
@@ -16,22 +16,19 @@
16
16
  #endif
17
17
 
18
18
 
19
- SDL_threadID mainThreadId;
19
+ static SDL_threadID mainThreadId;
20
20
 
21
21
  int filterEvents(void*, SDL_Event *event) {
22
- if (SDL_ThreadID() != mainThreadId) { return 1; }
23
-
24
- if (event->type != SDL_WINDOWEVENT) { return 1; }
25
-
26
- int window_event_type = event->window.event;
27
22
  if (true
28
- && window_event_type != SDL_WINDOWEVENT_MOVED
29
- && window_event_type != SDL_WINDOWEVENT_SIZE_CHANGED
30
- ) { return 1; }
31
-
32
- events::dispatchEvent(*event);
33
-
34
- return 0;
23
+ && SDL_ThreadID() == mainThreadId
24
+ && event->type == SDL_WINDOWEVENT
25
+ && (false
26
+ || event->window.event == SDL_WINDOWEVENT_MOVED
27
+ || event->window.event == SDL_WINDOWEVENT_SIZE_CHANGED
28
+ )
29
+ ) { events::dispatchEvent(*event); }
30
+
31
+ return 1;
35
32
  }
36
33
 
37
34
 
@@ -148,7 +148,7 @@ joystick::open (const Napi::CallbackInfo &info)
148
148
  Napi::Value firmware_version = firmware == 0 ? env.Null() : Napi::Number::New(env, firmware);
149
149
 
150
150
  const char *serial = SDL_JoystickGetSerial(joystick);
151
- Napi::Value serial_number = serial == 0 ? env.Null() : Napi::String::New(env, serial);
151
+ Napi::Value serial_number = serial == nullptr ? env.Null() : Napi::String::New(env, serial);
152
152
 
153
153
  SDL_bool has_led = SDL_JoystickHasLED(joystick);
154
154
  SDL_bool has_rumble = SDL_JoystickHasRumble(joystick);
@@ -164,12 +164,16 @@ joystick::open (const Napi::CallbackInfo &info)
164
164
 
165
165
  Napi::Array axes = Napi::Array::New(env, num_axes);
166
166
 
167
+ const char *error = SDL_GetError();
168
+ if (error != global::no_error) { fprintf(stderr, "SDL silent error: %s\n", error); }
169
+ SDL_ClearError();
170
+
167
171
  for (int i = 0; i < num_axes; i++) {
168
172
  double value = joystick::mapAxis(SDL_JoystickGetAxis(joystick, i));
169
- const char *error = SDL_GetError();
173
+ error = SDL_GetError();
170
174
  if (error != global::no_error) {
171
175
  std::ostringstream message;
172
- message << "SDL_JoystickGetAxis(" << index << ", " << i << ") error: " << SDL_GetError();
176
+ message << "SDL_JoystickGetAxis(" << index << ", " << i << ") error: " << error;
173
177
  SDL_ClearError();
174
178
  throw Napi::Error::New(env, message.str());
175
179
  }
@@ -28,10 +28,16 @@ keyboard::getScancode(const Napi::CallbackInfo &info)
28
28
  std::string keyname = info[0].As<Napi::String>().Utf8Value();
29
29
 
30
30
  SDL_Keycode keycode = SDL_GetKeyFromName(keyname.c_str());
31
- if (keycode == SDLK_UNKNOWN) { return env.Null(); }
31
+ if (keycode == SDLK_UNKNOWN) {
32
+ SDL_ClearError();
33
+ return env.Null();
34
+ }
32
35
 
33
36
  int scancode = SDL_GetScancodeFromKey(keycode);
34
- if (scancode == 0) { return env.Null(); }
37
+ if (scancode == 0) {
38
+ SDL_ClearError();
39
+ return env.Null();
40
+ }
35
41
 
36
42
  return Napi::Number::New(env, scancode);
37
43
  }
@@ -29,46 +29,6 @@
29
29
  #endif
30
30
 
31
31
 
32
- void
33
- updateTexture(
34
- Napi::Env &env,
35
- SDL_Window *window,
36
- SDL_Renderer *renderer,
37
- int pixel_width, int pixel_height,
38
- SDL_Texture **texture_dst
39
- ) {
40
- int window_id = SDL_GetWindowID(window);
41
- // Not likely to fail.
42
-
43
- int format = SDL_GetWindowPixelFormat(window);
44
- if (format == SDL_PIXELFORMAT_UNKNOWN) {
45
- std::ostringstream message;
46
- message << "SDL_GetWindowPixelFormat(" << window_id << ") error: " << SDL_GetError();
47
- SDL_ClearError();
48
- throw Napi::Error::New(env, message.str());
49
- }
50
-
51
- SDL_Texture *texture = SDL_CreateTexture(renderer, format, SDL_TEXTUREACCESS_STREAMING, pixel_width, pixel_height);
52
- if (texture == nullptr) {
53
- std::ostringstream message;
54
- message << "SDL_CreateTexture(" << window_id << ") error: " << SDL_GetError();
55
- SDL_ClearError();
56
- throw Napi::Error::New(env, message.str());
57
- }
58
-
59
- if (SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_NONE) < 0) {
60
- std::ostringstream message;
61
- message << "SDL_SetTextureBlendMode(" << window_id << ") error: " << SDL_GetError();
62
- SDL_ClearError();
63
- throw Napi::Error::New(env, message.str());
64
- }
65
-
66
- SDL_Texture *old_texture = (SDL_Texture *) SDL_SetWindowData(window, "texture", texture);
67
- if (old_texture != nullptr) { SDL_DestroyTexture(old_texture); }
68
-
69
- if (texture_dst != nullptr) { *texture_dst = texture; }
70
- }
71
-
72
32
  void
73
33
  updateRenderer(
74
34
  Napi::Env &env,
@@ -82,14 +42,24 @@ updateRenderer(
82
42
  SDL_Renderer *old_renderer = SDL_GetRenderer(window);
83
43
  if (old_renderer != nullptr) {
84
44
  SDL_DestroyRenderer(old_renderer);
85
- SDL_SetWindowData(window, "texture", nullptr);
86
45
  }
87
46
 
88
- int renderer_flags = 0
89
- | (*is_accelerated ? SDL_RENDERER_ACCELERATED : SDL_RENDERER_SOFTWARE)
90
- | (*is_accelerated && *is_vsync ? SDL_RENDERER_PRESENTVSYNC : 0);
47
+ SDL_Renderer *renderer;
48
+ for (int i = 0; i < 4; i++) {
49
+ int renderer_flags = 0
50
+ | (*is_accelerated ? SDL_RENDERER_ACCELERATED : SDL_RENDERER_SOFTWARE)
51
+ | (*is_vsync ? SDL_RENDERER_PRESENTVSYNC : 0);
52
+
53
+ renderer = SDL_CreateRenderer(window, -1, renderer_flags);
54
+ if (renderer != nullptr) {
55
+ SDL_ClearError();
56
+ break;
57
+ }
58
+
59
+ *is_vsync = !*is_vsync;
60
+ if (i % 2) { *is_accelerated = !*is_accelerated; }
61
+ }
91
62
 
92
- SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, renderer_flags);
93
63
  if (renderer == nullptr) {
94
64
  std::ostringstream message;
95
65
  message << "SDL_CreateRenderer(" << window_id << ", " << is_accelerated << ", " << is_vsync << ") error: " << SDL_GetError();
@@ -107,11 +77,6 @@ updateRenderer(
107
77
 
108
78
  *is_accelerated = info.flags & SDL_RENDERER_ACCELERATED;
109
79
  *is_vsync = info.flags & SDL_RENDERER_PRESENTVSYNC;
110
-
111
- int pixel_width, pixel_height;
112
- SDL_GetWindowSizeInPixels(window, &pixel_width, &pixel_height);
113
-
114
- updateTexture(env, window, renderer, pixel_width, pixel_height, nullptr);
115
80
  }
116
81
 
117
82
 
@@ -581,73 +546,38 @@ window::render (const Napi::CallbackInfo &info)
581
546
  throw Napi::Error::New(env, message.str());
582
547
  }
583
548
 
584
- SDL_Texture *texture = (SDL_Texture *) SDL_GetWindowData(window, "texture");
585
- if (texture == nullptr) {
586
- std::ostringstream message;
587
- message << "SDL_GetWindowData(" << window_id << ") error: " << SDL_GetError();
588
- SDL_ClearError();
589
- throw Napi::Error::New(env, message.str());
590
- }
591
-
592
- unsigned int texture_format;
593
- int texture_width, texture_height;
594
- if (SDL_QueryTexture(texture, &texture_format, nullptr, &texture_width, &texture_height) < 0) {
595
- std::ostringstream message;
596
- message << "SDL_QueryTexture(" << window_id << ") error: " << SDL_GetError();
597
- SDL_ClearError();
598
- throw Napi::Error::New(env, message.str());
599
- }
600
-
601
- int pixel_width, pixel_height;
602
- SDL_GetWindowSizeInPixels(window, &pixel_width, &pixel_height);
603
-
604
- if (pixel_width != texture_width || pixel_height != texture_height) {
605
- updateTexture(env, window, renderer, pixel_width, pixel_height, &texture);
606
- texture_width = pixel_width;
607
- texture_height = pixel_height;
608
- }
609
-
610
- SDL_Surface *src_surface = SDL_CreateRGBSurfaceWithFormatFrom(pixels, w, h, SDL_BITSPERPIXEL(format), stride, format);
611
- if (src_surface == nullptr) {
549
+ SDL_Surface *surface = SDL_CreateRGBSurfaceWithFormatFrom(pixels, w, h, SDL_BITSPERPIXEL(format), stride, format);
550
+ if (surface == nullptr) {
612
551
  std::ostringstream message;
613
552
  message << "SDL_CreateRGBSurfaceWithFormatFrom(" << window_id << ", " << w << ", " << h << ", " << stride << ", " << format << ") error: " << SDL_GetError();
614
553
  SDL_ClearError();
615
554
  throw Napi::Error::New(env, message.str());
616
555
  }
617
556
 
618
- if (SDL_SetSurfaceBlendMode(src_surface, SDL_BLENDMODE_NONE) < 0) {
557
+ if (SDL_SetSurfaceBlendMode(surface, SDL_BLENDMODE_NONE) < 0) {
619
558
  std::ostringstream message;
620
559
  message << "SDL_SetSurfaceBlendMode(" << window_id << ") error: " << SDL_GetError();
621
560
  SDL_ClearError();
622
561
  throw Napi::Error::New(env, message.str());
623
562
  }
624
563
 
625
- void *texture_pixels;
626
- int texture_stride;
627
- if (SDL_LockTexture(texture, nullptr, &texture_pixels, &texture_stride) < 0) {
564
+ SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, surface);
565
+ if (texture == nullptr) {
628
566
  std::ostringstream message;
629
- message << "SDL_LockTexture(" << window_id << ", " << texture_width << ", " << texture_height << ", " << texture_stride << ", " << texture_format << ") error: " << SDL_GetError();
567
+ message << "SDL_CreateTextureFromSurface(" << window_id << ") error: " << SDL_GetError();
630
568
  SDL_ClearError();
631
569
  throw Napi::Error::New(env, message.str());
632
570
  }
633
571
 
634
- SDL_Surface *dst_surface = SDL_CreateRGBSurfaceWithFormatFrom(texture_pixels, texture_width, texture_height, SDL_BITSPERPIXEL(texture_format), texture_stride, texture_format);
635
- if (dst_surface == nullptr) {
636
- std::ostringstream message;
637
- message << "SDL_CreateRGBSurfaceWithFormatFrom(" << window_id << ", " << texture_width << ", " << texture_height << ", " << texture_stride << ", " << texture_format << ") error: " << SDL_GetError();
638
- SDL_ClearError();
639
- throw Napi::Error::New(env, message.str());
640
- }
572
+ SDL_FreeSurface(surface);
641
573
 
642
- if (SDL_BlitScaled(src_surface, NULL, dst_surface, NULL) < 0) {
574
+ if (SDL_RenderClear(renderer) < 0) {
643
575
  std::ostringstream message;
644
- message << "SDL_BlitSurface(" << window_id << ") error: " << SDL_GetError();
576
+ message << "SDL_RenderClear(" << window_id << ") error: " << SDL_GetError();
645
577
  SDL_ClearError();
646
578
  throw Napi::Error::New(env, message.str());
647
579
  }
648
580
 
649
- SDL_UnlockTexture(texture);
650
-
651
581
  if (SDL_RenderCopy(renderer, texture, nullptr, nullptr) < 0) {
652
582
  std::ostringstream message;
653
583
  message << "SDL_RenderCopy(" << window_id << ") error: " << SDL_GetError();
@@ -657,9 +587,6 @@ window::render (const Napi::CallbackInfo &info)
657
587
 
658
588
  SDL_RenderPresent(renderer);
659
589
 
660
- SDL_FreeSurface(src_surface);
661
- SDL_FreeSurface(dst_surface);
662
-
663
590
  return env.Undefined();
664
591
  }
665
592
 
@@ -2,6 +2,7 @@
2
2
  #define _WINDOW_H_
3
3
 
4
4
  #include <napi.h>
5
+ #include <SDL.h>
5
6
 
6
7
  namespace window {
7
8
 
@@ -1,2 +0,0 @@
1
-
2
- brew install xquartz
@@ -1,5 +0,0 @@
1
-
2
- sudo apt-get update
3
- sudo apt-get install -y \
4
- build-essential \
5
- libx11-dev
@@ -1,5 +0,0 @@
1
-
2
- sudo apt-get update
3
- sudo apt-get install -y \
4
- build-essential \
5
- libx11-dev
@@ -1,116 +0,0 @@
1
- #!/usr/bin/env bash
2
-
3
- set -eEu -o pipefail
4
- set -x
5
-
6
- OS="raspios_lite_arm64"
7
- DATE="2023-05-03"
8
- IMAGE="$DATE-raspios-bullseye-arm64-lite.img"
9
- MOUNT="/mnt/raspbian"
10
-
11
- DIR="/tmp/node-sdl-rpi-qemu"
12
- rm -rf "$DIR" || true
13
- mkdir -p "$DIR"
14
- cd "$DIR"
15
- echo "Working in $DIR"
16
-
17
-
18
- sudo apt-get update
19
- sudo apt-get install -y xz-utils qemu-system-arm expect
20
-
21
- CACHED="/home/$USER/Desktop/$IMAGE.xz"
22
- if test -f "$CACHED"; then
23
- cp "$CACHED" .
24
- else
25
- wget --progress=dot:giga "https://downloads.raspberrypi.org/$OS/images/$OS-$DATE/$IMAGE.xz"
26
- fi
27
- unxz "$IMAGE.xz"
28
-
29
- qemu-img resize "$IMAGE" 4G
30
-
31
- expect -f - <<- EOF
32
- set timeout -1
33
- # exp_internal 1
34
-
35
- spawn fdisk {$IMAGE}
36
-
37
- expect -exact {Command (m for help): }
38
- send -- [string cat {d} "\r"]
39
-
40
- expect -exact {Partition number (1,2, default 2): }
41
- send -- [string cat {} "\r"]
42
-
43
- expect -exact {Command (m for help): }
44
- send -- [string cat {n} "\r"]
45
-
46
- expect -exact {Select (default p): }
47
- send -- [string cat {} "\r"]
48
-
49
- expect -exact {Partition number (2-4, default 2): }
50
- send -- [string cat {} "\r"]
51
-
52
- expect -exact {First sector (2048-8388607, default 2048): }
53
- send -- [string cat {532480} "\r"]
54
-
55
- expect -exact {(532480-8388607, default 8388607): }
56
- send -- [string cat {} "\r"]
57
-
58
- expect -exact {Do you want to remove the signature?}
59
- send -- [string cat {N} "\r"]
60
-
61
- expect -exact {Command (m for help): }
62
- send -- [string cat {w} "\r"]
63
-
64
- expect eof
65
- EOF
66
-
67
- LOOP="$(sudo losetup -f)"
68
- sudo losetup -P "$LOOP" "$IMAGE"
69
- sudo e2fsck -fa "${LOOP}p2" || true
70
- sudo resize2fs "${LOOP}p2"
71
- sudo losetup -d "${LOOP}"
72
-
73
- sudo mkdir -p "$MOUNT"
74
- sudo mount -o loop,offset=4194304 "$IMAGE" "$MOUNT"
75
-
76
- printf 'pi:$6$c70VpvPsVNCG0YR5$l5vWWLsLko9Kj65gcQ8qvMkuOoRkEagI90qi3F/Y7rm8eNYZHW8CY6BOIKwMH7a3YYzZYL90zf304cAHLFaZE0' | sudo tee "$MOUNT/userconf" > /dev/null
77
-
78
- expect -f - <<- EOF
79
- set timeout -1
80
- # exp_internal 1
81
-
82
- spawn qemu-system-aarch64 \
83
- -machine raspi3b \
84
- -cpu cortex-a72 \
85
- -dtb "$MOUNT/bcm2710-rpi-3-b-plus.dtb" \
86
- -m 1G -smp 4 -nographic -monitor none -serial stdio \
87
- -kernel "$MOUNT/kernel8.img" \
88
- -sd "$IMAGE" \
89
- -netdev user,id=net0 \
90
- -device usb-net,netdev=net0 \
91
- -append "rw earlyprintk loglevel=8 console=ttyAMA0,115200 dwc_otg.lpm_enable=0 root=/dev/mmcblk0p2 rootdelay=1"
92
-
93
- expect -exact {raspberrypi login: }
94
- send -- [string cat {pi} "\r"]
95
-
96
- expect -exact {Password: }
97
- sleep 5
98
- send -- [string cat {raspberry} "\r"]
99
-
100
- expect -exact {pi@raspberrypi}
101
- send -- [string cat {curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash - && sudo apt-get install -y nodejs} "\r"]
102
-
103
- expect -exact {pi@raspberrypi}
104
- send -- [string cat {wget --progress=dot https://github.com/kmamal/node-sdl/archive/refs/heads/master.tar.gz && tar xvf master.tar.gz && cd node-sdl-master} "\r"]
105
-
106
- expect -exact {pi@raspberrypi}
107
- send -- [string cat {./scripts/install-deps-raspbian.sh && GITHUB_TOKEN="$GITHUB_TOKEN" npm run release} "\r"]
108
-
109
- expect -exact {pi@raspberrypi}
110
- send -- [string cat {sudo shutdown -h now} "\r"]
111
-
112
- expect eof
113
- EOF
114
-
115
- # debug
116
- # sudo mount -o loop,offset=272629760 "$IMAGE" "$MOUNT"
@@ -1,13 +0,0 @@
1
- import { execSync } from 'node:child_process'
2
-
3
- await import('./clean.mjs')
4
-
5
- execSync('npm install', {
6
- stdio: 'inherit',
7
- env: {
8
- ...process.env,
9
- NODE_SDL_FROM_SOURCE: 1,
10
- },
11
- })
12
-
13
- await import('./upload-release.mjs')
@@ -1,86 +0,0 @@
1
- import Fs from 'node:fs'
2
- import Path from 'node:path'
3
- import C from './util/common.js'
4
- import { fetch } from './util/fetch.js'
5
- import Tar from 'tar'
6
-
7
- const commonHeaders = {
8
- "Accept": 'application/vnd.github+json',
9
- "Authorization": `Bearer ${process.env.GITHUB_TOKEN}`,
10
- 'User-Agent': `@kmamal/sdl@${C.version}`,
11
- }
12
-
13
- let response
14
-
15
- getRelease: {
16
- console.log("get release", C.version)
17
-
18
- try {
19
- response = await fetch(
20
- `https://api.github.com/repos/${C.owner}/${C.repo}/releases/tags/v${C.version}`,
21
- { headers: commonHeaders },
22
- )
23
- console.log("release exists", C.version)
24
- break getRelease
25
- } catch (error) {
26
- console.log(error.message)
27
- }
28
-
29
- console.log("create release", C.version)
30
-
31
- response = await fetch(
32
- `https://api.github.com/repos/${C.owner}/${C.repo}/releases`,
33
- {
34
- method: 'POST',
35
- headers: commonHeaders,
36
- body: JSON.stringify({
37
- tag_name: `v${C.version}`, // eslint-disable-line camelcase
38
- name: `v${C.version}`,
39
- }),
40
- },
41
- )
42
- }
43
- const releaseId = (await response.json()).id
44
-
45
- console.log("create archive", C.assetName)
46
- await Fs.promises.rm(C.dir.publish, { recursive: true }).catch(() => {})
47
- await Fs.promises.mkdir(C.dir.publish, { recursive: true })
48
- const assetPath = Path.join(C.dir.publish, C.assetName)
49
-
50
- process.chdir(C.dir.dist)
51
- await Tar.create(
52
- { gzip: true, file: assetPath },
53
- await Fs.promises.readdir('.'),
54
- )
55
- const buffer = await Fs.promises.readFile(assetPath)
56
-
57
- response = await fetch(
58
- `https://api.github.com/repos/${C.owner}/${C.repo}/releases/${releaseId}/assets`,
59
- { headers: commonHeaders },
60
- )
61
-
62
- const list = await response.json()
63
- const asset = list.find((x) => x.name === C.assetName)
64
- if (asset) {
65
- console.log("delete asset", C.assetName)
66
- await fetch(
67
- `https://api.github.com/repos/${C.owner}/${C.repo}/releases/assets/${asset.id}`,
68
- {
69
- method: 'DELETE',
70
- headers: commonHeaders,
71
- },
72
- )
73
- }
74
-
75
- console.log("upload", C.assetName)
76
- await fetch(
77
- `https://uploads.github.com/repos/${C.owner}/${C.repo}/releases/${releaseId}/assets?name=${C.assetName}`,
78
- {
79
- method: 'POST',
80
- headers: {
81
- ...commonHeaders,
82
- 'Content-Type': 'application/gzip',
83
- },
84
- body: buffer,
85
- },
86
- )