outsource async fetching

This commit is contained in:
Emile Clark-Boman 2026-02-02 05:02:18 +10:00
parent 618a3aec23
commit 1683d2bbe9
3 changed files with 42 additions and 30 deletions

View file

@ -2,18 +2,31 @@ import { Smc } from "./smc/smc.js"
main(); main();
async function fetchShader(uri, delegate) {
const res = await fetch(uri);
if (res.ok)
return await res.text();
this.raiseError(
SmcErr.FETCH_SHADER,
`Failed to load shader source ${url}: ${res.status} ${res.json()}`);
return ""
}
function main() { function main() {
const canvas = document.querySelector("#gl-canvas"); const canvas = document.querySelector("#gl-canvas");
canvas.setAttribute('width', window.innerWidth); canvas.setAttribute('width', window.innerWidth);
canvas.setAttribute('height', window.innerHeight); canvas.setAttribute('height', window.innerHeight);
new Smc(canvas)
.setMaxFps(30) fetchShader("../shaders/segfault.glsl")
.setProgram(builder => .then(frag =>
builder new Smc(canvas)
// .fetchVertexShader("../shaders/segfault.glsl") .setMaxFps(30)
// .fetchFragmentShader("../shaders/segfault.glsl")) .setProgram(builder =>
) builder
.run(); .addFragmentShader(frag))
.run()
);
} }

View file

@ -71,19 +71,21 @@ class SmcProgramBuilder {
source source
) )
) )
console.log(source)
this.#hasFragmentShader = true; this.#hasFragmentShader = true;
return this; return this;
} }
fetchVertexShader(uri) { // fetchVertexShader(uri) {
this.#fetchShader(uri, (source) => this.addVertexShader(source)); // (async () => this.#fetchShader(uri, (source) => this.addVertexShader(source)))();
return this; // return this;
} // }
fetchFragmentShader(uri) { // async fetchFragmentShader(uri) {
this.#fetchShader(uri, (source) => this.addFragmentShader(source)); // var delegate = (source) => this.addFragmentShader(source);
return this; // var source = await this.#fetchShader(uri);
} // return this;
// }
build() { build() {
// avoid user accidental calls to build() // avoid user accidental calls to build()
@ -117,18 +119,15 @@ class SmcProgramBuilder {
return shader; return shader;
} }
#fetchShader(uri, delegate) { // async #fetchShader(uri, delegate) {
return fetch(uri) // const res = await fetch(uri);
.then(res => { // if (res.ok)
if (res.ok) // return await res.text();
delegate(res.text()); // this.raiseError(
else { // SmcErr.FETCH_SHADER,
this.raiseError( // `Failed to load shader source ${url}: ${res.status} ${res.json()}`);
SmcErr.FETCH_SHADER, // return ""
`Failed to load shader source ${url}: ${res.status} ${res.json()}`); // }
}
});
}
} }

View file

@ -127,9 +127,9 @@ class Smc {
setProgram(delegate) { setProgram(delegate) {
const builder = new SmcProgramBuilder(this.#gl, this.raiseError); const builder = new SmcProgramBuilder(this.#gl, this.raiseError);
delegate(builder); // i pray js passes by ref well... var result = delegate(builder);
this.#program = builder.build(); this.#program = result.build();
if (!this.#gl.getProgramParameter(this.#program, this.#gl.LINK_STATUS)) { if (!this.#gl.getProgramParameter(this.#program, this.#gl.LINK_STATUS)) {
const infoLog = this.#gl.getProgramInfoLog(this.#program); const infoLog = this.#gl.getProgramInfoLog(this.#program);
this.raiseError( this.raiseError(