|
1 | 1 |
|
2 | | -# @benev/marduk |
| 2 | +# MARDUK |
| 3 | + |
| 4 | +**Marduk** is a [BabylonJS](https://www.babylonjs.com/) framework for rendering good web games. |
| 5 | +- 🧵 **Multithreaded** — rendering happens in its own thread |
| 6 | +- 🎳 **Smart architecture** — logic and rendering happen on separate threads |
| 7 | + |
| 8 | +<br/> |
| 9 | + |
| 10 | +## Install Marduk |
| 11 | + |
| 12 | +```sh |
| 13 | +npm install @benev/marduk |
| 14 | +``` |
| 15 | + |
| 16 | +<br/> |
| 17 | + |
| 18 | +## Theater |
| 19 | + |
| 20 | +The theater coordinates your rendering thread and displays the results onto a canvas. |
| 21 | + |
| 22 | +The theater is split into two main parts. Each will live in its own separate bundle. |
| 23 | +- *The Frontstage* hosts the visible canvas in the dom. It doesn't import Babylon, and will be lean in filesize. |
| 24 | +- *The Backstage* run inside a web worker, and performs rendering work. It imports Babylon, and will be fat in filesize. |
| 25 | + |
| 26 | +### Theater project setup |
| 27 | +1. Write your `spec.ts` type, which describes your renderable world |
| 28 | + ```ts |
| 29 | + export type MySpec = AsFigmentSpec<{ |
| 30 | + |
| 31 | + // Describe all the renderable objects in your world, |
| 32 | + // in terms of serializable data. |
| 33 | + hippo: {hungry: boolean} |
| 34 | + }> |
| 35 | + ``` |
| 36 | +1. Establish your `backstage.ts` module (this will be a web worker) |
| 37 | + ```ts |
| 38 | + import {MySpec} from "./spec.js" |
| 39 | + import {theaterWorker, babylonBackstage} from "@benev/marduk" |
| 40 | + |
| 41 | + void async function() { |
| 42 | + await theaterWorker( |
| 43 | + await babylonBackstage<MySpec>(async stagecraft => ({ |
| 44 | + |
| 45 | + // now we define a renderer for this object |
| 46 | + hippo: (id, data) => { |
| 47 | + console.log("spawn", id, data) |
| 48 | + return { |
| 49 | + update: data => console.log("update", id, data), |
| 50 | + dispose: () => console.log("dispose", id, data), |
| 51 | + } |
| 52 | + }, |
| 53 | + })) |
| 54 | + ) |
| 55 | + }() |
| 56 | + ``` |
| 57 | + - now bundle this module as `backstage.bundle.js` using `rollup`, `esbuild`, `parcel`, `vite`, or whatever |
| 58 | +1. Establish your `frontstage.ts` module (this will be your app's main entrypoint) |
| 59 | + ```ts |
| 60 | + import {MySpec} from "./spec.js" |
| 61 | + import {theaterHost, Frontstage, theaterElement, register} from "@benev/marduk/x/theater/index.js" |
| 62 | +
|
| 63 | + void async function() { |
| 64 | +
|
| 65 | + // relative url to the backstage web worker bundle |
| 66 | + const workerUrl = new URL("./backstage.bundle.js", import.meta.url) |
| 67 | +
|
| 68 | + // setup the frontstage |
| 69 | + const frontstage = new Frontstage( |
| 70 | + await theaterHost<MySpec>({workerUrl}) |
| 71 | + ) |
| 72 | +
|
| 73 | + // create the <marduk-theater> web component |
| 74 | + const MardukTheater = theaterElement(frontstage) |
| 75 | +
|
| 76 | + // register to the dom |
| 77 | + register({MardukTheater}) |
| 78 | +
|
| 79 | + // okay, now you're ready to manipulate your world |
| 80 | +
|
| 81 | + // spawn a hippo |
| 82 | + await theater.backstage.setFigments([[0, ["hippo", {hungry: false}]]]) |
| 83 | +
|
| 84 | + // update the hippo |
| 85 | + await theater.backstage.setFigments([[0, ["hippo", {hungry: true}]]]) |
| 86 | +
|
| 87 | + // delete the hippo |
| 88 | + await theater.backstage.setFigments([[0, undefined]]) |
| 89 | + }() |
| 90 | + ``` |
| 91 | + - now bundle this module as `frontstage.bundle.js` |
| 92 | +1. Load the frontstage onto your web page |
| 93 | + ```html |
| 94 | + <script type=module src="./frontstage.bundle.js"></script> |
| 95 | + ``` |
| 96 | +1. Place the `<marduk-theater>` element in your html body |
| 97 | + ```html |
| 98 | + <marduk-theater></marduk-theater> |
| 99 | + ``` |
3 | 100 |
|
0 commit comments