58 lines
1.3 KiB
JavaScript
58 lines
1.3 KiB
JavaScript
const imageboy = require("imageboy");
|
|
const fastify = require("fastify")();
|
|
const path = require("path");
|
|
const fs = require("fs");
|
|
let lastFrame = "";
|
|
let emulator;
|
|
const mainHTML = fs.readFileSync(path.join(__dirname, "html", "gb.html"), "utf8");
|
|
const KEYMAP = {
|
|
RIGHT: 0,
|
|
LEFT: 1,
|
|
UP: 2,
|
|
DOWN: 3,
|
|
A: 4,
|
|
B: 5,
|
|
SELECT: 6,
|
|
START: 7
|
|
};
|
|
|
|
async function run() {
|
|
if (!fs.existsSync(path.join(__dirname, "rom.gb"))) {
|
|
console.log("rom.gb missing.")
|
|
return;
|
|
}
|
|
emulator = imageboy.run({
|
|
path: "./rom.gb",
|
|
interval: 16.6666667,
|
|
onFrame: function(frame) {
|
|
if (lastFrame !== frame) {
|
|
lastFrame = frame;
|
|
}
|
|
}
|
|
});
|
|
|
|
fastify.get("/", async(request, reply) => {
|
|
reply.type("text/html");
|
|
reply.send(mainHTML);
|
|
});
|
|
|
|
fastify.get("/image", async(request, reply) => {
|
|
reply.type("image/png").send(lastFrame);
|
|
});
|
|
|
|
fastify.get("/control", async(request, reply) => {
|
|
const button = request.query.button;
|
|
if (button) {
|
|
const mappedButton = KEYMAP[button.toUpperCase()];
|
|
if (mappedButton) emulator.pressKeys(emulator.gameboy, [mappedButton]);
|
|
}
|
|
});
|
|
|
|
try {
|
|
await fastify.listen({ port: 3000 });
|
|
} catch (err) {
|
|
console.log(err);
|
|
}
|
|
}
|
|
|
|
run(); |