slifer 0.2.4 → 0.2.6

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/.npmignore CHANGED
@@ -135,3 +135,4 @@ test.ts
135
135
  char.png
136
136
 
137
137
  .wakatime-project
138
+ libs/
package/index.ts CHANGED
@@ -1,10 +1,8 @@
1
- import { SliferClass } from "./src/slifer";
1
+ import Slifer from "./src/slifer";
2
+ import Image from "./src/engine/image";
3
+ import Vector2 from "./src/engine/vector2";
4
+ import Color from "./src/engine/color";
5
+ import Audio from "./src/engine/audio";
2
6
 
3
- export type { ImageType as Image } from "./src/modules/graphics";
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
-
9
- const Slifer = new SliferClass();
10
- export default Slifer;
7
+ const slf = new Slifer();
8
+ export { Vector2, Color, Image, Audio, slf as Slifer };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "slifer",
3
- "version": "0.2.4",
3
+ "version": "0.2.6",
4
4
  "description": "A game framework made for bun and typescript",
5
5
  "module": "index.ts",
6
6
  "devDependencies": {
@@ -9,5 +9,9 @@
9
9
  "peerDependencies": {
10
10
  "typescript": "^5.0.0"
11
11
  },
12
- "type": "module"
12
+ "type": "module",
13
+ "scripts": {
14
+ "postinstall": "bun run src/download.ts"
15
+ },
16
+ "exports": "./index.ts"
13
17
  }
@@ -0,0 +1,23 @@
1
+ import * as https from 'https';
2
+ import * as fs from 'fs';
3
+ import * as path from 'path';
4
+
5
+ const libDir = path.join(__dirname, '../libs/');
6
+
7
+ async function downloadLibrary(url: string, filename: string) {
8
+ const response = await fetch(url);
9
+ await Bun.write(libDir + filename, response);
10
+
11
+
12
+ }
13
+
14
+ if (process.platform == "darwin") {
15
+ downloadLibrary("https://github.com/HazyVT/Slifer/releases/download/Libraries/libSDL2.dylib", "libSDL2.dylib");
16
+
17
+
18
+ downloadLibrary("https://github.com/HazyVT/Slifer/releases/download/Libraries/libSDL2_image.dylib", "libSDL2_image.dylib");
19
+
20
+ downloadLibrary("https://github.com/HazyVT/Slifer/releases/download/Libraries/libSDL2_mixer.dylib", "libSDL2_mixer.dylib");
21
+
22
+ downloadLibrary("https://github.com/HazyVT/Slifer/releases/download/Libraries/libSDL2_ttf.dylib", "libSDL2_ttf.dylib");
23
+ }
@@ -1,27 +1,12 @@
1
- import { ptr } from "bun:ffi";
2
1
  import { libmixer } from "../ffi";
3
2
 
4
- class Audio {
5
- static #instance: Audio;
6
-
7
- private constructor() {}
8
-
9
- public static get instance() {
10
- if (!Audio.#instance) Audio.#instance = new Audio();
11
-
12
- return Audio.#instance;
13
- }
14
-
15
- public loadAudio(path: string): AudioSource {
16
- return new AudioSource(path);
17
- }
18
- }
19
-
20
- export class AudioSource {
21
- public readonly pointer;
3
+ /** @internal */
4
+ export default class Audio {
5
+ private readonly pointer;
22
6
 
23
7
  constructor(path: string) {
24
8
  const audiomix = libmixer.symbols.Mix_LoadWAV(
9
+ //@ts-expect-error Buffer error
25
10
  Buffer.from(path + "\x00")
26
11
  );
27
12
 
@@ -36,6 +21,4 @@ export class AudioSource {
36
21
  public destroy() {
37
22
  libmixer.symbols.Mix_FreeChunk(this.pointer);
38
23
  }
39
- }
40
-
41
- export default Audio;
24
+ }
@@ -1,4 +1,5 @@
1
- class Color {
1
+ /** @internal */
2
+ export default class Color {
2
3
  readonly r;
3
4
  readonly g;
4
5
  readonly b;
@@ -11,5 +12,3 @@ class Color {
11
12
  this.a = a;
12
13
  }
13
14
  }
14
-
15
- export default Color;
@@ -0,0 +1,34 @@
1
+ import { libimage, libsdl } from "../ffi";
2
+ import { type Pointer, ptr } from 'bun:ffi';
3
+ import Render from "./render";
4
+
5
+ /** @internal */
6
+ export default class Image {
7
+
8
+ private pointer: Pointer;
9
+ private destArr: Uint32Array;
10
+
11
+ public readonly width;
12
+ public readonly height;
13
+
14
+ constructor(path: string) {
15
+ const cs = Buffer.from(path+'\x00');
16
+ //@ts-expect-error Buffer error;
17
+ const texture = libimage.symbols.IMG_LoadTexture(Render.pointer, cs);
18
+ if (texture == null) throw `Texture creation failed.`;
19
+ this.pointer = texture;
20
+
21
+ const wArr = new Uint32Array(1);
22
+ const hArr = new Uint32Array(1);
23
+ libsdl.symbols.SDL_QueryTexture(texture, null, null, ptr(wArr), ptr(hArr));
24
+
25
+ this.width = wArr[0];
26
+ this.height = hArr[0];
27
+
28
+ this.destArr = new Uint32Array(4);
29
+ this.destArr[0] = 0;
30
+ this.destArr[1] = 0;
31
+ this.destArr[2] = this.width;
32
+ this.destArr[3] = this.height;
33
+ }
34
+ }
@@ -1,26 +1,17 @@
1
- import { ptr } from "bun:ffi";
2
- import { Vector2 } from "./vector";
1
+ import type Vector2 from "./vector2";
2
+ import { ptr, type Pointer } from 'bun:ffi';
3
3
 
4
- export class Rectangle {
5
- public readonly pointer;
6
- public position: Vector2;
7
- public size: Vector2;
4
+ /** @internal */
5
+ export default class Rectangle {
6
+
7
+ public readonly position;
8
+ public readonly size;
8
9
 
9
10
  constructor(position: Vector2, size: Vector2) {
10
- const arr = new Uint32Array(4);
11
- arr[0] = position.x;
12
- arr[1] = position.y;
13
- arr[2] = size.x;
14
- arr[3] = size.y;
15
- this.pointer = ptr(arr);
16
11
  this.position = position;
17
12
  this.size = size;
18
13
  }
19
14
 
20
- static empty() {
21
- return new Rectangle(new Vector2(0, 0), new Vector2(0, 0));
22
- }
23
-
24
15
  public isColliding(rectangle: Rectangle) : boolean {
25
16
  if (
26
17
  this.position.x < rectangle.position.x + rectangle.size.x &&
@@ -33,5 +24,4 @@ export class Rectangle {
33
24
 
34
25
  return false;
35
26
  }
36
- }
37
-
27
+ }
@@ -0,0 +1,15 @@
1
+ import { type Pointer } from 'bun:ffi';
2
+ import { libsdl } from "../ffi";
3
+ import Window from "./window";
4
+
5
+ /** @internal */
6
+ export default class Render {
7
+
8
+ public static pointer : Pointer;
9
+
10
+ public static createRenderer() {
11
+ const renPointer = libsdl.symbols.SDL_CreateRenderer(Window.pointer, -1, 0);
12
+ if (renPointer == null) throw `Renderer creation failed.`;
13
+ this.pointer = renPointer;
14
+ }
15
+ }
@@ -0,0 +1,10 @@
1
+ /** @internal */
2
+ export default class Vector2 {
3
+ public x;
4
+ public y;
5
+
6
+ constructor(x: number, y: number) {
7
+ this.x = x;
8
+ this.y = y;
9
+ }
10
+ }
@@ -1,60 +1,32 @@
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,
1
+ import { type Pointer } from 'bun:ffi';
2
+ import Vector2 from './vector2';
3
+ import { libsdl } from '../ffi';
4
+
5
+ /** @internal */
6
+ export default class Window {
7
+ public static pointer: Pointer;
8
+
9
+ private static centerPos = 0x2fff0000;
10
+
11
+ public static createWindow(title: string, size: Vector2) : void {
12
+ const winPointer = libsdl.symbols.SDL_CreateWindow(
13
+ //@ts-expect-error Buffer error
14
+ Buffer.from(title + '\x00'),
15
+ this.centerPos,
16
+ this.centerPos,
17
+ size.x,
18
+ size.y,
42
19
  0
43
- );
44
-
45
- if (_winPointer == null) throw `Window Creation Failed`;
46
- Window.#pointer = _winPointer;
47
- }
20
+ )
48
21
 
49
- public setSize(size: Vector2): void {
50
- this.size = size;
51
- libsdl.symbols.SDL_SetWindowSize(Window.pointer, size.x, size.y);
22
+ if (winPointer == null) throw `Window creation failed.`;
23
+ this.pointer = winPointer;
52
24
  }
53
25
 
54
26
  public setTitle(title: string): void {
55
- this.title = title;
56
27
  libsdl.symbols.SDL_SetWindowTitle(
57
28
  Window.pointer,
29
+ //@ts-expect-error Buffer error
58
30
  Buffer.from(title + "\x00")
59
31
  );
60
32
  }
@@ -74,6 +46,4 @@ class Window {
74
46
  public centerWindow() {
75
47
  this.setPosition(new Vector2(Window.centerPos, Window.centerPos));
76
48
  }
77
- }
78
-
79
- export default Window;
49
+ }
package/src/engine.ts CHANGED
@@ -1,5 +1,4 @@
1
1
  import { libsdl, libimage, libttf, libmixer } from "./ffi";
2
- import Graphics from "./modules/graphics";
3
2
 
4
3
  export function initSDL() {
5
4
  const initVideo = 0x00000020;
package/src/ffi.ts CHANGED
@@ -220,7 +220,7 @@ export const libsdl = dlopen(libSDLImport.default, {
220
220
  SDL_GetScancodeFromName: {
221
221
  args: ["cstring"],
222
222
  returns: "int",
223
- },
223
+ }
224
224
  });
225
225
 
226
226
  /** @internal */
@@ -233,6 +233,10 @@ export const libimage = dlopen(libImageImport.default, {
233
233
  args: ["cstring"],
234
234
  returns: "pointer",
235
235
  },
236
+ IMG_LoadTexture: {
237
+ args: ['pointer', 'cstring'],
238
+ returns: 'pointer'
239
+ }
236
240
  });
237
241
 
238
242
  /** @internal */
@@ -1,173 +1,78 @@
1
- import { libimage, libsdl } from "../ffi";
2
- import { type Pointer, ptr } from "bun:ffi";
3
- import { Rectangle } from "../engine/rectangle";
4
- import Color from "../color";
5
- import { Vector2 } from "../engine/vector";
6
- import Renderer from "../engine/renderer";
1
+ import Vector2 from "../engine/vector2";
2
+ import { libsdl } from "../ffi";
3
+ import { ptr } from 'bun:ffi';
7
4
 
8
- class Graphics {
9
- static #instance: Graphics;
5
+ import type Image from "../engine/image";
6
+ import Render from "../engine/render";
7
+ import type Color from "../engine/color";
8
+ import type Rectangle from "../engine/rectangle";
10
9
 
11
- static #fontPointer: Pointer;
12
-
13
- public static get instance() {
14
- if (!Graphics.#instance) {
15
- Graphics.#instance = new Graphics();
16
- }
17
-
18
- return Graphics.#instance;
19
- }
20
- /**
21
- * Slifers draw function. Used to draw everything to the screen.
22
- */
23
- public render() {
24
- libsdl.symbols.SDL_RenderPresent(Renderer.pointer);
25
- }
26
-
27
- /**
28
- * Create a new color. All values are from 0-255
29
- *
30
- * @param r red value
31
- * @param g green value
32
- * @param b blue value
33
- * @param a alpha value
34
- * @returns Color object
35
- */
36
- public makeColor(r: number, g: number, b: number, a: number) {
37
- const _color = new Color(r, g, b, a);
38
- return _color;
39
- }
40
-
41
- /**
42
- * Sets the background of the window to a color of choice.
43
- *
44
- * Make sure this is put in the top level of the while loop
45
- * as it will clear the renderer.
46
- *
47
- * @param color Color object. Make using Slifer.Graphics.makeColor
48
- */
49
- public setBackground(color: Color) {
50
- libsdl.symbols.SDL_SetRenderDrawColor(
51
- Renderer.pointer,
52
- color.r,
53
- color.g,
54
- color.b,
55
- color.a
56
- );
57
- libsdl.symbols.SDL_RenderClear(Renderer.pointer);
58
- }
10
+ /** @internal */
11
+ export default class Graphics {
59
12
 
60
- /**
61
- * Loads a new image
62
- *
63
- * @param path string path to image
64
- * @returns Image ready to draw
65
- */
66
- public loadImage(path: string): Image {
67
- const _path = Buffer.from(path + "\x00");
68
- //@ts-expect-error
69
- const surface = libimage.symbols.IMG_Load(_path);
70
- if (surface == null) throw `Image failed to load`;
71
- const texture = libsdl.symbols.SDL_CreateTextureFromSurface(
72
- Renderer.pointer,
73
- surface
74
- );
75
- if (texture == null) throw `Image failed to be created`;
76
- return new Image(texture);
13
+ render() : void {
14
+ libsdl.symbols.SDL_RenderPresent(Render.pointer);
77
15
  }
16
+
17
+ draw(image: Image, position: Vector2) : void {
78
18
 
79
- /**
80
- * Method to draw to the screen simply
81
- *
82
- * @param image Image object to draw. Made using Slifer.Graphics.loadImage
83
- * @param position position to draw the image
84
- *
85
- */
86
- public draw(image: Image, position: Vector2) {
87
- const dstRect = new Uint32Array(4);
88
- dstRect[0] = position.x;
89
- dstRect[1] = position.y;
90
- dstRect[2] = image.size.x;
91
- dstRect[3] = image.size.y;
92
-
19
+ (image as any).destArr[0] = position.x;
20
+ (image as any).destArr[1] = position.y;
21
+
93
22
  libsdl.symbols.SDL_RenderCopy(
94
- Renderer.pointer,
23
+ Render.pointer,
95
24
  (image as any).pointer,
96
25
  null,
97
- dstRect
26
+ ptr((image as any).destArr)
98
27
  );
99
28
  }
100
29
 
101
- /*
102
- * Method to draw to the screen with extended arguments
103
- *
104
- * @param image Image object to draw. Made using Slifer.Graphics.loadImage
105
- * @param position Position to draw the image.
106
- * @param rotation Optional argument angle of rotation
107
- * @param scale Optional What to multiply the width and height of image by
108
- */
109
- public drawEx(
110
- image: Image,
111
- position: Vector2,
112
- rotation?: number,
113
- scale?: Vector2,
114
- flipH?: boolean
115
- ) {
116
- // Define destination rect
117
- const dstRect = new Uint32Array(4);
118
- dstRect[0] = position.x;
119
- dstRect[1] = position.y;
120
- dstRect[2] = image.size.x * (scale ? scale.x : 1);
121
- dstRect[3] = image.size.y * (scale ? scale.y : 1);
30
+ drawEx(image: Image, position: Vector2, rotation?: number, scale?: Vector2, flipH?: boolean) {
31
+
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);
122
37
 
123
- // Draw to screen
124
38
  libsdl.symbols.SDL_RenderCopyEx(
125
- Renderer.pointer,
126
- image.pointer,
39
+ Render.pointer,
40
+ (image as any).pointer,
127
41
  null,
128
- ptr(dstRect),
42
+ ptr(destArr),
129
43
  rotation ? rotation : 0,
130
44
  null,
131
- flipH ? Number(flipH) : 0
45
+ Number(flipH)
132
46
  );
47
+
48
+
133
49
  }
134
50
 
135
- public drawRect(rectangle: Rectangle, color: Color) {
136
- libsdl.symbols.SDL_SetRenderDrawColor(
137
- Renderer.pointer,
51
+ setBackground(color: Color) : void {
52
+ libsdl.symbols.SDL_SetRenderDrawColor(Render.pointer,
138
53
  color.r,
139
54
  color.g,
140
55
  color.b,
141
56
  color.a
142
57
  );
143
- libsdl.symbols.SDL_RenderFillRect(Renderer.pointer, rectangle.pointer);
58
+ libsdl.symbols.SDL_RenderClear(Render.pointer);
144
59
  }
145
- }
146
-
147
- class Image {
148
- public readonly pointer: Pointer;
149
- public readonly size: Vector2;
150
- public flipH: boolean = false;
151
-
152
- constructor(texture: Pointer) {
153
- this.pointer = texture;
154
60
 
155
- const _wArr = new Uint32Array(1);
156
- const _hArr = new Uint32Array(1);
157
-
158
- libsdl.symbols.SDL_QueryTexture(
159
- texture,
160
- null,
161
- null,
162
- ptr(_wArr),
163
- ptr(_hArr)
61
+ public drawRect(rectangle: Rectangle, color: Color) {
62
+ libsdl.symbols.SDL_SetRenderDrawColor(
63
+ Render.pointer,
64
+ color.r,
65
+ color.g,
66
+ color.b,
67
+ color.a
164
68
  );
165
69
 
166
- this.size = new Vector2(_wArr[0], _hArr[0]);
167
- }
168
- }
169
-
170
- export type ImageType = Image;
70
+ const rect = new Uint32Array(4);
71
+ rect[0] = rectangle.position.x;
72
+ rect[1] = rectangle.position.y;
73
+ rect[2] = rectangle.size.x;
74
+ rect[3] = rectangle.size.y;
171
75
 
172
- /** @internal */
173
- export default Graphics;
76
+ libsdl.symbols.SDL_RenderFillRect(Render.pointer, ptr(rect));
77
+ }
78
+ }
@@ -2,76 +2,50 @@ import { libsdl } from "../ffi";
2
2
  import { toArrayBuffer } from "bun:ffi";
3
3
 
4
4
  /** @internal */
5
- class Keyboard {
6
- static #instance: Keyboard;
5
+ export default class Keyboard {
7
6
 
8
- static keyState: DataView;
9
- static pressMap = new Map<string, number>();
10
-
11
- private constructor() {}
12
-
13
- public static get instance() {
14
- if (!Keyboard.#instance) {
15
- Keyboard.#instance = new Keyboard();
16
- }
17
-
18
- return Keyboard.#instance;
19
- }
7
+ public static keyMap = new Map<string, number>();
20
8
 
21
9
  private static convertScancodeToKey(scancode: number): string {
22
10
  const keyFromScancode = libsdl.symbols.SDL_GetKeyFromScancode(scancode);
23
11
  const keyName = libsdl.symbols
24
12
  .SDL_GetKeyName(keyFromScancode)
25
- .toString();
13
+ .toString()
14
+ .toLocaleLowerCase();
26
15
 
27
16
  return keyName;
28
17
  }
29
18
 
30
- static getStates() {
31
- const state = libsdl.symbols.SDL_GetKeyboardState(null);
32
- if (state == null) return;
33
- const myArr = new DataView(toArrayBuffer(state, 0, 512));
34
-
35
- for (let i = 0; i < 512; ++i) {
36
- if (myArr.getUint8(i) == 1) {
37
- const keyName = this.convertScancodeToKey(i).toLowerCase();
38
- const kmGet = this.pressMap.get(keyName);
19
+ public static handleStates() {
20
+ const keyState = libsdl.symbols.SDL_GetKeyboardState(null);
21
+ if (keyState == null) throw `Keyboard state returned null.`;
22
+ const keyDV = new DataView(toArrayBuffer(keyState, 0, 512));
23
+ for (let kn = 0; kn < 512; kn++) {
24
+ if (keyDV.getUint8(kn) == 1) {
25
+ const keyName = this.convertScancodeToKey(kn);
26
+ const kmGet = this.keyMap.get(keyName);
27
+
39
28
  if (kmGet == undefined || kmGet == 0) {
40
- this.pressMap.set(keyName, 1);
29
+ this.keyMap.set(keyName, 1);
41
30
  } else if (kmGet == 1) {
42
- this.pressMap.set(keyName, 2);
31
+ this.keyMap.set(keyName, 2);
43
32
  }
44
- } else if (myArr.getUint8(i) == 0) {
45
- const keyName = this.convertScancodeToKey(i).toLowerCase();
46
- this.pressMap.set(keyName, 0);
33
+ } else if (keyDV.getUint8(kn) == 0) {
34
+ const keyName = this.convertScancodeToKey(kn);
35
+ this.keyMap.set(keyName, 0);
47
36
  }
48
37
  }
49
38
  }
50
39
 
51
- public isPressed(key: keys) {
52
- /*
53
- const scancode = libsdl.symbols.SDL_GetScancodeFromName(
54
- Buffer.from(key + "\x00")
55
- );
56
- const thisval = Keyboard.keyState.getUint8(scancode);
57
- */
58
-
59
- const kmGet = Keyboard.pressMap.get(key);
60
- if (kmGet == 1) {
61
- return true;
62
- }
63
-
64
-
40
+ public isPressed(key: keys) : boolean {
41
+ const kmGet = Keyboard.keyMap.get(key);
42
+ if (kmGet == 1) return true;
65
43
  return false;
66
44
  }
67
45
 
68
-
69
- public isDown(key: keys) {
70
- const kmGet = Keyboard.pressMap.get(key);
71
- if (kmGet == 1 || kmGet == 2) {
72
- return true;
73
- }
74
-
46
+ public isDown(key: keys) : boolean {
47
+ const kmGet = Keyboard.keyMap.get(key);
48
+ if (kmGet == 1 || kmGet == 2) return true;
75
49
  return false;
76
50
  }
77
51
  }
@@ -120,6 +94,3 @@ type keys =
120
94
  | "right shift"
121
95
  | "left ctrl"
122
96
  | "escape";
123
-
124
- /** @internal */
125
- export default Keyboard;
@@ -1,106 +1,62 @@
1
1
  import { libsdl } from "../ffi";
2
2
  import { ptr } from "bun:ffi";
3
- import {Vector2} from "../engine/vector";
3
+ import Vector2 from "../engine/vector2";
4
4
 
5
5
  /** @internal */
6
6
  class Mouse {
7
- static #instance: Mouse;
8
7
 
9
- static downKeyMap = new Map<string, boolean>();
10
- static pressedKeyMap = new Map<string, boolean>();
11
- static releasedKeyMap = new Map<string, boolean>();
8
+ private static x : number;
9
+ private static y : number;
12
10
 
13
- private constructor() {}
14
-
15
- public static get instance() {
16
- if (!Mouse.#instance) Mouse.#instance = new Mouse();
17
-
18
- return Mouse.#instance;
11
+ private static buttonMap = new Map<number, number>();
12
+
13
+ static onRelease(button: number) {
14
+ this.buttonMap.set(button, 0);
19
15
  }
20
16
 
21
- static setButtonDown(button: number) {
22
- let key: string = "";
23
- if (button == 1) {
24
- key = "left";
25
- } else if (button == 2) {
26
- key = "middle";
27
- } else {
28
- key = "right";
29
- }
30
-
31
- this.downKeyMap.set(key, true);
32
- this.releasedKeyMap.set(key, false);
33
- }
17
+ static handleState() {
18
+ const down = libsdl.symbols.SDL_GetMouseState();
19
+ const bmGet = this.buttonMap.get(down);
34
20
 
35
- static setButtonUp(button: number) {
36
- let key: string = "";
37
- if (button == 1) {
38
- key = "left";
39
- } else if (button == 2) {
40
- key = "middle";
41
- } else {
42
- key = "right";
21
+ if (bmGet == undefined || bmGet == 0) {
22
+ this.buttonMap.set(down, 1);
23
+ } else if (bmGet == 1) {
24
+ this.buttonMap.set(down, 2);
43
25
  }
44
-
45
- this.downKeyMap.set(key, false);
46
- this.pressedKeyMap.set(key, false);
47
26
  }
48
27
 
49
- /**
50
- *
51
- * @param button string of button
52
- * @returns if the button is being held down
53
- */
54
- isDown(button: buttons) {
55
- const _state = Mouse.downKeyMap.get(button);
56
- if (_state == undefined) return false;
57
-
58
- return _state;
28
+ private static getPressMap(button: number) {
29
+ const bmGet = this.buttonMap.get(button);
30
+ if (bmGet == 1) return true;
31
+ return false;
59
32
  }
60
33
 
61
- /**
62
- *
63
- * @param button string of button
64
- * @returns if button is pressed. Returns only once
65
- */
66
- isPressed(button: buttons) {
67
- const _pressedState = Mouse.pressedKeyMap.get(button);
68
- const _downState = Mouse.downKeyMap.get(button);
69
-
70
- if (_downState == true) {
71
- if (_pressedState == false || _pressedState == undefined) {
72
- Mouse.pressedKeyMap.set(button, true);
73
- return true;
74
- }
75
- }
76
-
34
+ private static getDownMap(button: number) {
35
+ const bmGet = this.buttonMap.get(button);
36
+ if (bmGet == 1 || bmGet == 2) return true;
77
37
  return false;
78
38
  }
79
39
 
80
- /**
81
- *
82
- * @param button string of button
83
- * @returns if button is released. Returns only once
84
- */
85
- isReleased(button: buttons) {
86
- const _releasedState = Mouse.releasedKeyMap.get(button);
87
- const _downState = Mouse.downKeyMap.get(button);
88
-
89
- if (_downState == false) {
90
- if (_releasedState == false || undefined) {
91
- Mouse.releasedKeyMap.set(button, true);
92
- return true;
93
- }
40
+ isPressed(button: buttons) : boolean {
41
+ switch (button) {
42
+ case 'left':
43
+ return Mouse.getPressMap(1);
44
+ case 'middle':
45
+ return Mouse.getPressMap(2);
46
+ case 'right':
47
+ return Mouse.getPressMap(3);
94
48
  }
95
-
96
- return false;
97
49
  }
98
50
 
99
- getPosition() {
100
- const xArr = new Uint32Array(1);
101
- const yArr = new Uint32Array(1);
102
- libsdl.symbols.SDL_GetMouseState(ptr(xArr), ptr(yArr));
103
- return new Vector2(xArr[0], yArr[0]);
51
+ isDown(button: buttons) : boolean {
52
+ switch (button) {
53
+ case 'left':
54
+ return Mouse.getDownMap(1);
55
+ case 'middle':
56
+ return Mouse.getDownMap(2);
57
+ case 'right':
58
+ return Mouse.getDownMap(3);
59
+ }
104
60
  }
105
61
  }
106
62
 
@@ -1,8 +1,8 @@
1
1
  import { describe, expect, it } from "bun:test";
2
2
  import { initSDL, initSDLImage, initSDLTypeFont, initSDLMixer } from "./engine";
3
- import Vector2 from "./engine/vector";
3
+ import Vector2 from "./engine/vector2";
4
+ import Render from "./engine/render";
4
5
  import Window from "./engine/window";
5
- import Renderer from "./engine/renderer";
6
6
 
7
7
  describe("Initializing SDL ", () => {
8
8
  it("Should initialize without error ", () => {
@@ -38,6 +38,6 @@ describe("Window Creation ", () => {
38
38
 
39
39
  describe("Renderer Creation ", () => {
40
40
  it("Should create renderer without error", () => {
41
- expect(() => Renderer.createRenderer()).not.toThrow(Error);
41
+ expect(() => Render.createRenderer()).not.toThrow(Error);
42
42
  });
43
43
  });
package/src/slifer.ts CHANGED
@@ -1,116 +1,65 @@
1
1
  import { libsdl } from "./ffi";
2
- import Graphics from "./modules/graphics";
3
- import Keyboard from "./modules/keyboard";
4
- import Mouse from "./modules/mouse";
5
- import Audio from "./modules/audio";
6
2
  import Window from "./engine/window";
7
- import Renderer from "./engine/renderer";
8
- import { Vector2 } from "./engine/vector";
9
- import { Timer } from "./engine/time";
3
+ import Renderer from "./engine/render";
10
4
  import { ptr } from "bun:ffi";
11
5
  import { initLibraries } from "./engine";
12
6
  import { version } from "../package.json";
7
+ import type Vector2 from "./engine/vector2";
8
+ import Graphics from "./modules/graphics";
9
+ import Keyboard from "./modules/keyboard";
10
+ import Mouse from "./modules/mouse";
11
+ import Render from "./engine/render";
13
12
 
14
- /** @interal */
15
- export class SliferClass {
16
- // Modules
17
- Graphics = Graphics.instance;
18
- Keyboard = Keyboard.instance;
19
- Mouse = Mouse.instance;
20
- Audio = Audio.instance;
21
-
22
- // Public Variables
23
- public dt: number = 0;
24
- public isRunning: boolean = true;
13
+ class Slifer {
14
+
15
+ public isRunning: boolean;
25
16
 
26
- private fps = 60;
27
- private ticksPerFrame = 1000 / this.fps;
28
- private capTimer: Timer;
17
+ // Modules
18
+ public Graphics = new Graphics();
19
+ public Keyboard = new Keyboard();
20
+ public Mouse = new Mouse();
29
21
 
30
22
  constructor() {
31
23
  initLibraries();
32
- this.capTimer = new Timer();
24
+ this.isRunning = true;
33
25
  }
34
26
 
35
- /**
36
- * @param title Title of window
37
- * @param width Width of window
38
- * @param height Height of window
39
- */
40
- createWindow(title: string, size: Vector2): Window {
41
- // Create the window
42
- const window = Window.instance;
27
+ public createWindow(title: string, size: Vector2) : Window {
43
28
  Window.createWindow(title, size);
44
-
45
- // Create the renderer
46
29
  Renderer.createRenderer();
47
30
 
48
- // Return the window object
49
- return window;
31
+ return new Window();
50
32
  }
51
33
 
52
- /**
53
- * @returns if the window should close
54
- */
55
- shouldClose(): boolean {
56
- this.capTimer.start();
57
-
58
- // Clear the renderer
59
- Renderer.clear();
34
+ public shouldClose() : boolean {
60
35
 
36
+ libsdl.symbols.SDL_RenderClear(Render.pointer);
61
37
 
62
- // Calculate delta time
63
- // this.dt = Time.instance.calcDelta();
64
-
65
- // Poll Events
66
38
  const eventArray = new Uint16Array(32);
67
- const isEvent = libsdl.symbols.SDL_PollEvent(ptr(eventArray));
68
-
69
- if (isEvent) {
39
+ const event = libsdl.symbols.SDL_PollEvent(ptr(eventArray));
40
+ if (event) {
70
41
  switch (eventArray[0]) {
71
- // Quit event
72
42
  case 256:
73
43
  this.isRunning = false;
74
44
  break;
75
- // Mouse down event
76
- case 1025:
77
- const _dbtn = eventArray[8] - 256;
78
- Mouse.setButtonDown(_dbtn);
79
- break;
80
- // Mouse up event
81
45
  case 1026:
82
- const _ubtn = eventArray[8];
83
- Mouse.setButtonUp(_ubtn);
46
+ Mouse.onRelease(eventArray[8]);
84
47
  break;
85
48
  }
86
49
  }
87
50
 
88
- Keyboard.getStates();
89
-
90
- const frameTicks = this.capTimer.getTicks();
91
- if (frameTicks < this.ticksPerFrame) {
92
- libsdl.symbols.SDL_Delay(this.ticksPerFrame - frameTicks);
93
- }
51
+ Keyboard.handleStates();
94
52
 
53
+ Mouse.handleState();
54
+
95
55
 
96
56
  return !this.isRunning;
97
57
  }
98
58
 
99
- /**
100
- * Slifers quit method
101
- */
102
- quit() {
103
- libsdl.symbols.SDL_DestroyRenderer(Renderer.pointer);
104
- libsdl.symbols.SDL_DestroyWindow(Window.pointer);
105
- libsdl.symbols.SDL_Quit();
59
+ public getVersion() : string {
60
+ return 'v' + version;
106
61
  }
62
+ }
107
63
 
108
- getVersion() {
109
- return version;
110
- }
111
64
 
112
- setFps(fps: number) {
113
- this.fps = fps;
114
- this.ticksPerFrame = 1000 / fps;
115
- }
116
- }
65
+ export default Slifer;
package/libs/libSDL2.dll DELETED
Binary file
Binary file
package/libs/libSDL2.so DELETED
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
@@ -1,40 +0,0 @@
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
- 0
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;
@@ -1,79 +0,0 @@
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
-
@@ -1,47 +0,0 @@
1
- export class Vector2 {
2
- public x;
3
- public y;
4
-
5
- constructor(x: number, y: number) {
6
- this.x = x;
7
- this.y = y;
8
- }
9
-
10
- /**
11
- * Creates a Vector2 with the components 1,1
12
- */
13
- static one() {
14
- return new Vector2(1, 1);
15
- }
16
-
17
- /**
18
- * Creates a Vector2 with the components 1,0
19
- */
20
- static unitX() {
21
- return new Vector2(1, 0);
22
- }
23
-
24
- /**
25
- * Creates a Vector2 with the components 0,1
26
- */
27
- static unitY() {
28
- return new Vector2(0, 1);
29
- }
30
-
31
- /**
32
- * Creates a vector2 with component 0,0
33
- */
34
- static zero() {
35
- return new Vector2(0, 0);
36
- }
37
-
38
- /**
39
- * Adds two vectors together
40
- *
41
- * @param vec1 Vector2 object
42
- * @param vec2 Vector2 object
43
- */
44
- static add(vec1: Vector2, vec2: Vector2) {
45
- return new Vector2(vec1.x + vec2.x, vec1.y + vec2.y);
46
- }
47
- }