slifer 0.1.9 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,176 +1,233 @@
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
+ /**
69
+ * Method to draw to the screen simply
70
+ * @param image Image object to draw. Made using Slifer.Graphics.loadImage
71
+ * @param position position to draw the image
72
+ *
73
+ */
74
+ draw(image: Image, position: Vector2) {
75
+ const dstRect = new Uint32Array(4);
76
+ dstRect[0] = position.x;
77
+ dstRect[1] = position.y;
78
+ dstRect[2] = image.width;
79
+ dstRect[3] = image.height;
80
+
81
+ libsdl.symbols.SDL_RenderCopy(
82
+ Global.ptrRenderer,
83
+ (image as any).pointer,
84
+ null,
85
+ dstRect
86
+ );
87
+ }
88
+
89
+ /**
90
+ * Method to draw the image to the screen with extended arguments
91
+ *
92
+ * @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
99
+ */
100
+ drawEx(
101
+ image: Image,
102
+ position: Vector2,
103
+ clipRectangle?: Rectangle | null,
104
+ rotation?: number,
105
+ xs?: number,
106
+ ys?: number,
107
+ flip?: true
108
+ ) {
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
+ }
119
+
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;
127
+ libsdl.symbols.SDL_RenderCopyEx(
128
+ Global.ptrRenderer,
129
+ (image as any).pointer,
130
+ srcRect,
131
+ ptr(_dest),
132
+ 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
+ null,
188
+ ptr(destArr)
189
+ );
190
+ }
191
+
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)
225
+ );
226
+
227
+ this.width = _wArr[0];
228
+ this.height = _hArr[0];
229
+ }
230
+ }
231
+
232
+ /** @internal */
233
+ 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;