فهرست منبع

added loader cache, removed directory

ncannasse 12 سال پیش
والد
کامیت
0701013fa7
3فایلهای تغییر یافته به همراه44 افزوده شده و 52 حذف شده
  1. 16 9
      hxd/res/Any.hx
  2. 0 17
      hxd/res/Directory.hx
  3. 28 26
      hxd/res/Loader.hx

+ 16 - 9
hxd/res/Any.hx

@@ -1,26 +1,33 @@
 package hxd.res;
 
+@:access(hxd.res.Loader)
 class Any extends Resource {
-		
+
+	var loader : Loader;
+	
+	public function new(loader, entry) {
+		super(entry);
+		this.loader = loader;
+	}
+	
 	public function toFbx() {
-		return new Model(entry).toFbx();
+		return loader.loadModel(entry.path).toFbx();
 	}
 
 	public function toTexture() {
-		return new Texture(entry).toTexture();
+		return loader.loadTexture(entry.path).toTexture();
 	}
 	
 	public function toTile() {
-		return new Texture(entry).toTile();
+		return loader.loadTexture(entry.path).toTile();
 	}
 
 	public function toSound() {
-		return new Sound(entry);
+		return loader.loadSound(entry.path);
 	}
 	
-	public function toDir() {
-		if( !entry.isDirectory ) throw entry.path + " is not a directory ";
-		return new Directory(entry);
+	public inline function iterator() {
+		return new hxd.impl.ArrayIterator([for( f in entry ) new Any(loader,f)]);
 	}
-	
+
 }

+ 0 - 17
hxd/res/Directory.hx

@@ -1,17 +0,0 @@
-package hxd.res;
-
-class Directory extends Resource {
-	
-	public inline function iterator() {
-		return new hxd.impl.ArrayIterator([for( f in entry ) new Any(f)]);
-	}
-	
-	public function exists( name : String ) {
-		return entry.exists(name);
-	}
-
-	public function get( name : String ) {
-		return new Any(entry.get(name));
-	}
-
-}

+ 28 - 26
hxd/res/Loader.hx

@@ -3,52 +3,54 @@ package hxd.res;
 class Loader {
 	
 	var fs : FileSystem;
+	var modelCache : Map<String,Model>;
+	var textureCache : Map<String,Texture>;
+	var soundCache : Map<String,Sound>;
 	
 	public function new(fs) {
 		this.fs = fs;
+		modelCache = new Map();
+		textureCache = new Map();
+		soundCache = new Map();
 	}
 
 	public function exists( path : String ) : Bool {
 		return fs.exists(path);
 	}
 	
-	function resolveDynamic( path : String ) : Dynamic {
-		var extParts = path.split(".");
-		extParts.shift();
-		var ext = extParts.join(".").toLowerCase();
-		switch( ext ) {
-		case "fbx", "xbx": return loadModel(path);
-		case "png", "jpg": return loadTexture(path);
-		case "ttf": return loadFont(path);
-		case "wav", "mp3": return loadSound(path);
-		case "":
-			var f = fs.get(path);
-			if( f.isDirectory )
-				return new Directory(f);
-		default:
-		};
-		throw "Unknown extension " + ext;
-		return null;
-	}
-	
 	public function load( path : String ) : Any {
-		return new Any(fs.get(path));
+		return new Any(this, fs.get(path));
 	}
 	
 	function loadModel( path : String ) : Model {
-		return new Model(fs.get(path));
+		var m = modelCache.get(path);
+		if( m == null ) {
+			m = new Model(fs.get(path));
+			modelCache.set(path, m);
+		}
+		return m;
 	}
 	
 	function loadTexture( path : String ) : Texture {
-		return new Texture(fs.get(path));
+		var t = textureCache.get(path);
+		if( t == null ) {
+			t = new Texture(fs.get(path));
+			textureCache.set(path, t);
+		}
+		return t;
 	}
 	
+	function loadSound( path : String ) : Sound {
+		var s = soundCache.get(path);
+		if( s == null ) {
+			s = new Sound(fs.get(path));
+			soundCache.set(path, s);
+		}
+		return s;
+	}
+
 	function loadFont( path : String ) : Font {
 		return new Font(fs.get(path));
 	}
 	
-	function loadSound( path : String ) : Sound {
-		return new Sound(fs.get(path));
-	}
-
 }