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