Skip to content
Snippets Groups Projects
index.ts 39.1 KiB
Newer Older
  • Learn to ignore specific revisions
  •         this.crop();
        }
    
        /**
         * Restore the emulator's viewport to the minimal size, should make all
         * the other emulator's meta-information (info, buttons, etc.) visible.
         */
        minimize() {
            const canvasContainer = document.getElementById("canvas-container")!;
            const engineCanvas = document.getElementById("engine-canvas")!;
            canvasContainer.classList.remove("fullscreen");
            engineCanvas.style.width = "";
            engineCanvas.style.height = "";
            window.removeEventListener("resize", this.crop);
        }
    
        crop() {
            // @todo let's make this more flexible
            const engineCanvas = document.getElementById("engine-canvas")!;
    
            // calculates the window ratio as this is fundamental to
            // determine the proper way to crop the fullscreen
            const windowRatio = window.innerWidth / window.innerHeight;
    
            // in case the window is wider (more horizontal than the base ratio)
            // this means that we must crop horizontally
            if (windowRatio > DISPLAY_RATIO) {
                engineCanvas.style.width = `${
                    window.innerWidth * (DISPLAY_RATIO / windowRatio)
                }px`;
                engineCanvas.style.height = `${window.innerHeight}px`;
            } else {
                engineCanvas.style.width = `${window.innerWidth}px`;
                engineCanvas.style.height = `${
                    window.innerHeight * (windowRatio / DISPLAY_RATIO)
                }px`;
            }
        }
    
        async fetchRom(romPath: string): Promise<[string, Uint8Array]> {
            // extracts the name of the ROM from the provided
            // path by splitting its structure
            const romPathS = romPath.split(/\//g);
            let romName = romPathS[romPathS.length - 1].split("?")[0];
            const romNameS = romName.split(/\./g);
            romName = `${romNameS[0]}.${romNameS[romNameS.length - 1]}`;
    
            // loads the ROM data and converts it into the
            // target byte array buffer (to be used by WASM)
            const response = await fetch(ROM_PATH);
            const blob = await response.blob();
            const arrayBuffer = await blob.arrayBuffer();
            const romData = new Uint8Array(arrayBuffer);
    
            // returns both the name of the ROM and the data
            // contents as a byte array
            return [romName, romData];
        }
    }
    
    type Global = {
        modalCallback: Function | null;
    
    //@todo check if this is really required
    const global: Global = {
        modalCallback: null
    
    declare global {
        interface Window {
            panic: (message: string) => void;
    
    window.panic = (message: string) => {
        console.error(message);
    
    const wasm = async () => {
        await _wasm();
        GameBoy.set_panic_hook_ws();
    
        startApp("app", BACKGROUNDS);
    
    
        const emulator = new Emulator();
        await emulator.main();