slifer 0.1.4 → 0.1.5

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