slifer 0.2.3 → 0.2.5

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.
@@ -1,148 +1,78 @@
1
- import { libimage, libsdl } from "../ffi";
2
- import { type Pointer, ptr } from "bun:ffi";
3
- import { Image } from "../engine/image";
4
- import { Rectangle } from "../engine/rectangle";
5
- import Color from "../color";
6
- import { Vector2 } from "../engine/vector";
7
- import Renderer from "../engine/renderer";
1
+ import Vector2 from "../engine/vector2";
2
+ import { libsdl } from "../ffi";
3
+ import { ptr } from 'bun:ffi';
8
4
 
9
- class Graphics {
10
- 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";
11
9
 
12
- static #fontPointer: Pointer;
13
-
14
- public static get instance() {
15
- if (!Graphics.#instance) {
16
- Graphics.#instance = new Graphics();
17
- }
18
-
19
- return Graphics.#instance;
20
- }
21
- /**
22
- * Slifers draw function. Used to draw everything to the screen.
23
- */
24
- public render() {
25
- libsdl.symbols.SDL_RenderPresent(Renderer.pointer);
26
- }
27
-
28
- /**
29
- * Create a new color. All values are from 0-255
30
- *
31
- * @param r red value
32
- * @param g green value
33
- * @param b blue value
34
- * @param a alpha value
35
- * @returns Color object
36
- */
37
- public makeColor(r: number, g: number, b: number, a: number) {
38
- const _color = new Color(r, g, b, a);
39
- return _color;
40
- }
41
-
42
- /**
43
- * Sets the background of the window to a color of choice.
44
- *
45
- * Make sure this is put in the top level of the while loop
46
- * as it will clear the renderer.
47
- *
48
- * @param color Color object. Make using Slifer.Graphics.makeColor
49
- */
50
- public setBackground(color: Color) {
51
- libsdl.symbols.SDL_SetRenderDrawColor(
52
- Renderer.pointer,
53
- color.r,
54
- color.g,
55
- color.b,
56
- color.a
57
- );
58
- libsdl.symbols.SDL_RenderClear(Renderer.pointer);
59
- }
10
+ /** @internal */
11
+ export default class Graphics {
60
12
 
61
- /**
62
- * Loads a new image
63
- *
64
- * @param path string path to image
65
- * @returns Image ready to draw
66
- */
67
- public loadImage(path: string): Image {
68
- const _path = Buffer.from(path + "\x00");
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
+
49
+ }
50
+
51
+ setBackground(color: Color) : void {
52
+ libsdl.symbols.SDL_SetRenderDrawColor(Render.pointer,
53
+ color.r,
54
+ color.g,
55
+ color.b,
56
+ color.a
57
+ );
58
+ libsdl.symbols.SDL_RenderClear(Render.pointer);
133
59
  }
134
60
 
135
61
  public drawRect(rectangle: Rectangle, color: Color) {
136
62
  libsdl.symbols.SDL_SetRenderDrawColor(
137
- Renderer.pointer,
63
+ Render.pointer,
138
64
  color.r,
139
65
  color.g,
140
66
  color.b,
141
67
  color.a
142
68
  );
143
- libsdl.symbols.SDL_RenderFillRect(Renderer.pointer, rectangle.pointer);
144
- }
145
- }
146
69
 
147
- /** @internal */
148
- export default Graphics;
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;
75
+
76
+ libsdl.symbols.SDL_RenderFillRect(Render.pointer, ptr(rect));
77
+ }
78
+ }
@@ -1,77 +1,50 @@
1
1
  import { libsdl } from "../ffi";
2
2
  import { toArrayBuffer } from "bun:ffi";
3
-
4
3
  /** @internal */
5
- class Keyboard {
6
- static #instance: Keyboard;
7
-
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
- }
4
+ export default class Keyboard {
17
5
 
18
- return Keyboard.#instance;
19
- }
6
+ public static keyMap = new Map<string, number>();
20
7
 
21
8
  private static convertScancodeToKey(scancode: number): string {
22
9
  const keyFromScancode = libsdl.symbols.SDL_GetKeyFromScancode(scancode);
23
10
  const keyName = libsdl.symbols
24
11
  .SDL_GetKeyName(keyFromScancode)
25
- .toString();
12
+ .toString()
13
+ .toLocaleLowerCase();
26
14
 
27
15
  return keyName;
28
16
  }
29
17
 
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);
18
+ public static handleStates() {
19
+ const keyState = libsdl.symbols.SDL_GetKeyboardState(null);
20
+ if (keyState == null) throw `Keyboard state returned null.`;
21
+ const keyDV = new DataView(toArrayBuffer(keyState, 0, 512));
22
+ for (let kn = 0; kn < 512; kn++) {
23
+ if (keyDV.getUint8(kn) == 1) {
24
+ const keyName = this.convertScancodeToKey(kn);
25
+ const kmGet = this.keyMap.get(keyName);
26
+
39
27
  if (kmGet == undefined || kmGet == 0) {
40
- this.pressMap.set(keyName, 1);
28
+ this.keyMap.set(keyName, 1);
41
29
  } else if (kmGet == 1) {
42
- this.pressMap.set(keyName, 2);
30
+ this.keyMap.set(keyName, 2);
43
31
  }
44
- } else if (myArr.getUint8(i) == 0) {
45
- const keyName = this.convertScancodeToKey(i).toLowerCase();
46
- this.pressMap.set(keyName, 0);
32
+ } else if (keyDV.getUint8(kn) == 0) {
33
+ const keyName = this.convertScancodeToKey(kn);
34
+ this.keyMap.set(keyName, 0);
47
35
  }
48
36
  }
49
37
  }
50
38
 
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
-
39
+ public isPressed(key: keys) : boolean {
40
+ const kmGet = Keyboard.keyMap.get(key);
41
+ if (kmGet == 1) return true;
65
42
  return false;
66
43
  }
67
44
 
68
-
69
- public isDown(key: keys) {
70
- const kmGet = Keyboard.pressMap.get(key);
71
- if (kmGet == 1 || kmGet == 2) {
72
- return true;
73
- }
74
-
45
+ public isDown(key: keys) : boolean {
46
+ const kmGet = Keyboard.keyMap.get(key);
47
+ if (kmGet == 1 || kmGet == 2) return true;
75
48
  return false;
76
49
  }
77
50
  }
@@ -120,6 +93,3 @@ type keys =
120
93
  | "right shift"
121
94
  | "left ctrl"
122
95
  | "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,109 +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";
10
- import { ptr, toArrayBuffer } from "bun:ffi";
3
+ import Renderer from "./engine/render";
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();
34
+ public shouldClose() : boolean {
57
35
 
58
- // Clear the renderer
59
- Renderer.clear();
36
+ libsdl.symbols.SDL_RenderClear(Render.pointer);
60
37
 
61
- // Calculate delta time
62
- // this.dt = Time.instance.calcDelta();
63
-
64
- // Poll Events
65
38
  const eventArray = new Uint16Array(32);
66
- const isEvent = libsdl.symbols.SDL_PollEvent(ptr(eventArray));
67
-
68
- if (isEvent) {
39
+ const event = libsdl.symbols.SDL_PollEvent(ptr(eventArray));
40
+ if (event) {
69
41
  switch (eventArray[0]) {
70
- // Quit event
71
42
  case 256:
72
43
  this.isRunning = false;
73
44
  break;
74
- // Mouse down event
75
- case 1025:
76
- const _dbtn = eventArray[8] - 256;
77
- Mouse.setButtonDown(_dbtn);
78
- break;
79
- // Mouse up event
80
45
  case 1026:
81
- const _ubtn = eventArray[8];
82
- Mouse.setButtonUp(_ubtn);
46
+ Mouse.onRelease(eventArray[8]);
83
47
  break;
84
48
  }
85
49
  }
86
50
 
87
- Keyboard.getStates();
88
-
89
- const frameTicks = this.capTimer.getTicks();
90
- if (frameTicks < this.ticksPerFrame) {
91
- libsdl.symbols.SDL_Delay(this.ticksPerFrame - frameTicks);
92
- }
51
+ Keyboard.handleStates();
93
52
 
53
+ Mouse.handleState();
54
+
55
+
94
56
  return !this.isRunning;
95
57
  }
96
58
 
97
- /**
98
- * Slifers quit method
99
- */
100
- quit() {
101
- libsdl.symbols.SDL_DestroyRenderer(Renderer.pointer);
102
- libsdl.symbols.SDL_DestroyWindow(Window.pointer);
103
- libsdl.symbols.SDL_Quit();
104
- }
105
-
106
- getVersion() {
107
- return version;
59
+ public getVersion() : string {
60
+ return 'v' + version;
108
61
  }
109
62
  }
63
+
64
+
65
+ export default Slifer;
package/Logo.png DELETED
Binary file
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
package/logo-alpha.png DELETED
Binary file