slifer 0.1.8 → 0.1.9

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