-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.js
106 lines (104 loc) · 3.4 KB
/
api.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
const ffi = require('ffi-napi');
const ref = require('ref-napi');
const Struct = require('ref-struct-di')(ref);
const Union = require('ref-union-di')(ref);
const ArrayType = require('ref-array-di')(ref);
var e = (typeof process.env.sdl2_global_export == 'undefined') ? exports : global;
var library_exports = [];
var library_functions = [];
var enerator;
exports.defines = {
SDL_ASSERT_LEVEL: 0,
SDL_DYNAMIC_API: 1,
HAVE_STDIO_H: 1
};
if ( /*typeof process.env.sdl2_defines == 'undefined'*/ true) {
if (process.platform == 'win32')
exports.defines['__WIN32__'] = exports.defines['SDL_VIDEO_DRIVER_WINDOWS'] = 1;
if (process.platform == 'android')
exports.defines['__ANDROID__'] = exports.defines['SDL_VIDEO_DRIVER_ANDROID'] = 1;
}
if (typeof process.env.sdl2_defines !== 'undefined') {
const str_defines = process.env.sdl2_defines.split(';');
for (var i = 0; i < str_defines.length; i++) {
const str_define = str_defines[i];
if (str_define.includes('=')) {
exports.defines[str_define.split('=')[0].trim()] = eval(str_define.split('=')[1].trim());
continue;
}
exports.defines[str_define.trim()] = true;
}
}
exports.e = e;
exports.l = {};
exports.library = {};
exports.image_library = {};
exports.ttf_library = {};
exports.mixer_library = {};
exports.net_library = {};
exports.gfx_library = {};
exports.ffi = ffi;
exports.ref = ref;
exports.Func = function(...data) {
// data[0] += '*';
return ffi.Function(...data);
}
exports.SFunc = ffi.Function;
exports.Struct = Struct;
exports.Union = Union;
exports.ArrayType = ArrayType;
exports.library_exports = library_exports;
exports.library_functions = library_functions;
exports.ignore_list = [];
exports.clear_ignore = function(dict, ignore_list) {
for (var i = 0; i < ignore_list.length; i++) {
delete dict[ignore_list[i]];
}
return dict;
}
exports.join_exports = function() {
return exports.clear_ignore(Object.assign({}, ...exports.library_exports), exports.ignore_list);
}
exports.join_functions = function() {
return Object.assign({}, ...exports.library_functions);
}
exports.from_hex = function(hex_str) {
return parseInt(hex_str, 16);
}
exports.en = function(reset) {
if (typeof reset !== 'undefined')
enerator = reset;
return enerator++;
}
exports.encoder = new TextEncoder();
exports.decoder = new TextDecoder();
exports.default_terminator = '\x00';
exports.from_utf16 = function(str, terminator = exports.default_terminator) {
str += terminator;
var arr = new Uint16Array(str.length);
for (var i = 0; i < str.length; i++) {
arr[i] = str.charCodeAt(i);
}
return arr;
}
exports.to_utf16 = function(arr, terminator = exports.default_terminator) {
return exports.trim_terminator(String.fromCharCode(...arr), terminator);
}
exports.from_utf8 = function(str, terminator = exports.default_terminator) {
return encoder.encode(str + terminator);
}
exports.to_utf8 = function(arr, terminator = exports.default_terminator) {
return exports.trim_terminator(decoder.decode(arr), terminator);
}
exports.trim_terminator = function(str, terminator = exports.default_terminator) {
const terminator_index = str.indexOf(terminator);
if (!terminator || terminator_index < 0)
return str;
return str.substr(0, terminator_index);
}
exports.push_export = function(sublibrary_export) {
library_exports.push(sublibrary_export);
}
exports.push_functions = function(sublibrary_functions) {
library_functions.push(sublibrary_functions);
}