@kmamal/sdl 0.10.1 → 0.10.3
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 +11 -3
- package/package.json +4 -4
- package/scripts/build.mjs +6 -1
- package/src/native/events.cpp +148 -152
- package/src/native/mouse.cpp +3 -2
- package/src/native/window.cpp +10 -2
package/README.md
CHANGED
|
@@ -48,6 +48,7 @@ window.on('*', console.log)
|
|
|
48
48
|
import sdl from '@kmamal/sdl'
|
|
49
49
|
import { createCanvas } from 'canvas'
|
|
50
50
|
|
|
51
|
+
// Setup
|
|
51
52
|
const window = sdl.video.createWindow({ title: "Canvas" })
|
|
52
53
|
const { pixelWidth: width, pixelHeight: height } = window
|
|
53
54
|
const canvas = createCanvas(width, height)
|
|
@@ -56,6 +57,8 @@ const ctx = canvas.getContext('2d')
|
|
|
56
57
|
// Clear screen to red
|
|
57
58
|
ctx.fillStyle = 'red'
|
|
58
59
|
ctx.fillRect(0, 0, width, height)
|
|
60
|
+
|
|
61
|
+
// Render to window
|
|
59
62
|
const buffer = canvas.toBuffer('raw')
|
|
60
63
|
window.render(width, height, width * 4, 'bgra32', buffer)
|
|
61
64
|
```
|
|
@@ -65,6 +68,7 @@ window.render(width, height, width * 4, 'bgra32', buffer)
|
|
|
65
68
|
import sdl from '@kmamal/sdl'
|
|
66
69
|
import createContext from '@kmamal/gl'
|
|
67
70
|
|
|
71
|
+
// Setup
|
|
68
72
|
const window = sdl.video.createWindow({ title: "WebGL", opengl: true })
|
|
69
73
|
const { pixelWidth: width, pixelHeight: height, native } = window
|
|
70
74
|
const gl = createContext(width, height, { window: native })
|
|
@@ -72,6 +76,8 @@ const gl = createContext(width, height, { window: native })
|
|
|
72
76
|
// Clear screen to red
|
|
73
77
|
gl.clearColor(1, 0, 0, 1)
|
|
74
78
|
gl.clear(gl.COLOR_BUFFER_BIT)
|
|
79
|
+
|
|
80
|
+
// Render to window
|
|
75
81
|
gl.swap()
|
|
76
82
|
```
|
|
77
83
|
|
|
@@ -80,12 +86,11 @@ gl.swap()
|
|
|
80
86
|
import sdl from '@kmamal/sdl'
|
|
81
87
|
import gpu from '@kmamal/gpu'
|
|
82
88
|
|
|
89
|
+
// Setup
|
|
83
90
|
const window = sdl.video.createWindow({ title: "WebGPU", webgpu: true })
|
|
84
|
-
|
|
85
91
|
const instance = gpu.create([])
|
|
86
92
|
const adapter = await instance.requestAdapter()
|
|
87
93
|
const device = await adapter.requestDevice()
|
|
88
|
-
|
|
89
94
|
const renderer = gpu.renderGPUDeviceToWindow({ device, window })
|
|
90
95
|
|
|
91
96
|
// Clear screen to red
|
|
@@ -103,11 +108,14 @@ const renderPass = commandEncoder.beginRenderPass({
|
|
|
103
108
|
renderPass.end()
|
|
104
109
|
device.queue.submit([ commandEncoder.finish() ])
|
|
105
110
|
|
|
111
|
+
// Render to window
|
|
106
112
|
renderer.swap()
|
|
107
113
|
|
|
108
114
|
```
|
|
109
115
|
|
|
110
|
-
|
|
116
|
+
### More examples
|
|
117
|
+
|
|
118
|
+
Check the [`examples/`](https://github.com/kmamal/node-sdl/tree/master/examples) folder.
|
|
111
119
|
|
|
112
120
|
|
|
113
121
|
# API Reference
|
package/package.json
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
{
|
|
2
|
-
"version": "0.10.
|
|
2
|
+
"version": "0.10.3",
|
|
3
3
|
"name": "@kmamal/sdl",
|
|
4
4
|
"description": "SDL bindings for Node.js",
|
|
5
5
|
"keywords": [
|
|
@@ -34,7 +34,7 @@
|
|
|
34
34
|
"license": "MIT",
|
|
35
35
|
"repository": {
|
|
36
36
|
"type": "git",
|
|
37
|
-
"url": "git://github.com/kmamal/node-sdl.git"
|
|
37
|
+
"url": "git+ssh://git@github.com/kmamal/node-sdl.git"
|
|
38
38
|
},
|
|
39
39
|
"main": "./src/javascript/index.js",
|
|
40
40
|
"types": "./src/types/index.d.ts",
|
|
@@ -65,8 +65,8 @@
|
|
|
65
65
|
},
|
|
66
66
|
"devDependencies": {
|
|
67
67
|
"@kmamal/evdev": "^0.0.1",
|
|
68
|
-
"@kmamal/testing": "^0.0.
|
|
69
|
-
"@types/node": "^20.11.
|
|
68
|
+
"@kmamal/testing": "^0.0.30",
|
|
69
|
+
"@types/node": "^20.11.20",
|
|
70
70
|
"node-addon-api": "^7.1.0",
|
|
71
71
|
"node-gyp": "^10.0.1"
|
|
72
72
|
}
|
package/scripts/build.mjs
CHANGED
|
@@ -20,8 +20,13 @@ if (process.env.CROSS_COMPILE_ARCH) {
|
|
|
20
20
|
archFlag = `--arch ${process.env.CROSS_COMPILE_ARCH}`
|
|
21
21
|
}
|
|
22
22
|
|
|
23
|
+
let parallelFlag = '-j max'
|
|
24
|
+
if (process.env.NO_PARALLEL) {
|
|
25
|
+
parallelFlag = ''
|
|
26
|
+
}
|
|
27
|
+
|
|
23
28
|
process.chdir(C.dir.root)
|
|
24
|
-
execSync(`npx -y node-gyp rebuild ${archFlag}
|
|
29
|
+
execSync(`npx -y node-gyp rebuild ${archFlag} ${parallelFlag} --verbose`, {
|
|
25
30
|
stdio: 'inherit',
|
|
26
31
|
env: {
|
|
27
32
|
...process.env,
|
package/src/native/events.cpp
CHANGED
|
@@ -63,41 +63,47 @@ std::string events::types::UPDATE;
|
|
|
63
63
|
static Napi::Env *poll_env = nullptr;
|
|
64
64
|
static Napi::Function *poll_callback = nullptr;
|
|
65
65
|
|
|
66
|
-
|
|
67
|
-
|
|
66
|
+
void
|
|
67
|
+
events::dispatchEvent(const SDL_Event &event)
|
|
68
68
|
{
|
|
69
|
-
|
|
69
|
+
if (poll_env == nullptr) { return; }
|
|
70
|
+
Napi::Env &env = *poll_env;
|
|
71
|
+
|
|
72
|
+
Napi::Object packed = Napi::Object::New(env);
|
|
70
73
|
|
|
71
74
|
switch (event.type) {
|
|
72
75
|
case SDL_DISPLAYEVENT: {
|
|
73
|
-
|
|
74
|
-
|
|
76
|
+
packed.Set("family", events::families::DISPLAY);
|
|
77
|
+
packed.Set("displayIndex", Napi::Number::New(env, event.display.display));
|
|
75
78
|
|
|
76
79
|
switch (event.display.event) {
|
|
77
|
-
case SDL_DISPLAYEVENT_CONNECTED: {
|
|
78
|
-
case SDL_DISPLAYEVENT_DISCONNECTED: {
|
|
79
|
-
case SDL_DISPLAYEVENT_ORIENTATION: {
|
|
80
|
+
case SDL_DISPLAYEVENT_CONNECTED: { packed.Set("type", events::types::DISPLAY_ADD); break; }
|
|
81
|
+
case SDL_DISPLAYEVENT_DISCONNECTED: { packed.Set("type", events::types::DISPLAY_REMOVE); break; }
|
|
82
|
+
case SDL_DISPLAYEVENT_ORIENTATION: { packed.Set("type", events::types::DISPLAY_ORIENT); break; }
|
|
80
83
|
}
|
|
81
84
|
break;
|
|
82
85
|
}
|
|
83
86
|
|
|
84
87
|
case SDL_WINDOWEVENT: {
|
|
85
|
-
|
|
86
|
-
|
|
88
|
+
packed.Set("family", events::families::WINDOW);
|
|
89
|
+
packed.Set("windowId", Napi::Number::New(env, event.window.windowID));
|
|
87
90
|
|
|
88
91
|
switch (event.window.event) {
|
|
89
|
-
case SDL_WINDOWEVENT_SHOWN: {
|
|
90
|
-
case SDL_WINDOWEVENT_HIDDEN: {
|
|
91
|
-
case SDL_WINDOWEVENT_EXPOSED: {
|
|
92
|
+
case SDL_WINDOWEVENT_SHOWN: { packed.Set("type", events::types::SHOW); break; }
|
|
93
|
+
case SDL_WINDOWEVENT_HIDDEN: { packed.Set("type", events::types::HIDE); break; }
|
|
94
|
+
case SDL_WINDOWEVENT_EXPOSED: { packed.Set("type", events::types::EXPOSE); break; }
|
|
92
95
|
case SDL_WINDOWEVENT_MOVED: {
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
+
packed.Set("type", events::types::MOVE);
|
|
97
|
+
packed.Set("x", Napi::Number::New(env, event.window.data1));
|
|
98
|
+
packed.Set("y", Napi::Number::New(env, event.window.data2));
|
|
96
99
|
break;
|
|
97
100
|
}
|
|
98
101
|
case SDL_WINDOWEVENT_SIZE_CHANGED: {
|
|
99
102
|
SDL_Window *window = SDL_GetWindowFromID(event.window.windowID);
|
|
100
103
|
if (window == nullptr) {
|
|
104
|
+
const char *error = SDL_GetError();
|
|
105
|
+
if (error == global::no_error) { return; }
|
|
106
|
+
|
|
101
107
|
std::ostringstream message;
|
|
102
108
|
message << "SDL_GetWindowFromID() error: " << SDL_GetError();
|
|
103
109
|
SDL_ClearError();
|
|
@@ -107,171 +113,171 @@ packageEvent (Napi::Env &env, const SDL_Event &event)
|
|
|
107
113
|
int pixel_width, pixel_height;
|
|
108
114
|
SDL_GetWindowSizeInPixels(window, &pixel_width, &pixel_height);
|
|
109
115
|
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
116
|
+
packed.Set("type", events::types::RESIZE);
|
|
117
|
+
packed.Set("width", Napi::Number::New(env, event.window.data1));
|
|
118
|
+
packed.Set("height", Napi::Number::New(env, event.window.data2));
|
|
119
|
+
packed.Set("pixelWidth", Napi::Number::New(env, pixel_width));
|
|
120
|
+
packed.Set("pixelHeight", Napi::Number::New(env, pixel_height));
|
|
115
121
|
break;
|
|
116
122
|
}
|
|
117
|
-
case SDL_WINDOWEVENT_MINIMIZED: {
|
|
118
|
-
case SDL_WINDOWEVENT_MAXIMIZED: {
|
|
119
|
-
case SDL_WINDOWEVENT_RESTORED: {
|
|
120
|
-
case SDL_WINDOWEVENT_FOCUS_GAINED: {
|
|
121
|
-
case SDL_WINDOWEVENT_FOCUS_LOST: {
|
|
122
|
-
case SDL_WINDOWEVENT_ENTER: {
|
|
123
|
-
case SDL_WINDOWEVENT_LEAVE: {
|
|
124
|
-
case SDL_WINDOWEVENT_CLOSE: {
|
|
123
|
+
case SDL_WINDOWEVENT_MINIMIZED: { packed.Set("type", events::types::MINIMIZE); break; }
|
|
124
|
+
case SDL_WINDOWEVENT_MAXIMIZED: { packed.Set("type", events::types::MAXIMIZE); break; }
|
|
125
|
+
case SDL_WINDOWEVENT_RESTORED: { packed.Set("type", events::types::RESTORE); break; }
|
|
126
|
+
case SDL_WINDOWEVENT_FOCUS_GAINED: { packed.Set("type", events::types::FOCUS); break; }
|
|
127
|
+
case SDL_WINDOWEVENT_FOCUS_LOST: { packed.Set("type", events::types::BLUR); break; }
|
|
128
|
+
case SDL_WINDOWEVENT_ENTER: { packed.Set("type", events::types::HOVER); break; }
|
|
129
|
+
case SDL_WINDOWEVENT_LEAVE: { packed.Set("type", events::types::LEAVE); break; }
|
|
130
|
+
case SDL_WINDOWEVENT_CLOSE: { packed.Set("type", events::types::CLOSE); break; }
|
|
125
131
|
}
|
|
126
132
|
break;
|
|
127
133
|
}
|
|
128
134
|
|
|
129
135
|
case SDL_DROPBEGIN:
|
|
130
136
|
case SDL_DROPCOMPLETE: {
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
137
|
+
packed.Set("family", events::families::DROP);
|
|
138
|
+
packed.Set("type", event.type == SDL_DROPBEGIN ? events::types::DROP_BEGIN : events::types::DROP_COMPLETE);
|
|
139
|
+
packed.Set("windowId", Napi::Number::New(env, event.drop.windowID));
|
|
134
140
|
break;
|
|
135
141
|
}
|
|
136
142
|
case SDL_DROPFILE:
|
|
137
143
|
case SDL_DROPTEXT: {
|
|
138
|
-
|
|
144
|
+
packed.Set("family", events::families::DROP);
|
|
139
145
|
bool is_file = event.type == SDL_DROPFILE;
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
146
|
+
packed.Set("type", is_file ? events::types::DROP_FILE : events::types::DROP_TEXT);
|
|
147
|
+
packed.Set("windowId", Napi::Number::New(env, event.drop.windowID));
|
|
148
|
+
packed.Set(is_file ? "file" : "text", Napi::String::New(env, event.drop.file));
|
|
143
149
|
break;
|
|
144
150
|
}
|
|
145
151
|
|
|
146
152
|
case SDL_KEYDOWN:
|
|
147
153
|
case SDL_KEYUP: {
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
154
|
+
packed.Set("family", events::families::KEYBOARD);
|
|
155
|
+
packed.Set("type", event.type == SDL_KEYDOWN ? events::types::KEY_DOWN : events::types::KEY_UP);
|
|
156
|
+
packed.Set("windowId", Napi::Number::New(env, event.key.windowID));
|
|
151
157
|
|
|
152
158
|
SDL_Keysym symbol = event.key.keysym;
|
|
153
|
-
|
|
159
|
+
packed.Set("scancode", Napi::Number::New(env, symbol.scancode));
|
|
154
160
|
|
|
155
161
|
const char *name = SDL_GetKeyName(symbol.sym);
|
|
156
162
|
if (name[0] != '\0') {
|
|
157
|
-
|
|
163
|
+
packed.Set("key", Napi::String::New(env, name));
|
|
158
164
|
}
|
|
159
165
|
else {
|
|
160
|
-
|
|
166
|
+
packed.Set("key", env.Null());
|
|
161
167
|
}
|
|
162
168
|
|
|
163
169
|
if (event.type == SDL_KEYDOWN) {
|
|
164
|
-
|
|
170
|
+
packed.Set("repeat", Napi::Boolean::New(env, !!event.key.repeat));
|
|
165
171
|
}
|
|
166
172
|
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
173
|
+
packed.Set("alt", Napi::Boolean::New(env, !!(symbol.mod & KMOD_ALT)));
|
|
174
|
+
packed.Set("ctrl", Napi::Boolean::New(env, !!(symbol.mod & KMOD_CTRL)));
|
|
175
|
+
packed.Set("shift", Napi::Boolean::New(env, !!(symbol.mod & KMOD_SHIFT)));
|
|
176
|
+
packed.Set("super", Napi::Boolean::New(env, !!(symbol.mod & KMOD_GUI)));
|
|
177
|
+
packed.Set("altgr", Napi::Boolean::New(env, !!(symbol.mod & KMOD_MODE)));
|
|
178
|
+
packed.Set("numlock", Napi::Boolean::New(env, !!(symbol.mod & KMOD_NUM)));
|
|
179
|
+
packed.Set("capslock", Napi::Boolean::New(env, !!(symbol.mod & KMOD_CAPS)));
|
|
174
180
|
break;
|
|
175
181
|
}
|
|
176
182
|
|
|
177
183
|
case SDL_TEXTINPUT: {
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
184
|
+
packed.Set("family", events::families::TEXT);
|
|
185
|
+
packed.Set("type", events::types::TEXT_INPUT);
|
|
186
|
+
packed.Set("windowId", Napi::Number::New(env, event.text.windowID));
|
|
187
|
+
packed.Set("text", Napi::String::New(env, event.text.text));
|
|
182
188
|
break ;
|
|
183
189
|
}
|
|
184
190
|
|
|
185
191
|
case SDL_MOUSEMOTION: {
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
+
packed.Set("family", events::families::MOUSE);
|
|
193
|
+
packed.Set("type", events::types::MOUSE_MOVE);
|
|
194
|
+
packed.Set("windowId", Napi::Number::New(env, event.motion.windowID));
|
|
195
|
+
packed.Set("touch", Napi::Boolean::New(env, event.motion.which == SDL_TOUCH_MOUSEID));
|
|
196
|
+
packed.Set("x", Napi::Number::New(env, event.motion.x));
|
|
197
|
+
packed.Set("y", Napi::Number::New(env, event.motion.y));
|
|
192
198
|
break;
|
|
193
199
|
}
|
|
194
200
|
case SDL_MOUSEBUTTONDOWN:
|
|
195
201
|
case SDL_MOUSEBUTTONUP: {
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
202
|
+
packed.Set("family", events::families::MOUSE);
|
|
203
|
+
packed.Set("type", event.type == SDL_MOUSEBUTTONDOWN ? events::types::MOUSE_BUTTON_DOWN : events::types::MOUSE_BUTTON_UP);
|
|
204
|
+
packed.Set("windowId", Napi::Number::New(env, event.button.windowID));
|
|
205
|
+
packed.Set("touch", Napi::Boolean::New(env, event.button.which == SDL_TOUCH_MOUSEID));
|
|
206
|
+
packed.Set("button", Napi::Number::New(env, event.button.button));
|
|
207
|
+
packed.Set("x", Napi::Number::New(env, event.button.x));
|
|
208
|
+
packed.Set("y", Napi::Number::New(env, event.button.y));
|
|
203
209
|
break;
|
|
204
210
|
}
|
|
205
211
|
case SDL_MOUSEWHEEL: {
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
212
|
+
packed.Set("family", events::families::MOUSE);
|
|
213
|
+
packed.Set("type", events::types::MOUSE_WHEEL);
|
|
214
|
+
packed.Set("windowId", Napi::Number::New(env, event.wheel.windowID));
|
|
215
|
+
packed.Set("touch", Napi::Boolean::New(env, event.wheel.which == SDL_TOUCH_MOUSEID));
|
|
210
216
|
|
|
211
217
|
int x, y;
|
|
212
218
|
SDL_GetMouseState(&x, &y);
|
|
213
|
-
|
|
214
|
-
|
|
219
|
+
packed.Set("x", Napi::Number::New(env, x));
|
|
220
|
+
packed.Set("y", Napi::Number::New(env, y));
|
|
215
221
|
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
222
|
+
packed.Set("dx", Napi::Number::New(env, event.wheel.x));
|
|
223
|
+
packed.Set("dy", Napi::Number::New(env, event.wheel.y));
|
|
224
|
+
packed.Set("flipped", Napi::Boolean::New(env, event.wheel.direction));
|
|
219
225
|
break;
|
|
220
226
|
}
|
|
221
227
|
|
|
222
228
|
case SDL_JOYDEVICEADDED: {
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
229
|
+
packed.Set("family", events::families::JOYSTICK_DEVICE);
|
|
230
|
+
packed.Set("type", events::types::DEVICE_ADD);
|
|
231
|
+
packed.Set("devices", joystick::_getDevices(env));
|
|
226
232
|
break;
|
|
227
233
|
}
|
|
228
234
|
case SDL_JOYDEVICEREMOVED: {
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
235
|
+
packed.Set("family", events::families::JOYSTICK_DEVICE);
|
|
236
|
+
packed.Set("type", events::types::DEVICE_REMOVE);
|
|
237
|
+
packed.Set("joystickId", Napi::Number::New(env, event.jdevice.which));
|
|
238
|
+
packed.Set("devices", joystick::_getDevices(env));
|
|
233
239
|
break;
|
|
234
240
|
}
|
|
235
241
|
|
|
236
242
|
case SDL_JOYAXISMOTION: {
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
243
|
+
packed.Set("family", events::families::JOYSTICK);
|
|
244
|
+
packed.Set("type", events::types::AXIS_MOTION);
|
|
245
|
+
packed.Set("joystickId", Napi::Number::New(env, event.jaxis.which));
|
|
246
|
+
packed.Set("axis", Napi::Number::New(env, event.jaxis.axis));
|
|
247
|
+
packed.Set("value", Napi::Number::New(env, joystick::mapAxis(event.jaxis.value)));
|
|
242
248
|
break ;
|
|
243
249
|
}
|
|
244
250
|
case SDL_JOYBALLMOTION: {
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
+
packed.Set("family", events::families::JOYSTICK);
|
|
252
|
+
packed.Set("type", events::types::BALL_MOTION);
|
|
253
|
+
packed.Set("joystickId", Napi::Number::New(env, event.jball.which));
|
|
254
|
+
packed.Set("ball", Napi::Number::New(env, event.jball.ball));
|
|
255
|
+
packed.Set("x", Napi::Number::New(env, event.jball.xrel));
|
|
256
|
+
packed.Set("y", Napi::Number::New(env, event.jball.yrel));
|
|
251
257
|
break ;
|
|
252
258
|
}
|
|
253
259
|
case SDL_JOYBUTTONDOWN:
|
|
254
260
|
case SDL_JOYBUTTONUP: {
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
261
|
+
packed.Set("family", events::families::JOYSTICK);
|
|
262
|
+
packed.Set("type", event.type == SDL_JOYBUTTONDOWN ? events::types::BUTTON_DOWN : events::types::BUTTON_UP);
|
|
263
|
+
packed.Set("joystickId", Napi::Number::New(env, event.jbutton.which));
|
|
264
|
+
packed.Set("button", Napi::Number::New(env, event.jbutton.button));
|
|
259
265
|
break ;
|
|
260
266
|
}
|
|
261
267
|
case SDL_JOYHATMOTION: {
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
268
|
+
packed.Set("family", events::families::JOYSTICK);
|
|
269
|
+
packed.Set("type", events::types::HAT_MOTION);
|
|
270
|
+
packed.Set("joystickId", Napi::Number::New(env, event.jhat.which));
|
|
271
|
+
packed.Set("hat", Napi::Number::New(env, event.jhat.hat));
|
|
272
|
+
packed.Set("value", joystick::hat_positions[event.jhat.value]);
|
|
267
273
|
break ;
|
|
268
274
|
}
|
|
269
275
|
|
|
270
276
|
case SDL_CONTROLLERDEVICEREMAPPED: {
|
|
271
|
-
|
|
272
|
-
|
|
277
|
+
packed.Set("family", events::families::CONTROLLER);
|
|
278
|
+
packed.Set("type", events::types::REMAP);
|
|
273
279
|
SDL_JoystickID controller_id = event.cdevice.which;
|
|
274
|
-
|
|
280
|
+
packed.Set("controllerId", Napi::Number::New(env, controller_id));
|
|
275
281
|
|
|
276
282
|
SDL_GameController *controller = SDL_GameControllerFromInstanceID(controller_id);
|
|
277
283
|
if (controller == nullptr) {
|
|
@@ -281,55 +287,55 @@ packageEvent (Napi::Env &env, const SDL_Event &event)
|
|
|
281
287
|
throw Napi::Error::New(env, message.str());
|
|
282
288
|
}
|
|
283
289
|
|
|
284
|
-
controller::getState(env, controller,
|
|
290
|
+
controller::getState(env, controller, packed);
|
|
285
291
|
break;
|
|
286
292
|
}
|
|
287
293
|
|
|
288
294
|
case SDL_CONTROLLERAXISMOTION: {
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
295
|
+
packed.Set("family", events::families::CONTROLLER);
|
|
296
|
+
packed.Set("type", events::types::AXIS_MOTION);
|
|
297
|
+
packed.Set("controllerId", Napi::Number::New(env, event.caxis.which));
|
|
298
|
+
packed.Set("axis", controller::axes[(SDL_GameControllerAxis) event.caxis.axis]);
|
|
299
|
+
packed.Set("value", Napi::Number::New(env, joystick::mapAxis(event.caxis.value)));
|
|
294
300
|
break;
|
|
295
301
|
}
|
|
296
302
|
case SDL_CONTROLLERBUTTONDOWN:
|
|
297
303
|
case SDL_CONTROLLERBUTTONUP: {
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
304
|
+
packed.Set("family", events::families::CONTROLLER);
|
|
305
|
+
packed.Set("type", event.type == SDL_CONTROLLERBUTTONDOWN ? events::types::BUTTON_DOWN : events::types::BUTTON_UP);
|
|
306
|
+
packed.Set("controllerId", Napi::Number::New(env, event.cbutton.which));
|
|
307
|
+
packed.Set("button", controller::buttons[(SDL_GameControllerButton) event.cbutton.button]);
|
|
302
308
|
break;
|
|
303
309
|
}
|
|
304
310
|
|
|
305
311
|
case SDL_SENSORUPDATE: {
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
312
|
+
packed.Set("family", events::families::SENSOR);
|
|
313
|
+
packed.Set("type", events::types::UPDATE);
|
|
314
|
+
packed.Set("sensorId", Napi::Number::New(env, event.sensor.which));
|
|
309
315
|
break;
|
|
310
316
|
}
|
|
311
317
|
|
|
312
318
|
case SDL_AUDIODEVICEADDED: {
|
|
313
|
-
|
|
314
|
-
|
|
319
|
+
packed.Set("family", events::families::AUDIO_DEVICE);
|
|
320
|
+
packed.Set("type", events::types::DEVICE_ADD);
|
|
315
321
|
bool is_capture = event.adevice.iscapture;
|
|
316
|
-
|
|
317
|
-
|
|
322
|
+
packed.Set("isRecorder", Napi::Boolean::New(env, is_capture));
|
|
323
|
+
packed.Set("devices", audio::_getDevices(env, is_capture));
|
|
318
324
|
break;
|
|
319
325
|
}
|
|
320
326
|
case SDL_AUDIODEVICEREMOVED: {
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
327
|
+
packed.Set("family", events::families::AUDIO_DEVICE);
|
|
328
|
+
packed.Set("type", events::types::DEVICE_REMOVE);
|
|
329
|
+
packed.Set("audioId", Napi::Number::New(env, event.adevice.which));
|
|
324
330
|
bool is_capture = event.adevice.iscapture;
|
|
325
|
-
|
|
326
|
-
|
|
331
|
+
packed.Set("isRecorder", Napi::Boolean::New(env, is_capture));
|
|
332
|
+
packed.Set("devices", audio::_getDevices(env, is_capture));
|
|
327
333
|
break;
|
|
328
334
|
}
|
|
329
335
|
|
|
330
336
|
case SDL_CLIPBOARDUPDATE: {
|
|
331
|
-
|
|
332
|
-
|
|
337
|
+
packed.Set("family", events::families::CLIPBOARD);
|
|
338
|
+
packed.Set("type", events::types::UPDATE);
|
|
333
339
|
break;
|
|
334
340
|
}
|
|
335
341
|
|
|
@@ -338,36 +344,26 @@ packageEvent (Napi::Env &env, const SDL_Event &event)
|
|
|
338
344
|
// SET_STRING(object, "type", event.type == SDL_FINGERUP
|
|
339
345
|
// ? "fingerUp"
|
|
340
346
|
// : "fingerDown");
|
|
341
|
-
//
|
|
342
|
-
//
|
|
343
|
-
//
|
|
344
|
-
//
|
|
345
|
-
//
|
|
347
|
+
// packed.Set("touch_id", Napi::Number::New(env, event.tfinger.touchId));
|
|
348
|
+
// packed.Set("finger_id", Napi::Number::New(env, event.tfinger.fingerId));
|
|
349
|
+
// packed.Set("pressure", Napi::Number::New(env, event.tfinger.pressure));
|
|
350
|
+
// packed.Set("x", Napi::Number::New(env, event.tfinger.x));
|
|
351
|
+
// packed.Set("y", Napi::Number::New(env, event.tfinger.y));
|
|
346
352
|
// break;
|
|
347
353
|
// }
|
|
348
354
|
// case SDL_FINGERMOTION: {
|
|
349
355
|
// SET_STRING(object, "type", "fingerMotion");
|
|
350
|
-
//
|
|
351
|
-
//
|
|
352
|
-
//
|
|
353
|
-
//
|
|
354
|
-
//
|
|
355
|
-
//
|
|
356
|
-
//
|
|
356
|
+
// packed.Set("touch_id", Napi::Number::New(env, event.tfinger.touchId));
|
|
357
|
+
// packed.Set("finger_id", Napi::Number::New(env, event.tfinger.fingerId));
|
|
358
|
+
// packed.Set("pressure", Napi::Number::New(env, event.tfinger.pressure));
|
|
359
|
+
// packed.Set("x", Napi::Number::New(env, event.tfinger.x));
|
|
360
|
+
// packed.Set("y", Napi::Number::New(env, event.tfinger.y));
|
|
361
|
+
// packed.Set("dx", Napi::Number::New(env, event.tfinger.dx));
|
|
362
|
+
// packed.Set("dy", Napi::Number::New(env, event.tfinger.dy));
|
|
357
363
|
// break;
|
|
358
364
|
// }
|
|
359
365
|
}
|
|
360
366
|
|
|
361
|
-
return result;
|
|
362
|
-
}
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
void
|
|
366
|
-
events::dispatchEvent(const SDL_Event &event)
|
|
367
|
-
{
|
|
368
|
-
if (poll_env == nullptr || poll_callback == nullptr) { return; }
|
|
369
|
-
|
|
370
|
-
Napi::Value packed = packageEvent(*poll_env, event);
|
|
371
367
|
poll_callback->Call(poll_env->Global(), { packed });
|
|
372
368
|
}
|
|
373
369
|
|
package/src/native/mouse.cpp
CHANGED
|
@@ -128,16 +128,17 @@ mouse::setCursorImage (const Napi::CallbackInfo &info)
|
|
|
128
128
|
|
|
129
129
|
allocated_cursor = SDL_CreateColorCursor(surface, x, y);
|
|
130
130
|
if (allocated_cursor == nullptr) {
|
|
131
|
+
SDL_FreeSurface(surface);
|
|
132
|
+
|
|
131
133
|
std::ostringstream message;
|
|
132
134
|
message << "SDL_CreateColorCursor(" << x << ", " << y << ") error: " << SDL_GetError();
|
|
133
135
|
SDL_ClearError();
|
|
134
136
|
throw Napi::Error::New(env, message.str());
|
|
135
137
|
}
|
|
136
138
|
|
|
137
|
-
SDL_FreeSurface(surface);
|
|
138
|
-
|
|
139
139
|
SDL_SetCursor(allocated_cursor);
|
|
140
140
|
|
|
141
|
+
SDL_FreeSurface(surface);
|
|
141
142
|
return env.Undefined();
|
|
142
143
|
}
|
|
143
144
|
|
package/src/native/window.cpp
CHANGED
|
@@ -563,15 +563,18 @@ window::render (const Napi::CallbackInfo &info)
|
|
|
563
563
|
|
|
564
564
|
SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, surface);
|
|
565
565
|
if (texture == nullptr) {
|
|
566
|
+
SDL_FreeSurface(surface);
|
|
567
|
+
|
|
566
568
|
std::ostringstream message;
|
|
567
569
|
message << "SDL_CreateTextureFromSurface(" << window_id << ") error: " << SDL_GetError();
|
|
568
570
|
SDL_ClearError();
|
|
569
571
|
throw Napi::Error::New(env, message.str());
|
|
570
572
|
}
|
|
571
573
|
|
|
572
|
-
SDL_FreeSurface(surface);
|
|
573
|
-
|
|
574
574
|
if (SDL_RenderClear(renderer) < 0) {
|
|
575
|
+
SDL_DestroyTexture(texture);
|
|
576
|
+
SDL_FreeSurface(surface);
|
|
577
|
+
|
|
575
578
|
std::ostringstream message;
|
|
576
579
|
message << "SDL_RenderClear(" << window_id << ") error: " << SDL_GetError();
|
|
577
580
|
SDL_ClearError();
|
|
@@ -579,6 +582,9 @@ window::render (const Napi::CallbackInfo &info)
|
|
|
579
582
|
}
|
|
580
583
|
|
|
581
584
|
if (SDL_RenderCopy(renderer, texture, nullptr, nullptr) < 0) {
|
|
585
|
+
SDL_DestroyTexture(texture);
|
|
586
|
+
SDL_FreeSurface(surface);
|
|
587
|
+
|
|
582
588
|
std::ostringstream message;
|
|
583
589
|
message << "SDL_RenderCopy(" << window_id << ") error: " << SDL_GetError();
|
|
584
590
|
SDL_ClearError();
|
|
@@ -587,6 +593,8 @@ window::render (const Napi::CallbackInfo &info)
|
|
|
587
593
|
|
|
588
594
|
SDL_RenderPresent(renderer);
|
|
589
595
|
|
|
596
|
+
SDL_DestroyTexture(texture);
|
|
597
|
+
SDL_FreeSurface(surface);
|
|
590
598
|
return env.Undefined();
|
|
591
599
|
}
|
|
592
600
|
|