Skip to content

Commit 594e355

Browse files
committed
Add DoomCanvas component
1 parent fea59ea commit 594e355

File tree

14 files changed

+101
-4
lines changed

14 files changed

+101
-4
lines changed
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

public/websockets-doom.wasm.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/App.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import GameContextProvider from "./context/GameContextProvider";
88
const queryClient = new QueryClient();
99

1010
export default function App() {
11-
const [isGameStarted, setIsGameStarted] = useState(false);
11+
const [isGameStarted, setIsGameStarted] = useState(true);
1212

1313
return (
1414
<QueryClientProvider client={queryClient}>
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import React, { useEffect, useRef } from "react";
2+
import { EmscriptenModule } from "../../types";
3+
4+
const DoomCanvas: React.FC = () => {
5+
const canvasRef = useRef<HTMLCanvasElement>(null);
6+
7+
useEffect(() => {
8+
const canvas = canvasRef.current;
9+
10+
if (!canvas) {
11+
console.error("Canvas element not found.");
12+
return;
13+
}
14+
15+
const handleContextLost = (e: Event) => {
16+
alert("WebGL context lost. You will need to reload the page.");
17+
e.preventDefault();
18+
};
19+
20+
canvas.addEventListener("webglcontextlost", handleContextLost, false);
21+
22+
// Setup configuration for doom-wasm
23+
const Module: EmscriptenModule = {
24+
noInitialRun: true,
25+
preRun: function () {
26+
const files = [
27+
"doom1.wad",
28+
"freedoom2.wad",
29+
"default.cfg",
30+
"Cardano.wad",
31+
];
32+
files.forEach((file) => {
33+
Module.FS!.createPreloadedFile("/", file, file, true, true);
34+
});
35+
},
36+
printErr: console.error,
37+
postRun: () => {},
38+
canvas: canvas,
39+
print: (text: string) => {
40+
console.log("stdout:", text);
41+
},
42+
setStatus: (text: string) => {
43+
console.log("setStatus:", text);
44+
},
45+
};
46+
47+
// Attach Module to the window object to make it globally accessible
48+
window.Module = Module;
49+
50+
// Dynamically load websockets-doom.js
51+
const script = document.createElement("script");
52+
script.src = "/websockets-doom.js";
53+
document.body.appendChild(script);
54+
55+
return () => {
56+
canvas.removeEventListener("webglcontextlost", handleContextLost);
57+
document.body.removeChild(script);
58+
};
59+
}, []);
60+
61+
return <canvas id="canvas" ref={canvasRef} className="w-full h-full" />;
62+
};
63+
64+
export default DoomCanvas;

src/components/DoomCanvas/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { default } from "./DoomCanvas";

src/components/GameView/GameView.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Card from "../Card";
2+
import DoomCanvas from "../DoomCanvas";
23
import GlobalTotals from "../GlobalTotals";
34
import GlobalTPS from "../GlobalTPS";
45
import HydraHeadLiveTxs from "../HydraHeadLiveTxs";
@@ -30,7 +31,9 @@ const GameView = () => {
3031
/>
3132
<HydraHeadLiveTxs />
3233
</div>
33-
<Card className="h-[40rem]">dsad</Card>
34+
<Card className="h-[40rem]">
35+
<DoomCanvas />
36+
</Card>
3437
<div className="w-80 flex flex-col gap-4">
3538
<GlobalTPS size="sm" titleAlign="left" />
3639
<Card className="text-white py-3 px-4 text-sm leading-3">

0 commit comments

Comments
 (0)