slifer 0.1.3 → 0.1.4

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,156 +1,158 @@
1
- import { libimage, libsdl, libttf } from "../ffi";
2
- import Global from "../global";
3
- import { type Pointer, ptr } from 'bun:ffi';
4
- import path from 'path';
5
-
6
- class Graphics {
7
- /**
8
- * Slifers draw function. Used to draw everything to the screen.
9
- */
10
- render() {
11
- libsdl.symbols.SDL_RenderPresent(Global.ptrRenderer);
12
- }
13
-
14
- /**
15
- * Create a new color. All values are from 0-255
16
- *
17
- * @param r red value
18
- * @param g green value
19
- * @param b blue value
20
- * @param a alpha value
21
- * @returns Color object
22
- */
23
- makeColor(r: number, g: number, b: number, a: number) {
24
- const _color = new Color(r, g, b, a);
25
- return _color;
26
- }
27
-
28
- /**
29
- * Sets the background of the window to a color of choice.
30
- *
31
- * Make sure this is put in the top level of the while loop
32
- * as it will clear the renderer.
33
- *
34
- * @param color Color object. Make using Slifer.Graphics.makeColor
35
- */
36
- setBackground(color: Color) {
37
- libsdl.symbols.SDL_SetRenderDrawColor(Global.ptrRenderer, color.r, color.g, color.b, color.a);
38
- libsdl.symbols.SDL_RenderClear(Global.ptrRenderer);
39
- }
40
-
41
- /**
42
- * Loads a new image
43
- *
44
- * @param path string path to image
45
- * @returns Image ready to draw
46
- */
47
- loadImage(path: string) : Image {
48
- const _path = Buffer.from(path + "\x00");
49
- const surface = libimage.symbols.IMG_Load(_path);
50
- if (surface == null) throw `Image failed to load`;
51
- const texture = libsdl.symbols.SDL_CreateTextureFromSurface(Global.ptrRenderer, surface);
52
- if (texture == null) throw `Image failed to be created`;
53
- return new Image(texture);
54
- }
55
-
56
- /**
57
- * Method to draw the image to the screen
58
- *
59
- * @param image Image object to draw. Made using Slifer.Graphics.loadImage
60
- * @param x x position to draw image
61
- * @param y y position to draw image
62
- * @param rotation (optional) rotation of image
63
- * @param xs (optional) scale of x axis
64
- * @param ys (optional) scale of y axis
65
- * @param flip (optional) horizontal flip
66
- */
67
- draw(image: Image, x: number, y: number, rotation?: number, xs?: number, ys?: number, flip?: true) {
68
- const _dest = new Uint32Array(4);
69
- const wArr = new Uint32Array(1);
70
- const hArr = new Uint32Array(1);
71
- libsdl.symbols.SDL_QueryTexture((image as any).pointer, null, null, ptr(wArr), ptr(hArr));
72
- _dest[0] = x;
73
- _dest[1] = y;
74
- _dest[2] = wArr[0] * (xs ? xs : 1);
75
- _dest[3] = hArr[0] * (ys ? ys : 1);
76
- const _center = new Uint32Array(2);
77
- _center[0] = _dest[2] / 2;
78
- _center[1] = _dest[3] / 2;
79
- libsdl.symbols.SDL_RenderCopyEx(Global.ptrRenderer, (image as any).pointer, null, ptr(_dest), rotation ? rotation : 0, ptr(_center), flip ? Number(flip) : 0);
80
- }
81
-
82
- /**
83
- * Method to draw text to the screen
84
- *
85
- * @param text the string of text to print
86
- * @param x x position
87
- * @param y y position
88
- * @param color color of text. Made using Slifer.Graphics.makeColor.
89
- */
90
- print(text: string, x: number, y: number, color: Color) {
91
-
92
- // Create text buffer
93
- const textBuffer = Buffer.from(text+"\x00");
94
-
95
- // Get width and height of text
96
- const wArr = new Uint32Array(1);
97
- const hArr = new Uint32Array(1);
98
- libttf.symbols.TTF_SizeText(Global.ptrFont,textBuffer , ptr(wArr), ptr(hArr));
99
-
100
- // Define color
101
- const _col = ((color.r << 0) + (color.g << 8) + (color.b << 16));
102
-
103
- // Create texture
104
- const surface = libttf.symbols.TTF_RenderText_Solid(Global.ptrFont, textBuffer, _col);
105
- if (surface == null) throw `Surface creation failed on print`;
106
- const texture = libsdl.symbols.SDL_CreateTextureFromSurface(Global.ptrRenderer, surface);
107
- if (texture == null) throw `Texture creation failed on print`;
108
-
109
- // Create destination
110
- const destArr = new Uint32Array(4);
111
- destArr[0] = x;
112
- destArr[1] = y;
113
- destArr[2] = wArr[0];
114
- destArr[3] = hArr[0];
115
-
116
- // Draw text
117
- libsdl.symbols.SDL_RenderCopy(Global.ptrRenderer, texture, null, ptr(destArr));
118
- }
119
-
120
- /**
121
- * Sets the font to a ttf file in your project
122
- *
123
- * @param path relative path to font
124
- * @param pt size of text
125
- */
126
- setFont(path: string, pt: number) {
127
- const tempFont = libttf.symbols.TTF_OpenFont(Buffer.from(path+"\x00"), pt);
128
- if (tempFont == null) throw `Font loading failed`;
129
- Global.ptrFont = tempFont;
130
- }
131
- }
132
-
133
- class Image {
134
-
135
- private pointer;
136
-
137
- constructor(texture: Pointer) {
138
- this.pointer = texture;
139
- }
140
- }
141
-
142
- class Color {
143
- readonly r;
144
- readonly g;
145
- readonly b;
146
- readonly a;
147
-
148
- constructor(r: number, g: number, b: number, a: number) {
149
- this.r = r;
150
- this.g = g;
151
- this.b = b;
152
- this.a = a;
153
- }
154
- }
155
-
1
+ import { libimage, libsdl, libttf } from "../ffi";
2
+ import Global from "../global";
3
+ import { type Pointer, ptr } from 'bun:ffi';
4
+ import path from 'path';
5
+
6
+ /** @internal */
7
+ class Graphics {
8
+ /**
9
+ * Slifers draw function. Used to draw everything to the screen.
10
+ */
11
+ render() {
12
+ libsdl.symbols.SDL_RenderPresent(Global.ptrRenderer);
13
+ }
14
+
15
+ /**
16
+ * Create a new color. All values are from 0-255
17
+ *
18
+ * @param r red value
19
+ * @param g green value
20
+ * @param b blue value
21
+ * @param a alpha value
22
+ * @returns Color object
23
+ */
24
+ makeColor(r: number, g: number, b: number, a: number) {
25
+ const _color = new Color(r, g, b, a);
26
+ return _color;
27
+ }
28
+
29
+ /**
30
+ * Sets the background of the window to a color of choice.
31
+ *
32
+ * Make sure this is put in the top level of the while loop
33
+ * as it will clear the renderer.
34
+ *
35
+ * @param color Color object. Make using Slifer.Graphics.makeColor
36
+ */
37
+ setBackground(color: Color) {
38
+ libsdl.symbols.SDL_SetRenderDrawColor(Global.ptrRenderer, color.r, color.g, color.b, color.a);
39
+ libsdl.symbols.SDL_RenderClear(Global.ptrRenderer);
40
+ }
41
+
42
+ /**
43
+ * Loads a new image
44
+ *
45
+ * @param path string path to image
46
+ * @returns Image ready to draw
47
+ */
48
+ loadImage(path: string) : Image {
49
+ const _path = Buffer.from(path + "\x00");
50
+ const surface = libimage.symbols.IMG_Load(_path);
51
+ if (surface == null) throw `Image failed to load`;
52
+ const texture = libsdl.symbols.SDL_CreateTextureFromSurface(Global.ptrRenderer, surface);
53
+ if (texture == null) throw `Image failed to be created`;
54
+ return new Image(texture);
55
+ }
56
+
57
+ /**
58
+ * Method to draw the image to the screen
59
+ *
60
+ * @param image Image object to draw. Made using Slifer.Graphics.loadImage
61
+ * @param x x position to draw image
62
+ * @param y y position to draw image
63
+ * @param rotation (optional) rotation of image
64
+ * @param xs (optional) scale of x axis
65
+ * @param ys (optional) scale of y axis
66
+ * @param flip (optional) horizontal flip
67
+ */
68
+ draw(image: Image, x: number, y: number, rotation?: number, xs?: number, ys?: number, flip?: true) {
69
+ const _dest = new Uint32Array(4);
70
+ const wArr = new Uint32Array(1);
71
+ const hArr = new Uint32Array(1);
72
+ libsdl.symbols.SDL_QueryTexture((image as any).pointer, null, null, ptr(wArr), ptr(hArr));
73
+ _dest[0] = x;
74
+ _dest[1] = y;
75
+ _dest[2] = wArr[0] * (xs ? xs : 1);
76
+ _dest[3] = hArr[0] * (ys ? ys : 1);
77
+ const _center = new Uint32Array(2);
78
+ _center[0] = _dest[2] / 2;
79
+ _center[1] = _dest[3] / 2;
80
+ libsdl.symbols.SDL_RenderCopyEx(Global.ptrRenderer, (image as any).pointer, null, ptr(_dest), rotation ? rotation : 0, ptr(_center), flip ? Number(flip) : 0);
81
+ }
82
+
83
+ /**
84
+ * Method to draw text to the screen
85
+ *
86
+ * @param text the string of text to print
87
+ * @param x x position
88
+ * @param y y position
89
+ * @param color color of text. Made using Slifer.Graphics.makeColor.
90
+ */
91
+ print(text: string, x: number, y: number, color: Color) {
92
+
93
+ // Create text buffer
94
+ const textBuffer = Buffer.from(text+"\x00");
95
+
96
+ // Get width and height of text
97
+ const wArr = new Uint32Array(1);
98
+ const hArr = new Uint32Array(1);
99
+ libttf.symbols.TTF_SizeText(Global.ptrFont,textBuffer , ptr(wArr), ptr(hArr));
100
+
101
+ // Define color
102
+ const _col = ((color.r << 0) + (color.g << 8) + (color.b << 16));
103
+
104
+ // Create texture
105
+ const surface = libttf.symbols.TTF_RenderText_Solid(Global.ptrFont, textBuffer, _col);
106
+ if (surface == null) throw `Surface creation failed on print`;
107
+ const texture = libsdl.symbols.SDL_CreateTextureFromSurface(Global.ptrRenderer, surface);
108
+ if (texture == null) throw `Texture creation failed on print`;
109
+
110
+ // Create destination
111
+ const destArr = new Uint32Array(4);
112
+ destArr[0] = x;
113
+ destArr[1] = y;
114
+ destArr[2] = wArr[0];
115
+ destArr[3] = hArr[0];
116
+
117
+ // Draw text
118
+ libsdl.symbols.SDL_RenderCopy(Global.ptrRenderer, texture, null, ptr(destArr));
119
+ }
120
+
121
+ /**
122
+ * Sets the font to a ttf file in your project
123
+ *
124
+ * @param path relative path to font
125
+ * @param pt size of text
126
+ */
127
+ setFont(path: string, pt: number) {
128
+ const tempFont = libttf.symbols.TTF_OpenFont(Buffer.from(path+"\x00"), pt);
129
+ if (tempFont == null) throw `Font loading failed`;
130
+ Global.ptrFont = tempFont;
131
+ }
132
+ }
133
+
134
+ class Image {
135
+
136
+ private pointer;
137
+
138
+ constructor(texture: Pointer) {
139
+ this.pointer = texture;
140
+ }
141
+ }
142
+
143
+ class Color {
144
+ readonly r;
145
+ readonly g;
146
+ readonly b;
147
+ readonly a;
148
+
149
+ constructor(r: number, g: number, b: number, a: number) {
150
+ this.r = r;
151
+ this.g = g;
152
+ this.b = b;
153
+ this.a = a;
154
+ }
155
+ }
156
+
157
+ /** @internal */
156
158
  export default Graphics;
@@ -1,78 +1,80 @@
1
- class Keyboard {
2
-
3
- static downKeyMap = new Map<string, boolean>();
4
- static pressedKeyMap = new Map<string, boolean>();
5
- static releasedKeyMap = new Map<string, boolean>();
6
-
7
- static setKeyDown(key: string) {
8
- this.downKeyMap.set(key, true);
9
- this.releasedKeyMap.set(key, false);
10
- }
11
-
12
- static setKeyUp(key: string) {
13
- this.downKeyMap.set(key, false);
14
- this.pressedKeyMap.set(key, false);
15
- }
16
-
17
- /**
18
- *
19
- * @param key string of key
20
- * @returns if the key is being held down
21
- */
22
- isDown(key: keys) {
23
- const _state = Keyboard.downKeyMap.get(key);
24
- if (_state == undefined) return false
25
-
26
- return _state;
27
- }
28
-
29
- /**
30
- *
31
- * @param key string of key
32
- * @returns if key is pressed. Returns only once
33
- */
34
- isPressed(key: keys) {
35
- const _pressedState = Keyboard.pressedKeyMap.get(key);
36
- const _downState = Keyboard.downKeyMap.get(key);
37
-
38
- if (_downState == true) {
39
- if (_pressedState == false || _pressedState == undefined) {
40
- Keyboard.pressedKeyMap.set(key, true);
41
- return true;
42
- }
43
- }
44
-
45
- return false;
46
- }
47
-
48
- /**
49
- *
50
- * @param key string of key
51
- * @returns if key is released. Returns only once
52
- */
53
- isReleased(key: keys) {
54
- const _releasedState = Keyboard.releasedKeyMap.get(key);
55
- const _downState = Keyboard.downKeyMap.get(key);
56
-
57
- if (_downState == false) {
58
- if (_releasedState == false || undefined) {
59
- Keyboard.releasedKeyMap.set(key, true);
60
- return true;
61
- }
62
- }
63
-
64
- return false;
65
- }
66
- }
67
-
68
- 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' |
69
- '1' |'2' |'3' |'4' |'5' |'6' |'7' |'8' |'9' |'0' |
70
- 'space' |
71
- 'caps lock' |
72
- 'tab' |
73
- 'left shift' |
74
- 'right shift' |
75
- 'left ctrl' |
76
- 'escape';
77
-
1
+ /** @internal */
2
+ class Keyboard {
3
+
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
+ }
12
+
13
+ static setKeyUp(key: string) {
14
+ this.downKeyMap.set(key, false);
15
+ this.pressedKeyMap.set(key, false);
16
+ }
17
+
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
26
+
27
+ return _state;
28
+ }
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;
43
+ }
44
+ }
45
+
46
+ return false;
47
+ }
48
+
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
+ }
63
+ }
64
+
65
+ return false;
66
+ }
67
+ }
68
+
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';
78
+
79
+ /** @internal */
78
80
  export default Keyboard;