slifer 0.1.9 → 0.2.0

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,176 +1,210 @@
1
- import { libimage, libsdl, libttf } from "../ffi";
2
- import Global from "../global";
3
- import { type Pointer, ptr } from 'bun:ffi';
4
- import type { Rectangle } from "../slifer";
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
- //@ts-expect-error Buffer and CString differences
51
- const surface = libimage.symbols.IMG_Load(_path);
52
- if (surface == null) throw `Image failed to load`;
53
- const texture = libsdl.symbols.SDL_CreateTextureFromSurface(Global.ptrRenderer, surface);
54
- if (texture == null) throw `Image failed to be created`;
55
- return new Image(texture);
56
- }
57
-
58
- /**
59
- * Method to draw the image to the screen
60
- *
61
- * @param image Image object to draw. Made using Slifer.Graphics.loadImage
62
- * @param x x position to draw image
63
- * @param y y position to draw image
64
- * @param rotation (optional) rotation of image
65
- * @param xs (optional) scale of x axis
66
- * @param ys (optional) scale of y axis
67
- * @param flip (optional) horizontal flip
68
- */
69
- draw(image: Image, positionX: number, positionY: number, clipRectangle?: Rectangle, rotation?: number, xs?: number, ys?: number, flip?: true) {
70
- const _dest = new Uint32Array(4);
71
- const wArr = new Uint32Array(1);
72
- const hArr = new Uint32Array(1);
73
- let srcRect: null | Uint32Array = null;
74
-
75
- if (clipRectangle == undefined) {
76
- libsdl.symbols.SDL_QueryTexture((image as any).pointer, null, null, ptr(wArr), ptr(hArr));
77
- } else {
78
- srcRect = new Uint32Array(4);
79
- srcRect[0] = clipRectangle.x;
80
- srcRect[1] = clipRectangle.y;
81
- srcRect[2] = clipRectangle.width;
82
- srcRect[3] = clipRectangle.height;
83
- wArr[0] = clipRectangle.width;
84
- hArr[0] = clipRectangle.height;
85
- }
86
-
87
-
88
- _dest[0] = positionX;
89
- _dest[1] = positionY;
90
- _dest[2] = wArr[0] * (xs ? xs : 1);
91
- _dest[3] = hArr[0] * (ys ? ys : 1);
92
- const _center = new Uint32Array(2);
93
- _center[0] = _dest[2] / 2;
94
- _center[1] = _dest[3] / 2;
95
- libsdl.symbols.SDL_RenderCopyEx(Global.ptrRenderer, (image as any).pointer, srcRect, ptr(_dest), rotation ? rotation : 0, ptr(_center), flip ? Number(flip) : 0);
96
- }
97
-
98
- /**
99
- * Method to draw text to the screen
100
- *
101
- * @param text the string of text to print
102
- * @param x x position
103
- * @param y y position
104
- * @param color color of text. Made using Slifer.Graphics.makeColor.
105
- */
106
- print(text: string, x: number, y: number, color: Color) {
107
-
108
- // Create text buffer
109
- const textBuffer = Buffer.from(text+"\x00");
110
-
111
- // Get width and height of text
112
- const wArr = new Uint32Array(1);
113
- const hArr = new Uint32Array(1);
114
- //@ts-expect-error Buffer and CString differences
115
- libttf.symbols.TTF_SizeText(Global.ptrFont,textBuffer , ptr(wArr), ptr(hArr));
116
-
117
- // Define color
118
- const _col = ((color.r << 0) + (color.g << 8) + (color.b << 16));
119
-
120
- // Create texture
121
- //@ts-expect-error Buffer and CString differences
122
- const surface = libttf.symbols.TTF_RenderText_Solid(Global.ptrFont, textBuffer, _col);
123
- if (surface == null) throw `Surface creation failed on print`;
124
- const texture = libsdl.symbols.SDL_CreateTextureFromSurface(Global.ptrRenderer, surface);
125
- if (texture == null) throw `Texture creation failed on print`;
126
-
127
- // Create destination
128
- const destArr = new Uint32Array(4);
129
- destArr[0] = x;
130
- destArr[1] = y;
131
- destArr[2] = wArr[0];
132
- destArr[3] = hArr[0];
133
-
134
- // Draw text
135
- libsdl.symbols.SDL_RenderCopy(Global.ptrRenderer, texture, null, ptr(destArr));
136
- }
137
-
138
- /**
139
- * Sets the font to a ttf file in your project
140
- *
141
- * @param path relative path to font
142
- * @param pt size of text
143
- */
144
- setFont(path: string, pt: number) {
145
- //@ts-expect-error Buffer and CString differences
146
- const tempFont = libttf.symbols.TTF_OpenFont(Buffer.from(path+"\x00"), pt);
147
- if (tempFont == null) throw `Font loading failed`;
148
- Global.ptrFont = tempFont;
149
- }
150
- }
151
-
152
- class Image {
153
-
154
- private pointer;
155
-
156
- constructor(texture: Pointer) {
157
- this.pointer = texture;
158
- }
159
- }
160
-
161
- class Color {
162
- readonly r;
163
- readonly g;
164
- readonly b;
165
- readonly a;
166
-
167
- constructor(r: number, g: number, b: number, a: number) {
168
- this.r = r;
169
- this.g = g;
170
- this.b = b;
171
- this.a = a;
172
- }
173
- }
174
-
175
- /** @internal */
176
- export default Graphics;
1
+ import { libimage, libsdl, libttf } from "../ffi";
2
+ import Global from "../global";
3
+ import { type Pointer, ptr } from "bun:ffi";
4
+ import Rectangle from "../engine/rectangle";
5
+ import Color from "../color";
6
+ import Vector2 from "../engine/vector";
7
+
8
+ /** @internal */
9
+ class Graphics {
10
+ /**
11
+ * Slifers draw function. Used to draw everything to the screen.
12
+ */
13
+ render() {
14
+ libsdl.symbols.SDL_RenderPresent(Global.ptrRenderer);
15
+ }
16
+
17
+ /**
18
+ * Create a new color. All values are from 0-255
19
+ *
20
+ * @param r red value
21
+ * @param g green value
22
+ * @param b blue value
23
+ * @param a alpha value
24
+ * @returns Color object
25
+ */
26
+ makeColor(r: number, g: number, b: number, a: number) {
27
+ const _color = new Color(r, g, b, a);
28
+ return _color;
29
+ }
30
+
31
+ /**
32
+ * Sets the background of the window to a color of choice.
33
+ *
34
+ * Make sure this is put in the top level of the while loop
35
+ * as it will clear the renderer.
36
+ *
37
+ * @param color Color object. Make using Slifer.Graphics.makeColor
38
+ */
39
+ setBackground(color: Color) {
40
+ libsdl.symbols.SDL_SetRenderDrawColor(
41
+ Global.ptrRenderer,
42
+ color.r,
43
+ color.g,
44
+ color.b,
45
+ color.a
46
+ );
47
+ libsdl.symbols.SDL_RenderClear(Global.ptrRenderer);
48
+ }
49
+
50
+ /**
51
+ * Loads a new image
52
+ *
53
+ * @param path string path to image
54
+ * @returns Image ready to draw
55
+ */
56
+ loadImage(path: string): Image {
57
+ const _path = Buffer.from(path + "\x00");
58
+ const surface = libimage.symbols.IMG_Load(_path);
59
+ if (surface == null) throw `Image failed to load`;
60
+ const texture = libsdl.symbols.SDL_CreateTextureFromSurface(
61
+ Global.ptrRenderer,
62
+ surface
63
+ );
64
+ if (texture == null) throw `Image failed to be created`;
65
+ return new Image(texture);
66
+ }
67
+
68
+ draw(image: Image, position: Vector2) {}
69
+
70
+ /**
71
+ * Method to draw the image to the screen
72
+ *
73
+ * @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
133
+ *
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
+ */
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];
175
+
176
+ // Draw text
177
+ libsdl.symbols.SDL_RenderCopy(
178
+ Global.ptrRenderer,
179
+ texture,
180
+ null,
181
+ ptr(destArr)
182
+ );
183
+ }
184
+
185
+ /**
186
+ * Sets the font to a ttf file in your project
187
+ *
188
+ * @param path relative path to font
189
+ * @param pt size of text
190
+ */
191
+ setFont(path: string, pt: number) {
192
+ const tempFont = libttf.symbols.TTF_OpenFont(
193
+ Buffer.from(path + "\x00"),
194
+ pt
195
+ );
196
+ if (tempFont == null) throw `Font loading failed`;
197
+ Global.ptrFont = tempFont;
198
+ }
199
+ }
200
+
201
+ class Image {
202
+ private pointer;
203
+
204
+ constructor(texture: Pointer) {
205
+ this.pointer = texture;
206
+ }
207
+ }
208
+
209
+ /** @internal */
210
+ export default Graphics;
@@ -1,80 +1,80 @@
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 */
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 */
80
80
  export default Keyboard;