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 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
- Slifer.Graphics.setBackground(bg);
58
+ Slifer.Graphics.setBackground(bg);
57
59
 
58
- if (Slifer.Keyboard.isPressed("escape")) {
59
- Slifer.isRunning = false;
60
- }
60
+ if (Slifer.Keyboard.isPressed("escape")) {
61
+ Slifer.isRunning = false;
62
+ }
61
63
 
62
- Slifer.Graphics.render();
64
+ Slifer.Graphics.render();
63
65
  }
64
66
 
65
67
  Slifer.quit();
Binary file
package/libs/libSDL2.so CHANGED
Binary file
Binary file
Binary file
Binary file
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "slifer",
3
- "version": "0.2.01",
3
+ "version": "0.2.2",
4
4
  "description": "A game framework made for bun and typescript",
5
5
  "module": "index.ts",
6
6
  "devDependencies": {
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 Global from "./global";
1
+ import { libsdl, libimage, libttf, libmixer } from "./ffi";
2
+ import Graphics from "./modules/graphics";
3
3
 
4
- //@ts-expect-error
5
- const fontFile = await import("./Jost-Bold.ttf");
6
-
7
- export function initLibraries(): void {
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
- const tempFont = libttf.symbols.TTF_OpenFont(
18
- Buffer.from(fontFile.default),
19
- 24
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
- if (tempFont == null) throw `Default font loading failed`;
23
- Global.ptrFont = tempFont;
26
+ export function initLibraries(): void {
27
+ initSDL();
28
+ initSDLImage();
29
+ initSDLTypeFont();
30
+ initSDLMixer();
24
31
  }