slifer 0.2.1 → 0.2.3
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/README.md +8 -6
- package/index.ts +8 -1
- package/libs/libSDL2.so +0 -0
- package/libs/libSDL2_image.so +0 -0
- package/libs/libSDL2_mixer.dll +0 -0
- package/libs/libSDL2_mixer.dylib +0 -0
- package/libs/libSDL2_mixer.so +0 -0
- package/package.json +1 -1
- package/src/engine/image.ts +28 -0
- package/src/engine/rectangle.ts +16 -4
- package/src/engine/renderer.ts +40 -0
- package/src/engine/time.ts +79 -0
- package/src/engine/vector.ts +1 -3
- package/src/engine/window.ts +79 -0
- package/src/engine.ts +20 -13
- package/src/ffi.ts +268 -220
- package/src/modules/audio.ts +41 -0
- package/src/modules/graphics.ts +57 -142
- package/src/modules/keyboard.ts +102 -57
- package/src/modules/mouse.ts +22 -14
- package/src/slifer.test.ts +38 -3
- package/src/slifer.ts +42 -108
package/README.md
CHANGED
@@ -7,7 +7,7 @@
|
|
7
7
|
> [!CAUTION]
|
8
8
|
> Slifer is currently in alpha. Use at your own risk.
|
9
9
|
|
10
|
-
> [!NOTE]
|
10
|
+
> [!NOTE]
|
11
11
|
> Not all basic features have been implemented. Many are missing such as
|
12
12
|
> window customization. As such, I recommend waiting for a beta release of
|
13
13
|
> Slifer before using it for a long term project.
|
@@ -16,6 +16,8 @@ Slifer is a game framework made to allow users to code games in typescript. The
|
|
16
16
|
framework uses bun and SDL2 under the hood to allow your game to render and
|
17
17
|
build natively to desktop.
|
18
18
|
|
19
|
+
If you'd like to learn more about Slifer, feel free to head to [Slifers Webpage](https://slifer.hazyvt.com).
|
20
|
+
|
19
21
|
## Contents
|
20
22
|
|
21
23
|
- [Goals](#goals)
|
@@ -53,13 +55,13 @@ Slifer.createWindow("Example Window", 640, 480);
|
|
53
55
|
const bg = Slifer.Graphics.makeColor(36, 36, 36, 255);
|
54
56
|
|
55
57
|
while (!Slifer.shouldClose()) {
|
56
|
-
|
58
|
+
Slifer.Graphics.setBackground(bg);
|
57
59
|
|
58
|
-
|
59
|
-
|
60
|
-
|
60
|
+
if (Slifer.Keyboard.isPressed("escape")) {
|
61
|
+
Slifer.isRunning = false;
|
62
|
+
}
|
61
63
|
|
62
|
-
|
64
|
+
Slifer.Graphics.render();
|
63
65
|
}
|
64
66
|
|
65
67
|
Slifer.quit();
|
package/index.ts
CHANGED
@@ -1,4 +1,11 @@
|
|
1
1
|
import { SliferClass } from "./src/slifer";
|
2
2
|
|
3
|
+
export { Image } from './src/engine/image';
|
4
|
+
export { Vector2 } from './src/engine/vector';
|
5
|
+
export { Rectangle } from './src/engine/rectangle';
|
6
|
+
export { Timer } from './src/engine/time';
|
7
|
+
export { AudioSource } from './src/modules/audio'
|
8
|
+
|
3
9
|
const Slifer = new SliferClass();
|
4
|
-
export default Slifer;
|
10
|
+
export default Slifer;
|
11
|
+
|
package/libs/libSDL2.so
CHANGED
Binary file
|
package/libs/libSDL2_image.so
CHANGED
Binary file
|
Binary file
|
Binary file
|
Binary file
|
package/package.json
CHANGED
@@ -0,0 +1,28 @@
|
|
1
|
+
import { type Pointer, ptr } from 'bun:ffi';
|
2
|
+
import { libsdl } from '../ffi';
|
3
|
+
import { Vector2 } from './vector';
|
4
|
+
|
5
|
+
export class Image {
|
6
|
+
public readonly pointer: Pointer;
|
7
|
+
public readonly size: Vector2;
|
8
|
+
public flipH: boolean = false;
|
9
|
+
|
10
|
+
constructor(texture: Pointer) {
|
11
|
+
this.pointer = texture;
|
12
|
+
|
13
|
+
const _wArr = new Uint32Array(1);
|
14
|
+
const _hArr = new Uint32Array(1);
|
15
|
+
|
16
|
+
libsdl.symbols.SDL_QueryTexture(
|
17
|
+
texture,
|
18
|
+
null,
|
19
|
+
null,
|
20
|
+
ptr(_wArr),
|
21
|
+
ptr(_hArr)
|
22
|
+
);
|
23
|
+
|
24
|
+
this.size = new Vector2(_wArr[0], _hArr[0]);
|
25
|
+
}
|
26
|
+
|
27
|
+
}
|
28
|
+
|
package/src/engine/rectangle.ts
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
import { ptr } from "bun:ffi";
|
2
|
-
import Vector2 from "./vector";
|
2
|
+
import { Vector2 } from "./vector";
|
3
3
|
|
4
|
-
class Rectangle {
|
5
|
-
|
4
|
+
export class Rectangle {
|
5
|
+
public readonly pointer;
|
6
6
|
public position: Vector2;
|
7
7
|
public size: Vector2;
|
8
8
|
|
@@ -20,6 +20,18 @@ class Rectangle {
|
|
20
20
|
static empty() {
|
21
21
|
return new Rectangle(new Vector2(0, 0), new Vector2(0, 0));
|
22
22
|
}
|
23
|
+
|
24
|
+
public isColliding(rectangle: Rectangle) : boolean {
|
25
|
+
if (
|
26
|
+
this.position.x < rectangle.position.x + rectangle.size.x &&
|
27
|
+
this.position.x + this.size.x > rectangle.position.x &&
|
28
|
+
this.position.y < rectangle.position.y + rectangle.size.y &&
|
29
|
+
this.position.y + this.size.y > rectangle.position.y
|
30
|
+
) {
|
31
|
+
return true;
|
32
|
+
}
|
33
|
+
|
34
|
+
return false;
|
35
|
+
}
|
23
36
|
}
|
24
37
|
|
25
|
-
export default Rectangle;
|
@@ -0,0 +1,40 @@
|
|
1
|
+
import { type Pointer } from "bun:ffi";
|
2
|
+
import { libsdl } from "../ffi";
|
3
|
+
import Window from "./window";
|
4
|
+
|
5
|
+
class Renderer {
|
6
|
+
static #instance: Renderer;
|
7
|
+
static #pointer: Pointer;
|
8
|
+
|
9
|
+
private constructor() {}
|
10
|
+
|
11
|
+
public static get instance() {
|
12
|
+
if (!Renderer.#instance) {
|
13
|
+
Renderer.#instance = new Renderer();
|
14
|
+
}
|
15
|
+
|
16
|
+
return Renderer.#instance;
|
17
|
+
}
|
18
|
+
|
19
|
+
public static get pointer() {
|
20
|
+
return Renderer.#pointer;
|
21
|
+
}
|
22
|
+
|
23
|
+
static createRenderer() {
|
24
|
+
const vsyncHint = 0x00000004;
|
25
|
+
const _ren = libsdl.symbols.SDL_CreateRenderer(
|
26
|
+
Window.pointer,
|
27
|
+
-1,
|
28
|
+
vsyncHint
|
29
|
+
);
|
30
|
+
|
31
|
+
if (_ren == null) throw `Renderer Creation Failed`;
|
32
|
+
Renderer.#pointer = _ren;
|
33
|
+
}
|
34
|
+
|
35
|
+
static clear() {
|
36
|
+
libsdl.symbols.SDL_RenderClear(Renderer.pointer);
|
37
|
+
}
|
38
|
+
}
|
39
|
+
|
40
|
+
export default Renderer;
|
@@ -0,0 +1,79 @@
|
|
1
|
+
import { libsdl } from "../ffi";
|
2
|
+
|
3
|
+
export class Timer {
|
4
|
+
|
5
|
+
private mStartTicks;
|
6
|
+
private mPausedTicks;
|
7
|
+
private mPaused;
|
8
|
+
private mStarted;
|
9
|
+
|
10
|
+
|
11
|
+
constructor() {
|
12
|
+
this.mStartTicks = 0;
|
13
|
+
this.mPausedTicks = 0;
|
14
|
+
this.mPaused = false;
|
15
|
+
this.mStarted = false;
|
16
|
+
}
|
17
|
+
|
18
|
+
public start() {
|
19
|
+
this.mStarted = true;
|
20
|
+
|
21
|
+
this.mPaused = false;
|
22
|
+
|
23
|
+
this.mStartTicks = libsdl.symbols.SDL_GetTicks();
|
24
|
+
this.mPausedTicks = 0;
|
25
|
+
}
|
26
|
+
|
27
|
+
public stop() {
|
28
|
+
this.mStarted = false;
|
29
|
+
this.mPaused = true;
|
30
|
+
|
31
|
+
this.mStartTicks = 0;
|
32
|
+
this.mPausedTicks = 0;
|
33
|
+
}
|
34
|
+
|
35
|
+
public pause() {
|
36
|
+
if (this.mStarted && !this.mPaused) {
|
37
|
+
this.mPaused = true;
|
38
|
+
|
39
|
+
this.mPausedTicks = libsdl.symbols.SDL_GetTicks() - this.mStartTicks;
|
40
|
+
this.mStartTicks = 0;
|
41
|
+
}
|
42
|
+
}
|
43
|
+
|
44
|
+
public unpause() {
|
45
|
+
if (this.mStarted && this.mPaused) {
|
46
|
+
this.mPaused = false;
|
47
|
+
|
48
|
+
this.mStartTicks = libsdl.symbols.SDL_GetTicks() - this.mPausedTicks;
|
49
|
+
this.mPausedTicks = 0;
|
50
|
+
}
|
51
|
+
}
|
52
|
+
|
53
|
+
public getTicks() {
|
54
|
+
let time = 0;
|
55
|
+
|
56
|
+
if (this.mStarted) {
|
57
|
+
if (this.mPaused) {
|
58
|
+
time = this.mPausedTicks;
|
59
|
+
} else {
|
60
|
+
time = libsdl.symbols.SDL_GetTicks() - this.mStartTicks;
|
61
|
+
}
|
62
|
+
}
|
63
|
+
|
64
|
+
return time;
|
65
|
+
}
|
66
|
+
|
67
|
+
public isStarted() {
|
68
|
+
return this.mStarted;
|
69
|
+
}
|
70
|
+
|
71
|
+
public isPaused() {
|
72
|
+
return (this.mPaused && this.mStarted);
|
73
|
+
}
|
74
|
+
|
75
|
+
|
76
|
+
|
77
|
+
|
78
|
+
}
|
79
|
+
|
package/src/engine/vector.ts
CHANGED
@@ -0,0 +1,79 @@
|
|
1
|
+
import { type Pointer } from "bun:ffi";
|
2
|
+
import { Vector2 } from "./vector";
|
3
|
+
import { libsdl } from "../ffi";
|
4
|
+
|
5
|
+
class Window {
|
6
|
+
static #instance: Window;
|
7
|
+
static #pointer: Pointer;
|
8
|
+
|
9
|
+
private title!: string;
|
10
|
+
private size!: Vector2;
|
11
|
+
|
12
|
+
private static readonly centerPos = 0x2fff0000;
|
13
|
+
|
14
|
+
private constructor() {}
|
15
|
+
|
16
|
+
public static get instance() {
|
17
|
+
if (!Window.#instance) {
|
18
|
+
Window.#instance = new Window();
|
19
|
+
}
|
20
|
+
|
21
|
+
return Window.#instance;
|
22
|
+
}
|
23
|
+
|
24
|
+
public static get pointer() {
|
25
|
+
return Window.#pointer;
|
26
|
+
}
|
27
|
+
|
28
|
+
public static createWindow(title: string, size: Vector2): void {
|
29
|
+
Window.instance.title = title;
|
30
|
+
Window.instance.size = size;
|
31
|
+
|
32
|
+
// Create cstring by buffer from string
|
33
|
+
const _titleBuffer = new Buffer(Window.instance.title + "\x00");
|
34
|
+
|
35
|
+
// Create window pointer
|
36
|
+
const _winPointer = libsdl.symbols.SDL_CreateWindow(
|
37
|
+
_titleBuffer,
|
38
|
+
Window.centerPos,
|
39
|
+
Window.centerPos,
|
40
|
+
Window.instance.size.x,
|
41
|
+
Window.instance.size.y,
|
42
|
+
0
|
43
|
+
);
|
44
|
+
|
45
|
+
if (_winPointer == null) throw `Window Creation Failed`;
|
46
|
+
Window.#pointer = _winPointer;
|
47
|
+
}
|
48
|
+
|
49
|
+
public setSize(size: Vector2): void {
|
50
|
+
this.size = size;
|
51
|
+
libsdl.symbols.SDL_SetWindowSize(Window.pointer, size.x, size.y);
|
52
|
+
}
|
53
|
+
|
54
|
+
public setTitle(title: string): void {
|
55
|
+
this.title = title;
|
56
|
+
libsdl.symbols.SDL_SetWindowTitle(
|
57
|
+
Window.pointer,
|
58
|
+
Buffer.from(title + "\x00")
|
59
|
+
);
|
60
|
+
}
|
61
|
+
|
62
|
+
public setFullscreen(flag: boolean) {
|
63
|
+
libsdl.symbols.SDL_SetWindowFullscreen(Window.pointer, Number(flag));
|
64
|
+
}
|
65
|
+
|
66
|
+
public setPosition(position: Vector2) {
|
67
|
+
libsdl.symbols.SDL_SetWindowPosition(
|
68
|
+
Window.pointer,
|
69
|
+
position.x,
|
70
|
+
position.y
|
71
|
+
);
|
72
|
+
}
|
73
|
+
|
74
|
+
public centerWindow() {
|
75
|
+
this.setPosition(new Vector2(Window.centerPos, Window.centerPos));
|
76
|
+
}
|
77
|
+
}
|
78
|
+
|
79
|
+
export default Window;
|
package/src/engine.ts
CHANGED
@@ -1,24 +1,31 @@
|
|
1
|
-
import { libsdl, libimage, libttf } from "./ffi";
|
2
|
-
import
|
1
|
+
import { libsdl, libimage, libttf, libmixer } from "./ffi";
|
2
|
+
import Graphics from "./modules/graphics";
|
3
3
|
|
4
|
-
|
5
|
-
const
|
6
|
-
|
7
|
-
|
8
|
-
const baseInit = libsdl.symbols.SDL_Init(0x00000020);
|
4
|
+
export function initSDL() {
|
5
|
+
const initVideo = 0x00000020;
|
6
|
+
const initAudio = 0x00000010;
|
7
|
+
const baseInit = libsdl.symbols.SDL_Init(initVideo + initAudio);
|
9
8
|
if (baseInit != 0) throw `SDL failed to initialize`;
|
9
|
+
}
|
10
10
|
|
11
|
+
export function initSDLImage() {
|
11
12
|
const imageInit = libimage.symbols.IMG_Init(3);
|
12
13
|
if (imageInit != 3) throw `SDL Image failed to initialize`;
|
14
|
+
}
|
13
15
|
|
16
|
+
export function initSDLTypeFont() {
|
14
17
|
const ttfInit = libttf.symbols.TTF_Init();
|
15
18
|
if (ttfInit != 0) throw `SDL TTF failed to initialize`;
|
19
|
+
}
|
16
20
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
+
export function initSDLMixer() {
|
22
|
+
const mixInit = libmixer.symbols.Mix_OpenAudio(22050, null, 2, 4096);
|
23
|
+
if (mixInit != 0) throw `SDL Audio failed to initialize`;
|
24
|
+
}
|
21
25
|
|
22
|
-
|
23
|
-
|
26
|
+
export function initLibraries(): void {
|
27
|
+
initSDL();
|
28
|
+
initSDLImage();
|
29
|
+
initSDLTypeFont();
|
30
|
+
initSDLMixer();
|
24
31
|
}
|