mirror of
https://github.com/exoticorn/microw8.git
synced 2026-01-20 11:16:42 +01:00
add support for load from url
This commit is contained in:
File diff suppressed because one or more lines are too long
114
web/src/main.js
114
web/src/main.js
@@ -24,40 +24,39 @@ let pad = 0;
|
|||||||
let keyHandler = (e) => {
|
let keyHandler = (e) => {
|
||||||
let isKeyDown = e.type == 'keydown';
|
let isKeyDown = e.type == 'keydown';
|
||||||
let mask;
|
let mask;
|
||||||
switch(e.code) {
|
switch (e.code) {
|
||||||
case 'ArrowUp':
|
case 'ArrowUp':
|
||||||
mask = 1;
|
mask = 1;
|
||||||
break;
|
break;
|
||||||
case 'ArrowDown':
|
case 'ArrowDown':
|
||||||
mask = 2;
|
mask = 2;
|
||||||
break;
|
break;
|
||||||
case 'ArrowLeft':
|
case 'ArrowLeft':
|
||||||
mask = 4;
|
mask = 4;
|
||||||
break;
|
break;
|
||||||
case 'ArrowRight':
|
case 'ArrowRight':
|
||||||
mask = 8;
|
mask = 8;
|
||||||
break;
|
break;
|
||||||
case 'KeyZ':
|
case 'KeyZ':
|
||||||
mask = 16;
|
mask = 16;
|
||||||
break;
|
break;
|
||||||
case 'KeyX':
|
case 'KeyX':
|
||||||
mask = 32;
|
mask = 32;
|
||||||
break;
|
break;
|
||||||
case 'KeyA':
|
case 'KeyA':
|
||||||
mask = 64;
|
mask = 64;
|
||||||
break;
|
break;
|
||||||
case 'KeyS':
|
case 'KeyS':
|
||||||
mask = 128;
|
mask = 128;
|
||||||
break;
|
break;
|
||||||
case 'KeyR':
|
case 'KeyR':
|
||||||
if(isKeyDown)
|
if (isKeyDown) {
|
||||||
{
|
runModule(currentData);
|
||||||
runModule(currentData);
|
}
|
||||||
}
|
break;
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if(isKeyDown) {
|
if (isKeyDown) {
|
||||||
pad |= mask;
|
pad |= mask;
|
||||||
} else {
|
} else {
|
||||||
pad &= ~mask;
|
pad &= ~mask;
|
||||||
@@ -66,7 +65,7 @@ let keyHandler = (e) => {
|
|||||||
window.onkeydown = keyHandler;
|
window.onkeydown = keyHandler;
|
||||||
window.onkeyup = keyHandler;
|
window.onkeyup = keyHandler;
|
||||||
|
|
||||||
async function runModule(data) {
|
async function runModule(data, keepUrl) {
|
||||||
if (cancelFunction) {
|
if (cancelFunction) {
|
||||||
cancelFunction();
|
cancelFunction();
|
||||||
cancelFunction = null;
|
cancelFunction = null;
|
||||||
@@ -82,15 +81,16 @@ async function runModule(data) {
|
|||||||
currentData = data;
|
currentData = data;
|
||||||
|
|
||||||
let newURL = window.location.pathname;
|
let newURL = window.location.pathname;
|
||||||
if (cartridgeSize <= 1024) {
|
if (cartridgeSize <= 1024 && !keepUrl) {
|
||||||
let dataString = '';
|
let dataString = '';
|
||||||
for (let byte of U8(data)) {
|
for (let byte of U8(data)) {
|
||||||
dataString += String.fromCharCode(byte);
|
dataString += String.fromCharCode(byte);
|
||||||
}
|
}
|
||||||
newURL += '#' + btoa(dataString);
|
newURL += '#' + btoa(dataString);
|
||||||
}
|
|
||||||
if (newURL != window.location.pathname + window.location.hash) {
|
if (newURL != window.location.pathname + window.location.hash) {
|
||||||
history.pushState(null, null, newURL);
|
history.pushState(null, null, newURL);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
screen.width = screen.width;
|
screen.width = screen.width;
|
||||||
@@ -139,7 +139,7 @@ async function runModule(data) {
|
|||||||
|
|
||||||
let platform_instance = await loadModuleURL(platformUrl);
|
let platform_instance = await loadModuleURL(platformUrl);
|
||||||
|
|
||||||
for(let name in platform_instance.exports) {
|
for (let name in platform_instance.exports) {
|
||||||
importObject.env[name] = platform_instance.exports[name]
|
importObject.env[name] = platform_instance.exports[name]
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -163,29 +163,29 @@ async function runModule(data) {
|
|||||||
try {
|
try {
|
||||||
let now = Date.now();
|
let now = Date.now();
|
||||||
let restart = false;
|
let restart = false;
|
||||||
if(now >= nextFrame) {
|
if (now >= nextFrame) {
|
||||||
let gamepads = navigator.getGamepads();
|
let gamepads = navigator.getGamepads();
|
||||||
let gamepad = 0;
|
let gamepad = 0;
|
||||||
for(let i = 0; i < 4; ++i) {
|
for (let i = 0; i < 4; ++i) {
|
||||||
let pad = gamepads[i];
|
let pad = gamepads[i];
|
||||||
if(!pad) {
|
if (!pad) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
for(let j = 0; j < 8; ++j) {
|
for (let j = 0; j < 8; ++j) {
|
||||||
let buttonIdx = (j+12) % 16;
|
let buttonIdx = (j + 12) % 16;
|
||||||
if(pad.buttons.length > buttonIdx && pad.buttons[buttonIdx].pressed) {
|
if (pad.buttons.length > buttonIdx && pad.buttons[buttonIdx].pressed) {
|
||||||
gamepad |= 1 << (i * 8 + j);
|
gamepad |= 1 << (i * 8 + j);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(pad.axes.length > 1) {
|
if (pad.axes.length > 1) {
|
||||||
for(let j = 0; j < 4; ++j) {
|
for (let j = 0; j < 4; ++j) {
|
||||||
let v = pad.axes[1-(j>>1)];
|
let v = pad.axes[1 - (j >> 1)];
|
||||||
if(((j & 1) ? v : -v) > 0.5) {
|
if (((j & 1) ? v : -v) > 0.5) {
|
||||||
gamepad |= 1 << (i * 8 + j);
|
gamepad |= 1 << (i * 8 + j);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(pad.buttons.length > 9 && pad.buttons[9].pressed) {
|
if (pad.buttons.length > 9 && pad.buttons[9].pressed) {
|
||||||
restart = true;
|
restart = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -204,7 +204,7 @@ async function runModule(data) {
|
|||||||
nextFrame = Math.max(nextFrame + timePerFrame, now);
|
nextFrame = Math.max(nextFrame + timePerFrame, now);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(restart) {
|
if (restart) {
|
||||||
runModule(currentData);
|
runModule(currentData);
|
||||||
} else {
|
} else {
|
||||||
window.requestAnimationFrame(mainloop);
|
window.requestAnimationFrame(mainloop);
|
||||||
@@ -220,14 +220,18 @@ async function runModule(data) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function runModuleFromURL(url) {
|
async function runModuleFromURL(url, keepUrl) {
|
||||||
runModule(await (await fetch(url)).arrayBuffer());
|
runModule(await (await fetch(url)).arrayBuffer(), keepUrl);
|
||||||
}
|
}
|
||||||
|
|
||||||
function runModuleFromHash() {
|
function runModuleFromHash() {
|
||||||
let base64Data = window.location.hash.slice(1);
|
let hash = window.location.hash.slice(1);
|
||||||
if (base64Data.length > 0) {
|
if (hash.length > 0) {
|
||||||
runModuleFromURL('data:;base64,' + base64Data);
|
if (hash.startsWith("url=")) {
|
||||||
|
runModuleFromURL(hash.slice(4), true);
|
||||||
|
} else {
|
||||||
|
runModuleFromURL('data:;base64,' + hash);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
runModule(new ArrayBuffer(0));
|
runModule(new ArrayBuffer(0));
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user