From 06855542a1ce16c8d244a6cdb81144a74abf3dcc Mon Sep 17 00:00:00 2001 From: HorizonCode Date: Thu, 11 Aug 2022 14:16:02 +0200 Subject: [PATCH] inital commit --- .gitignore | 3 ++ app.js | 58 ++++++++++++++++++++++++++ html/gb.html | 112 +++++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 24 +++++++++++ 4 files changed, 197 insertions(+) create mode 100644 .gitignore create mode 100644 app.js create mode 100644 html/gb.html create mode 100644 package.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0ac136c --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +rom.gb +node_modules/ +pnpm-lock.yaml \ No newline at end of file diff --git a/app.js b/app.js new file mode 100644 index 0000000..3dd5393 --- /dev/null +++ b/app.js @@ -0,0 +1,58 @@ +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(); \ No newline at end of file diff --git a/html/gb.html b/html/gb.html new file mode 100644 index 0000000..b7b0e5b --- /dev/null +++ b/html/gb.html @@ -0,0 +1,112 @@ + + + + + + + +
+ +
+
+ + + + +
+ + + + + + + + + + + + + + + + +
+ + + + +
+ + + + + + + + +
+
+ + + + \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..ae57b18 --- /dev/null +++ b/package.json @@ -0,0 +1,24 @@ +{ + "name": "gbonline", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "start": "node app.js" + }, + "keywords": [], + "author": "", + "license": "ISC", + "dependencies": { + "@tensorflow/tfjs": ">=3.19.0 <4.0.0\n", + "@tensorflow/tfjs-node": "^3.19.0", + "fastify": "^4.4.0", + "fastify-socket.io": "^4.0.0", + "imageboy": "^1.1.1", + "seedrandom": "^3.0.5", + "serverboy": "^0.0.7", + "socket.io": "^4.5.1", + "upscaler": "1.0.0-beta.6", + "waifu2x": "^1.0.9" + } +} \ No newline at end of file