@kmamal/sdl 0.11.4 → 0.11.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (39) hide show
  1. package/README.md +70 -16
  2. package/binding.gyp +1 -0
  3. package/package.json +1 -1
  4. package/src/javascript/audio/audio-instance.js +2 -3
  5. package/src/javascript/audio/device.js +1 -10
  6. package/src/javascript/audio/index.js +5 -7
  7. package/src/javascript/controller/controller-instance.js +2 -0
  8. package/src/javascript/controller/device.js +14 -3
  9. package/src/javascript/controller/index.js +5 -11
  10. package/src/javascript/events/index.js +61 -123
  11. package/src/javascript/events/reconcile-audio-devices.js +15 -0
  12. package/src/javascript/events/reconcile-displays.js +16 -0
  13. package/src/javascript/events/reconcile-joystick-and-controller-devices.js +37 -0
  14. package/src/javascript/events/reconcile.js +61 -0
  15. package/src/javascript/joystick/device.js +9 -3
  16. package/src/javascript/joystick/index.js +13 -5
  17. package/src/javascript/joystick/joystick-instance.js +2 -0
  18. package/src/javascript/sensor/sensor-instance.js +2 -0
  19. package/src/javascript/video/display.js +4 -0
  20. package/src/javascript/video/index.js +1 -0
  21. package/src/javascript/video/window.js +6 -0
  22. package/src/native/audio.cpp +10 -12
  23. package/src/native/audio.h +4 -0
  24. package/src/native/controller.cpp +72 -56
  25. package/src/native/controller.h +3 -0
  26. package/src/native/enums.cpp +315 -315
  27. package/src/native/events.cpp +161 -79
  28. package/src/native/events.h +2 -0
  29. package/src/native/global.cpp +47 -20
  30. package/src/native/joystick.cpp +87 -48
  31. package/src/native/joystick.h +3 -2
  32. package/src/native/keyboard.cpp +1 -1
  33. package/src/native/mouse.cpp +2 -2
  34. package/src/native/power.cpp +9 -3
  35. package/src/native/sensor.cpp +24 -13
  36. package/src/native/video.cpp +21 -13
  37. package/src/native/video.h +1 -0
  38. package/src/native/window.cpp +41 -29
  39. package/src/types/index.d.ts +65 -17
@@ -1,4 +1,5 @@
1
1
  #include "joystick.h"
2
+ #include "controller.h"
2
3
  #include "global.h"
3
4
  #include <SDL.h>
4
5
  #include <string>
@@ -7,14 +8,23 @@
7
8
 
8
9
 
9
10
  std::map<Uint8, std::string> joystick::hat_positions;
10
- std::map<SDL_JoystickType, std::string> joystick::joystick_types;
11
+ std::map<SDL_JoystickType, std::string> joystick::types;
11
12
  std::map<SDL_JoystickPowerLevel, std::string> joystick::power_levels;
12
13
 
13
14
 
14
15
  double
15
- joystick::mapAxis (int value) {
16
- const static int SDL_JOYSTICK_AXIS_RANGE = SDL_JOYSTICK_AXIS_MAX - SDL_JOYSTICK_AXIS_MIN;
17
- return (value - SDL_JOYSTICK_AXIS_MIN) / (SDL_JOYSTICK_AXIS_RANGE / 2.0) - 1;
16
+ joystick::mapAxis (SDL_Joystick *joystick, int axis) {
17
+ return mapAxisValue(joystick, axis, SDL_JoystickGetAxis(joystick, axis));
18
+ }
19
+
20
+ double
21
+ joystick::mapAxisValue (SDL_Joystick *joystick, int axis, int value) {
22
+ Sint16 initial;
23
+ if (!SDL_JoystickGetAxisInitialState(joystick, axis, &initial)) { initial = 0; }
24
+ double range = value < initial
25
+ ? initial - SDL_JOYSTICK_AXIS_MIN
26
+ : SDL_JOYSTICK_AXIS_MAX - initial;
27
+ return (value - initial) / range;
18
28
  }
19
29
 
20
30
  Napi::Array
@@ -39,9 +49,6 @@ joystick::_getDevices (Napi::Env &env)
39
49
  throw Napi::Error::New(env, message.str());
40
50
  }
41
51
 
42
- SDL_JoystickType type = SDL_JoystickGetDeviceType(i);
43
- // This function can only error if the index is invalid.
44
-
45
52
  const char *name = SDL_JoystickNameForIndex(i);
46
53
  if (name == nullptr) {
47
54
  std::ostringstream message;
@@ -58,53 +65,89 @@ joystick::_getDevices (Napi::Env &env)
58
65
  throw Napi::Error::New(env, message.str());
59
66
  }
60
67
 
61
- SDL_JoystickGUID guid = SDL_JoystickGetDeviceGUID(i);
62
68
  // This function can only error if the index is invalid.
69
+ SDL_JoystickGUID guid = SDL_JoystickGetDeviceGUID(i);
63
70
 
64
71
  char guid_string[33];
65
72
  SDL_JoystickGetGUIDString(guid, guid_string, 33);
66
73
 
74
+ // This function can only error if the index is invalid.
75
+ SDL_JoystickType _type = SDL_JoystickGetDeviceType(i);
76
+ Napi::Value type = _type != SDL_JOYSTICK_TYPE_UNKNOWN
77
+ ? Napi::String::New(env, joystick::types[_type])
78
+ : env.Null();
79
+
67
80
  int _vendor = SDL_JoystickGetDeviceVendor(i);
68
- Napi::Value vendor = _vendor == 0 ? env.Null() : Napi::Number::New(env, _vendor);
81
+ Napi::Value vendor = _vendor != 0
82
+ ? Napi::Number::New(env, _vendor)
83
+ : env.Null();
69
84
 
70
85
  int _product = SDL_JoystickGetDeviceProduct(i);
71
- Napi::Value product = _product == 0 ? env.Null() : Napi::Number::New(env, _product);
86
+ Napi::Value product = _product != 0
87
+ ? Napi::Number::New(env, _product)
88
+ : env.Null();
72
89
 
73
90
  int _Version = SDL_JoystickGetDeviceProductVersion(i);
74
- Napi::Value version = _Version == 0 ? env.Null() : Napi::Number::New(env, _Version);
91
+ Napi::Value version = _Version != 0
92
+ ? Napi::Number::New(env, _Version)
93
+ : env.Null();
75
94
 
76
95
  int _player = SDL_JoystickGetDevicePlayerIndex(i);
77
- Napi::Value player = _player == -1 ? env.Null() : Napi::Number::New(env, _player);
96
+ Napi::Value player = _player != -1
97
+ ? Napi::Number::New(env, _player)
98
+ : env.Null();
78
99
 
79
100
  bool is_controller = SDL_IsGameController(i);
80
101
 
81
- Napi::Value mapping;
102
+ Napi::Value controller_mapping;
103
+ Napi::Value controller_name;
104
+ Napi::Value controller_type;
105
+
82
106
  if (is_controller) {
83
- const char *device_mapping = SDL_GameControllerMappingForDeviceIndex(i);
84
- if (device_mapping == nullptr) {
107
+ const char *_controller_mapping = SDL_GameControllerMappingForDeviceIndex(i);
108
+ if (_controller_mapping == nullptr) {
85
109
  std::ostringstream message;
86
110
  message << "SDL_GameControllerMappingForDeviceIndex(" << i << ") error: " << SDL_GetError();
87
111
  SDL_ClearError();
88
112
  throw Napi::Error::New(env, message.str());
89
113
  }
90
- mapping = Napi::String::New(env, device_mapping);
114
+ controller_mapping = Napi::String::New(env, _controller_mapping);
115
+
116
+ const char *_controller_name = SDL_GameControllerNameForIndex(i);
117
+ if (_controller_name == nullptr) {
118
+ std::ostringstream message;
119
+ message << "SDL_GameControllerNameForIndex(" << i << ") error: " << SDL_GetError();
120
+ SDL_ClearError();
121
+ throw Napi::Error::New(env, message.str());
122
+ }
123
+ controller_name = Napi::String::New(env, _controller_name);
124
+
125
+ // This function can only error if the index is invalid.
126
+ SDL_GameControllerType _controller_type = SDL_GameControllerTypeForIndex(i);
127
+ controller_type = _controller_type != SDL_CONTROLLER_TYPE_UNKNOWN
128
+ ? Napi::String::New(env, controller::types[_controller_type])
129
+ : env.Null();
91
130
  } else {
92
- mapping = env.Null();
131
+ controller_mapping = env.Null();
132
+ controller_name = env.Null();
133
+ controller_type = env.Null();
93
134
  }
94
135
 
95
136
  Napi::Object device = Napi::Object::New(env);
96
- device.Set("_index", Napi::Number::New(env, i));
97
- device.Set("id", Napi::Number::New(env, id));
98
- device.Set("type", joystick::joystick_types[type]);
99
- device.Set("name", Napi::String::New(env, name));
100
- device.Set("path", Napi::String::New(env, path));
101
- device.Set("guid", Napi::String::New(env, guid_string));
137
+ device.Set("_index", i);
138
+ device.Set("id", id);
139
+ device.Set("name", name);
140
+ device.Set("path", path);
141
+ device.Set("guid", guid_string);
142
+ device.Set("type", type);
102
143
  device.Set("vendor", vendor);
103
144
  device.Set("product", product);
104
145
  device.Set("version", version);
105
146
  device.Set("player", player);
106
- device.Set("isController", Napi::Boolean::New(env, is_controller));
107
- device.Set("mapping", mapping);
147
+ device.Set("isController", is_controller);
148
+ device.Set("controllerMapping", controller_mapping);
149
+ device.Set("controllerName", controller_name);
150
+ device.Set("controllerType", controller_type);
108
151
 
109
152
  devices.Set(i, device);
110
153
  }
@@ -136,19 +179,20 @@ joystick::open (const Napi::CallbackInfo &info)
136
179
  throw Napi::Error::New(env, message.str());
137
180
  }
138
181
 
139
- SDL_JoystickID joystick_id = SDL_JoystickInstanceID(joystick);
140
- if (joystick_id < 0) {
141
- std::ostringstream message;
142
- message << "SDL_JoystickInstanceID(" << index << ") error: " << SDL_GetError();
143
- SDL_ClearError();
144
- throw Napi::Error::New(env, message.str());
145
- }
182
+ // SDL_JoystickOpen produces errors even though it succeeds
183
+ const char *error = SDL_GetError();
184
+ if (error != global::no_error) { fprintf(stderr, "SDL silent error: %s\n", error); }
185
+ SDL_ClearError();
146
186
 
147
187
  int _firmware_version = SDL_JoystickGetFirmwareVersion(joystick);
148
- Napi::Value firmware_version = _firmware_version == 0 ? env.Null() : Napi::Number::New(env, _firmware_version);
188
+ Napi::Value firmware_version = _firmware_version != 0
189
+ ? Napi::Number::New(env, _firmware_version)
190
+ : env.Null();
149
191
 
150
192
  const char *_serial_number = SDL_JoystickGetSerial(joystick);
151
- Napi::Value serial_number = _serial_number == nullptr ? env.Null() : Napi::String::New(env, _serial_number);
193
+ Napi::Value serial_number = _serial_number != nullptr
194
+ ? Napi::String::New(env, _serial_number)
195
+ : env.Null();
152
196
 
153
197
  SDL_bool has_led = SDL_JoystickHasLED(joystick);
154
198
  SDL_bool has_rumble = SDL_JoystickHasRumble(joystick);
@@ -164,12 +208,8 @@ joystick::open (const Napi::CallbackInfo &info)
164
208
 
165
209
  Napi::Array axes = Napi::Array::New(env, num_axes);
166
210
 
167
- const char *error = SDL_GetError();
168
- if (error != global::no_error) { fprintf(stderr, "SDL silent error: %s\n", error); }
169
- SDL_ClearError();
170
-
171
211
  for (int i = 0; i < num_axes; i++) {
172
- double value = joystick::mapAxis(SDL_JoystickGetAxis(joystick, i));
212
+ double value = joystick::mapAxis(joystick, i);
173
213
  error = SDL_GetError();
174
214
  if (error != global::no_error) {
175
215
  std::ostringstream message;
@@ -177,7 +217,7 @@ joystick::open (const Napi::CallbackInfo &info)
177
217
  SDL_ClearError();
178
218
  throw Napi::Error::New(env, message.str());
179
219
  }
180
- axes.Set(i, Napi::Number::New(env, value));
220
+ axes.Set(i, value);
181
221
  }
182
222
 
183
223
  int num_balls = SDL_JoystickNumBalls(joystick);
@@ -200,8 +240,8 @@ joystick::open (const Napi::CallbackInfo &info)
200
240
  }
201
241
 
202
242
  Napi::Object ball = Napi::Object::New(env);
203
- ball.Set("x", Napi::Number::New(env, dx));
204
- ball.Set("y", Napi::Number::New(env, dy));
243
+ ball.Set("x", dx);
244
+ ball.Set("y", dy);
205
245
 
206
246
  balls.Set(i, ball);
207
247
  }
@@ -217,8 +257,8 @@ joystick::open (const Napi::CallbackInfo &info)
217
257
  Napi::Array buttons = Napi::Array::New(env, num_buttons);
218
258
 
219
259
  for (int i = 0; i < num_buttons; i++) {
220
- int pressed = SDL_JoystickGetButton(joystick, i);
221
- buttons.Set(i, Napi::Boolean::New(env, pressed));
260
+ bool pressed = SDL_JoystickGetButton(joystick, i);
261
+ buttons.Set(i, pressed);
222
262
  }
223
263
 
224
264
  int num_hats = SDL_JoystickNumHats(joystick);
@@ -237,12 +277,11 @@ joystick::open (const Napi::CallbackInfo &info)
237
277
  }
238
278
 
239
279
  Napi::Object result = Napi::Object::New(env);
240
- // result.Set("id", Napi::Number::New(env, joystick_id)); // NOTE: Unused. Same as the device id
241
280
  result.Set("firmwareVersion", firmware_version);
242
281
  result.Set("serialNumber", serial_number);
243
- result.Set("hasLed", Napi::Boolean::New(env, has_led));
244
- result.Set("hasRumble", Napi::Boolean::New(env, has_rumble));
245
- result.Set("hasRumbleTriggers", Napi::Boolean::New(env, has_rumble_triggers));
282
+ result.Set("hasLed", !!has_led);
283
+ result.Set("hasRumble", !!has_rumble);
284
+ result.Set("hasRumbleTriggers", !!has_rumble_triggers);
246
285
  result.Set("axes", axes);
247
286
  result.Set("balls", balls);
248
287
  result.Set("buttons", buttons);
@@ -8,11 +8,12 @@
8
8
 
9
9
  namespace joystick {
10
10
 
11
- extern std::map<SDL_JoystickType, std::string> joystick_types;
11
+ extern std::map<SDL_JoystickType, std::string> types;
12
12
  extern std::map<Uint8, std::string> hat_positions;
13
13
  extern std::map<SDL_JoystickPowerLevel, std::string> power_levels;
14
14
 
15
- double mapAxis (int value);
15
+ double mapAxis (SDL_Joystick *joystick, int axis);
16
+ double mapAxisValue (SDL_Joystick *joystick, int axis, int value);
16
17
  Napi::Array _getDevices (Napi::Env &env);
17
18
 
18
19
  Napi::Value getDevices(const Napi::CallbackInfo &info);
@@ -50,7 +50,7 @@ keyboard::getState(const Napi::CallbackInfo &info)
50
50
  Napi::Array result = Napi::Array::New(env, keyboard::num_keys);
51
51
 
52
52
  for (int i = 0 ; i < keyboard::num_keys; i++) {
53
- result.Set(i, Napi::Boolean::New(env, !!keyboard::keys[i]));
53
+ result.Set(i, !!keyboard::keys[i]);
54
54
  }
55
55
 
56
56
  return result;
@@ -28,8 +28,8 @@ mouse::getPosition (const Napi::CallbackInfo &info)
28
28
  SDL_GetGlobalMouseState(&x, &y);
29
29
 
30
30
  Napi::Object result = Napi::Object::New(env);
31
- result.Set("x", Napi::Number::New(env, x));
32
- result.Set("y", Napi::Number::New(env, y));
31
+ result.Set("x", x);
32
+ result.Set("y", y);
33
33
 
34
34
  return result;
35
35
  }
@@ -14,9 +14,15 @@ power::getInfo (const Napi::CallbackInfo &info)
14
14
  int _seconds, _percent;
15
15
  SDL_PowerState _state = SDL_GetPowerInfo(&_seconds, &_percent);
16
16
 
17
- Napi::Value state = _state == SDL_POWERSTATE_UNKNOWN ? env.Null() : Napi::String::New(env, power::states[_state]);
18
- Napi::Value seconds = _seconds == -1 ? env.Null() : Napi::Number::New(env, _seconds);
19
- Napi::Value percent = _percent == -1 ? env.Null() : Napi::Number::New(env, _percent);
17
+ Napi::Value state = _state != SDL_POWERSTATE_UNKNOWN
18
+ ? Napi::String::New(env, power::states[_state])
19
+ : env.Null();
20
+ Napi::Value seconds = _seconds != -1
21
+ ? Napi::Number::New(env, _seconds)
22
+ : env.Null();
23
+ Napi::Value percent = _percent != -1
24
+ ? Napi::Number::New(env, _percent)
25
+ : env.Null();
20
26
 
21
27
  Napi::Object result = Napi::Object::New(env);
22
28
  result.Set("state", state);
@@ -33,18 +33,26 @@ sensor::getDevices (const Napi::CallbackInfo &info)
33
33
  throw Napi::Error::New(env, message.str());
34
34
  }
35
35
 
36
- SDL_SensorType _type = SDL_SensorGetDeviceType(i);
37
36
  // This function can only error if the index is invalid.
38
- Napi::Value type = _type == SDL_SENSOR_UNKNOWN ? env.Null() : Napi::String::New(env, sensor::types[_type]);
39
- Napi::Value side = _type == SDL_SENSOR_UNKNOWN || _type == SDL_SENSOR_ACCEL || _type == SDL_SENSOR_GYRO ? env.Null() : Napi::String::New(env, sensor::sides[_type]);
37
+ SDL_SensorType _type = SDL_SensorGetDeviceType(i);
38
+ Napi::Value type = _type != SDL_SENSOR_UNKNOWN
39
+ ? Napi::String::New(env, sensor::types[_type])
40
+ : env.Null();
41
+ Napi::Value side = (
42
+ _type != SDL_SENSOR_UNKNOWN &&
43
+ _type != SDL_SENSOR_ACCEL &&
44
+ _type != SDL_SENSOR_GYRO
45
+ )
46
+ ? Napi::String::New(env, sensor::sides[_type])
47
+ : env.Null();
40
48
 
41
- const char *name = SDL_SensorGetDeviceName(i);
42
49
  // This function can only error if the index is invalid.
50
+ const char *name = SDL_SensorGetDeviceName(i);
43
51
 
44
52
  Napi::Object device = Napi::Object::New(env);
45
- device.Set("_index", Napi::Number::New(env, i));
46
- device.Set("id", Napi::Number::New(env, id));
47
- device.Set("name", Napi::String::New(env, name));
53
+ device.Set("_index", i);
54
+ device.Set("id", id);
55
+ device.Set("name", name);
48
56
  device.Set("type", type);
49
57
  device.Set("side", side);
50
58
 
@@ -87,20 +95,23 @@ sensor::getData (const Napi::CallbackInfo &info)
87
95
  throw Napi::Error::New(env, message.str());
88
96
  }
89
97
 
90
- Uint64 timestamp;
98
+ Uint64 _timestamp;
91
99
  float data[3];
92
- if (SDL_SensorGetDataWithTimestamp(sensor, &timestamp, data, 3) == -1) {
100
+ if (SDL_SensorGetDataWithTimestamp(sensor, &_timestamp, data, 3) == -1) {
93
101
  std::ostringstream message;
94
102
  message << "SDL_SensorGetDataWithTimestamp(" << sensor_id << ") error: " << SDL_GetError();
95
103
  SDL_ClearError();
96
104
  throw Napi::Error::New(env, message.str());
97
105
  }
106
+ Napi::Value timestamp = _timestamp != 0
107
+ ? Napi::Number::New(env, _timestamp)
108
+ : env.Null();
98
109
 
99
110
  Napi::Object result = Napi::Object::New(env);
100
- result.Set("timestamp", timestamp == 0 ? env.Null() : Napi::Number::New(env, timestamp));
101
- result.Set("x", Napi::Number::New(env, data[0]));
102
- result.Set("y", Napi::Number::New(env, data[1]));
103
- result.Set("z", Napi::Number::New(env, data[2]));
111
+ result.Set("timestamp", timestamp);
112
+ result.Set("x", data[0]);
113
+ result.Set("y", data[1]);
114
+ result.Set("z", data[2]);
104
115
 
105
116
  return result;
106
117
  }
@@ -5,6 +5,7 @@
5
5
  #include <map>
6
6
 
7
7
 
8
+ std::map<SDL_DisplayOrientation, std::string> video::orientations;
8
9
  std::map<SDL_PixelFormatEnum, std::string> video::formats;
9
10
 
10
11
 
@@ -49,10 +50,10 @@ video::getDisplays(const Napi::CallbackInfo &info)
49
50
  }
50
51
 
51
52
  Napi::Object geometry = Napi::Object::New(env);
52
- geometry.Set("x", Napi::Number::New(env, rect.x));
53
- geometry.Set("y", Napi::Number::New(env, rect.y));
54
- geometry.Set("width", Napi::Number::New(env, rect.w));
55
- geometry.Set("height", Napi::Number::New(env, rect.h));
53
+ geometry.Set("x", rect.x);
54
+ geometry.Set("y", rect.y);
55
+ geometry.Set("width", rect.w);
56
+ geometry.Set("height", rect.h);
56
57
 
57
58
  if(SDL_GetDisplayUsableBounds(i, &rect) < 0) {
58
59
  std::ostringstream message;
@@ -62,10 +63,10 @@ video::getDisplays(const Napi::CallbackInfo &info)
62
63
  }
63
64
 
64
65
  Napi::Object usable = Napi::Object::New(env);
65
- usable.Set("x", Napi::Number::New(env, rect.x));
66
- usable.Set("y", Napi::Number::New(env, rect.y));
67
- usable.Set("width", Napi::Number::New(env, rect.w));
68
- usable.Set("height", Napi::Number::New(env, rect.h));
66
+ usable.Set("x", rect.x);
67
+ usable.Set("y", rect.y);
68
+ usable.Set("width", rect.w);
69
+ usable.Set("height", rect.h);
69
70
 
70
71
  Napi::Value dpi;
71
72
  float ddpi, hdpi, vdpi;
@@ -73,19 +74,26 @@ video::getDisplays(const Napi::CallbackInfo &info)
73
74
  dpi = env.Null();
74
75
  } else {
75
76
  Napi::Object dpi_obj = Napi::Object::New(env);
76
- dpi_obj.Set("diagonal", Napi::Number::New(env, ddpi));
77
- dpi_obj.Set("horizontal", Napi::Number::New(env, hdpi));
78
- dpi_obj.Set("vertical", Napi::Number::New(env, vdpi));
77
+ dpi_obj.Set("diagonal", ddpi);
78
+ dpi_obj.Set("horizontal", hdpi);
79
+ dpi_obj.Set("vertical", vdpi);
79
80
  dpi = dpi_obj;
80
81
  }
81
82
 
83
+ SDL_DisplayOrientation _orientation = SDL_GetDisplayOrientation(i);
84
+ Napi::Value orientation = _orientation != SDL_ORIENTATION_UNKNOWN
85
+ ? Napi::String::New(env, video::orientations[_orientation])
86
+ : env.Null();
87
+
82
88
  Napi::Object display = Napi::Object::New(env);
83
- display.Set("name", Napi::String::New(env, name));
89
+ display.Set("_index", i);
90
+ display.Set("name", name);
84
91
  display.Set("format", video::formats[(SDL_PixelFormatEnum) mode.format]);
85
- display.Set("frequency", Napi::Number::New(env, mode.refresh_rate));
92
+ display.Set("frequency", mode.refresh_rate);
86
93
  display.Set("geometry", geometry);
87
94
  display.Set("usable", usable);
88
95
  display.Set("dpi", dpi);
96
+ display.Set("orientation", orientation);
89
97
 
90
98
  displays.Set(i, display);
91
99
  }
@@ -7,6 +7,7 @@
7
7
 
8
8
  namespace video {
9
9
 
10
+ extern std::map<SDL_DisplayOrientation, std::string> orientations;
10
11
  extern std::map<SDL_PixelFormatEnum, std::string> formats;
11
12
 
12
13
  Napi::Value getDisplays(const Napi::CallbackInfo &info);
@@ -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;
@@ -221,24 +228,25 @@ window::create (const Napi::CallbackInfo &info)
221
228
  if (is_visible) { SDL_ShowWindow(window); }
222
229
 
223
230
  Napi::Object result = Napi::Object::New(env);
224
- result.Set("id", Napi::Number::New(env, window_id));
225
- result.Set("x", Napi::Number::New(env, x));
226
- result.Set("y", Napi::Number::New(env, y));
227
- result.Set("width", Napi::Number::New(env, width));
228
- result.Set("height", Napi::Number::New(env, height));
229
- result.Set("pixelWidth", Napi::Number::New(env, pixel_width));
230
- result.Set("pixelHeight", Napi::Number::New(env, pixel_height));
231
- result.Set("fullscreen", Napi::Boolean::New(env, is_fullscreen));
232
- result.Set("resizable", Napi::Boolean::New(env, is_resizable));
233
- result.Set("borderless", Napi::Boolean::New(env, is_borderless));
234
- result.Set("alwaysOnTop", Napi::Boolean::New(env, is_always_on_top));
235
- result.Set("accelerated", Napi::Boolean::New(env, is_accelerated));
236
- result.Set("vsync", Napi::Boolean::New(env, is_vsync));
231
+ result.Set("id", window_id);
232
+ result.Set("x", x);
233
+ result.Set("y", y);
234
+ result.Set("width", width);
235
+ result.Set("height", height);
236
+ result.Set("pixelWidth", pixel_width);
237
+ result.Set("pixelHeight", pixel_height);
238
+ result.Set("displayIndex", display);
239
+ result.Set("fullscreen", is_fullscreen);
240
+ result.Set("resizable", is_resizable);
241
+ result.Set("borderless", is_borderless);
242
+ result.Set("alwaysOnTop", is_always_on_top);
243
+ result.Set("accelerated", is_accelerated);
244
+ result.Set("vsync", is_vsync);
237
245
  result.Set("native", native);
238
- result.Set("skipTaskbar", Napi::Boolean::New(env, should_skip_taskbar));
239
- result.Set("popupMenu", Napi::Boolean::New(env, is_popup_menu));
240
- result.Set("tooltip", Napi::Boolean::New(env, is_tooltip));
241
- result.Set("utility", Napi::Boolean::New(env, is_utility));
246
+ result.Set("skipTaskbar", should_skip_taskbar);
247
+ result.Set("popupMenu", is_popup_menu);
248
+ result.Set("tooltip", is_tooltip);
249
+ result.Set("utility", is_utility);
242
250
 
243
251
  return result;
244
252
  }
@@ -309,8 +317,8 @@ window::setSize (const Napi::CallbackInfo &info)
309
317
  SDL_GetWindowSizeInPixels(window, &pixel_width, &pixel_height);
310
318
 
311
319
  Napi::Object result = Napi::Object::New(env);
312
- result.Set("pixelWidth", Napi::Number::New(env, pixel_width));
313
- result.Set("pixelHeight", Napi::Number::New(env, pixel_height));
320
+ result.Set("pixelWidth", pixel_width);
321
+ result.Set("pixelHeight", pixel_height);
314
322
 
315
323
  return result;
316
324
  }
@@ -412,8 +420,8 @@ window::setAcceleratedAndVsync (const Napi::CallbackInfo &info)
412
420
  updateRenderer(env, window, &is_accelerated, &is_vsync);
413
421
 
414
422
  Napi::Object result = Napi::Object::New(env);
415
- result.Set("accelerated", Napi::Boolean::New(env, is_accelerated));
416
- result.Set("vsync", Napi::Boolean::New(env, is_vsync));
423
+ result.Set("accelerated", is_accelerated);
424
+ result.Set("vsync", is_vsync);
417
425
 
418
426
  return result;
419
427
  }
@@ -584,22 +592,20 @@ 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
 
591
598
  texture = SDL_CreateTexture(renderer, format, SDL_TEXTUREACCESS_STREAMING, width, height);
592
-
593
- if (SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_NONE) < 0) {
599
+ if (texture == nullptr) {
594
600
  std::ostringstream message;
595
- message << "SDL_SetTextureBlendMode(" << window_id << ") error: " << SDL_GetError();
601
+ message << "SDL_CreateTexture(" << width << ", " << height << ", " << format << ") error: " << SDL_GetError();
596
602
  SDL_ClearError();
597
603
  throw Napi::Error::New(env, message.str());
598
604
  }
599
605
 
600
- if(SDL_SetTextureScaleMode(texture, scaling) < 0) {
606
+ if (SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_NONE) < 0) {
601
607
  std::ostringstream message;
602
- message << "SDL_SetTextureScaleMode(" << window_id << ", " << scaling << ") error: " << SDL_GetError();
608
+ message << "SDL_SetTextureBlendMode(" << window_id << ") error: " << SDL_GetError();
603
609
  SDL_ClearError();
604
610
  throw Napi::Error::New(env, message.str());
605
611
  }
@@ -608,7 +614,13 @@ window::render (const Napi::CallbackInfo &info)
608
614
  cached.width = width;
609
615
  cached.height = height;
610
616
  cached.format = format;
611
- cached.scaling = scaling;
617
+ }
618
+
619
+ if(SDL_SetTextureScaleMode(texture, scaling) < 0) {
620
+ std::ostringstream message;
621
+ message << "SDL_SetTextureScaleMode(" << window_id << ", " << scaling << ") error: " << SDL_GetError();
622
+ SDL_ClearError();
623
+ throw Napi::Error::New(env, message.str());
612
624
  }
613
625
 
614
626
  SDL_UpdateTexture(texture, nullptr, pixels, stride);