slifer 0.2.1 → 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
- package/README.md +8 -6
- package/Sir-BC_stop.png +0 -0
- package/libs/libSDL2.so +0 -0
- package/libs/libSDL2_image.so +0 -0
- package/libs/libSDL2_mixer.dylib +0 -0
- package/libs/libSDL2_mixer.so +0 -0
- package/package.json +1 -1
- package/sample.wav +0 -0
- package/src/engine/renderer.ts +40 -0
- package/src/engine/time.ts +34 -0
- package/src/engine/window.ts +79 -0
- package/src/engine.ts +20 -13
- package/src/ffi.ts +257 -220
- package/src/modules/audio.ts +41 -0
- package/src/modules/graphics.ts +47 -123
- package/src/modules/keyboard.ts +80 -21
- package/src/modules/mouse.ts +22 -14
- package/src/slifer.test.ts +38 -3
- package/src/slifer.ts +32 -99
- package/bun.lockb +0 -0
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/Sir-BC_stop.png
ADDED
Binary file
|
package/libs/libSDL2.so
CHANGED
Binary file
|
package/libs/libSDL2_image.so
CHANGED
Binary file
|
Binary file
|
Binary file
|
package/package.json
CHANGED
package/sample.wav
ADDED
Binary file
|
@@ -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,34 @@
|
|
1
|
+
import { libsdl } from "../ffi";
|
2
|
+
|
3
|
+
class Time {
|
4
|
+
static #instance: Time;
|
5
|
+
|
6
|
+
private lastframe: number = 0;
|
7
|
+
private firstFrame: number = 0;
|
8
|
+
|
9
|
+
private constructor() {}
|
10
|
+
|
11
|
+
public static get instance() {
|
12
|
+
if (!Time.#instance) {
|
13
|
+
Time.#instance = new Time();
|
14
|
+
}
|
15
|
+
|
16
|
+
return Time.#instance;
|
17
|
+
}
|
18
|
+
|
19
|
+
public init() {
|
20
|
+
this.firstFrame = Number(libsdl.symbols.SDL_GetPerformanceCounter());
|
21
|
+
}
|
22
|
+
|
23
|
+
public calcDelta(): number {
|
24
|
+
this.lastframe = this.firstFrame;
|
25
|
+
this.firstFrame = Number(libsdl.symbols.SDL_GetPerformanceCounter());
|
26
|
+
const deltaTime =
|
27
|
+
((this.firstFrame - this.lastframe) * 1000) /
|
28
|
+
Number(libsdl.symbols.SDL_GetPerformanceFrequency());
|
29
|
+
|
30
|
+
return deltaTime;
|
31
|
+
}
|
32
|
+
}
|
33
|
+
|
34
|
+
export default Time;
|
@@ -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
|
}
|