Files
picobook/src/client/pico8-client/renderCart.ts

148 lines
4.3 KiB
TypeScript
Raw Normal View History

2024-03-29 22:23:01 -07:00
import { assertNever } from "@firebox/tsutil";
2024-03-29 20:23:14 -07:00
import { pngToRom } from "./pngToRom";
2024-03-29 02:02:40 -07:00
import { renderCart as rawRenderCart } from "./rawRenderCart";
type PicoCart = {
name: string;
2024-03-29 22:23:01 -07:00
src: string;
} | {
name: string;
rom: number[];
2024-03-29 02:02:40 -07:00
}
type PlayerButtons = {
left: boolean;
right: boolean;
up: boolean;
down: boolean;
o: boolean;
x: boolean;
menu: boolean;
}
type PicoPlayerHandle = {
// external things
readonly canvas: HTMLCanvasElement;
// i/o
setButtons: (buttons: PlayerButtons[]) => void;
setMouse: (mouse: {
x: number;
y: number;
leftClick: boolean;
rightClick: boolean;
}) => void;
setGamepadCount: (count: number) => void;
readonly gpio: number[]; // read + write (should be 256-tuple)
// state (all communicated out)
readonly state: {
readonly frameNumber: number;
readonly isPaused: boolean;
readonly hasFocus: boolean;
readonly requestPointerLock: boolean;
readonly requirePageNavigateConfirmation: boolean;
readonly showDpad: boolean;
readonly shutdownRequested: boolean;
readonly soundVolume: number;
};
// misc?
setTouchDetected: (touchDetected: boolean) => void;
dropCart: (cart: PicoCart) => void;
2024-03-29 20:23:14 -07:00
// Module
toggleSound: () => void;
toggleControlMenu: () => void;
togglePaused: () => void;
// TODO: rename these two better (what do they do??)
modDragOver: () => void;
modDragStop: () => void;
2024-03-29 02:02:40 -07:00
}
const bitfield = (...args: boolean[]): number => {
if (!args.length) {
return 0;
}
return (args[0]?1:0)+2*bitfield(...args.slice(1));
}
2024-03-29 22:23:01 -07:00
const getRom = async (cart: PicoCart) => {
if ("src" in cart) {
return await pngToRom(cart.src);
} else if ("rom" in cart) {
return cart.rom;
2024-03-29 20:23:14 -07:00
}
2024-03-29 22:23:01 -07:00
assertNever(cart);
}
2024-03-29 20:23:14 -07:00
2024-03-29 22:23:01 -07:00
export const makePicoConsole = async (props: {
canvas?: HTMLCanvasElement;
audioContext?: AudioContext;
carts: PicoCart[];
}): Promise<PicoPlayerHandle> => {
const {carts, canvas = document.createElement("canvas"), audioContext = new AudioContext()} = props;
canvas.style.imageRendering = "pixelated";
const Module = {canvas};
const cartsDatas = await Promise.all(carts.map(cart => getRom(cart)));
2024-03-29 20:23:14 -07:00
const handle = rawRenderCart(Module, carts.map(cart => cart.name), cartsDatas, audioContext);
2024-03-29 02:02:40 -07:00
handle.pico8_state = {};
handle.pico8_buttons = [0,0,0,0,0,0,0,0];
handle.pico8_mouse = [0,0,0];
handle.pico8_gpio = [
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
];
handle.pico8_gamepads = {count: 0};
return {
canvas,
state: {
frameNumber: handle.pico8_state.frame_number!,
isPaused: !!handle.pico8_state.is_paused!,
hasFocus: !!handle.pico8_state.has_focus!,
requestPointerLock: !!handle.pico8_state.request_pointer_lock!,
requirePageNavigateConfirmation: !!handle.pico8_state.require_page_navigate_confirmation!,
showDpad: !!handle.pico8_state.show_dpad!,
shutdownRequested: !!handle.pico8_state.shutdown_requested!,
soundVolume: handle.pico8_state.sound_volume!,
},
gpio: handle.pico8_gpio,
setMouse({x, y, leftClick, rightClick}) {
handle.pico8_mouse = [x, y, bitfield(leftClick, rightClick)];
},
setButtons(buttons) {
// TODO: pad this properly here instead of casting
handle.pico8_buttons = buttons.map(({left, right, up, down, o, x, menu}) => bitfield(left, right, up, down, o, x, menu)) as any;
},
setGamepadCount(count) {
handle.pico8_gamepads = {count};
},
setTouchDetected(touchDetected) {
handle.p8_touch_detected = touchDetected ? 1 : 0;
},
dropCart(cart) {
handle.p8_dropped_cart_name = cart.name;
// TODO: make sure this is a dataURL first, and if not, load it and then pass it in
2024-03-29 22:23:01 -07:00
// handle.p8_dropped_cart = cart.src;
2024-03-29 20:23:14 -07:00
},
modDragOver: (Module as any).pico8DragOver,
modDragStop: (Module as any).pico8DragStop,
togglePaused: (Module as any).pico8TogglePaused,
toggleSound: (Module as any).pico8ToggleSound,
toggleControlMenu: (Module as any).pico8ToggleControlMenu,
2024-03-29 02:02:40 -07:00
}
}