Added asset manager.
This commit is contained in:
@ -29,7 +29,8 @@ const outputFix = (output, dir) => {
|
|||||||
let match;
|
let match;
|
||||||
while(!(match = matches.next()).done) {
|
while(!(match = matches.next()).done) {
|
||||||
const exp = new RegExp(`${match.value[1]}\.`, 'gm');
|
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.');
|
out = out.replace(match.value[0], '').replace(exp, 'exports.');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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) {
|
||||||
|
}
|
||||||
|
}
|
@ -1,3 +1,4 @@
|
|||||||
|
import { AssetManager } from "../asset/AssetManager";
|
||||||
import { Scene } from "../scene/Scene";
|
import { Scene } from "../scene/Scene";
|
||||||
|
|
||||||
type Time = {
|
type Time = {
|
||||||
@ -9,9 +10,11 @@ type Time = {
|
|||||||
export class Game {
|
export class Game {
|
||||||
public time:Time;
|
public time:Time;
|
||||||
public scene:Scene|null;
|
public scene:Scene|null;
|
||||||
|
public assets:AssetManager;
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
this.time = { delta: 0, current: 0, last: 0 };
|
this.time = { delta: 0, current: 0, last: 0 };
|
||||||
|
this.assets = new AssetManager();
|
||||||
this.scene = null;
|
this.scene = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
10
ts/main.ts
10
ts/main.ts
@ -14,18 +14,14 @@ class TestScene extends Scene {
|
|||||||
1, 1, 1, 1
|
1, 1, 1, 1
|
||||||
);
|
);
|
||||||
|
|
||||||
this.shader = shaderCreate();
|
this.shader = this.game.assets.shaderLoad(
|
||||||
assetShaderLoad(this.shader,
|
"shaders/textured.vert", "shaders/textured.frag", this
|
||||||
"shaders/textured.vert",
|
|
||||||
"shaders/textured.frag"
|
|
||||||
);
|
);
|
||||||
|
this.texture = this.game.assets.textureLoad("test_texture.png", this);
|
||||||
|
|
||||||
this.camera = cameraCreate();
|
this.camera = cameraCreate();
|
||||||
cameraLookAt(this.camera, 3,3,3, 0,0,0);
|
cameraLookAt(this.camera, 3,3,3, 0,0,0);
|
||||||
cameraPerspective(this.camera, 45, 16/9, 0.01, 1000);
|
cameraPerspective(this.camera, 45, 16/9, 0.01, 1000);
|
||||||
|
|
||||||
this.texture = textureCreate();
|
|
||||||
assetTextureLoad(this.texture, "test_texture.png");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
update() {
|
update() {
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
|
import { IAssetOwner } from "../asset/AssetManager";
|
||||||
import { Game } from "../game/Game";
|
import { Game } from "../game/Game";
|
||||||
|
|
||||||
export abstract class Scene {
|
export abstract class Scene implements IAssetOwner {
|
||||||
public readonly game:Game;
|
public readonly game:Game;
|
||||||
|
|
||||||
constructor(game:Game) {
|
constructor(game:Game) {
|
||||||
|
Reference in New Issue
Block a user