slifer 0.1.4 → 0.1.6

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,179 @@
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
+ //@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
+ }
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
+ }