slifer 0.2.8 → 0.2.9

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "slifer",
3
- "version": "0.2.8",
3
+ "version": "0.2.9",
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,27 +1,48 @@
1
1
  import * as path from 'path';
2
+ import { parseArgs } from 'util';
2
3
 
3
4
  const libDir = path.join(__dirname, '../libs/');
4
5
 
5
6
  async function downloadLibrary(url: string, filename: string) {
6
7
  const response = await fetch(url);
7
8
  await Bun.write(libDir + filename, response);
8
-
9
-
10
9
  }
11
10
 
12
- if (process.platform == "darwin") {
13
- downloadLibrary("https://github.com/HazyVT/Slifer/releases/download/Libraries/libSDL2.dylib", "libSDL2.dylib");
14
- downloadLibrary("https://github.com/HazyVT/Slifer/releases/download/Libraries/libSDL2_image.dylib", "libSDL2_image.dylib");
15
- downloadLibrary("https://github.com/HazyVT/Slifer/releases/download/Libraries/libSDL2_mixer.dylib", "libSDL2_mixer.dylib");
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");
27
- }
11
+ function downloadFiles(check: string) {
12
+ if (check == "darwin") {
13
+ downloadLibrary("https://github.com/HazyVT/Slifer/releases/download/Libraries/libSDL2.dylib", "libSDL2.dylib");
14
+ downloadLibrary("https://github.com/HazyVT/Slifer/releases/download/Libraries/libSDL2_image.dylib", "libSDL2_image.dylib");
15
+ downloadLibrary("https://github.com/HazyVT/Slifer/releases/download/Libraries/libSDL2_mixer.dylib", "libSDL2_mixer.dylib");
16
+ downloadLibrary("https://github.com/HazyVT/Slifer/releases/download/Libraries/libSDL2_ttf.dylib", "libSDL2_ttf.dylib");
17
+ } else if (check == "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 (check == "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");
27
+ }
28
+ }
29
+
30
+ const { values } = parseArgs({
31
+ args: Bun.argv,
32
+ options: {
33
+ mode: {
34
+ type: 'string'
35
+ }
36
+ },
37
+ strict: true,
38
+ allowPositionals: true
39
+ })
40
+
41
+ if (values.mode == undefined) {
42
+ downloadFiles(process.platform);
43
+ } else {
44
+ downloadFiles(values.mode);
45
+ }
46
+
47
+
48
+
@@ -1,13 +1,15 @@
1
1
  import { type Pointer, ptr } from 'bun:ffi';
2
- import { libsdl } from '../ffi';
2
+ import { libsdl, libttf } from '../ffi';
3
3
  import Image from './image';
4
4
  import Render from './render';
5
5
  import Color from './color';
6
+ import Font from './font';
6
7
 
7
8
  /** @internal */
8
9
  export default class Canvas {
9
10
 
10
11
  private pointer : Pointer;
12
+ private destArray: Uint32Array;
11
13
 
12
14
  public readonly width;
13
15
  public readonly height;
@@ -18,18 +20,15 @@ export default class Canvas {
18
20
  this.pointer = surPointer;
19
21
  this.width = width;
20
22
  this.height = height;
23
+ this.destArray = new Uint32Array(4);
24
+ this.destArray[2] = this.width;
25
+ this.destArray[3] = this.height;
21
26
  }
22
27
 
23
28
  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
29
  const _col = ((color.r << 16) + (color.g << 8) + (color.b << 0));
31
30
 
32
- libsdl.symbols.SDL_FillRect(this.pointer, ptr(rect), _col);
31
+ libsdl.symbols.SDL_FillRect(this.pointer, ptr(this.destArray), _col);
33
32
  }
34
33
 
35
34
  clear() {
@@ -40,19 +39,52 @@ export default class Canvas {
40
39
  this.drawBG(color);
41
40
  }
42
41
 
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);
42
+ draw(drawable: Image | Canvas, x: number, y: number) : void {
43
+ (drawable as any).destArray[0] = x;
44
+ (drawable as any).destArray[1] = y;
49
45
 
50
46
  libsdl.symbols.SDL_UpperBlitScaled(
51
47
  (drawable as any).pointer,
52
48
  null,
53
49
  this.pointer,
54
- ptr(destArray)
50
+ ptr((drawable as any).destArray)
55
51
  );
56
52
  }
57
53
 
54
+ public print(text: string, x: number, y: number, font: Font, color: Color) {
55
+ const wArr = new Uint32Array(1);
56
+ const hArr = new Uint32Array(1);
57
+
58
+ libttf.symbols.TTF_SizeText(
59
+ (font as any).pointer,
60
+ Buffer.from(text+"\x00"),
61
+ ptr(wArr),
62
+ ptr(hArr)
63
+ );
64
+
65
+ const _col = ((color.r << 16) + (color.g << 8) + (color.b << 0));
66
+
67
+ const surface = libttf.symbols.TTF_RenderText_Solid(
68
+ (font as any).pointer,
69
+ Buffer.from(text+'\x00'),
70
+ _col
71
+ );
72
+ if (surface == null) throw `Rendering text failed`;
73
+
74
+ const destRect = new Uint32Array(4);
75
+ destRect[0] = x;
76
+ destRect[1] = y;
77
+ destRect[2] = wArr[0];
78
+ destRect[3] = hArr[0];
79
+
80
+ libsdl.symbols.SDL_UpperBlitScaled(
81
+ surface,
82
+ null,
83
+ this.pointer,
84
+ ptr(destRect)
85
+ );
86
+
87
+ }
88
+
89
+
58
90
  }
@@ -6,18 +6,40 @@ import Render from "./render";
6
6
  export default class Image {
7
7
 
8
8
  private pointer: Pointer;
9
+ private destArray: Uint32Array;
9
10
 
10
11
  public readonly width: number;
11
12
  public readonly height: number;
12
13
 
13
14
  constructor(path: string) {
14
15
  const cs = Buffer.from(path+'\x00');
15
- //@ts-expect-error
16
16
  const surface = libimage.symbols.IMG_Load(cs);
17
17
  if (surface == null) throw `Loading ${path} failed`;
18
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);
19
+
20
+
21
+ // Convert to texture
22
+ const texture = libsdl.symbols.SDL_CreateTextureFromSurface(
23
+ Render.pointer,
24
+ this.pointer
25
+ )
26
+ if (texture == null) throw `Loading ${path} failed`;
27
+
28
+ const wArr = new Uint32Array(1);
29
+ const hArr = new Uint32Array(1);
30
+ libsdl.symbols.SDL_QueryTexture(
31
+ texture,
32
+ null,
33
+ null,
34
+ ptr(wArr),
35
+ ptr(hArr)
36
+ )
37
+
38
+ this.width = wArr[0];
39
+ this.height = hArr[0];
40
+
41
+ this.destArray = new Uint32Array(4);
42
+ this.destArray[2] = this.width;
43
+ this.destArray[3] = this.height;
22
44
  }
23
45
  }
@@ -7,13 +7,19 @@ export default class Render {
7
7
 
8
8
  public static pointer : Pointer;
9
9
  public static surface: Pointer;
10
+ public static destArray : Uint32Array;
10
11
 
11
12
  public static createRenderer(width: number, height: number) {
12
- const renPointer = libsdl.symbols.SDL_CreateRenderer(Window.pointer, -1, 0);
13
+ const renPointer = libsdl.symbols.SDL_CreateRenderer(Window.pointer, -1, 0x00000004 + 0x00000002);
13
14
  if (renPointer == null) throw `Renderer creation failed.`;
14
15
  this.pointer = renPointer;
15
16
  const surPointer = libsdl.symbols.SDL_CreateRGBSurface(0, width, height, 32, 0, 0, 0, 0)
16
17
  if (surPointer == null) throw `Surface creation failed`;
17
18
  this.surface = surPointer;
19
+ this.destArray = new Uint32Array(4);
20
+ this.destArray[0] = 0;
21
+ this.destArray[1] = 0;
22
+ this.destArray[2] = width;
23
+ this.destArray[3] = height;
18
24
  }
19
25
  }
@@ -12,19 +12,19 @@ export default class Window {
12
12
 
13
13
 
14
14
 
15
- public static createWindow(title: string, size: Vector2) : void {
15
+ public static createWindow(title: string, width: number, height: number) : void {
16
16
  const winPointer = libsdl.symbols.SDL_CreateWindow(
17
17
  Buffer.from(title + '\x00'),
18
18
  this.centerPos,
19
19
  this.centerPos,
20
- size.x,
21
- size.y,
20
+ width,
21
+ height,
22
22
  0
23
23
  );
24
24
 
25
25
  if (winPointer == null) throw `Window creation failed.`;
26
26
  this.pointer = winPointer;
27
- this.size = size;
27
+ this.size = new Vector2(width, height);
28
28
  }
29
29
 
30
30
  public setTitle(title: string): void {
@@ -49,4 +49,8 @@ export default class Window {
49
49
  public centerWindow() {
50
50
  this.setPosition(new Vector2(Window.centerPos, Window.centerPos));
51
51
  }
52
+
53
+ public setSize(width: number, height: number) {
54
+ libsdl.symbols.SDL_SetWindowSize(Window.pointer, width, height);
55
+ }
52
56
  }
package/src/ffi.ts CHANGED
@@ -27,10 +27,14 @@ if (process.platform == "win32") {
27
27
  //@ts-expect-error
28
28
  libMixerImport = await import("../libs/libSDL2_mixer.dylib");
29
29
  } else if (process.platform == "linux") {
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`);
30
+ //@ts-expect-error
31
+ libSDLImport = await import("../libs/libSDL2.so");
32
+ //@ts-expect-error
33
+ libImageImport = await import("../libs/libSDL2_image.so");
34
+ //@ts-expect-error
35
+ libTTFImport = await import("../libs/libSDL2_ttf.so");
36
+ //@ts-expect-error
37
+ libMixerImport = await import("../libs/libSDL2_mixer.so");
34
38
  }
35
39
 
36
40
  /** @internal */
@@ -242,6 +246,10 @@ export const libsdl = dlopen(libSDLImport.default, {
242
246
  SDL_MapRGB: {
243
247
  args: ['pointer', 'int', 'int', 'int'],
244
248
  returns: 'u32'
249
+ },
250
+ SDL_FreeSurface: {
251
+ args: ['pointer'],
252
+ returns: 'void'
245
253
  }
246
254
  });
247
255
 
@@ -25,28 +25,24 @@ export default class Graphics {
25
25
  libsdl.symbols.SDL_DestroyTexture(texture);
26
26
  }
27
27
 
28
- public draw(drawable: Image | Canvas, x: number, y: number, scaleX?: number, scaleY?: number) : void {
28
+ public draw(drawable: Image | Canvas, x: number, y: number) : void {
29
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);
30
+ (drawable as any).destArray[0] = x;
31
+ (drawable as any).destArray[1] = y;
32
+
35
33
 
36
34
  libsdl.symbols.SDL_UpperBlitScaled(
37
35
  (drawable as any).pointer,
38
36
  null,
39
37
  Render.surface,
40
- ptr(destArray)
38
+ ptr((drawable as any).destArray)
41
39
  );
42
40
  }
43
41
 
44
42
  public setBackground(color: Color) : void {
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);
43
+ const _col = ((color.r << 16) + (color.g << 8) + (color.b << 0));
44
+ libsdl.symbols.SDL_FillRect(Render.surface, ptr(Render.destArray), _col);
45
+
50
46
  }
51
47
 
52
48
  public drawRect(rectangle: Rectangle, color: Color) {
@@ -67,7 +63,6 @@ export default class Graphics {
67
63
 
68
64
  libttf.symbols.TTF_SizeText(
69
65
  (font as any).pointer,
70
- //@ts-expect-error
71
66
  Buffer.from(text+"\x00"),
72
67
  ptr(wArr),
73
68
  ptr(hArr)
@@ -77,7 +72,6 @@ export default class Graphics {
77
72
 
78
73
  const surface = libttf.symbols.TTF_RenderText_Solid(
79
74
  (font as any).pointer,
80
- //@ts-expect-error
81
75
  Buffer.from(text+'\x00'),
82
76
  _col
83
77
  );
@@ -89,7 +83,7 @@ export default class Graphics {
89
83
  destRect[2] = wArr[0];
90
84
  destRect[3] = hArr[0];
91
85
 
92
- libsdl.symbols.SDL_UpperBlit(
86
+ libsdl.symbols.SDL_UpperBlitScaled(
93
87
  surface,
94
88
  null,
95
89
  Render.surface,
@@ -31,13 +31,13 @@ describe("Initializing SDL Mixer ", () => {
31
31
  describe("Window Creation ", () => {
32
32
  it("Should create window without error", () => {
33
33
  expect(() =>
34
- Window.createWindow("Game", new Vector2(1, 1))
34
+ Window.createWindow("Game", 1, 1)
35
35
  ).not.toThrow(Error);
36
36
  });
37
37
  });
38
38
 
39
39
  describe("Renderer Creation ", () => {
40
40
  it("Should create renderer without error", () => {
41
- expect(() => Render.createRenderer()).not.toThrow(Error);
41
+ expect(() => Render.createRenderer(1, 1)).not.toThrow(Error);
42
42
  });
43
43
  });
package/src/slifer.ts CHANGED
@@ -30,9 +30,9 @@ class Slifer {
30
30
  this.black = new Color(0,0,0,0);
31
31
  }
32
32
 
33
- public createWindow(title: string, size: Vector2): Window {
34
- Window.createWindow(title, size);
35
- Render.createRenderer(size.x, size.y);
33
+ public createWindow(title: string, width: number, height: number): Window {
34
+ Window.createWindow(title, width, height);
35
+ Render.createRenderer(width, height);
36
36
 
37
37
  this.start = Number(libsdl.symbols.SDL_GetTicks64());
38
38
 
@@ -41,6 +41,8 @@ class Slifer {
41
41
 
42
42
  public shouldClose(): boolean {
43
43
  libsdl.symbols.SDL_RenderClear(Render.pointer);
44
+
45
+ this.Graphics.setBackground(this.black);
44
46
 
45
47
  const eventArray = new Uint16Array(32);
46
48
  const event = libsdl.symbols.SDL_PollEvent(ptr(eventArray));
@@ -78,6 +80,7 @@ class Slifer {
78
80
  }
79
81
 
80
82
  public quit(): void {
83
+ libsdl.symbols.SDL_FreeSurface(Render.surface);
81
84
  libsdl.symbols.SDL_DestroyRenderer(Render.pointer);
82
85
  libsdl.symbols.SDL_DestroyWindow(Window.pointer);
83
86
  libsdl.symbols.SDL_Quit();