slifer 0.1.9 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -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;
@@ -0,0 +1,8 @@
1
+ import { describe, expect, it } from "bun:test";
2
+ import { initLibraries } from "./engine";
3
+
4
+ describe("Initializing ", () => {
5
+ it("Should initialize without error ", () => {
6
+ expect(() => initLibraries()).not.toThrow(Error);
7
+ });
8
+ });
package/src/slifer.ts CHANGED
@@ -1,225 +1,175 @@
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
- }
1
+ import { libimage, libsdl, libttf } from "./ffi";
2
+ import { initLibraries } from "./engine";
3
+ import Global from "./global";
4
+ import { ptr } from "bun:ffi";
5
+ import Graphics from "./modules/graphics";
6
+ import Keyboard from "./modules/keyboard";
7
+ import Mouse from "./modules/mouse";
8
+ import { version } from "../package.json";
9
+
10
+ /** @internal */
11
+ class Window {
12
+ public width: number;
13
+ public height: number;
14
+ public title: string;
15
+
16
+ constructor(title: string, width: number, height: number) {
17
+ this.width = width;
18
+ this.height = height;
19
+ this.title = title;
20
+ }
21
+
22
+ setSize(width: number, height: number): void {
23
+ libsdl.symbols.SDL_SetWindowSize(Global.ptrWindow, width, height);
24
+ }
25
+
26
+ setTitle(title: string): void {
27
+ libsdl.symbols.SDL_SetWindowTitle(
28
+ Global.ptrWindow,
29
+ Buffer.from(title + "\x00")
30
+ );
31
+ }
32
+
33
+ setFullscreen(flag: boolean): void {
34
+ libsdl.symbols.SDL_SetWindowFullscreen(Global.ptrWindow, Number(flag));
35
+ }
36
+
37
+ centerWindow(): void {
38
+ libsdl.symbols.SDL_SetWindowPosition(
39
+ Global.ptrWindow,
40
+ 0x2fff0000,
41
+ 0x2fff0000
42
+ );
43
+ }
44
+
45
+ setPosition(x: number, y: number): void {
46
+ libsdl.symbols.SDL_SetWindowPosition(Global.ptrWindow, x, y);
47
+ }
48
+ }
49
+
50
+ /** @interal */
51
+ export class SliferClass {
52
+ private isRunning: boolean = true;
53
+ private lastFrame: number = 0;
54
+ private firstFrame: number = 0;
55
+
56
+ // Modules
57
+ Graphics = new Graphics();
58
+ Keyboard = new Keyboard();
59
+ Mouse = new Mouse();
60
+
61
+ // Public Variables
62
+ public dt: number = 0;
63
+
64
+ constructor() {
65
+ initLibraries();
66
+ }
67
+
68
+ /**
69
+ * @param title Title of window
70
+ * @param width Width of window
71
+ * @param height Height of window
72
+ */
73
+ createWindow(title: string, width: number, height: number): Window {
74
+ // Creating cstring buffer from string
75
+ const _title = Buffer.from(title + "\x00");
76
+
77
+ // Creating window pointer
78
+ const _win = libsdl.symbols.SDL_CreateWindow(
79
+ _title,
80
+ 0x2fff0000,
81
+ 0x2fff0000,
82
+ width,
83
+ height,
84
+ 0
85
+ );
86
+ if (_win == null) throw `Window creation failed`;
87
+ Global.ptrWindow = _win;
88
+
89
+ // Creating renderer pointer
90
+ const vsyncHint = 0x00000004;
91
+ const _ren = libsdl.symbols.SDL_CreateRenderer(
92
+ Global.ptrWindow,
93
+ -1,
94
+ vsyncHint
95
+ );
96
+ if (_ren == null) throw `Renderer Creation failed`;
97
+ Global.ptrRenderer = _ren;
98
+
99
+ this.firstFrame = Number(libsdl.symbols.SDL_GetPerformanceCounter());
100
+
101
+ return new Window(title, width, height);
102
+ }
103
+
104
+ /**
105
+ * @returns if the window should close
106
+ */
107
+ shouldClose(): boolean {
108
+ // Clear the renderer
109
+ libsdl.symbols.SDL_RenderClear(Global.ptrRenderer);
110
+
111
+ // Setup deltatime
112
+ this.lastFrame = this.firstFrame;
113
+ this.firstFrame = Number(libsdl.symbols.SDL_GetPerformanceCounter());
114
+
115
+ this.dt =
116
+ ((this.firstFrame - this.lastFrame) * 1000) /
117
+ Number(libsdl.symbols.SDL_GetPerformanceFrequency());
118
+
119
+ // Poll Events
120
+ const eventArray = new Uint16Array(32);
121
+ const isEvent = libsdl.symbols.SDL_PollEvent(ptr(eventArray));
122
+
123
+ if (isEvent) {
124
+ switch (eventArray[0]) {
125
+ // Quit event
126
+ case 256:
127
+ this.isRunning = false;
128
+ break;
129
+ // Keydown event
130
+ case 768:
131
+ const _dscancode = eventArray[8];
132
+ const _dkey =
133
+ libsdl.symbols.SDL_GetKeyFromScancode(_dscancode);
134
+ const _dname = libsdl.symbols.SDL_GetKeyName(_dkey);
135
+ Keyboard.setKeyDown(_dname.toString().toLowerCase());
136
+ break;
137
+ // Keyup event
138
+ case 769:
139
+ const _uscancode = eventArray[8];
140
+ const _ukey =
141
+ libsdl.symbols.SDL_GetKeyFromScancode(_uscancode);
142
+ const _uname = libsdl.symbols.SDL_GetKeyName(_ukey);
143
+ Keyboard.setKeyUp(_uname.toString().toLowerCase());
144
+ break;
145
+ // Mouse down event
146
+ case 1025:
147
+ const _dbtn = eventArray[8] - 256;
148
+ Mouse.setButtonDown(_dbtn);
149
+ break;
150
+ // Mouse up event
151
+ case 1026:
152
+ const _ubtn = eventArray[8];
153
+ Mouse.setButtonUp(_ubtn);
154
+ break;
155
+ }
156
+ }
157
+
158
+ return !this.isRunning;
159
+ }
160
+
161
+ /**
162
+ * Slifers quit method
163
+ */
164
+ quit() {
165
+ libttf.symbols.TTF_CloseFont(Global.ptrFont);
166
+ libsdl.symbols.SDL_DestroyRenderer(Global.ptrRenderer);
167
+ libsdl.symbols.SDL_DestroyWindow(Global.ptrWindow);
168
+ libsdl.symbols.SDL_Quit();
169
+ }
170
+
171
+ getVersion() {
172
+ return version;
173
+ }
174
+ }
175
+
package/tsconfig.json CHANGED
@@ -1,28 +1,28 @@
1
- {
2
- "compilerOptions": {
3
- // Enable latest features
4
- "lib": ["ESNext", "DOM"],
5
- "target": "ESNext",
6
- "module": "ESNext",
7
- "moduleDetection": "force",
8
- "jsx": "react-jsx",
9
- "allowJs": true,
10
-
11
- // Bundler mode
12
- "moduleResolution": "bundler",
13
- "allowImportingTsExtensions": true,
14
- "verbatimModuleSyntax": true,
15
- "noEmit": true,
16
-
17
- // Best practices
18
- "strict": true,
19
- "skipLibCheck": true,
20
- "noFallthroughCasesInSwitch": true,
21
- "stripInternal": true,
22
-
23
- // Some stricter flags (disabled by default)
24
- "noUnusedLocals": false,
25
- "noUnusedParameters": false,
26
- "noPropertyAccessFromIndexSignature": false
27
- }
28
- }
1
+ {
2
+ "compilerOptions": {
3
+ // Enable latest features
4
+ "lib": ["ESNext", "DOM"],
5
+ "target": "ESNext",
6
+ "module": "ESNext",
7
+ "moduleDetection": "force",
8
+ "jsx": "react-jsx",
9
+ "allowJs": true,
10
+
11
+ // Bundler mode
12
+ "moduleResolution": "bundler",
13
+ "allowImportingTsExtensions": true,
14
+ "verbatimModuleSyntax": true,
15
+ "noEmit": true,
16
+
17
+ // Best practices
18
+ "strict": true,
19
+ "skipLibCheck": true,
20
+ "noFallthroughCasesInSwitch": true,
21
+ "stripInternal": true,
22
+
23
+ // Some stricter flags (disabled by default)
24
+ "noUnusedLocals": false,
25
+ "noUnusedParameters": false,
26
+ "noPropertyAccessFromIndexSignature": false
27
+ }
28
+ }