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,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;
package/src/slifer.ts CHANGED
@@ -1,234 +1,225 @@
1
- import { libimage, libsdl, libttf } from "./ffi";
2
- import Global from "./global";
3
- import { ptr } from "bun:ffi";
4
- import Graphics from "./modules/graphics";
5
- import Keyboard from "./modules/keyboard";
6
- import Mouse from "./modules/mouse";
7
- import { version } from "../package.json";
8
-
9
- //@ts-expect-error
10
- const fontFile = await import("./Jost-Bold.ttf");
11
-
12
- /** @internal */
13
- class Window {
14
- public width: number;
15
- public height: number;
16
- public title: string;
17
-
18
- constructor(title: string, width: number, height: number) {
19
- this.width = width;
20
- this.height = height;
21
- this.title = title;
22
- }
23
-
24
- setSize(width: number, height: number): void {
25
- libsdl.symbols.SDL_SetWindowSize(Global.ptrWindow, width, height);
26
- }
27
-
28
- setTitle(title: string): void {
29
- libsdl.symbols.SDL_SetWindowTitle(
30
- Global.ptrWindow,
31
- //@ts-expect-error Buffer and CString differences
32
- Buffer.from(title + "\x00")
33
- );
34
- }
35
-
36
- setFullscreen(flag: boolean): void {
37
- libsdl.symbols.SDL_SetWindowFullscreen(Global.ptrWindow, Number(flag));
38
- }
39
-
40
- centerWindow(): void {
41
- libsdl.symbols.SDL_SetWindowPosition(
42
- Global.ptrWindow,
43
- 0x2fff0000,
44
- 0x2fff0000
45
- );
46
- }
47
-
48
- setPosition(x: number, y: number): void {
49
- libsdl.symbols.SDL_SetWindowPosition(Global.ptrWindow, x, y);
50
- }
51
- }
52
-
53
- /** @interal */
54
- export class SliferClass {
55
- private isRunning: boolean = true;
56
- private lastFrame: number = 0;
57
- private firstFrame: number = 0;
58
-
59
- // Modules
60
- Graphics = new Graphics();
61
- Keyboard = new Keyboard();
62
- Mouse = new Mouse();
63
-
64
- // Public Variables
65
- public dt: number = 0;
66
-
67
- constructor() {
68
- const baseInit = libsdl.symbols.SDL_Init(0x00000020);
69
- if (baseInit != 0) throw `SDL failed to initialize`;
70
-
71
- const imageInit = libimage.symbols.IMG_Init(3);
72
- if (imageInit != 3) throw `SDL Image failed to initialize`;
73
-
74
- const ttfInit = libttf.symbols.TTF_Init();
75
- if (ttfInit != 0) throw `SDL TTF failed to initialize`;
76
-
77
- /*
78
- if (process.platform == "darwin") {
79
- const tempFont = libttf.symbols.TTF_OpenFont(
80
- Buffer.from("/System/Library/Fonts/SFNSMono.ttf"),
81
- 12,
82
- );
83
- if (tempFont == null) throw `Default font loading failed`;
84
- Global.ptrFont = tempFont;
85
- } else if (process.platform == "linux") {
86
- const tempFont = libttf.symbols.TTF_OpenFont(
87
- Buffer.from("/usr/share/fonts/truetype/dejavu/DejaVuSansMono.ttf"),
88
- 12,
89
- );
90
- if (tempFont == null) throw `Default font loading failed`;
91
- Global.ptrFont = tempFont;
92
- }
93
- */
94
-
95
- const tempFont = libttf.symbols.TTF_OpenFont(
96
- //@ts-expect-error Buffer and CString differences
97
- Buffer.from(fontFile.default),
98
- 24
99
- );
100
- if (tempFont == null) throw `Default font loading failed`;
101
- Global.ptrFont = tempFont;
102
- }
103
-
104
- /**
105
- * @param title Title of window
106
- * @param width Width of window
107
- * @param height Height of window
108
- */
109
- createWindow(title: string, width: number, height: number): Window {
110
- // Creating cstring buffer from string
111
- const _title = Buffer.from(title + "\x00");
112
-
113
- // Creating window pointer
114
- const _win = libsdl.symbols.SDL_CreateWindow(
115
- //@ts-expect-error Buffer and CString differences
116
- _title,
117
- 0x2fff0000,
118
- 0x2fff0000,
119
- width,
120
- height,
121
- 0
122
- );
123
- if (_win == null) throw `Window creation failed`;
124
- Global.ptrWindow = _win;
125
-
126
- // Creating renderer pointer
127
- const _ren = libsdl.symbols.SDL_CreateRenderer(Global.ptrWindow, -1, 0);
128
- if (_ren == null) throw `Renderer Creation failed`;
129
- Global.ptrRenderer = _ren;
130
-
131
- this.firstFrame = Number(libsdl.symbols.SDL_GetPerformanceCounter());
132
-
133
- return new Window(title, width, height);
134
- }
135
-
136
- /**
137
- * @returns if the window should close
138
- */
139
- shouldClose(): boolean {
140
- // Clear the renderer
141
- libsdl.symbols.SDL_RenderClear(Global.ptrRenderer);
142
-
143
- // Setup deltatime
144
- this.lastFrame = this.firstFrame;
145
- this.firstFrame = Number(libsdl.symbols.SDL_GetPerformanceCounter());
146
-
147
- this.dt = ((this.firstFrame - this.lastFrame) * 1000 / Number(libsdl.symbols.SDL_GetPerformanceFrequency()));
148
-
149
-
150
- // Poll Events
151
- const eventArray = new Uint16Array(32);
152
- const isEvent = libsdl.symbols.SDL_PollEvent(ptr(eventArray));
153
-
154
- if (isEvent) {
155
- switch (eventArray[0]) {
156
- // Quit event
157
- case 256:
158
- this.isRunning = false;
159
- break;
160
- // Keydown event
161
- case 768:
162
- const _dscancode = eventArray[8];
163
- const _dkey = libsdl.symbols.SDL_GetKeyFromScancode(_dscancode);
164
- const _dname = libsdl.symbols.SDL_GetKeyName(_dkey);
165
- Keyboard.setKeyDown(_dname.toString().toLowerCase());
166
- break;
167
- // Keyup event
168
- case 769:
169
- const _uscancode = eventArray[8];
170
- const _ukey = libsdl.symbols.SDL_GetKeyFromScancode(_uscancode);
171
- const _uname = libsdl.symbols.SDL_GetKeyName(_ukey);
172
- Keyboard.setKeyUp(_uname.toString().toLowerCase());
173
- break;
174
- // Mouse down event
175
- case 1025:
176
- const _dbtn = eventArray[8] - 256;
177
- Mouse.setButtonDown(_dbtn);
178
- break;
179
- // Mouse up event
180
- case 1026:
181
- const _ubtn = eventArray[8];
182
- Mouse.setButtonUp(_ubtn);
183
- break;
184
- }
185
- }
186
-
187
- return !this.isRunning;
188
- }
189
-
190
- /**
191
- * Slifers quit method
192
- */
193
- quit() {
194
- libttf.symbols.TTF_CloseFont(Global.ptrFont);
195
- libsdl.symbols.SDL_DestroyRenderer(Global.ptrRenderer);
196
- libsdl.symbols.SDL_DestroyWindow(Global.ptrWindow);
197
- libsdl.symbols.SDL_Quit();
198
- }
199
-
200
- getVersion() {
201
- return version;
202
- }
203
- }
204
-
205
- export class Vector2 {
206
- x;
207
- y;
208
-
209
- constructor(x: number, y: number) {
210
- this.x = x;
211
- this.y = y;
212
- }
213
- }
214
-
215
- export class Rectangle {
216
- private readonly pointer;
217
- readonly x;
218
- readonly y;
219
- readonly width;
220
- readonly height;
221
-
222
- constructor(x: number, y: number, width: number, height: number) {
223
- const arr = new Uint32Array(4);
224
- arr[0] = x;
225
- arr[1] = y;
226
- arr[2] = width;
227
- arr[3] = height;
228
- this.pointer = ptr(arr);
229
- this.x = x;
230
- this.y = y;
231
- this.width = width;
232
- this.height = height;
233
- }
234
- }
1
+ import { libimage, libsdl, libttf } from "./ffi";
2
+ import Global from "./global";
3
+ import { ptr } from "bun:ffi";
4
+ import Graphics from "./modules/graphics";
5
+ import Keyboard from "./modules/keyboard";
6
+ import Mouse from "./modules/mouse";
7
+ import { version } from "../package.json";
8
+
9
+ //@ts-expect-error
10
+ const fontFile = await import("./Jost-Bold.ttf");
11
+
12
+ /** @internal */
13
+ class Window {
14
+ public width: number;
15
+ public height: number;
16
+ public title: string;
17
+
18
+ constructor(title: string, width: number, height: number) {
19
+ this.width = width;
20
+ this.height = height;
21
+ this.title = title;
22
+ }
23
+
24
+ setSize(width: number, height: number): void {
25
+ libsdl.symbols.SDL_SetWindowSize(Global.ptrWindow, width, height);
26
+ }
27
+
28
+ setTitle(title: string): void {
29
+ libsdl.symbols.SDL_SetWindowTitle(
30
+ Global.ptrWindow,
31
+ //@ts-expect-error Buffer and CString differences
32
+ Buffer.from(title + "\x00")
33
+ );
34
+ }
35
+
36
+ setFullscreen(flag: boolean): void {
37
+ libsdl.symbols.SDL_SetWindowFullscreen(Global.ptrWindow, Number(flag));
38
+ }
39
+
40
+ centerWindow(): void {
41
+ libsdl.symbols.SDL_SetWindowPosition(
42
+ Global.ptrWindow,
43
+ 0x2fff0000,
44
+ 0x2fff0000
45
+ );
46
+ }
47
+
48
+ setPosition(x: number, y: number): void {
49
+ libsdl.symbols.SDL_SetWindowPosition(Global.ptrWindow, x, y);
50
+ }
51
+ }
52
+
53
+ /** @interal */
54
+ export class SliferClass {
55
+ private isRunning: boolean = true;
56
+ private lastFrame: number = 0;
57
+ private firstFrame: number = 0;
58
+
59
+ // Modules
60
+ Graphics = new Graphics();
61
+ Keyboard = new Keyboard();
62
+ Mouse = new Mouse();
63
+
64
+ // Public Variables
65
+ public dt: number = 0;
66
+
67
+ constructor() {
68
+ const baseInit = libsdl.symbols.SDL_Init(0x00000020);
69
+ if (baseInit != 0) throw `SDL failed to initialize`;
70
+
71
+ const imageInit = libimage.symbols.IMG_Init(3);
72
+ if (imageInit != 3) throw `SDL Image failed to initialize`;
73
+
74
+ const ttfInit = libttf.symbols.TTF_Init();
75
+ if (ttfInit != 0) throw `SDL TTF failed to initialize`;
76
+
77
+ /*
78
+ if (process.platform == "darwin") {
79
+ const tempFont = libttf.symbols.TTF_OpenFont(
80
+ Buffer.from("/System/Library/Fonts/SFNSMono.ttf"),
81
+ 12,
82
+ );
83
+ if (tempFont == null) throw `Default font loading failed`;
84
+ Global.ptrFont = tempFont;
85
+ } else if (process.platform == "linux") {
86
+ const tempFont = libttf.symbols.TTF_OpenFont(
87
+ Buffer.from("/usr/share/fonts/truetype/dejavu/DejaVuSansMono.ttf"),
88
+ 12,
89
+ );
90
+ if (tempFont == null) throw `Default font loading failed`;
91
+ Global.ptrFont = tempFont;
92
+ }
93
+ */
94
+
95
+ const tempFont = libttf.symbols.TTF_OpenFont(
96
+ //@ts-expect-error Buffer and CString differences
97
+ Buffer.from(fontFile.default),
98
+ 24
99
+ );
100
+ if (tempFont == null) throw `Default font loading failed`;
101
+ Global.ptrFont = tempFont;
102
+ }
103
+
104
+ /**
105
+ * @param title Title of window
106
+ * @param width Width of window
107
+ * @param height Height of window
108
+ */
109
+ createWindow(title: string, width: number, height: number): Window {
110
+ // Creating cstring buffer from string
111
+ const _title = Buffer.from(title + "\x00");
112
+
113
+ // Creating window pointer
114
+ const _win = libsdl.symbols.SDL_CreateWindow(
115
+ //@ts-expect-error Buffer and CString differences
116
+ _title,
117
+ 0x2fff0000,
118
+ 0x2fff0000,
119
+ width,
120
+ height,
121
+ 0
122
+ );
123
+ if (_win == null) throw `Window creation failed`;
124
+ Global.ptrWindow = _win;
125
+
126
+ // Creating renderer pointer
127
+ const vsyncHint = 0x00000004;
128
+ const _ren = libsdl.symbols.SDL_CreateRenderer(Global.ptrWindow, -1, vsyncHint);
129
+ if (_ren == null) throw `Renderer Creation failed`;
130
+ Global.ptrRenderer = _ren;
131
+
132
+ this.firstFrame = Number(libsdl.symbols.SDL_GetPerformanceCounter());
133
+
134
+ return new Window(title, width, height);
135
+ }
136
+
137
+ /**
138
+ * @returns if the window should close
139
+ */
140
+ shouldClose(): boolean {
141
+ // Clear the renderer
142
+ libsdl.symbols.SDL_RenderClear(Global.ptrRenderer);
143
+
144
+ // Setup deltatime
145
+ this.lastFrame = this.firstFrame;
146
+ this.firstFrame = Number(libsdl.symbols.SDL_GetPerformanceCounter());
147
+
148
+ this.dt = ((this.firstFrame - this.lastFrame) * 1000 / Number(libsdl.symbols.SDL_GetPerformanceFrequency()));
149
+
150
+
151
+ // Poll Events
152
+ const eventArray = new Uint16Array(32);
153
+ const isEvent = libsdl.symbols.SDL_PollEvent(ptr(eventArray));
154
+
155
+ if (isEvent) {
156
+ switch (eventArray[0]) {
157
+ // Quit event
158
+ case 256:
159
+ this.isRunning = false;
160
+ break;
161
+ // Keydown event
162
+ case 768:
163
+ const _dscancode = eventArray[8];
164
+ const _dkey = libsdl.symbols.SDL_GetKeyFromScancode(_dscancode);
165
+ const _dname = libsdl.symbols.SDL_GetKeyName(_dkey);
166
+ Keyboard.setKeyDown(_dname.toString().toLowerCase());
167
+ break;
168
+ // Keyup event
169
+ case 769:
170
+ const _uscancode = eventArray[8];
171
+ const _ukey = libsdl.symbols.SDL_GetKeyFromScancode(_uscancode);
172
+ const _uname = libsdl.symbols.SDL_GetKeyName(_ukey);
173
+ Keyboard.setKeyUp(_uname.toString().toLowerCase());
174
+ break;
175
+ // Mouse down event
176
+ case 1025:
177
+ const _dbtn = eventArray[8] - 256;
178
+ Mouse.setButtonDown(_dbtn);
179
+ break;
180
+ // Mouse up event
181
+ case 1026:
182
+ const _ubtn = eventArray[8];
183
+ Mouse.setButtonUp(_ubtn);
184
+ break;
185
+ }
186
+ }
187
+
188
+ return !this.isRunning;
189
+ }
190
+
191
+ /**
192
+ * Slifers quit method
193
+ */
194
+ quit() {
195
+ libttf.symbols.TTF_CloseFont(Global.ptrFont);
196
+ libsdl.symbols.SDL_DestroyRenderer(Global.ptrRenderer);
197
+ libsdl.symbols.SDL_DestroyWindow(Global.ptrWindow);
198
+ libsdl.symbols.SDL_Quit();
199
+ }
200
+
201
+ getVersion() {
202
+ return version;
203
+ }
204
+ }
205
+
206
+ export class Rectangle {
207
+ private readonly pointer;
208
+ readonly x;
209
+ readonly y;
210
+ readonly width;
211
+ readonly height;
212
+
213
+ constructor(x: number, y: number, width: number, height: number) {
214
+ const arr = new Uint32Array(4);
215
+ arr[0] = x;
216
+ arr[1] = y;
217
+ arr[2] = width;
218
+ arr[3] = height;
219
+ this.pointer = ptr(arr);
220
+ this.x = x;
221
+ this.y = y;
222
+ this.width = width;
223
+ this.height = height;
224
+ }
225
+ }