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/graphics.ts
CHANGED
@@ -1,156 +1,158 @@
|
|
1
|
-
import { libimage, libsdl, libttf } from "../ffi";
|
2
|
-
import Global from "../global";
|
3
|
-
import { type Pointer, ptr } from 'bun:ffi';
|
4
|
-
import path from 'path';
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
*
|
17
|
-
*
|
18
|
-
* @param
|
19
|
-
* @param
|
20
|
-
* @param
|
21
|
-
* @
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
*
|
31
|
-
*
|
32
|
-
*
|
33
|
-
*
|
34
|
-
*
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
libsdl.symbols.
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
*
|
44
|
-
*
|
45
|
-
* @
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
const
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
*
|
59
|
-
*
|
60
|
-
* @param
|
61
|
-
* @param
|
62
|
-
* @param
|
63
|
-
* @param
|
64
|
-
* @param
|
65
|
-
* @param
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
const
|
70
|
-
const
|
71
|
-
|
72
|
-
|
73
|
-
_dest[
|
74
|
-
_dest[
|
75
|
-
_dest[
|
76
|
-
|
77
|
-
_center
|
78
|
-
_center[
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
*
|
85
|
-
*
|
86
|
-
* @param
|
87
|
-
* @param
|
88
|
-
* @param
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
const
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
destArr
|
112
|
-
destArr[
|
113
|
-
destArr[
|
114
|
-
destArr[
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
*
|
123
|
-
*
|
124
|
-
* @param
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
}
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
}
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
readonly
|
145
|
-
readonly
|
146
|
-
readonly
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
this.
|
151
|
-
this.
|
152
|
-
this.
|
153
|
-
|
154
|
-
}
|
155
|
-
|
1
|
+
import { libimage, libsdl, libttf } from "../ffi";
|
2
|
+
import Global from "../global";
|
3
|
+
import { type Pointer, ptr } from 'bun:ffi';
|
4
|
+
import path from 'path';
|
5
|
+
|
6
|
+
/** @internal */
|
7
|
+
class Graphics {
|
8
|
+
/**
|
9
|
+
* Slifers draw function. Used to draw everything to the screen.
|
10
|
+
*/
|
11
|
+
render() {
|
12
|
+
libsdl.symbols.SDL_RenderPresent(Global.ptrRenderer);
|
13
|
+
}
|
14
|
+
|
15
|
+
/**
|
16
|
+
* Create a new color. All values are from 0-255
|
17
|
+
*
|
18
|
+
* @param r red value
|
19
|
+
* @param g green value
|
20
|
+
* @param b blue value
|
21
|
+
* @param a alpha value
|
22
|
+
* @returns Color object
|
23
|
+
*/
|
24
|
+
makeColor(r: number, g: number, b: number, a: number) {
|
25
|
+
const _color = new Color(r, g, b, a);
|
26
|
+
return _color;
|
27
|
+
}
|
28
|
+
|
29
|
+
/**
|
30
|
+
* Sets the background of the window to a color of choice.
|
31
|
+
*
|
32
|
+
* Make sure this is put in the top level of the while loop
|
33
|
+
* as it will clear the renderer.
|
34
|
+
*
|
35
|
+
* @param color Color object. Make using Slifer.Graphics.makeColor
|
36
|
+
*/
|
37
|
+
setBackground(color: Color) {
|
38
|
+
libsdl.symbols.SDL_SetRenderDrawColor(Global.ptrRenderer, color.r, color.g, color.b, color.a);
|
39
|
+
libsdl.symbols.SDL_RenderClear(Global.ptrRenderer);
|
40
|
+
}
|
41
|
+
|
42
|
+
/**
|
43
|
+
* Loads a new image
|
44
|
+
*
|
45
|
+
* @param path string path to image
|
46
|
+
* @returns Image ready to draw
|
47
|
+
*/
|
48
|
+
loadImage(path: string) : Image {
|
49
|
+
const _path = Buffer.from(path + "\x00");
|
50
|
+
const surface = libimage.symbols.IMG_Load(_path);
|
51
|
+
if (surface == null) throw `Image failed to load`;
|
52
|
+
const texture = libsdl.symbols.SDL_CreateTextureFromSurface(Global.ptrRenderer, surface);
|
53
|
+
if (texture == null) throw `Image failed to be created`;
|
54
|
+
return new Image(texture);
|
55
|
+
}
|
56
|
+
|
57
|
+
/**
|
58
|
+
* Method to draw the image to the screen
|
59
|
+
*
|
60
|
+
* @param image Image object to draw. Made using Slifer.Graphics.loadImage
|
61
|
+
* @param x x position to draw image
|
62
|
+
* @param y y position to draw image
|
63
|
+
* @param rotation (optional) rotation of image
|
64
|
+
* @param xs (optional) scale of x axis
|
65
|
+
* @param ys (optional) scale of y axis
|
66
|
+
* @param flip (optional) horizontal flip
|
67
|
+
*/
|
68
|
+
draw(image: Image, x: number, y: number, rotation?: number, xs?: number, ys?: number, flip?: true) {
|
69
|
+
const _dest = new Uint32Array(4);
|
70
|
+
const wArr = new Uint32Array(1);
|
71
|
+
const hArr = new Uint32Array(1);
|
72
|
+
libsdl.symbols.SDL_QueryTexture((image as any).pointer, null, null, ptr(wArr), ptr(hArr));
|
73
|
+
_dest[0] = x;
|
74
|
+
_dest[1] = y;
|
75
|
+
_dest[2] = wArr[0] * (xs ? xs : 1);
|
76
|
+
_dest[3] = hArr[0] * (ys ? ys : 1);
|
77
|
+
const _center = new Uint32Array(2);
|
78
|
+
_center[0] = _dest[2] / 2;
|
79
|
+
_center[1] = _dest[3] / 2;
|
80
|
+
libsdl.symbols.SDL_RenderCopyEx(Global.ptrRenderer, (image as any).pointer, null, ptr(_dest), rotation ? rotation : 0, ptr(_center), flip ? Number(flip) : 0);
|
81
|
+
}
|
82
|
+
|
83
|
+
/**
|
84
|
+
* Method to draw text to the screen
|
85
|
+
*
|
86
|
+
* @param text the string of text to print
|
87
|
+
* @param x x position
|
88
|
+
* @param y y position
|
89
|
+
* @param color color of text. Made using Slifer.Graphics.makeColor.
|
90
|
+
*/
|
91
|
+
print(text: string, x: number, y: number, color: Color) {
|
92
|
+
|
93
|
+
// Create text buffer
|
94
|
+
const textBuffer = Buffer.from(text+"\x00");
|
95
|
+
|
96
|
+
// Get width and height of text
|
97
|
+
const wArr = new Uint32Array(1);
|
98
|
+
const hArr = new Uint32Array(1);
|
99
|
+
libttf.symbols.TTF_SizeText(Global.ptrFont,textBuffer , ptr(wArr), ptr(hArr));
|
100
|
+
|
101
|
+
// Define color
|
102
|
+
const _col = ((color.r << 0) + (color.g << 8) + (color.b << 16));
|
103
|
+
|
104
|
+
// Create texture
|
105
|
+
const surface = libttf.symbols.TTF_RenderText_Solid(Global.ptrFont, textBuffer, _col);
|
106
|
+
if (surface == null) throw `Surface creation failed on print`;
|
107
|
+
const texture = libsdl.symbols.SDL_CreateTextureFromSurface(Global.ptrRenderer, surface);
|
108
|
+
if (texture == null) throw `Texture creation failed on print`;
|
109
|
+
|
110
|
+
// Create destination
|
111
|
+
const destArr = new Uint32Array(4);
|
112
|
+
destArr[0] = x;
|
113
|
+
destArr[1] = y;
|
114
|
+
destArr[2] = wArr[0];
|
115
|
+
destArr[3] = hArr[0];
|
116
|
+
|
117
|
+
// Draw text
|
118
|
+
libsdl.symbols.SDL_RenderCopy(Global.ptrRenderer, texture, null, ptr(destArr));
|
119
|
+
}
|
120
|
+
|
121
|
+
/**
|
122
|
+
* Sets the font to a ttf file in your project
|
123
|
+
*
|
124
|
+
* @param path relative path to font
|
125
|
+
* @param pt size of text
|
126
|
+
*/
|
127
|
+
setFont(path: string, pt: number) {
|
128
|
+
const tempFont = libttf.symbols.TTF_OpenFont(Buffer.from(path+"\x00"), pt);
|
129
|
+
if (tempFont == null) throw `Font loading failed`;
|
130
|
+
Global.ptrFont = tempFont;
|
131
|
+
}
|
132
|
+
}
|
133
|
+
|
134
|
+
class Image {
|
135
|
+
|
136
|
+
private pointer;
|
137
|
+
|
138
|
+
constructor(texture: Pointer) {
|
139
|
+
this.pointer = texture;
|
140
|
+
}
|
141
|
+
}
|
142
|
+
|
143
|
+
class Color {
|
144
|
+
readonly r;
|
145
|
+
readonly g;
|
146
|
+
readonly b;
|
147
|
+
readonly a;
|
148
|
+
|
149
|
+
constructor(r: number, g: number, b: number, a: number) {
|
150
|
+
this.r = r;
|
151
|
+
this.g = g;
|
152
|
+
this.b = b;
|
153
|
+
this.a = a;
|
154
|
+
}
|
155
|
+
}
|
156
|
+
|
157
|
+
/** @internal */
|
156
158
|
export default Graphics;
|
package/src/modules/keyboard.ts
CHANGED
@@ -1,78 +1,80 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
static
|
5
|
-
static
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
this.
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
this.
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
*
|
20
|
-
* @
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
*
|
32
|
-
* @
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
const
|
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
|
-
|
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 */
|
78
80
|
export default Keyboard;
|