slifer 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -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,126 +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
- isRunning: boolean = true;
10
-
11
- // Modules
12
- Graphics = new Graphics();
13
- Keyboard = new Keyboard();
14
- Mouse = new Mouse();
15
-
16
- constructor() {
17
- const baseInit = libsdl.symbols.SDL_Init(0x00000020);
18
- if (baseInit != 0) throw `SDL failed to initialize`;
19
-
20
- const imageInit = libimage.symbols.IMG_Init(3);
21
- if (imageInit != 3) throw `SDL Image failed to initialize`;
22
-
23
- const ttfInit = libttf.symbols.TTF_Init();
24
- if (ttfInit != 0) throw `SDL TTF failed to initialize`;
25
-
26
- if (process.platform == "darwin") {
27
- const tempFont = libttf.symbols.TTF_OpenFont(
28
- Buffer.from("/System/Library/Fonts/SFNSMono.ttf"),
29
- 12,
30
- );
31
- if (tempFont == null) throw `Default font loading failed`;
32
- Global.ptrFont = tempFont;
33
- } else if (process.platform == "linux") {
34
- const tempFont = libttf.symbols.TTF_OpenFont(
35
- Buffer.from("/usr/share/fonts/truetype/dejavu/DejaVuSansMono.ttf"),
36
- 12,
37
- );
38
- if (tempFont == null) throw `Default font loading failed`;
39
- Global.ptrFont = tempFont;
40
- }
41
- }
42
-
43
- /**
44
- * @param title Title of window
45
- * @param width Width of window
46
- * @param height Height of window
47
- */
48
- createWindow(title: string, width: number, height: number): void {
49
- // Creating cstring buffer from string
50
- const _title = Buffer.from(title + "\x00");
51
-
52
- // Creating window pointer
53
- const _win = libsdl.symbols.SDL_CreateWindow(
54
- _title,
55
- 0x2FFF0000,
56
- 0x2FFF0000,
57
- width,
58
- height,
59
- 0,
60
- );
61
- if (_win == null) throw `Window creation failed`;
62
- Global.ptrWindow = _win;
63
-
64
- // Creating renderer pointer
65
- const _ren = libsdl.symbols.SDL_CreateRenderer(Global.ptrWindow, -1, 0);
66
- if (_ren == null) throw `Renderer Creation failed`;
67
- Global.ptrRenderer = _ren;
68
- }
69
-
70
- /**
71
- * @returns if the window should close
72
- */
73
- shouldClose(): boolean {
74
- // Clear the renderer
75
- libsdl.symbols.SDL_RenderClear(Global.ptrRenderer);
76
-
77
- // Poll Events
78
- const eventArray = new Uint16Array(32);
79
- const isEvent = libsdl.symbols.SDL_PollEvent(ptr(eventArray));
80
-
81
- if (isEvent) {
82
- switch (eventArray[0]) {
83
- // Quit event
84
- case 256:
85
- this.isRunning = false;
86
- break;
87
- // Keydown event
88
- case 768:
89
- const _dscancode = eventArray[8];
90
- const _dkey = libsdl.symbols.SDL_GetKeyFromScancode(_dscancode);
91
- const _dname = libsdl.symbols.SDL_GetKeyName(_dkey);
92
- Keyboard.setKeyDown(_dname.toString().toLowerCase());
93
- break;
94
- // Keyup event
95
- case 769:
96
- const _uscancode = eventArray[8];
97
- const _ukey = libsdl.symbols.SDL_GetKeyFromScancode(_uscancode);
98
- const _uname = libsdl.symbols.SDL_GetKeyName(_ukey);
99
- Keyboard.setKeyUp(_uname.toString().toLowerCase());
100
- break;
101
- // Mouse down event
102
- case 1025:
103
- const _dbtn = eventArray[8] - 256;
104
- Mouse.setButtonDown(_dbtn);
105
- break;
106
- // Mouse up event
107
- case 1026:
108
- const _ubtn = eventArray[8];
109
- Mouse.setButtonUp(_ubtn);
110
- break;
111
- }
112
- }
113
-
114
- return !this.isRunning;
115
- }
116
-
117
- /**
118
- * Slifers quit method
119
- */
120
- quit() {
121
- libttf.symbols.TTF_CloseFont(Global.ptrFont);
122
- libsdl.symbols.SDL_DestroyRenderer(Global.ptrRenderer);
123
- libsdl.symbols.SDL_DestroyWindow(Global.ptrWindow);
124
- libsdl.symbols.SDL_Quit();
125
- }
126
- }
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
+ }