slifer 0.1.2 → 0.1.4
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/LICENSE +21 -21
- package/README.md +66 -59
- package/bun.lockb +0 -0
- package/index.ts +3 -3
- package/libs/libSDL2.so +0 -0
- package/libs/libSDL2_image.so +0 -0
- package/libs/libSDL2_ttf.so +0 -0
- package/package.json +17 -14
- package/src/ffi.ts +230 -213
- package/src/global.ts +10 -9
- package/src/modules/graphics.ts +157 -155
- package/src/modules/keyboard.ts +79 -77
- package/src/modules/mouse.ts +101 -99
- package/src/slifer.ts +132 -115
- package/tsconfig.json +28 -27
package/src/modules/mouse.ts
CHANGED
@@ -1,100 +1,102 @@
|
|
1
|
-
import { libsdl } from "../ffi";
|
2
|
-
import { ptr } from 'bun:ffi';
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
static
|
8
|
-
static
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
this.
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
this.
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
*
|
43
|
-
* @
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
*
|
55
|
-
* @
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
const
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
*
|
74
|
-
* @
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
const
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
const
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
}
|
97
|
-
|
98
|
-
|
99
|
-
|
1
|
+
import { libsdl } from "../ffi";
|
2
|
+
import { ptr } from 'bun:ffi';
|
3
|
+
|
4
|
+
/** @internal */
|
5
|
+
class Mouse {
|
6
|
+
|
7
|
+
static downKeyMap = new Map<string, boolean>();
|
8
|
+
static pressedKeyMap = new Map<string, boolean>();
|
9
|
+
static releasedKeyMap = new Map<string, boolean>();
|
10
|
+
|
11
|
+
static setButtonDown(button: number) {
|
12
|
+
|
13
|
+
let key : string = '';
|
14
|
+
if (button == 1) {
|
15
|
+
key = "left";
|
16
|
+
} else if (button == 2) {
|
17
|
+
key = "middle";
|
18
|
+
} else {
|
19
|
+
key = "right";
|
20
|
+
}
|
21
|
+
|
22
|
+
this.downKeyMap.set(key, true);
|
23
|
+
this.releasedKeyMap.set(key, false);
|
24
|
+
}
|
25
|
+
|
26
|
+
static setButtonUp(button: number) {
|
27
|
+
|
28
|
+
let key : string = '';
|
29
|
+
if (button == 1) {
|
30
|
+
key = "left";
|
31
|
+
} else if (button == 2) {
|
32
|
+
key = "middle";
|
33
|
+
} else {
|
34
|
+
key = "right";
|
35
|
+
}
|
36
|
+
|
37
|
+
this.downKeyMap.set(key, false);
|
38
|
+
this.pressedKeyMap.set(key, false);
|
39
|
+
}
|
40
|
+
|
41
|
+
/**
|
42
|
+
*
|
43
|
+
* @param button string of button
|
44
|
+
* @returns if the button is being held down
|
45
|
+
*/
|
46
|
+
isDown(button: buttons) {
|
47
|
+
const _state = Mouse.downKeyMap.get(button);
|
48
|
+
if (_state == undefined) return false
|
49
|
+
|
50
|
+
return _state;
|
51
|
+
}
|
52
|
+
|
53
|
+
/**
|
54
|
+
*
|
55
|
+
* @param button string of button
|
56
|
+
* @returns if button is pressed. Returns only once
|
57
|
+
*/
|
58
|
+
isPressed(button: buttons) {
|
59
|
+
const _pressedState = Mouse.pressedKeyMap.get(button);
|
60
|
+
const _downState = Mouse.downKeyMap.get(button);
|
61
|
+
|
62
|
+
if (_downState == true) {
|
63
|
+
if (_pressedState == false || _pressedState == undefined) {
|
64
|
+
Mouse.pressedKeyMap.set(button, true);
|
65
|
+
return true;
|
66
|
+
}
|
67
|
+
}
|
68
|
+
|
69
|
+
return false;
|
70
|
+
}
|
71
|
+
|
72
|
+
/**
|
73
|
+
*
|
74
|
+
* @param button string of button
|
75
|
+
* @returns if button is released. Returns only once
|
76
|
+
*/
|
77
|
+
isReleased(button: buttons) {
|
78
|
+
const _releasedState = Mouse.releasedKeyMap.get(button);
|
79
|
+
const _downState = Mouse.downKeyMap.get(button);
|
80
|
+
|
81
|
+
if (_downState == false) {
|
82
|
+
if (_releasedState == false || undefined) {
|
83
|
+
Mouse.releasedKeyMap.set(button, true);
|
84
|
+
return true;
|
85
|
+
}
|
86
|
+
}
|
87
|
+
|
88
|
+
return false;
|
89
|
+
}
|
90
|
+
|
91
|
+
getPosition() {
|
92
|
+
const xArr = new Uint32Array(1);
|
93
|
+
const yArr = new Uint32Array(1);
|
94
|
+
libsdl.symbols.SDL_GetMouseState(ptr(xArr), ptr(yArr));
|
95
|
+
return {x: xArr[0], y: yArr[0]};
|
96
|
+
}
|
97
|
+
}
|
98
|
+
|
99
|
+
type buttons = 'left' | 'middle' | 'right';
|
100
|
+
|
101
|
+
/** @internal */
|
100
102
|
export default Mouse;
|
package/src/slifer.ts
CHANGED
@@ -1,115 +1,132 @@
|
|
1
|
-
import { libimage, libsdl, libttf } from "./ffi";
|
2
|
-
import Global from "./global";
|
3
|
-
import { ptr } from
|
4
|
-
import Graphics from "./modules/graphics";
|
5
|
-
import Keyboard from "./modules/keyboard";
|
6
|
-
import Mouse from "./modules/mouse";
|
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
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
}
|
115
|
-
|
1
|
+
import { libimage, libsdl, libttf } from "./ffi";
|
2
|
+
import Global from "./global";
|
3
|
+
import { ptr } from "bun:ffi";
|
4
|
+
import Graphics from "./modules/graphics";
|
5
|
+
import Keyboard from "./modules/keyboard";
|
6
|
+
import Mouse from "./modules/mouse";
|
7
|
+
import { version } from '../package.json';
|
8
|
+
|
9
|
+
/** @interal */
|
10
|
+
export class SliferClass {
|
11
|
+
isRunning: boolean = true;
|
12
|
+
|
13
|
+
// Modules
|
14
|
+
Graphics = new Graphics();
|
15
|
+
Keyboard = new Keyboard();
|
16
|
+
Mouse = new Mouse();
|
17
|
+
|
18
|
+
constructor() {
|
19
|
+
const baseInit = libsdl.symbols.SDL_Init(0x00000020);
|
20
|
+
if (baseInit != 0) throw `SDL failed to initialize`;
|
21
|
+
|
22
|
+
const imageInit = libimage.symbols.IMG_Init(3);
|
23
|
+
if (imageInit != 3) throw `SDL Image failed to initialize`;
|
24
|
+
|
25
|
+
const ttfInit = libttf.symbols.TTF_Init();
|
26
|
+
if (ttfInit != 0) throw `SDL TTF failed to initialize`;
|
27
|
+
|
28
|
+
if (process.platform == "darwin") {
|
29
|
+
const tempFont = libttf.symbols.TTF_OpenFont(
|
30
|
+
Buffer.from("/System/Library/Fonts/SFNSMono.ttf"),
|
31
|
+
12,
|
32
|
+
);
|
33
|
+
if (tempFont == null) throw `Default font loading failed`;
|
34
|
+
Global.ptrFont = tempFont;
|
35
|
+
} else if (process.platform == "linux") {
|
36
|
+
const tempFont = libttf.symbols.TTF_OpenFont(
|
37
|
+
Buffer.from("/usr/share/fonts/truetype/dejavu/DejaVuSansMono.ttf"),
|
38
|
+
12,
|
39
|
+
);
|
40
|
+
if (tempFont == null) throw `Default font loading failed`;
|
41
|
+
Global.ptrFont = tempFont;
|
42
|
+
}
|
43
|
+
}
|
44
|
+
|
45
|
+
/**
|
46
|
+
* @param title Title of window
|
47
|
+
* @param width Width of window
|
48
|
+
* @param height Height of window
|
49
|
+
*/
|
50
|
+
createWindow(title: string, width: number, height: number): void {
|
51
|
+
// Creating cstring buffer from string
|
52
|
+
const _title = Buffer.from(title + "\x00");
|
53
|
+
|
54
|
+
// Creating window pointer
|
55
|
+
const _win = libsdl.symbols.SDL_CreateWindow(
|
56
|
+
_title,
|
57
|
+
0x2FFF0000,
|
58
|
+
0x2FFF0000,
|
59
|
+
width,
|
60
|
+
height,
|
61
|
+
0,
|
62
|
+
);
|
63
|
+
if (_win == null) throw `Window creation failed`;
|
64
|
+
Global.ptrWindow = _win;
|
65
|
+
|
66
|
+
// Creating renderer pointer
|
67
|
+
const _ren = libsdl.symbols.SDL_CreateRenderer(Global.ptrWindow, -1, 0);
|
68
|
+
if (_ren == null) throw `Renderer Creation failed`;
|
69
|
+
Global.ptrRenderer = _ren;
|
70
|
+
}
|
71
|
+
|
72
|
+
/**
|
73
|
+
* @returns if the window should close
|
74
|
+
*/
|
75
|
+
shouldClose(): boolean {
|
76
|
+
// Clear the renderer
|
77
|
+
libsdl.symbols.SDL_RenderClear(Global.ptrRenderer);
|
78
|
+
|
79
|
+
// Poll Events
|
80
|
+
const eventArray = new Uint16Array(32);
|
81
|
+
const isEvent = libsdl.symbols.SDL_PollEvent(ptr(eventArray));
|
82
|
+
|
83
|
+
if (isEvent) {
|
84
|
+
switch (eventArray[0]) {
|
85
|
+
// Quit event
|
86
|
+
case 256:
|
87
|
+
this.isRunning = false;
|
88
|
+
break;
|
89
|
+
// Keydown event
|
90
|
+
case 768:
|
91
|
+
const _dscancode = eventArray[8];
|
92
|
+
const _dkey = libsdl.symbols.SDL_GetKeyFromScancode(_dscancode);
|
93
|
+
const _dname = libsdl.symbols.SDL_GetKeyName(_dkey);
|
94
|
+
Keyboard.setKeyDown(_dname.toString().toLowerCase());
|
95
|
+
break;
|
96
|
+
// Keyup event
|
97
|
+
case 769:
|
98
|
+
const _uscancode = eventArray[8];
|
99
|
+
const _ukey = libsdl.symbols.SDL_GetKeyFromScancode(_uscancode);
|
100
|
+
const _uname = libsdl.symbols.SDL_GetKeyName(_ukey);
|
101
|
+
Keyboard.setKeyUp(_uname.toString().toLowerCase());
|
102
|
+
break;
|
103
|
+
// Mouse down event
|
104
|
+
case 1025:
|
105
|
+
const _dbtn = eventArray[8] - 256;
|
106
|
+
Mouse.setButtonDown(_dbtn);
|
107
|
+
break;
|
108
|
+
// Mouse up event
|
109
|
+
case 1026:
|
110
|
+
const _ubtn = eventArray[8];
|
111
|
+
Mouse.setButtonUp(_ubtn);
|
112
|
+
break;
|
113
|
+
}
|
114
|
+
}
|
115
|
+
|
116
|
+
return !this.isRunning;
|
117
|
+
}
|
118
|
+
|
119
|
+
/**
|
120
|
+
* Slifers quit method
|
121
|
+
*/
|
122
|
+
quit() {
|
123
|
+
libttf.symbols.TTF_CloseFont(Global.ptrFont);
|
124
|
+
libsdl.symbols.SDL_DestroyRenderer(Global.ptrRenderer);
|
125
|
+
libsdl.symbols.SDL_DestroyWindow(Global.ptrWindow);
|
126
|
+
libsdl.symbols.SDL_Quit();
|
127
|
+
}
|
128
|
+
|
129
|
+
getVersion() {
|
130
|
+
return version;
|
131
|
+
}
|
132
|
+
}
|
package/tsconfig.json
CHANGED
@@ -1,27 +1,28 @@
|
|
1
|
-
{
|
2
|
-
"compilerOptions": {
|
3
|
-
// Enable latest features
|
4
|
-
"lib": ["ESNext", "DOM"],
|
5
|
-
"target": "ESNext",
|
6
|
-
"module": "ESNext",
|
7
|
-
"moduleDetection": "force",
|
8
|
-
"jsx": "react-jsx",
|
9
|
-
"allowJs": true,
|
10
|
-
|
11
|
-
// Bundler mode
|
12
|
-
"moduleResolution": "bundler",
|
13
|
-
"allowImportingTsExtensions": true,
|
14
|
-
"verbatimModuleSyntax": true,
|
15
|
-
"noEmit": true,
|
16
|
-
|
17
|
-
// Best practices
|
18
|
-
"strict": true,
|
19
|
-
"skipLibCheck": true,
|
20
|
-
"noFallthroughCasesInSwitch": true,
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
"
|
25
|
-
"
|
26
|
-
|
27
|
-
}
|
1
|
+
{
|
2
|
+
"compilerOptions": {
|
3
|
+
// Enable latest features
|
4
|
+
"lib": ["ESNext", "DOM"],
|
5
|
+
"target": "ESNext",
|
6
|
+
"module": "ESNext",
|
7
|
+
"moduleDetection": "force",
|
8
|
+
"jsx": "react-jsx",
|
9
|
+
"allowJs": true,
|
10
|
+
|
11
|
+
// Bundler mode
|
12
|
+
"moduleResolution": "bundler",
|
13
|
+
"allowImportingTsExtensions": true,
|
14
|
+
"verbatimModuleSyntax": true,
|
15
|
+
"noEmit": true,
|
16
|
+
|
17
|
+
// Best practices
|
18
|
+
"strict": true,
|
19
|
+
"skipLibCheck": true,
|
20
|
+
"noFallthroughCasesInSwitch": true,
|
21
|
+
"stripInternal": true,
|
22
|
+
|
23
|
+
// Some stricter flags (disabled by default)
|
24
|
+
"noUnusedLocals": false,
|
25
|
+
"noUnusedParameters": false,
|
26
|
+
"noPropertyAccessFromIndexSignature": false
|
27
|
+
}
|
28
|
+
}
|