slifer 0.2.6 → 0.2.7

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/bun.lockb CHANGED
Binary file
package/index.ts CHANGED
@@ -3,6 +3,8 @@ import Image from "./src/engine/image";
3
3
  import Vector2 from "./src/engine/vector2";
4
4
  import Color from "./src/engine/color";
5
5
  import Audio from "./src/engine/audio";
6
+ import Rectangle from "./src/engine/rectangle";
7
+ import Font from "./src/engine/font";
6
8
 
7
9
  const slf = new Slifer();
8
- export { Vector2, Color, Image, Audio, slf as Slifer };
10
+ export { Vector2, Color, Image, Audio, Rectangle, Font, slf as Slifer };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "slifer",
3
- "version": "0.2.6",
3
+ "version": "0.2.7",
4
4
  "description": "A game framework made for bun and typescript",
5
5
  "module": "index.ts",
6
6
  "devDependencies": {
package/src/download.ts CHANGED
@@ -1,5 +1,3 @@
1
- import * as https from 'https';
2
- import * as fs from 'fs';
3
1
  import * as path from 'path';
4
2
 
5
3
  const libDir = path.join(__dirname, '../libs/');
@@ -13,11 +11,17 @@ async function downloadLibrary(url: string, filename: string) {
13
11
 
14
12
  if (process.platform == "darwin") {
15
13
  downloadLibrary("https://github.com/HazyVT/Slifer/releases/download/Libraries/libSDL2.dylib", "libSDL2.dylib");
16
-
17
-
18
14
  downloadLibrary("https://github.com/HazyVT/Slifer/releases/download/Libraries/libSDL2_image.dylib", "libSDL2_image.dylib");
19
-
20
15
  downloadLibrary("https://github.com/HazyVT/Slifer/releases/download/Libraries/libSDL2_mixer.dylib", "libSDL2_mixer.dylib");
21
-
22
16
  downloadLibrary("https://github.com/HazyVT/Slifer/releases/download/Libraries/libSDL2_ttf.dylib", "libSDL2_ttf.dylib");
17
+ } else if (process.platform == "win32") {
18
+ downloadLibrary("https://github.com/HazyVT/Slifer/releases/download/Libraries/SDL2.dll", "SDL2.dll");
19
+ downloadLibrary("https://github.com/HazyVT/Slifer/releases/download/Libraries/SDL2_image.dll", "SDL2_image.dll");
20
+ downloadLibrary("https://github.com/HazyVT/Slifer/releases/download/Libraries/SDL2_mixer.dll", "SDL2_mixer.dll");
21
+ downloadLibrary("https://github.com/HazyVT/Slifer/releases/download/Libraries/SDL2_ttf.dll", "SDL2_ttf.dll");
22
+ } else if (process.platform == "linux") {
23
+ downloadLibrary("https://github.com/HazyVT/Slifer/releases/download/Libraries/libSDL2.so", "libSDL2.so");
24
+ downloadLibrary("https://github.com/HazyVT/Slifer/releases/download/Libraries/libSDL2_image.so", "libSDL2_image.so");
25
+ downloadLibrary("https://github.com/HazyVT/Slifer/releases/download/Libraries/libSDL2_mixer.so", "libSDL2_mixer.so");
26
+ downloadLibrary("https://github.com/HazyVT/Slifer/releases/download/Libraries/libSDL2_ttf.so", "libSDL2_ttf.so");
23
27
  }
@@ -0,0 +1,16 @@
1
+ import { type Pointer } from 'bun:ffi';
2
+ import { libttf } from '../ffi';
3
+
4
+ export default class Font {
5
+ private pointer : Pointer;
6
+
7
+ constructor(path: string, size: number) {
8
+ const fp = libttf.symbols.TTF_OpenFont(Buffer.from(path+"\x00"), size);
9
+ if (fp == null) throw `Font failed to be opened`;
10
+ this.pointer = fp;
11
+ }
12
+
13
+ public destroy() {
14
+ libttf.symbols.TTF_CloseFont(this.pointer);
15
+ }
16
+ }
@@ -1,4 +1,4 @@
1
- import type Vector2 from "./vector2";
1
+ import Vector2 from "./vector2";
2
2
  import { ptr, type Pointer } from 'bun:ffi';
3
3
 
4
4
  /** @internal */
@@ -12,6 +12,10 @@ export default class Rectangle {
12
12
  this.size = size;
13
13
  }
14
14
 
15
+ static empty() : Rectangle {
16
+ return new Rectangle(new Vector2(0, 0), new Vector2(0, 0));
17
+ }
18
+
15
19
  public isColliding(rectangle: Rectangle) : boolean {
16
20
  if (
17
21
  this.position.x < rectangle.position.x + rectangle.size.x &&
@@ -7,4 +7,20 @@ export default class Vector2 {
7
7
  this.x = x;
8
8
  this.y = y;
9
9
  }
10
+
11
+ static one() : Vector2 {
12
+ return new Vector2(1, 1);
13
+ }
14
+
15
+ static unitX() : Vector2 {
16
+ return new Vector2(1, 0);
17
+ }
18
+
19
+ static unitY() : Vector2 {
20
+ return new Vector2(0, 1);
21
+ }
22
+
23
+ static zero() : Vector2 {
24
+ return new Vector2(0, 0);
25
+ }
10
26
  }
package/src/ffi.ts CHANGED
@@ -1,19 +1,22 @@
1
1
  import { dlopen, FFIType, suffix } from "bun:ffi";
2
+ import * as path from "path";
2
3
 
3
4
  let libSDLImport;
4
5
  let libImageImport;
5
6
  let libTTFImport;
6
7
  let libMixerImport;
7
8
 
9
+ const libDir = path.join(__dirname, "../libs/");
10
+
8
11
  if (process.platform == "win32") {
9
12
  //@ts-expect-error
10
- libSDLImport = await import("../libs/libSDL2.dll");
13
+ libSDLImport = await import("../libs/SDL2.dll");
11
14
  //@ts-expect-error
12
- libImageImport = await import("../libs/libSDL2_image.dll");
15
+ libImageImport = await import("../libs/SDL2_image.dll");
13
16
  //@ts-expect-error
14
- libTTFImport = await import("../libs/libSDL2_ttf.dll");
17
+ libTTFImport = await import("../libs/SDL2_ttf.dll");
15
18
  //@ts-expect-error
16
- libMixerImport = await import("../libs/libSDL2_mixer.dll");
19
+ libMixerImport = await import("../libs/SDL2_mixer.dll");
17
20
  } else if (process.platform == "darwin") {
18
21
  //@ts-expect-error
19
22
  libSDLImport = await import("../libs/libSDL2.dylib");
@@ -24,14 +27,10 @@ if (process.platform == "win32") {
24
27
  //@ts-expect-error
25
28
  libMixerImport = await import("../libs/libSDL2_mixer.dylib");
26
29
  } else if (process.platform == "linux") {
27
- //@ts-expect-error
28
- libSDLImport = await import("../libs/libSDL2.so");
29
- //@ts-expect-error
30
- libImageImport = await import("../libs/libSDL2_image.so");
31
- //@ts-expect-error
32
- libTTFImport = await import("../libs/libSDL2_ttf.so");
33
- //@ts-expect-error
34
- libMixerImport = await import("../libs/libSDL2_mixer.so");
30
+ libSDLImport = await import(`${libDir}libSDL2.so`);
31
+ libImageImport = await import(`${libDir}libSDL2_image.so`);
32
+ libTTFImport = await import(`${libDir}libSDL2_ttf.so`);
33
+ libMixerImport = await import(`${libDir}libSDL2_mixer.so`);
35
34
  }
36
35
 
37
36
  /** @internal */
@@ -220,6 +219,9 @@ export const libsdl = dlopen(libSDLImport.default, {
220
219
  SDL_GetScancodeFromName: {
221
220
  args: ["cstring"],
222
221
  returns: "int",
222
+ },
223
+ SDL_GetTicks64: {
224
+ returns: 'u64'
223
225
  }
224
226
  });
225
227
 
@@ -234,9 +236,9 @@ export const libimage = dlopen(libImageImport.default, {
234
236
  returns: "pointer",
235
237
  },
236
238
  IMG_LoadTexture: {
237
- args: ['pointer', 'cstring'],
238
- returns: 'pointer'
239
- }
239
+ args: ["pointer", "cstring"],
240
+ returns: "pointer",
241
+ },
240
242
  });
241
243
 
242
244
  /** @internal */
@@ -1,7 +1,7 @@
1
1
  import Vector2 from "../engine/vector2";
2
- import { libsdl } from "../ffi";
2
+ import { libsdl, libttf } from "../ffi";
3
3
  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
7
  import type Color from "../engine/color";
@@ -10,14 +10,14 @@ import type Rectangle from "../engine/rectangle";
10
10
  /** @internal */
11
11
  export default class Graphics {
12
12
 
13
- render() : void {
13
+ public render() : void {
14
14
  libsdl.symbols.SDL_RenderPresent(Render.pointer);
15
15
  }
16
16
 
17
- draw(image: Image, position: Vector2) : void {
17
+ public draw(image: Image, x: number, y: number) : void {
18
18
 
19
- (image as any).destArr[0] = position.x;
20
- (image as any).destArr[1] = position.y;
19
+ (image as any).destArr[0] = x;
20
+ (image as any).destArr[1] = y;
21
21
 
22
22
  libsdl.symbols.SDL_RenderCopy(
23
23
  Render.pointer,
@@ -27,13 +27,13 @@ export default class Graphics {
27
27
  );
28
28
  }
29
29
 
30
- drawEx(image: Image, position: Vector2, rotation?: number, scale?: Vector2, flipH?: boolean) {
30
+ public drawEx(image: Image, x: number, y: number, rotation?: number, scaleX?: number, scaleY?: number, flipH?: boolean) {
31
31
 
32
32
  const destArr = (image as any).destArr;
33
- destArr[0] = position.x;
34
- destArr[1] = position.y;
35
- destArr[2] = image.width * (scale ? scale.x : 1);
36
- destArr[3] = image.height * (scale ? scale.y : 1);
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
37
 
38
38
  libsdl.symbols.SDL_RenderCopyEx(
39
39
  Render.pointer,
@@ -48,7 +48,7 @@ export default class Graphics {
48
48
 
49
49
  }
50
50
 
51
- setBackground(color: Color) : void {
51
+ public setBackground(color: Color) : void {
52
52
  libsdl.symbols.SDL_SetRenderDrawColor(Render.pointer,
53
53
  color.r,
54
54
  color.g,
@@ -69,10 +69,53 @@ export default class Graphics {
69
69
 
70
70
  const rect = new Uint32Array(4);
71
71
  rect[0] = rectangle.position.x;
72
- rect[1] = rectangle.position.y;
72
+ rect[1] = rectangle.position.y;
73
73
  rect[2] = rectangle.size.x;
74
74
  rect[3] = rectangle.size.y;
75
75
 
76
76
  libsdl.symbols.SDL_RenderFillRect(Render.pointer, ptr(rect));
77
77
  }
78
- }
78
+
79
+ public print(text: string, x: number, y: number, font: Font, color: Color) {
80
+ const wArr = new Uint32Array(1);
81
+ const hArr = new Uint32Array(1);
82
+
83
+ libttf.symbols.TTF_SizeText(
84
+ (font as any).pointer,
85
+ Buffer.from(text+"\x00"),
86
+ ptr(wArr),
87
+ ptr(hArr)
88
+ );
89
+
90
+ const _col = ((color.r << 0) + (color.g << 8) + (color.b << 16));
91
+
92
+ const surface = libttf.symbols.TTF_RenderText_Solid(
93
+ (font as any).pointer,
94
+ Buffer.from(text+'\x00'),
95
+ _col
96
+ );
97
+ if (surface == null) throw `Rendering text failed`;
98
+
99
+ const texture = libsdl.symbols.SDL_CreateTextureFromSurface(
100
+ Render.pointer,
101
+ surface
102
+ );
103
+ if (texture == null) throw `Texture Creation failed`;
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
+
120
+ }
121
+ }
@@ -4,18 +4,24 @@ import Vector2 from "../engine/vector2";
4
4
 
5
5
  /** @internal */
6
6
  class Mouse {
7
-
8
- private static x : number;
9
- private static y : number;
7
+ private static x: number;
8
+ private static y: number;
10
9
 
11
10
  private static buttonMap = new Map<number, number>();
12
-
11
+
13
12
  static onRelease(button: number) {
14
13
  this.buttonMap.set(button, 0);
15
14
  }
16
15
 
16
+ static onMove(x: number, y: number) {
17
+ this.x = x;
18
+ this.y = y;
19
+ }
20
+
17
21
  static handleState() {
18
- const down = libsdl.symbols.SDL_GetMouseState();
22
+ const xArr = new Uint32Array(1);
23
+ const yArr = new Uint32Array(1);
24
+ const down = libsdl.symbols.SDL_GetMouseState(ptr(xArr), ptr(yArr));
19
25
  const bmGet = this.buttonMap.get(down);
20
26
 
21
27
  if (bmGet == undefined || bmGet == 0) {
@@ -37,27 +43,31 @@ class Mouse {
37
43
  return false;
38
44
  }
39
45
 
40
- isPressed(button: buttons) : boolean {
46
+ isPressed(button: buttons): boolean {
41
47
  switch (button) {
42
- case 'left':
48
+ case "left":
43
49
  return Mouse.getPressMap(1);
44
- case 'middle':
50
+ case "middle":
45
51
  return Mouse.getPressMap(2);
46
- case 'right':
52
+ case "right":
47
53
  return Mouse.getPressMap(3);
48
54
  }
49
55
  }
50
56
 
51
- isDown(button: buttons) : boolean {
57
+ isDown(button: buttons): boolean {
52
58
  switch (button) {
53
- case 'left':
59
+ case "left":
54
60
  return Mouse.getDownMap(1);
55
- case 'middle':
61
+ case "middle":
56
62
  return Mouse.getDownMap(2);
57
- case 'right':
63
+ case "right":
58
64
  return Mouse.getDownMap(3);
59
65
  }
60
66
  }
67
+
68
+ getPosition(): Vector2 {
69
+ return new Vector2(Mouse.x, Mouse.y);
70
+ }
61
71
  }
62
72
 
63
73
  type buttons = "left" | "middle" | "right";
package/src/slifer.ts CHANGED
@@ -11,28 +11,33 @@ import Mouse from "./modules/mouse";
11
11
  import Render from "./engine/render";
12
12
 
13
13
  class Slifer {
14
-
15
14
  public isRunning: boolean;
15
+ public deltaTime: number;
16
16
 
17
17
  // Modules
18
18
  public Graphics = new Graphics();
19
19
  public Keyboard = new Keyboard();
20
20
  public Mouse = new Mouse();
21
21
 
22
+ private start = 0;
23
+ private end = 0;
24
+
22
25
  constructor() {
23
26
  initLibraries();
24
27
  this.isRunning = true;
28
+ this.deltaTime = 0;
25
29
  }
26
30
 
27
- public createWindow(title: string, size: Vector2) : Window {
31
+ public createWindow(title: string, size: Vector2): Window {
28
32
  Window.createWindow(title, size);
29
33
  Renderer.createRenderer();
30
34
 
35
+ this.start = Number(libsdl.symbols.SDL_GetTicks64());
36
+
31
37
  return new Window();
32
38
  }
33
39
 
34
- public shouldClose() : boolean {
35
-
40
+ public shouldClose(): boolean {
36
41
  libsdl.symbols.SDL_RenderClear(Render.pointer);
37
42
 
38
43
  const eventArray = new Uint16Array(32);
@@ -42,24 +47,39 @@ class Slifer {
42
47
  case 256:
43
48
  this.isRunning = false;
44
49
  break;
50
+ case 1024:
51
+ Mouse.onMove(eventArray[10], eventArray[12]);
52
+ break;
45
53
  case 1026:
46
54
  Mouse.onRelease(eventArray[8]);
47
55
  break;
48
56
  }
49
57
  }
50
58
 
59
+
51
60
  Keyboard.handleStates();
52
61
 
53
62
  Mouse.handleState();
63
+
64
+ this.end = Number(libsdl.symbols.SDL_GetTicks64());
65
+
66
+ this.deltaTime = (this.end - this.start) / 1000;
67
+
68
+ this.start = this.end;
54
69
 
55
-
70
+
56
71
  return !this.isRunning;
57
72
  }
58
73
 
59
- public getVersion() : string {
60
- return 'v' + version;
74
+ public getVersion(): string {
75
+ return "v" + version;
61
76
  }
62
- }
63
77
 
78
+ public quit(): void {
79
+ libsdl.symbols.SDL_DestroyRenderer(Render.pointer);
80
+ libsdl.symbols.SDL_DestroyWindow(Window.pointer);
81
+ libsdl.symbols.SDL_Quit();
82
+ }
83
+ }
64
84
 
65
85
  export default Slifer;
package/src/Jost-Bold.ttf DELETED
Binary file