slifer 0.1.9 → 0.2.1
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/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 -14
- package/src/color.ts +15 -0
- package/src/engine/rectangle.ts +25 -0
- package/src/engine/vector.ts +49 -0
- package/src/engine.ts +24 -0
- package/src/ffi.ts +242 -242
- package/src/global.ts +10 -10
- package/src/modules/graphics.ts +233 -176
- package/src/modules/keyboard.ts +79 -79
- package/src/modules/mouse.ts +101 -101
- package/src/slifer.test.ts +8 -0
- package/src/slifer.ts +175 -225
- package/tsconfig.json +28 -28
package/src/modules/graphics.ts
CHANGED
@@ -1,176 +1,233 @@
|
|
1
|
-
import { libimage, libsdl, libttf } from "../ffi";
|
2
|
-
import Global from "../global";
|
3
|
-
import { type Pointer, ptr } from
|
4
|
-
import
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
*
|
19
|
-
*
|
20
|
-
* @param
|
21
|
-
* @param
|
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
|
-
const
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
const
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
}
|
137
|
-
|
138
|
-
/**
|
139
|
-
*
|
140
|
-
*
|
141
|
-
* @param
|
142
|
-
* @param
|
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
|
-
|
1
|
+
import { libimage, libsdl, libttf } from "../ffi";
|
2
|
+
import Global from "../global";
|
3
|
+
import { type Pointer, ptr } from "bun:ffi";
|
4
|
+
import Rectangle from "../engine/rectangle";
|
5
|
+
import Color from "../color";
|
6
|
+
import Vector2 from "../engine/vector";
|
7
|
+
|
8
|
+
/** @internal */
|
9
|
+
class Graphics {
|
10
|
+
/**
|
11
|
+
* Slifers draw function. Used to draw everything to the screen.
|
12
|
+
*/
|
13
|
+
render() {
|
14
|
+
libsdl.symbols.SDL_RenderPresent(Global.ptrRenderer);
|
15
|
+
}
|
16
|
+
|
17
|
+
/**
|
18
|
+
* Create a new color. All values are from 0-255
|
19
|
+
*
|
20
|
+
* @param r red value
|
21
|
+
* @param g green value
|
22
|
+
* @param b blue value
|
23
|
+
* @param a alpha value
|
24
|
+
* @returns Color object
|
25
|
+
*/
|
26
|
+
makeColor(r: number, g: number, b: number, a: number) {
|
27
|
+
const _color = new Color(r, g, b, a);
|
28
|
+
return _color;
|
29
|
+
}
|
30
|
+
|
31
|
+
/**
|
32
|
+
* Sets the background of the window to a color of choice.
|
33
|
+
*
|
34
|
+
* Make sure this is put in the top level of the while loop
|
35
|
+
* as it will clear the renderer.
|
36
|
+
*
|
37
|
+
* @param color Color object. Make using Slifer.Graphics.makeColor
|
38
|
+
*/
|
39
|
+
setBackground(color: Color) {
|
40
|
+
libsdl.symbols.SDL_SetRenderDrawColor(
|
41
|
+
Global.ptrRenderer,
|
42
|
+
color.r,
|
43
|
+
color.g,
|
44
|
+
color.b,
|
45
|
+
color.a
|
46
|
+
);
|
47
|
+
libsdl.symbols.SDL_RenderClear(Global.ptrRenderer);
|
48
|
+
}
|
49
|
+
|
50
|
+
/**
|
51
|
+
* Loads a new image
|
52
|
+
*
|
53
|
+
* @param path string path to image
|
54
|
+
* @returns Image ready to draw
|
55
|
+
*/
|
56
|
+
loadImage(path: string): Image {
|
57
|
+
const _path = Buffer.from(path + "\x00");
|
58
|
+
const surface = libimage.symbols.IMG_Load(_path);
|
59
|
+
if (surface == null) throw `Image failed to load`;
|
60
|
+
const texture = libsdl.symbols.SDL_CreateTextureFromSurface(
|
61
|
+
Global.ptrRenderer,
|
62
|
+
surface
|
63
|
+
);
|
64
|
+
if (texture == null) throw `Image failed to be created`;
|
65
|
+
return new Image(texture);
|
66
|
+
}
|
67
|
+
|
68
|
+
/**
|
69
|
+
* Method to draw to the screen simply
|
70
|
+
* @param image Image object to draw. Made using Slifer.Graphics.loadImage
|
71
|
+
* @param position position to draw the image
|
72
|
+
*
|
73
|
+
*/
|
74
|
+
draw(image: Image, position: Vector2) {
|
75
|
+
const dstRect = new Uint32Array(4);
|
76
|
+
dstRect[0] = position.x;
|
77
|
+
dstRect[1] = position.y;
|
78
|
+
dstRect[2] = image.width;
|
79
|
+
dstRect[3] = image.height;
|
80
|
+
|
81
|
+
libsdl.symbols.SDL_RenderCopy(
|
82
|
+
Global.ptrRenderer,
|
83
|
+
(image as any).pointer,
|
84
|
+
null,
|
85
|
+
dstRect
|
86
|
+
);
|
87
|
+
}
|
88
|
+
|
89
|
+
/**
|
90
|
+
* Method to draw the image to the screen with extended arguments
|
91
|
+
*
|
92
|
+
* @param image Image object to draw. Made using Slifer.Graphics.loadImage
|
93
|
+
* @param position position to draw the image
|
94
|
+
* @param y y position to draw image
|
95
|
+
* @param rotation (optional) rotation of image
|
96
|
+
* @param xs (optional) scale of x axis
|
97
|
+
* @param ys (optional) scale of y axis
|
98
|
+
* @param flip (optional) horizontal flip
|
99
|
+
*/
|
100
|
+
drawEx(
|
101
|
+
image: Image,
|
102
|
+
position: Vector2,
|
103
|
+
clipRectangle?: Rectangle | null,
|
104
|
+
rotation?: number,
|
105
|
+
xs?: number,
|
106
|
+
ys?: number,
|
107
|
+
flip?: true
|
108
|
+
) {
|
109
|
+
const _dest = new Uint32Array(4);
|
110
|
+
let srcRect: null | Uint32Array = null;
|
111
|
+
|
112
|
+
if (clipRectangle != null) {
|
113
|
+
srcRect = new Uint32Array(4);
|
114
|
+
srcRect[0] = clipRectangle.position.x;
|
115
|
+
srcRect[1] = clipRectangle.position.y;
|
116
|
+
srcRect[2] = clipRectangle.size.x;
|
117
|
+
srcRect[3] = clipRectangle.size.y;
|
118
|
+
}
|
119
|
+
|
120
|
+
_dest[0] = position.x;
|
121
|
+
_dest[1] = position.y;
|
122
|
+
_dest[2] = image.width * (xs ? xs : 1);
|
123
|
+
_dest[3] = image.height * (ys ? ys : 1);
|
124
|
+
const _center = new Uint32Array(2);
|
125
|
+
_center[0] = _dest[2] / 2;
|
126
|
+
_center[1] = _dest[3] / 2;
|
127
|
+
libsdl.symbols.SDL_RenderCopyEx(
|
128
|
+
Global.ptrRenderer,
|
129
|
+
(image as any).pointer,
|
130
|
+
srcRect,
|
131
|
+
ptr(_dest),
|
132
|
+
rotation ? rotation : 0,
|
133
|
+
ptr(_center),
|
134
|
+
flip ? Number(flip) : 0
|
135
|
+
);
|
136
|
+
}
|
137
|
+
|
138
|
+
/**
|
139
|
+
* Method to draw text to the screen
|
140
|
+
*
|
141
|
+
* @param text the string of text to print
|
142
|
+
* @param x x position
|
143
|
+
* @param y y position
|
144
|
+
* @param color color of text. Made using Slifer.Graphics.makeColor.
|
145
|
+
*/
|
146
|
+
print(text: string, x: number, y: number, color: Color) {
|
147
|
+
// Create text buffer
|
148
|
+
const textBuffer = Buffer.from(text + "\x00");
|
149
|
+
|
150
|
+
// Get width and height of text
|
151
|
+
const wArr = new Uint32Array(1);
|
152
|
+
const hArr = new Uint32Array(1);
|
153
|
+
libttf.symbols.TTF_SizeText(
|
154
|
+
Global.ptrFont,
|
155
|
+
textBuffer,
|
156
|
+
ptr(wArr),
|
157
|
+
ptr(hArr)
|
158
|
+
);
|
159
|
+
|
160
|
+
// Define color
|
161
|
+
const _col = (color.r << 0) + (color.g << 8) + (color.b << 16);
|
162
|
+
|
163
|
+
// Create texture
|
164
|
+
const surface = libttf.symbols.TTF_RenderText_Solid(
|
165
|
+
Global.ptrFont,
|
166
|
+
textBuffer,
|
167
|
+
_col
|
168
|
+
);
|
169
|
+
if (surface == null) throw `Surface creation failed on print`;
|
170
|
+
const texture = libsdl.symbols.SDL_CreateTextureFromSurface(
|
171
|
+
Global.ptrRenderer,
|
172
|
+
surface
|
173
|
+
);
|
174
|
+
if (texture == null) throw `Texture creation failed on print`;
|
175
|
+
|
176
|
+
// Create destination
|
177
|
+
const destArr = new Uint32Array(4);
|
178
|
+
destArr[0] = x;
|
179
|
+
destArr[1] = y;
|
180
|
+
destArr[2] = wArr[0];
|
181
|
+
destArr[3] = hArr[0];
|
182
|
+
|
183
|
+
// Draw text
|
184
|
+
libsdl.symbols.SDL_RenderCopy(
|
185
|
+
Global.ptrRenderer,
|
186
|
+
texture,
|
187
|
+
null,
|
188
|
+
ptr(destArr)
|
189
|
+
);
|
190
|
+
}
|
191
|
+
|
192
|
+
/**
|
193
|
+
* Sets the font to a ttf file in your project
|
194
|
+
*
|
195
|
+
* @param path relative path to font
|
196
|
+
* @param pt size of text
|
197
|
+
*/
|
198
|
+
setFont(path: string, pt: number) {
|
199
|
+
const tempFont = libttf.symbols.TTF_OpenFont(
|
200
|
+
Buffer.from(path + "\x00"),
|
201
|
+
pt
|
202
|
+
);
|
203
|
+
if (tempFont == null) throw `Font loading failed`;
|
204
|
+
Global.ptrFont = tempFont;
|
205
|
+
}
|
206
|
+
}
|
207
|
+
|
208
|
+
class Image {
|
209
|
+
private pointer;
|
210
|
+
public readonly width;
|
211
|
+
public readonly height;
|
212
|
+
|
213
|
+
constructor(texture: Pointer) {
|
214
|
+
this.pointer = texture;
|
215
|
+
|
216
|
+
const _wArr = new Uint32Array(1);
|
217
|
+
const _hArr = new Uint32Array(1);
|
218
|
+
|
219
|
+
libsdl.symbols.SDL_QueryTexture(
|
220
|
+
texture,
|
221
|
+
null,
|
222
|
+
null,
|
223
|
+
ptr(_wArr),
|
224
|
+
ptr(_hArr)
|
225
|
+
);
|
226
|
+
|
227
|
+
this.width = _wArr[0];
|
228
|
+
this.height = _hArr[0];
|
229
|
+
}
|
230
|
+
}
|
231
|
+
|
232
|
+
/** @internal */
|
233
|
+
export default Graphics;
|
package/src/modules/keyboard.ts
CHANGED
@@ -1,80 +1,80 @@
|
|
1
|
-
/** @internal */
|
2
|
-
class Keyboard {
|
3
|
-
|
4
|
-
static downKeyMap = new Map<string, boolean>();
|
5
|
-
static pressedKeyMap = new Map<string, boolean>();
|
6
|
-
static releasedKeyMap = new Map<string, boolean>();
|
7
|
-
|
8
|
-
static setKeyDown(key: string) {
|
9
|
-
this.downKeyMap.set(key, true);
|
10
|
-
this.releasedKeyMap.set(key, false);
|
11
|
-
}
|
12
|
-
|
13
|
-
static setKeyUp(key: string) {
|
14
|
-
this.downKeyMap.set(key, false);
|
15
|
-
this.pressedKeyMap.set(key, false);
|
16
|
-
}
|
17
|
-
|
18
|
-
/**
|
19
|
-
*
|
20
|
-
* @param key string of key
|
21
|
-
* @returns if the key is being held down
|
22
|
-
*/
|
23
|
-
isDown(key: keys) {
|
24
|
-
const _state = Keyboard.downKeyMap.get(key);
|
25
|
-
if (_state == undefined) return false
|
26
|
-
|
27
|
-
return _state;
|
28
|
-
}
|
29
|
-
|
30
|
-
/**
|
31
|
-
*
|
32
|
-
* @param key string of key
|
33
|
-
* @returns if key is pressed. Returns only once
|
34
|
-
*/
|
35
|
-
isPressed(key: keys) {
|
36
|
-
const _pressedState = Keyboard.pressedKeyMap.get(key);
|
37
|
-
const _downState = Keyboard.downKeyMap.get(key);
|
38
|
-
|
39
|
-
if (_downState == true) {
|
40
|
-
if (_pressedState == false || _pressedState == undefined) {
|
41
|
-
Keyboard.pressedKeyMap.set(key, true);
|
42
|
-
return true;
|
43
|
-
}
|
44
|
-
}
|
45
|
-
|
46
|
-
return false;
|
47
|
-
}
|
48
|
-
|
49
|
-
/**
|
50
|
-
*
|
51
|
-
* @param key string of key
|
52
|
-
* @returns if key is released. Returns only once
|
53
|
-
*/
|
54
|
-
isReleased(key: keys) {
|
55
|
-
const _releasedState = Keyboard.releasedKeyMap.get(key);
|
56
|
-
const _downState = Keyboard.downKeyMap.get(key);
|
57
|
-
|
58
|
-
if (_downState == false) {
|
59
|
-
if (_releasedState == false || undefined) {
|
60
|
-
Keyboard.releasedKeyMap.set(key, true);
|
61
|
-
return true;
|
62
|
-
}
|
63
|
-
}
|
64
|
-
|
65
|
-
return false;
|
66
|
-
}
|
67
|
-
}
|
68
|
-
|
69
|
-
type keys = 'a' |'b' |'c' |'d' |'e' |'f' |'g' |'h' |'i' |'j' |'k' |'l' |'m' |'n' |'o' |'p' |'q' |'r' |'s' |'t' |'u' |'v' |'w' |'x' |'y' |'z' |
|
70
|
-
'1' |'2' |'3' |'4' |'5' |'6' |'7' |'8' |'9' |'0' |
|
71
|
-
'space' |
|
72
|
-
'caps lock' |
|
73
|
-
'tab' |
|
74
|
-
'left shift' |
|
75
|
-
'right shift' |
|
76
|
-
'left ctrl' |
|
77
|
-
'escape';
|
78
|
-
|
79
|
-
/** @internal */
|
1
|
+
/** @internal */
|
2
|
+
class Keyboard {
|
3
|
+
|
4
|
+
static downKeyMap = new Map<string, boolean>();
|
5
|
+
static pressedKeyMap = new Map<string, boolean>();
|
6
|
+
static releasedKeyMap = new Map<string, boolean>();
|
7
|
+
|
8
|
+
static setKeyDown(key: string) {
|
9
|
+
this.downKeyMap.set(key, true);
|
10
|
+
this.releasedKeyMap.set(key, false);
|
11
|
+
}
|
12
|
+
|
13
|
+
static setKeyUp(key: string) {
|
14
|
+
this.downKeyMap.set(key, false);
|
15
|
+
this.pressedKeyMap.set(key, false);
|
16
|
+
}
|
17
|
+
|
18
|
+
/**
|
19
|
+
*
|
20
|
+
* @param key string of key
|
21
|
+
* @returns if the key is being held down
|
22
|
+
*/
|
23
|
+
isDown(key: keys) {
|
24
|
+
const _state = Keyboard.downKeyMap.get(key);
|
25
|
+
if (_state == undefined) return false
|
26
|
+
|
27
|
+
return _state;
|
28
|
+
}
|
29
|
+
|
30
|
+
/**
|
31
|
+
*
|
32
|
+
* @param key string of key
|
33
|
+
* @returns if key is pressed. Returns only once
|
34
|
+
*/
|
35
|
+
isPressed(key: keys) {
|
36
|
+
const _pressedState = Keyboard.pressedKeyMap.get(key);
|
37
|
+
const _downState = Keyboard.downKeyMap.get(key);
|
38
|
+
|
39
|
+
if (_downState == true) {
|
40
|
+
if (_pressedState == false || _pressedState == undefined) {
|
41
|
+
Keyboard.pressedKeyMap.set(key, true);
|
42
|
+
return true;
|
43
|
+
}
|
44
|
+
}
|
45
|
+
|
46
|
+
return false;
|
47
|
+
}
|
48
|
+
|
49
|
+
/**
|
50
|
+
*
|
51
|
+
* @param key string of key
|
52
|
+
* @returns if key is released. Returns only once
|
53
|
+
*/
|
54
|
+
isReleased(key: keys) {
|
55
|
+
const _releasedState = Keyboard.releasedKeyMap.get(key);
|
56
|
+
const _downState = Keyboard.downKeyMap.get(key);
|
57
|
+
|
58
|
+
if (_downState == false) {
|
59
|
+
if (_releasedState == false || undefined) {
|
60
|
+
Keyboard.releasedKeyMap.set(key, true);
|
61
|
+
return true;
|
62
|
+
}
|
63
|
+
}
|
64
|
+
|
65
|
+
return false;
|
66
|
+
}
|
67
|
+
}
|
68
|
+
|
69
|
+
type keys = 'a' |'b' |'c' |'d' |'e' |'f' |'g' |'h' |'i' |'j' |'k' |'l' |'m' |'n' |'o' |'p' |'q' |'r' |'s' |'t' |'u' |'v' |'w' |'x' |'y' |'z' |
|
70
|
+
'1' |'2' |'3' |'4' |'5' |'6' |'7' |'8' |'9' |'0' |
|
71
|
+
'space' |
|
72
|
+
'caps lock' |
|
73
|
+
'tab' |
|
74
|
+
'left shift' |
|
75
|
+
'right shift' |
|
76
|
+
'left ctrl' |
|
77
|
+
'escape';
|
78
|
+
|
79
|
+
/** @internal */
|
80
80
|
export default Keyboard;
|