@bloopjs/engine 0.0.0
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.
- package/dist/engine.d.ts +46 -0
- package/dist/engine.d.ts.map +1 -0
- package/dist/engine.js +1185 -0
- package/dist/engine.js.map +14 -0
- package/dist/events.d.ts +75 -0
- package/dist/events.d.ts.map +1 -0
- package/dist/inputs.d.ts +459 -0
- package/dist/inputs.d.ts.map +1 -0
- package/dist/mod.d.ts +37 -0
- package/dist/mod.d.ts.map +1 -0
- package/dist/mod.js +1115 -0
- package/dist/mod.js.map +12 -0
- package/dist/runtime.d.ts +31 -0
- package/dist/runtime.d.ts.map +1 -0
- package/dist/timing.d.ts +11 -0
- package/dist/timing.d.ts.map +1 -0
- package/dist/wasmEngine.d.ts +13 -0
- package/dist/wasmEngine.d.ts.map +1 -0
- package/js/codegen/enums.ts +223 -0
- package/js/contexts/inputContext.ts +745 -0
- package/js/contexts/timeContext.ts +31 -0
- package/js/engine.ts +34 -0
- package/js/inputs.ts +47 -0
- package/js/wasmEngine.ts +47 -0
- package/package.json +37 -0
- package/wasm/bloop.wasm +0 -0
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
export class TimeContext {
|
|
2
|
+
dataView?: DataView;
|
|
3
|
+
|
|
4
|
+
constructor(dataView?: DataView) {
|
|
5
|
+
this.dataView = dataView;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
/** The number of fixed timestep frames processed */
|
|
9
|
+
get frame(): number {
|
|
10
|
+
if (!this.dataView) {
|
|
11
|
+
throw new Error("TimeContext DataView is not initialized");
|
|
12
|
+
}
|
|
13
|
+
return this.dataView.getUint32(0, true);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** The number of seconds since the last frame */
|
|
17
|
+
get dt(): number {
|
|
18
|
+
if (!this.dataView) {
|
|
19
|
+
throw new Error("TimeContext DataView is not initialized");
|
|
20
|
+
}
|
|
21
|
+
return this.dataView.getUint32(4, true) / 1000;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** The total number of seconds since the engine started */
|
|
25
|
+
get time(): number {
|
|
26
|
+
if (!this.dataView) {
|
|
27
|
+
throw new Error("TimeContext DataView is not initialized");
|
|
28
|
+
}
|
|
29
|
+
return this.dataView.getUint32(8, true) / 1000;
|
|
30
|
+
}
|
|
31
|
+
}
|
package/js/engine.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
export * as Enums from "./codegen/enums";
|
|
2
|
+
export * from "./contexts/inputContext";
|
|
3
|
+
export * from "./contexts/timeContext";
|
|
4
|
+
export * from "./inputs";
|
|
5
|
+
export type * from "./wasmEngine";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* A pointer to shared wasm memory space. Use it like
|
|
9
|
+
*
|
|
10
|
+
* const dataView = new DataView(wasmMemory.buffer, ptr);
|
|
11
|
+
*/
|
|
12
|
+
export type EnginePointer = number;
|
|
13
|
+
export type EngineLen = number;
|
|
14
|
+
/**
|
|
15
|
+
* A return value indicating success.
|
|
16
|
+
*
|
|
17
|
+
* 0 = success
|
|
18
|
+
* 1 = failure
|
|
19
|
+
* other values are function specific error codes
|
|
20
|
+
*/
|
|
21
|
+
export type EngineOk = number;
|
|
22
|
+
|
|
23
|
+
export const DEFAULT_WASM_URL = new URL("../wasm/bloop.wasm", import.meta.url);
|
|
24
|
+
|
|
25
|
+
export const TIME_CTX_OFFSET = 0;
|
|
26
|
+
export const INPUT_CTX_OFFSET = TIME_CTX_OFFSET + 4;
|
|
27
|
+
export const EVENTS_OFFSET = INPUT_CTX_OFFSET + 4;
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Size of snapshot header in bytes
|
|
31
|
+
*/
|
|
32
|
+
export const SNAPSHOT_HEADER_LEN = 16;
|
|
33
|
+
export const SNAPSHOT_HEADER_USER_LEN_OFFSET = 4;
|
|
34
|
+
export const SNAPSHOT_HEADER_ENGINE_LEN_OFFSET = 8;
|
package/js/inputs.ts
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import * as Enums from "./codegen/enums";
|
|
2
|
+
|
|
3
|
+
// todo - move magic numbers to codegen
|
|
4
|
+
export const MOUSE_OFFSET = 256;
|
|
5
|
+
export const MOUSE_BUTTONS_OFFSET = 16;
|
|
6
|
+
export const KEYBOARD_OFFSET = 0;
|
|
7
|
+
|
|
8
|
+
export const EVENT_PAYLOAD_SIZE = 8;
|
|
9
|
+
export const EVENT_PAYLOAD_ALIGN = 4;
|
|
10
|
+
|
|
11
|
+
export type MouseButton = keyof typeof Enums.MouseButton;
|
|
12
|
+
export type Key = keyof typeof Enums.Key;
|
|
13
|
+
|
|
14
|
+
export function keyToKeyCode(key: Key): Enums.Key {
|
|
15
|
+
return Enums.Key[key];
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function keyCodeToKey(code: Enums.Key | number): Key {
|
|
19
|
+
return Enums.Key[code] as Key;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function mouseButtonToMouseButtonCode(
|
|
23
|
+
button: MouseButton,
|
|
24
|
+
): Enums.MouseButton {
|
|
25
|
+
return Enums.MouseButton[button];
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function mouseButtonCodeToMouseButton(
|
|
29
|
+
code: Enums.MouseButton | number,
|
|
30
|
+
): MouseButton {
|
|
31
|
+
return Enums.MouseButton[code] as MouseButton;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export type KeyState = {
|
|
35
|
+
down: boolean;
|
|
36
|
+
held: boolean;
|
|
37
|
+
up: boolean;
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
export type MouseState = {
|
|
41
|
+
left: KeyState;
|
|
42
|
+
middle: KeyState;
|
|
43
|
+
right: KeyState;
|
|
44
|
+
wheel: { x: number; y: number };
|
|
45
|
+
x: number;
|
|
46
|
+
y: number;
|
|
47
|
+
};
|
package/js/wasmEngine.ts
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import type * as Enums from "./codegen/enums";
|
|
2
|
+
import type { EngineOk, EnginePointer } from "./engine";
|
|
3
|
+
|
|
4
|
+
export type WasmEngine = {
|
|
5
|
+
initialize: () => void;
|
|
6
|
+
alloc: (size: number) => EnginePointer;
|
|
7
|
+
free: (ptr: EnginePointer, size: number) => void;
|
|
8
|
+
register_systems: (cb_handle: number) => void;
|
|
9
|
+
|
|
10
|
+
get_time_ctx: () => EnginePointer;
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Step forward one simulation frame
|
|
14
|
+
*/
|
|
15
|
+
step: (ms: number) => void;
|
|
16
|
+
/**
|
|
17
|
+
* Seek to a specific frame number
|
|
18
|
+
*/
|
|
19
|
+
seek: (frame: number) => void;
|
|
20
|
+
/**
|
|
21
|
+
* Start recording inputs to tape
|
|
22
|
+
*/
|
|
23
|
+
start_recording: (data_len: number, max_events: number) => EngineOk;
|
|
24
|
+
/**
|
|
25
|
+
* Whether the engine is currently recording to tape
|
|
26
|
+
*/
|
|
27
|
+
is_recording: () => boolean;
|
|
28
|
+
/**
|
|
29
|
+
* Whether the engine is currently replaying from tape
|
|
30
|
+
*/
|
|
31
|
+
is_replaying: () => boolean;
|
|
32
|
+
|
|
33
|
+
// Input platform events
|
|
34
|
+
emit_keydown: (key: Enums.Key) => void;
|
|
35
|
+
emit_keyup: (key: Enums.Key) => void;
|
|
36
|
+
emit_mousedown: (button: Enums.MouseButton) => void;
|
|
37
|
+
emit_mouseup: (button: Enums.MouseButton) => void;
|
|
38
|
+
emit_mousemove: (x: number, y: number) => void;
|
|
39
|
+
emit_mousewheel: (x: number, y: number) => void;
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Returns a pointer to the snapshot data.
|
|
43
|
+
*/
|
|
44
|
+
take_snapshot: (data_len: number) => EnginePointer;
|
|
45
|
+
/** Restores the engine state from a snapshot */
|
|
46
|
+
restore: (ptr: EnginePointer) => void;
|
|
47
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@bloopjs/engine",
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "https://github.com/bloopgames/bloop"
|
|
8
|
+
},
|
|
9
|
+
"publishConfig": {
|
|
10
|
+
"access": "public"
|
|
11
|
+
},
|
|
12
|
+
"files": [
|
|
13
|
+
"dist",
|
|
14
|
+
"js",
|
|
15
|
+
"wasm"
|
|
16
|
+
],
|
|
17
|
+
"exports": {
|
|
18
|
+
".": {
|
|
19
|
+
"import": "./js/engine.ts",
|
|
20
|
+
"types": "./js/engine.ts"
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
"scripts": {
|
|
24
|
+
"build": "bun run build:wasm && bun build js/engine.ts --outdir=dist --sourcemap=linked && bun run build:tsc",
|
|
25
|
+
"build:tsc": "tsc -p publish/tsconfig.publish.json",
|
|
26
|
+
"build:wasm": "zig build -p .",
|
|
27
|
+
"ci:tsc": "tsc --noEmit",
|
|
28
|
+
"check:wasm": "wasm-objdump -x wasm/bloop.wasm",
|
|
29
|
+
"dev": "zig build -p . --watch"
|
|
30
|
+
},
|
|
31
|
+
"devDependencies": {
|
|
32
|
+
"@types/bun": "latest"
|
|
33
|
+
},
|
|
34
|
+
"peerDependencies": {
|
|
35
|
+
"typescript": "^5"
|
|
36
|
+
}
|
|
37
|
+
}
|
package/wasm/bloop.wasm
ADDED
|
Binary file
|