slifer 0.1.4 → 0.1.6
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/Jacquard_12/Jacquard12-Regular.ttf +0 -0
- package/Jacquard_12/OFL.txt +93 -0
- package/LICENSE +21 -21
- package/README.md +66 -66
- 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 +14 -17
- package/src/ffi.ts +242 -230
- package/src/global.ts +10 -10
- package/src/modules/graphics.ts +157 -157
- package/src/modules/keyboard.ts +79 -79
- package/src/modules/mouse.ts +101 -101
- package/src/slifer.ts +179 -132
- package/tsconfig.json +28 -28
package/src/slifer.ts
CHANGED
@@ -1,132 +1,179 @@
|
|
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
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
constructor() {
|
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
|
-
const
|
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
|
-
|
116
|
-
return
|
117
|
-
}
|
118
|
-
|
119
|
-
/**
|
120
|
-
*
|
121
|
-
*/
|
122
|
-
|
123
|
-
|
124
|
-
libsdl.symbols.
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
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
|
+
//@ts-expect-error
|
10
|
+
const fontFile = await import("../Jacquard_12/Jacquard12-Regular.ttf");
|
11
|
+
|
12
|
+
/** @internal */
|
13
|
+
class Window {
|
14
|
+
public width: number;
|
15
|
+
public height: number;
|
16
|
+
public title: string;
|
17
|
+
|
18
|
+
constructor(title: string, width: number, height: number) {
|
19
|
+
this.width = width;
|
20
|
+
this.height = height;
|
21
|
+
this.title = title;
|
22
|
+
}
|
23
|
+
|
24
|
+
setSize(width: number, height: number) : void {
|
25
|
+
libsdl.symbols.SDL_SetWindowSize(Global.ptrWindow, width, height);
|
26
|
+
}
|
27
|
+
|
28
|
+
setTitle(title: string) : void {
|
29
|
+
libsdl.symbols.SDL_SetWindowTitle(Global.ptrWindow, Buffer.from(title+'\x00'));
|
30
|
+
}
|
31
|
+
|
32
|
+
setFullscreen(flag: boolean) : void {
|
33
|
+
libsdl.symbols.SDL_SetWindowFullscreen(Global.ptrWindow, Number(flag));
|
34
|
+
}
|
35
|
+
|
36
|
+
centerWindow() : void {
|
37
|
+
libsdl.symbols.SDL_SetWindowPosition(Global.ptrWindow, 0x2FFF0000, 0x2FFF0000);
|
38
|
+
}
|
39
|
+
|
40
|
+
setPosition(x: number, y: number) : void {
|
41
|
+
libsdl.symbols.SDL_SetWindowPosition(Global.ptrWindow, x, y);
|
42
|
+
}
|
43
|
+
}
|
44
|
+
|
45
|
+
/** @interal */
|
46
|
+
export class SliferClass {
|
47
|
+
isRunning: boolean = true;
|
48
|
+
|
49
|
+
// Modules
|
50
|
+
Graphics = new Graphics();
|
51
|
+
Keyboard = new Keyboard();
|
52
|
+
Mouse = new Mouse();
|
53
|
+
|
54
|
+
constructor() {
|
55
|
+
const baseInit = libsdl.symbols.SDL_Init(0x00000020);
|
56
|
+
if (baseInit != 0) throw `SDL failed to initialize`;
|
57
|
+
|
58
|
+
const imageInit = libimage.symbols.IMG_Init(3);
|
59
|
+
if (imageInit != 3) throw `SDL Image failed to initialize`;
|
60
|
+
|
61
|
+
const ttfInit = libttf.symbols.TTF_Init();
|
62
|
+
if (ttfInit != 0) throw `SDL TTF failed to initialize`;
|
63
|
+
|
64
|
+
/*
|
65
|
+
if (process.platform == "darwin") {
|
66
|
+
const tempFont = libttf.symbols.TTF_OpenFont(
|
67
|
+
Buffer.from("/System/Library/Fonts/SFNSMono.ttf"),
|
68
|
+
12,
|
69
|
+
);
|
70
|
+
if (tempFont == null) throw `Default font loading failed`;
|
71
|
+
Global.ptrFont = tempFont;
|
72
|
+
} else if (process.platform == "linux") {
|
73
|
+
const tempFont = libttf.symbols.TTF_OpenFont(
|
74
|
+
Buffer.from("/usr/share/fonts/truetype/dejavu/DejaVuSansMono.ttf"),
|
75
|
+
12,
|
76
|
+
);
|
77
|
+
if (tempFont == null) throw `Default font loading failed`;
|
78
|
+
Global.ptrFont = tempFont;
|
79
|
+
}
|
80
|
+
*/
|
81
|
+
|
82
|
+
|
83
|
+
|
84
|
+
const tempFont = libttf.symbols.TTF_OpenFont(
|
85
|
+
Buffer.from(fontFile.default), 24);
|
86
|
+
if (tempFont == null) throw `Default font loading failed`;
|
87
|
+
Global.ptrFont = tempFont;
|
88
|
+
}
|
89
|
+
|
90
|
+
/**
|
91
|
+
* @param title Title of window
|
92
|
+
* @param width Width of window
|
93
|
+
* @param height Height of window
|
94
|
+
*/
|
95
|
+
createWindow(title: string, width: number, height: number): Window {
|
96
|
+
// Creating cstring buffer from string
|
97
|
+
const _title = Buffer.from(title + "\x00");
|
98
|
+
|
99
|
+
// Creating window pointer
|
100
|
+
const _win = libsdl.symbols.SDL_CreateWindow(
|
101
|
+
_title,
|
102
|
+
0x2FFF0000,
|
103
|
+
0x2FFF0000,
|
104
|
+
width,
|
105
|
+
height,
|
106
|
+
0,
|
107
|
+
);
|
108
|
+
if (_win == null) throw `Window creation failed`;
|
109
|
+
Global.ptrWindow = _win;
|
110
|
+
|
111
|
+
// Creating renderer pointer
|
112
|
+
const _ren = libsdl.symbols.SDL_CreateRenderer(Global.ptrWindow, -1, 0);
|
113
|
+
if (_ren == null) throw `Renderer Creation failed`;
|
114
|
+
Global.ptrRenderer = _ren;
|
115
|
+
|
116
|
+
return new Window(title, width, height);
|
117
|
+
}
|
118
|
+
|
119
|
+
/**
|
120
|
+
* @returns if the window should close
|
121
|
+
*/
|
122
|
+
shouldClose(): boolean {
|
123
|
+
// Clear the renderer
|
124
|
+
libsdl.symbols.SDL_RenderClear(Global.ptrRenderer);
|
125
|
+
|
126
|
+
// Poll Events
|
127
|
+
const eventArray = new Uint16Array(32);
|
128
|
+
const isEvent = libsdl.symbols.SDL_PollEvent(ptr(eventArray));
|
129
|
+
|
130
|
+
if (isEvent) {
|
131
|
+
switch (eventArray[0]) {
|
132
|
+
// Quit event
|
133
|
+
case 256:
|
134
|
+
this.isRunning = false;
|
135
|
+
break;
|
136
|
+
// Keydown event
|
137
|
+
case 768:
|
138
|
+
const _dscancode = eventArray[8];
|
139
|
+
const _dkey = libsdl.symbols.SDL_GetKeyFromScancode(_dscancode);
|
140
|
+
const _dname = libsdl.symbols.SDL_GetKeyName(_dkey);
|
141
|
+
Keyboard.setKeyDown(_dname.toString().toLowerCase());
|
142
|
+
break;
|
143
|
+
// Keyup event
|
144
|
+
case 769:
|
145
|
+
const _uscancode = eventArray[8];
|
146
|
+
const _ukey = libsdl.symbols.SDL_GetKeyFromScancode(_uscancode);
|
147
|
+
const _uname = libsdl.symbols.SDL_GetKeyName(_ukey);
|
148
|
+
Keyboard.setKeyUp(_uname.toString().toLowerCase());
|
149
|
+
break;
|
150
|
+
// Mouse down event
|
151
|
+
case 1025:
|
152
|
+
const _dbtn = eventArray[8] - 256;
|
153
|
+
Mouse.setButtonDown(_dbtn);
|
154
|
+
break;
|
155
|
+
// Mouse up event
|
156
|
+
case 1026:
|
157
|
+
const _ubtn = eventArray[8];
|
158
|
+
Mouse.setButtonUp(_ubtn);
|
159
|
+
break;
|
160
|
+
}
|
161
|
+
}
|
162
|
+
|
163
|
+
return !this.isRunning;
|
164
|
+
}
|
165
|
+
|
166
|
+
/**
|
167
|
+
* Slifers quit method
|
168
|
+
*/
|
169
|
+
quit() {
|
170
|
+
libttf.symbols.TTF_CloseFont(Global.ptrFont);
|
171
|
+
libsdl.symbols.SDL_DestroyRenderer(Global.ptrRenderer);
|
172
|
+
libsdl.symbols.SDL_DestroyWindow(Global.ptrWindow);
|
173
|
+
libsdl.symbols.SDL_Quit();
|
174
|
+
}
|
175
|
+
|
176
|
+
getVersion() {
|
177
|
+
return version;
|
178
|
+
}
|
179
|
+
}
|
package/tsconfig.json
CHANGED
@@ -1,28 +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
|
-
"stripInternal": true,
|
22
|
-
|
23
|
-
// Some stricter flags (disabled by default)
|
24
|
-
"noUnusedLocals": false,
|
25
|
-
"noUnusedParameters": false,
|
26
|
-
"noPropertyAccessFromIndexSignature": false
|
27
|
-
}
|
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
|
+
"stripInternal": true,
|
22
|
+
|
23
|
+
// Some stricter flags (disabled by default)
|
24
|
+
"noUnusedLocals": false,
|
25
|
+
"noUnusedParameters": false,
|
26
|
+
"noPropertyAccessFromIndexSignature": false
|
27
|
+
}
|
28
|
+
}
|