slifer 0.1.7 → 0.1.9
Sign up to get free protection for your applications and to get access to all the features.
- package/LICENSE +21 -21
- package/README.md +66 -66
- 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 -14
- package/src/Jost-Bold.ttf +0 -0
- package/src/ffi.ts +242 -242
- package/src/global.ts +10 -10
- package/src/modules/graphics.ts +175 -172
- package/src/modules/keyboard.ts +79 -79
- package/src/modules/mouse.ts +101 -101
- package/src/slifer.ts +225 -211
- package/tsconfig.json +28 -28
- package/Jacquard_12/Jacquard12-Regular.ttf +0 -0
- package/Jacquard_12/OFL.txt +0 -93
- package/bun.lockb +0 -0
package/src/modules/mouse.ts
CHANGED
@@ -1,102 +1,102 @@
|
|
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 */
|
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 */
|
102
102
|
export default Mouse;
|
package/src/slifer.ts
CHANGED
@@ -1,211 +1,225 @@
|
|
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
|
8
|
-
|
9
|
-
//@ts-expect-error
|
10
|
-
const fontFile = await import("
|
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)
|
25
|
-
libsdl.symbols.SDL_SetWindowSize(Global.ptrWindow, width, height);
|
26
|
-
}
|
27
|
-
|
28
|
-
setTitle(title: string)
|
29
|
-
libsdl.symbols.SDL_SetWindowTitle(
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
}
|
35
|
-
|
36
|
-
|
37
|
-
libsdl.symbols.
|
38
|
-
}
|
39
|
-
|
40
|
-
|
41
|
-
libsdl.symbols.SDL_SetWindowPosition(
|
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
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
//
|
127
|
-
const
|
128
|
-
const
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
}
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
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("./Jost-Bold.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(
|
30
|
+
Global.ptrWindow,
|
31
|
+
//@ts-expect-error Buffer and CString differences
|
32
|
+
Buffer.from(title + "\x00")
|
33
|
+
);
|
34
|
+
}
|
35
|
+
|
36
|
+
setFullscreen(flag: boolean): void {
|
37
|
+
libsdl.symbols.SDL_SetWindowFullscreen(Global.ptrWindow, Number(flag));
|
38
|
+
}
|
39
|
+
|
40
|
+
centerWindow(): void {
|
41
|
+
libsdl.symbols.SDL_SetWindowPosition(
|
42
|
+
Global.ptrWindow,
|
43
|
+
0x2fff0000,
|
44
|
+
0x2fff0000
|
45
|
+
);
|
46
|
+
}
|
47
|
+
|
48
|
+
setPosition(x: number, y: number): void {
|
49
|
+
libsdl.symbols.SDL_SetWindowPosition(Global.ptrWindow, x, y);
|
50
|
+
}
|
51
|
+
}
|
52
|
+
|
53
|
+
/** @interal */
|
54
|
+
export class SliferClass {
|
55
|
+
private isRunning: boolean = true;
|
56
|
+
private lastFrame: number = 0;
|
57
|
+
private firstFrame: number = 0;
|
58
|
+
|
59
|
+
// Modules
|
60
|
+
Graphics = new Graphics();
|
61
|
+
Keyboard = new Keyboard();
|
62
|
+
Mouse = new Mouse();
|
63
|
+
|
64
|
+
// Public Variables
|
65
|
+
public dt: number = 0;
|
66
|
+
|
67
|
+
constructor() {
|
68
|
+
const baseInit = libsdl.symbols.SDL_Init(0x00000020);
|
69
|
+
if (baseInit != 0) throw `SDL failed to initialize`;
|
70
|
+
|
71
|
+
const imageInit = libimage.symbols.IMG_Init(3);
|
72
|
+
if (imageInit != 3) throw `SDL Image failed to initialize`;
|
73
|
+
|
74
|
+
const ttfInit = libttf.symbols.TTF_Init();
|
75
|
+
if (ttfInit != 0) throw `SDL TTF failed to initialize`;
|
76
|
+
|
77
|
+
/*
|
78
|
+
if (process.platform == "darwin") {
|
79
|
+
const tempFont = libttf.symbols.TTF_OpenFont(
|
80
|
+
Buffer.from("/System/Library/Fonts/SFNSMono.ttf"),
|
81
|
+
12,
|
82
|
+
);
|
83
|
+
if (tempFont == null) throw `Default font loading failed`;
|
84
|
+
Global.ptrFont = tempFont;
|
85
|
+
} else if (process.platform == "linux") {
|
86
|
+
const tempFont = libttf.symbols.TTF_OpenFont(
|
87
|
+
Buffer.from("/usr/share/fonts/truetype/dejavu/DejaVuSansMono.ttf"),
|
88
|
+
12,
|
89
|
+
);
|
90
|
+
if (tempFont == null) throw `Default font loading failed`;
|
91
|
+
Global.ptrFont = tempFont;
|
92
|
+
}
|
93
|
+
*/
|
94
|
+
|
95
|
+
const tempFont = libttf.symbols.TTF_OpenFont(
|
96
|
+
//@ts-expect-error Buffer and CString differences
|
97
|
+
Buffer.from(fontFile.default),
|
98
|
+
24
|
99
|
+
);
|
100
|
+
if (tempFont == null) throw `Default font loading failed`;
|
101
|
+
Global.ptrFont = tempFont;
|
102
|
+
}
|
103
|
+
|
104
|
+
/**
|
105
|
+
* @param title Title of window
|
106
|
+
* @param width Width of window
|
107
|
+
* @param height Height of window
|
108
|
+
*/
|
109
|
+
createWindow(title: string, width: number, height: number): Window {
|
110
|
+
// Creating cstring buffer from string
|
111
|
+
const _title = Buffer.from(title + "\x00");
|
112
|
+
|
113
|
+
// Creating window pointer
|
114
|
+
const _win = libsdl.symbols.SDL_CreateWindow(
|
115
|
+
//@ts-expect-error Buffer and CString differences
|
116
|
+
_title,
|
117
|
+
0x2fff0000,
|
118
|
+
0x2fff0000,
|
119
|
+
width,
|
120
|
+
height,
|
121
|
+
0
|
122
|
+
);
|
123
|
+
if (_win == null) throw `Window creation failed`;
|
124
|
+
Global.ptrWindow = _win;
|
125
|
+
|
126
|
+
// Creating renderer pointer
|
127
|
+
const vsyncHint = 0x00000004;
|
128
|
+
const _ren = libsdl.symbols.SDL_CreateRenderer(Global.ptrWindow, -1, vsyncHint);
|
129
|
+
if (_ren == null) throw `Renderer Creation failed`;
|
130
|
+
Global.ptrRenderer = _ren;
|
131
|
+
|
132
|
+
this.firstFrame = Number(libsdl.symbols.SDL_GetPerformanceCounter());
|
133
|
+
|
134
|
+
return new Window(title, width, height);
|
135
|
+
}
|
136
|
+
|
137
|
+
/**
|
138
|
+
* @returns if the window should close
|
139
|
+
*/
|
140
|
+
shouldClose(): boolean {
|
141
|
+
// Clear the renderer
|
142
|
+
libsdl.symbols.SDL_RenderClear(Global.ptrRenderer);
|
143
|
+
|
144
|
+
// Setup deltatime
|
145
|
+
this.lastFrame = this.firstFrame;
|
146
|
+
this.firstFrame = Number(libsdl.symbols.SDL_GetPerformanceCounter());
|
147
|
+
|
148
|
+
this.dt = ((this.firstFrame - this.lastFrame) * 1000 / Number(libsdl.symbols.SDL_GetPerformanceFrequency()));
|
149
|
+
|
150
|
+
|
151
|
+
// Poll Events
|
152
|
+
const eventArray = new Uint16Array(32);
|
153
|
+
const isEvent = libsdl.symbols.SDL_PollEvent(ptr(eventArray));
|
154
|
+
|
155
|
+
if (isEvent) {
|
156
|
+
switch (eventArray[0]) {
|
157
|
+
// Quit event
|
158
|
+
case 256:
|
159
|
+
this.isRunning = false;
|
160
|
+
break;
|
161
|
+
// Keydown event
|
162
|
+
case 768:
|
163
|
+
const _dscancode = eventArray[8];
|
164
|
+
const _dkey = libsdl.symbols.SDL_GetKeyFromScancode(_dscancode);
|
165
|
+
const _dname = libsdl.symbols.SDL_GetKeyName(_dkey);
|
166
|
+
Keyboard.setKeyDown(_dname.toString().toLowerCase());
|
167
|
+
break;
|
168
|
+
// Keyup event
|
169
|
+
case 769:
|
170
|
+
const _uscancode = eventArray[8];
|
171
|
+
const _ukey = libsdl.symbols.SDL_GetKeyFromScancode(_uscancode);
|
172
|
+
const _uname = libsdl.symbols.SDL_GetKeyName(_ukey);
|
173
|
+
Keyboard.setKeyUp(_uname.toString().toLowerCase());
|
174
|
+
break;
|
175
|
+
// Mouse down event
|
176
|
+
case 1025:
|
177
|
+
const _dbtn = eventArray[8] - 256;
|
178
|
+
Mouse.setButtonDown(_dbtn);
|
179
|
+
break;
|
180
|
+
// Mouse up event
|
181
|
+
case 1026:
|
182
|
+
const _ubtn = eventArray[8];
|
183
|
+
Mouse.setButtonUp(_ubtn);
|
184
|
+
break;
|
185
|
+
}
|
186
|
+
}
|
187
|
+
|
188
|
+
return !this.isRunning;
|
189
|
+
}
|
190
|
+
|
191
|
+
/**
|
192
|
+
* Slifers quit method
|
193
|
+
*/
|
194
|
+
quit() {
|
195
|
+
libttf.symbols.TTF_CloseFont(Global.ptrFont);
|
196
|
+
libsdl.symbols.SDL_DestroyRenderer(Global.ptrRenderer);
|
197
|
+
libsdl.symbols.SDL_DestroyWindow(Global.ptrWindow);
|
198
|
+
libsdl.symbols.SDL_Quit();
|
199
|
+
}
|
200
|
+
|
201
|
+
getVersion() {
|
202
|
+
return version;
|
203
|
+
}
|
204
|
+
}
|
205
|
+
|
206
|
+
export class Rectangle {
|
207
|
+
private readonly pointer;
|
208
|
+
readonly x;
|
209
|
+
readonly y;
|
210
|
+
readonly width;
|
211
|
+
readonly height;
|
212
|
+
|
213
|
+
constructor(x: number, y: number, width: number, height: number) {
|
214
|
+
const arr = new Uint32Array(4);
|
215
|
+
arr[0] = x;
|
216
|
+
arr[1] = y;
|
217
|
+
arr[2] = width;
|
218
|
+
arr[3] = height;
|
219
|
+
this.pointer = ptr(arr);
|
220
|
+
this.x = x;
|
221
|
+
this.y = y;
|
222
|
+
this.width = width;
|
223
|
+
this.height = height;
|
224
|
+
}
|
225
|
+
}
|