Added asset manager.

This commit is contained in:
2021-09-26 00:42:35 -07:00
parent 3a94e307d4
commit 4a57adf427
5 changed files with 49 additions and 9 deletions

View File

@ -29,7 +29,8 @@ const outputFix = (output, dir) => {
let match;
while(!(match = matches.next()).done) {
const exp = new RegExp(`${match.value[1]}\.`, 'gm');
scanFile(path.join(dir, match.value[2]), dir);
const filePath = path.join(dir, match.value[2]);
scanFile(filePath, path.dirname(filePath));
out = out.replace(match.value[0], '').replace(exp, 'exports.');
}

View File

@ -0,0 +1,39 @@
export interface IAssetOwner {
}
export class AssetManager {
private owners:{ [key:string]:IAssetOwner[] };
private textures:{ [key:string]:Texture };
private shaders:{ [key:string]:Shader };
constructor() {
this.owners = {};
this.textures = {};
this.shaders = {};
}
private addOwner(name:string, owner:IAssetOwner) {
this.owners[name] = this.owners[name] || [];
this.owners[name].push(owner);
}
public textureLoad(name:string, owner:IAssetOwner):Texture {
this.addOwner(name, owner);
if(this.textures[name]) return this.textures[name];
this.textures[name] = textureCreate();
assetTextureLoad(this.textures[name], name);
return this.textures[name];
}
public shaderLoad(vert:string, frag:string, owner:IAssetOwner):Shader {
this.addOwner(vert, owner);
if(this.shaders[vert]) return this.shaders[vert];
this.shaders[vert] = shaderCreate();
assetShaderLoad(this.shaders[vert], vert, frag);
return this.shaders[vert];
}
public releaseOwner(owner:IAssetOwner) {
}
}

View File

@ -1,3 +1,4 @@
import { AssetManager } from "../asset/AssetManager";
import { Scene } from "../scene/Scene";
type Time = {
@ -9,9 +10,11 @@ type Time = {
export class Game {
public time:Time;
public scene:Scene|null;
public assets:AssetManager;
constructor() {
this.time = { delta: 0, current: 0, last: 0 };
this.assets = new AssetManager();
this.scene = null;
}

View File

@ -14,18 +14,14 @@ class TestScene extends Scene {
1, 1, 1, 1
);
this.shader = shaderCreate();
assetShaderLoad(this.shader,
"shaders/textured.vert",
"shaders/textured.frag"
this.shader = this.game.assets.shaderLoad(
"shaders/textured.vert", "shaders/textured.frag", this
);
this.texture = this.game.assets.textureLoad("test_texture.png", this);
this.camera = cameraCreate();
cameraLookAt(this.camera, 3,3,3, 0,0,0);
cameraPerspective(this.camera, 45, 16/9, 0.01, 1000);
this.texture = textureCreate();
assetTextureLoad(this.texture, "test_texture.png");
}
update() {

View File

@ -1,6 +1,7 @@
import { IAssetOwner } from "../asset/AssetManager";
import { Game } from "../game/Game";
export abstract class Scene {
export abstract class Scene implements IAssetOwner {
public readonly game:Game;
constructor(game:Game) {