slifer 0.1.2 → 0.1.4

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,100 +1,102 @@
1
- import { libsdl } from "../ffi";
2
- import { ptr } from 'bun:ffi';
3
-
4
- class Mouse {
5
-
6
- static downKeyMap = new Map<string, boolean>();
7
- static pressedKeyMap = new Map<string, boolean>();
8
- static releasedKeyMap = new Map<string, boolean>();
9
-
10
- static setButtonDown(button: number) {
11
-
12
- let key : string = '';
13
- if (button == 1) {
14
- key = "left";
15
- } else if (button == 2) {
16
- key = "middle";
17
- } else {
18
- key = "right";
19
- }
20
-
21
- this.downKeyMap.set(key, true);
22
- this.releasedKeyMap.set(key, false);
23
- }
24
-
25
- static setButtonUp(button: number) {
26
-
27
- let key : string = '';
28
- if (button == 1) {
29
- key = "left";
30
- } else if (button == 2) {
31
- key = "middle";
32
- } else {
33
- key = "right";
34
- }
35
-
36
- this.downKeyMap.set(key, false);
37
- this.pressedKeyMap.set(key, false);
38
- }
39
-
40
- /**
41
- *
42
- * @param button string of button
43
- * @returns if the button is being held down
44
- */
45
- isDown(button: buttons) {
46
- const _state = Mouse.downKeyMap.get(button);
47
- if (_state == undefined) return false
48
-
49
- return _state;
50
- }
51
-
52
- /**
53
- *
54
- * @param button string of button
55
- * @returns if button is pressed. Returns only once
56
- */
57
- isPressed(button: buttons) {
58
- const _pressedState = Mouse.pressedKeyMap.get(button);
59
- const _downState = Mouse.downKeyMap.get(button);
60
-
61
- if (_downState == true) {
62
- if (_pressedState == false || _pressedState == undefined) {
63
- Mouse.pressedKeyMap.set(button, true);
64
- return true;
65
- }
66
- }
67
-
68
- return false;
69
- }
70
-
71
- /**
72
- *
73
- * @param button string of button
74
- * @returns if button is released. Returns only once
75
- */
76
- isReleased(button: buttons) {
77
- const _releasedState = Mouse.releasedKeyMap.get(button);
78
- const _downState = Mouse.downKeyMap.get(button);
79
-
80
- if (_downState == false) {
81
- if (_releasedState == false || undefined) {
82
- Mouse.releasedKeyMap.set(button, true);
83
- return true;
84
- }
85
- }
86
-
87
- return false;
88
- }
89
-
90
- getPosition() {
91
- const xArr = new Uint32Array(1);
92
- const yArr = new Uint32Array(1);
93
- libsdl.symbols.SDL_GetMouseState(ptr(xArr), ptr(yArr));
94
- return {x: xArr[0], y: yArr[0]};
95
- }
96
- }
97
-
98
- type buttons = 'left' | 'middle' | 'right';
99
-
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 */
100
102
  export default Mouse;
package/src/slifer.ts CHANGED
@@ -1,115 +1,132 @@
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
-
8
- export class SliferClass {
9
-
10
- isRunning : boolean = true;
11
-
12
- // Modules
13
- Graphics = new Graphics();
14
- Keyboard = new Keyboard();
15
- Mouse = new Mouse();
16
-
17
- constructor() {
18
- const baseInit = libsdl.symbols.SDL_Init(0x00000020);
19
- if (baseInit != 0) throw `SDL failed to initialize`;
20
-
21
- const imageInit = libimage.symbols.IMG_Init(3);
22
- if (imageInit != 3) throw `SDL Image failed to initialize`;
23
-
24
- const ttfInit = libttf.symbols.TTF_Init();
25
- if (ttfInit != 0) throw `SDL TTF failed to initialize`;
26
-
27
- if (process.platform == "darwin") {
28
- const tempFont = libttf.symbols.TTF_OpenFont(Buffer.from("/System/Library/Fonts/SFNSMono.ttf"), 12);
29
- if (tempFont == null) throw `Default font loading failed`;
30
- Global.ptrFont = tempFont;
31
- }
32
-
33
- }
34
-
35
- /**
36
- *
37
- * @param title Title of window
38
- * @param width Width of window
39
- * @param height Height of window
40
- */
41
- createWindow(title: string, width: number, height: number) : void {
42
- // Creating cstring buffer from string
43
- const _title = Buffer.from(title + "\x00");
44
-
45
- // Creating window pointer
46
- const _win = libsdl.symbols.SDL_CreateWindow(_title, 0x2FFF0000, 0x2FFF0000, width, height, 0);
47
- if (_win == null) throw `Window creation failed`;
48
- Global.ptrWindow = _win;
49
-
50
- // Creating renderer pointer
51
- const _ren = libsdl.symbols.SDL_CreateRenderer(Global.ptrWindow, -1, 0);
52
- if (_ren == null) throw `Renderer Creation failed`;
53
- Global.ptrRenderer = _ren;
54
-
55
- }
56
-
57
- /**
58
- *
59
- * @returns if the window should close
60
- */
61
- shouldClose() : boolean {
62
-
63
- // Clear the renderer
64
- libsdl.symbols.SDL_RenderClear(Global.ptrRenderer);
65
-
66
- // Poll Events
67
- const eventArray = new Uint16Array(32);
68
- const isEvent = libsdl.symbols.SDL_PollEvent(ptr(eventArray));
69
-
70
- if (isEvent) {
71
- switch (eventArray[0]) {
72
- // Quit event
73
- case 256:
74
- this.isRunning = false;
75
- break;
76
- // Keydown event
77
- case 768:
78
- const _dscancode = eventArray[8];
79
- const _dkey = libsdl.symbols.SDL_GetKeyFromScancode(_dscancode);
80
- const _dname = libsdl.symbols.SDL_GetKeyName(_dkey);
81
- Keyboard.setKeyDown(_dname.toString().toLowerCase());
82
- break;
83
- // Keyup event
84
- case 769:
85
- const _uscancode = eventArray[8];
86
- const _ukey = libsdl.symbols.SDL_GetKeyFromScancode(_uscancode);
87
- const _uname = libsdl.symbols.SDL_GetKeyName(_ukey);
88
- Keyboard.setKeyUp(_uname.toString().toLowerCase());
89
- break;
90
- // Mouse down event
91
- case 1025:
92
- const _dbtn = eventArray[8] - 256;
93
- Mouse.setButtonDown(_dbtn);
94
- break;
95
- // Mouse up event
96
- case 1026:
97
- const _ubtn = eventArray[8];
98
- Mouse.setButtonUp(_ubtn);
99
- break;
100
- }
101
- }
102
-
103
- return !this.isRunning;
104
- }
105
-
106
- /**
107
- * Slifers quit method
108
- */
109
- quit() {
110
- libttf.symbols.TTF_CloseFont(Global.ptrFont);
111
- libsdl.symbols.SDL_DestroyRenderer(Global.ptrRenderer);
112
- libsdl.symbols.SDL_DestroyWindow(Global.ptrWindow);
113
- libsdl.symbols.SDL_Quit();
114
- }
115
- }
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
+ /** @interal */
10
+ export class SliferClass {
11
+ isRunning: boolean = true;
12
+
13
+ // Modules
14
+ Graphics = new Graphics();
15
+ Keyboard = new Keyboard();
16
+ Mouse = new Mouse();
17
+
18
+ constructor() {
19
+ const baseInit = libsdl.symbols.SDL_Init(0x00000020);
20
+ if (baseInit != 0) throw `SDL failed to initialize`;
21
+
22
+ const imageInit = libimage.symbols.IMG_Init(3);
23
+ if (imageInit != 3) throw `SDL Image failed to initialize`;
24
+
25
+ const ttfInit = libttf.symbols.TTF_Init();
26
+ if (ttfInit != 0) throw `SDL TTF failed to initialize`;
27
+
28
+ if (process.platform == "darwin") {
29
+ const tempFont = libttf.symbols.TTF_OpenFont(
30
+ Buffer.from("/System/Library/Fonts/SFNSMono.ttf"),
31
+ 12,
32
+ );
33
+ if (tempFont == null) throw `Default font loading failed`;
34
+ Global.ptrFont = tempFont;
35
+ } else if (process.platform == "linux") {
36
+ const tempFont = libttf.symbols.TTF_OpenFont(
37
+ Buffer.from("/usr/share/fonts/truetype/dejavu/DejaVuSansMono.ttf"),
38
+ 12,
39
+ );
40
+ if (tempFont == null) throw `Default font loading failed`;
41
+ Global.ptrFont = tempFont;
42
+ }
43
+ }
44
+
45
+ /**
46
+ * @param title Title of window
47
+ * @param width Width of window
48
+ * @param height Height of window
49
+ */
50
+ createWindow(title: string, width: number, height: number): void {
51
+ // Creating cstring buffer from string
52
+ const _title = Buffer.from(title + "\x00");
53
+
54
+ // Creating window pointer
55
+ const _win = libsdl.symbols.SDL_CreateWindow(
56
+ _title,
57
+ 0x2FFF0000,
58
+ 0x2FFF0000,
59
+ width,
60
+ height,
61
+ 0,
62
+ );
63
+ if (_win == null) throw `Window creation failed`;
64
+ Global.ptrWindow = _win;
65
+
66
+ // Creating renderer pointer
67
+ const _ren = libsdl.symbols.SDL_CreateRenderer(Global.ptrWindow, -1, 0);
68
+ if (_ren == null) throw `Renderer Creation failed`;
69
+ Global.ptrRenderer = _ren;
70
+ }
71
+
72
+ /**
73
+ * @returns if the window should close
74
+ */
75
+ shouldClose(): boolean {
76
+ // Clear the renderer
77
+ libsdl.symbols.SDL_RenderClear(Global.ptrRenderer);
78
+
79
+ // Poll Events
80
+ const eventArray = new Uint16Array(32);
81
+ const isEvent = libsdl.symbols.SDL_PollEvent(ptr(eventArray));
82
+
83
+ if (isEvent) {
84
+ switch (eventArray[0]) {
85
+ // Quit event
86
+ case 256:
87
+ this.isRunning = false;
88
+ break;
89
+ // Keydown event
90
+ case 768:
91
+ const _dscancode = eventArray[8];
92
+ const _dkey = libsdl.symbols.SDL_GetKeyFromScancode(_dscancode);
93
+ const _dname = libsdl.symbols.SDL_GetKeyName(_dkey);
94
+ Keyboard.setKeyDown(_dname.toString().toLowerCase());
95
+ break;
96
+ // Keyup event
97
+ case 769:
98
+ const _uscancode = eventArray[8];
99
+ const _ukey = libsdl.symbols.SDL_GetKeyFromScancode(_uscancode);
100
+ const _uname = libsdl.symbols.SDL_GetKeyName(_ukey);
101
+ Keyboard.setKeyUp(_uname.toString().toLowerCase());
102
+ break;
103
+ // Mouse down event
104
+ case 1025:
105
+ const _dbtn = eventArray[8] - 256;
106
+ Mouse.setButtonDown(_dbtn);
107
+ break;
108
+ // Mouse up event
109
+ case 1026:
110
+ const _ubtn = eventArray[8];
111
+ Mouse.setButtonUp(_ubtn);
112
+ break;
113
+ }
114
+ }
115
+
116
+ return !this.isRunning;
117
+ }
118
+
119
+ /**
120
+ * Slifers quit method
121
+ */
122
+ quit() {
123
+ libttf.symbols.TTF_CloseFont(Global.ptrFont);
124
+ libsdl.symbols.SDL_DestroyRenderer(Global.ptrRenderer);
125
+ libsdl.symbols.SDL_DestroyWindow(Global.ptrWindow);
126
+ libsdl.symbols.SDL_Quit();
127
+ }
128
+
129
+ getVersion() {
130
+ return version;
131
+ }
132
+ }
package/tsconfig.json CHANGED
@@ -1,27 +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
-
22
- // Some stricter flags (disabled by default)
23
- "noUnusedLocals": false,
24
- "noUnusedParameters": false,
25
- "noPropertyAccessFromIndexSignature": false
26
- }
27
- }
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
+ }