slifer 0.2.7 → 0.2.8
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/.npmignore +2 -1
- package/index.ts +2 -1
- package/package.json +1 -1
- package/src/engine/canvas.ts +58 -0
- package/src/engine/image.ts +11 -22
- package/src/engine/render.ts +6 -2
- package/src/engine/window.ts +8 -5
- package/src/ffi.ts +20 -0
- package/src/modules/graphics.ts +57 -78
- package/src/slifer.ts +5 -3
package/.npmignore
CHANGED
package/index.ts
CHANGED
@@ -5,6 +5,7 @@ import Color from "./src/engine/color";
|
|
5
5
|
import Audio from "./src/engine/audio";
|
6
6
|
import Rectangle from "./src/engine/rectangle";
|
7
7
|
import Font from "./src/engine/font";
|
8
|
+
import Canvas from './src/engine/canvas';
|
8
9
|
|
9
10
|
const slf = new Slifer();
|
10
|
-
export { Vector2, Color, Image, Audio, Rectangle, Font, slf as Slifer };
|
11
|
+
export { Vector2, Color, Image, Audio, Rectangle, Font, Canvas, slf as Slifer };
|
package/package.json
CHANGED
@@ -0,0 +1,58 @@
|
|
1
|
+
import { type Pointer, ptr } from 'bun:ffi';
|
2
|
+
import { libsdl } from '../ffi';
|
3
|
+
import Image from './image';
|
4
|
+
import Render from './render';
|
5
|
+
import Color from './color';
|
6
|
+
|
7
|
+
/** @internal */
|
8
|
+
export default class Canvas {
|
9
|
+
|
10
|
+
private pointer : Pointer;
|
11
|
+
|
12
|
+
public readonly width;
|
13
|
+
public readonly height;
|
14
|
+
|
15
|
+
constructor(width: number, height: number) {
|
16
|
+
const surPointer = libsdl.symbols.SDL_CreateRGBSurface(0, width, height, 32, 0, 0, 0, 0)
|
17
|
+
if (surPointer == null) throw `Canvas creation failed`;
|
18
|
+
this.pointer = surPointer;
|
19
|
+
this.width = width;
|
20
|
+
this.height = height;
|
21
|
+
}
|
22
|
+
|
23
|
+
private drawBG(color: Color) {
|
24
|
+
const rect = new Uint32Array(4);
|
25
|
+
rect[0] = 0;
|
26
|
+
rect[1] = 0;
|
27
|
+
rect[2] = this.width;
|
28
|
+
rect[3] = this.height;
|
29
|
+
|
30
|
+
const _col = ((color.r << 16) + (color.g << 8) + (color.b << 0));
|
31
|
+
|
32
|
+
libsdl.symbols.SDL_FillRect(this.pointer, ptr(rect), _col);
|
33
|
+
}
|
34
|
+
|
35
|
+
clear() {
|
36
|
+
this.drawBG(new Color(0, 0, 0, 0));
|
37
|
+
}
|
38
|
+
|
39
|
+
setBackground(color: Color) {
|
40
|
+
this.drawBG(color);
|
41
|
+
}
|
42
|
+
|
43
|
+
draw(drawable: Image | Canvas, x: number, y: number, scaleX?: number, scaleY?: number) : void {
|
44
|
+
const destArray = new Uint32Array(4);
|
45
|
+
destArray[0] = x;
|
46
|
+
destArray[1] = y;
|
47
|
+
destArray[2] = drawable.width * (scaleX ? scaleX : 1);
|
48
|
+
destArray[3] = drawable.height * (scaleY ? scaleY : 1);
|
49
|
+
|
50
|
+
libsdl.symbols.SDL_UpperBlitScaled(
|
51
|
+
(drawable as any).pointer,
|
52
|
+
null,
|
53
|
+
this.pointer,
|
54
|
+
ptr(destArray)
|
55
|
+
);
|
56
|
+
}
|
57
|
+
|
58
|
+
}
|
package/src/engine/image.ts
CHANGED
@@ -1,34 +1,23 @@
|
|
1
1
|
import { libimage, libsdl } from "../ffi";
|
2
|
-
import { type Pointer, ptr } from 'bun:ffi';
|
2
|
+
import { type Pointer, ptr, toArrayBuffer } from 'bun:ffi';
|
3
3
|
import Render from "./render";
|
4
4
|
|
5
5
|
/** @internal */
|
6
6
|
export default class Image {
|
7
7
|
|
8
8
|
private pointer: Pointer;
|
9
|
-
private destArr: Uint32Array;
|
10
9
|
|
11
|
-
public readonly width;
|
12
|
-
public readonly height;
|
10
|
+
public readonly width: number;
|
11
|
+
public readonly height: number;
|
13
12
|
|
14
13
|
constructor(path: string) {
|
15
14
|
const cs = Buffer.from(path+'\x00');
|
16
|
-
//@ts-expect-error
|
17
|
-
const
|
18
|
-
if (
|
19
|
-
this.pointer =
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
libsdl.symbols.SDL_QueryTexture(texture, null, null, ptr(wArr), ptr(hArr));
|
24
|
-
|
25
|
-
this.width = wArr[0];
|
26
|
-
this.height = hArr[0];
|
27
|
-
|
28
|
-
this.destArr = new Uint32Array(4);
|
29
|
-
this.destArr[0] = 0;
|
30
|
-
this.destArr[1] = 0;
|
31
|
-
this.destArr[2] = this.width;
|
32
|
-
this.destArr[3] = this.height;
|
15
|
+
//@ts-expect-error
|
16
|
+
const surface = libimage.symbols.IMG_Load(cs);
|
17
|
+
if (surface == null) throw `Loading ${path} failed`;
|
18
|
+
this.pointer = surface;
|
19
|
+
const dv = new DataView(toArrayBuffer(this.pointer, 0, 21));
|
20
|
+
this.width = dv.getUint8(16);
|
21
|
+
this.height = dv.getUint8(20);
|
33
22
|
}
|
34
|
-
}
|
23
|
+
}
|
package/src/engine/render.ts
CHANGED
@@ -6,10 +6,14 @@ import Window from "./window";
|
|
6
6
|
export default class Render {
|
7
7
|
|
8
8
|
public static pointer : Pointer;
|
9
|
+
public static surface: Pointer;
|
9
10
|
|
10
|
-
public static createRenderer() {
|
11
|
+
public static createRenderer(width: number, height: number) {
|
11
12
|
const renPointer = libsdl.symbols.SDL_CreateRenderer(Window.pointer, -1, 0);
|
12
13
|
if (renPointer == null) throw `Renderer creation failed.`;
|
13
14
|
this.pointer = renPointer;
|
15
|
+
const surPointer = libsdl.symbols.SDL_CreateRGBSurface(0, width, height, 32, 0, 0, 0, 0)
|
16
|
+
if (surPointer == null) throw `Surface creation failed`;
|
17
|
+
this.surface = surPointer;
|
14
18
|
}
|
15
|
-
}
|
19
|
+
}
|
package/src/engine/window.ts
CHANGED
@@ -4,29 +4,32 @@ import { libsdl } from '../ffi';
|
|
4
4
|
|
5
5
|
/** @internal */
|
6
6
|
export default class Window {
|
7
|
-
public static pointer: Pointer;
|
8
7
|
|
8
|
+
public static pointer: Pointer;
|
9
|
+
public static size: Vector2;
|
10
|
+
|
9
11
|
private static centerPos = 0x2fff0000;
|
10
12
|
|
13
|
+
|
14
|
+
|
11
15
|
public static createWindow(title: string, size: Vector2) : void {
|
12
16
|
const winPointer = libsdl.symbols.SDL_CreateWindow(
|
13
|
-
//@ts-expect-error Buffer error
|
14
17
|
Buffer.from(title + '\x00'),
|
15
18
|
this.centerPos,
|
16
19
|
this.centerPos,
|
17
20
|
size.x,
|
18
21
|
size.y,
|
19
22
|
0
|
20
|
-
)
|
23
|
+
);
|
21
24
|
|
22
25
|
if (winPointer == null) throw `Window creation failed.`;
|
23
26
|
this.pointer = winPointer;
|
27
|
+
this.size = size;
|
24
28
|
}
|
25
29
|
|
26
30
|
public setTitle(title: string): void {
|
27
31
|
libsdl.symbols.SDL_SetWindowTitle(
|
28
32
|
Window.pointer,
|
29
|
-
//@ts-expect-error Buffer error
|
30
33
|
Buffer.from(title + "\x00")
|
31
34
|
);
|
32
35
|
}
|
@@ -46,4 +49,4 @@ export default class Window {
|
|
46
49
|
public centerWindow() {
|
47
50
|
this.setPosition(new Vector2(Window.centerPos, Window.centerPos));
|
48
51
|
}
|
49
|
-
}
|
52
|
+
}
|
package/src/ffi.ts
CHANGED
@@ -222,6 +222,26 @@ export const libsdl = dlopen(libSDLImport.default, {
|
|
222
222
|
},
|
223
223
|
SDL_GetTicks64: {
|
224
224
|
returns: 'u64'
|
225
|
+
},
|
226
|
+
SDL_CreateRGBSurface: {
|
227
|
+
args: ['int', 'int', 'int', 'int', 'int', 'int', 'int', 'int'],
|
228
|
+
returns: 'pointer'
|
229
|
+
},
|
230
|
+
SDL_UpperBlitScaled: {
|
231
|
+
args: ['pointer', 'pointer','pointer','pointer'],
|
232
|
+
returns: 'int'
|
233
|
+
},
|
234
|
+
SDL_FillRect: {
|
235
|
+
args: ['pointer', 'pointer', 'int'],
|
236
|
+
returns: 'int'
|
237
|
+
},
|
238
|
+
SDL_DestroyTexture : {
|
239
|
+
args: ['pointer'],
|
240
|
+
returns: 'void'
|
241
|
+
},
|
242
|
+
SDL_MapRGB: {
|
243
|
+
args: ['pointer', 'int', 'int', 'int'],
|
244
|
+
returns: 'u32'
|
225
245
|
}
|
226
246
|
});
|
227
247
|
|
package/src/modules/graphics.ts
CHANGED
@@ -4,118 +4,97 @@ import { ptr } from 'bun:ffi';
|
|
4
4
|
import Font from '../engine/font';
|
5
5
|
import type Image from "../engine/image";
|
6
6
|
import Render from "../engine/render";
|
7
|
+
import Window from '../engine/window';
|
7
8
|
import type Color from "../engine/color";
|
8
|
-
import
|
9
|
+
import Rectangle from "../engine/rectangle";
|
10
|
+
import Canvas from '../engine/canvas';
|
9
11
|
|
10
12
|
/** @internal */
|
11
13
|
export default class Graphics {
|
12
14
|
|
13
15
|
public render() : void {
|
16
|
+
const texture = libsdl.symbols.SDL_CreateTextureFromSurface(Render.pointer, Render.surface);
|
17
|
+
if (texture == null) throw `Render texture creation failed`;
|
18
|
+
libsdl.symbols.SDL_RenderCopy(
|
19
|
+
Render.pointer,
|
20
|
+
texture,
|
21
|
+
null,
|
22
|
+
null
|
23
|
+
);
|
14
24
|
libsdl.symbols.SDL_RenderPresent(Render.pointer);
|
25
|
+
libsdl.symbols.SDL_DestroyTexture(texture);
|
15
26
|
}
|
16
27
|
|
17
|
-
public draw(
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
public drawEx(image: Image, x: number, y: number, rotation?: number, scaleX?: number, scaleY?: number, flipH?: boolean) {
|
31
|
-
|
32
|
-
const destArr = (image as any).destArr;
|
33
|
-
destArr[0] = x;
|
34
|
-
destArr[1] = y;
|
35
|
-
destArr[2] = image.width * (scaleX ? scaleX : 1)
|
36
|
-
destArr[3] = image.height * (scaleY ? scaleY : 1);
|
37
|
-
|
38
|
-
libsdl.symbols.SDL_RenderCopyEx(
|
39
|
-
Render.pointer,
|
40
|
-
(image as any).pointer,
|
41
|
-
null,
|
42
|
-
ptr(destArr),
|
43
|
-
rotation ? rotation : 0,
|
44
|
-
null,
|
45
|
-
Number(flipH)
|
28
|
+
public draw(drawable: Image | Canvas, x: number, y: number, scaleX?: number, scaleY?: number) : void {
|
29
|
+
|
30
|
+
const destArray = new Uint32Array(4);
|
31
|
+
destArray[0] = x;
|
32
|
+
destArray[1] = y;
|
33
|
+
destArray[2] = drawable.width * (scaleX ? scaleX : 1);
|
34
|
+
destArray[3] = drawable.height * (scaleY ? scaleY : 1);
|
35
|
+
|
36
|
+
libsdl.symbols.SDL_UpperBlitScaled(
|
37
|
+
(drawable as any).pointer,
|
38
|
+
null,
|
39
|
+
Render.surface,
|
40
|
+
ptr(destArray)
|
46
41
|
);
|
47
|
-
|
48
|
-
|
49
42
|
}
|
50
43
|
|
51
44
|
public setBackground(color: Color) : void {
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
libsdl.symbols.SDL_RenderClear(Render.pointer);
|
59
|
-
}
|
45
|
+
const rect = new Rectangle(
|
46
|
+
new Vector2(0, 0),
|
47
|
+
new Vector2(Window.size.x, Window.size.y)
|
48
|
+
);
|
49
|
+
this.drawRect(rect, color);
|
50
|
+
}
|
60
51
|
|
61
52
|
public drawRect(rectangle: Rectangle, color: Color) {
|
62
|
-
|
63
|
-
Render.pointer,
|
64
|
-
color.r,
|
65
|
-
color.g,
|
66
|
-
color.b,
|
67
|
-
color.a
|
68
|
-
);
|
53
|
+
const _col = ((color.r << 16) + (color.g << 8) + (color.b << 0));
|
69
54
|
|
70
55
|
const rect = new Uint32Array(4);
|
71
56
|
rect[0] = rectangle.position.x;
|
72
|
-
|
57
|
+
rect[1] = rectangle.position.y;
|
73
58
|
rect[2] = rectangle.size.x;
|
74
59
|
rect[3] = rectangle.size.y;
|
75
60
|
|
76
|
-
libsdl.symbols.
|
61
|
+
libsdl.symbols.SDL_FillRect(Render.surface, ptr(rect), _col);
|
77
62
|
}
|
78
|
-
|
63
|
+
|
79
64
|
public print(text: string, x: number, y: number, font: Font, color: Color) {
|
80
65
|
const wArr = new Uint32Array(1);
|
81
66
|
const hArr = new Uint32Array(1);
|
82
|
-
|
67
|
+
|
83
68
|
libttf.symbols.TTF_SizeText(
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
69
|
+
(font as any).pointer,
|
70
|
+
//@ts-expect-error
|
71
|
+
Buffer.from(text+"\x00"),
|
72
|
+
ptr(wArr),
|
73
|
+
ptr(hArr)
|
88
74
|
);
|
89
75
|
|
90
|
-
const _col = ((color.r <<
|
76
|
+
const _col = ((color.r << 16) + (color.g << 8) + (color.b << 0));
|
91
77
|
|
92
78
|
const surface = libttf.symbols.TTF_RenderText_Solid(
|
93
79
|
(font as any).pointer,
|
94
|
-
|
95
|
-
|
80
|
+
//@ts-expect-error
|
81
|
+
Buffer.from(text+'\x00'),
|
82
|
+
_col
|
96
83
|
);
|
97
84
|
if (surface == null) throw `Rendering text failed`;
|
98
85
|
|
99
|
-
const
|
100
|
-
|
101
|
-
|
86
|
+
const destRect = new Uint32Array(4);
|
87
|
+
destRect[0] = x;
|
88
|
+
destRect[1] = y;
|
89
|
+
destRect[2] = wArr[0];
|
90
|
+
destRect[3] = hArr[0];
|
91
|
+
|
92
|
+
libsdl.symbols.SDL_UpperBlit(
|
93
|
+
surface,
|
94
|
+
null,
|
95
|
+
Render.surface,
|
96
|
+
ptr(destRect)
|
102
97
|
);
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
const destRect = new Uint32Array(4);
|
107
|
-
destRect[0] = x;
|
108
|
-
destRect[1] = y;
|
109
|
-
destRect[2] = wArr[0];
|
110
|
-
destRect[3] = hArr[0];
|
111
|
-
libsdl.symbols.SDL_RenderCopy(
|
112
|
-
Render.pointer,
|
113
|
-
texture,
|
114
|
-
null,
|
115
|
-
ptr(destRect)
|
116
|
-
);
|
117
|
-
|
118
|
-
|
119
|
-
|
98
|
+
|
120
99
|
}
|
121
100
|
}
|
package/src/slifer.ts
CHANGED
@@ -1,6 +1,5 @@
|
|
1
1
|
import { libsdl } from "./ffi";
|
2
2
|
import Window from "./engine/window";
|
3
|
-
import Renderer from "./engine/render";
|
4
3
|
import { ptr } from "bun:ffi";
|
5
4
|
import { initLibraries } from "./engine";
|
6
5
|
import { version } from "../package.json";
|
@@ -9,6 +8,7 @@ import Graphics from "./modules/graphics";
|
|
9
8
|
import Keyboard from "./modules/keyboard";
|
10
9
|
import Mouse from "./modules/mouse";
|
11
10
|
import Render from "./engine/render";
|
11
|
+
import Color from './engine/color';
|
12
12
|
|
13
13
|
class Slifer {
|
14
14
|
public isRunning: boolean;
|
@@ -21,16 +21,18 @@ class Slifer {
|
|
21
21
|
|
22
22
|
private start = 0;
|
23
23
|
private end = 0;
|
24
|
+
private black : Color;
|
24
25
|
|
25
26
|
constructor() {
|
26
27
|
initLibraries();
|
27
28
|
this.isRunning = true;
|
28
29
|
this.deltaTime = 0;
|
30
|
+
this.black = new Color(0,0,0,0);
|
29
31
|
}
|
30
32
|
|
31
33
|
public createWindow(title: string, size: Vector2): Window {
|
32
34
|
Window.createWindow(title, size);
|
33
|
-
|
35
|
+
Render.createRenderer(size.x, size.y);
|
34
36
|
|
35
37
|
this.start = Number(libsdl.symbols.SDL_GetTicks64());
|
36
38
|
|
@@ -39,7 +41,7 @@ class Slifer {
|
|
39
41
|
|
40
42
|
public shouldClose(): boolean {
|
41
43
|
libsdl.symbols.SDL_RenderClear(Render.pointer);
|
42
|
-
|
44
|
+
|
43
45
|
const eventArray = new Uint16Array(32);
|
44
46
|
const event = libsdl.symbols.SDL_PollEvent(ptr(eventArray));
|
45
47
|
if (event) {
|