slifer 0.2.0 → 0.2.2

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