slifer 0.1.4 → 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,158 +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
- /** @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 */
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 */
158
158
  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;
@@ -1,102 +1,102 @@
1
- import { libsdl } from "../ffi";
2
- import { ptr } from 'bun:ffi';
3
-
4
- /** @internal */
5
- class Mouse {
6
-
7
- static downKeyMap = new Map<string, boolean>();
8
- static pressedKeyMap = new Map<string, boolean>();
9
- static releasedKeyMap = new Map<string, boolean>();
10
-
11
- static setButtonDown(button: number) {
12
-
13
- let key : string = '';
14
- if (button == 1) {
15
- key = "left";
16
- } else if (button == 2) {
17
- key = "middle";
18
- } else {
19
- key = "right";
20
- }
21
-
22
- this.downKeyMap.set(key, true);
23
- this.releasedKeyMap.set(key, false);
24
- }
25
-
26
- static setButtonUp(button: number) {
27
-
28
- let key : string = '';
29
- if (button == 1) {
30
- key = "left";
31
- } else if (button == 2) {
32
- key = "middle";
33
- } else {
34
- key = "right";
35
- }
36
-
37
- this.downKeyMap.set(key, false);
38
- this.pressedKeyMap.set(key, false);
39
- }
40
-
41
- /**
42
- *
43
- * @param button string of button
44
- * @returns if the button is being held down
45
- */
46
- isDown(button: buttons) {
47
- const _state = Mouse.downKeyMap.get(button);
48
- if (_state == undefined) return false
49
-
50
- return _state;
51
- }
52
-
53
- /**
54
- *
55
- * @param button string of button
56
- * @returns if button is pressed. Returns only once
57
- */
58
- isPressed(button: buttons) {
59
- const _pressedState = Mouse.pressedKeyMap.get(button);
60
- const _downState = Mouse.downKeyMap.get(button);
61
-
62
- if (_downState == true) {
63
- if (_pressedState == false || _pressedState == undefined) {
64
- Mouse.pressedKeyMap.set(button, true);
65
- return true;
66
- }
67
- }
68
-
69
- return false;
70
- }
71
-
72
- /**
73
- *
74
- * @param button string of button
75
- * @returns if button is released. Returns only once
76
- */
77
- isReleased(button: buttons) {
78
- const _releasedState = Mouse.releasedKeyMap.get(button);
79
- const _downState = Mouse.downKeyMap.get(button);
80
-
81
- if (_downState == false) {
82
- if (_releasedState == false || undefined) {
83
- Mouse.releasedKeyMap.set(button, true);
84
- return true;
85
- }
86
- }
87
-
88
- return false;
89
- }
90
-
91
- getPosition() {
92
- const xArr = new Uint32Array(1);
93
- const yArr = new Uint32Array(1);
94
- libsdl.symbols.SDL_GetMouseState(ptr(xArr), ptr(yArr));
95
- return {x: xArr[0], y: yArr[0]};
96
- }
97
- }
98
-
99
- type buttons = 'left' | 'middle' | 'right';
100
-
101
- /** @internal */
1
+ import { libsdl } from "../ffi";
2
+ import { ptr } from 'bun:ffi';
3
+
4
+ /** @internal */
5
+ class Mouse {
6
+
7
+ static downKeyMap = new Map<string, boolean>();
8
+ static pressedKeyMap = new Map<string, boolean>();
9
+ static releasedKeyMap = new Map<string, boolean>();
10
+
11
+ static setButtonDown(button: number) {
12
+
13
+ let key : string = '';
14
+ if (button == 1) {
15
+ key = "left";
16
+ } else if (button == 2) {
17
+ key = "middle";
18
+ } else {
19
+ key = "right";
20
+ }
21
+
22
+ this.downKeyMap.set(key, true);
23
+ this.releasedKeyMap.set(key, false);
24
+ }
25
+
26
+ static setButtonUp(button: number) {
27
+
28
+ let key : string = '';
29
+ if (button == 1) {
30
+ key = "left";
31
+ } else if (button == 2) {
32
+ key = "middle";
33
+ } else {
34
+ key = "right";
35
+ }
36
+
37
+ this.downKeyMap.set(key, false);
38
+ this.pressedKeyMap.set(key, false);
39
+ }
40
+
41
+ /**
42
+ *
43
+ * @param button string of button
44
+ * @returns if the button is being held down
45
+ */
46
+ isDown(button: buttons) {
47
+ const _state = Mouse.downKeyMap.get(button);
48
+ if (_state == undefined) return false
49
+
50
+ return _state;
51
+ }
52
+
53
+ /**
54
+ *
55
+ * @param button string of button
56
+ * @returns if button is pressed. Returns only once
57
+ */
58
+ isPressed(button: buttons) {
59
+ const _pressedState = Mouse.pressedKeyMap.get(button);
60
+ const _downState = Mouse.downKeyMap.get(button);
61
+
62
+ if (_downState == true) {
63
+ if (_pressedState == false || _pressedState == undefined) {
64
+ Mouse.pressedKeyMap.set(button, true);
65
+ return true;
66
+ }
67
+ }
68
+
69
+ return false;
70
+ }
71
+
72
+ /**
73
+ *
74
+ * @param button string of button
75
+ * @returns if button is released. Returns only once
76
+ */
77
+ isReleased(button: buttons) {
78
+ const _releasedState = Mouse.releasedKeyMap.get(button);
79
+ const _downState = Mouse.downKeyMap.get(button);
80
+
81
+ if (_downState == false) {
82
+ if (_releasedState == false || undefined) {
83
+ Mouse.releasedKeyMap.set(button, true);
84
+ return true;
85
+ }
86
+ }
87
+
88
+ return false;
89
+ }
90
+
91
+ getPosition() {
92
+ const xArr = new Uint32Array(1);
93
+ const yArr = new Uint32Array(1);
94
+ libsdl.symbols.SDL_GetMouseState(ptr(xArr), ptr(yArr));
95
+ return {x: xArr[0], y: yArr[0]};
96
+ }
97
+ }
98
+
99
+ type buttons = 'left' | 'middle' | 'right';
100
+
101
+ /** @internal */
102
102
  export default Mouse;