From 4a57adf42722bf7c3df69245ed15c3026eee6103 Mon Sep 17 00:00:00 2001 From: Dominic Masters Date: Sun, 26 Sep 2021 00:42:35 -0700 Subject: [PATCH] Added asset manager. --- scripts/bundle.js | 3 ++- ts/asset/AssetManager.ts | 39 +++++++++++++++++++++++++++++++++++++++ ts/game/Game.ts | 3 +++ ts/main.ts | 10 +++------- ts/scene/Scene.ts | 3 ++- 5 files changed, 49 insertions(+), 9 deletions(-) diff --git a/scripts/bundle.js b/scripts/bundle.js index a3dac80a..13f75a62 100644 --- a/scripts/bundle.js +++ b/scripts/bundle.js @@ -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.'); } diff --git a/ts/asset/AssetManager.ts b/ts/asset/AssetManager.ts index e69de29b..3ae18f9a 100644 --- a/ts/asset/AssetManager.ts +++ b/ts/asset/AssetManager.ts @@ -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) { + } +} \ No newline at end of file diff --git a/ts/game/Game.ts b/ts/game/Game.ts index 229deb74..86929a1e 100644 --- a/ts/game/Game.ts +++ b/ts/game/Game.ts @@ -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; } diff --git a/ts/main.ts b/ts/main.ts index 672b81d2..3a4c2ee5 100644 --- a/ts/main.ts +++ b/ts/main.ts @@ -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() { diff --git a/ts/scene/Scene.ts b/ts/scene/Scene.ts index f100e4a9..83df2009 100644 --- a/ts/scene/Scene.ts +++ b/ts/scene/Scene.ts @@ -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) {