Skip to content

Commit

Permalink
JSYSTEM: Introducing 'ResourceResolver' concept for resources which r…
Browse files Browse the repository at this point in the history
…eference other resources

J2D BLO files reference other texture resources. The games need a way of resolving these references into the actual resources. A ResourceResolver is a simple callback of the form:

`ResourceResolver = (resType: string, resName: string) => any`

Note that the arc which contains those resources is not passed directly to the callback, because the BLO file does not know which arc it is being loaded from. Instead, the resolver is expected to have been created with the arc already specified. E.g. In Wind Waker this is handled by `dResControl_c.getResResolver(arcName: string): ResourceResolver` which returns a resolver function for the specified arc.

Passing the JKRArchive directly to the BLO was also considered. But in the case of Wind Waker, the BTI images that are referenced have already been loaded by dResCtrl_c, so having the BLO create them again would result in duplicate resources. The callback seems to be the best way to resolve this.
  • Loading branch information
themikelester committed Dec 28, 2024
1 parent 8c85c4b commit 5c3eb88
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 9 deletions.
26 changes: 21 additions & 5 deletions src/Common/JSYSTEM/J2Dv1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { GfxClipSpaceNearZ, GfxDevice } from "../../gfx/platform/GfxPlatform.js"
import { computeModelMatrixT, projectionMatrixForCuboid } from "../../MathHelpers.js";
import { projectionMatrixConvertClipSpaceNearZ } from "../../gfx/helpers/ProjectionHelpers.js";
import { TSDraw } from "../../SuperMarioGalaxy/DDraw.js";
import { BTIData } from "./JUTTexture.js";
import { BTI, BTIData } from "./JUTTexture.js";
import { GXMaterialBuilder } from "../../gx/GXMaterialBuilder.js";
import { mat4, vec2, vec4 } from "gl-matrix";
import { GfxRenderCache } from "../../gfx/render/GfxRenderCache.js";
Expand All @@ -24,6 +24,9 @@ const drawParams = new DrawParams();

const scratchMat = mat4.create();

// TODO: Find a better home for this
export type ResourceResolver = (resType: string, resName: string) => any | null;

interface ResRef {
refType: number;
resType: string;
Expand Down Expand Up @@ -316,6 +319,12 @@ export class J2DPane {
computeModelMatrixT(this.drawMtx, this.drawPos[0], this.drawPos[1], 0);
}
}

protected resolveReferences(resolver: ResourceResolver) {
for(let child of this.children) {
child.resolveReferences(resolver);
}
}
}
//#endregion

Expand All @@ -329,10 +338,7 @@ export class J2DPicture extends J2DPane {

constructor(data: PAN1, private cache: GfxRenderCache, parent: J2DPane | null) {
super(data, cache, parent);
// @TODO: If type > 4, load the image on construction
if (this.data.timg.refType !== 0 && this.data.timg.refType !== 2) { console.warn('Untested J2D feature'); }

if (this.data.tlut.refType !== 0) { console.warn('Untested J2D feature'); }
if (this.data.uvBinding !== 15) { console.warn('Untested J2D feature'); }
if (this.data.flags !== 0) { console.warn('Untested J2D feature'); }
if (this.data.colorBlack !== 0 || this.data.colorWhite !== 0xFFFFFFFF) { console.warn('Untested J2D feature'); }
Expand Down Expand Up @@ -448,16 +454,26 @@ export class J2DPicture extends J2DPane {
mb.setUsePnMtxIdx(false);
this.materialHelper = new GXMaterialHelperGfx(mb.finish());
}

protected override resolveReferences(resolver: ResourceResolver) {
if(this.data.timg.refType > 1) {
if (this.data.timg.refType !== 2) { console.warn('Untested J2D feature'); }
this.tex = resolver(this.data.timg.resType, this.data.timg.resName);
if (this.tex) { this.prepare(); }
}
assert(this.data.tlut.refType === 0, 'TLUT references currently unsupported');
}
}
//#endregion

//#region J2DScreen
export class J2DScreen extends J2DPane {
public color: Color

constructor(data: SCRN, cache: GfxRenderCache) {
constructor(data: SCRN, cache: GfxRenderCache, resolver: ResourceResolver) {
super(data, cache, null);
this.color = data.color;
this.resolveReferences(resolver);
}

public override draw(renderInstManager: GfxRenderInstManager, viewerRenderInput: ViewerRenderInput, ctx2D: J2DGrafContext, offsetX?: number, offsetY?: number): void {
Expand Down
4 changes: 2 additions & 2 deletions src/ZeldaWindWaker/d_place_name.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { GfxRenderInstManager } from "../gfx/render/GfxRenderInstManager.js";
import { ViewerRenderInput } from "../viewer.js";
import { EDemoMode } from "./d_demo.js";
import { dProcName_e } from "./d_procname.js";
import { dComIfG_resLoad, ResType } from "./d_resorce.js";
import { dComIfG_resLoad, dRes_control_c, ResType } from "./d_resorce.js";
import { cPhs__Status, fGlobals, fopMsgM_Delete, fpc_bs__Constructor, fpcPf__Register, fpcSCtRq_Request, msg_class } from "./framework.js";
import { dGlobals } from "./Main.js";

Expand Down Expand Up @@ -103,7 +103,7 @@ export class d_place_name extends msg_class {
img = new BTIData(globals.sceneContext.device, globals.renderer.renderCache, BTI.parse(imgData, filename).texture);
}

this.screen = new J2DScreen(screen, globals.renderer.renderCache);
this.screen = new J2DScreen(screen, globals.renderer.renderCache, globals.resCtrl.getResResolver('PName'));
this.screen.children[0].children[0].data.visible = false;
this.screen.children[0].children[1].data.visible = false;
const pic = this.screen.children[0].children[2] as J2DPicture;
Expand Down
15 changes: 13 additions & 2 deletions src/ZeldaWindWaker/d_resorce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { dGlobals } from "./Main.js";
import { cPhs__Status } from "./framework.js";
import { cBgD_t } from "./d_bg.js";
import { NamedArrayBufferSlice } from "../DataFetcher.js";
import { BLO, SCRN } from "../Common/JSYSTEM/J2Dv1.js";
import { BLO, ResourceResolver, SCRN } from "../Common/JSYSTEM/J2Dv1.js";

export interface DZSChunkHeader {
type: string;
Expand Down Expand Up @@ -113,7 +113,18 @@ export class dRes_control_c {
public getResByID<T extends ResType>(resType: T, arcName: string, resID: number, resList: dRes_info_c[]): ResAssetType<T> {
const resInfo = assertExists(this.findResInfo(arcName, resList));
return resInfo.getResByID(resType, resID);
}
}

public getResResolver(arcName: string): ResourceResolver {
return (resType: string, resName: string) => {
switch(resType) {
case 'TIMG': return this.getObjectResByName(ResType.Bti, arcName, resName);
case 'TLUT': console.warn('TLUT resource references not yet supported'); debugger; return null;
case 'FONT': console.warn('FONT resource references not yet supported'); debugger; return null;
default: return null;
}
}
}

public mountRes(device: GfxDevice, cache: GfxRenderCache, arcName: string, archive: JKRArchive, resList: dRes_info_c[]): void {
if (this.findResInfo(arcName, resList) !== null)
Expand Down

0 comments on commit 5c3eb88

Please sign in to comment.