slifer 0.2.1 → 0.2.3

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,17 +1,28 @@
1
- import { libimage, libsdl, libttf } from "../ffi";
2
- import Global from "../global";
1
+ import { libimage, libsdl } from "../ffi";
3
2
  import { type Pointer, ptr } from "bun:ffi";
4
- import Rectangle from "../engine/rectangle";
3
+ import { Image } from "../engine/image";
4
+ import { Rectangle } from "../engine/rectangle";
5
5
  import Color from "../color";
6
- import Vector2 from "../engine/vector";
6
+ import { Vector2 } from "../engine/vector";
7
+ import Renderer from "../engine/renderer";
7
8
 
8
- /** @internal */
9
9
  class Graphics {
10
+ static #instance: Graphics;
11
+
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
+ }
10
21
  /**
11
22
  * Slifers draw function. Used to draw everything to the screen.
12
23
  */
13
- render() {
14
- libsdl.symbols.SDL_RenderPresent(Global.ptrRenderer);
24
+ public render() {
25
+ libsdl.symbols.SDL_RenderPresent(Renderer.pointer);
15
26
  }
16
27
 
17
28
  /**
@@ -23,7 +34,7 @@ class Graphics {
23
34
  * @param a alpha value
24
35
  * @returns Color object
25
36
  */
26
- makeColor(r: number, g: number, b: number, a: number) {
37
+ public makeColor(r: number, g: number, b: number, a: number) {
27
38
  const _color = new Color(r, g, b, a);
28
39
  return _color;
29
40
  }
@@ -36,15 +47,15 @@ class Graphics {
36
47
  *
37
48
  * @param color Color object. Make using Slifer.Graphics.makeColor
38
49
  */
39
- setBackground(color: Color) {
50
+ public setBackground(color: Color) {
40
51
  libsdl.symbols.SDL_SetRenderDrawColor(
41
- Global.ptrRenderer,
52
+ Renderer.pointer,
42
53
  color.r,
43
54
  color.g,
44
55
  color.b,
45
56
  color.a
46
57
  );
47
- libsdl.symbols.SDL_RenderClear(Global.ptrRenderer);
58
+ libsdl.symbols.SDL_RenderClear(Renderer.pointer);
48
59
  }
49
60
 
50
61
  /**
@@ -53,12 +64,12 @@ class Graphics {
53
64
  * @param path string path to image
54
65
  * @returns Image ready to draw
55
66
  */
56
- loadImage(path: string): Image {
67
+ public loadImage(path: string): Image {
57
68
  const _path = Buffer.from(path + "\x00");
58
69
  const surface = libimage.symbols.IMG_Load(_path);
59
70
  if (surface == null) throw `Image failed to load`;
60
71
  const texture = libsdl.symbols.SDL_CreateTextureFromSurface(
61
- Global.ptrRenderer,
72
+ Renderer.pointer,
62
73
  surface
63
74
  );
64
75
  if (texture == null) throw `Image failed to be created`;
@@ -67,165 +78,69 @@ class Graphics {
67
78
 
68
79
  /**
69
80
  * Method to draw to the screen simply
81
+ *
70
82
  * @param image Image object to draw. Made using Slifer.Graphics.loadImage
71
83
  * @param position position to draw the image
72
84
  *
73
85
  */
74
- draw(image: Image, position: Vector2) {
86
+ public draw(image: Image, position: Vector2) {
75
87
  const dstRect = new Uint32Array(4);
76
88
  dstRect[0] = position.x;
77
89
  dstRect[1] = position.y;
78
- dstRect[2] = image.width;
79
- dstRect[3] = image.height;
90
+ dstRect[2] = image.size.x;
91
+ dstRect[3] = image.size.y;
80
92
 
81
93
  libsdl.symbols.SDL_RenderCopy(
82
- Global.ptrRenderer,
94
+ Renderer.pointer,
83
95
  (image as any).pointer,
84
96
  null,
85
97
  dstRect
86
98
  );
87
99
  }
88
100
 
89
- /**
90
- * Method to draw the image to the screen with extended arguments
101
+ /*
102
+ * Method to draw to the screen with extended arguments
91
103
  *
92
104
  * @param image Image object to draw. Made using Slifer.Graphics.loadImage
93
- * @param position position to draw the image
94
- * @param y y position to draw image
95
- * @param rotation (optional) rotation of image
96
- * @param xs (optional) scale of x axis
97
- * @param ys (optional) scale of y axis
98
- * @param flip (optional) horizontal flip
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
99
108
  */
100
- drawEx(
109
+ public drawEx(
101
110
  image: Image,
102
111
  position: Vector2,
103
- clipRectangle?: Rectangle | null,
104
112
  rotation?: number,
105
- xs?: number,
106
- ys?: number,
107
- flip?: true
113
+ scale?: Vector2,
114
+ flipH?: boolean
108
115
  ) {
109
- const _dest = new Uint32Array(4);
110
- let srcRect: null | Uint32Array = null;
111
-
112
- if (clipRectangle != null) {
113
- srcRect = new Uint32Array(4);
114
- srcRect[0] = clipRectangle.position.x;
115
- srcRect[1] = clipRectangle.position.y;
116
- srcRect[2] = clipRectangle.size.x;
117
- srcRect[3] = clipRectangle.size.y;
118
- }
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);
119
122
 
120
- _dest[0] = position.x;
121
- _dest[1] = position.y;
122
- _dest[2] = image.width * (xs ? xs : 1);
123
- _dest[3] = image.height * (ys ? ys : 1);
124
- const _center = new Uint32Array(2);
125
- _center[0] = _dest[2] / 2;
126
- _center[1] = _dest[3] / 2;
123
+ // Draw to screen
127
124
  libsdl.symbols.SDL_RenderCopyEx(
128
- Global.ptrRenderer,
129
- (image as any).pointer,
130
- srcRect,
131
- ptr(_dest),
125
+ Renderer.pointer,
126
+ image.pointer,
127
+ null,
128
+ ptr(dstRect),
132
129
  rotation ? rotation : 0,
133
- ptr(_center),
134
- flip ? Number(flip) : 0
135
- );
136
- }
137
-
138
- /**
139
- * Method to draw text to the screen
140
- *
141
- * @param text the string of text to print
142
- * @param x x position
143
- * @param y y position
144
- * @param color color of text. Made using Slifer.Graphics.makeColor.
145
- */
146
- print(text: string, x: number, y: number, color: Color) {
147
- // Create text buffer
148
- const textBuffer = Buffer.from(text + "\x00");
149
-
150
- // Get width and height of text
151
- const wArr = new Uint32Array(1);
152
- const hArr = new Uint32Array(1);
153
- libttf.symbols.TTF_SizeText(
154
- Global.ptrFont,
155
- textBuffer,
156
- ptr(wArr),
157
- ptr(hArr)
158
- );
159
-
160
- // Define color
161
- const _col = (color.r << 0) + (color.g << 8) + (color.b << 16);
162
-
163
- // Create texture
164
- const surface = libttf.symbols.TTF_RenderText_Solid(
165
- Global.ptrFont,
166
- textBuffer,
167
- _col
168
- );
169
- if (surface == null) throw `Surface creation failed on print`;
170
- const texture = libsdl.symbols.SDL_CreateTextureFromSurface(
171
- Global.ptrRenderer,
172
- surface
173
- );
174
- if (texture == null) throw `Texture creation failed on print`;
175
-
176
- // Create destination
177
- const destArr = new Uint32Array(4);
178
- destArr[0] = x;
179
- destArr[1] = y;
180
- destArr[2] = wArr[0];
181
- destArr[3] = hArr[0];
182
-
183
- // Draw text
184
- libsdl.symbols.SDL_RenderCopy(
185
- Global.ptrRenderer,
186
- texture,
187
130
  null,
188
- ptr(destArr)
131
+ flipH ? Number(flipH) : 0
189
132
  );
190
133
  }
191
134
 
192
- /**
193
- * Sets the font to a ttf file in your project
194
- *
195
- * @param path relative path to font
196
- * @param pt size of text
197
- */
198
- setFont(path: string, pt: number) {
199
- const tempFont = libttf.symbols.TTF_OpenFont(
200
- Buffer.from(path + "\x00"),
201
- pt
202
- );
203
- if (tempFont == null) throw `Font loading failed`;
204
- Global.ptrFont = tempFont;
205
- }
206
- }
207
-
208
- class Image {
209
- private pointer;
210
- public readonly width;
211
- public readonly height;
212
-
213
- constructor(texture: Pointer) {
214
- this.pointer = texture;
215
-
216
- const _wArr = new Uint32Array(1);
217
- const _hArr = new Uint32Array(1);
218
-
219
- libsdl.symbols.SDL_QueryTexture(
220
- texture,
221
- null,
222
- null,
223
- ptr(_wArr),
224
- ptr(_hArr)
135
+ public drawRect(rectangle: Rectangle, color: Color) {
136
+ libsdl.symbols.SDL_SetRenderDrawColor(
137
+ Renderer.pointer,
138
+ color.r,
139
+ color.g,
140
+ color.b,
141
+ color.a
225
142
  );
226
-
227
- this.width = _wArr[0];
228
- this.height = _hArr[0];
143
+ libsdl.symbols.SDL_RenderFillRect(Renderer.pointer, rectangle.pointer);
229
144
  }
230
145
  }
231
146
 
@@ -1,80 +1,125 @@
1
+ import { libsdl } from "../ffi";
2
+ import { toArrayBuffer } from "bun:ffi";
3
+
1
4
  /** @internal */
2
5
  class Keyboard {
6
+ static #instance: Keyboard;
3
7
 
4
- static downKeyMap = new Map<string, boolean>();
5
- static pressedKeyMap = new Map<string, boolean>();
6
- static releasedKeyMap = new Map<string, boolean>();
7
-
8
- static setKeyDown(key: string) {
9
- this.downKeyMap.set(key, true);
10
- this.releasedKeyMap.set(key, false);
11
- }
8
+ static keyState: DataView;
9
+ static pressMap = new Map<string, number>();
10
+
11
+ private constructor() {}
12
12
 
13
- static setKeyUp(key: string) {
14
- this.downKeyMap.set(key, false);
15
- this.pressedKeyMap.set(key, false);
13
+ public static get instance() {
14
+ if (!Keyboard.#instance) {
15
+ Keyboard.#instance = new Keyboard();
16
+ }
17
+
18
+ return Keyboard.#instance;
16
19
  }
17
20
 
18
- /**
19
- *
20
- * @param key string of key
21
- * @returns if the key is being held down
22
- */
23
- isDown(key: keys) {
24
- const _state = Keyboard.downKeyMap.get(key);
25
- if (_state == undefined) return false
21
+ private static convertScancodeToKey(scancode: number): string {
22
+ const keyFromScancode = libsdl.symbols.SDL_GetKeyFromScancode(scancode);
23
+ const keyName = libsdl.symbols
24
+ .SDL_GetKeyName(keyFromScancode)
25
+ .toString();
26
26
 
27
- return _state;
27
+ return keyName;
28
28
  }
29
29
 
30
- /**
31
- *
32
- * @param key string of key
33
- * @returns if key is pressed. Returns only once
34
- */
35
- isPressed(key: keys) {
36
- const _pressedState = Keyboard.pressedKeyMap.get(key);
37
- const _downState = Keyboard.downKeyMap.get(key);
38
-
39
- if (_downState == true) {
40
- if (_pressedState == false || _pressedState == undefined) {
41
- Keyboard.pressedKeyMap.set(key, true);
42
- return true;
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);
39
+ if (kmGet == undefined || kmGet == 0) {
40
+ this.pressMap.set(keyName, 1);
41
+ } else if (kmGet == 1) {
42
+ this.pressMap.set(keyName, 2);
43
+ }
44
+ } else if (myArr.getUint8(i) == 0) {
45
+ const keyName = this.convertScancodeToKey(i).toLowerCase();
46
+ this.pressMap.set(keyName, 0);
43
47
  }
44
48
  }
49
+ }
50
+
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
+
45
64
 
46
65
  return false;
47
66
  }
48
67
 
49
- /**
50
- *
51
- * @param key string of key
52
- * @returns if key is released. Returns only once
53
- */
54
- isReleased(key: keys) {
55
- const _releasedState = Keyboard.releasedKeyMap.get(key);
56
- const _downState = Keyboard.downKeyMap.get(key);
57
-
58
- if (_downState == false) {
59
- if (_releasedState == false || undefined) {
60
- Keyboard.releasedKeyMap.set(key, true);
61
- return true;
62
- }
68
+
69
+ public isDown(key: keys) {
70
+ const kmGet = Keyboard.pressMap.get(key);
71
+ if (kmGet == 1 || kmGet == 2) {
72
+ return true;
63
73
  }
64
74
 
65
75
  return false;
66
76
  }
67
77
  }
68
78
 
69
- type keys = 'a' |'b' |'c' |'d' |'e' |'f' |'g' |'h' |'i' |'j' |'k' |'l' |'m' |'n' |'o' |'p' |'q' |'r' |'s' |'t' |'u' |'v' |'w' |'x' |'y' |'z' |
70
- '1' |'2' |'3' |'4' |'5' |'6' |'7' |'8' |'9' |'0' |
71
- 'space' |
72
- 'caps lock' |
73
- 'tab' |
74
- 'left shift' |
75
- 'right shift' |
76
- 'left ctrl' |
77
- 'escape';
79
+ type keys =
80
+ | "a"
81
+ | "b"
82
+ | "c"
83
+ | "d"
84
+ | "e"
85
+ | "f"
86
+ | "g"
87
+ | "h"
88
+ | "i"
89
+ | "j"
90
+ | "k"
91
+ | "l"
92
+ | "m"
93
+ | "n"
94
+ | "o"
95
+ | "p"
96
+ | "q"
97
+ | "r"
98
+ | "s"
99
+ | "t"
100
+ | "u"
101
+ | "v"
102
+ | "w"
103
+ | "x"
104
+ | "y"
105
+ | "z"
106
+ | "1"
107
+ | "2"
108
+ | "3"
109
+ | "4"
110
+ | "5"
111
+ | "6"
112
+ | "7"
113
+ | "8"
114
+ | "9"
115
+ | "0"
116
+ | "space"
117
+ | "caps lock"
118
+ | "tab"
119
+ | "left shift"
120
+ | "right shift"
121
+ | "left ctrl"
122
+ | "escape";
78
123
 
79
124
  /** @internal */
80
- export default Keyboard;
125
+ export default Keyboard;
@@ -1,16 +1,25 @@
1
1
  import { libsdl } from "../ffi";
2
- import { ptr } from 'bun:ffi';
2
+ import { ptr } from "bun:ffi";
3
+ import {Vector2} from "../engine/vector";
3
4
 
4
5
  /** @internal */
5
6
  class Mouse {
7
+ static #instance: Mouse;
6
8
 
7
9
  static downKeyMap = new Map<string, boolean>();
8
10
  static pressedKeyMap = new Map<string, boolean>();
9
11
  static releasedKeyMap = new Map<string, boolean>();
10
-
11
- static setButtonDown(button: number) {
12
12
 
13
- let key : string = '';
13
+ private constructor() {}
14
+
15
+ public static get instance() {
16
+ if (!Mouse.#instance) Mouse.#instance = new Mouse();
17
+
18
+ return Mouse.#instance;
19
+ }
20
+
21
+ static setButtonDown(button: number) {
22
+ let key: string = "";
14
23
  if (button == 1) {
15
24
  key = "left";
16
25
  } else if (button == 2) {
@@ -24,8 +33,7 @@ class Mouse {
24
33
  }
25
34
 
26
35
  static setButtonUp(button: number) {
27
-
28
- let key : string = '';
36
+ let key: string = "";
29
37
  if (button == 1) {
30
38
  key = "left";
31
39
  } else if (button == 2) {
@@ -33,25 +41,25 @@ class Mouse {
33
41
  } else {
34
42
  key = "right";
35
43
  }
36
-
44
+
37
45
  this.downKeyMap.set(key, false);
38
46
  this.pressedKeyMap.set(key, false);
39
47
  }
40
48
 
41
49
  /**
42
- *
50
+ *
43
51
  * @param button string of button
44
52
  * @returns if the button is being held down
45
53
  */
46
54
  isDown(button: buttons) {
47
55
  const _state = Mouse.downKeyMap.get(button);
48
- if (_state == undefined) return false
56
+ if (_state == undefined) return false;
49
57
 
50
58
  return _state;
51
59
  }
52
60
 
53
61
  /**
54
- *
62
+ *
55
63
  * @param button string of button
56
64
  * @returns if button is pressed. Returns only once
57
65
  */
@@ -70,7 +78,7 @@ class Mouse {
70
78
  }
71
79
 
72
80
  /**
73
- *
81
+ *
74
82
  * @param button string of button
75
83
  * @returns if button is released. Returns only once
76
84
  */
@@ -92,11 +100,11 @@ class Mouse {
92
100
  const xArr = new Uint32Array(1);
93
101
  const yArr = new Uint32Array(1);
94
102
  libsdl.symbols.SDL_GetMouseState(ptr(xArr), ptr(yArr));
95
- return {x: xArr[0], y: yArr[0]};
103
+ return new Vector2(xArr[0], yArr[0]);
96
104
  }
97
105
  }
98
106
 
99
- type buttons = 'left' | 'middle' | 'right';
107
+ type buttons = "left" | "middle" | "right";
100
108
 
101
109
  /** @internal */
102
- export default Mouse;
110
+ export default Mouse;
@@ -1,8 +1,43 @@
1
1
  import { describe, expect, it } from "bun:test";
2
- import { initLibraries } from "./engine";
2
+ import { initSDL, initSDLImage, initSDLTypeFont, initSDLMixer } from "./engine";
3
+ import Vector2 from "./engine/vector";
4
+ import Window from "./engine/window";
5
+ import Renderer from "./engine/renderer";
3
6
 
4
- describe("Initializing ", () => {
7
+ describe("Initializing SDL ", () => {
5
8
  it("Should initialize without error ", () => {
6
- expect(() => initLibraries()).not.toThrow(Error);
9
+ expect(() => initSDL()).not.toThrow(Error);
10
+ });
11
+ });
12
+
13
+ describe("Initializing SDL Image ", () => {
14
+ it("Should initialize without error ", () => {
15
+ expect(() => initSDLImage()).not.toThrow(Error);
16
+ });
17
+ });
18
+
19
+ describe("Initializing SDL TTF ", () => {
20
+ it("Should initialize without error ", () => {
21
+ expect(() => initSDLTypeFont()).not.toThrow(Error);
22
+ });
23
+ });
24
+
25
+ describe("Initializing SDL Mixer ", () => {
26
+ it("Should initialize without error ", () => {
27
+ expect(() => initSDLMixer()).not.toThrow(Error);
28
+ });
29
+ });
30
+
31
+ describe("Window Creation ", () => {
32
+ it("Should create window without error", () => {
33
+ expect(() =>
34
+ Window.createWindow("Game", new Vector2(1, 1))
35
+ ).not.toThrow(Error);
36
+ });
37
+ });
38
+
39
+ describe("Renderer Creation ", () => {
40
+ it("Should create renderer without error", () => {
41
+ expect(() => Renderer.createRenderer()).not.toThrow(Error);
7
42
  });
8
43
  });