progress!

This commit is contained in:
dylan
2024-03-29 02:02:40 -07:00
parent 2721848afa
commit d4d436f34a
13 changed files with 178 additions and 4628 deletions

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,29 @@
// @ts-ignore
import renderCart from "./build/rawRenderCart.js";
type PicoBool = 0 | 1;
type RenderCart = (Module: {canvas: HTMLCanvasElement}, cartNames: string[], cartDatas: number[][]) => {
p8_touch_detected?: PicoBool;
p8_dropped_cart?: string;
p8_dropped_cart_name?: string;
pico8_state?: Partial<{
frame_number: number;
has_focus: PicoBool;
is_paused: PicoBool;
request_pointer_lock: PicoBool;
require_page_navigate_confirmation: PicoBool;
show_dpad: PicoBool;
shutdown_requested: PicoBool;
sound_volume: number;
}>;
pico8_buttons?: [number, number, number, number, number, number, number, number];
pico8_gamepads?: {count: number};
pico8_gpio?: number[]; // should be 128 length
pico8_audio_context?: AudioContext;
pico8_mouse?: [number, number, number];
}
const typedRenderCart = renderCart as RenderCart;
export {typedRenderCart as renderCart}

View File

@ -0,0 +1,126 @@
import { renderCart as rawRenderCart } from "./rawRenderCart";
type PicoCart = {
name: string;
data: string; // TODO: ideally, accept png data url as well (or even just actual url?)
}
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)
// audio
setAudioContext: (audioContext: AudioContext) => void;
// 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;
}
const imageToNumbers = async (url: string): Promise<number[]> => {
return [];
}
const bitfield = (...args: boolean[]): number => {
if (!args.length) {
return 0;
}
return (args[0]?1:0)+2*bitfield(...args.slice(1));
}
export const makePlayer = async (carts: PicoCart[]): Promise<PicoPlayerHandle> => {
const canvas = document.createElement("canvas");
canvas.style.imageRendering = "pixelated";
const Module = {canvas};
const handle = rawRenderCart(Module, carts.map(cart => cart.name), await Promise.all(carts.map(cart => imageToNumbers(cart.data))));
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
handle.p8_dropped_cart = cart.data;
},
setAudioContext(audioContext) {
handle.pico8_audio_context = audioContext;
}
}
}

File diff suppressed because one or more lines are too long

View File

@ -24,15 +24,15 @@ const execPico = async (args: string[]) => {
}
export const pico8 = {
async export(fileIn: string, fileOut: string) {
async export(filesIn: string[], fileOut: string) {
try {
// console.log((await cmd("ls", ["-la", "/app/pico8"]) as any).stdout)
return await execPico([fileIn, "-export", fileOut]);
return await execPico([...filesIn, "-export", fileOut]);
} catch (err) {
console.log("CAUGHT ERROR", err);
}
}
}
// const result = await pico8.export("/home/dylan/.lexaloffle/pico-8/carts/my-pico-project/mygame.p8", "/home/dylan/repos/picobook/sample.p8.png");
// const result = await pico8.export(["/home/dylan/.lexaloffle/pico-8/carts/my-pico-project/mygame.p8","/home/dylan/.lexaloffle/pico-8/carts/my-pico-project/secondcart.p8"], "/home/dylan/repos/picobook/sample2.js");
// console.log(result);