slifer 0.2.2 → 0.2.3
Sign up to get free protection for your applications and to get access to all the features.
- package/bun.lockb +0 -0
- package/index.ts +8 -1
- package/libs/libSDL2_mixer.dll +0 -0
- package/package.json +1 -1
- package/src/engine/image.ts +28 -0
- package/src/engine/rectangle.ts +16 -4
- package/src/engine/time.ts +68 -23
- package/src/engine/vector.ts +1 -3
- package/src/engine/window.ts +1 -1
- package/src/ffi.ts +14 -3
- package/src/modules/audio.ts +1 -1
- package/src/modules/graphics.ts +15 -24
- package/src/modules/keyboard.ts +39 -53
- package/src/modules/mouse.ts +1 -1
- package/src/slifer.ts +18 -17
- package/Sir-BC_stop.png +0 -0
- package/sample.wav +0 -0
package/bun.lockb
ADDED
Binary file
|
package/index.ts
CHANGED
@@ -1,4 +1,11 @@
|
|
1
1
|
import { SliferClass } from "./src/slifer";
|
2
2
|
|
3
|
+
export { Image } from './src/engine/image';
|
4
|
+
export { Vector2 } from './src/engine/vector';
|
5
|
+
export { Rectangle } from './src/engine/rectangle';
|
6
|
+
export { Timer } from './src/engine/time';
|
7
|
+
export { AudioSource } from './src/modules/audio'
|
8
|
+
|
3
9
|
const Slifer = new SliferClass();
|
4
|
-
export default Slifer;
|
10
|
+
export default Slifer;
|
11
|
+
|
Binary file
|
package/package.json
CHANGED
@@ -0,0 +1,28 @@
|
|
1
|
+
import { type Pointer, ptr } from 'bun:ffi';
|
2
|
+
import { libsdl } from '../ffi';
|
3
|
+
import { Vector2 } from './vector';
|
4
|
+
|
5
|
+
export class Image {
|
6
|
+
public readonly pointer: Pointer;
|
7
|
+
public readonly size: Vector2;
|
8
|
+
public flipH: boolean = false;
|
9
|
+
|
10
|
+
constructor(texture: Pointer) {
|
11
|
+
this.pointer = texture;
|
12
|
+
|
13
|
+
const _wArr = new Uint32Array(1);
|
14
|
+
const _hArr = new Uint32Array(1);
|
15
|
+
|
16
|
+
libsdl.symbols.SDL_QueryTexture(
|
17
|
+
texture,
|
18
|
+
null,
|
19
|
+
null,
|
20
|
+
ptr(_wArr),
|
21
|
+
ptr(_hArr)
|
22
|
+
);
|
23
|
+
|
24
|
+
this.size = new Vector2(_wArr[0], _hArr[0]);
|
25
|
+
}
|
26
|
+
|
27
|
+
}
|
28
|
+
|
package/src/engine/rectangle.ts
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
import { ptr } from "bun:ffi";
|
2
|
-
import Vector2 from "./vector";
|
2
|
+
import { Vector2 } from "./vector";
|
3
3
|
|
4
|
-
class Rectangle {
|
5
|
-
|
4
|
+
export class Rectangle {
|
5
|
+
public readonly pointer;
|
6
6
|
public position: Vector2;
|
7
7
|
public size: Vector2;
|
8
8
|
|
@@ -20,6 +20,18 @@ class Rectangle {
|
|
20
20
|
static empty() {
|
21
21
|
return new Rectangle(new Vector2(0, 0), new Vector2(0, 0));
|
22
22
|
}
|
23
|
+
|
24
|
+
public isColliding(rectangle: Rectangle) : boolean {
|
25
|
+
if (
|
26
|
+
this.position.x < rectangle.position.x + rectangle.size.x &&
|
27
|
+
this.position.x + this.size.x > rectangle.position.x &&
|
28
|
+
this.position.y < rectangle.position.y + rectangle.size.y &&
|
29
|
+
this.position.y + this.size.y > rectangle.position.y
|
30
|
+
) {
|
31
|
+
return true;
|
32
|
+
}
|
33
|
+
|
34
|
+
return false;
|
35
|
+
}
|
23
36
|
}
|
24
37
|
|
25
|
-
export default Rectangle;
|
package/src/engine/time.ts
CHANGED
@@ -1,34 +1,79 @@
|
|
1
1
|
import { libsdl } from "../ffi";
|
2
2
|
|
3
|
-
class
|
4
|
-
static #instance: Time;
|
3
|
+
export class Timer {
|
5
4
|
|
6
|
-
|
7
|
-
|
5
|
+
private mStartTicks;
|
6
|
+
private mPausedTicks;
|
7
|
+
private mPaused;
|
8
|
+
private mStarted;
|
9
|
+
|
10
|
+
|
11
|
+
constructor() {
|
12
|
+
this.mStartTicks = 0;
|
13
|
+
this.mPausedTicks = 0;
|
14
|
+
this.mPaused = false;
|
15
|
+
this.mStarted = false;
|
16
|
+
}
|
17
|
+
|
18
|
+
public start() {
|
19
|
+
this.mStarted = true;
|
8
20
|
|
9
|
-
|
21
|
+
this.mPaused = false;
|
10
22
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
}
|
23
|
+
this.mStartTicks = libsdl.symbols.SDL_GetTicks();
|
24
|
+
this.mPausedTicks = 0;
|
25
|
+
}
|
15
26
|
|
16
|
-
|
17
|
-
|
27
|
+
public stop() {
|
28
|
+
this.mStarted = false;
|
29
|
+
this.mPaused = true;
|
18
30
|
|
19
|
-
|
20
|
-
|
21
|
-
|
31
|
+
this.mStartTicks = 0;
|
32
|
+
this.mPausedTicks = 0;
|
33
|
+
}
|
22
34
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
const deltaTime =
|
27
|
-
((this.firstFrame - this.lastframe) * 1000) /
|
28
|
-
Number(libsdl.symbols.SDL_GetPerformanceFrequency());
|
35
|
+
public pause() {
|
36
|
+
if (this.mStarted && !this.mPaused) {
|
37
|
+
this.mPaused = true;
|
29
38
|
|
30
|
-
|
31
|
-
|
39
|
+
this.mPausedTicks = libsdl.symbols.SDL_GetTicks() - this.mStartTicks;
|
40
|
+
this.mStartTicks = 0;
|
41
|
+
}
|
42
|
+
}
|
43
|
+
|
44
|
+
public unpause() {
|
45
|
+
if (this.mStarted && this.mPaused) {
|
46
|
+
this.mPaused = false;
|
47
|
+
|
48
|
+
this.mStartTicks = libsdl.symbols.SDL_GetTicks() - this.mPausedTicks;
|
49
|
+
this.mPausedTicks = 0;
|
50
|
+
}
|
51
|
+
}
|
52
|
+
|
53
|
+
public getTicks() {
|
54
|
+
let time = 0;
|
55
|
+
|
56
|
+
if (this.mStarted) {
|
57
|
+
if (this.mPaused) {
|
58
|
+
time = this.mPausedTicks;
|
59
|
+
} else {
|
60
|
+
time = libsdl.symbols.SDL_GetTicks() - this.mStartTicks;
|
61
|
+
}
|
62
|
+
}
|
63
|
+
|
64
|
+
return time;
|
65
|
+
}
|
66
|
+
|
67
|
+
public isStarted() {
|
68
|
+
return this.mStarted;
|
69
|
+
}
|
70
|
+
|
71
|
+
public isPaused() {
|
72
|
+
return (this.mPaused && this.mStarted);
|
73
|
+
}
|
74
|
+
|
75
|
+
|
76
|
+
|
77
|
+
|
32
78
|
}
|
33
79
|
|
34
|
-
export default Time;
|
package/src/engine/vector.ts
CHANGED
package/src/engine/window.ts
CHANGED
package/src/ffi.ts
CHANGED
@@ -12,6 +12,8 @@ if (process.platform == "win32") {
|
|
12
12
|
libImageImport = await import("../libs/libSDL2_image.dll");
|
13
13
|
//@ts-expect-error
|
14
14
|
libTTFImport = await import("../libs/libSDL2_ttf.dll");
|
15
|
+
//@ts-expect-error
|
16
|
+
libMixerImport = await import("../libs/libSDL2_mixer.dll");
|
15
17
|
} else if (process.platform == "darwin") {
|
16
18
|
//@ts-expect-error
|
17
19
|
libSDLImport = await import("../libs/libSDL2.dylib");
|
@@ -30,7 +32,6 @@ if (process.platform == "win32") {
|
|
30
32
|
libTTFImport = await import("../libs/libSDL2_ttf.so");
|
31
33
|
//@ts-expect-error
|
32
34
|
libMixerImport = await import("../libs/libSDL2_mixer.so");
|
33
|
-
|
34
35
|
}
|
35
36
|
|
36
37
|
/** @internal */
|
@@ -206,10 +207,20 @@ export const libsdl = dlopen(libSDLImport.default, {
|
|
206
207
|
args: ["pointer", "cstring"],
|
207
208
|
returns: "void",
|
208
209
|
},
|
209
|
-
|
210
|
-
|
210
|
+
SDL_GetTicks: {
|
211
|
+
returns: "uint32_t",
|
212
|
+
},
|
213
|
+
SDL_Delay: {
|
214
|
+
args: ["uint32_t"],
|
215
|
+
},
|
216
|
+
SDL_GetKeyboardState: {
|
217
|
+
args: ["pointer"],
|
211
218
|
returns: "pointer",
|
212
219
|
},
|
220
|
+
SDL_GetScancodeFromName: {
|
221
|
+
args: ["cstring"],
|
222
|
+
returns: "int",
|
223
|
+
},
|
213
224
|
});
|
214
225
|
|
215
226
|
/** @internal */
|
package/src/modules/audio.ts
CHANGED
package/src/modules/graphics.ts
CHANGED
@@ -1,8 +1,9 @@
|
|
1
|
-
import { libimage, libsdl
|
1
|
+
import { libimage, libsdl } from "../ffi";
|
2
2
|
import { type Pointer, ptr } from "bun:ffi";
|
3
|
-
import
|
3
|
+
import { Image } from "../engine/image";
|
4
|
+
import { Rectangle } from "../engine/rectangle";
|
4
5
|
import Color from "../color";
|
5
|
-
import Vector2 from "../engine/vector";
|
6
|
+
import { Vector2 } from "../engine/vector";
|
6
7
|
import Renderer from "../engine/renderer";
|
7
8
|
|
8
9
|
class Graphics {
|
@@ -109,7 +110,8 @@ class Graphics {
|
|
109
110
|
image: Image,
|
110
111
|
position: Vector2,
|
111
112
|
rotation?: number,
|
112
|
-
scale?: Vector2
|
113
|
+
scale?: Vector2,
|
114
|
+
flipH?: boolean
|
113
115
|
) {
|
114
116
|
// Define destination rect
|
115
117
|
const dstRect = new Uint32Array(4);
|
@@ -126,30 +128,19 @@ class Graphics {
|
|
126
128
|
ptr(dstRect),
|
127
129
|
rotation ? rotation : 0,
|
128
130
|
null,
|
129
|
-
|
131
|
+
flipH ? Number(flipH) : 0
|
130
132
|
);
|
131
133
|
}
|
132
|
-
}
|
133
|
-
|
134
|
-
class Image {
|
135
|
-
public readonly pointer: Pointer;
|
136
|
-
public readonly size: Vector2;
|
137
|
-
|
138
|
-
constructor(texture: Pointer) {
|
139
|
-
this.pointer = texture;
|
140
|
-
|
141
|
-
const _wArr = new Uint32Array(1);
|
142
|
-
const _hArr = new Uint32Array(1);
|
143
134
|
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
135
|
+
public drawRect(rectangle: Rectangle, color: Color) {
|
136
|
+
libsdl.symbols.SDL_SetRenderDrawColor(
|
137
|
+
Renderer.pointer,
|
138
|
+
color.r,
|
139
|
+
color.g,
|
140
|
+
color.b,
|
141
|
+
color.a
|
150
142
|
);
|
151
|
-
|
152
|
-
this.size = new Vector2(_wArr[0], _hArr[0]);
|
143
|
+
libsdl.symbols.SDL_RenderFillRect(Renderer.pointer, rectangle.pointer);
|
153
144
|
}
|
154
145
|
}
|
155
146
|
|
package/src/modules/keyboard.ts
CHANGED
@@ -1,12 +1,12 @@
|
|
1
1
|
import { libsdl } from "../ffi";
|
2
|
+
import { toArrayBuffer } from "bun:ffi";
|
2
3
|
|
3
4
|
/** @internal */
|
4
5
|
class Keyboard {
|
5
6
|
static #instance: Keyboard;
|
6
7
|
|
7
|
-
static
|
8
|
-
static
|
9
|
-
static releasedKeyMap = new Map<string, boolean>();
|
8
|
+
static keyState: DataView;
|
9
|
+
static pressMap = new Map<string, number>();
|
10
10
|
|
11
11
|
private constructor() {}
|
12
12
|
|
@@ -27,63 +27,49 @@ class Keyboard {
|
|
27
27
|
return keyName;
|
28
28
|
}
|
29
29
|
|
30
|
-
static
|
31
|
-
const
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
if (_state == undefined) return false;
|
50
|
-
|
51
|
-
return _state;
|
30
|
+
static getStates() {
|
31
|
+
const state = libsdl.symbols.SDL_GetKeyboardState(null);
|
32
|
+
if (state == null) return;
|
33
|
+
const myArr = new DataView(toArrayBuffer(state, 0, 512));
|
34
|
+
|
35
|
+
for (let i = 0; i < 512; ++i) {
|
36
|
+
if (myArr.getUint8(i) == 1) {
|
37
|
+
const keyName = this.convertScancodeToKey(i).toLowerCase();
|
38
|
+
const kmGet = this.pressMap.get(keyName);
|
39
|
+
if (kmGet == undefined || kmGet == 0) {
|
40
|
+
this.pressMap.set(keyName, 1);
|
41
|
+
} else if (kmGet == 1) {
|
42
|
+
this.pressMap.set(keyName, 2);
|
43
|
+
}
|
44
|
+
} else if (myArr.getUint8(i) == 0) {
|
45
|
+
const keyName = this.convertScancodeToKey(i).toLowerCase();
|
46
|
+
this.pressMap.set(keyName, 0);
|
47
|
+
}
|
48
|
+
}
|
52
49
|
}
|
53
50
|
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
if (
|
64
|
-
|
65
|
-
Keyboard.pressedKeyMap.set(key, true);
|
66
|
-
return true;
|
67
|
-
}
|
51
|
+
public isPressed(key: keys) {
|
52
|
+
/*
|
53
|
+
const scancode = libsdl.symbols.SDL_GetScancodeFromName(
|
54
|
+
Buffer.from(key + "\x00")
|
55
|
+
);
|
56
|
+
const thisval = Keyboard.keyState.getUint8(scancode);
|
57
|
+
*/
|
58
|
+
|
59
|
+
const kmGet = Keyboard.pressMap.get(key);
|
60
|
+
if (kmGet == 1) {
|
61
|
+
return true;
|
68
62
|
}
|
69
63
|
|
64
|
+
|
70
65
|
return false;
|
71
66
|
}
|
72
67
|
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
isReleased(key: keys) {
|
79
|
-
const _releasedState = Keyboard.releasedKeyMap.get(key);
|
80
|
-
const _downState = Keyboard.downKeyMap.get(key);
|
81
|
-
|
82
|
-
if (_downState == false) {
|
83
|
-
if (_releasedState == false || undefined) {
|
84
|
-
Keyboard.releasedKeyMap.set(key, true);
|
85
|
-
return true;
|
86
|
-
}
|
68
|
+
|
69
|
+
public isDown(key: keys) {
|
70
|
+
const kmGet = Keyboard.pressMap.get(key);
|
71
|
+
if (kmGet == 1 || kmGet == 2) {
|
72
|
+
return true;
|
87
73
|
}
|
88
74
|
|
89
75
|
return false;
|
package/src/modules/mouse.ts
CHANGED
package/src/slifer.ts
CHANGED
@@ -5,9 +5,9 @@ import Mouse from "./modules/mouse";
|
|
5
5
|
import Audio from "./modules/audio";
|
6
6
|
import Window from "./engine/window";
|
7
7
|
import Renderer from "./engine/renderer";
|
8
|
-
import Vector2 from "./engine/vector";
|
9
|
-
import
|
10
|
-
import { ptr } from "bun:ffi";
|
8
|
+
import { Vector2 } from "./engine/vector";
|
9
|
+
import { Timer } from "./engine/time";
|
10
|
+
import { ptr, toArrayBuffer } from "bun:ffi";
|
11
11
|
import { initLibraries } from "./engine";
|
12
12
|
import { version } from "../package.json";
|
13
13
|
|
@@ -23,8 +23,13 @@ export class SliferClass {
|
|
23
23
|
public dt: number = 0;
|
24
24
|
public isRunning: boolean = true;
|
25
25
|
|
26
|
+
private fps = 60;
|
27
|
+
private ticksPerFrame = 1000 / this.fps;
|
28
|
+
private capTimer: Timer;
|
29
|
+
|
26
30
|
constructor() {
|
27
31
|
initLibraries();
|
32
|
+
this.capTimer = new Timer();
|
28
33
|
}
|
29
34
|
|
30
35
|
/**
|
@@ -40,9 +45,6 @@ export class SliferClass {
|
|
40
45
|
// Create the renderer
|
41
46
|
Renderer.createRenderer();
|
42
47
|
|
43
|
-
// Start delta time calculations
|
44
|
-
Time.instance.init();
|
45
|
-
|
46
48
|
// Return the window object
|
47
49
|
return window;
|
48
50
|
}
|
@@ -51,11 +53,13 @@ export class SliferClass {
|
|
51
53
|
* @returns if the window should close
|
52
54
|
*/
|
53
55
|
shouldClose(): boolean {
|
56
|
+
this.capTimer.start();
|
57
|
+
|
54
58
|
// Clear the renderer
|
55
59
|
Renderer.clear();
|
56
60
|
|
57
61
|
// Calculate delta time
|
58
|
-
this.dt = Time.instance.calcDelta();
|
62
|
+
// this.dt = Time.instance.calcDelta();
|
59
63
|
|
60
64
|
// Poll Events
|
61
65
|
const eventArray = new Uint16Array(32);
|
@@ -67,16 +71,6 @@ export class SliferClass {
|
|
67
71
|
case 256:
|
68
72
|
this.isRunning = false;
|
69
73
|
break;
|
70
|
-
// Keydown event
|
71
|
-
case 768:
|
72
|
-
var scancode = eventArray[8];
|
73
|
-
Keyboard.setKeyDown(scancode);
|
74
|
-
break;
|
75
|
-
// Keyup event
|
76
|
-
case 769:
|
77
|
-
var scancode = eventArray[8];
|
78
|
-
Keyboard.setKeyUp(scancode);
|
79
|
-
break;
|
80
74
|
// Mouse down event
|
81
75
|
case 1025:
|
82
76
|
const _dbtn = eventArray[8] - 256;
|
@@ -90,6 +84,13 @@ export class SliferClass {
|
|
90
84
|
}
|
91
85
|
}
|
92
86
|
|
87
|
+
Keyboard.getStates();
|
88
|
+
|
89
|
+
const frameTicks = this.capTimer.getTicks();
|
90
|
+
if (frameTicks < this.ticksPerFrame) {
|
91
|
+
libsdl.symbols.SDL_Delay(this.ticksPerFrame - frameTicks);
|
92
|
+
}
|
93
|
+
|
93
94
|
return !this.isRunning;
|
94
95
|
}
|
95
96
|
|
package/Sir-BC_stop.png
DELETED
Binary file
|
package/sample.wav
DELETED
Binary file
|